Blame view

drivers/base/bus.c 31.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
  /*
   * bus.c - bus driver management
   *
   * Copyright (c) 2002-3 Patrick Mochel
   * Copyright (c) 2002-3 Open Source Development Labs
e5dd12784   Greg Kroah-Hartman   Driver core: move...
6
7
   * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
   * Copyright (c) 2007 Novell Inc.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
11
   *
   * This file is released under the GPLv2
   *
   */
765230b5f   Dmitry Torokhov   driver-core: add ...
12
  #include <linux/async.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
  #include <linux/device.h>
  #include <linux/module.h>
  #include <linux/errno.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
16
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
  #include <linux/init.h>
  #include <linux/string.h>
ca22e56de   Kay Sievers   driver-core: impl...
19
  #include <linux/mutex.h>
639676856   Greg Kroah-Hartman   driver core: add ...
20
  #include <linux/sysfs.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
  #include "base.h"
  #include "power/power.h"
ca22e56de   Kay Sievers   driver-core: impl...
23
  /* /sys/devices/system */
97ec448ae   H Hartley Sweeten   drivers/base/bus....
24
  static struct kset *system_kset;
ca22e56de   Kay Sievers   driver-core: impl...
25

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
30
31
32
  
  /*
   * sysfs bindings for drivers
   */
  
  #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33

b8c5cec23   Kay Sievers   Driver core: udev...
34
35
  static int __must_check bus_rescan_devices_helper(struct device *dev,
  						void *data);
5901d0145   Greg Kroah-Hartman   Driver core: remo...
36
37
  static struct bus_type *bus_get(struct bus_type *bus)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
38
39
40
41
42
  	if (bus) {
  		kset_get(&bus->p->subsys);
  		return bus;
  	}
  	return NULL;
5901d0145   Greg Kroah-Hartman   Driver core: remo...
43
  }
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
44
45
  static void bus_put(struct bus_type *bus)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
46
47
  	if (bus)
  		kset_put(&bus->p->subsys);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
48
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
49
50
  static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
  			     char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
52
  	struct driver_attribute *drv_attr = to_drv_attr(attr);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
53
  	struct driver_private *drv_priv = to_driver(kobj);
