^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * driver.c - centralized device driver management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2002-3 Patrick Mochel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2002-3 Open Source Development Labs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2007 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/device/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "base.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static struct device *next_device(struct klist_iter *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct klist_node *n = klist_next(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct device_private *dev_prv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) dev_prv = to_device_private_driver(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) dev = dev_prv->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * driver_for_each_device - Iterator for devices bound to a driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @drv: Driver we're iterating.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @start: Device to begin with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @data: Data to pass to the callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @fn: Function to call for each device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Iterate over the @drv's list of devices calling @fn for each one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int driver_for_each_device(struct device_driver *drv, struct device *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void *data, int (*fn)(struct device *, void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct klist_iter i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (!drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) klist_iter_init_node(&drv->p->klist_devices, &i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) start ? &start->p->knode_driver : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) while (!error && (dev = next_device(&i)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) error = fn(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) klist_iter_exit(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) EXPORT_SYMBOL_GPL(driver_for_each_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * driver_find_device - device iterator for locating a particular device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @drv: The device's driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @start: Device to begin with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @data: Data to pass to match function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @match: Callback function to check device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * This is similar to the driver_for_each_device() function above, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * it returns a reference to a device that is 'found' for later use, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * determined by the @match callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * The callback should return 0 if the device doesn't match and non-zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * if it does. If the callback returns non-zero, this function will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * return to the caller and not iterate over any more devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct device *driver_find_device(struct device_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct device *start, const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int (*match)(struct device *dev, const void *data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct klist_iter i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!drv || !drv->p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) klist_iter_init_node(&drv->p->klist_devices, &i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) (start ? &start->p->knode_driver : NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) while ((dev = next_device(&i)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (match(dev, data) && get_device(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) klist_iter_exit(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) EXPORT_SYMBOL_GPL(driver_find_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * driver_create_file - create sysfs file for driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * @drv: driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @attr: driver attribute descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int driver_create_file(struct device_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) const struct driver_attribute *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) error = sysfs_create_file(&drv->p->kobj, &attr->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) EXPORT_SYMBOL_GPL(driver_create_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * driver_remove_file - remove sysfs file for driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * @drv: driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @attr: driver attribute descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) void driver_remove_file(struct device_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) const struct driver_attribute *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) sysfs_remove_file(&drv->p->kobj, &attr->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) EXPORT_SYMBOL_GPL(driver_remove_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int driver_add_groups(struct device_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) const struct attribute_group **groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return sysfs_create_groups(&drv->p->kobj, groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void driver_remove_groups(struct device_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) const struct attribute_group **groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) sysfs_remove_groups(&drv->p->kobj, groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * driver_register - register driver with bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * @drv: driver to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * We pass off most of the work to the bus_add_driver() call,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * since most of the things we have to do deal with the bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int driver_register(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct device_driver *other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!drv->bus->p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) drv->name, drv->bus->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if ((drv->bus->probe && drv->probe) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) (drv->bus->remove && drv->remove) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) (drv->bus->shutdown && drv->shutdown))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) pr_warn("Driver '%s' needs updating - please use "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) "bus_type methods\n", drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) other = driver_find(drv->name, drv->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (other) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) pr_err("Error: Driver '%s' is already registered, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) "aborting...\n", drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ret = bus_add_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ret = driver_add_groups(drv, drv->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) bus_remove_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) kobject_uevent(&drv->p->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) EXPORT_SYMBOL_GPL(driver_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * driver_unregister - remove driver from system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * @drv: driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * Again, we pass off most of the work to the bus-level call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) void driver_unregister(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (!drv || !drv->p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) WARN(1, "Unexpected driver unregister!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) driver_remove_groups(drv, drv->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) bus_remove_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) EXPORT_SYMBOL_GPL(driver_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * driver_find - locate driver on a bus by its name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * @name: name of the driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * @bus: bus to scan for the driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * Call kset_find_obj() to iterate over list of drivers on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * a bus to find driver by name. Return driver if found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * This routine provides no locking to prevent the driver it returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * from being unregistered or unloaded while the caller is using it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * The caller is responsible for preventing this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct device_driver *driver_find(const char *name, struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct driver_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /* Drop reference added by kset_find_obj() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) kobject_put(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) priv = to_driver(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return priv->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) EXPORT_SYMBOL_GPL(driver_find);