Blame view

drivers/base/bus.c 30.2 KB
989d42e85   Greg Kroah-Hartman   driver core: add ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
  /*
   * 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...
7
8
   * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
   * Copyright (c) 2007 Novell Inc.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
   */
765230b5f   Dmitry Torokhov   driver-core: add ...
10
  #include <linux/async.h>
5aee2bf26   Greg Kroah-Hartman   device.h: move 's...
11
  #include <linux/device/bus.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
14
  #include <linux/device.h>
  #include <linux/module.h>
  #include <linux/errno.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
15
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
17
  #include <linux/init.h>
  #include <linux/string.h>
ca22e56de   Kay Sievers   driver-core: impl...
18
  #include <linux/mutex.h>
639676856   Greg Kroah-Hartman   driver core: add ...
19
  #include <linux/sysfs.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  #include "base.h"
  #include "power/power.h"
ca22e56de   Kay Sievers   driver-core: impl...
22
  /* /sys/devices/system */
97ec448ae   H Hartley Sweeten   drivers/base/bus....
23
  static struct kset *system_kset;
ca22e56de   Kay Sievers   driver-core: impl...
24

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

4f4b37433   Daniel Vetter   sysfs: Disable lo...
33
34
35
  #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
36

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

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

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

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

2b08c8d04   Alan Stern   [PATCH] Small fix...
166
  /* Manually detach a device from its associated driver. */
2581c9cc0   Greg Kroah-Hartman   driver core: bus:...
167
168
  static ssize_t unbind_store(struct device_driver *drv, const char *buf,
  			    size_t count)
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
169
  {
5901d0145   Greg Kroah-Hartman   Driver core: remo...
170
  	struct bus_type *bus = bus_get(drv->bus);
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
171
172
  	struct device *dev;
  	int err = -ENODEV;
1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
173
  	dev = bus_find_device_by_name(bus, NULL, buf);
2b08c8d04   Alan Stern   [PATCH] Small fix...
174
  	if (dev && dev->driver == drv) {
ed88747c6   Alexander Duyck   device core: Cons...
175
  		device_driver_detach(dev);
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
176
177
  		err = count;
  	}
2b08c8d04   Alan Stern   [PATCH] Small fix...
178
  	put_device(dev);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
179
  	bus_put(bus);
2b08c8d04   Alan Stern   [PATCH] Small fix...
180
  	return err;
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
181
  }
4f4b37433   Daniel Vetter   sysfs: Disable lo...
182
  static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, S_IWUSR, NULL, unbind_store);
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
183

afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
184
185
186
187
188
  /*
   * 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:...
189
190
  static ssize_t bind_store(struct device_driver *drv, const char *buf,
  			  size_t count)
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
191
  {
5901d0145   Greg Kroah-Hartman   Driver core: remo...
192
  	struct bus_type *bus = bus_get(drv->bus);
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
193
194
  	struct device *dev;
  	int err = -ENODEV;
1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
195
  	dev = bus_find_device_by_name(bus, NULL, buf);
49b420a13   Ming Lei   driver core: chec...
196
  	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
ed88747c6   Alexander Duyck   device core: Cons...
197
  		err = device_driver_attach(drv, dev);
372254018   Ryan Wilson   [PATCH] driver co...
198

4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
199
200
  		if (err > 0) {
  			/* success */
372254018   Ryan Wilson   [PATCH] driver co...
201
  			err = count;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
202
203
  		} else if (err == 0) {
  			/* driver didn't accept device */
372254018   Ryan Wilson   [PATCH] driver co...
204
  			err = -ENODEV;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
205
  		}
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
206
  	}
2b08c8d04   Alan Stern   [PATCH] Small fix...
207
  	put_device(dev);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
208
  	bus_put(bus);
2b08c8d04   Alan Stern   [PATCH] Small fix...
209
  	return err;
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
210
  }
4f4b37433   Daniel Vetter   sysfs: Disable lo...
211
  static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store);
afdce75f1   Greg Kroah-Hartman   [PATCH] driver co...
212

2e7189b6c   Greg Kroah-Hartman   driver core: bus:...
213
  static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf)