4a0c20bf8   Dmitry Torokhov   [PATCH] sysfs: (d...
54
  	ssize_t ret = -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
  
  	if (drv_attr->show)
e5dd12784   Greg Kroah-Hartman   Driver core: move...
57
  		ret = drv_attr->show(drv_priv->driver, buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
59
  	return ret;
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
60
61
  static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
  			      const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
63
  	struct driver_attribute *drv_attr = to_drv_attr(attr);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
64
  	struct driver_private *drv_priv = to_driver(kobj);
4a0c20bf8   Dmitry Torokhov   [PATCH] sysfs: (d...
65
  	ssize_t ret = -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
67
  
  	if (drv_attr->store)
e5dd12784   Greg Kroah-Hartman   Driver core: move...
68
  		ret = drv_attr->store(drv_priv->driver, buf, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
  	return ret;
  }
52cf25d0a   Emese Revfy   Driver core: Cons...
71
  static const struct sysfs_ops driver_sysfs_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
  	.show	= drv_attr_show,
  	.store	= drv_attr_store,
  };
e5dd12784   Greg Kroah-Hartman   Driver core: move...
75
  static void driver_release(struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
  {
e5dd12784   Greg Kroah-Hartman   Driver core: move...
77
  	struct driver_private *drv_priv = to_driver(kobj);
2b3a302a0   Harvey Harrison   driver core: repl...
78
79
  	pr_debug("driver: '%s': %s
  ", kobject_name(kobj), __func__);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
80
  	kfree(drv_priv);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
  }
a1148fb03   Greg Kroah-Hartman   Driver core: rena...
82
  static struct kobj_type driver_ktype = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
84
85
  	.sysfs_ops	= &driver_sysfs_ops,
  	.release	= driver_release,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
  /*
   * sysfs bindings for buses
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
89
90
  static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
  			     char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
92
  	struct bus_attribute *bus_attr = to_bus_attr(attr);
6b6e39a6a   Kay Sievers   driver-core: merg...
93
  	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
96
  	ssize_t ret = 0;
  
  	if (bus_attr->show)
6b6e39a6a   Kay Sievers   driver-core: merg...
97
  		ret = bus_attr->show(subsys_priv->bus, buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
  	return ret;
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
100
101
  static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
  			      const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
103
  	struct bus_attribute *bus_attr = to_bus_attr(attr);
6b6e39a6a   Kay Sievers   driver-core: merg...
104
  	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
107
  	ssize_t ret = 0;
  
  	if (bus_attr->store)
6b6e39a6a   Kay Sievers   driver-core: merg...
108
  		ret = bus_attr->store(subsys_priv->bus, buf, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
  	return ret;
  }
52cf25d0a   Emese Revfy   Driver core: Cons...
111
  static const struct sysfs_ops bus_sysfs_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
114
  	.show	= bus_attr_show,
  	.store	= bus_attr_store,
  };
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
115
  int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
  {
  	int error;
5901d0145   Greg Kroah-Hartman   Driver core: remo...
118
  	if (bus_get(bus)) {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
119
  		error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
120
  		bus_put(bus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
124
  	} else
  		error = -EINVAL;
  	return error;
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
125
  EXPORT_SYMBOL_GPL(bus_create_file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126

4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
127
  void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
  {
5901d0145   Greg Kroah-Hartman   Driver core: remo...
129
  	if (bus_get(bus)) {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
130
  		sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
131
  		bus_put(bus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
133
  	}
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
134
  EXPORT_SYMBOL_GPL(bus_remove_file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135

174be70b6   Bart Van Assche   driver-core: Fix ...
136
137
  static void bus_release(struct kobject *kobj)
  {
371fd7a2c   Geliang Tang   driver core: bus:...
138
  	struct subsys_private *priv = to_subsys_private(kobj);
174be70b6   Bart Van Assche   driver-core: Fix ...
139
140
141
142
143
  	struct bus_type *bus = priv->bus;
  
  	kfree(priv);
  	bus->p = NULL;
  }
80f03e349   Kay Sievers   Driver core: add ...
144
  static struct kobj_type bus_ktype = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  	.sysfs_ops	= &bus_sysfs_ops,
174be70b6   Bart Van Assche   driver-core: Fix ...
146
  	.release	= bus_release,
80f03e349   Kay Sievers   Driver core: add ...
147
148
149
150
151
152
153
154
155
156
  };
  
  static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
  {
  	struct kobj_type *ktype = get_ktype(kobj);
  
  	if (ktype == &bus_ktype)
  		return 1;
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157

9cd43611c   Emese Revfy   kobject: Constify...
158
  static const struct kset_uevent_ops bus_uevent_ops = {
80f03e349   Kay Sievers   Driver core: add ...
159
  	.filter = bus_uevent_filter,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
  };
59a548338   Greg Kroah-Hartman   kset: convert dri...
161
  static struct kset *bus_kset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162

2b08c8d04   Alan Stern   [PATCH] Small fix...
163
  /* Manually detach a device from its associated driver. */
2581c9cc0   Greg Kroah-Hartman   driver core: bus:...
164
165
  static ssize_t unbind_store(struct device_driver *drv, const char *buf,
  			    size_t count)
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
166
  {
5901d0145   Greg Kroah-Hartman   Driver core: remo...
167
  	struct bus_type *bus = bus_get(drv->bus);
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
168
169
  	struct device *dev;
  	int err = -ENODEV;
1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
170
  	dev = bus_find_device_by_name(bus, NULL, buf);
2b08c8d04   Alan Stern   [PATCH] Small fix...
171
  	if (dev && dev->driver == drv) {
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
172
  		if (dev->parent)	/* Needed for USB */
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
173
  			device_lock(dev->parent);
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
174
  		device_release_driver(dev);
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
175
  		if (dev->parent)
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
176
  			device_unlock(dev->parent);
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
177
178
  		err = count;
  	}
2b08c8d04   Alan Stern   [PATCH] Small fix...
179
  	put_device(dev);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
180
  	bus_put(bus);
2b08c8d04   Alan Stern   [PATCH] Small fix...
181
  	return err;
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
182
  }
2581c9cc0   Greg Kroah-Hartman   driver core: bus:...
183
  static DRIVER_ATTR_WO(unbind);
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
184

afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
185
186
187
188
189
  /*
   * Manually attach a device to a driver.
   * Note: the driver must want to bind to the device,
   * it is not possible to override the driver's id table.
   */
2581c9cc0   Greg Kroah-Hartman   driver core: bus:...
190
191
  static ssize_t bind_store(struct device_driver *drv, const char *buf,
  			  size_t count)
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
192
  {
5901d0145   Greg Kroah-Hartman   Driver core: remo...
193
  	struct bus_type *bus = bus_get(drv->bus);
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
194
195
  	struct device *dev;
  	int err = -ENODEV;
1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
196
  	dev = bus_find_device_by_name(bus, NULL, buf);
49b420a13   Ming Lei   driver core: chec...
197
  	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
198
  		if (dev->parent)	/* Needed for USB */
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
199
200
  			device_lock(dev->parent);
  		device_lock(dev);
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
201
  		err = driver_probe_device(drv, dev);
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
202
  		device_unlock(dev);
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
203
  		if (dev->parent)
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
204
  			device_unlock(dev->parent);
372254018   Ryan Wilson   [PATCH] driver co...
205

4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
206
207
  		if (err > 0) {
  			/* success */
372254018   Ryan Wilson   [PATCH] driver co...
208
  			err = count;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
209
210
  		} else if (err == 0) {
  			/* driver didn't accept device */
372254018   Ryan Wilson   [PATCH] driver co...
211
  			err = -ENODEV;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
212
  		}
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
213
  	}
2b08c8d04   Alan Stern   [PATCH] Small fix...
214
  	put_device(dev);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
215
  	bus_put(bus);
2b08c8d04   Alan Stern   [PATCH] Small fix...
216
  	return err;
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
217
  }
2581c9cc0   Greg Kroah-Hartman   driver core: bus:...
218
  static DRIVER_ATTR_WO(bind);
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
219

b8c5cec23   Kay Sievers   Driver core: udev...
220
221
  static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
222
223
  	return sprintf(buf, "%d
  ", bus->p->drivers_autoprobe);
b8c5cec23   Kay Sievers   Driver core: udev...
224
225
226
227
228
229
  }
  
  static ssize_t store_drivers_autoprobe(struct bus_type *bus,
  				       const char *buf, size_t count)
  {
  	if (buf[0] == '0')
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
230
  		bus->p->drivers_autoprobe = 0;
b8c5cec23   Kay Sievers   Driver core: udev...
231
  	else
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
232
  		bus->p->drivers_autoprobe = 1;
b8c5cec23   Kay Sievers   Driver core: udev...
233
234
235
236
237
238
239
  	return count;
  }
  
  static ssize_t store_drivers_probe(struct bus_type *bus,
  				   const char *buf, size_t count)
  {
  	struct device *dev;
0372ffb35   Alex Williamson   driver core: Fix ...
240
  	int err = -EINVAL;
b8c5cec23   Kay Sievers   Driver core: udev...
241

1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
242
  	dev = bus_find_device_by_name(bus, NULL, buf);
b8c5cec23   Kay Sievers   Driver core: udev...
243
244
  	if (!dev)
  		return -ENODEV;
0372ffb35   Alex Williamson   driver core: Fix ...
245
246
247
248
  	if (bus_rescan_devices_helper(dev, NULL) == 0)
  		err = count;
  	put_device(dev);
  	return err;
b8c5cec23   Kay Sievers   Driver core: udev...
249
  }
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
250

4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
251
  static struct device *next_device(struct klist_iter *i)
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
252
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
253
  	struct klist_node *n = klist_next(i);
ae1b41715   Greg Kroah-Hartman   driver core: move...
254
255
256
257
258
259
260
261
  	struct device *dev = NULL;
  	struct device_private *dev_prv;
  
  	if (n) {
  		dev_prv = to_device_private_bus(n);
  		dev = dev_prv->device;
  	}
  	return dev;
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
262
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
264
265
266
267
268
   * bus_for_each_dev - device iterator.
   * @bus: bus type.
   * @start: device to start iterating from.
   * @data: data for the callback.
   * @fn: function to be called for each device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
270
271
272
   * Iterate over @bus's list of devices, and call @fn for each,
   * passing it @data. If @start is not NULL, we use that device to
   * begin iterating from.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
274
275
   * We check the return of @fn each time. If it returns anything
   * other than 0, we break out and return that value.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
277
278
   * NOTE: The device that returns a non-zero value is not retained
   * in any way, nor is its refcount incremented. If the caller needs
0fa1b0a14   Alex Chiang   trivial: fix gram...
279
   * to retain this data, it should do so, and increment the reference
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
280
   * count in the supplied callback.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
282
283
  int bus_for_each_dev(struct bus_type *bus, struct device *start,
  		     void *data, int (*fn)(struct device *, void *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
  {
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
285
  	struct klist_iter i;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
286
  	struct device *dev;
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
287
  	int error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288

4fa3e78be   Bjorn Helgaas   Driver core: trea...
289
  	if (!bus || !bus->p)
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
290
  		return -EINVAL;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
291
292
293
294
295
  	klist_iter_init_node(&bus->p->klist_devices, &i,
  			     (start ? &start->p->knode_bus : NULL));
  	while ((dev = next_device(&i)) && !error)
  		error = fn(dev, data);
  	klist_iter_exit(&i);
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
296
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
298
  EXPORT_SYMBOL_GPL(bus_for_each_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299

0edb58604   Cornelia Huck   [PATCH] driver co...
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  /**
   * bus_find_device - device iterator for locating a particular device.
   * @bus: bus type
   * @start: Device to begin with
   * @data: Data to pass to match function
   * @match: Callback function to check device
   *
   * This is similar to the bus_for_each_dev() function above, but it
   * returns a reference to a device that is 'found' for later use, as
   * determined by the @match callback.
   *
   * The callback should return 0 if the device doesn't match and non-zero
   * if it does.  If the callback returns non-zero, this function will
   * return to the caller and not iterate over any more devices.
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
315
316
317
  struct device *bus_find_device(struct bus_type *bus,
  			       struct device *start, void *data,
  			       int (*match)(struct device *dev, void *data))
0edb58604   Cornelia Huck   [PATCH] driver co...
318
319
320
  {
  	struct klist_iter i;
  	struct device *dev;
4fa3e78be   Bjorn Helgaas   Driver core: trea...
321
  	if (!bus || !bus->p)
0edb58604   Cornelia Huck   [PATCH] driver co...
322
  		return NULL;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
323
324
  	klist_iter_init_node(&bus->p->klist_devices, &i,
  			     (start ? &start->p->knode_bus : NULL));
0edb58604   Cornelia Huck   [PATCH] driver co...
325
326
327
328
329
330
  	while ((dev = next_device(&i)))
  		if (match(dev, data) && get_device(dev))
  			break;
  	klist_iter_exit(&i);
  	return dev;
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
331
  EXPORT_SYMBOL_GPL(bus_find_device);
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
332

1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
333
334
335
  static int match_name(struct device *dev, void *data)
  {
  	const char *name = data;
1e0b2cf93   Kay Sievers   driver core: stru...
336
  	return sysfs_streq(name, dev_name(dev));
1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  }
  
  /**
   * bus_find_device_by_name - device iterator for locating a particular device of a specific name
   * @bus: bus type
   * @start: Device to begin with
   * @name: name of the device to match
   *
   * This is similar to the bus_find_device() function above, but it handles
   * searching by a name automatically, no need to write another strcmp matching
   * function.
   */
  struct device *bus_find_device_by_name(struct bus_type *bus,
  				       struct device *start, const char *name)
  {
  	return bus_find_device(bus, start, (void *)name, match_name);
  }
  EXPORT_SYMBOL_GPL(bus_find_device_by_name);
ca22e56de   Kay Sievers   driver-core: impl...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  /**
   * subsys_find_device_by_id - find a device with a specific enumeration number
   * @subsys: subsystem
   * @id: index 'id' in struct device
   * @hint: device to check first
   *
   * Check the hint's next object and if it is a match return it directly,
   * otherwise, fall back to a full list search. Either way a reference for
   * the returned object is taken.
   */
  struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
  					struct device *hint)
  {
  	struct klist_iter i;
  	struct device *dev;
  
  	if (!subsys)
  		return NULL;
  
  	if (hint) {
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
375
  		klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
ca22e56de   Kay Sievers   driver-core: impl...
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  		dev = next_device(&i);
  		if (dev && dev->id == id && get_device(dev)) {
  			klist_iter_exit(&i);
  			return dev;
  		}
  		klist_iter_exit(&i);
  	}
  
  	klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
  	while ((dev = next_device(&i))) {
  		if (dev->id == id && get_device(dev)) {
  			klist_iter_exit(&i);
  			return dev;
  		}
  	}
  	klist_iter_exit(&i);
  	return NULL;
  }
  EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
395
  static struct device_driver *next_driver(struct klist_iter *i)
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
396
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
397
  	struct klist_node *n = klist_next(i);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
398
399
400
401
402
403
404
  	struct driver_private *drv_priv;
  
  	if (n) {
  		drv_priv = container_of(n, struct driver_private, knode_bus);
  		return drv_priv->driver;
  	}
  	return NULL;
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
405
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
406
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
407
408
409
410
411
   * bus_for_each_drv - driver iterator
   * @bus: bus we're dealing with.
   * @start: driver to start iterating on.
   * @data: data to pass to the callback.
   * @fn: function to call for each driver.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
413
414
415
416
417
   * This is nearly identical to the device iterator above.
   * We iterate over each driver that belongs to @bus, and call
   * @fn for each. If @fn returns anything but 0, we break out
   * and return it. If @start is not NULL, we use it as the head
   * of the list.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
419
420
421
422
423
   * NOTE: we don't return the driver that returns a non-zero
   * value, nor do we leave the reference count incremented for that
   * driver. If the caller needs to know that info, it must set it
   * in the callback. It must also be sure to increment the refcount
   * so it doesn't disappear before returning to the caller.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
424
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
425
426
  int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  		     void *data, int (*fn)(struct device_driver *, void *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
  {
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
428
  	struct klist_iter i;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
429
  	struct device_driver *drv;
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
430
  	int error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431

38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
432
433
  	if (!bus)
  		return -EINVAL;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
434
435
436
437
438
  	klist_iter_init_node(&bus->p->klist_drivers, &i,
  			     start ? &start->p->knode_bus : NULL);
  	while ((drv = next_driver(&i)) && !error)
  		error = fn(drv, data);
  	klist_iter_exit(&i);
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
439
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
441
  EXPORT_SYMBOL_GPL(bus_for_each_drv);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
444
445
   * bus_add_device - add device to bus
   * @dev: device being added
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
   *
2023c610d   Alan Stern   Driver core: add ...
447
448
   * - Add device's bus attributes.
   * - Create links to device's bus.
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
449
   * - Add the device to its bus's list of devices.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
451
  int bus_add_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
453
  	struct bus_type *bus = bus_get(dev->bus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454
455
456
  	int error = 0;
  
  	if (bus) {
1e0b2cf93   Kay Sievers   driver core: stru...
457
458
  		pr_debug("bus: '%s': add device %s
  ", bus->name, dev_name(dev));
fa6fdb33b   Greg Kroah-Hartman   driver core: bus_...
459
460
  		error = device_add_groups(dev, bus->dev_groups);
  		if (error)
b46c73378   Greg Kroah-Hartman   driver-core: remo...
461
  			goto out_put;
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
462
  		error = sysfs_create_link(&bus->p->devices_kset->kobj,
1e0b2cf93   Kay Sievers   driver core: stru...
463
  						&dev->kobj, dev_name(dev));
f86db396f   Andrew Morton   drivers/base: che...
464
  		if (error)
1c34203a1   Junjie Mao   driver core: bus:...
465
  			goto out_groups;
f86db396f   Andrew Morton   drivers/base: che...
466
  		error = sysfs_create_link(&dev->kobj,
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
467
  				&dev->bus->p->subsys.kobj, "subsystem");
f86db396f   Andrew Morton   drivers/base: che...
468
  		if (error)
513e7337a   Cornelia Huck   driver core fixes...
469
  			goto out_subsys;
2023c610d   Alan Stern   Driver core: add ...
470
  		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
  	}
513e7337a   Cornelia Huck   driver core fixes...
472
  	return 0;
513e7337a   Cornelia Huck   driver core fixes...
473
  out_subsys:
1e0b2cf93   Kay Sievers   driver core: stru...
474
  	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
fa6fdb33b   Greg Kroah-Hartman   driver core: bus_...
475
476
  out_groups:
  	device_remove_groups(dev, bus->dev_groups);
513e7337a   Cornelia Huck   driver core fixes...
477
  out_put:
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
478
  	bus_put(dev->bus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
480
481
482
  	return error;
  }
  
  /**
2023c610d   Alan Stern   Driver core: add ...
483
484
   * bus_probe_device - probe drivers for a new device
   * @dev: device to probe
53877d06d   Kay Sievers   [PATCH] Driver co...
485
   *
2023c610d   Alan Stern   Driver core: add ...
486
   * - Automatically probe for a driver if the bus allows it.
53877d06d   Kay Sievers   [PATCH] Driver co...
487
   */
2023c610d   Alan Stern   Driver core: add ...
488
  void bus_probe_device(struct device *dev)
53877d06d   Kay Sievers   [PATCH] Driver co...
489
  {
f86db396f   Andrew Morton   drivers/base: che...
490
  	struct bus_type *bus = dev->bus;
ca22e56de   Kay Sievers   driver-core: impl...
491
  	struct subsys_interface *sif;
53877d06d   Kay Sievers   [PATCH] Driver co...
492

ca22e56de   Kay Sievers   driver-core: impl...
493
494
  	if (!bus)
  		return;
765230b5f   Dmitry Torokhov   driver-core: add ...
495
496
  	if (bus->p->drivers_autoprobe)
  		device_initial_probe(dev);
ca22e56de   Kay Sievers   driver-core: impl...
497
498
499
500
501
502
  
  	mutex_lock(&bus->p->mutex);
  	list_for_each_entry(sif, &bus->p->interfaces, node)
  		if (sif->add_dev)
  			sif->add_dev(dev, sif);
  	mutex_unlock(&bus->p->mutex);
53877d06d   Kay Sievers   [PATCH] Driver co...
503
504
505
  }
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
506
507
   * bus_remove_device - remove device from bus
   * @dev: device to be removed
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
508
   *
ca22e56de   Kay Sievers   driver-core: impl...
509
510
   * - Remove device from all interfaces.
   * - Remove symlink from bus' directory.
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
511
512
513
   * - Delete device from bus's list.
   * - Detach from its driver.
   * - Drop reference taken in bus_add_device().
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
515
  void bus_remove_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
  {
ca22e56de   Kay Sievers   driver-core: impl...
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
  	struct bus_type *bus = dev->bus;
  	struct subsys_interface *sif;
  
  	if (!bus)
  		return;
  
  	mutex_lock(&bus->p->mutex);
  	list_for_each_entry(sif, &bus->p->interfaces, node)
  		if (sif->remove_dev)
  			sif->remove_dev(dev, sif);
  	mutex_unlock(&bus->p->mutex);
  
  	sysfs_remove_link(&dev->kobj, "subsystem");
  	sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
  			  dev_name(dev));
fa6fdb33b   Greg Kroah-Hartman   driver core: bus_...
532
  	device_remove_groups(dev, dev->bus->dev_groups);
ca22e56de   Kay Sievers   driver-core: impl...
533
534
535
536
537
538
539
540
  	if (klist_node_attached(&dev->p->knode_bus))
  		klist_del(&dev->p->knode_bus);
  
  	pr_debug("bus: '%s': remove device %s
  ",
  		 dev->bus->name, dev_name(dev));
  	device_release_driver(dev);
  	bus_put(dev->bus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
  }
f86db396f   Andrew Morton   drivers/base: che...
542
  static int __must_check add_bind_files(struct device_driver *drv)
874c6241b   Greg Kroah-Hartman   [PATCH] Driver co...
543
  {
f86db396f   Andrew Morton   drivers/base: che...
544
545
546
547
548
549
550
551
552
  	int ret;
  
  	ret = driver_create_file(drv, &driver_attr_unbind);
  	if (ret == 0) {
  		ret = driver_create_file(drv, &driver_attr_bind);
  		if (ret)
  			driver_remove_file(drv, &driver_attr_unbind);
  	}
  	return ret;
874c6241b   Greg Kroah-Hartman   [PATCH] Driver co...
553
554
555
556
557
558
559
  }
  
  static void remove_bind_files(struct device_driver *drv)
  {
  	driver_remove_file(drv, &driver_attr_bind);
  	driver_remove_file(drv, &driver_attr_unbind);
  }
b8c5cec23   Kay Sievers   Driver core: udev...
560

8380770c8   Kay Sievers   Driver core: make...
561
562
563
  static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
  static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
  		show_drivers_autoprobe, store_drivers_autoprobe);
b8c5cec23   Kay Sievers   Driver core: udev...
564
565
566
  static int add_probe_files(struct bus_type *bus)
  {
  	int retval;
8380770c8   Kay Sievers   Driver core: make...
567
  	retval = bus_create_file(bus, &bus_attr_drivers_probe);
b8c5cec23   Kay Sievers   Driver core: udev...
568
569
  	if (retval)
  		goto out;
8380770c8   Kay Sievers   Driver core: make...
570
  	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
b8c5cec23   Kay Sievers   Driver core: udev...
571
  	if (retval)
8380770c8   Kay Sievers   Driver core: make...
572
  		bus_remove_file(bus, &bus_attr_drivers_probe);
b8c5cec23   Kay Sievers   Driver core: udev...
573
574
575
576
577
578
  out:
  	return retval;
  }
  
  static void remove_probe_files(struct bus_type *bus)
  {
8380770c8   Kay Sievers   Driver core: make...
579
580
  	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  	bus_remove_file(bus, &bus_attr_drivers_probe);
b8c5cec23   Kay Sievers   Driver core: udev...
581
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
582

2581c9cc0   Greg Kroah-Hartman   driver core: bus:...
583
584
  static ssize_t uevent_store(struct device_driver *drv, const char *buf,
  			    size_t count)
7ac1cf4a8   Kay Sievers   Driver core: add ...
585
  {
f36776faf   Peter Rajnoha   kobject: support ...
586
  	kobject_synth_uevent(&drv->p->kobj, buf, count);
7ac1cf4a8   Kay Sievers   Driver core: add ...
587
588
  	return count;
  }
2581c9cc0   Greg Kroah-Hartman   driver core: bus:...
589
  static DRIVER_ATTR_WO(uevent);
7ac1cf4a8   Kay Sievers   Driver core: add ...
590

765230b5f   Dmitry Torokhov   driver-core: add ...
591
592
593
594
595
596
597
598
599
600
601
  static void driver_attach_async(void *_drv, async_cookie_t cookie)
  {
  	struct device_driver *drv = _drv;
  	int ret;
  
  	ret = driver_attach(drv);
  
  	pr_debug("bus: '%s': driver %s async attach completed: %d
  ",
  		 drv->bus->name, drv->name, ret);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
603
604
   * bus_add_driver - Add a driver to the bus.
   * @drv: driver.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
   */
f86db396f   Andrew Morton   drivers/base: che...
606
  int bus_add_driver(struct device_driver *drv)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
607
  {
e5dd12784   Greg Kroah-Hartman   Driver core: move...
608
609
  	struct bus_type *bus;
  	struct driver_private *priv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
  	int error = 0;
e5dd12784   Greg Kroah-Hartman   Driver core: move...
611
  	bus = bus_get(drv->bus);
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
612
  	if (!bus)
4f6e1945f   Greg Kroah-Hartman   driver core: bus_...
613
  		return -EINVAL;
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
614

7dc72b284   Greg Kroah-Hartman   Driver core: clea...
615
616
  	pr_debug("bus: '%s': add driver %s
  ", bus->name, drv->name);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
617
618
  
  	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
076344642   Cornelia Huck   Driver core: Fix ...
619
620
621
622
  	if (!priv) {
  		error = -ENOMEM;
  		goto out_put_bus;
  	}
e5dd12784   Greg Kroah-Hartman   Driver core: move...
623
624
625
  	klist_init(&priv->klist_devices, NULL, NULL);
  	priv->driver = drv;
  	drv->p = priv;
c8e90d822   Greg Kroah-Hartman   Kobject: change d...
626
627
628
  	priv->kobj.kset = bus->p->drivers_kset;
  	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
  				     "%s", drv->name);
dc0afa838   Cornelia Huck   Driver core: codi...
629
  	if (error)
076344642   Cornelia Huck   Driver core: Fix ...
630
  		goto out_unregister;
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
631

190888ac0   Ming Lei   driver core: fix ...
632
  	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
633
  	if (drv->bus->p->drivers_autoprobe) {
765230b5f   Dmitry Torokhov   driver-core: add ...
634
635
636
637
638
639
640
641
642
643
  		if (driver_allows_async_probing(drv)) {
  			pr_debug("bus: '%s': probing driver %s asynchronously
  ",
  				drv->bus->name, drv->name);
  			async_schedule(driver_attach_async, drv);
  		} else {
  			error = driver_attach(drv);
  			if (error)
  				goto out_unregister;
  		}
b8c5cec23   Kay Sievers   Driver core: udev...
644
  	}
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
645
  	module_add_driver(drv->owner, drv);
7ac1cf4a8   Kay Sievers   Driver core: add ...
646
647
648
649
  	error = driver_create_file(drv, &driver_attr_uevent);
  	if (error) {
  		printk(KERN_ERR "%s: uevent attr (%s) failed
  ",
2b3a302a0   Harvey Harrison   driver core: repl...
650
  			__func__, drv->name);
7ac1cf4a8   Kay Sievers   Driver core: add ...
651
  	}
e18945b15   Greg Kroah-Hartman   driver-core: remo...
652
  	error = driver_add_groups(drv, bus->drv_groups);
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
653
654
  	if (error) {
  		/* How the hell do we get out of this pickle? Give up */
ed0617b5c   Greg Kroah-Hartman   driver core: bus_...
655
656
657
  		printk(KERN_ERR "%s: driver_create_groups(%s) failed
  ",
  			__func__, drv->name);
e18945b15   Greg Kroah-Hartman   driver-core: remo...
658
  	}
1a6f2a751   Dmitry Torokhov   Driver core: allo...
659
660
661
662
663
664
665
666
667
  
  	if (!drv->suppress_bind_attrs) {
  		error = add_bind_files(drv);
  		if (error) {
  			/* Ditto */
  			printk(KERN_ERR "%s: add_bind_files(%s) failed
  ",
  				__func__, drv->name);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
  	}
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
669

5c8563d77   Kay Sievers   Driver Core: do n...
670
  	return 0;
1a6f2a751   Dmitry Torokhov   Driver core: allo...
671

f86db396f   Andrew Morton   drivers/base: che...
672
  out_unregister:
99b28f1b4   Phil Carmody   driver core: Prev...
673
  	kobject_put(&priv->kobj);
0f9b011d3   Christophe JAILLET   driver core: bus:...
674
  	/* drv->p is freed in driver_release()  */
5c8563d77   Kay Sievers   Driver Core: do n...
675
  	drv->p = NULL;
f86db396f   Andrew Morton   drivers/base: che...
676
  out_put_bus:
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
677
  	bus_put(bus);
f86db396f   Andrew Morton   drivers/base: che...
678
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
681
682
   * bus_remove_driver - delete driver from bus's knowledge.
   * @drv: driver.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
684
685
686
   * Detach the driver from the devices it controls, and remove
   * it from its bus's list of drivers. Finally, we drop the reference
   * to the bus we took in bus_add_driver().
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
688
  void bus_remove_driver(struct device_driver *drv)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
689
  {
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
690
691
  	if (!drv->bus)
  		return;
1a6f2a751   Dmitry Torokhov   Driver core: allo...
692
693
  	if (!drv->suppress_bind_attrs)
  		remove_bind_files(drv);
ed0617b5c   Greg Kroah-Hartman   driver core: bus_...
694
  	driver_remove_groups(drv, drv->bus->drv_groups);
7ac1cf4a8   Kay Sievers   Driver core: add ...
695
  	driver_remove_file(drv, &driver_attr_uevent);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
696
  	klist_remove(&drv->p->knode_bus);
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
697
698
  	pr_debug("bus: '%s': remove driver %s
  ", drv->bus->name, drv->name);
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
699
700
  	driver_detach(drv);
  	module_remove_driver(drv);
c10997f65   Greg Kroah-Hartman   Kobject: convert ...
701
  	kobject_put(&drv->p->kobj);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
702
  	bus_put(drv->bus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
  /* Helper for bus_rescan_devices's iter */
f86db396f   Andrew Morton   drivers/base: che...
705
  static int __must_check bus_rescan_devices_helper(struct device *dev,
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
706
  						  void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
707
  {
f86db396f   Andrew Morton   drivers/base: che...
708
  	int ret = 0;
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
709
710
  	if (!dev->driver) {
  		if (dev->parent)	/* Needed for USB */
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
711
  			device_lock(dev->parent);
f86db396f   Andrew Morton   drivers/base: che...
712
  		ret = device_attach(dev);
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
713
  		if (dev->parent)
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
714
  			device_unlock(dev->parent);
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
715
  	}
f86db396f   Andrew Morton   drivers/base: che...
716
  	return ret < 0 ? ret : 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
718
  /**
23d3d602c   Greg Kroah-Hartman   [PATCH] driver co...
719
720
   * bus_rescan_devices - rescan devices on the bus for possible drivers
   * @bus: the bus to scan.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
721
   *
23d3d602c   Greg Kroah-Hartman   [PATCH] driver co...
722
723
724
   * This function will look for devices on the bus with no driver
   * attached and rescan it against existing drivers to see if it matches
   * any by calling device_attach() for the unbound devices.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
726
  int bus_rescan_devices(struct bus_type *bus)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
727
  {
f86db396f   Andrew Morton   drivers/base: che...
728
  	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
730
  EXPORT_SYMBOL_GPL(bus_rescan_devices);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731

e935d5da8   Moore, Eric   [SCSI] drivers/ba...
732
733
734
735
736
737
738
739
740
  /**
   * device_reprobe - remove driver for a device and probe for a new driver
   * @dev: the device to reprobe
   *
   * This function detaches the attached driver (if any) for the given
   * device and restarts the driver probing process.  It is intended
   * to use if probing criteria changed during a devices lifetime and
   * driver attachment should change accordingly.
   */
f86db396f   Andrew Morton   drivers/base: che...
741
  int device_reprobe(struct device *dev)
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
742
743
744
  {
  	if (dev->driver) {
  		if (dev->parent)        /* Needed for USB */
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
745
  			device_lock(dev->parent);
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
746
747
  		device_release_driver(dev);
  		if (dev->parent)
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
748
  			device_unlock(dev->parent);
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
749
  	}
f86db396f   Andrew Morton   drivers/base: che...
750
  	return bus_rescan_devices_helper(dev, NULL);
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
751
752
  }
  EXPORT_SYMBOL_GPL(device_reprobe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
754
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
755
756
   * find_bus - locate bus by name.
   * @name: name of bus.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
757
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
758
759
   * Call kset_find_obj() to iterate over list of buses to
   * find a bus by name. Return bus if found.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
760
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
761
   * Note that kset_find_obj increments bus' reference count.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
   */
7e4ef085e   Adrian Bunk   [PATCH] Driver co...
763
  #if 0
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
764
  struct bus_type *find_bus(char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
765
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
766
  	struct kobject *k = kset_find_obj(bus_kset, name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
767
768
  	return k ? to_bus(k) : NULL;
  }
7e4ef085e   Adrian Bunk   [PATCH] Driver co...
769
  #endif  /*  0  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
770

12478ba07   Greg Kroah-Hartman   driver core: bus_...
771
772
773
  static int bus_add_groups(struct bus_type *bus,
  			  const struct attribute_group **groups)
  {
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
774
  	return sysfs_create_groups(&bus->p->subsys.kobj, groups);
12478ba07   Greg Kroah-Hartman   driver core: bus_...
775
776
777
778
779
  }
  
  static void bus_remove_groups(struct bus_type *bus,
  			      const struct attribute_group **groups)
  {
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
780
  	sysfs_remove_groups(&bus->p->subsys.kobj, groups);
12478ba07   Greg Kroah-Hartman   driver core: bus_...
781
  }
34bb61f9d   James Bottomley   [PATCH] fix klist...
782
783
  static void klist_devices_get(struct klist_node *n)
  {
ae1b41715   Greg Kroah-Hartman   driver core: move...
784
785
  	struct device_private *dev_prv = to_device_private_bus(n);
  	struct device *dev = dev_prv->device;
34bb61f9d   James Bottomley   [PATCH] fix klist...
786
787
788
789
790
791
  
  	get_device(dev);
  }
  
  static void klist_devices_put(struct klist_node *n)
  {
ae1b41715   Greg Kroah-Hartman   driver core: move...
792
793
  	struct device_private *dev_prv = to_device_private_bus(n);
  	struct device *dev = dev_prv->device;
34bb61f9d   James Bottomley   [PATCH] fix klist...
794
795
796
  
  	put_device(dev);
  }
7ac1cf4a8   Kay Sievers   Driver core: add ...
797
798
799
  static ssize_t bus_uevent_store(struct bus_type *bus,
  				const char *buf, size_t count)
  {
f36776faf   Peter Rajnoha   kobject: support ...
800
  	kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
7ac1cf4a8   Kay Sievers   Driver core: add ...
801
802
803
  	return count;
  }
  static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
804
  /**
be871b7e5   Michal Hocko   device: separate ...
805
   * bus_register - register a driver-core subsystem
78d79559f   Randy Dunlap   kernel-doc: fix n...
806
   * @bus: bus to register
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
807
   *
78d79559f   Randy Dunlap   kernel-doc: fix n...
808
   * Once we have that, we register the bus with the kobject
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
809
   * infrastructure, then register the children subsystems it has:
ca22e56de   Kay Sievers   driver-core: impl...
810
   * the devices and drivers that belong to the subsystem.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
811
   */
be871b7e5   Michal Hocko   device: separate ...
812
  int bus_register(struct bus_type *bus)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
813
814
  {
  	int retval;
6b6e39a6a   Kay Sievers   driver-core: merg...
815
  	struct subsys_private *priv;
be871b7e5   Michal Hocko   device: separate ...
816
  	struct lock_class_key *key = &bus->lock_key;
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
817

6b6e39a6a   Kay Sievers   driver-core: merg...
818
  	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
819
820
821
822
823
  	if (!priv)
  		return -ENOMEM;
  
  	priv->bus = bus;
  	bus->p = priv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
824

c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
825
  	BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
116af3782   Benjamin Herrenschmidt   Driver core: add ...
826

c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
827
  	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
829
  	if (retval)
  		goto out;
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
830
831
832
  	priv->subsys.kobj.kset = bus_kset;
  	priv->subsys.kobj.ktype = &bus_ktype;
  	priv->drivers_autoprobe = 1;
d6b05b84e   Greg Kroah-Hartman   Driver core: remo...
833

c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
834
  	retval = kset_register(&priv->subsys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
835
836
  	if (retval)
  		goto out;
7ac1cf4a8   Kay Sievers   Driver core: add ...
837
838
839
  	retval = bus_create_file(bus, &bus_attr_uevent);
  	if (retval)
  		goto bus_uevent_fail;
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
840
841
842
  	priv->devices_kset = kset_create_and_add("devices", NULL,
  						 &priv->subsys.kobj);
  	if (!priv->devices_kset) {
3d8995963   Greg Kroah-Hartman   kset: convert str...
843
  		retval = -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
844
  		goto bus_devices_fail;
3d8995963   Greg Kroah-Hartman   kset: convert str...
845
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846

c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
847
848
849
  	priv->drivers_kset = kset_create_and_add("drivers", NULL,
  						 &priv->subsys.kobj);
  	if (!priv->drivers_kset) {
6dcec2511   Greg Kroah-Hartman   kset: convert str...
850
  		retval = -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
851
  		goto bus_drivers_fail;
6dcec2511   Greg Kroah-Hartman   kset: convert str...
852
  	}
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
853

ca22e56de   Kay Sievers   driver-core: impl...
854
855
  	INIT_LIST_HEAD(&priv->interfaces);
  	__mutex_init(&priv->mutex, "subsys mutex", key);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
856
857
  	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  	klist_init(&priv->klist_drivers, NULL, NULL);
b8c5cec23   Kay Sievers   Driver core: udev...
858

b8c5cec23   Kay Sievers   Driver core: udev...
859
860
861
  	retval = add_probe_files(bus);
  	if (retval)
  		goto bus_probe_files_fail;
12478ba07   Greg Kroah-Hartman   driver core: bus_...
862
863
864
  	retval = bus_add_groups(bus, bus->bus_groups);
  	if (retval)
  		goto bus_groups_fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
865

7dc72b284   Greg Kroah-Hartman   Driver core: clea...
866
867
  	pr_debug("bus: '%s': registered
  ", bus->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
868
  	return 0;
12478ba07   Greg Kroah-Hartman   driver core: bus_...
869
  bus_groups_fail:
b8c5cec23   Kay Sievers   Driver core: udev...
870
871
  	remove_probe_files(bus);
  bus_probe_files_fail:
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
872
  	kset_unregister(bus->p->drivers_kset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
873
  bus_drivers_fail:
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
874
  	kset_unregister(bus->p->devices_kset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
  bus_devices_fail:
7ac1cf4a8   Kay Sievers   Driver core: add ...
876
877
  	bus_remove_file(bus, &bus_attr_uevent);
  bus_uevent_fail:
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
878
  	kset_unregister(&bus->p->subsys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
879
  out:
600c20f34   Jike Song   driver core: fix ...
880
  	kfree(bus->p);
f48f3febb   Dave Young   driver-core: do n...
881
  	bus->p = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
882
883
  	return retval;
  }
be871b7e5   Michal Hocko   device: separate ...
884
  EXPORT_SYMBOL_GPL(bus_register);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
885

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
886
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
887
888
   * bus_unregister - remove a bus from the system
   * @bus: bus.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
889
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
890
891
   * Unregister the child subsystems and the bus itself.
   * Finally, we call bus_put() to release the refcount
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
892
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
893
  void bus_unregister(struct bus_type *bus)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
894
  {
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
895
896
  	pr_debug("bus: '%s': unregistering
  ", bus->name);
ca22e56de   Kay Sievers   driver-core: impl...
897
898
  	if (bus->dev_root)
  		device_unregister(bus->dev_root);
12478ba07   Greg Kroah-Hartman   driver core: bus_...
899
  	bus_remove_groups(bus, bus->bus_groups);
b8c5cec23   Kay Sievers   Driver core: udev...
900
  	remove_probe_files(bus);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
901
902
  	kset_unregister(bus->p->drivers_kset);
  	kset_unregister(bus->p->devices_kset);
7ac1cf4a8   Kay Sievers   Driver core: add ...
903
  	bus_remove_file(bus, &bus_attr_uevent);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
904
  	kset_unregister(&bus->p->subsys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
905
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
906
  EXPORT_SYMBOL_GPL(bus_unregister);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
907

116af3782   Benjamin Herrenschmidt   Driver core: add ...
908
909
  int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
910
  	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
116af3782   Benjamin Herrenschmidt   Driver core: add ...
911
912
913
914
915
  }
  EXPORT_SYMBOL_GPL(bus_register_notifier);
  
  int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
916
  	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
116af3782   Benjamin Herrenschmidt   Driver core: add ...
917
918
  }
  EXPORT_SYMBOL_GPL(bus_unregister_notifier);
0fed80f7a   Greg Kroah-Hartman   driver core: add ...
919
920
  struct kset *bus_get_kset(struct bus_type *bus)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
921
  	return &bus->p->subsys;
0fed80f7a   Greg Kroah-Hartman   driver core: add ...
922
923
  }
  EXPORT_SYMBOL_GPL(bus_get_kset);
b249072ee   Greg Kroah-Hartman   driver core: add ...
924
925
  struct klist *bus_get_device_klist(struct bus_type *bus)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
926
  	return &bus->p->klist_devices;
b249072ee   Greg Kroah-Hartman   driver core: add ...
927
928
  }
  EXPORT_SYMBOL_GPL(bus_get_device_klist);
99178b036   Greg Kroah-Hartman   Driver core: add ...
929
  /*
dca25ebdd   Robert P. J. Day   Fix "forcably" co...
930
   * Yes, this forcibly breaks the klist abstraction temporarily.  It
99178b036   Greg Kroah-Hartman   Driver core: add ...
931
932
933
934
935
936
937
938
939
   * just wants to sort the klist, not change reference counts and
   * take/drop locks rapidly in the process.  It does all this while
   * holding the lock for the list, so objects can't otherwise be
   * added/removed while we're swizzling.
   */
  static void device_insertion_sort_klist(struct device *a, struct list_head *list,
  					int (*compare)(const struct device *a,
  							const struct device *b))
  {
99178b036   Greg Kroah-Hartman   Driver core: add ...
940
  	struct klist_node *n;
ae1b41715   Greg Kroah-Hartman   driver core: move...
941
  	struct device_private *dev_prv;
99178b036   Greg Kroah-Hartman   Driver core: add ...
942
  	struct device *b;
4c62785e6   Geliang Tang   driver core: bus:...
943
  	list_for_each_entry(n, list, n_node) {
ae1b41715   Greg Kroah-Hartman   driver core: move...
944
945
  		dev_prv = to_device_private_bus(n);
  		b = dev_prv->device;
99178b036   Greg Kroah-Hartman   Driver core: add ...
946
  		if (compare(a, b) <= 0) {
ae1b41715   Greg Kroah-Hartman   driver core: move...
947
948
  			list_move_tail(&a->p->knode_bus.n_node,
  				       &b->p->knode_bus.n_node);
99178b036   Greg Kroah-Hartman   Driver core: add ...
949
950
951
  			return;
  		}
  	}
ae1b41715   Greg Kroah-Hartman   driver core: move...
952
  	list_move_tail(&a->p->knode_bus.n_node, list);
99178b036   Greg Kroah-Hartman   Driver core: add ...
953
954
955
956
957
958
959
  }
  
  void bus_sort_breadthfirst(struct bus_type *bus,
  			   int (*compare)(const struct device *a,
  					  const struct device *b))
  {
  	LIST_HEAD(sorted_devices);
4c62785e6   Geliang Tang   driver core: bus:...
960
  	struct klist_node *n, *tmp;
ae1b41715   Greg Kroah-Hartman   driver core: move...
961
  	struct device_private *dev_prv;
99178b036   Greg Kroah-Hartman   Driver core: add ...
962
963
964
965
966
967
  	struct device *dev;
  	struct klist *device_klist;
  
  	device_klist = bus_get_device_klist(bus);
  
  	spin_lock(&device_klist->k_lock);
4c62785e6   Geliang Tang   driver core: bus:...
968
  	list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
ae1b41715   Greg Kroah-Hartman   driver core: move...
969
970
  		dev_prv = to_device_private_bus(n);
  		dev = dev_prv->device;
99178b036   Greg Kroah-Hartman   Driver core: add ...
971
972
973
974
975
976
  		device_insertion_sort_klist(dev, &sorted_devices, compare);
  	}
  	list_splice(&sorted_devices, &device_klist->k_list);
  	spin_unlock(&device_klist->k_lock);
  }
  EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
ca22e56de   Kay Sievers   driver-core: impl...
977
978
979
980
981
982
983
984
985
986
987
988
  /**
   * subsys_dev_iter_init - initialize subsys device iterator
   * @iter: subsys iterator to initialize
   * @subsys: the subsys we wanna iterate over
   * @start: the device to start iterating from, if any
   * @type: device_type of the devices to iterate over, NULL for all
   *
   * Initialize subsys iterator @iter such that it iterates over devices
   * of @subsys.  If @start is set, the list iteration will start there,
   * otherwise if it is NULL, the iteration starts at the beginning of
   * the list.
   */
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
989
990
  void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
  			  struct device *start, const struct device_type *type)
ca22e56de   Kay Sievers   driver-core: impl...
991
992
993
994
995
  {
  	struct klist_node *start_knode = NULL;
  
  	if (start)
  		start_knode = &start->p->knode_bus;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
996
997
  	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
  	iter->type = type;
ca22e56de   Kay Sievers   driver-core: impl...
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  }
  EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
  
  /**
   * subsys_dev_iter_next - iterate to the next device
   * @iter: subsys iterator to proceed
   *
   * Proceed @iter to the next device and return it.  Returns NULL if
   * iteration is complete.
   *
   * The returned device is referenced and won't be released till
   * iterator is proceed to the next device or exited.  The caller is
   * free to do whatever it wants to do with the device including
   * calling back into subsys code.
   */
  struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
  {
  	struct klist_node *knode;
  	struct device *dev;
  
  	for (;;) {
  		knode = klist_next(&iter->ki);
  		if (!knode)
  			return NULL;
371fd7a2c   Geliang Tang   driver core: bus:...
1022
  		dev = to_device_private_bus(knode)->device;
ca22e56de   Kay Sievers   driver-core: impl...
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
  		if (!iter->type || iter->type == dev->type)
  			return dev;
  	}
  }
  EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
  
  /**
   * subsys_dev_iter_exit - finish iteration
   * @iter: subsys iterator to finish
   *
   * Finish an iteration.  Always call this function after iteration is
   * complete whether the iteration ran till the end or not.
   */
  void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
  {
  	klist_iter_exit(&iter->ki);
  }
  EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
  
  int subsys_interface_register(struct subsys_interface *sif)
  {
  	struct bus_type *subsys;
  	struct subsys_dev_iter iter;
  	struct device *dev;
  
  	if (!sif || !sif->subsys)
  		return -ENODEV;
  
  	subsys = bus_get(sif->subsys);
  	if (!subsys)
  		return -EINVAL;
  
  	mutex_lock(&subsys->p->mutex);
  	list_add_tail(&sif->node, &subsys->p->interfaces);
  	if (sif->add_dev) {
  		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
  		while ((dev = subsys_dev_iter_next(&iter)))
  			sif->add_dev(dev, sif);
  		subsys_dev_iter_exit(&iter);
  	}
  	mutex_unlock(&subsys->p->mutex);
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(subsys_interface_register);
  
  void subsys_interface_unregister(struct subsys_interface *sif)
  {
2b31594a9   Jonghwan Choi   driver-core: Fix ...
1071
  	struct bus_type *subsys;
ca22e56de   Kay Sievers   driver-core: impl...
1072
1073
  	struct subsys_dev_iter iter;
  	struct device *dev;
2b31594a9   Jonghwan Choi   driver-core: Fix ...
1074
  	if (!sif || !sif->subsys)
ca22e56de   Kay Sievers   driver-core: impl...
1075
  		return;
2b31594a9   Jonghwan Choi   driver-core: Fix ...
1076
  	subsys = sif->subsys;
ca22e56de   Kay Sievers   driver-core: impl...
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
  	mutex_lock(&subsys->p->mutex);
  	list_del_init(&sif->node);
  	if (sif->remove_dev) {
  		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
  		while ((dev = subsys_dev_iter_next(&iter)))
  			sif->remove_dev(dev, sif);
  		subsys_dev_iter_exit(&iter);
  	}
  	mutex_unlock(&subsys->p->mutex);
  
  	bus_put(subsys);
  }
  EXPORT_SYMBOL_GPL(subsys_interface_unregister);
  
  static void system_root_device_release(struct device *dev)
  {
  	kfree(dev);
  }
d73ce0042   Tejun Heo   driver/base: impl...
1095
1096
1097
1098
  
  static int subsys_register(struct bus_type *subsys,
  			   const struct attribute_group **groups,
  			   struct kobject *parent_of_root)
ca22e56de   Kay Sievers   driver-core: impl...
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
  {
  	struct device *dev;
  	int err;
  
  	err = bus_register(subsys);
  	if (err < 0)
  		return err;
  
  	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  	if (!dev) {
  		err = -ENOMEM;
  		goto err_dev;
  	}
  
  	err = dev_set_name(dev, "%s", subsys->name);
  	if (err < 0)
  		goto err_name;
d73ce0042   Tejun Heo   driver/base: impl...
1116
  	dev->kobj.parent = parent_of_root;
ca22e56de   Kay Sievers   driver-core: impl...
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
  	dev->groups = groups;
  	dev->release = system_root_device_release;
  
  	err = device_register(dev);
  	if (err < 0)
  		goto err_dev_reg;
  
  	subsys->dev_root = dev;
  	return 0;
  
  err_dev_reg:
  	put_device(dev);
  	dev = NULL;
  err_name:
  	kfree(dev);
  err_dev:
  	bus_unregister(subsys);
  	return err;
  }
d73ce0042   Tejun Heo   driver/base: impl...
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
  
  /**
   * subsys_system_register - register a subsystem at /sys/devices/system/
   * @subsys: system subsystem
   * @groups: default attributes for the root device
   *
   * All 'system' subsystems have a /sys/devices/system/<name> root device
   * with the name of the subsystem. The root device can carry subsystem-
   * wide attributes. All registered devices are below this single root
   * device and are named after the subsystem with a simple enumeration
e227867f1   Masanari Iida   treewide: Fix typ...
1146
   * number appended. The registered devices are not explicitly named;
d73ce0042   Tejun Heo   driver/base: impl...
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
   * only 'id' in the device needs to be set.
   *
   * Do not use this interface for anything new, it exists for compatibility
   * with bad ideas only. New subsystems should use plain subsystems; and
   * add the subsystem-wide attributes should be added to the subsystem
   * directory itself and not some create fake root-device placed in
   * /sys/devices/system/<name>.
   */
  int subsys_system_register(struct bus_type *subsys,
  			   const struct attribute_group **groups)
  {
  	return subsys_register(subsys, groups, &system_kset->kobj);
  }
ca22e56de   Kay Sievers   driver-core: impl...
1160
  EXPORT_SYMBOL_GPL(subsys_system_register);
d73ce0042   Tejun Heo   driver/base: impl...
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
  /**
   * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
   * @subsys: virtual subsystem
   * @groups: default attributes for the root device
   *
   * All 'virtual' subsystems have a /sys/devices/system/<name> root device
   * with the name of the subystem.  The root device can carry subsystem-wide
   * attributes.  All registered devices are below this single root device.
   * There's no restriction on device naming.  This is for kernel software
   * constructs which need sysfs interface.
   */
  int subsys_virtual_register(struct bus_type *subsys,
  			    const struct attribute_group **groups)
  {
  	struct kobject *virtual_dir;
  
  	virtual_dir = virtual_device_parent(NULL);
  	if (!virtual_dir)
  		return -ENOMEM;
  
  	return subsys_register(subsys, groups, virtual_dir);
  }
1c04fc353   Greg Kroah-Hartman   driver core: expo...
1183
  EXPORT_SYMBOL_GPL(subsys_virtual_register);
d73ce0042   Tejun Heo   driver/base: impl...
1184

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1185
1186
  int __init buses_init(void)
  {
59a548338   Greg Kroah-Hartman   kset: convert dri...
1187
1188
1189
  	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  	if (!bus_kset)
  		return -ENOMEM;
ca22e56de   Kay Sievers   driver-core: impl...
1190
1191
1192
1193
  
  	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
  	if (!system_kset)
  		return -ENOMEM;
59a548338   Greg Kroah-Hartman   kset: convert dri...
1194
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1195
  }