]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * bus.c - bus driver management | |
3 | * | |
4 | * Copyright (c) 2002-3 Patrick Mochel | |
5 | * Copyright (c) 2002-3 Open Source Development Labs | |
6 | * | |
7 | * This file is released under the GPLv2 | |
8 | * | |
9 | */ | |
10 | ||
1da177e4 LT |
11 | #include <linux/device.h> |
12 | #include <linux/module.h> | |
13 | #include <linux/errno.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/string.h> | |
16 | #include "base.h" | |
17 | #include "power/power.h" | |
18 | ||
1da177e4 | 19 | #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) |
c6f7e72a | 20 | #define to_bus(obj) container_of(obj, struct bus_type_private, subsys.kobj) |
1da177e4 LT |
21 | |
22 | /* | |
23 | * sysfs bindings for drivers | |
24 | */ | |
25 | ||
26 | #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) | |
27 | #define to_driver(obj) container_of(obj, struct device_driver, kobj) | |
28 | ||
29 | ||
b8c5cec2 KS |
30 | static int __must_check bus_rescan_devices_helper(struct device *dev, |
31 | void *data); | |
32 | ||
5901d014 GKH |
33 | static struct bus_type *bus_get(struct bus_type *bus) |
34 | { | |
c6f7e72a GKH |
35 | if (bus) { |
36 | kset_get(&bus->p->subsys); | |
37 | return bus; | |
38 | } | |
39 | return NULL; | |
5901d014 GKH |
40 | } |
41 | ||
fc1ede58 GKH |
42 | static void bus_put(struct bus_type *bus) |
43 | { | |
c6f7e72a GKH |
44 | if (bus) |
45 | kset_put(&bus->p->subsys); | |
fc1ede58 GKH |
46 | } |
47 | ||
1da177e4 LT |
48 | static ssize_t |
49 | drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |
50 | { | |
51 | struct driver_attribute * drv_attr = to_drv_attr(attr); | |
52 | struct device_driver * drv = to_driver(kobj); | |
4a0c20bf | 53 | ssize_t ret = -EIO; |
1da177e4 LT |
54 | |
55 | if (drv_attr->show) | |
56 | ret = drv_attr->show(drv, buf); | |
57 | return ret; | |
58 | } | |
59 | ||
60 | static ssize_t | |
61 | drv_attr_store(struct kobject * kobj, struct attribute * attr, | |
62 | const char * buf, size_t count) | |
63 | { | |
64 | struct driver_attribute * drv_attr = to_drv_attr(attr); | |
65 | struct device_driver * drv = to_driver(kobj); | |
4a0c20bf | 66 | ssize_t ret = -EIO; |
1da177e4 LT |
67 | |
68 | if (drv_attr->store) | |
69 | ret = drv_attr->store(drv, buf, count); | |
70 | return ret; | |
71 | } | |
72 | ||
73 | static struct sysfs_ops driver_sysfs_ops = { | |
74 | .show = drv_attr_show, | |
75 | .store = drv_attr_store, | |
76 | }; | |
77 | ||
78 | ||
79 | static void driver_release(struct kobject * kobj) | |
80 | { | |
74e9f5fa GKH |
81 | /* |
82 | * Yes this is an empty release function, it is this way because struct | |
83 | * device is always a static object, not a dynamic one. Yes, this is | |
84 | * not nice and bad, but remember, drivers are code, reference counted | |
85 | * by the module count, not a device, which is really data. And yes, | |
86 | * in the future I do want to have all drivers be created dynamically, | |
87 | * and am working toward that goal, but it will take a bit longer... | |
88 | * | |
89 | * But do not let this example give _anyone_ the idea that they can | |
90 | * create a release function without any code in it at all, to do that | |
91 | * is almost always wrong. If you have any questions about this, | |
92 | * please send an email to <greg@kroah.com> | |
93 | */ | |
1da177e4 LT |
94 | } |
95 | ||
a1148fb0 | 96 | static struct kobj_type driver_ktype = { |
1da177e4 LT |
97 | .sysfs_ops = &driver_sysfs_ops, |
98 | .release = driver_release, | |
99 | }; | |
100 | ||
101 | ||
102 | /* | |
103 | * sysfs bindings for buses | |
104 | */ | |
105 | ||
106 | ||
107 | static ssize_t | |
108 | bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |
109 | { | |
110 | struct bus_attribute * bus_attr = to_bus_attr(attr); | |
c6f7e72a | 111 | struct bus_type_private *bus_priv = to_bus(kobj); |
1da177e4 LT |
112 | ssize_t ret = 0; |
113 | ||
114 | if (bus_attr->show) | |
c6f7e72a | 115 | ret = bus_attr->show(bus_priv->bus, buf); |
1da177e4 LT |
116 | return ret; |
117 | } | |
118 | ||
119 | static ssize_t | |
120 | bus_attr_store(struct kobject * kobj, struct attribute * attr, | |
121 | const char * buf, size_t count) | |
122 | { | |
123 | struct bus_attribute * bus_attr = to_bus_attr(attr); | |
c6f7e72a | 124 | struct bus_type_private *bus_priv = to_bus(kobj); |
1da177e4 LT |
125 | ssize_t ret = 0; |
126 | ||
127 | if (bus_attr->store) | |
c6f7e72a | 128 | ret = bus_attr->store(bus_priv->bus, buf, count); |
1da177e4 LT |
129 | return ret; |
130 | } | |
131 | ||
132 | static struct sysfs_ops bus_sysfs_ops = { | |
133 | .show = bus_attr_show, | |
134 | .store = bus_attr_store, | |
135 | }; | |
136 | ||
137 | int bus_create_file(struct bus_type * bus, struct bus_attribute * attr) | |
138 | { | |
139 | int error; | |
5901d014 | 140 | if (bus_get(bus)) { |
c6f7e72a | 141 | error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr); |
fc1ede58 | 142 | bus_put(bus); |
1da177e4 LT |
143 | } else |
144 | error = -EINVAL; | |
145 | return error; | |
146 | } | |
147 | ||
148 | void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr) | |
149 | { | |
5901d014 | 150 | if (bus_get(bus)) { |
c6f7e72a | 151 | sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr); |
fc1ede58 | 152 | bus_put(bus); |
1da177e4 LT |
153 | } |
154 | } | |
155 | ||
80f03e34 | 156 | static struct kobj_type bus_ktype = { |
1da177e4 | 157 | .sysfs_ops = &bus_sysfs_ops, |
80f03e34 KS |
158 | }; |
159 | ||
160 | static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) | |
161 | { | |
162 | struct kobj_type *ktype = get_ktype(kobj); | |
163 | ||
164 | if (ktype == &bus_ktype) | |
165 | return 1; | |
166 | return 0; | |
167 | } | |
1da177e4 | 168 | |
80f03e34 KS |
169 | static struct kset_uevent_ops bus_uevent_ops = { |
170 | .filter = bus_uevent_filter, | |
1da177e4 LT |
171 | }; |
172 | ||
59a54833 | 173 | static struct kset *bus_kset; |
1da177e4 | 174 | |
1da177e4 | 175 | |
2139bdd5 | 176 | #ifdef CONFIG_HOTPLUG |
2b08c8d0 | 177 | /* Manually detach a device from its associated driver. */ |
151ef38f GKH |
178 | static int driver_helper(struct device *dev, void *data) |
179 | { | |
180 | const char *name = data; | |
181 | ||
182 | if (strcmp(name, dev->bus_id) == 0) | |
183 | return 1; | |
184 | return 0; | |
185 | } | |
186 | ||
187 | static ssize_t driver_unbind(struct device_driver *drv, | |
188 | const char *buf, size_t count) | |
189 | { | |
5901d014 | 190 | struct bus_type *bus = bus_get(drv->bus); |
151ef38f GKH |
191 | struct device *dev; |
192 | int err = -ENODEV; | |
193 | ||
194 | dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); | |
2b08c8d0 | 195 | if (dev && dev->driver == drv) { |
bf74ad5b AS |
196 | if (dev->parent) /* Needed for USB */ |
197 | down(&dev->parent->sem); | |
151ef38f | 198 | device_release_driver(dev); |
bf74ad5b AS |
199 | if (dev->parent) |
200 | up(&dev->parent->sem); | |
151ef38f GKH |
201 | err = count; |
202 | } | |
2b08c8d0 | 203 | put_device(dev); |
fc1ede58 | 204 | bus_put(bus); |
2b08c8d0 | 205 | return err; |
151ef38f GKH |
206 | } |
207 | static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind); | |
208 | ||
afdce75f GKH |
209 | /* |
210 | * Manually attach a device to a driver. | |
211 | * Note: the driver must want to bind to the device, | |
212 | * it is not possible to override the driver's id table. | |
213 | */ | |
214 | static ssize_t driver_bind(struct device_driver *drv, | |
215 | const char *buf, size_t count) | |
216 | { | |
5901d014 | 217 | struct bus_type *bus = bus_get(drv->bus); |
afdce75f GKH |
218 | struct device *dev; |
219 | int err = -ENODEV; | |
220 | ||
221 | dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); | |
2b08c8d0 | 222 | if (dev && dev->driver == NULL) { |
bf74ad5b AS |
223 | if (dev->parent) /* Needed for USB */ |
224 | down(&dev->parent->sem); | |
afdce75f GKH |
225 | down(&dev->sem); |
226 | err = driver_probe_device(drv, dev); | |
227 | up(&dev->sem); | |
bf74ad5b AS |
228 | if (dev->parent) |
229 | up(&dev->parent->sem); | |
37225401 RW |
230 | |
231 | if (err > 0) /* success */ | |
232 | err = count; | |
233 | else if (err == 0) /* driver didn't accept device */ | |
234 | err = -ENODEV; | |
afdce75f | 235 | } |
2b08c8d0 | 236 | put_device(dev); |
fc1ede58 | 237 | bus_put(bus); |
2b08c8d0 | 238 | return err; |
afdce75f GKH |
239 | } |
240 | static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind); | |
241 | ||
b8c5cec2 KS |
242 | static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf) |
243 | { | |
c6f7e72a | 244 | return sprintf(buf, "%d\n", bus->p->drivers_autoprobe); |
b8c5cec2 KS |
245 | } |
246 | ||
247 | static ssize_t store_drivers_autoprobe(struct bus_type *bus, | |
248 | const char *buf, size_t count) | |
249 | { | |
250 | if (buf[0] == '0') | |
c6f7e72a | 251 | bus->p->drivers_autoprobe = 0; |
b8c5cec2 | 252 | else |
c6f7e72a | 253 | bus->p->drivers_autoprobe = 1; |
b8c5cec2 KS |
254 | return count; |
255 | } | |
256 | ||
257 | static ssize_t store_drivers_probe(struct bus_type *bus, | |
258 | const char *buf, size_t count) | |
259 | { | |
260 | struct device *dev; | |
261 | ||
262 | dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); | |
263 | if (!dev) | |
264 | return -ENODEV; | |
265 | if (bus_rescan_devices_helper(dev, NULL) != 0) | |
266 | return -EINVAL; | |
267 | return count; | |
268 | } | |
2139bdd5 | 269 | #endif |
151ef38f | 270 | |
465c7a3a PM |
271 | static struct device * next_device(struct klist_iter * i) |
272 | { | |
273 | struct klist_node * n = klist_next(i); | |
274 | return n ? container_of(n, struct device, knode_bus) : NULL; | |
275 | } | |
276 | ||
1da177e4 LT |
277 | /** |
278 | * bus_for_each_dev - device iterator. | |
279 | * @bus: bus type. | |
280 | * @start: device to start iterating from. | |
281 | * @data: data for the callback. | |
282 | * @fn: function to be called for each device. | |
283 | * | |
284 | * Iterate over @bus's list of devices, and call @fn for each, | |
285 | * passing it @data. If @start is not NULL, we use that device to | |
286 | * begin iterating from. | |
287 | * | |
288 | * We check the return of @fn each time. If it returns anything | |
289 | * other than 0, we break out and return that value. | |
290 | * | |
291 | * NOTE: The device that returns a non-zero value is not retained | |
292 | * in any way, nor is its refcount incremented. If the caller needs | |
293 | * to retain this data, it should do, and increment the reference | |
294 | * count in the supplied callback. | |
295 | */ | |
296 | ||
297 | int bus_for_each_dev(struct bus_type * bus, struct device * start, | |
298 | void * data, int (*fn)(struct device *, void *)) | |
299 | { | |
465c7a3a PM |
300 | struct klist_iter i; |
301 | struct device * dev; | |
302 | int error = 0; | |
1da177e4 | 303 | |
465c7a3a PM |
304 | if (!bus) |
305 | return -EINVAL; | |
306 | ||
c6f7e72a | 307 | klist_iter_init_node(&bus->p->klist_devices, &i, |
465c7a3a PM |
308 | (start ? &start->knode_bus : NULL)); |
309 | while ((dev = next_device(&i)) && !error) | |
310 | error = fn(dev, data); | |
311 | klist_iter_exit(&i); | |
312 | return error; | |
1da177e4 LT |
313 | } |
314 | ||
0edb5860 CH |
315 | /** |
316 | * bus_find_device - device iterator for locating a particular device. | |
317 | * @bus: bus type | |
318 | * @start: Device to begin with | |
319 | * @data: Data to pass to match function | |
320 | * @match: Callback function to check device | |
321 | * | |
322 | * This is similar to the bus_for_each_dev() function above, but it | |
323 | * returns a reference to a device that is 'found' for later use, as | |
324 | * determined by the @match callback. | |
325 | * | |
326 | * The callback should return 0 if the device doesn't match and non-zero | |
327 | * if it does. If the callback returns non-zero, this function will | |
328 | * return to the caller and not iterate over any more devices. | |
329 | */ | |
330 | struct device * bus_find_device(struct bus_type *bus, | |
331 | struct device *start, void *data, | |
332 | int (*match)(struct device *, void *)) | |
333 | { | |
334 | struct klist_iter i; | |
335 | struct device *dev; | |
336 | ||
337 | if (!bus) | |
338 | return NULL; | |
339 | ||
c6f7e72a | 340 | klist_iter_init_node(&bus->p->klist_devices, &i, |
0edb5860 CH |
341 | (start ? &start->knode_bus : NULL)); |
342 | while ((dev = next_device(&i))) | |
343 | if (match(dev, data) && get_device(dev)) | |
344 | break; | |
345 | klist_iter_exit(&i); | |
346 | return dev; | |
347 | } | |
38fdac3c PM |
348 | |
349 | ||
350 | static struct device_driver * next_driver(struct klist_iter * i) | |
351 | { | |
352 | struct klist_node * n = klist_next(i); | |
353 | return n ? container_of(n, struct device_driver, knode_bus) : NULL; | |
354 | } | |
355 | ||
1da177e4 LT |
356 | /** |
357 | * bus_for_each_drv - driver iterator | |
358 | * @bus: bus we're dealing with. | |
359 | * @start: driver to start iterating on. | |
360 | * @data: data to pass to the callback. | |
361 | * @fn: function to call for each driver. | |
362 | * | |
363 | * This is nearly identical to the device iterator above. | |
364 | * We iterate over each driver that belongs to @bus, and call | |
365 | * @fn for each. If @fn returns anything but 0, we break out | |
366 | * and return it. If @start is not NULL, we use it as the head | |
367 | * of the list. | |
368 | * | |
369 | * NOTE: we don't return the driver that returns a non-zero | |
370 | * value, nor do we leave the reference count incremented for that | |
371 | * driver. If the caller needs to know that info, it must set it | |
372 | * in the callback. It must also be sure to increment the refcount | |
373 | * so it doesn't disappear before returning to the caller. | |
374 | */ | |
375 | ||
376 | int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, | |
377 | void * data, int (*fn)(struct device_driver *, void *)) | |
378 | { | |
38fdac3c PM |
379 | struct klist_iter i; |
380 | struct device_driver * drv; | |
381 | int error = 0; | |
1da177e4 | 382 | |
38fdac3c PM |
383 | if (!bus) |
384 | return -EINVAL; | |
385 | ||
c6f7e72a | 386 | klist_iter_init_node(&bus->p->klist_drivers, &i, |
38fdac3c PM |
387 | start ? &start->knode_bus : NULL); |
388 | while ((drv = next_driver(&i)) && !error) | |
389 | error = fn(drv, data); | |
390 | klist_iter_exit(&i); | |
391 | return error; | |
1da177e4 LT |
392 | } |
393 | ||
4aca67e5 | 394 | static int device_add_attrs(struct bus_type *bus, struct device *dev) |
1da177e4 LT |
395 | { |
396 | int error = 0; | |
397 | int i; | |
398 | ||
4aca67e5 AM |
399 | if (!bus->dev_attrs) |
400 | return 0; | |
401 | ||
402 | for (i = 0; attr_name(bus->dev_attrs[i]); i++) { | |
403 | error = device_create_file(dev,&bus->dev_attrs[i]); | |
404 | if (error) { | |
405 | while (--i >= 0) | |
406 | device_remove_file(dev, &bus->dev_attrs[i]); | |
407 | break; | |
1da177e4 LT |
408 | } |
409 | } | |
1da177e4 | 410 | return error; |
1da177e4 LT |
411 | } |
412 | ||
1da177e4 LT |
413 | static void device_remove_attrs(struct bus_type * bus, struct device * dev) |
414 | { | |
415 | int i; | |
416 | ||
417 | if (bus->dev_attrs) { | |
418 | for (i = 0; attr_name(bus->dev_attrs[i]); i++) | |
419 | device_remove_file(dev,&bus->dev_attrs[i]); | |
420 | } | |
421 | } | |
422 | ||
b9cafc7d KS |
423 | #ifdef CONFIG_SYSFS_DEPRECATED |
424 | static int make_deprecated_bus_links(struct device *dev) | |
425 | { | |
426 | return sysfs_create_link(&dev->kobj, | |
c6f7e72a | 427 | &dev->bus->p->subsys.kobj, "bus"); |
b9cafc7d KS |
428 | } |
429 | ||
430 | static void remove_deprecated_bus_links(struct device *dev) | |
431 | { | |
432 | sysfs_remove_link(&dev->kobj, "bus"); | |
433 | } | |
434 | #else | |
435 | static inline int make_deprecated_bus_links(struct device *dev) { return 0; } | |
436 | static inline void remove_deprecated_bus_links(struct device *dev) { } | |
437 | #endif | |
1da177e4 LT |
438 | |
439 | /** | |
440 | * bus_add_device - add device to bus | |
441 | * @dev: device being added | |
442 | * | |
443 | * - Add the device to its bus's list of devices. | |
53877d06 | 444 | * - Create link to device's bus. |
1da177e4 LT |
445 | */ |
446 | int bus_add_device(struct device * dev) | |
447 | { | |
5901d014 | 448 | struct bus_type * bus = bus_get(dev->bus); |
1da177e4 LT |
449 | int error = 0; |
450 | ||
451 | if (bus) { | |
1da177e4 | 452 | pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id); |
d377e85b | 453 | error = device_add_attrs(bus, dev); |
f86db396 | 454 | if (error) |
513e7337 | 455 | goto out_put; |
c6f7e72a | 456 | error = sysfs_create_link(&bus->p->devices_kset->kobj, |
f86db396 AM |
457 | &dev->kobj, dev->bus_id); |
458 | if (error) | |
513e7337 | 459 | goto out_id; |
f86db396 | 460 | error = sysfs_create_link(&dev->kobj, |
c6f7e72a | 461 | &dev->bus->p->subsys.kobj, "subsystem"); |
f86db396 | 462 | if (error) |
513e7337 | 463 | goto out_subsys; |
b9cafc7d | 464 | error = make_deprecated_bus_links(dev); |
513e7337 CH |
465 | if (error) |
466 | goto out_deprecated; | |
1da177e4 | 467 | } |
513e7337 CH |
468 | return 0; |
469 | ||
470 | out_deprecated: | |
471 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
472 | out_subsys: | |
c6f7e72a | 473 | sysfs_remove_link(&bus->p->devices_kset->kobj, dev->bus_id); |
513e7337 CH |
474 | out_id: |
475 | device_remove_attrs(bus, dev); | |
476 | out_put: | |
fc1ede58 | 477 | bus_put(dev->bus); |
1da177e4 LT |
478 | return error; |
479 | } | |
480 | ||
53877d06 KS |
481 | /** |
482 | * bus_attach_device - add device to bus | |
483 | * @dev: device tried to attach to a driver | |
484 | * | |
f2eaae19 | 485 | * - Add device to bus's list of devices. |
53877d06 KS |
486 | * - Try to attach to driver. |
487 | */ | |
c6a46696 | 488 | void bus_attach_device(struct device * dev) |
53877d06 | 489 | { |
f86db396 AM |
490 | struct bus_type *bus = dev->bus; |
491 | int ret = 0; | |
53877d06 KS |
492 | |
493 | if (bus) { | |
f2eaae19 | 494 | dev->is_registered = 1; |
c6f7e72a | 495 | if (bus->p->drivers_autoprobe) |
b8c5cec2 | 496 | ret = device_attach(dev); |
c6a46696 CH |
497 | WARN_ON(ret < 0); |
498 | if (ret >= 0) | |
c6f7e72a | 499 | klist_add_tail(&dev->knode_bus, &bus->p->klist_devices); |
c6a46696 | 500 | else |
f2eaae19 | 501 | dev->is_registered = 0; |
53877d06 KS |
502 | } |
503 | } | |
504 | ||
1da177e4 LT |
505 | /** |
506 | * bus_remove_device - remove device from bus | |
507 | * @dev: device to be removed | |
508 | * | |
509 | * - Remove symlink from bus's directory. | |
510 | * - Delete device from bus's list. | |
511 | * - Detach from its driver. | |
512 | * - Drop reference taken in bus_add_device(). | |
513 | */ | |
514 | void bus_remove_device(struct device * dev) | |
515 | { | |
516 | if (dev->bus) { | |
b9d9c82b | 517 | sysfs_remove_link(&dev->kobj, "subsystem"); |
b9cafc7d | 518 | remove_deprecated_bus_links(dev); |
c6f7e72a | 519 | sysfs_remove_link(&dev->bus->p->devices_kset->kobj, dev->bus_id); |
1da177e4 | 520 | device_remove_attrs(dev->bus, dev); |
f70fa629 AS |
521 | if (dev->is_registered) { |
522 | dev->is_registered = 0; | |
523 | klist_del(&dev->knode_bus); | |
524 | } | |
1da177e4 LT |
525 | pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id); |
526 | device_release_driver(dev); | |
fc1ede58 | 527 | bus_put(dev->bus); |
1da177e4 LT |
528 | } |
529 | } | |
530 | ||
531 | static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv) | |
532 | { | |
533 | int error = 0; | |
534 | int i; | |
535 | ||
536 | if (bus->drv_attrs) { | |
537 | for (i = 0; attr_name(bus->drv_attrs[i]); i++) { | |
538 | error = driver_create_file(drv, &bus->drv_attrs[i]); | |
539 | if (error) | |
540 | goto Err; | |
541 | } | |
542 | } | |
543 | Done: | |
544 | return error; | |
545 | Err: | |
546 | while (--i >= 0) | |
547 | driver_remove_file(drv, &bus->drv_attrs[i]); | |
548 | goto Done; | |
549 | } | |
550 | ||
551 | ||
552 | static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv) | |
553 | { | |
554 | int i; | |
555 | ||
556 | if (bus->drv_attrs) { | |
557 | for (i = 0; attr_name(bus->drv_attrs[i]); i++) | |
558 | driver_remove_file(drv, &bus->drv_attrs[i]); | |
559 | } | |
560 | } | |
561 | ||
874c6241 GKH |
562 | #ifdef CONFIG_HOTPLUG |
563 | /* | |
564 | * Thanks to drivers making their tables __devinit, we can't allow manual | |
565 | * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled. | |
566 | */ | |
f86db396 | 567 | static int __must_check add_bind_files(struct device_driver *drv) |
874c6241 | 568 | { |
f86db396 AM |
569 | int ret; |
570 | ||
571 | ret = driver_create_file(drv, &driver_attr_unbind); | |
572 | if (ret == 0) { | |
573 | ret = driver_create_file(drv, &driver_attr_bind); | |
574 | if (ret) | |
575 | driver_remove_file(drv, &driver_attr_unbind); | |
576 | } | |
577 | return ret; | |
874c6241 GKH |
578 | } |
579 | ||
580 | static void remove_bind_files(struct device_driver *drv) | |
581 | { | |
582 | driver_remove_file(drv, &driver_attr_bind); | |
583 | driver_remove_file(drv, &driver_attr_unbind); | |
584 | } | |
b8c5cec2 | 585 | |
8380770c KS |
586 | static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe); |
587 | static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO, | |
588 | show_drivers_autoprobe, store_drivers_autoprobe); | |
589 | ||
b8c5cec2 KS |
590 | static int add_probe_files(struct bus_type *bus) |
591 | { | |
592 | int retval; | |
593 | ||
8380770c | 594 | retval = bus_create_file(bus, &bus_attr_drivers_probe); |
b8c5cec2 KS |
595 | if (retval) |
596 | goto out; | |
597 | ||
8380770c | 598 | retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); |
b8c5cec2 | 599 | if (retval) |
8380770c | 600 | bus_remove_file(bus, &bus_attr_drivers_probe); |
b8c5cec2 KS |
601 | out: |
602 | return retval; | |
603 | } | |
604 | ||
605 | static void remove_probe_files(struct bus_type *bus) | |
606 | { | |
8380770c KS |
607 | bus_remove_file(bus, &bus_attr_drivers_autoprobe); |
608 | bus_remove_file(bus, &bus_attr_drivers_probe); | |
b8c5cec2 | 609 | } |
874c6241 | 610 | #else |
35acfdd7 | 611 | static inline int add_bind_files(struct device_driver *drv) { return 0; } |
874c6241 | 612 | static inline void remove_bind_files(struct device_driver *drv) {} |
b8c5cec2 KS |
613 | static inline int add_probe_files(struct bus_type *bus) { return 0; } |
614 | static inline void remove_probe_files(struct bus_type *bus) {} | |
874c6241 | 615 | #endif |
1da177e4 | 616 | |
7ac1cf4a KS |
617 | static ssize_t driver_uevent_store(struct device_driver *drv, |
618 | const char *buf, size_t count) | |
619 | { | |
620 | enum kobject_action action; | |
621 | ||
622 | if (kobject_action_type(buf, count, &action) == 0) | |
623 | kobject_uevent(&drv->kobj, action); | |
624 | return count; | |
625 | } | |
626 | static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store); | |
627 | ||
1da177e4 LT |
628 | /** |
629 | * bus_add_driver - Add a driver to the bus. | |
630 | * @drv: driver. | |
631 | * | |
632 | */ | |
f86db396 | 633 | int bus_add_driver(struct device_driver *drv) |
1da177e4 | 634 | { |
5901d014 | 635 | struct bus_type * bus = bus_get(drv->bus); |
1da177e4 LT |
636 | int error = 0; |
637 | ||
d9fd4d3b | 638 | if (!bus) |
4f6e1945 | 639 | return -EINVAL; |
d9fd4d3b JG |
640 | |
641 | pr_debug("bus %s: add driver %s\n", bus->name, drv->name); | |
642 | error = kobject_set_name(&drv->kobj, "%s", drv->name); | |
643 | if (error) | |
644 | goto out_put_bus; | |
c6f7e72a | 645 | drv->kobj.kset = bus->p->drivers_kset; |
3514faca | 646 | drv->kobj.ktype = &driver_ktype; |
dc0afa83 CH |
647 | error = kobject_register(&drv->kobj); |
648 | if (error) | |
d9fd4d3b JG |
649 | goto out_put_bus; |
650 | ||
c6f7e72a | 651 | if (drv->bus->p->drivers_autoprobe) { |
b8c5cec2 KS |
652 | error = driver_attach(drv); |
653 | if (error) | |
654 | goto out_unregister; | |
655 | } | |
c6f7e72a | 656 | klist_add_tail(&drv->knode_bus, &bus->p->klist_drivers); |
d9fd4d3b JG |
657 | module_add_driver(drv->owner, drv); |
658 | ||
7ac1cf4a KS |
659 | error = driver_create_file(drv, &driver_attr_uevent); |
660 | if (error) { | |
661 | printk(KERN_ERR "%s: uevent attr (%s) failed\n", | |
662 | __FUNCTION__, drv->name); | |
663 | } | |
d9fd4d3b JG |
664 | error = driver_add_attrs(bus, drv); |
665 | if (error) { | |
666 | /* How the hell do we get out of this pickle? Give up */ | |
667 | printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n", | |
668 | __FUNCTION__, drv->name); | |
669 | } | |
670 | error = add_bind_files(drv); | |
671 | if (error) { | |
672 | /* Ditto */ | |
673 | printk(KERN_ERR "%s: add_bind_files(%s) failed\n", | |
674 | __FUNCTION__, drv->name); | |
1da177e4 | 675 | } |
d9fd4d3b | 676 | |
1da177e4 | 677 | return error; |
f86db396 AM |
678 | out_unregister: |
679 | kobject_unregister(&drv->kobj); | |
680 | out_put_bus: | |
fc1ede58 | 681 | bus_put(bus); |
f86db396 | 682 | return error; |
1da177e4 LT |
683 | } |
684 | ||
1da177e4 LT |
685 | /** |
686 | * bus_remove_driver - delete driver from bus's knowledge. | |
687 | * @drv: driver. | |
688 | * | |
689 | * Detach the driver from the devices it controls, and remove | |
690 | * it from its bus's list of drivers. Finally, we drop the reference | |
691 | * to the bus we took in bus_add_driver(). | |
692 | */ | |
693 | ||
694 | void bus_remove_driver(struct device_driver * drv) | |
695 | { | |
d9fd4d3b JG |
696 | if (!drv->bus) |
697 | return; | |
698 | ||
699 | remove_bind_files(drv); | |
700 | driver_remove_attrs(drv->bus, drv); | |
7ac1cf4a | 701 | driver_remove_file(drv, &driver_attr_uevent); |
d9fd4d3b JG |
702 | klist_remove(&drv->knode_bus); |
703 | pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name); | |
704 | driver_detach(drv); | |
705 | module_remove_driver(drv); | |
706 | kobject_unregister(&drv->kobj); | |
fc1ede58 | 707 | bus_put(drv->bus); |
1da177e4 LT |
708 | } |
709 | ||
710 | ||
711 | /* Helper for bus_rescan_devices's iter */ | |
f86db396 AM |
712 | static int __must_check bus_rescan_devices_helper(struct device *dev, |
713 | void *data) | |
1da177e4 | 714 | { |
f86db396 AM |
715 | int ret = 0; |
716 | ||
bf74ad5b AS |
717 | if (!dev->driver) { |
718 | if (dev->parent) /* Needed for USB */ | |
719 | down(&dev->parent->sem); | |
f86db396 | 720 | ret = device_attach(dev); |
bf74ad5b AS |
721 | if (dev->parent) |
722 | up(&dev->parent->sem); | |
723 | } | |
f86db396 | 724 | return ret < 0 ? ret : 0; |
1da177e4 LT |
725 | } |
726 | ||
1da177e4 | 727 | /** |
23d3d602 GKH |
728 | * bus_rescan_devices - rescan devices on the bus for possible drivers |
729 | * @bus: the bus to scan. | |
1da177e4 | 730 | * |
23d3d602 GKH |
731 | * This function will look for devices on the bus with no driver |
732 | * attached and rescan it against existing drivers to see if it matches | |
733 | * any by calling device_attach() for the unbound devices. | |
1da177e4 | 734 | */ |
f86db396 | 735 | int bus_rescan_devices(struct bus_type * bus) |
1da177e4 | 736 | { |
f86db396 | 737 | return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); |
1da177e4 LT |
738 | } |
739 | ||
e935d5da ME |
740 | /** |
741 | * device_reprobe - remove driver for a device and probe for a new driver | |
742 | * @dev: the device to reprobe | |
743 | * | |
744 | * This function detaches the attached driver (if any) for the given | |
745 | * device and restarts the driver probing process. It is intended | |
746 | * to use if probing criteria changed during a devices lifetime and | |
747 | * driver attachment should change accordingly. | |
748 | */ | |
f86db396 | 749 | int device_reprobe(struct device *dev) |
e935d5da ME |
750 | { |
751 | if (dev->driver) { | |
752 | if (dev->parent) /* Needed for USB */ | |
753 | down(&dev->parent->sem); | |
754 | device_release_driver(dev); | |
755 | if (dev->parent) | |
756 | up(&dev->parent->sem); | |
757 | } | |
f86db396 | 758 | return bus_rescan_devices_helper(dev, NULL); |
e935d5da ME |
759 | } |
760 | EXPORT_SYMBOL_GPL(device_reprobe); | |
1da177e4 | 761 | |
1da177e4 LT |
762 | /** |
763 | * find_bus - locate bus by name. | |
764 | * @name: name of bus. | |
765 | * | |
766 | * Call kset_find_obj() to iterate over list of buses to | |
767 | * find a bus by name. Return bus if found. | |
768 | * | |
769 | * Note that kset_find_obj increments bus' reference count. | |
770 | */ | |
7e4ef085 | 771 | #if 0 |
1da177e4 LT |
772 | struct bus_type * find_bus(char * name) |
773 | { | |
59a54833 | 774 | struct kobject * k = kset_find_obj(bus_kset, name); |
1da177e4 LT |
775 | return k ? to_bus(k) : NULL; |
776 | } | |
7e4ef085 | 777 | #endif /* 0 */ |
1da177e4 LT |
778 | |
779 | ||
780 | /** | |
781 | * bus_add_attrs - Add default attributes for this bus. | |
782 | * @bus: Bus that has just been registered. | |
783 | */ | |
784 | ||
785 | static int bus_add_attrs(struct bus_type * bus) | |
786 | { | |
787 | int error = 0; | |
788 | int i; | |
789 | ||
790 | if (bus->bus_attrs) { | |
791 | for (i = 0; attr_name(bus->bus_attrs[i]); i++) { | |
dc0afa83 CH |
792 | error = bus_create_file(bus,&bus->bus_attrs[i]); |
793 | if (error) | |
1da177e4 LT |
794 | goto Err; |
795 | } | |
796 | } | |
797 | Done: | |
798 | return error; | |
799 | Err: | |
800 | while (--i >= 0) | |
801 | bus_remove_file(bus,&bus->bus_attrs[i]); | |
802 | goto Done; | |
803 | } | |
804 | ||
805 | static void bus_remove_attrs(struct bus_type * bus) | |
806 | { | |
807 | int i; | |
808 | ||
809 | if (bus->bus_attrs) { | |
810 | for (i = 0; attr_name(bus->bus_attrs[i]); i++) | |
811 | bus_remove_file(bus,&bus->bus_attrs[i]); | |
812 | } | |
813 | } | |
814 | ||
34bb61f9 JB |
815 | static void klist_devices_get(struct klist_node *n) |
816 | { | |
817 | struct device *dev = container_of(n, struct device, knode_bus); | |
818 | ||
819 | get_device(dev); | |
820 | } | |
821 | ||
822 | static void klist_devices_put(struct klist_node *n) | |
823 | { | |
824 | struct device *dev = container_of(n, struct device, knode_bus); | |
825 | ||
826 | put_device(dev); | |
827 | } | |
828 | ||
7ac1cf4a KS |
829 | static ssize_t bus_uevent_store(struct bus_type *bus, |
830 | const char *buf, size_t count) | |
831 | { | |
832 | enum kobject_action action; | |
833 | ||
834 | if (kobject_action_type(buf, count, &action) == 0) | |
c6f7e72a | 835 | kobject_uevent(&bus->p->subsys.kobj, action); |
7ac1cf4a KS |
836 | return count; |
837 | } | |
838 | static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store); | |
839 | ||
1da177e4 LT |
840 | /** |
841 | * bus_register - register a bus with the system. | |
842 | * @bus: bus. | |
843 | * | |
844 | * Once we have that, we registered the bus with the kobject | |
845 | * infrastructure, then register the children subsystems it has: | |
846 | * the devices and drivers that belong to the bus. | |
847 | */ | |
848 | int bus_register(struct bus_type * bus) | |
849 | { | |
850 | int retval; | |
c6f7e72a GKH |
851 | struct bus_type_private *priv; |
852 | ||
853 | priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL); | |
854 | if (!priv) | |
855 | return -ENOMEM; | |
856 | ||
857 | priv->bus = bus; | |
858 | bus->p = priv; | |
1da177e4 | 859 | |
c6f7e72a | 860 | BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); |
116af378 | 861 | |
c6f7e72a | 862 | retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name); |
1da177e4 LT |
863 | if (retval) |
864 | goto out; | |
865 | ||
c6f7e72a GKH |
866 | priv->subsys.kobj.kset = bus_kset; |
867 | priv->subsys.kobj.ktype = &bus_ktype; | |
868 | priv->drivers_autoprobe = 1; | |
d6b05b84 | 869 | |
c6f7e72a | 870 | retval = kset_register(&priv->subsys); |
1da177e4 LT |
871 | if (retval) |
872 | goto out; | |
873 | ||
7ac1cf4a KS |
874 | retval = bus_create_file(bus, &bus_attr_uevent); |
875 | if (retval) | |
876 | goto bus_uevent_fail; | |
877 | ||
c6f7e72a GKH |
878 | priv->devices_kset = kset_create_and_add("devices", NULL, |
879 | &priv->subsys.kobj); | |
880 | if (!priv->devices_kset) { | |
3d899596 | 881 | retval = -ENOMEM; |
1da177e4 | 882 | goto bus_devices_fail; |
3d899596 | 883 | } |
1da177e4 | 884 | |
c6f7e72a GKH |
885 | priv->drivers_kset = kset_create_and_add("drivers", NULL, |
886 | &priv->subsys.kobj); | |
887 | if (!priv->drivers_kset) { | |
6dcec251 | 888 | retval = -ENOMEM; |
1da177e4 | 889 | goto bus_drivers_fail; |
6dcec251 | 890 | } |
465c7a3a | 891 | |
c6f7e72a GKH |
892 | klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); |
893 | klist_init(&priv->klist_drivers, NULL, NULL); | |
b8c5cec2 | 894 | |
b8c5cec2 KS |
895 | retval = add_probe_files(bus); |
896 | if (retval) | |
897 | goto bus_probe_files_fail; | |
898 | ||
1bb6881a CH |
899 | retval = bus_add_attrs(bus); |
900 | if (retval) | |
901 | goto bus_attrs_fail; | |
1da177e4 LT |
902 | |
903 | pr_debug("bus type '%s' registered\n", bus->name); | |
904 | return 0; | |
905 | ||
1bb6881a | 906 | bus_attrs_fail: |
b8c5cec2 KS |
907 | remove_probe_files(bus); |
908 | bus_probe_files_fail: | |
c6f7e72a | 909 | kset_unregister(bus->p->drivers_kset); |
1da177e4 | 910 | bus_drivers_fail: |
c6f7e72a | 911 | kset_unregister(bus->p->devices_kset); |
1da177e4 | 912 | bus_devices_fail: |
7ac1cf4a KS |
913 | bus_remove_file(bus, &bus_attr_uevent); |
914 | bus_uevent_fail: | |
c6f7e72a GKH |
915 | kset_unregister(&bus->p->subsys); |
916 | kfree(bus->p); | |
1da177e4 LT |
917 | out: |
918 | return retval; | |
919 | } | |
920 | ||
1da177e4 LT |
921 | /** |
922 | * bus_unregister - remove a bus from the system | |
923 | * @bus: bus. | |
924 | * | |
925 | * Unregister the child subsystems and the bus itself. | |
fc1ede58 | 926 | * Finally, we call bus_put() to release the refcount |
1da177e4 LT |
927 | */ |
928 | void bus_unregister(struct bus_type * bus) | |
929 | { | |
930 | pr_debug("bus %s: unregistering\n", bus->name); | |
931 | bus_remove_attrs(bus); | |
b8c5cec2 | 932 | remove_probe_files(bus); |
c6f7e72a GKH |
933 | kset_unregister(bus->p->drivers_kset); |
934 | kset_unregister(bus->p->devices_kset); | |
7ac1cf4a | 935 | bus_remove_file(bus, &bus_attr_uevent); |
c6f7e72a GKH |
936 | kset_unregister(&bus->p->subsys); |
937 | kfree(bus->p); | |
1da177e4 LT |
938 | } |
939 | ||
116af378 BH |
940 | int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) |
941 | { | |
c6f7e72a | 942 | return blocking_notifier_chain_register(&bus->p->bus_notifier, nb); |
116af378 BH |
943 | } |
944 | EXPORT_SYMBOL_GPL(bus_register_notifier); | |
945 | ||
946 | int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) | |
947 | { | |
c6f7e72a | 948 | return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb); |
116af378 BH |
949 | } |
950 | EXPORT_SYMBOL_GPL(bus_unregister_notifier); | |
951 | ||
0fed80f7 GKH |
952 | struct kset *bus_get_kset(struct bus_type *bus) |
953 | { | |
c6f7e72a | 954 | return &bus->p->subsys; |
0fed80f7 GKH |
955 | } |
956 | EXPORT_SYMBOL_GPL(bus_get_kset); | |
957 | ||
b249072e GKH |
958 | struct klist *bus_get_device_klist(struct bus_type *bus) |
959 | { | |
c6f7e72a | 960 | return &bus->p->klist_devices; |
b249072e GKH |
961 | } |
962 | EXPORT_SYMBOL_GPL(bus_get_device_klist); | |
963 | ||
1da177e4 LT |
964 | int __init buses_init(void) |
965 | { | |
59a54833 GKH |
966 | bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); |
967 | if (!bus_kset) | |
968 | return -ENOMEM; | |
969 | return 0; | |
1da177e4 LT |
970 | } |
971 | ||
972 | ||
973 | EXPORT_SYMBOL_GPL(bus_for_each_dev); | |
0edb5860 | 974 | EXPORT_SYMBOL_GPL(bus_find_device); |
1da177e4 LT |
975 | EXPORT_SYMBOL_GPL(bus_for_each_drv); |
976 | ||
1da177e4 LT |
977 | EXPORT_SYMBOL_GPL(bus_register); |
978 | EXPORT_SYMBOL_GPL(bus_unregister); | |
979 | EXPORT_SYMBOL_GPL(bus_rescan_devices); | |
1da177e4 LT |
980 | |
981 | EXPORT_SYMBOL_GPL(bus_create_file); | |
982 | EXPORT_SYMBOL_GPL(bus_remove_file); |