Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/base/bus.c
26378 views
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
* bus.c - bus driver management
4
*
5
* Copyright (c) 2002-3 Patrick Mochel
6
* Copyright (c) 2002-3 Open Source Development Labs
7
* Copyright (c) 2007 Greg Kroah-Hartman <[email protected]>
8
* Copyright (c) 2007 Novell Inc.
9
* Copyright (c) 2023 Greg Kroah-Hartman <[email protected]>
10
*/
11
12
#include <linux/async.h>
13
#include <linux/device/bus.h>
14
#include <linux/device.h>
15
#include <linux/module.h>
16
#include <linux/errno.h>
17
#include <linux/slab.h>
18
#include <linux/init.h>
19
#include <linux/string.h>
20
#include <linux/mutex.h>
21
#include <linux/sysfs.h>
22
#include "base.h"
23
#include "power/power.h"
24
25
/* /sys/devices/system */
26
static struct kset *system_kset;
27
28
/* /sys/bus */
29
static struct kset *bus_kset;
30
31
#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
32
33
/*
34
* sysfs bindings for drivers
35
*/
36
37
#define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
38
39
#define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
40
struct driver_attribute driver_attr_##_name = \
41
__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
42
43
static int __must_check bus_rescan_devices_helper(struct device *dev,
44
void *data);
45
46
/**
47
* bus_to_subsys - Turn a struct bus_type into a struct subsys_private
48
*
49
* @bus: pointer to the struct bus_type to look up
50
*
51
* The driver core internals needs to work on the subsys_private structure, not
52
* the external struct bus_type pointer. This function walks the list of
53
* registered busses in the system and finds the matching one and returns the
54
* internal struct subsys_private that relates to that bus.
55
*
56
* Note, the reference count of the return value is INCREMENTED if it is not
57
* NULL. A call to subsys_put() must be done when finished with the pointer in
58
* order for it to be properly freed.
59
*/
60
struct subsys_private *bus_to_subsys(const struct bus_type *bus)
61
{
62
struct subsys_private *sp = NULL;
63
struct kobject *kobj;
64
65
if (!bus || !bus_kset)
66
return NULL;
67
68
spin_lock(&bus_kset->list_lock);
69
70
if (list_empty(&bus_kset->list))
71
goto done;
72
73
list_for_each_entry(kobj, &bus_kset->list, entry) {
74
struct kset *kset = container_of(kobj, struct kset, kobj);
75
76
sp = container_of_const(kset, struct subsys_private, subsys);
77
if (sp->bus == bus)
78
goto done;
79
}
80
sp = NULL;
81
done:
82
sp = subsys_get(sp);
83
spin_unlock(&bus_kset->list_lock);
84
return sp;
85
}
86
87
static const struct bus_type *bus_get(const struct bus_type *bus)
88
{
89
struct subsys_private *sp = bus_to_subsys(bus);
90
91
if (sp)
92
return bus;
93
return NULL;
94
}
95
96
static void bus_put(const struct bus_type *bus)
97
{
98
struct subsys_private *sp = bus_to_subsys(bus);
99
100
/* two puts are required as the call to bus_to_subsys incremented it again */
101
subsys_put(sp);
102
subsys_put(sp);
103
}
104
105
static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
106
char *buf)
107
{
108
struct driver_attribute *drv_attr = to_drv_attr(attr);
109
struct driver_private *drv_priv = to_driver(kobj);
110
ssize_t ret = -EIO;
111
112
if (drv_attr->show)
113
ret = drv_attr->show(drv_priv->driver, buf);
114
return ret;
115
}
116
117
static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
118
const char *buf, size_t count)
119
{
120
struct driver_attribute *drv_attr = to_drv_attr(attr);
121
struct driver_private *drv_priv = to_driver(kobj);
122
ssize_t ret = -EIO;
123
124
if (drv_attr->store)
125
ret = drv_attr->store(drv_priv->driver, buf, count);
126
return ret;
127
}
128
129
static const struct sysfs_ops driver_sysfs_ops = {
130
.show = drv_attr_show,
131
.store = drv_attr_store,
132
};
133
134
static void driver_release(struct kobject *kobj)
135
{
136
struct driver_private *drv_priv = to_driver(kobj);
137
138
pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
139
kfree(drv_priv);
140
}
141
142
static const struct kobj_type driver_ktype = {
143
.sysfs_ops = &driver_sysfs_ops,
144
.release = driver_release,
145
};
146
147
/*
148
* sysfs bindings for buses
149
*/
150
static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
151
char *buf)
152
{
153
struct bus_attribute *bus_attr = to_bus_attr(attr);
154
struct subsys_private *subsys_priv = to_subsys_private(kobj);
155
/* return -EIO for reading a bus attribute without show() */
156
ssize_t ret = -EIO;
157
158
if (bus_attr->show)
159
ret = bus_attr->show(subsys_priv->bus, buf);
160
return ret;
161
}
162
163
static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
164
const char *buf, size_t count)
165
{
166
struct bus_attribute *bus_attr = to_bus_attr(attr);
167
struct subsys_private *subsys_priv = to_subsys_private(kobj);
168
/* return -EIO for writing a bus attribute without store() */
169
ssize_t ret = -EIO;
170
171
if (bus_attr->store)
172
ret = bus_attr->store(subsys_priv->bus, buf, count);
173
return ret;
174
}
175
176
static const struct sysfs_ops bus_sysfs_ops = {
177
.show = bus_attr_show,
178
.store = bus_attr_store,
179
};
180
181
int bus_create_file(const struct bus_type *bus, struct bus_attribute *attr)
182
{
183
struct subsys_private *sp = bus_to_subsys(bus);
184
int error;
185
186
if (!sp)
187
return -EINVAL;
188
189
error = sysfs_create_file(&sp->subsys.kobj, &attr->attr);
190
191
subsys_put(sp);
192
return error;
193
}
194
EXPORT_SYMBOL_GPL(bus_create_file);
195
196
void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr)
197
{
198
struct subsys_private *sp = bus_to_subsys(bus);
199
200
if (!sp)
201
return;
202
203
sysfs_remove_file(&sp->subsys.kobj, &attr->attr);
204
subsys_put(sp);
205
}
206
EXPORT_SYMBOL_GPL(bus_remove_file);
207
208
static void bus_release(struct kobject *kobj)
209
{
210
struct subsys_private *priv = to_subsys_private(kobj);
211
212
lockdep_unregister_key(&priv->lock_key);
213
kfree(priv);
214
}
215
216
static const struct kobj_type bus_ktype = {
217
.sysfs_ops = &bus_sysfs_ops,
218
.release = bus_release,
219
};
220
221
static int bus_uevent_filter(const struct kobject *kobj)
222
{
223
const struct kobj_type *ktype = get_ktype(kobj);
224
225
if (ktype == &bus_ktype)
226
return 1;
227
return 0;
228
}
229
230
static const struct kset_uevent_ops bus_uevent_ops = {
231
.filter = bus_uevent_filter,
232
};
233
234
/* Manually detach a device from its associated driver. */
235
static ssize_t unbind_store(struct device_driver *drv, const char *buf,
236
size_t count)
237
{
238
const struct bus_type *bus = bus_get(drv->bus);
239
struct device *dev;
240
int err = -ENODEV;
241
242
dev = bus_find_device_by_name(bus, NULL, buf);
243
if (dev && dev->driver == drv) {
244
device_driver_detach(dev);
245
err = count;
246
}
247
put_device(dev);
248
bus_put(bus);
249
return err;
250
}
251
static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store);
252
253
/*
254
* Manually attach a device to a driver.
255
* Note: the driver must want to bind to the device,
256
* it is not possible to override the driver's id table.
257
*/
258
static ssize_t bind_store(struct device_driver *drv, const char *buf,
259
size_t count)
260
{
261
const struct bus_type *bus = bus_get(drv->bus);
262
struct device *dev;
263
int err = -ENODEV;
264
265
dev = bus_find_device_by_name(bus, NULL, buf);
266
if (dev && driver_match_device(drv, dev)) {
267
err = device_driver_attach(drv, dev);
268
if (!err) {
269
/* success */
270
err = count;
271
}
272
}
273
put_device(dev);
274
bus_put(bus);
275
return err;
276
}
277
static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store);
278
279
static ssize_t drivers_autoprobe_show(const struct bus_type *bus, char *buf)
280
{
281
struct subsys_private *sp = bus_to_subsys(bus);
282
int ret;
283
284
if (!sp)
285
return -EINVAL;
286
287
ret = sysfs_emit(buf, "%d\n", sp->drivers_autoprobe);
288
subsys_put(sp);
289
return ret;
290
}
291
292
static ssize_t drivers_autoprobe_store(const struct bus_type *bus,
293
const char *buf, size_t count)
294
{
295
struct subsys_private *sp = bus_to_subsys(bus);
296
297
if (!sp)
298
return -EINVAL;
299
300
if (buf[0] == '0')
301
sp->drivers_autoprobe = 0;
302
else
303
sp->drivers_autoprobe = 1;
304
305
subsys_put(sp);
306
return count;
307
}
308
309
static ssize_t drivers_probe_store(const struct bus_type *bus,
310
const char *buf, size_t count)
311
{
312
struct device *dev;
313
int err = -EINVAL;
314
315
dev = bus_find_device_by_name(bus, NULL, buf);
316
if (!dev)
317
return -ENODEV;
318
if (bus_rescan_devices_helper(dev, NULL) == 0)
319
err = count;
320
put_device(dev);
321
return err;
322
}
323
324
static struct device *next_device(struct klist_iter *i)
325
{
326
struct klist_node *n = klist_next(i);
327
struct device *dev = NULL;
328
struct device_private *dev_prv;
329
330
if (n) {
331
dev_prv = to_device_private_bus(n);
332
dev = dev_prv->device;
333
}
334
return dev;
335
}
336
337
/**
338
* bus_for_each_dev - device iterator.
339
* @bus: bus type.
340
* @start: device to start iterating from.
341
* @data: data for the callback.
342
* @fn: function to be called for each device.
343
*
344
* Iterate over @bus's list of devices, and call @fn for each,
345
* passing it @data. If @start is not NULL, we use that device to
346
* begin iterating from.
347
*
348
* We check the return of @fn each time. If it returns anything
349
* other than 0, we break out and return that value.
350
*
351
* NOTE: The device that returns a non-zero value is not retained
352
* in any way, nor is its refcount incremented. If the caller needs
353
* to retain this data, it should do so, and increment the reference
354
* count in the supplied callback.
355
*/
356
int bus_for_each_dev(const struct bus_type *bus, struct device *start,
357
void *data, device_iter_t fn)
358
{
359
struct subsys_private *sp = bus_to_subsys(bus);
360
struct klist_iter i;
361
struct device *dev;
362
int error = 0;
363
364
if (!sp)
365
return -EINVAL;
366
367
klist_iter_init_node(&sp->klist_devices, &i,
368
(start ? &start->p->knode_bus : NULL));
369
while (!error && (dev = next_device(&i)))
370
error = fn(dev, data);
371
klist_iter_exit(&i);
372
subsys_put(sp);
373
return error;
374
}
375
EXPORT_SYMBOL_GPL(bus_for_each_dev);
376
377
/**
378
* bus_find_device - device iterator for locating a particular device.
379
* @bus: bus type
380
* @start: Device to begin with
381
* @data: Data to pass to match function
382
* @match: Callback function to check device
383
*
384
* This is similar to the bus_for_each_dev() function above, but it
385
* returns a reference to a device that is 'found' for later use, as
386
* determined by the @match callback.
387
*
388
* The callback should return 0 if the device doesn't match and non-zero
389
* if it does. If the callback returns non-zero, this function will
390
* return to the caller and not iterate over any more devices.
391
*/
392
struct device *bus_find_device(const struct bus_type *bus,
393
struct device *start, const void *data,
394
device_match_t match)
395
{
396
struct subsys_private *sp = bus_to_subsys(bus);
397
struct klist_iter i;
398
struct device *dev;
399
400
if (!sp)
401
return NULL;
402
403
klist_iter_init_node(&sp->klist_devices, &i,
404
(start ? &start->p->knode_bus : NULL));
405
while ((dev = next_device(&i))) {
406
if (match(dev, data)) {
407
get_device(dev);
408
break;
409
}
410
}
411
klist_iter_exit(&i);
412
subsys_put(sp);
413
return dev;
414
}
415
EXPORT_SYMBOL_GPL(bus_find_device);
416
417
static struct device_driver *next_driver(struct klist_iter *i)
418
{
419
struct klist_node *n = klist_next(i);
420
struct driver_private *drv_priv;
421
422
if (n) {
423
drv_priv = container_of(n, struct driver_private, knode_bus);
424
return drv_priv->driver;
425
}
426
return NULL;
427
}
428
429
/**
430
* bus_for_each_drv - driver iterator
431
* @bus: bus we're dealing with.
432
* @start: driver to start iterating on.
433
* @data: data to pass to the callback.
434
* @fn: function to call for each driver.
435
*
436
* This is nearly identical to the device iterator above.
437
* We iterate over each driver that belongs to @bus, and call
438
* @fn for each. If @fn returns anything but 0, we break out
439
* and return it. If @start is not NULL, we use it as the head
440
* of the list.
441
*
442
* NOTE: we don't return the driver that returns a non-zero
443
* value, nor do we leave the reference count incremented for that
444
* driver. If the caller needs to know that info, it must set it
445
* in the callback. It must also be sure to increment the refcount
446
* so it doesn't disappear before returning to the caller.
447
*/
448
int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start,
449
void *data, int (*fn)(struct device_driver *, void *))
450
{
451
struct subsys_private *sp = bus_to_subsys(bus);
452
struct klist_iter i;
453
struct device_driver *drv;
454
int error = 0;
455
456
if (!sp)
457
return -EINVAL;
458
459
klist_iter_init_node(&sp->klist_drivers, &i,
460
start ? &start->p->knode_bus : NULL);
461
while ((drv = next_driver(&i)) && !error)
462
error = fn(drv, data);
463
klist_iter_exit(&i);
464
subsys_put(sp);
465
return error;
466
}
467
EXPORT_SYMBOL_GPL(bus_for_each_drv);
468
469
/**
470
* bus_add_device - add device to bus
471
* @dev: device being added
472
*
473
* - Add device's bus attributes.
474
* - Create links to device's bus.
475
* - Add the device to its bus's list of devices.
476
*/
477
int bus_add_device(struct device *dev)
478
{
479
struct subsys_private *sp = bus_to_subsys(dev->bus);
480
int error;
481
482
if (!sp) {
483
/*
484
* This is a normal operation for many devices that do not
485
* have a bus assigned to them, just say that all went
486
* well.
487
*/
488
return 0;
489
}
490
491
/*
492
* Reference in sp is now incremented and will be dropped when
493
* the device is removed from the bus
494
*/
495
496
pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev));
497
498
error = device_add_groups(dev, sp->bus->dev_groups);
499
if (error)
500
goto out_put;
501
502
error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev));
503
if (error)
504
goto out_groups;
505
506
error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
507
if (error)
508
goto out_subsys;
509
510
klist_add_tail(&dev->p->knode_bus, &sp->klist_devices);
511
return 0;
512
513
out_subsys:
514
sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
515
out_groups:
516
device_remove_groups(dev, sp->bus->dev_groups);
517
out_put:
518
subsys_put(sp);
519
return error;
520
}
521
522
/**
523
* bus_probe_device - probe drivers for a new device
524
* @dev: device to probe
525
*
526
* - Automatically probe for a driver if the bus allows it.
527
*/
528
void bus_probe_device(struct device *dev)
529
{
530
struct subsys_private *sp = bus_to_subsys(dev->bus);
531
struct subsys_interface *sif;
532
533
if (!sp)
534
return;
535
536
if (sp->drivers_autoprobe)
537
device_initial_probe(dev);
538
539
mutex_lock(&sp->mutex);
540
list_for_each_entry(sif, &sp->interfaces, node)
541
if (sif->add_dev)
542
sif->add_dev(dev, sif);
543
mutex_unlock(&sp->mutex);
544
subsys_put(sp);
545
}
546
547
/**
548
* bus_remove_device - remove device from bus
549
* @dev: device to be removed
550
*
551
* - Remove device from all interfaces.
552
* - Remove symlink from bus' directory.
553
* - Delete device from bus's list.
554
* - Detach from its driver.
555
* - Drop reference taken in bus_add_device().
556
*/
557
void bus_remove_device(struct device *dev)
558
{
559
struct subsys_private *sp = bus_to_subsys(dev->bus);
560
struct subsys_interface *sif;
561
562
if (!sp)
563
return;
564
565
mutex_lock(&sp->mutex);
566
list_for_each_entry(sif, &sp->interfaces, node)
567
if (sif->remove_dev)
568
sif->remove_dev(dev, sif);
569
mutex_unlock(&sp->mutex);
570
571
sysfs_remove_link(&dev->kobj, "subsystem");
572
sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
573
device_remove_groups(dev, dev->bus->dev_groups);
574
if (klist_node_attached(&dev->p->knode_bus))
575
klist_del(&dev->p->knode_bus);
576
577
pr_debug("bus: '%s': remove device %s\n",
578
dev->bus->name, dev_name(dev));
579
device_release_driver(dev);
580
581
/*
582
* Decrement the reference count twice, once for the bus_to_subsys()
583
* call in the start of this function, and the second one from the
584
* reference increment in bus_add_device()
585
*/
586
subsys_put(sp);
587
subsys_put(sp);
588
}
589
590
static int __must_check add_bind_files(struct device_driver *drv)
591
{
592
int ret;
593
594
ret = driver_create_file(drv, &driver_attr_unbind);
595
if (ret == 0) {
596
ret = driver_create_file(drv, &driver_attr_bind);
597
if (ret)
598
driver_remove_file(drv, &driver_attr_unbind);
599
}
600
return ret;
601
}
602
603
static void remove_bind_files(struct device_driver *drv)
604
{
605
driver_remove_file(drv, &driver_attr_bind);
606
driver_remove_file(drv, &driver_attr_unbind);
607
}
608
609
static BUS_ATTR_WO(drivers_probe);
610
static BUS_ATTR_RW(drivers_autoprobe);
611
612
static int add_probe_files(const struct bus_type *bus)
613
{
614
int retval;
615
616
retval = bus_create_file(bus, &bus_attr_drivers_probe);
617
if (retval)
618
goto out;
619
620
retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
621
if (retval)
622
bus_remove_file(bus, &bus_attr_drivers_probe);
623
out:
624
return retval;
625
}
626
627
static void remove_probe_files(const struct bus_type *bus)
628
{
629
bus_remove_file(bus, &bus_attr_drivers_autoprobe);
630
bus_remove_file(bus, &bus_attr_drivers_probe);
631
}
632
633
static ssize_t uevent_store(struct device_driver *drv, const char *buf,
634
size_t count)
635
{
636
int rc;
637
638
rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
639
return rc ? rc : count;
640
}
641
static DRIVER_ATTR_WO(uevent);
642
643
/**
644
* bus_add_driver - Add a driver to the bus.
645
* @drv: driver.
646
*/
647
int bus_add_driver(struct device_driver *drv)
648
{
649
struct subsys_private *sp = bus_to_subsys(drv->bus);
650
struct driver_private *priv;
651
int error = 0;
652
653
if (!sp)
654
return -EINVAL;
655
656
/*
657
* Reference in sp is now incremented and will be dropped when
658
* the driver is removed from the bus
659
*/
660
pr_debug("bus: '%s': add driver %s\n", sp->bus->name, drv->name);
661
662
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
663
if (!priv) {
664
error = -ENOMEM;
665
goto out_put_bus;
666
}
667
klist_init(&priv->klist_devices, NULL, NULL);
668
priv->driver = drv;
669
drv->p = priv;
670
priv->kobj.kset = sp->drivers_kset;
671
error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
672
"%s", drv->name);
673
if (error)
674
goto out_unregister;
675
676
klist_add_tail(&priv->knode_bus, &sp->klist_drivers);
677
if (sp->drivers_autoprobe) {
678
error = driver_attach(drv);
679
if (error)
680
goto out_del_list;
681
}
682
error = module_add_driver(drv->owner, drv);
683
if (error) {
684
printk(KERN_ERR "%s: failed to create module links for %s\n",
685
__func__, drv->name);
686
goto out_detach;
687
}
688
689
error = driver_create_file(drv, &driver_attr_uevent);
690
if (error) {
691
printk(KERN_ERR "%s: uevent attr (%s) failed\n",
692
__func__, drv->name);
693
}
694
error = driver_add_groups(drv, sp->bus->drv_groups);
695
if (error) {
696
/* How the hell do we get out of this pickle? Give up */
697
printk(KERN_ERR "%s: driver_add_groups(%s) failed\n",
698
__func__, drv->name);
699
}
700
701
if (!drv->suppress_bind_attrs) {
702
error = add_bind_files(drv);
703
if (error) {
704
/* Ditto */
705
printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
706
__func__, drv->name);
707
}
708
}
709
710
return 0;
711
712
out_detach:
713
driver_detach(drv);
714
out_del_list:
715
klist_del(&priv->knode_bus);
716
out_unregister:
717
kobject_put(&priv->kobj);
718
/* drv->p is freed in driver_release() */
719
drv->p = NULL;
720
out_put_bus:
721
subsys_put(sp);
722
return error;
723
}
724
725
/**
726
* bus_remove_driver - delete driver from bus's knowledge.
727
* @drv: driver.
728
*
729
* Detach the driver from the devices it controls, and remove
730
* it from its bus's list of drivers. Finally, we drop the reference
731
* to the bus we took in bus_add_driver().
732
*/
733
void bus_remove_driver(struct device_driver *drv)
734
{
735
struct subsys_private *sp = bus_to_subsys(drv->bus);
736
737
if (!sp)
738
return;
739
740
pr_debug("bus: '%s': remove driver %s\n", sp->bus->name, drv->name);
741
742
if (!drv->suppress_bind_attrs)
743
remove_bind_files(drv);
744
driver_remove_groups(drv, sp->bus->drv_groups);
745
driver_remove_file(drv, &driver_attr_uevent);
746
klist_remove(&drv->p->knode_bus);
747
driver_detach(drv);
748
module_remove_driver(drv);
749
kobject_put(&drv->p->kobj);
750
751
/*
752
* Decrement the reference count twice, once for the bus_to_subsys()
753
* call in the start of this function, and the second one from the
754
* reference increment in bus_add_driver()
755
*/
756
subsys_put(sp);
757
subsys_put(sp);
758
}
759
760
/* Helper for bus_rescan_devices's iter */
761
static int __must_check bus_rescan_devices_helper(struct device *dev,
762
void *data)
763
{
764
int ret = 0;
765
766
if (!dev->driver) {
767
if (dev->parent && dev->bus->need_parent_lock)
768
device_lock(dev->parent);
769
ret = device_attach(dev);
770
if (dev->parent && dev->bus->need_parent_lock)
771
device_unlock(dev->parent);
772
}
773
return ret < 0 ? ret : 0;
774
}
775
776
/**
777
* bus_rescan_devices - rescan devices on the bus for possible drivers
778
* @bus: the bus to scan.
779
*
780
* This function will look for devices on the bus with no driver
781
* attached and rescan it against existing drivers to see if it matches
782
* any by calling device_attach() for the unbound devices.
783
*/
784
int bus_rescan_devices(const struct bus_type *bus)
785
{
786
return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
787
}
788
EXPORT_SYMBOL_GPL(bus_rescan_devices);
789
790
/**
791
* device_reprobe - remove driver for a device and probe for a new driver
792
* @dev: the device to reprobe
793
*
794
* This function detaches the attached driver (if any) for the given
795
* device and restarts the driver probing process. It is intended
796
* to use if probing criteria changed during a devices lifetime and
797
* driver attachment should change accordingly.
798
*/
799
int device_reprobe(struct device *dev)
800
{
801
if (dev->driver)
802
device_driver_detach(dev);
803
return bus_rescan_devices_helper(dev, NULL);
804
}
805
EXPORT_SYMBOL_GPL(device_reprobe);
806
807
static void klist_devices_get(struct klist_node *n)
808
{
809
struct device_private *dev_prv = to_device_private_bus(n);
810
struct device *dev = dev_prv->device;
811
812
get_device(dev);
813
}
814
815
static void klist_devices_put(struct klist_node *n)
816
{
817
struct device_private *dev_prv = to_device_private_bus(n);
818
struct device *dev = dev_prv->device;
819
820
put_device(dev);
821
}
822
823
static ssize_t bus_uevent_store(const struct bus_type *bus,
824
const char *buf, size_t count)
825
{
826
struct subsys_private *sp = bus_to_subsys(bus);
827
int ret;
828
829
if (!sp)
830
return -EINVAL;
831
832
ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count);
833
subsys_put(sp);
834
835
if (ret)
836
return ret;
837
return count;
838
}
839
/*
840
* "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO()
841
* here, but can not use it as earlier in the file we have
842
* DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
843
* function name.
844
*/
845
static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL,
846
bus_uevent_store);
847
848
/**
849
* bus_register - register a driver-core subsystem
850
* @bus: bus to register
851
*
852
* Once we have that, we register the bus with the kobject
853
* infrastructure, then register the children subsystems it has:
854
* the devices and drivers that belong to the subsystem.
855
*/
856
int bus_register(const struct bus_type *bus)
857
{
858
int retval;
859
struct subsys_private *priv;
860
struct kobject *bus_kobj;
861
struct lock_class_key *key;
862
863
priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
864
if (!priv)
865
return -ENOMEM;
866
867
priv->bus = bus;
868
869
BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
870
871
bus_kobj = &priv->subsys.kobj;
872
retval = kobject_set_name(bus_kobj, "%s", bus->name);
873
if (retval)
874
goto out;
875
876
bus_kobj->kset = bus_kset;
877
bus_kobj->ktype = &bus_ktype;
878
priv->drivers_autoprobe = 1;
879
880
retval = kset_register(&priv->subsys);
881
if (retval)
882
goto out;
883
884
retval = bus_create_file(bus, &bus_attr_uevent);
885
if (retval)
886
goto bus_uevent_fail;
887
888
priv->devices_kset = kset_create_and_add("devices", NULL, bus_kobj);
889
if (!priv->devices_kset) {
890
retval = -ENOMEM;
891
goto bus_devices_fail;
892
}
893
894
priv->drivers_kset = kset_create_and_add("drivers", NULL, bus_kobj);
895
if (!priv->drivers_kset) {
896
retval = -ENOMEM;
897
goto bus_drivers_fail;
898
}
899
900
INIT_LIST_HEAD(&priv->interfaces);
901
key = &priv->lock_key;
902
lockdep_register_key(key);
903
__mutex_init(&priv->mutex, "subsys mutex", key);
904
klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
905
klist_init(&priv->klist_drivers, NULL, NULL);
906
907
retval = add_probe_files(bus);
908
if (retval)
909
goto bus_probe_files_fail;
910
911
retval = sysfs_create_groups(bus_kobj, bus->bus_groups);
912
if (retval)
913
goto bus_groups_fail;
914
915
pr_debug("bus: '%s': registered\n", bus->name);
916
return 0;
917
918
bus_groups_fail:
919
remove_probe_files(bus);
920
bus_probe_files_fail:
921
kset_unregister(priv->drivers_kset);
922
bus_drivers_fail:
923
kset_unregister(priv->devices_kset);
924
bus_devices_fail:
925
bus_remove_file(bus, &bus_attr_uevent);
926
bus_uevent_fail:
927
kset_unregister(&priv->subsys);
928
/* Above kset_unregister() will kfree @priv */
929
priv = NULL;
930
out:
931
kfree(priv);
932
return retval;
933
}
934
EXPORT_SYMBOL_GPL(bus_register);
935
936
/**
937
* bus_unregister - remove a bus from the system
938
* @bus: bus.
939
*
940
* Unregister the child subsystems and the bus itself.
941
* Finally, we call bus_put() to release the refcount
942
*/
943
void bus_unregister(const struct bus_type *bus)
944
{
945
struct subsys_private *sp = bus_to_subsys(bus);
946
struct kobject *bus_kobj;
947
948
if (!sp)
949
return;
950
951
pr_debug("bus: '%s': unregistering\n", bus->name);
952
if (sp->dev_root)
953
device_unregister(sp->dev_root);
954
955
bus_kobj = &sp->subsys.kobj;
956
sysfs_remove_groups(bus_kobj, bus->bus_groups);
957
remove_probe_files(bus);
958
bus_remove_file(bus, &bus_attr_uevent);
959
960
kset_unregister(sp->drivers_kset);
961
kset_unregister(sp->devices_kset);
962
kset_unregister(&sp->subsys);
963
subsys_put(sp);
964
}
965
EXPORT_SYMBOL_GPL(bus_unregister);
966
967
int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb)
968
{
969
struct subsys_private *sp = bus_to_subsys(bus);
970
int retval;
971
972
if (!sp)
973
return -EINVAL;
974
975
retval = blocking_notifier_chain_register(&sp->bus_notifier, nb);
976
subsys_put(sp);
977
return retval;
978
}
979
EXPORT_SYMBOL_GPL(bus_register_notifier);
980
981
int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb)
982
{
983
struct subsys_private *sp = bus_to_subsys(bus);
984
int retval;
985
986
if (!sp)
987
return -EINVAL;
988
retval = blocking_notifier_chain_unregister(&sp->bus_notifier, nb);
989
subsys_put(sp);
990
return retval;
991
}
992
EXPORT_SYMBOL_GPL(bus_unregister_notifier);
993
994
void bus_notify(struct device *dev, enum bus_notifier_event value)
995
{
996
struct subsys_private *sp = bus_to_subsys(dev->bus);
997
998
if (!sp)
999
return;
1000
1001
blocking_notifier_call_chain(&sp->bus_notifier, value, dev);
1002
subsys_put(sp);
1003
}
1004
1005
struct kset *bus_get_kset(const struct bus_type *bus)
1006
{
1007
struct subsys_private *sp = bus_to_subsys(bus);
1008
struct kset *kset;
1009
1010
if (!sp)
1011
return NULL;
1012
1013
kset = &sp->subsys;
1014
subsys_put(sp);
1015
1016
return kset;
1017
}
1018
EXPORT_SYMBOL_GPL(bus_get_kset);
1019
1020
/*
1021
* Yes, this forcibly breaks the klist abstraction temporarily. It
1022
* just wants to sort the klist, not change reference counts and
1023
* take/drop locks rapidly in the process. It does all this while
1024
* holding the lock for the list, so objects can't otherwise be
1025
* added/removed while we're swizzling.
1026
*/
1027
static void device_insertion_sort_klist(struct device *a, struct list_head *list,
1028
int (*compare)(const struct device *a,
1029
const struct device *b))
1030
{
1031
struct klist_node *n;
1032
struct device_private *dev_prv;
1033
struct device *b;
1034
1035
list_for_each_entry(n, list, n_node) {
1036
dev_prv = to_device_private_bus(n);
1037
b = dev_prv->device;
1038
if (compare(a, b) <= 0) {
1039
list_move_tail(&a->p->knode_bus.n_node,
1040
&b->p->knode_bus.n_node);
1041
return;
1042
}
1043
}
1044
list_move_tail(&a->p->knode_bus.n_node, list);
1045
}
1046
1047
void bus_sort_breadthfirst(const struct bus_type *bus,
1048
int (*compare)(const struct device *a,
1049
const struct device *b))
1050
{
1051
struct subsys_private *sp = bus_to_subsys(bus);
1052
LIST_HEAD(sorted_devices);
1053
struct klist_node *n, *tmp;
1054
struct device_private *dev_prv;
1055
struct device *dev;
1056
struct klist *device_klist;
1057
1058
if (!sp)
1059
return;
1060
device_klist = &sp->klist_devices;
1061
1062
spin_lock(&device_klist->k_lock);
1063
list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
1064
dev_prv = to_device_private_bus(n);
1065
dev = dev_prv->device;
1066
device_insertion_sort_klist(dev, &sorted_devices, compare);
1067
}
1068
list_splice(&sorted_devices, &device_klist->k_list);
1069
spin_unlock(&device_klist->k_lock);
1070
subsys_put(sp);
1071
}
1072
EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
1073
1074
struct subsys_dev_iter {
1075
struct klist_iter ki;
1076
const struct device_type *type;
1077
};
1078
1079
/**
1080
* subsys_dev_iter_init - initialize subsys device iterator
1081
* @iter: subsys iterator to initialize
1082
* @sp: the subsys private (i.e. bus) we wanna iterate over
1083
* @start: the device to start iterating from, if any
1084
* @type: device_type of the devices to iterate over, NULL for all
1085
*
1086
* Initialize subsys iterator @iter such that it iterates over devices
1087
* of @subsys. If @start is set, the list iteration will start there,
1088
* otherwise if it is NULL, the iteration starts at the beginning of
1089
* the list.
1090
*/
1091
static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct subsys_private *sp,
1092
struct device *start, const struct device_type *type)
1093
{
1094
struct klist_node *start_knode = NULL;
1095
1096
if (start)
1097
start_knode = &start->p->knode_bus;
1098
klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
1099
iter->type = type;
1100
}
1101
1102
/**
1103
* subsys_dev_iter_next - iterate to the next device
1104
* @iter: subsys iterator to proceed
1105
*
1106
* Proceed @iter to the next device and return it. Returns NULL if
1107
* iteration is complete.
1108
*
1109
* The returned device is referenced and won't be released till
1110
* iterator is proceed to the next device or exited. The caller is
1111
* free to do whatever it wants to do with the device including
1112
* calling back into subsys code.
1113
*/
1114
static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1115
{
1116
struct klist_node *knode;
1117
struct device *dev;
1118
1119
for (;;) {
1120
knode = klist_next(&iter->ki);
1121
if (!knode)
1122
return NULL;
1123
dev = to_device_private_bus(knode)->device;
1124
if (!iter->type || iter->type == dev->type)
1125
return dev;
1126
}
1127
}
1128
1129
/**
1130
* subsys_dev_iter_exit - finish iteration
1131
* @iter: subsys iterator to finish
1132
*
1133
* Finish an iteration. Always call this function after iteration is
1134
* complete whether the iteration ran till the end or not.
1135
*/
1136
static void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1137
{
1138
klist_iter_exit(&iter->ki);
1139
}
1140
1141
int subsys_interface_register(struct subsys_interface *sif)
1142
{
1143
struct subsys_private *sp;
1144
struct subsys_dev_iter iter;
1145
struct device *dev;
1146
1147
if (!sif || !sif->subsys)
1148
return -ENODEV;
1149
1150
sp = bus_to_subsys(sif->subsys);
1151
if (!sp)
1152
return -EINVAL;
1153
1154
/*
1155
* Reference in sp is now incremented and will be dropped when
1156
* the interface is removed from the bus
1157
*/
1158
1159
mutex_lock(&sp->mutex);
1160
list_add_tail(&sif->node, &sp->interfaces);
1161
if (sif->add_dev) {
1162
subsys_dev_iter_init(&iter, sp, NULL, NULL);
1163
while ((dev = subsys_dev_iter_next(&iter)))
1164
sif->add_dev(dev, sif);
1165
subsys_dev_iter_exit(&iter);
1166
}
1167
mutex_unlock(&sp->mutex);
1168
1169
return 0;
1170
}
1171
EXPORT_SYMBOL_GPL(subsys_interface_register);
1172
1173
void subsys_interface_unregister(struct subsys_interface *sif)
1174
{
1175
struct subsys_private *sp;
1176
struct subsys_dev_iter iter;
1177
struct device *dev;
1178
1179
if (!sif || !sif->subsys)
1180
return;
1181
1182
sp = bus_to_subsys(sif->subsys);
1183
if (!sp)
1184
return;
1185
1186
mutex_lock(&sp->mutex);
1187
list_del_init(&sif->node);
1188
if (sif->remove_dev) {
1189
subsys_dev_iter_init(&iter, sp, NULL, NULL);
1190
while ((dev = subsys_dev_iter_next(&iter)))
1191
sif->remove_dev(dev, sif);
1192
subsys_dev_iter_exit(&iter);
1193
}
1194
mutex_unlock(&sp->mutex);
1195
1196
/*
1197
* Decrement the reference count twice, once for the bus_to_subsys()
1198
* call in the start of this function, and the second one from the
1199
* reference increment in subsys_interface_register()
1200
*/
1201
subsys_put(sp);
1202
subsys_put(sp);
1203
}
1204
EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1205
1206
static void system_root_device_release(struct device *dev)
1207
{
1208
kfree(dev);
1209
}
1210
1211
static int subsys_register(const struct bus_type *subsys,
1212
const struct attribute_group **groups,
1213
struct kobject *parent_of_root)
1214
{
1215
struct subsys_private *sp;
1216
struct device *dev;
1217
int err;
1218
1219
err = bus_register(subsys);
1220
if (err < 0)
1221
return err;
1222
1223
sp = bus_to_subsys(subsys);
1224
if (!sp) {
1225
err = -EINVAL;
1226
goto err_sp;
1227
}
1228
1229
dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1230
if (!dev) {
1231
err = -ENOMEM;
1232
goto err_dev;
1233
}
1234
1235
err = dev_set_name(dev, "%s", subsys->name);
1236
if (err < 0)
1237
goto err_name;
1238
1239
dev->kobj.parent = parent_of_root;
1240
dev->groups = groups;
1241
dev->release = system_root_device_release;
1242
1243
err = device_register(dev);
1244
if (err < 0)
1245
goto err_dev_reg;
1246
1247
sp->dev_root = dev;
1248
subsys_put(sp);
1249
return 0;
1250
1251
err_dev_reg:
1252
put_device(dev);
1253
dev = NULL;
1254
err_name:
1255
kfree(dev);
1256
err_dev:
1257
subsys_put(sp);
1258
err_sp:
1259
bus_unregister(subsys);
1260
return err;
1261
}
1262
1263
/**
1264
* subsys_system_register - register a subsystem at /sys/devices/system/
1265
* @subsys: system subsystem
1266
* @groups: default attributes for the root device
1267
*
1268
* All 'system' subsystems have a /sys/devices/system/<name> root device
1269
* with the name of the subsystem. The root device can carry subsystem-
1270
* wide attributes. All registered devices are below this single root
1271
* device and are named after the subsystem with a simple enumeration
1272
* number appended. The registered devices are not explicitly named;
1273
* only 'id' in the device needs to be set.
1274
*
1275
* Do not use this interface for anything new, it exists for compatibility
1276
* with bad ideas only. New subsystems should use plain subsystems; and
1277
* add the subsystem-wide attributes should be added to the subsystem
1278
* directory itself and not some create fake root-device placed in
1279
* /sys/devices/system/<name>.
1280
*/
1281
int subsys_system_register(const struct bus_type *subsys,
1282
const struct attribute_group **groups)
1283
{
1284
return subsys_register(subsys, groups, &system_kset->kobj);
1285
}
1286
EXPORT_SYMBOL_GPL(subsys_system_register);
1287
1288
/**
1289
* subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1290
* @subsys: virtual subsystem
1291
* @groups: default attributes for the root device
1292
*
1293
* All 'virtual' subsystems have a /sys/devices/system/<name> root device
1294
* with the name of the subsystem. The root device can carry subsystem-wide
1295
* attributes. All registered devices are below this single root device.
1296
* There's no restriction on device naming. This is for kernel software
1297
* constructs which need sysfs interface.
1298
*/
1299
int subsys_virtual_register(const struct bus_type *subsys,
1300
const struct attribute_group **groups)
1301
{
1302
struct kobject *virtual_dir;
1303
1304
virtual_dir = virtual_device_parent();
1305
if (!virtual_dir)
1306
return -ENOMEM;
1307
1308
return subsys_register(subsys, groups, virtual_dir);
1309
}
1310
EXPORT_SYMBOL_GPL(subsys_virtual_register);
1311
1312
/**
1313
* driver_find - locate driver on a bus by its name.
1314
* @name: name of the driver.
1315
* @bus: bus to scan for the driver.
1316
*
1317
* Call kset_find_obj() to iterate over list of drivers on
1318
* a bus to find driver by name. Return driver if found.
1319
*
1320
* This routine provides no locking to prevent the driver it returns
1321
* from being unregistered or unloaded while the caller is using it.
1322
* The caller is responsible for preventing this.
1323
*/
1324
struct device_driver *driver_find(const char *name, const struct bus_type *bus)
1325
{
1326
struct subsys_private *sp = bus_to_subsys(bus);
1327
struct kobject *k;
1328
struct driver_private *priv;
1329
1330
if (!sp)
1331
return NULL;
1332
1333
k = kset_find_obj(sp->drivers_kset, name);
1334
subsys_put(sp);
1335
if (!k)
1336
return NULL;
1337
1338
priv = to_driver(k);
1339
1340
/* Drop reference added by kset_find_obj() */
1341
kobject_put(k);
1342
return priv->driver;
1343
}
1344
EXPORT_SYMBOL_GPL(driver_find);
1345
1346
/*
1347
* Warning, the value could go to "removed" instantly after calling this function, so be very
1348
* careful when calling it...
1349
*/
1350
bool bus_is_registered(const struct bus_type *bus)
1351
{
1352
struct subsys_private *sp = bus_to_subsys(bus);
1353
bool is_initialized = false;
1354
1355
if (sp) {
1356
is_initialized = true;
1357
subsys_put(sp);
1358
}
1359
return is_initialized;
1360
}
1361
1362
/**
1363
* bus_get_dev_root - return a pointer to the "device root" of a bus
1364
* @bus: bus to return the device root of.
1365
*
1366
* If a bus has a "device root" structure, return it, WITH THE REFERENCE
1367
* COUNT INCREMENTED.
1368
*
1369
* Note, when finished with the device, a call to put_device() is required.
1370
*
1371
* If the device root is not present (or bus is not a valid pointer), NULL
1372
* will be returned.
1373
*/
1374
struct device *bus_get_dev_root(const struct bus_type *bus)
1375
{
1376
struct subsys_private *sp = bus_to_subsys(bus);
1377
struct device *dev_root;
1378
1379
if (!sp)
1380
return NULL;
1381
1382
dev_root = get_device(sp->dev_root);
1383
subsys_put(sp);
1384
return dev_root;
1385
}
1386
EXPORT_SYMBOL_GPL(bus_get_dev_root);
1387
1388
int __init buses_init(void)
1389
{
1390
bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
1391
if (!bus_kset)
1392
return -ENOMEM;
1393
1394
system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1395
if (!system_kset) {
1396
/* Do error handling here as devices_init() do */
1397
kset_unregister(bus_kset);
1398
bus_kset = NULL;
1399
pr_err("%s: failed to create and add kset 'bus'\n", __func__);
1400
return -ENOMEM;
1401
}
1402
1403
return 0;
1404
}
1405
1406