0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/device/driver.h>
0012 #include <linux/device.h>
0013 #include <linux/module.h>
0014 #include <linux/errno.h>
0015 #include <linux/slab.h>
0016 #include <linux/string.h>
0017 #include <linux/sysfs.h>
0018 #include "base.h"
0019
0020 static struct device *next_device(struct klist_iter *i)
0021 {
0022 struct klist_node *n = klist_next(i);
0023 struct device *dev = NULL;
0024 struct device_private *dev_prv;
0025
0026 if (n) {
0027 dev_prv = to_device_private_driver(n);
0028 dev = dev_prv->device;
0029 }
0030 return dev;
0031 }
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 int driver_set_override(struct device *dev, const char **override,
0049 const char *s, size_t len)
0050 {
0051 const char *new, *old;
0052 char *cp;
0053
0054 if (!override || !s)
0055 return -EINVAL;
0056
0057
0058
0059
0060
0061
0062
0063 if (len >= (PAGE_SIZE - 1))
0064 return -EINVAL;
0065
0066
0067
0068
0069
0070 len = strlen(s);
0071
0072 if (!len) {
0073
0074 device_lock(dev);
0075 old = *override;
0076 *override = NULL;
0077 device_unlock(dev);
0078 kfree(old);
0079
0080 return 0;
0081 }
0082
0083 cp = strnchr(s, len, '\n');
0084 if (cp)
0085 len = cp - s;
0086
0087 new = kstrndup(s, len, GFP_KERNEL);
0088 if (!new)
0089 return -ENOMEM;
0090
0091 device_lock(dev);
0092 old = *override;
0093 if (cp != s) {
0094 *override = new;
0095 } else {
0096
0097 kfree(new);
0098 *override = NULL;
0099 }
0100 device_unlock(dev);
0101
0102 kfree(old);
0103
0104 return 0;
0105 }
0106 EXPORT_SYMBOL_GPL(driver_set_override);
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117 int driver_for_each_device(struct device_driver *drv, struct device *start,
0118 void *data, int (*fn)(struct device *, void *))
0119 {
0120 struct klist_iter i;
0121 struct device *dev;
0122 int error = 0;
0123
0124 if (!drv)
0125 return -EINVAL;
0126
0127 klist_iter_init_node(&drv->p->klist_devices, &i,
0128 start ? &start->p->knode_driver : NULL);
0129 while (!error && (dev = next_device(&i)))
0130 error = fn(dev, data);
0131 klist_iter_exit(&i);
0132 return error;
0133 }
0134 EXPORT_SYMBOL_GPL(driver_for_each_device);
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151 struct device *driver_find_device(struct device_driver *drv,
0152 struct device *start, const void *data,
0153 int (*match)(struct device *dev, const void *data))
0154 {
0155 struct klist_iter i;
0156 struct device *dev;
0157
0158 if (!drv || !drv->p)
0159 return NULL;
0160
0161 klist_iter_init_node(&drv->p->klist_devices, &i,
0162 (start ? &start->p->knode_driver : NULL));
0163 while ((dev = next_device(&i)))
0164 if (match(dev, data) && get_device(dev))
0165 break;
0166 klist_iter_exit(&i);
0167 return dev;
0168 }
0169 EXPORT_SYMBOL_GPL(driver_find_device);
0170
0171
0172
0173
0174
0175
0176 int driver_create_file(struct device_driver *drv,
0177 const struct driver_attribute *attr)
0178 {
0179 int error;
0180
0181 if (drv)
0182 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
0183 else
0184 error = -EINVAL;
0185 return error;
0186 }
0187 EXPORT_SYMBOL_GPL(driver_create_file);
0188
0189
0190
0191
0192
0193
0194 void driver_remove_file(struct device_driver *drv,
0195 const struct driver_attribute *attr)
0196 {
0197 if (drv)
0198 sysfs_remove_file(&drv->p->kobj, &attr->attr);
0199 }
0200 EXPORT_SYMBOL_GPL(driver_remove_file);
0201
0202 int driver_add_groups(struct device_driver *drv,
0203 const struct attribute_group **groups)
0204 {
0205 return sysfs_create_groups(&drv->p->kobj, groups);
0206 }
0207
0208 void driver_remove_groups(struct device_driver *drv,
0209 const struct attribute_group **groups)
0210 {
0211 sysfs_remove_groups(&drv->p->kobj, groups);
0212 }
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222 int driver_register(struct device_driver *drv)
0223 {
0224 int ret;
0225 struct device_driver *other;
0226
0227 if (!drv->bus->p) {
0228 pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
0229 drv->name, drv->bus->name);
0230 return -EINVAL;
0231 }
0232
0233 if ((drv->bus->probe && drv->probe) ||
0234 (drv->bus->remove && drv->remove) ||
0235 (drv->bus->shutdown && drv->shutdown))
0236 pr_warn("Driver '%s' needs updating - please use "
0237 "bus_type methods\n", drv->name);
0238
0239 other = driver_find(drv->name, drv->bus);
0240 if (other) {
0241 pr_err("Error: Driver '%s' is already registered, "
0242 "aborting...\n", drv->name);
0243 return -EBUSY;
0244 }
0245
0246 ret = bus_add_driver(drv);
0247 if (ret)
0248 return ret;
0249 ret = driver_add_groups(drv, drv->groups);
0250 if (ret) {
0251 bus_remove_driver(drv);
0252 return ret;
0253 }
0254 kobject_uevent(&drv->p->kobj, KOBJ_ADD);
0255 deferred_probe_extend_timeout();
0256
0257 return ret;
0258 }
0259 EXPORT_SYMBOL_GPL(driver_register);
0260
0261
0262
0263
0264
0265
0266
0267 void driver_unregister(struct device_driver *drv)
0268 {
0269 if (!drv || !drv->p) {
0270 WARN(1, "Unexpected driver unregister!\n");
0271 return;
0272 }
0273 driver_remove_groups(drv, drv->groups);
0274 bus_remove_driver(drv);
0275 }
0276 EXPORT_SYMBOL_GPL(driver_unregister);
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290 struct device_driver *driver_find(const char *name, struct bus_type *bus)
0291 {
0292 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
0293 struct driver_private *priv;
0294
0295 if (k) {
0296
0297 kobject_put(k);
0298 priv = to_driver(k);
0299 return priv->driver;
0300 }
0301 return NULL;
0302 }
0303 EXPORT_SYMBOL_GPL(driver_find);