Blame view

drivers/base/bus.c 31.4 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

d9c25a6fe   Daniel Vetter   sysfs: Disable lo...
34
35
36
  #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
  	struct driver_attribute driver_attr_##_name =		\
  		__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37

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

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

174be70b6   Bart Van Assche   driver-core: Fix ...
140
141
  static void bus_release(struct kobject *kobj)
  {
371fd7a2c   Geliang Tang   driver core: bus:...
142
  	struct subsys_private *priv = to_subsys_private(kobj);
174be70b6   Bart Van Assche   driver-core: Fix ...
143
144
145
146
147
  	struct bus_type *bus = priv->bus;
  
  	kfree(priv);
  	bus->p = NULL;
  }
80f03e349   Kay Sievers   Driver core: add ...
148
  static struct kobj_type bus_ktype = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
  	.sysfs_ops	= &bus_sysfs_ops,
174be70b6   Bart Van Assche   driver-core: Fix ...
150
  	.release	= bus_release,
80f03e349   Kay Sievers   Driver core: add ...
151
152
153
154
155
156
157
158
159
160
  };
  
  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
161

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

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

afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
189
190
191
192
193
  /*
   * 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:...
194
195
  static ssize_t bind_store(struct device_driver *drv, const char *buf,
  			  size_t count)
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
196
  {
5901d0145   Greg Kroah-Hartman   Driver core: remo...
197
  	struct bus_type *bus = bus_get(drv->bus);
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
198
199
  	struct device *dev;
  	int err = -ENODEV;
1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
200
  	dev = bus_find_device_by_name(bus, NULL, buf);
49b420a13   Ming Lei   driver core: chec...
201
  	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
202
  		if (dev->parent)	/* Needed for USB */
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
203
204
  			device_lock(dev->parent);
  		device_lock(dev);
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
205
  		err = driver_probe_device(drv, dev);
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
206
  		device_unlock(dev);
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
207
  		if (dev->parent)
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
208
  			device_unlock(dev->parent);
372254018   Ryan Wilson   [PATCH] driver co...
209

4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
210
211
  		if (err > 0) {
  			/* success */
372254018   Ryan Wilson   [PATCH] driver co...
212
  			err = count;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
213
214
  		} else if (err == 0) {
  			/* driver didn't accept device */
372254018   Ryan Wilson   [PATCH] driver co...
215
  			err = -ENODEV;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
216
  		}
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
217
  	}
2b08c8d04   Alan Stern   [PATCH] Small fix...
218
  	put_device(dev);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
219
  	bus_put(bus);
2b08c8d04   Alan Stern   [PATCH] Small fix...
220
  	return err;
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
221
  }
d9c25a6fe   Daniel Vetter   sysfs: Disable lo...
222
  static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store);
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
223

b8c5cec23   Kay Sievers   Driver core: udev...
224
225
  static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
226
227
  	return sprintf(buf, "%d
  ", bus->p->drivers_autoprobe);
b8c5cec23   Kay Sievers   Driver core: udev...
228
229
230
231
232
233
  }
  
  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...
234
  		bus->p->drivers_autoprobe = 0;
b8c5cec23   Kay Sievers   Driver core: udev...
235
  	else
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
236
  		bus->p->drivers_autoprobe = 1;
b8c5cec23   Kay Sievers   Driver core: udev...
237
238
239
240
241
242
243
  	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 ...
244
  	int err = -EINVAL;
b8c5cec23   Kay Sievers   Driver core: udev...
245

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

4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
255
  static struct device *next_device(struct klist_iter *i)
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
256
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
257
  	struct klist_node *n = klist_next(i);
ae1b41715   Greg Kroah-Hartman   driver core: move...
258
259
260
261
262
263
264
265
  	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...
266
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
268
269
270
271
272
   * 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
273
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
274
275
276
   * 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
277
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
278
279
   * 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
280
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
281
282
   * 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...