b8c5cec23   Kay Sievers   Driver core: udev...
214
  {
948b3edba   Joe Perches   drivers core: Mis...
215
216
  	return sysfs_emit(buf, "%d
  ", bus->p->drivers_autoprobe);
b8c5cec23   Kay Sievers   Driver core: udev...
217
  }
2e7189b6c   Greg Kroah-Hartman   driver core: bus:...
218
  static ssize_t drivers_autoprobe_store(struct bus_type *bus,
b8c5cec23   Kay Sievers   Driver core: udev...
219
220
221
  				       const char *buf, size_t count)
  {
  	if (buf[0] == '0')
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
222
  		bus->p->drivers_autoprobe = 0;
b8c5cec23   Kay Sievers   Driver core: udev...
223
  	else
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
224
  		bus->p->drivers_autoprobe = 1;
b8c5cec23   Kay Sievers   Driver core: udev...
225
226
  	return count;
  }
2e7189b6c   Greg Kroah-Hartman   driver core: bus:...
227
  static ssize_t drivers_probe_store(struct bus_type *bus,
b8c5cec23   Kay Sievers   Driver core: udev...
228
229
230
  				   const char *buf, size_t count)
  {
  	struct device *dev;
0372ffb35   Alex Williamson   driver core: Fix ...
231
  	int err = -EINVAL;
b8c5cec23   Kay Sievers   Driver core: udev...
232

1f9ffc049   Greg Kroah-Hartman   Driver core: add ...
233
  	dev = bus_find_device_by_name(bus, NULL, buf);
b8c5cec23   Kay Sievers   Driver core: udev...
234
235
  	if (!dev)
  		return -ENODEV;
0372ffb35   Alex Williamson   driver core: Fix ...
236
237
238
239
  	if (bus_rescan_devices_helper(dev, NULL) == 0)
  		err = count;
  	put_device(dev);
  	return err;
b8c5cec23   Kay Sievers   Driver core: udev...
240
  }
151ef38f7   Greg Kroah-Hartman   [PATCH] driver co...
241

4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
242
  static struct device *next_device(struct klist_iter *i)
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
243
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
244
  	struct klist_node *n = klist_next(i);
ae1b41715   Greg Kroah-Hartman   driver core: move...
245
246
247
248
249
250
251
252
  	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...
253
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
255
256
257
258
259
   * 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
260
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
261
262
263
   * 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
264
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
265
266
   * 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
267
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
268
269
   * 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...
270
   * to retain this data, it should do so, and increment the reference
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
271
   * count in the supplied callback.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
273
274
  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
275
  {
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
276
  	struct klist_iter i;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
277
  	struct device *dev;
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
278
  	int error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279

4fa3e78be   Bjorn Helgaas   Driver core: trea...
280
  	if (!bus || !bus->p)
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
281
  		return -EINVAL;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
282
283
  	klist_iter_init_node(&bus->p->klist_devices, &i,
  			     (start ? &start->p->knode_bus : NULL));
93ead7c94   Gimcuan Hui   drivers: base: om...
284
  	while (!error && (dev = next_device(&i)))
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
285
286
  		error = fn(dev, data);
  	klist_iter_exit(&i);
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
287
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
289
  EXPORT_SYMBOL_GPL(bus_for_each_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290

0edb58604   Cornelia Huck   [PATCH] driver co...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  /**
   * 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...
306
  struct device *bus_find_device(struct bus_type *bus,
418e3ea15   Suzuki K Poulose   bus_find_device: ...
307
308
  			       struct device *start, const void *data,
  			       int (*match)(struct device *dev, const void *data))
0edb58604   Cornelia Huck   [PATCH] driver co...
309
310
311
  {
  	struct klist_iter i;
  	struct device *dev;
4fa3e78be   Bjorn Helgaas   Driver core: trea...
312
  	if (!bus || !bus->p)
0edb58604   Cornelia Huck   [PATCH] driver co...
313
  		return NULL;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
314
315
  	klist_iter_init_node(&bus->p->klist_devices, &i,
  			     (start ? &start->p->knode_bus : NULL));
0edb58604   Cornelia Huck   [PATCH] driver co...
316
317
318
319
320
321
  	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...
322
  EXPORT_SYMBOL_GPL(bus_find_device);
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
323

ca22e56de   Kay Sievers   driver-core: impl...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  /**
   * 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...
344
  		klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
ca22e56de   Kay Sievers   driver-core: impl...
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
  		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);
b8ef4c9af   Ioana Ciornei   drivers: base: ex...
364
  struct device_driver *next_driver(struct klist_iter *i)
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
365
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
366
  	struct klist_node *n = klist_next(i);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
367
368
369
370
371
372
373
  	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...
374
  }
b8ef4c9af   Ioana Ciornei   drivers: base: ex...
375
  EXPORT_SYMBOL_GPL(next_driver);
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
376

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
378
379
380
381
382
   * 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
383
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
384
385
386
387
388
   * 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
389
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
390
391
392
393
394
   * 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
395
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
396
397
  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
398
  {
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
399
  	struct klist_iter i;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
400
  	struct device_driver *drv;
38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
401
  	int error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402

38fdac3cd   Patrick Mochel   [PATCH] Add a kli...
403
404
  	if (!bus)
  		return -EINVAL;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
405
406
407
408
409
  	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...
410
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
411
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
412
  EXPORT_SYMBOL_GPL(bus_for_each_drv);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
415
416
   * bus_add_device - add device to bus
   * @dev: device being added
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417
   *
2023c610d   Alan Stern   Driver core: add ...
418
419
   * - Add device's bus attributes.
   * - Create links to device's bus.
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
420
   * - Add the device to its bus's list of devices.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
422
  int bus_add_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
424
  	struct bus_type *bus = bus_get(dev->bus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
426
427
  	int error = 0;
  
  	if (bus) {
1e0b2cf93   Kay Sievers   driver core: stru...
428
429
  		pr_debug("bus: '%s': add device %s
  ", bus->name, dev_name(dev));
fa6fdb33b   Greg Kroah-Hartman   driver core: bus_...
430
431
  		error = device_add_groups(dev, bus->dev_groups);
  		if (error)
b46c73378   Greg Kroah-Hartman   driver-core: remo...
432
  			goto out_put;
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
433
  		error = sysfs_create_link(&bus->p->devices_kset->kobj,
1e0b2cf93   Kay Sievers   driver core: stru...
434
  						&dev->kobj, dev_name(dev));
f86db396f   Andrew Morton   drivers/base: che...
435
  		if (error)
1c34203a1   Junjie Mao   driver core: bus:...
436
  			goto out_groups;
f86db396f   Andrew Morton   drivers/base: che...
437
  		error = sysfs_create_link(&dev->kobj,
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
438
  				&dev->bus->p->subsys.kobj, "subsystem");
f86db396f   Andrew Morton   drivers/base: che...
439
  		if (error)
513e7337a   Cornelia Huck   driver core fixes...
440
  			goto out_subsys;
2023c610d   Alan Stern   Driver core: add ...
441
  		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
  	}
513e7337a   Cornelia Huck   driver core fixes...
443
  	return 0;
513e7337a   Cornelia Huck   driver core fixes...
444
  out_subsys:
1e0b2cf93   Kay Sievers   driver core: stru...
445
  	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
fa6fdb33b   Greg Kroah-Hartman   driver core: bus_...
446
447
  out_groups:
  	device_remove_groups(dev, bus->dev_groups);
513e7337a   Cornelia Huck   driver core fixes...
448
  out_put:
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
449
  	bus_put(dev->bus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
452
453
  	return error;
  }
  
  /**
2023c610d   Alan Stern   Driver core: add ...
454
455
   * bus_probe_device - probe drivers for a new device
   * @dev: device to probe
53877d06d   Kay Sievers   [PATCH] Driver co...
456
   *
2023c610d   Alan Stern   Driver core: add ...
457
   * - Automatically probe for a driver if the bus allows it.
53877d06d   Kay Sievers   [PATCH] Driver co...
458
   */
2023c610d   Alan Stern   Driver core: add ...
459
  void bus_probe_device(struct device *dev)
53877d06d   Kay Sievers   [PATCH] Driver co...
460
  {
f86db396f   Andrew Morton   drivers/base: che...
461
  	struct bus_type *bus = dev->bus;
ca22e56de   Kay Sievers   driver-core: impl...
462
  	struct subsys_interface *sif;
53877d06d   Kay Sievers   [PATCH] Driver co...
463

ca22e56de   Kay Sievers   driver-core: impl...
464
465
  	if (!bus)
  		return;
765230b5f   Dmitry Torokhov   driver-core: add ...
466
467
  	if (bus->p->drivers_autoprobe)
  		device_initial_probe(dev);
ca22e56de   Kay Sievers   driver-core: impl...
468
469
470
471
472
473
  
  	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...
474
475
476
  }
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
477
478
   * bus_remove_device - remove device from bus
   * @dev: device to be removed
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
   *
ca22e56de   Kay Sievers   driver-core: impl...
480
481
   * - Remove device from all interfaces.
   * - Remove symlink from bus' directory.
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
482
483
484
   * - 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
485
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
486
  void bus_remove_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
  {
ca22e56de   Kay Sievers   driver-core: impl...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
  	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_...
503
  	device_remove_groups(dev, dev->bus->dev_groups);
ca22e56de   Kay Sievers   driver-core: impl...
504
505
506
507
508
509
510
511
  	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
512
  }
f86db396f   Andrew Morton   drivers/base: che...
513
  static int __must_check add_bind_files(struct device_driver *drv)
874c6241b   Greg Kroah-Hartman   [PATCH] Driver co...
514
  {
f86db396f   Andrew Morton   drivers/base: che...
515
516
517
518
519
520
521
522
523
  	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...
524
525
526
527
528
529
530
  }
  
  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...
531

2e7189b6c   Greg Kroah-Hartman   driver core: bus:...
532
533
  static BUS_ATTR_WO(drivers_probe);
  static BUS_ATTR_RW(drivers_autoprobe);
8380770c8   Kay Sievers   Driver core: make...
534

b8c5cec23   Kay Sievers   Driver core: udev...
535
536
537
  static int add_probe_files(struct bus_type *bus)
  {
  	int retval;
8380770c8   Kay Sievers   Driver core: make...
538
  	retval = bus_create_file(bus, &bus_attr_drivers_probe);
b8c5cec23   Kay Sievers   Driver core: udev...
539
540
  	if (retval)
  		goto out;
8380770c8   Kay Sievers   Driver core: make...
541
  	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
b8c5cec23   Kay Sievers   Driver core: udev...
542
  	if (retval)
8380770c8   Kay Sievers   Driver core: make...
543
  		bus_remove_file(bus, &bus_attr_drivers_probe);
b8c5cec23   Kay Sievers   Driver core: udev...
544
545
546
547
548
549
  out:
  	return retval;
  }
  
  static void remove_probe_files(struct bus_type *bus)
  {
8380770c8   Kay Sievers   Driver core: make...
550
551
  	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  	bus_remove_file(bus, &bus_attr_drivers_probe);
b8c5cec23   Kay Sievers   Driver core: udev...
552
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
553

2581c9cc0   Greg Kroah-Hartman   driver core: bus:...
554
555
  static ssize_t uevent_store(struct device_driver *drv, const char *buf,
  			    size_t count)
7ac1cf4a8   Kay Sievers   Driver core: add ...
556
  {
df44b4796   Peter Rajnoha   kobject: return e...
557
558
559
560
  	int rc;
  
  	rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
  	return rc ? rc : count;
7ac1cf4a8   Kay Sievers   Driver core: add ...
561
  }
2581c9cc0   Greg Kroah-Hartman   driver core: bus:...
562
  static DRIVER_ATTR_WO(uevent);
7ac1cf4a8   Kay Sievers   Driver core: add ...
563

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
565
566
   * bus_add_driver - Add a driver to the bus.
   * @drv: driver.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
567
   */
f86db396f   Andrew Morton   drivers/base: che...
568
  int bus_add_driver(struct device_driver *drv)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
  {
e5dd12784   Greg Kroah-Hartman   Driver core: move...
570
571
  	struct bus_type *bus;
  	struct driver_private *priv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572
  	int error = 0;
e5dd12784   Greg Kroah-Hartman   Driver core: move...
573
  	bus = bus_get(drv->bus);
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
574
  	if (!bus)
4f6e1945f   Greg Kroah-Hartman   driver core: bus_...
575
  		return -EINVAL;
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
576

7dc72b284   Greg Kroah-Hartman   Driver core: clea...
577
578
  	pr_debug("bus: '%s': add driver %s
  ", bus->name, drv->name);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
579
580
  
  	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
076344642   Cornelia Huck   Driver core: Fix ...
581
582
583
584
  	if (!priv) {
  		error = -ENOMEM;
  		goto out_put_bus;
  	}
e5dd12784   Greg Kroah-Hartman   Driver core: move...
585
586
587
  	klist_init(&priv->klist_devices, NULL, NULL);
  	priv->driver = drv;
  	drv->p = priv;
c8e90d822   Greg Kroah-Hartman   Kobject: change d...
588
589
590
  	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...
591
  	if (error)
076344642   Cornelia Huck   Driver core: Fix ...
592
  		goto out_unregister;
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
593

190888ac0   Ming Lei   driver core: fix ...
594
  	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
595
  	if (drv->bus->p->drivers_autoprobe) {
ef0ff6835   Alexander Duyck   driver core: Prob...
596
597
598
  		error = driver_attach(drv);
  		if (error)
  			goto out_unregister;
b8c5cec23   Kay Sievers   Driver core: udev...
599
  	}
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
600
  	module_add_driver(drv->owner, drv);
7ac1cf4a8   Kay Sievers   Driver core: add ...
601
602
603
604
  	error = driver_create_file(drv, &driver_attr_uevent);
  	if (error) {
  		printk(KERN_ERR "%s: uevent attr (%s) failed
  ",
2b3a302a0   Harvey Harrison   driver core: repl...
605
  			__func__, drv->name);
7ac1cf4a8   Kay Sievers   Driver core: add ...
606
  	}
e18945b15   Greg Kroah-Hartman   driver-core: remo...
607
  	error = driver_add_groups(drv, bus->drv_groups);
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
608
609
  	if (error) {
  		/* How the hell do we get out of this pickle? Give up */
ed0617b5c   Greg Kroah-Hartman   driver core: bus_...
610
611
612
  		printk(KERN_ERR "%s: driver_create_groups(%s) failed
  ",
  			__func__, drv->name);
e18945b15   Greg Kroah-Hartman   driver-core: remo...
613
  	}
1a6f2a751   Dmitry Torokhov   Driver core: allo...
614
615
616
617
618
619
620
621
622
  
  	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
623
  	}
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
624

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

f86db396f   Andrew Morton   drivers/base: che...
627
  out_unregister:
99b28f1b4   Phil Carmody   driver core: Prev...
628
  	kobject_put(&priv->kobj);
0f9b011d3   Christophe JAILLET   driver core: bus:...
629
  	/* drv->p is freed in driver_release()  */
5c8563d77   Kay Sievers   Driver Core: do n...
630
  	drv->p = NULL;
f86db396f   Andrew Morton   drivers/base: che...
631
  out_put_bus:
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
632
  	bus_put(bus);
f86db396f   Andrew Morton   drivers/base: che...
633
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
636
637
   * bus_remove_driver - delete driver from bus's knowledge.
   * @drv: driver.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
638
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
639
640
641
   * 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
642
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
643
  void bus_remove_driver(struct device_driver *drv)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
  {
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
645
646
  	if (!drv->bus)
  		return;
1a6f2a751   Dmitry Torokhov   Driver core: allo...
647
648
  	if (!drv->suppress_bind_attrs)
  		remove_bind_files(drv);
ed0617b5c   Greg Kroah-Hartman   driver core: bus_...
649
  	driver_remove_groups(drv, drv->bus->drv_groups);
7ac1cf4a8   Kay Sievers   Driver core: add ...
650
  	driver_remove_file(drv, &driver_attr_uevent);
e5dd12784   Greg Kroah-Hartman   Driver core: move...
651
  	klist_remove(&drv->p->knode_bus);
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
652
653
  	pr_debug("bus: '%s': remove driver %s
  ", drv->bus->name, drv->name);
d9fd4d3b3   Jeff Garzik   Driver core: bus:...
654
655
  	driver_detach(drv);
  	module_remove_driver(drv);
c10997f65   Greg Kroah-Hartman   Kobject: convert ...
656
  	kobject_put(&drv->p->kobj);
fc1ede588   Greg Kroah-Hartman   Driver core: remo...
657
  	bus_put(drv->bus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
  /* Helper for bus_rescan_devices's iter */
f86db396f   Andrew Morton   drivers/base: che...
660
  static int __must_check bus_rescan_devices_helper(struct device *dev,
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
661
  						  void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
662
  {
f86db396f   Andrew Morton   drivers/base: che...
663
  	int ret = 0;
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
664
  	if (!dev->driver) {
8c97a46af   Martin Liu   driver core: hold...
665
  		if (dev->parent && dev->bus->need_parent_lock)
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
666
  			device_lock(dev->parent);
f86db396f   Andrew Morton   drivers/base: che...
667
  		ret = device_attach(dev);
8c97a46af   Martin Liu   driver core: hold...
668
  		if (dev->parent && dev->bus->need_parent_lock)
8e9394ce2   Greg Kroah-Hartman   Driver core: crea...
669
  			device_unlock(dev->parent);
bf74ad5bc   Alan Stern   [PATCH] Hold the ...
670
  	}
f86db396f   Andrew Morton   drivers/base: che...
671
  	return ret < 0 ? ret : 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
672
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
  /**
23d3d602c   Greg Kroah-Hartman   [PATCH] driver co...
674
675
   * bus_rescan_devices - rescan devices on the bus for possible drivers
   * @bus: the bus to scan.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
676
   *
23d3d602c   Greg Kroah-Hartman   [PATCH] driver co...
677
678
679
   * 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
680
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
681
  int bus_rescan_devices(struct bus_type *bus)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
  {
f86db396f   Andrew Morton   drivers/base: che...
683
  	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
684
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
685
  EXPORT_SYMBOL_GPL(bus_rescan_devices);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686

e935d5da8   Moore, Eric   [SCSI] drivers/ba...
687
688
689
690
691
692
693
694
695
  /**
   * 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...
696
  int device_reprobe(struct device *dev)
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
697
  {
ed88747c6   Alexander Duyck   device core: Cons...
698
699
  	if (dev->driver)
  		device_driver_detach(dev);
f86db396f   Andrew Morton   drivers/base: che...
700
  	return bus_rescan_devices_helper(dev, NULL);
e935d5da8   Moore, Eric   [SCSI] drivers/ba...
701
702
  }
  EXPORT_SYMBOL_GPL(device_reprobe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
705
706
   * find_bus - locate bus by name.
   * @name: name of bus.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
707
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
708
709
   * 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
710
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
711
   * Note that kset_find_obj increments bus' reference count.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
712
   */
7e4ef085e   Adrian Bunk   [PATCH] Driver co...
713
  #if 0
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
714
  struct bus_type *find_bus(char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
716
  	struct kobject *k = kset_find_obj(bus_kset, name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
718
  	return k ? to_bus(k) : NULL;
  }
7e4ef085e   Adrian Bunk   [PATCH] Driver co...
719
  #endif  /*  0  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720

12478ba07   Greg Kroah-Hartman   driver core: bus_...
721
722
723
  static int bus_add_groups(struct bus_type *bus,
  			  const struct attribute_group **groups)
  {
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
724
  	return sysfs_create_groups(&bus->p->subsys.kobj, groups);
12478ba07   Greg Kroah-Hartman   driver core: bus_...
725
726
727
728
729
  }
  
  static void bus_remove_groups(struct bus_type *bus,
  			      const struct attribute_group **groups)
  {
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
730
  	sysfs_remove_groups(&bus->p->subsys.kobj, groups);
12478ba07   Greg Kroah-Hartman   driver core: bus_...
731
  }
34bb61f9d   James Bottomley   [PATCH] fix klist...
732
733
  static void klist_devices_get(struct klist_node *n)
  {
ae1b41715   Greg Kroah-Hartman   driver core: move...
734
735
  	struct device_private *dev_prv = to_device_private_bus(n);
  	struct device *dev = dev_prv->device;
34bb61f9d   James Bottomley   [PATCH] fix klist...
736
737
738
739
740
741
  
  	get_device(dev);
  }
  
  static void klist_devices_put(struct klist_node *n)
  {
ae1b41715   Greg Kroah-Hartman   driver core: move...
742
743
  	struct device_private *dev_prv = to_device_private_bus(n);
  	struct device *dev = dev_prv->device;
34bb61f9d   James Bottomley   [PATCH] fix klist...
744
745
746
  
  	put_device(dev);
  }
7ac1cf4a8   Kay Sievers   Driver core: add ...
747
748
749
  static ssize_t bus_uevent_store(struct bus_type *bus,
  				const char *buf, size_t count)
  {
df44b4796   Peter Rajnoha   kobject: return e...
750
751
752
753
  	int rc;
  
  	rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
  	return rc ? rc : count;
7ac1cf4a8   Kay Sievers   Driver core: add ...
754
  }
a47230418   Greg Kroah-Hartman   driver core: drop...
755
756
757
758
759
760
761
762
  /*
   * "open code" the old BUS_ATTR() macro here.  We want to use BUS_ATTR_WO()
   * here, but can not use it as earlier in the file we have
   * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
   * function name.
   */
  static struct bus_attribute bus_attr_uevent = __ATTR(uevent, S_IWUSR, NULL,
  						     bus_uevent_store);
7ac1cf4a8   Kay Sievers   Driver core: add ...
763

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
764
  /**
be871b7e5   Michal Hocko   device: separate ...
765
   * bus_register - register a driver-core subsystem
78d79559f   Randy Dunlap   kernel-doc: fix n...
766
   * @bus: bus to register
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
767
   *
78d79559f   Randy Dunlap   kernel-doc: fix n...
768
   * Once we have that, we register the bus with the kobject
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
769
   * infrastructure, then register the children subsystems it has:
ca22e56de   Kay Sievers   driver-core: impl...
770
   * the devices and drivers that belong to the subsystem.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
   */
be871b7e5   Michal Hocko   device: separate ...
772
  int bus_register(struct bus_type *bus)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
773
774
  {
  	int retval;
6b6e39a6a   Kay Sievers   driver-core: merg...
775
  	struct subsys_private *priv;
be871b7e5   Michal Hocko   device: separate ...
776
  	struct lock_class_key *key = &bus->lock_key;
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
777

6b6e39a6a   Kay Sievers   driver-core: merg...
778
  	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
779
780
781
782
783
  	if (!priv)
  		return -ENOMEM;
  
  	priv->bus = bus;
  	bus->p = priv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784

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

c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
787
  	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
  	if (retval)
  		goto out;
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
790
791
792
  	priv->subsys.kobj.kset = bus_kset;
  	priv->subsys.kobj.ktype = &bus_ktype;
  	priv->drivers_autoprobe = 1;
d6b05b84e   Greg Kroah-Hartman   Driver core: remo...
793

c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
794
  	retval = kset_register(&priv->subsys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
795
796
  	if (retval)
  		goto out;
7ac1cf4a8   Kay Sievers   Driver core: add ...
797
798
799
  	retval = bus_create_file(bus, &bus_attr_uevent);
  	if (retval)
  		goto bus_uevent_fail;
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
800
801
802
  	priv->devices_kset = kset_create_and_add("devices", NULL,
  						 &priv->subsys.kobj);
  	if (!priv->devices_kset) {
3d8995963   Greg Kroah-Hartman   kset: convert str...
803
  		retval = -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
804
  		goto bus_devices_fail;
3d8995963   Greg Kroah-Hartman   kset: convert str...
805
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806

c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
807
808
809
  	priv->drivers_kset = kset_create_and_add("drivers", NULL,
  						 &priv->subsys.kobj);
  	if (!priv->drivers_kset) {
6dcec2511   Greg Kroah-Hartman   kset: convert str...
810
  		retval = -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
811
  		goto bus_drivers_fail;
6dcec2511   Greg Kroah-Hartman   kset: convert str...
812
  	}
465c7a3a3   Patrick Mochel   [PATCH] Add a kli...
813

ca22e56de   Kay Sievers   driver-core: impl...
814
815
  	INIT_LIST_HEAD(&priv->interfaces);
  	__mutex_init(&priv->mutex, "subsys mutex", key);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
816
817
  	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  	klist_init(&priv->klist_drivers, NULL, NULL);
b8c5cec23   Kay Sievers   Driver core: udev...
818

b8c5cec23   Kay Sievers   Driver core: udev...
819
820
821
  	retval = add_probe_files(bus);
  	if (retval)
  		goto bus_probe_files_fail;
12478ba07   Greg Kroah-Hartman   driver core: bus_...
822
823
824
  	retval = bus_add_groups(bus, bus->bus_groups);
  	if (retval)
  		goto bus_groups_fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825

7dc72b284   Greg Kroah-Hartman   Driver core: clea...
826
827
  	pr_debug("bus: '%s': registered
  ", bus->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
  	return 0;
12478ba07   Greg Kroah-Hartman   driver core: bus_...
829
  bus_groups_fail:
b8c5cec23   Kay Sievers   Driver core: udev...
830
831
  	remove_probe_files(bus);
  bus_probe_files_fail:
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
832
  	kset_unregister(bus->p->drivers_kset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833
  bus_drivers_fail:
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
834
  	kset_unregister(bus->p->devices_kset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
835
  bus_devices_fail:
7ac1cf4a8   Kay Sievers   Driver core: add ...
836
837
  	bus_remove_file(bus, &bus_attr_uevent);
  bus_uevent_fail:
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
838
  	kset_unregister(&bus->p->subsys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
839
  out:
600c20f34   Jike Song   driver core: fix ...
840
  	kfree(bus->p);
f48f3febb   Dave Young   driver-core: do n...
841
  	bus->p = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842
843
  	return retval;
  }
be871b7e5   Michal Hocko   device: separate ...
844
  EXPORT_SYMBOL_GPL(bus_register);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
847
848
   * bus_unregister - remove a bus from the system
   * @bus: bus.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
849
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
850
851
   * 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
852
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
853
  void bus_unregister(struct bus_type *bus)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
854
  {
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
855
856
  	pr_debug("bus: '%s': unregistering
  ", bus->name);
ca22e56de   Kay Sievers   driver-core: impl...
857
858
  	if (bus->dev_root)
  		device_unregister(bus->dev_root);
12478ba07   Greg Kroah-Hartman   driver core: bus_...
859
  	bus_remove_groups(bus, bus->bus_groups);
b8c5cec23   Kay Sievers   Driver core: udev...
860
  	remove_probe_files(bus);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
861
862
  	kset_unregister(bus->p->drivers_kset);
  	kset_unregister(bus->p->devices_kset);
7ac1cf4a8   Kay Sievers   Driver core: add ...
863
  	bus_remove_file(bus, &bus_attr_uevent);
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
864
  	kset_unregister(&bus->p->subsys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
865
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
866
  EXPORT_SYMBOL_GPL(bus_unregister);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
867

116af3782   Benjamin Herrenschmidt   Driver core: add ...
868
869
  int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
870
  	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
116af3782   Benjamin Herrenschmidt   Driver core: add ...
871
872
873
874
875
  }
  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...
876
  	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
116af3782   Benjamin Herrenschmidt   Driver core: add ...
877
878
  }
  EXPORT_SYMBOL_GPL(bus_unregister_notifier);
0fed80f7a   Greg Kroah-Hartman   driver core: add ...
879
880
  struct kset *bus_get_kset(struct bus_type *bus)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
881
  	return &bus->p->subsys;
0fed80f7a   Greg Kroah-Hartman   driver core: add ...
882
883
  }
  EXPORT_SYMBOL_GPL(bus_get_kset);
b249072ee   Greg Kroah-Hartman   driver core: add ...
884
885
  struct klist *bus_get_device_klist(struct bus_type *bus)
  {
c6f7e72a3   Greg Kroah-Hartman   driver core: remo...
886
  	return &bus->p->klist_devices;
b249072ee   Greg Kroah-Hartman   driver core: add ...
887
888
  }
  EXPORT_SYMBOL_GPL(bus_get_device_klist);
99178b036   Greg Kroah-Hartman   Driver core: add ...
889
  /*
dca25ebdd   Robert P. J. Day   Fix "forcably" co...
890
   * Yes, this forcibly breaks the klist abstraction temporarily.  It
99178b036   Greg Kroah-Hartman   Driver core: add ...
891
892
893
894
895
896
897
898
899
   * 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 ...
900
  	struct klist_node *n;
ae1b41715   Greg Kroah-Hartman   driver core: move...
901
  	struct device_private *dev_prv;
99178b036   Greg Kroah-Hartman   Driver core: add ...
902
  	struct device *b;
4c62785e6   Geliang Tang   driver core: bus:...
903
  	list_for_each_entry(n, list, n_node) {
ae1b41715   Greg Kroah-Hartman   driver core: move...
904
905
  		dev_prv = to_device_private_bus(n);
  		b = dev_prv->device;
99178b036   Greg Kroah-Hartman   Driver core: add ...
906
  		if (compare(a, b) <= 0) {
ae1b41715   Greg Kroah-Hartman   driver core: move...
907
908
  			list_move_tail(&a->p->knode_bus.n_node,
  				       &b->p->knode_bus.n_node);
99178b036   Greg Kroah-Hartman   Driver core: add ...
909
910
911
  			return;
  		}
  	}
ae1b41715   Greg Kroah-Hartman   driver core: move...
912
  	list_move_tail(&a->p->knode_bus.n_node, list);
99178b036   Greg Kroah-Hartman   Driver core: add ...
913
914
915
916
917
918
919
  }
  
  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:...
920
  	struct klist_node *n, *tmp;
ae1b41715   Greg Kroah-Hartman   driver core: move...
921
  	struct device_private *dev_prv;
99178b036   Greg Kroah-Hartman   Driver core: add ...
922
923
924
925
926
927
  	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:...
928
  	list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
ae1b41715   Greg Kroah-Hartman   driver core: move...
929
930
  		dev_prv = to_device_private_bus(n);
  		dev = dev_prv->device;
99178b036   Greg Kroah-Hartman   Driver core: add ...
931
932
933
934
935
936
  		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...
937
938
939
940
941
942
943
944
945
946
947
948
  /**
   * 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...
949
950
  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...
951
952
953
954
955
  {
  	struct klist_node *start_knode = NULL;
  
  	if (start)
  		start_knode = &start->p->knode_bus;
7cd9c9bb5   Greg Kroah-Hartman   Revert "driver co...
956
957
  	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
  	iter->type = type;
ca22e56de   Kay Sievers   driver-core: impl...
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
  }
  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:...
982
  		dev = to_device_private_bus(knode)->device;
ca22e56de   Kay Sievers   driver-core: impl...
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
  		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 ...
1031
  	struct bus_type *subsys;
ca22e56de   Kay Sievers   driver-core: impl...
1032
1033
  	struct subsys_dev_iter iter;
  	struct device *dev;
2b31594a9   Jonghwan Choi   driver-core: Fix ...
1034
  	if (!sif || !sif->subsys)
ca22e56de   Kay Sievers   driver-core: impl...
1035
  		return;
2b31594a9   Jonghwan Choi   driver-core: Fix ...
1036
  	subsys = sif->subsys;
ca22e56de   Kay Sievers   driver-core: impl...
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
  	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...
1055
1056
1057
1058
  
  static int subsys_register(struct bus_type *subsys,
  			   const struct attribute_group **groups,
  			   struct kobject *parent_of_root)
ca22e56de   Kay Sievers   driver-core: impl...
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
  {
  	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...
1076
  	dev->kobj.parent = parent_of_root;
ca22e56de   Kay Sievers   driver-core: impl...
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
  	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...
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
  
  /**
   * 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...
1106
   * number appended. The registered devices are not explicitly named;
d73ce0042   Tejun Heo   driver/base: impl...
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
   * 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...
1120
  EXPORT_SYMBOL_GPL(subsys_system_register);
d73ce0042   Tejun Heo   driver/base: impl...
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
  /**
   * 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...
1143
  EXPORT_SYMBOL_GPL(subsys_virtual_register);
d73ce0042   Tejun Heo   driver/base: impl...
1144

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1145
1146
  int __init buses_init(void)
  {
59a548338   Greg Kroah-Hartman   kset: convert dri...
1147
1148
1149
  	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  	if (!bus_kset)
  		return -ENOMEM;
ca22e56de   Kay Sievers   driver-core: impl...
1150
1151
1152
1153
  
  	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
  	if (!system_kset)
  		return -ENOMEM;
59a548338   Greg Kroah-Hartman   kset: convert dri...
1154
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1155
  }