source: GPL/branches/uniaud32-next/lib32/driver.c@ 715

Last change on this file since 715 was 715, checked in by Paul Smedley, 3 years ago

Code cleanups, fix trap on emu10k1

File size: 21.7 KB
Line 
1/*
2 * driver.c - centralized device driver management
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2007 Novell Inc.
8 *
9 * This file is released under the GPLv2
10 *
11 */
12
13// current based on linux 3.0.95 code
14
15#include <linux/device.h>
16#include <linux/printk.h>
17#include <linux/pci.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/slab.h>
21#include <linux/string.h>
22#include <linux/mm.h>
23#include <linux/klist.h>
24#include <linux/notifier.h>
25#include <linux/pm_runtime.h>
26#include "base.h"
27
28#define devres_log(dev, node, op) do {} while (0)
29
30struct devres_node {
31 struct list_head entry;
32 dr_release_t release;
33#ifdef CONFIG_DEBUG_DEVRES
34 const char *name;
35 size_t size;
36#endif
37};
38
39struct devres {
40 struct devres_node node;
41 /* -- 3 pointers */
42 unsigned long long data[1]; /* guarantee ull alignment */
43};
44
45
46/**
47 * dev_set_name - set a device name
48 * @dev: device
49 * @fmt: format string for the device's name
50 */
51int dev_set_name(struct device *dev, const char *fmt, ...)
52{
53 va_list vargs;
54 int err;
55
56 va_start(vargs, fmt);
57 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
58 va_end(vargs);
59 return err;
60}
61
62static void add_dr(struct device *dev, struct devres_node *node)
63{
64 devres_log(dev, node, "ADD");
65 BUG_ON(!list_empty(&node->entry));
66 list_add_tail(&node->entry, &dev->devres_head);
67}
68
69/**
70 * devres_add - Register device resource
71 * @dev: Device to add resource to
72 * @res: Resource to register
73 *
74 * Register devres @res to @dev. @res should have been allocated
75 * using devres_alloc(). On driver detach, the associated release
76 * function will be invoked and devres will be freed automatically.
77 */
78void devres_add(struct device *dev, void *res)
79{
80#ifndef TARGET_OS2
81 /* Traps here on OS/2 */
82 struct devres *dr = container_of(res, struct devres, data);
83 unsigned long flags;
84
85 spin_lock_irqsave(&dev->devres_lock, flags);
86 add_dr(dev, &dr->node);
87 spin_unlock_irqrestore(&dev->devres_lock, flags);
88#endif
89}
90
91static struct device *next_device(struct klist_iter *i)
92{
93 struct klist_node *n = klist_next(i);
94 struct device *dev = NULL;
95 struct device_private *dev_prv;
96
97 if (n) {
98 dev_prv = to_device_private_driver(n);
99 dev = dev_prv->device;
100 }
101 return dev;
102}
103
104#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
105
106static struct bus_type *bus_get(struct bus_type *bus)
107{
108 if (bus) {
109 kset_get(&bus->p->subsys);
110 return bus;
111 }
112 return NULL;
113}
114
115static void bus_put(struct bus_type *bus)
116{
117 if (bus)
118 kset_put(&bus->p->subsys);
119}
120
121static void driver_release(struct kobject *kobj)
122{
123 struct driver_private *drv_priv = to_driver(kobj);
124
125 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
126 kfree(drv_priv);
127}
128
129static struct kobj_type driver_ktype = {
130// .sysfs_ops = &driver_sysfs_ops,
131 .release = driver_release,
132};
133
134/*
135 * sysfs bindings for buses
136 */
137static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
138 char *buf)
139{
140 struct bus_attribute *bus_attr = to_bus_attr(attr);
141 struct subsys_private *subsys_priv = to_subsys_private(kobj);
142 ssize_t ret = 0;
143
144 if (bus_attr->show)
145 ret = bus_attr->show(subsys_priv->bus, buf);
146 return ret;
147}
148
149static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
150 const char *buf, size_t count)
151{
152 struct bus_attribute *bus_attr = to_bus_attr(attr);
153 struct subsys_private *subsys_priv = to_subsys_private(kobj);
154 ssize_t ret = 0;
155
156 if (bus_attr->store)
157 ret = bus_attr->store(subsys_priv->bus, buf, count);
158 return ret;
159}
160
161static const struct sysfs_ops bus_sysfs_ops = {
162 .show = bus_attr_show,
163 .store = bus_attr_store,
164};
165
166static struct kobj_type bus_ktype = {
167 .sysfs_ops = &bus_sysfs_ops,
168};
169
170
171static void klist_devices_get(struct klist_node *n)
172{
173 struct device_private *dev_prv = to_device_private_bus(n);
174 struct device *dev = dev_prv->device;
175
176 get_device(dev);
177}
178
179static void klist_devices_put(struct klist_node *n)
180{
181 struct device_private *dev_prv = to_device_private_bus(n);
182 struct device *dev = dev_prv->device;
183
184 put_device(dev);
185}
186
187static struct kset *bus_kset;
188
189/**
190 * bus_add_driver - Add a driver to the bus.
191 * @drv: driver.
192 */
193int bus_add_driver(struct device_driver *drv)
194{
195 struct bus_type *bus;
196 struct driver_private *priv;
197 int error = 0;
198
199 bus = bus_get(drv->bus);
200 if (!bus)
201 return -EINVAL;
202
203 pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
204
205 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
206 if (!priv) {
207 error = -ENOMEM;
208 goto out_put_bus;
209 }
210 klist_init(&priv->klist_devices, NULL, NULL);
211 priv->driver = drv;
212 drv->p = priv;
213
214 priv->kobj.kset = bus->p->drivers_kset; // traps?
215 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
216 "%s", drv->name);
217 if (error)
218 goto out_unregister;
219
220 if (drv->bus->p->drivers_autoprobe) {
221 error = driver_attach(drv);
222 if (error)
223 goto out_unregister;
224 }
225 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
226
227 module_add_driver(drv->owner, drv);
228
229#if 0 //not required ?
230 error = driver_create_file(drv, &driver_attr_uevent);
231 if (error) {
232 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
233 __func__, drv->name);
234 }
235 error = driver_add_attrs(bus, drv);
236 if (error) {
237 /* How the hell do we get out of this pickle? Give up */
238 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
239 __func__, drv->name);
240 }
241
242 if (!drv->suppress_bind_attrs) {
243 error = add_bind_files(drv);
244 if (error) {
245 /* Ditto */
246 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
247 __func__, drv->name);
248 }
249 }
250#endif
251// kobject_uevent(&priv->kobj, KOBJ_ADD);
252 return 0;
253
254out_unregister:
255 kobject_put(&priv->kobj);
256 kfree(drv->p);
257 drv->p = NULL;
258out_put_bus:
259 bus_put(bus);
260 return error;
261}
262
263/**
264 * bus_remove_driver - delete driver from bus's knowledge.
265 * @drv: driver.
266 *
267 * Detach the driver from the devices it controls, and remove
268 * it from its bus's list of drivers. Finally, we drop the reference
269 * to the bus we took in bus_add_driver().
270 */
271void bus_remove_driver(struct device_driver *drv)
272{
273 if (!drv->bus)
274 return;
275
276// if (!drv->suppress_bind_attrs)
277// remove_bind_files(drv);
278// driver_remove_attrs(drv->bus, drv);
279// driver_remove_file(drv, &driver_attr_uevent);
280 klist_remove(&drv->p->knode_bus);
281 pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
282 driver_detach(drv);
283 module_remove_driver(drv);
284
285 kobject_put(&drv->p->kobj);
286 bus_put(drv->bus);
287}
288
289/**
290 * bus_for_each_dev - device iterator.
291 * @bus: bus type.
292 * @start: device to start iterating from.
293 * @data: data for the callback.
294 * @fn: function to be called for each device.
295 *
296 * Iterate over @bus's list of devices, and call @fn for each,
297 * passing it @data. If @start is not NULL, we use that device to
298 * begin iterating from.
299 *
300 * We check the return of @fn each time. If it returns anything
301 * other than 0, we break out and return that value.
302 *
303 * NOTE: The device that returns a non-zero value is not retained
304 * in any way, nor is its refcount incremented. If the caller needs
305 * to retain this data, it should do so, and increment the reference
306 * count in the supplied callback.
307 */
308int bus_for_each_dev(struct bus_type *bus, struct device *start,
309 void *data, int (*fn)(struct device *, void *))
310{
311 struct klist_iter i;
312 struct device *dev;
313 int error = 0;
314
315 if (!bus || !bus->p)
316 return -EINVAL;
317
318 klist_iter_init_node(&bus->p->klist_devices, &i,
319 (start ? &start->p->knode_bus : NULL));
320 while ((dev = next_device(&i)) && !error)
321 error = fn(dev, data);
322 klist_iter_exit(&i);
323 return error;
324}
325EXPORT_SYMBOL_GPL(bus_for_each_dev);
326
327/**
328 * driver_find - locate driver on a bus by its name.
329 * @name: name of the driver.
330 * @bus: bus to scan for the driver.
331 *
332 * Call kset_find_obj() to iterate over list of drivers on
333 * a bus to find driver by name. Return driver if found.
334 *
335 * Note that kset_find_obj increments driver's reference count.
336 */
337struct device_driver *driver_find(const char *name, struct bus_type *bus)
338{
339 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
340 struct driver_private *priv;
341
342 if (k) {
343 priv = to_driver(k);
344 return priv->driver;
345 }
346 return NULL;
347}
348
349/**
350 * driver_register - register driver with bus
351 * @drv: driver to register
352 *
353 * We pass off most of the work to the bus_add_driver() call,
354 * since most of the things we have to do deal with the bus
355 * structures.
356 */
357int driver_register(struct device_driver *drv)
358{
359 int ret;
360 struct device_driver *other;
361
362 BUG_ON(!drv->bus->p);
363#ifndef TARGET_OS2
364 if ((drv->bus->probe && drv->probe) ||
365 (drv->bus->remove && drv->remove) ||
366 (drv->bus->shutdown && drv->shutdown))
367 printk(KERN_WARNING "Driver '%s' needs updating - please use "
368 "bus_type methods\n", drv->name);
369#endif
370 other = driver_find(drv->name, drv->bus);
371 if (other) {
372 printk(KERN_ERR "Error: Driver '%s' is already registered, "
373 "aborting...\n", drv->name);
374 return -EBUSY;
375 }
376
377 ret = bus_add_driver(drv);
378 if (ret)
379 return ret;
380#if 0
381 ret = driver_add_groups(drv, drv->groups);
382 if (ret) {
383 bus_remove_driver(drv);
384 return ret;
385 }
386 kobject_uevent(&drv->p->kobj, KOBJ_ADD);
387#endif
388 return ret;
389}
390
391/**
392 * driver_unregister - remove driver from system.
393 * @drv: driver.
394 *
395 * Again, we pass off most of the work to the bus-level call.
396 */
397void driver_unregister(struct device_driver *drv)
398{
399 if (!drv || !drv->p) {
400 WARN(1, "Unexpected driver unregister!\n");
401 return;
402 }
403// driver_remove_groups(drv, drv->groups);
404 bus_remove_driver(drv);
405}
406EXPORT_SYMBOL_GPL(driver_unregister);
407
408
409/**
410 * driver_find_device - device iterator for locating a particular device.
411 * @drv: The device's driver
412 * @start: Device to begin with
413 * @data: Data to pass to match function
414 * @match: Callback function to check device
415 *
416 * This is similar to the driver_for_each_device() function above, but
417 * it returns a reference to a device that is 'found' for later use, as
418 * determined by the @match callback.
419 *
420 * The callback should return 0 if the device doesn't match and non-zero
421 * if it does. If the callback returns non-zero, this function will
422 * return to the caller and not iterate over any more devices.
423 */
424struct device *driver_find_device(struct device_driver *drv,
425 struct device *start, void *data,
426 int (*match)(struct device *dev, void *data))
427{
428 struct klist_iter i;
429 struct device *dev;
430
431 if (!drv)
432 return NULL;
433
434 klist_iter_init_node(&drv->p->klist_devices, &i,
435 (start ? &start->p->knode_driver : NULL));
436 while ((dev = next_device(&i)))
437 if (match(dev, data) && get_device(dev))
438 break;
439 klist_iter_exit(&i);
440 return dev;
441}
442EXPORT_SYMBOL_GPL(driver_find_device);
443
444
445/**
446 * bus_register - register a bus with the system.
447 * @bus: bus.
448 *
449 * Once we have that, we registered the bus with the kobject
450 * infrastructure, then register the children subsystems it has:
451 * the devices and drivers that belong to the bus.
452 */
453int bus_register(struct bus_type *bus)
454{
455 int retval;
456 struct subsys_private *priv;
457
458 priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
459 if (!priv)
460 return -ENOMEM;
461
462 priv->bus = bus;
463 bus->p = priv;
464
465 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
466
467 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
468 if (retval)
469 goto out;
470
471 priv->subsys.kobj.kset = bus_kset;
472 priv->subsys.kobj.ktype = &bus_ktype;
473 priv->drivers_autoprobe = 1;
474
475 retval = kset_register(&priv->subsys);
476 if (retval)
477 goto out;
478
479#if 0
480 retval = bus_create_file(bus, &bus_attr_uevent);
481 if (retval)
482 goto bus_uevent_fail;
483#endif
484 priv->devices_kset = kset_create_and_add("devices", NULL,
485 &priv->subsys.kobj);
486 if (!priv->devices_kset) {
487 retval = -ENOMEM;
488 goto bus_devices_fail;
489 }
490
491 priv->drivers_kset = kset_create_and_add("drivers", NULL,
492 &priv->subsys.kobj);
493 if (!priv->drivers_kset) {
494 retval = -ENOMEM;
495 goto bus_drivers_fail;
496 }
497
498 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
499 klist_init(&priv->klist_drivers, NULL, NULL);
500
501#if 0
502 retval = add_probe_files(bus);
503 if (retval)
504 goto bus_probe_files_fail;
505
506 retval = bus_add_attrs(bus);
507 if (retval)
508 goto bus_attrs_fail;
509#endif
510 pr_debug("bus: '%s': registered\n", bus->name);
511 return 0;
512
513//bus_attrs_fail:
514// remove_probe_files(bus);
515//bus_probe_files_fail:
516// kset_unregister(bus->p->drivers_kset);
517bus_drivers_fail:
518 kset_unregister(bus->p->devices_kset);
519bus_devices_fail:
520// bus_remove_file(bus, &bus_attr_uevent);
521//bus_uevent_fail:
522 kset_unregister(&bus->p->subsys);
523out:
524 kfree(bus->p);
525 bus->p = NULL;
526 return retval;
527}
528EXPORT_SYMBOL_GPL(bus_register);
529
530static int __driver_attach(struct device *dev, void *data)
531{
532 struct device_driver *drv = data;
533
534 /*
535 * Lock device and try to bind to it. We drop the error
536 * here and always return 0, because we need to keep trying
537 * to bind to devices and some drivers will return an error
538 * simply if it didn't support the device.
539 *
540 * driver_probe_device() will spit a warning if there
541 * is an error.
542 */
543
544 if (!driver_match_device(drv, dev))
545 return 0;
546
547 if (dev->parent) /* Needed for USB */
548 device_lock(dev->parent);
549 device_lock(dev);
550 if (!dev->driver)
551 driver_probe_device(drv, dev);
552 device_unlock(dev);
553 if (dev->parent)
554 device_unlock(dev->parent);
555
556 return 0;
557}
558
559/**
560 * driver_attach - try to bind driver to devices.
561 * @drv: driver.
562 *
563 * Walk the list of devices that the bus has on it and try to
564 * match the driver with each one. If driver_probe_device()
565 * returns 0 and the @dev->driver is set, we've found a
566 * compatible pair.
567 */
568int driver_attach(struct device_driver *drv)
569{
570 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
571}
572
573static atomic_t probe_count = ATOMIC_INIT(0);
574static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
575
576static int really_probe(struct device *dev, struct device_driver *drv)
577{
578 int ret = 0;
579 atomic_inc(&probe_count);
580 pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
581 drv->bus->name, __func__, drv->name, dev_name(dev));
582
583 WARN_ON(!list_empty(&dev->devres_head));
584
585 dev->driver = drv;
586
587#if 0
588 if (driver_sysfs_add(dev)) {
589 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
590 __func__, dev_name(dev));
591 goto probe_failed;
592 }
593#endif
594
595 if (dev->bus->probe) {
596 ret = dev->bus->probe(dev);
597// if (ret)
598// goto probe_failed;
599 } else if (drv->probe) {
600 ret = drv->probe(dev);
601// if (ret)
602// goto probe_failed;
603 }
604#ifdef TARGET_OS2xxx
605 driver_bound(dev);
606 ret = 1;
607 pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
608 drv->bus->name, __func__, dev_name(dev), drv->name);
609 goto done;
610
611probe_failed:
612 devres_release_all(dev);
613// driver_sysfs_remove(dev);
614 dev->driver = NULL;
615
616 if (ret != -ENODEV && ret != -ENXIO) {
617 /* driver matched but the probe failed */
618 printk(KERN_WARNING
619 "%s: probe of %s failed with error %d\n",
620 drv->name, dev_name(dev), ret);
621 }
622 /*
623 * Ignore errors returned by ->probe so that the next driver can try
624 * its luck.
625 */
626 ret = 0;
627done:
628 atomic_dec(&probe_count);
629 wake_up(&probe_waitqueue);
630#endif
631 return ret;
632}
633
634/*
635 * __device_release_driver() must be called with @dev lock held.
636 * When called for a USB interface, @dev->parent lock must be held as well.
637 */
638static void __device_release_driver(struct device *dev)
639{
640 struct device_driver *drv;
641
642 drv = dev->driver;
643 if (drv) {
644 pm_runtime_get_sync(dev);
645
646#if 0
647 driver_sysfs_remove(dev);
648
649 if (dev->bus)
650 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
651 BUS_NOTIFY_UNBIND_DRIVER,
652 dev);
653#endif
654 pm_runtime_put_sync(dev);
655
656 if (dev->bus && dev->bus->remove)
657 dev->bus->remove(dev);
658 else if (drv->remove)
659 drv->remove(dev);
660 devres_release_all(dev);
661 dev->driver = NULL;
662 klist_remove(&dev->p->knode_driver);
663#if 0
664 if (dev->bus)
665 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
666 BUS_NOTIFY_UNBOUND_DRIVER,
667 dev);
668#endif
669 }
670}
671
672/**
673 * driver_detach - detach driver from all devices it controls.
674 * @drv: driver.
675 */
676void driver_detach(struct device_driver *drv)
677{
678 struct device_private *dev_prv;
679 struct device *dev;
680
681 for (;;) {
682 spin_lock(&drv->p->klist_devices.k_lock);
683 if (list_empty(&drv->p->klist_devices.k_list)) {
684 spin_unlock(&drv->p->klist_devices.k_lock);
685 break;
686 }
687 dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
688 struct device_private,
689 knode_driver.n_node);
690 dev = dev_prv->device;
691 get_device(dev);
692 spin_unlock(&drv->p->klist_devices.k_lock);
693
694 if (dev->parent) /* Needed for USB */
695 device_lock(dev->parent);
696 device_lock(dev);
697 if (dev->driver == drv)
698 __device_release_driver(dev);
699 device_unlock(dev);
700 if (dev->parent)
701 device_unlock(dev->parent);
702 put_device(dev);
703 }
704}
705
706/**
707 * driver_probe_device - attempt to bind device & driver together
708 * @drv: driver to bind a device to
709 * @dev: device to try to bind to the driver
710 *
711 * This function returns -ENODEV if the device is not registered,
712 * 1 if the device is bound successfully and 0 otherwise.
713 *
714 * This function must be called with @dev lock held. When called for a
715 * USB interface, @dev->parent lock must be held as well.
716 */
717int driver_probe_device(struct device_driver *drv, struct device *dev)
718{
719 int ret = 0;
720#if 0
721 if (!device_is_registered(dev))
722 return -ENODEV;
723#endif
724 pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
725 drv->bus->name, __func__, dev_name(dev), drv->name);
726 pm_runtime_get_noresume(dev);
727 pm_runtime_barrier(dev);
728
729 ret = really_probe(dev, drv);
730 pm_runtime_put_sync(dev);
731 return ret;
732}
733
734static int __device_attach(struct device_driver *drv, void *data)
735{
736 struct device *dev = data;
737
738 if (!driver_match_device(drv, dev))
739 return 0;
740
741 return driver_probe_device(drv, dev);
742}
743
744/**
745 * device_attach - try to attach device to a driver.
746 * @dev: device.
747 *
748 * Walk the list of drivers that the bus has and call
749 * driver_probe_device() for each pair. If a compatible
750 * pair is found, break out and return.
751 *
752 * Returns 1 if the device was bound to a driver;
753 * 0 if no matching driver was found;
754 * -ENODEV if the device is not registered.
755 *
756 * When called for a USB interface, @dev->parent lock must be held.
757 */
758int device_attach(struct device *dev)
759{
760 int ret = 0;
761 device_lock(dev);
762 if (dev->driver) {
763 if (klist_node_attached(&dev->p->knode_driver)) {
764 ret = 1;
765 goto out_unlock;
766 }
767 ret = device_bind_driver(dev);
768 if (ret == 0)
769 ret = 1;
770 else {
771 dev->driver = NULL;
772 ret = 0;
773 }
774 } else {
775 pm_runtime_get_noresume(dev);
776 ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
777 pm_runtime_put_sync(dev);
778 }
779out_unlock:
780 device_unlock(dev);
781 return ret;
782}
783EXPORT_SYMBOL_GPL(device_attach);
784
785static struct device_driver *next_driver(struct klist_iter *i)
786{
787 struct klist_node *n = klist_next(i);
788 struct driver_private *drv_priv;
789
790 if (n) {
791 drv_priv = container_of(n, struct driver_private, knode_bus);
792 return drv_priv->driver;
793 }
794 return NULL;
795}
796
797/**
798 * bus_for_each_drv - driver iterator
799 * @bus: bus we're dealing with.
800 * @start: driver to start iterating on.
801 * @data: data to pass to the callback.
802 * @fn: function to call for each driver.
803 *
804 * This is nearly identical to the device iterator above.
805 * We iterate over each driver that belongs to @bus, and call
806 * @fn for each. If @fn returns anything but 0, we break out
807 * and return it. If @start is not NULL, we use it as the head
808 * of the list.
809 *
810 * NOTE: we don't return the driver that returns a non-zero
811 * value, nor do we leave the reference count incremented for that
812 * driver. If the caller needs to know that info, it must set it
813 * in the callback. It must also be sure to increment the refcount
814 * so it doesn't disappear before returning to the caller.
815 */
816int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
817 void *data, int (*fn)(struct device_driver *, void *))
818{
819 struct klist_iter i;
820 struct device_driver *drv;
821 int error = 0;
822
823 if (!bus)
824 return -EINVAL;
825
826 klist_iter_init_node(&bus->p->klist_drivers, &i,
827 start ? &start->p->knode_bus : NULL);
828 while ((drv = next_driver(&i)) && !error)
829 error = fn(drv, data);
830 klist_iter_exit(&i);
831 return error;
832}
833EXPORT_SYMBOL_GPL(bus_for_each_drv);
834
835static void driver_bound(struct device *dev)
836{
837 if (klist_node_attached(&dev->p->knode_driver)) {
838 printk(KERN_WARNING "%s: device %s already bound\n",
839 __func__, kobject_name(&dev->kobj));
840 return;
841 }
842
843 pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
844 __func__, dev->driver->name);
845
846 klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
847
848#if 0
849 if (dev->bus)
850 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
851 BUS_NOTIFY_BOUND_DRIVER, dev);
852#endif
853}
854
855/**
856 * device_bind_driver - bind a driver to one device.
857 * @dev: device.
858 *
859 * Allow manual attachment of a driver to a device.
860 * Caller must have already set @dev->driver.
861 *
862 * Note that this does not modify the bus reference count
863 * nor take the bus's rwsem. Please verify those are accounted
864 * for before calling this. (It is ok to call with no other effort
865 * from a driver's probe() method.)
866 *
867 * This function must be called with the device lock held.
868 */
869int device_bind_driver(struct device *dev)
870{
871 int ret=0;
872
873// ret = driver_sysfs_add(dev);
874// if (!ret)
875 driver_bound(dev);
876 return ret;
877}
878EXPORT_SYMBOL_GPL(device_bind_driver);
Note: See TracBrowser for help on using the repository browser.