projects/linux·blob master · refs

204 lines · 5.1 KiB

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