283
   * to retain this data, it should do so, and increment the reference
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
284
   * count in the supplied callback.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
286
287
  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
288
  {
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
289
  	struct klist_iter i;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
290
  	struct device *dev;
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
291
  	int error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292

4fa3e78be   Bjorn Helgaas   Driver core: trea...
293
  	if (!bus || !bus->p)
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
294
  		return -EINVAL;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
295
296
297
298
299
  	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...
300
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
302
  EXPORT_SYMBOL_GPL(bus_for_each_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303

0edb58604   Cornelia Huck   [PATCH] driver co...
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  /**
   * 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...
319
320
321
  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...
322
323
324
  {
  	struct klist_iter i;
  	struct device *dev;
4fa3e78be   Bjorn Helgaas   Driver core: trea...
325
  	if (!bus || !bus->p)
0edb58604   Cornelia Huck   [PATCH] driver co...
326
  		return NULL;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
327
328
  	klist_iter_init_node(&bus->p->klist_devices, &i,
  			     (start ? &start->p->knode_bus : NULL));
0edb58604   Cornelia Huck   [PATCH] driver co...
329
330
331
332
333
334
  	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...
335
  EXPORT_SYMBOL_GPL(bus_find_device);
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
336

1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
337
338
339
  static int match_name(struct device *dev, void *data)
  {
  	const char *name = data;
1e0b2cf93   Kay Sievers   driver core: stru...
340
  	return sysfs_streq(name, dev_name(dev));
1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  }
  
  /**
   * 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...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  /**
   * 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...
379
  		klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
ca22e56de   Kay Sievers   driver-core: impl...
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  		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...
399
  static struct device_driver *next_driver(struct klist_iter *i)
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
400
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
401
  	struct klist_node *n = klist_next(i);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
402
403
404
405
406
407
408
  	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...
409
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
411
412
413
414
415
   * 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
416
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
417
418
419
420
421
   * 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
422
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
423
424
425
426
427
   * 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
428
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
429
430
  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
431
  {
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
432
  	struct klist_iter i;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
433
  	struct device_driver *drv;
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
434
  	int error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435

38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
436
437
  	if (!bus)
  		return -EINVAL;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
438
439
440
441
442
  	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...
443
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
445
  EXPORT_SYMBOL_GPL(bus_for_each_drv);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446

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

ca22e56de   Kay Sievers   driver-core: impl...
497
498
  	if (!bus)
  		return;
765230b5f   Dmitry Torokhov   driver-core: add ...
499
500
  	if (bus->p->drivers_autoprobe)
  		device_initial_probe(dev);
ca22e56de   Kay Sievers   driver-core: impl...
501
502
503
504
505
506
  
  	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...
507
508
509
  }
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
510
511
   * bus_remove_device - remove device from bus
   * @dev: device to be removed
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
512
   *
ca22e56de   Kay Sievers   driver-core: impl...
513
514
   * - Remove device from all interfaces.
   * - Remove symlink from bus' directory.
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
515
516
517
   * - 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
518
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
519
  void bus_remove_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
520
  {
ca22e56de   Kay Sievers   driver-core: impl...
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
  	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_...
536
  	device_remove_groups(dev, dev->bus->dev_groups);
ca22e56de   Kay Sievers   driver-core: impl...
537
538
539
540
541
542
543
544
  	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
545
  }
f86db396f   Andrew Morton   drivers/base: che...
546
  static int __must_check add_bind_files(struct device_driver *drv)
874c6241b   Greg Kroah-Hartman   [PATCH] Driver co...
547
  {
f86db396f   Andrew Morton   drivers/base: che...
548
549
550
551
552
553
554
555
556
  	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...
557
558
559
560
561
562
563
  }
  
  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...
564

8380770c8   Kay Sievers   Driver core: make...
565
566
567
  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...
568
569
570
  static int add_probe_files(struct bus_type *bus)
  {
  	int retval;
8380770c8   Kay Sievers   Driver core: make...
571
  	retval = bus_create_file(bus, &bus_attr_drivers_probe);
b8c5cec23   Kay Sievers   Driver core: udev...
572
573
  	if (retval)
  		goto out;
8380770c8   Kay Sievers   Driver core: make...
574
  	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
b8c5cec23   Kay Sievers   Driver core: udev...
575
  	if (retval)
8380770c8   Kay Sievers   Driver core: make...
576
  		bus_remove_file(bus, &bus_attr_drivers_probe);
b8c5cec23   Kay Sievers   Driver core: udev...
577
578
579
580
581
582
  out:
  	return retval;
  }
  
  static void remove_probe_files(struct bus_type *bus)
  {
8380770c8   Kay Sievers   Driver core: make...
583
584
  	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  	bus_remove_file(bus, &bus_attr_drivers_probe);
b8c5cec23   Kay Sievers   Driver core: udev...
585
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586

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

765230b5f   Dmitry Torokhov   driver-core: add ...
595
596
597
598
599
600
601
602
603
604
605
  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
606
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
607
608
   * bus_add_driver - Add a driver to the bus.
   * @drv: driver.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
609
   */
f86db396f   Andrew Morton   drivers/base: che...
610
  int bus_add_driver(struct device_driver *drv)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
611
  {
e5dd12784   Greg Kroah-Hartman   Driver core: move...
612
613
  	struct bus_type *bus;
  	struct driver_private *priv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
614
  	int error = 0;
e5dd12784   Greg Kroah-Hartman   Driver core: move...
615
  	bus = bus_get(drv->bus);
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
616
  	if (!bus)
4f6e1945f   Greg Kroah-Hartman   driver core: bus_...
617
  		return -EINVAL;
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
618

7dc72b284   Greg Kroah-Hartman   Driver core: clea...
619
620
  	pr_debug("bus: '%s': add driver %s
  ", bus->name, drv->name);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
621
622
  
  	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
076344642   Cornelia Huck   Driver core: Fix ...
623
624
625
626
  	if (!priv) {
  		error = -ENOMEM;
  		goto out_put_bus;
  	}
e5dd12784   Greg Kroah-Hartman   Driver core: move...
627
628
629
  	klist_init(&priv->klist_devices, NULL, NULL);
  	priv->driver = drv;
  	drv->p = priv;
c8e90d822   Greg Kroah-Hartman   Kobject: change d...
630
631
632
  	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...
633
  	if (error)
076344642   Cornelia Huck   Driver core: Fix ...
634
  		goto out_unregister;
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
635

190888ac0   Ming Lei   driver core: fix ...
636
  	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
637
  	if (drv->bus->p->drivers_autoprobe) {
765230b5f   Dmitry Torokhov   driver-core: add ...
638
639
640
641
642
643
644
645
646
647
  		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...
648
  	}
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
649
  	module_add_driver(drv->owner, drv);
7ac1cf4a8   Kay Sievers   Driver core: add ...
650
651
652
653
  	error = driver_create_file(drv, &driver_attr_uevent);
  	if (error) {
  		printk(KERN_ERR "%s: uevent attr (%s) failed
  ",
2b3a302a0   Harvey Harrison   driver core: repl...
654
  			__func__, drv->name);
7ac1cf4a8   Kay Sievers   Driver core: add ...
655
  	}
e18945b15   Greg Kroah-Hartman   driver-core: remo...
656
  	error = driver_add_groups(drv, bus->drv_groups);
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
657
658
  	if (error) {
  		/* How the hell do we get out of this pickle? Give up */
ed0617b5c   Greg Kroah-Hartman   driver core: bus_...
659
660
661
  		printk(KERN_ERR "%s: driver_create_groups(%s) failed
  ",
  			__func__, drv->name);
e18945b15   Greg Kroah-Hartman   driver-core: remo...
662
  	}
1a6f2a751   Dmitry Torokhov   Driver core: allo...
663
664
665
666
667
668
669
670
671
  
  	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
672
  	}
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
673

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

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

e935d5da8   Moore, Eric   [SCSI] drivers/ba...
736
737
738
739
740
741
742
743
744
  /**
   * 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...
745
  int device_reprobe(struct device *dev)
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
746
747
748
  {
  	if (dev->driver) {
  		if (dev->parent)        /* Needed for USB */
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
749
  			device_lock(dev->parent);
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
750
751
  		device_release_driver(dev);
  		if (dev->parent)
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
752
  			device_unlock(dev->parent);
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
753
  	}
f86db396f   Andrew Morton   drivers/base: che...
754
  	return bus_rescan_devices_helper(dev, NULL);
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
755
756
  }
  EXPORT_SYMBOL_GPL(device_reprobe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
757

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
758
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
759
760
   * find_bus - locate bus by name.
   * @name: name of bus.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
762
763
   * 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
764
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
765
   * Note that kset_find_obj increments bus' reference count.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
766
   */
7e4ef085e   Adrian Bunk   [PATCH] Driver co...
767
  #if 0
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
768
  struct bus_type *find_bus(char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
769
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
770
  	struct kobject *k = kset_find_obj(bus_kset, name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
772
  	return k ? to_bus(k) : NULL;
  }
7e4ef085e   Adrian Bunk   [PATCH] Driver co...
773
  #endif  /*  0  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774

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

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

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

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

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

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

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

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
890
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
891
892
   * bus_unregister - remove a bus from the system
   * @bus: bus.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
893
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
894
895
   * 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
896
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
897
  void bus_unregister(struct bus_type *bus)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
898
  {
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
899
900
  	pr_debug("bus: '%s': unregistering
  ", bus->name);
ca22e56de   Kay Sievers   driver-core: impl...
901
902
  	if (bus->dev_root)
  		device_unregister(bus->dev_root);
12478ba07   Greg Kroah-Hartman   driver core: bus_...
903
  	bus_remove_groups(bus, bus->bus_groups);
b8c5cec23   Kay Sievers   Driver core: udev...
904
  	remove_probe_files(bus);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
905
906
  	kset_unregister(bus->p->drivers_kset);
  	kset_unregister(bus->p->devices_kset);
7ac1cf4a8   Kay Sievers   Driver core: add ...
907
  	bus_remove_file(bus, &bus_attr_uevent);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
908
  	kset_unregister(&bus->p->subsys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
909
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
910
  EXPORT_SYMBOL_GPL(bus_unregister);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
911

116af3782   Benjamin Herrenschmidt   Driver core: add ...
912
913
  int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
914
  	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
116af3782   Benjamin Herrenschmidt   Driver core: add ...
915
916
917
918
919
  }
  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...
920
  	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
116af3782   Benjamin Herrenschmidt   Driver core: add ...
921
922
  }
  EXPORT_SYMBOL_GPL(bus_unregister_notifier);
0fed80f7a   Greg Kroah-Hartman   driver core: add ...
923
924
  struct kset *bus_get_kset(struct bus_type *bus)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
925
  	return &bus->p->subsys;
0fed80f7a   Greg Kroah-Hartman   driver core: add ...
926
927
  }
  EXPORT_SYMBOL_GPL(bus_get_kset);
b249072ee   Greg Kroah-Hartman   driver core: add ...
928
929
  struct klist *bus_get_device_klist(struct bus_type *bus)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
930
  	return &bus->p->klist_devices;
b249072ee   Greg Kroah-Hartman   driver core: add ...
931
932
  }
  EXPORT_SYMBOL_GPL(bus_get_device_klist);
99178b036   Greg Kroah-Hartman   Driver core: add ...
933
  /*
dca25ebdd   Robert P. J. Day   Fix "forcably" co...
934
   * Yes, this forcibly breaks the klist abstraction temporarily.  It
99178b036   Greg Kroah-Hartman   Driver core: add ...
935
936
937
938
939
940
941
942
943
   * 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 ...
944
  	struct klist_node *n;
ae1b41715   Greg Kroah-Hartman   driver core: move...
945
  	struct device_private *dev_prv;
99178b036   Greg Kroah-Hartman   Driver core: add ...
946
  	struct device *b;
4c62785e6   Geliang Tang   driver core: bus:...
947
  	list_for_each_entry(n, list, n_node) {
ae1b41715   Greg Kroah-Hartman   driver core: move...
948
949
  		dev_prv = to_device_private_bus(n);
  		b = dev_prv->device;
99178b036   Greg Kroah-Hartman   Driver core: add ...
950
  		if (compare(a, b) <= 0) {
ae1b41715   Greg Kroah-Hartman   driver core: move...
951
952
  			list_move_tail(&a->p->knode_bus.n_node,
  				       &b->p->knode_bus.n_node);
99178b036   Greg Kroah-Hartman   Driver core: add ...
953
954
955
  			return;
  		}
  	}
ae1b41715   Greg Kroah-Hartman   driver core: move...
956
  	list_move_tail(&a->p->knode_bus.n_node, list);
99178b036   Greg Kroah-Hartman   Driver core: add ...
957
958
959
960
961
962
963
  }
  
  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:...
964
  	struct klist_node *n, *tmp;
ae1b41715   Greg Kroah-Hartman   driver core: move...
965
  	struct device_private *dev_prv;
99178b036   Greg Kroah-Hartman   Driver core: add ...
966
967
968
969
970
971
  	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:...
972
  	list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
ae1b41715   Greg Kroah-Hartman   driver core: move...
973
974
  		dev_prv = to_device_private_bus(n);
  		dev = dev_prv->device;
99178b036   Greg Kroah-Hartman   Driver core: add ...
975
976
977
978
979
980
  		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...
981
982
983
984
985
986
987
988
989
990
991
992
  /**
   * 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...
993
994
  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...
995
996
997
998
999
  {
  	struct klist_node *start_knode = NULL;
  
  	if (start)
  		start_knode = &start->p->knode_bus;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
1000
1001
  	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
  	iter->type = type;
ca22e56de   Kay Sievers   driver-core: impl...
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
  }
  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:...
1026
  		dev = to_device_private_bus(knode)->device;
ca22e56de   Kay Sievers   driver-core: impl...
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
1071
1072
1073
1074
  		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 ...
1075
  	struct bus_type *subsys;
ca22e56de   Kay Sievers   driver-core: impl...
1076
1077
  	struct subsys_dev_iter iter;
  	struct device *dev;
2b31594a9   Jonghwan Choi   driver-core: Fix ...
1078
  	if (!sif || !sif->subsys)
ca22e56de   Kay Sievers   driver-core: impl...
1079
  		return;
2b31594a9   Jonghwan Choi   driver-core: Fix ...
1080
  	subsys = sif->subsys;
ca22e56de   Kay Sievers   driver-core: impl...
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
  	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...
1099
1100
1101
1102
  
  static int subsys_register(struct bus_type *subsys,
  			   const struct attribute_group **groups,
  			   struct kobject *parent_of_root)
ca22e56de   Kay Sievers   driver-core: impl...
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
  {
  	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...
1120
  	dev->kobj.parent = parent_of_root;
ca22e56de   Kay Sievers   driver-core: impl...
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
  	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...
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
  
  /**
   * 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...
1150
   * number appended. The registered devices are not explicitly named;
d73ce0042   Tejun Heo   driver/base: impl...
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
   * 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...
1164
  EXPORT_SYMBOL_GPL(subsys_system_register);
d73ce0042   Tejun Heo   driver/base: impl...
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
  /**
   * 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...
1187
  EXPORT_SYMBOL_GPL(subsys_virtual_register);
d73ce0042   Tejun Heo   driver/base: impl...
1188

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