driver.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #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"
  18. static struct device *next_device(struct klist_iter *i)
  19. {
  20. struct klist_node *n = klist_next(i);
  21. struct device *dev = NULL;
  22. struct device_private *dev_prv;
  23. if (n) {
  24. dev_prv = to_device_private_driver(n);
  25. dev = dev_prv->device;
  26. }
  27. return dev;
  28. }
  29. /**
  30. * driver_set_override() - Helper to set or clear driver override.
  31. * @dev: Device to change
  32. * @override: Address of string to change (e.g. &device->driver_override);
  33. * The contents will be freed and hold newly allocated override.
  34. * @s: NUL-terminated string, new driver name to force a match, pass empty
  35. * string to clear it ("" or "\n", where the latter is only for sysfs
  36. * interface).
  37. * @len: length of @s
  38. *
  39. * Helper to set or clear driver override in a device, intended for the cases
  40. * when the driver_override field is allocated by driver/bus code.
  41. *
  42. * Returns: 0 on success or a negative error code on failure.
  43. */
  44. int driver_set_override(struct device *dev, const char **override,
  45. const char *s, size_t len)
  46. {
  47. const char *new, *old;
  48. char *cp;
  49. if (!override || !s)
  50. return -EINVAL;
  51. /*
  52. * The stored value will be used in sysfs show callback (sysfs_emit()),
  53. * which has a length limit of PAGE_SIZE and adds a trailing newline.
  54. * Thus we can store one character less to avoid truncation during sysfs
  55. * show.
  56. */
  57. if (len >= (PAGE_SIZE - 1))
  58. return -EINVAL;
  59. /*
  60. * Compute the real length of the string in case userspace sends us a
  61. * bunch of \0 characters like python likes to do.
  62. */
  63. len = strlen(s);
  64. if (!len) {
  65. /* Empty string passed - clear override */
  66. device_lock(dev);
  67. old = *override;
  68. *override = NULL;
  69. device_unlock(dev);
  70. kfree(old);
  71. return 0;
  72. }
  73. cp = strnchr(s, len, '\n');
  74. if (cp)
  75. len = cp - s;
  76. new = kstrndup(s, len, GFP_KERNEL);
  77. if (!new)
  78. return -ENOMEM;
  79. device_lock(dev);
  80. old = *override;
  81. if (cp != s) {
  82. *override = new;
  83. } else {
  84. /* "\n" passed - clear override */
  85. kfree(new);
  86. *override = NULL;
  87. }
  88. device_unlock(dev);
  89. kfree(old);
  90. return 0;
  91. }
  92. EXPORT_SYMBOL_GPL(driver_set_override);
  93. /**
  94. * driver_for_each_device - Iterator for devices bound to a driver.
  95. * @drv: Driver we're iterating.
  96. * @start: Device to begin with
  97. * @data: Data to pass to the callback.
  98. * @fn: Function to call for each device.
  99. *
  100. * Iterate over the @drv's list of devices calling @fn for each one.
  101. */
  102. int driver_for_each_device(struct device_driver *drv, struct device *start,
  103. void *data, int (*fn)(struct device *, void *))
  104. {
  105. struct klist_iter i;
  106. struct device *dev;
  107. int error = 0;
  108. if (!drv)
  109. return -EINVAL;
  110. klist_iter_init_node(&drv->p->klist_devices, &i,
  111. start ? &start->p->knode_driver : NULL);
  112. while (!error && (dev = next_device(&i)))
  113. error = fn(dev, data);
  114. klist_iter_exit(&i);
  115. return error;
  116. }
  117. EXPORT_SYMBOL_GPL(driver_for_each_device);
  118. /**
  119. * driver_find_device - device iterator for locating a particular device.
  120. * @drv: The device's driver
  121. * @start: Device to begin with
  122. * @data: Data to pass to match function
  123. * @match: Callback function to check device
  124. *
  125. * This is similar to the driver_for_each_device() function above, but
  126. * it returns a reference to a device that is 'found' for later use, as
  127. * determined by the @match callback.
  128. *
  129. * The callback should return 0 if the device doesn't match and non-zero
  130. * if it does. If the callback returns non-zero, this function will
  131. * return to the caller and not iterate over any more devices.
  132. */
  133. struct device *driver_find_device(struct device_driver *drv,
  134. struct device *start, const void *data,
  135. int (*match)(struct device *dev, const void *data))
  136. {
  137. struct klist_iter i;
  138. struct device *dev;
  139. if (!drv || !drv->p)
  140. return NULL;
  141. klist_iter_init_node(&drv->p->klist_devices, &i,
  142. (start ? &start->p->knode_driver : NULL));
  143. while ((dev = next_device(&i)))
  144. if (match(dev, data) && get_device(dev))
  145. break;
  146. klist_iter_exit(&i);
  147. return dev;
  148. }
  149. EXPORT_SYMBOL_GPL(driver_find_device);
  150. /**
  151. * driver_create_file - create sysfs file for driver.
  152. * @drv: driver.
  153. * @attr: driver attribute descriptor.
  154. */
  155. int driver_create_file(struct device_driver *drv,
  156. const struct driver_attribute *attr)
  157. {
  158. int error;
  159. if (drv)
  160. error = sysfs_create_file(&drv->p->kobj, &attr->attr);
  161. else
  162. error = -EINVAL;
  163. return error;
  164. }
  165. EXPORT_SYMBOL_GPL(driver_create_file);
  166. /**
  167. * driver_remove_file - remove sysfs file for driver.
  168. * @drv: driver.
  169. * @attr: driver attribute descriptor.
  170. */
  171. void driver_remove_file(struct device_driver *drv,
  172. const struct driver_attribute *attr)
  173. {
  174. if (drv)
  175. sysfs_remove_file(&drv->p->kobj, &attr->attr);
  176. }
  177. EXPORT_SYMBOL_GPL(driver_remove_file);
  178. int driver_add_groups(struct device_driver *drv,
  179. const struct attribute_group **groups)
  180. {
  181. return sysfs_create_groups(&drv->p->kobj, groups);
  182. }
  183. void driver_remove_groups(struct device_driver *drv,
  184. const struct attribute_group **groups)
  185. {
  186. sysfs_remove_groups(&drv->p->kobj, groups);
  187. }
  188. /**
  189. * driver_register - register driver with bus
  190. * @drv: driver to register
  191. *
  192. * We pass off most of the work to the bus_add_driver() call,
  193. * since most of the things we have to do deal with the bus
  194. * structures.
  195. */
  196. int driver_register(struct device_driver *drv)
  197. {
  198. int ret;
  199. struct device_driver *other;
  200. if (!drv->bus->p) {
  201. pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
  202. drv->name, drv->bus->name);
  203. return -EINVAL;
  204. }
  205. if ((drv->bus->probe && drv->probe) ||
  206. (drv->bus->remove && drv->remove) ||
  207. (drv->bus->shutdown && drv->shutdown))
  208. pr_warn("Driver '%s' needs updating - please use "
  209. "bus_type methods\n", drv->name);
  210. other = driver_find(drv->name, drv->bus);
  211. if (other) {
  212. pr_err("Error: Driver '%s' is already registered, "
  213. "aborting...\n", drv->name);
  214. return -EBUSY;
  215. }
  216. ret = bus_add_driver(drv);
  217. if (ret)
  218. return ret;
  219. ret = driver_add_groups(drv, drv->groups);
  220. if (ret) {
  221. bus_remove_driver(drv);
  222. return ret;
  223. }
  224. kobject_uevent(&drv->p->kobj, KOBJ_ADD);
  225. deferred_probe_extend_timeout();
  226. return ret;
  227. }
  228. EXPORT_SYMBOL_GPL(driver_register);
  229. /**
  230. * driver_unregister - remove driver from system.
  231. * @drv: driver.
  232. *
  233. * Again, we pass off most of the work to the bus-level call.
  234. */
  235. void driver_unregister(struct device_driver *drv)
  236. {
  237. if (!drv || !drv->p) {
  238. WARN(1, "Unexpected driver unregister!\n");
  239. return;
  240. }
  241. driver_remove_groups(drv, drv->groups);
  242. bus_remove_driver(drv);
  243. }
  244. EXPORT_SYMBOL_GPL(driver_unregister);
  245. /**
  246. * driver_find - locate driver on a bus by its name.
  247. * @name: name of the driver.
  248. * @bus: bus to scan for the driver.
  249. *
  250. * Call kset_find_obj() to iterate over list of drivers on
  251. * a bus to find driver by name. Return driver if found.
  252. *
  253. * This routine provides no locking to prevent the driver it returns
  254. * from being unregistered or unloaded while the caller is using it.
  255. * The caller is responsible for preventing this.
  256. */
  257. struct device_driver *driver_find(const char *name, struct bus_type *bus)
  258. {
  259. struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
  260. struct driver_private *priv;
  261. if (k) {
  262. /* Drop reference added by kset_find_obj() */
  263. kobject_put(k);
  264. priv = to_driver(k);
  265. return priv->driver;
  266. }
  267. return NULL;
  268. }
  269. EXPORT_SYMBOL_GPL(driver_find);