VIPPER
VIPPER

Reputation: 352

List all devices bound to a driver

Let's say I have drivers A and B in the Linux kernel space, with devices bound to them. I would like to export API in the driver A to B to provide list of devices bound to the driver A. Is it possible for a driver A to get to know about all devices currently being detected and bound to that driver?

Upvotes: 1

Views: 714

Answers (2)

Li Chen
Li Chen

Reputation: 5270

If your driver is pci driver, then:

void get_all_devices_of_driver(const char *driver_name)
{
    bool found = false;

    struct pci_dev *pdev = NULL;
    for_each_pci_dev (pdev) {
        if (!strcmp(dev_driver_string(&pdev->dev),
                driver_name)) {
                // do what you want
        }
    }
}

Or use kernel's general helper function:

/**
 * driver_for_each_device - Iterator for devices bound to a driver.
 * @drv: Driver we're iterating.
 * @start: Device to begin with
 * @data: Data to pass to the callback.
 * @fn: Function to call for each device.
 *
 * Iterate over the @drv's list of devices calling @fn for each one.
 */
int driver_for_each_device(struct device_driver *drv, struct device *start,
               void *data, int (*fn)(struct device *, void *))
{
    struct klist_iter i;
    struct device *dev;
    int error = 0;

    if (!drv)
        return -EINVAL;

    klist_iter_init_node(&drv->p->klist_devices, &i,
                 start ? &start->p->knode_driver : NULL);
    while (!error && (dev = next_device(&i)))
        error = fn(dev, data);
    klist_iter_exit(&i);
    return error;
}

Upvotes: 0

tej parkash
tej parkash

Reputation: 152

Yes, simply register functions with A when driver B loads and call same function whenever device list is required. e.g

Driver A <<< register_func(func_ptr_list); export register_func Driver B <<< Call register_func with function list.

Multiple driver talks to each other using similar function element. for example Look at module_int for cxgb4 and cxgb4i

Upvotes: 1

Related Questions