// SPDX-License-Identifier: GPL-2.01/*2* driver.c - centralized device driver management3*4* Copyright (c) 2002-3 Patrick Mochel5* Copyright (c) 2002-3 Open Source Development Labs6* Copyright (c) 2007 Greg Kroah-Hartman <[email protected]>7* Copyright (c) 2007 Novell Inc.8*/910#include <linux/device/driver.h>11#include <linux/device.h>12#include <linux/module.h>13#include <linux/errno.h>14#include <linux/slab.h>15#include <linux/string.h>16#include <linux/sysfs.h>17#include "base.h"1819static struct device *next_device(struct klist_iter *i)20{21struct klist_node *n = klist_next(i);22struct device *dev = NULL;23struct device_private *dev_prv;2425if (n) {26dev_prv = to_device_private_driver(n);27dev = dev_prv->device;28}29return dev;30}3132/**33* driver_set_override() - Helper to set or clear driver override.34* @dev: Device to change35* @override: Address of string to change (e.g. &device->driver_override);36* The contents will be freed and hold newly allocated override.37* @s: NUL-terminated string, new driver name to force a match, pass empty38* string to clear it ("" or "\n", where the latter is only for sysfs39* interface).40* @len: length of @s41*42* Helper to set or clear driver override in a device, intended for the cases43* when the driver_override field is allocated by driver/bus code.44*45* Returns: 0 on success or a negative error code on failure.46*/47int driver_set_override(struct device *dev, const char **override,48const char *s, size_t len)49{50const char *new, *old;51char *cp;5253if (!override || !s)54return -EINVAL;5556/*57* The stored value will be used in sysfs show callback (sysfs_emit()),58* which has a length limit of PAGE_SIZE and adds a trailing newline.59* Thus we can store one character less to avoid truncation during sysfs60* show.61*/62if (len >= (PAGE_SIZE - 1))63return -EINVAL;6465/*66* Compute the real length of the string in case userspace sends us a67* bunch of \0 characters like python likes to do.68*/69len = strlen(s);7071if (!len) {72/* Empty string passed - clear override */73device_lock(dev);74old = *override;75*override = NULL;76device_unlock(dev);77kfree(old);7879return 0;80}8182cp = strnchr(s, len, '\n');83if (cp)84len = cp - s;8586new = kstrndup(s, len, GFP_KERNEL);87if (!new)88return -ENOMEM;8990device_lock(dev);91old = *override;92if (cp != s) {93*override = new;94} else {95/* "\n" passed - clear override */96kfree(new);97*override = NULL;98}99device_unlock(dev);100101kfree(old);102103return 0;104}105EXPORT_SYMBOL_GPL(driver_set_override);106107/**108* driver_for_each_device - Iterator for devices bound to a driver.109* @drv: Driver we're iterating.110* @start: Device to begin with111* @data: Data to pass to the callback.112* @fn: Function to call for each device.113*114* Iterate over the @drv's list of devices calling @fn for each one.115*/116int driver_for_each_device(struct device_driver *drv, struct device *start,117void *data, device_iter_t fn)118{119struct klist_iter i;120struct device *dev;121int error = 0;122123if (!drv)124return -EINVAL;125126klist_iter_init_node(&drv->p->klist_devices, &i,127start ? &start->p->knode_driver : NULL);128while (!error && (dev = next_device(&i)))129error = fn(dev, data);130klist_iter_exit(&i);131return error;132}133EXPORT_SYMBOL_GPL(driver_for_each_device);134135/**136* driver_find_device - device iterator for locating a particular device.137* @drv: The device's driver138* @start: Device to begin with139* @data: Data to pass to match function140* @match: Callback function to check device141*142* This is similar to the driver_for_each_device() function above, but143* it returns a reference to a device that is 'found' for later use, as144* determined by the @match callback.145*146* The callback should return 0 if the device doesn't match and non-zero147* if it does. If the callback returns non-zero, this function will148* return to the caller and not iterate over any more devices.149*/150struct device *driver_find_device(const struct device_driver *drv,151struct device *start, const void *data,152device_match_t match)153{154struct klist_iter i;155struct device *dev;156157if (!drv || !drv->p)158return NULL;159160klist_iter_init_node(&drv->p->klist_devices, &i,161(start ? &start->p->knode_driver : NULL));162while ((dev = next_device(&i))) {163if (match(dev, data)) {164get_device(dev);165break;166}167}168klist_iter_exit(&i);169return dev;170}171EXPORT_SYMBOL_GPL(driver_find_device);172173/**174* driver_create_file - create sysfs file for driver.175* @drv: driver.176* @attr: driver attribute descriptor.177*/178int driver_create_file(const struct device_driver *drv,179const struct driver_attribute *attr)180{181int error;182183if (drv)184error = sysfs_create_file(&drv->p->kobj, &attr->attr);185else186error = -EINVAL;187return error;188}189EXPORT_SYMBOL_GPL(driver_create_file);190191/**192* driver_remove_file - remove sysfs file for driver.193* @drv: driver.194* @attr: driver attribute descriptor.195*/196void driver_remove_file(const struct device_driver *drv,197const struct driver_attribute *attr)198{199if (drv)200sysfs_remove_file(&drv->p->kobj, &attr->attr);201}202EXPORT_SYMBOL_GPL(driver_remove_file);203204int driver_add_groups(const struct device_driver *drv,205const struct attribute_group **groups)206{207return sysfs_create_groups(&drv->p->kobj, groups);208}209210void driver_remove_groups(const struct device_driver *drv,211const struct attribute_group **groups)212{213sysfs_remove_groups(&drv->p->kobj, groups);214}215216/**217* driver_register - register driver with bus218* @drv: driver to register219*220* We pass off most of the work to the bus_add_driver() call,221* since most of the things we have to do deal with the bus222* structures.223*/224int driver_register(struct device_driver *drv)225{226int ret;227struct device_driver *other;228229if (!bus_is_registered(drv->bus)) {230pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",231drv->name, drv->bus->name);232return -EINVAL;233}234235if ((drv->bus->probe && drv->probe) ||236(drv->bus->remove && drv->remove) ||237(drv->bus->shutdown && drv->shutdown))238pr_warn("Driver '%s' needs updating - please use "239"bus_type methods\n", drv->name);240241other = driver_find(drv->name, drv->bus);242if (other) {243pr_err("Error: Driver '%s' is already registered, "244"aborting...\n", drv->name);245return -EBUSY;246}247248ret = bus_add_driver(drv);249if (ret)250return ret;251ret = driver_add_groups(drv, drv->groups);252if (ret) {253bus_remove_driver(drv);254return ret;255}256kobject_uevent(&drv->p->kobj, KOBJ_ADD);257deferred_probe_extend_timeout();258259return ret;260}261EXPORT_SYMBOL_GPL(driver_register);262263/**264* driver_unregister - remove driver from system.265* @drv: driver.266*267* Again, we pass off most of the work to the bus-level call.268*/269void driver_unregister(struct device_driver *drv)270{271if (!drv || !drv->p) {272WARN(1, "Unexpected driver unregister!\n");273return;274}275driver_remove_groups(drv, drv->groups);276bus_remove_driver(drv);277}278EXPORT_SYMBOL_GPL(driver_unregister);279280281