driver.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <gregkh@suse.de>
  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, device_iter_t fn)
  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(const struct device_driver *drv,
  134. struct device *start, const void *data,
  135. device_match_t match)
  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)) {
  145. get_device(dev);
  146. break;
  147. }
  148. }
  149. klist_iter_exit(&i);
  150. return dev;
  151. }
  152. EXPORT_SYMBOL_GPL(driver_find_device);
  153. /**
  154. * driver_create_file - create sysfs file for driver.
  155. * @drv: driver.
  156. * @attr: driver attribute descriptor.
  157. */
  158. int driver_create_file(const struct device_driver *drv,
  159. const struct driver_attribute *attr)
  160. {
  161. int error;
  162. if (drv)
  163. error = sysfs_create_file(&drv->p->kobj, &attr->attr);
  164. else
  165. error = -EINVAL;
  166. return error;
  167. }
  168. EXPORT_SYMBOL_GPL(driver_create_file);
  169. /**
  170. * driver_remove_file - remove sysfs file for driver.
  171. * @drv: driver.
  172. * @attr: driver attribute descriptor.
  173. */
  174. void driver_remove_file(const struct device_driver *drv,
  175. const struct driver_attribute *attr)
  176. {
  177. if (drv)
  178. sysfs_remove_file(&drv->p->kobj, &attr->attr);
  179. }
  180. EXPORT_SYMBOL_GPL(driver_remove_file);
  181. int driver_add_groups(const struct device_driver *drv,
  182. const struct attribute_group **groups)
  183. {
  184. return sysfs_create_groups(&drv->p->kobj, groups);
  185. }
  186. void driver_remove_groups(const struct device_driver *drv,
  187. const struct attribute_group **groups)
  188. {
  189. sysfs_remove_groups(&drv->p->kobj, groups);
  190. }
  191. /**
  192. * driver_register - register driver with bus
  193. * @drv: driver to register
  194. *
  195. * We pass off most of the work to the bus_add_driver() call,
  196. * since most of the things we have to do deal with the bus
  197. * structures.
  198. */
  199. int driver_register(struct device_driver *drv)
  200. {
  201. int ret;
  202. struct device_driver *other;
  203. if (!bus_is_registered(drv->bus)) {
  204. pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
  205. drv->name, drv->bus->name);
  206. return -EINVAL;
  207. }
  208. if ((drv->bus->probe && drv->probe) ||
  209. (drv->bus->remove && drv->remove) ||
  210. (drv->bus->shutdown && drv->shutdown))
  211. pr_warn("Driver '%s' needs updating - please use "
  212. "bus_type methods\n", drv->name);
  213. other = driver_find(drv->name, drv->bus);
  214. if (other) {
  215. pr_err("Error: Driver '%s' is already registered, "
  216. "aborting...\n", drv->name);
  217. return -EBUSY;
  218. }
  219. ret = bus_add_driver(drv);
  220. if (ret)
  221. return ret;
  222. ret = driver_add_groups(drv, drv->groups);
  223. if (ret) {
  224. bus_remove_driver(drv);
  225. return ret;
  226. }
  227. kobject_uevent(&drv->p->kobj, KOBJ_ADD);
  228. deferred_probe_extend_timeout();
  229. return ret;
  230. }
  231. EXPORT_SYMBOL_GPL(driver_register);
  232. /**
  233. * driver_unregister - remove driver from system.
  234. * @drv: driver.
  235. *
  236. * Again, we pass off most of the work to the bus-level call.
  237. */
  238. void driver_unregister(struct device_driver *drv)
  239. {
  240. if (!drv || !drv->p) {
  241. WARN(1, "Unexpected driver unregister!\n");
  242. return;
  243. }
  244. driver_remove_groups(drv, drv->groups);
  245. bus_remove_driver(drv);
  246. }
  247. EXPORT_SYMBOL_GPL(driver_unregister);