base.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
  4. * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
  5. * Copyright (c) 2008-2012 Novell Inc.
  6. * Copyright (c) 2012-2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  7. * Copyright (c) 2012-2019 Linux Foundation
  8. *
  9. * Core driver model functions and structures that should not be
  10. * shared outside of the drivers/base/ directory.
  11. *
  12. */
  13. #include <linux/notifier.h>
  14. /**
  15. * struct subsys_private - structure to hold the private to the driver core portions of the bus_type/class structure.
  16. *
  17. * @subsys - the struct kset that defines this subsystem
  18. * @devices_kset - the subsystem's 'devices' directory
  19. * @interfaces - list of subsystem interfaces associated
  20. * @mutex - protect the devices, and interfaces lists.
  21. *
  22. * @drivers_kset - the list of drivers associated
  23. * @klist_devices - the klist to iterate over the @devices_kset
  24. * @klist_drivers - the klist to iterate over the @drivers_kset
  25. * @bus_notifier - the bus notifier list for anything that cares about things
  26. * on this bus.
  27. * @bus - pointer back to the struct bus_type that this structure is associated
  28. * with.
  29. * @dev_root: Default device to use as the parent.
  30. *
  31. * @glue_dirs - "glue" directory to put in-between the parent device to
  32. * avoid namespace conflicts
  33. * @class - pointer back to the struct class that this structure is associated
  34. * with.
  35. * @lock_key: Lock class key for use by the lock validator
  36. *
  37. * This structure is the one that is the actual kobject allowing struct
  38. * bus_type/class to be statically allocated safely. Nothing outside of the
  39. * driver core should ever touch these fields.
  40. */
  41. struct subsys_private {
  42. struct kset subsys;
  43. struct kset *devices_kset;
  44. struct list_head interfaces;
  45. struct mutex mutex;
  46. struct kset *drivers_kset;
  47. struct klist klist_devices;
  48. struct klist klist_drivers;
  49. struct blocking_notifier_head bus_notifier;
  50. unsigned int drivers_autoprobe:1;
  51. const struct bus_type *bus;
  52. struct device *dev_root;
  53. struct kset glue_dirs;
  54. const struct class *class;
  55. struct lock_class_key lock_key;
  56. };
  57. #define to_subsys_private(obj) container_of_const(obj, struct subsys_private, subsys.kobj)
  58. static inline struct subsys_private *subsys_get(struct subsys_private *sp)
  59. {
  60. if (sp)
  61. kset_get(&sp->subsys);
  62. return sp;
  63. }
  64. static inline void subsys_put(struct subsys_private *sp)
  65. {
  66. if (sp)
  67. kset_put(&sp->subsys);
  68. }
  69. struct subsys_private *bus_to_subsys(const struct bus_type *bus);
  70. struct subsys_private *class_to_subsys(const struct class *class);
  71. struct driver_private {
  72. struct kobject kobj;
  73. struct klist klist_devices;
  74. struct klist_node knode_bus;
  75. struct module_kobject *mkobj;
  76. struct device_driver *driver;
  77. };
  78. #define to_driver(obj) container_of(obj, struct driver_private, kobj)
  79. /**
  80. * struct device_private - structure to hold the private to the driver core portions of the device structure.
  81. *
  82. * @klist_children - klist containing all children of this device
  83. * @knode_parent - node in sibling list
  84. * @knode_driver - node in driver list
  85. * @knode_bus - node in bus list
  86. * @knode_class - node in class list
  87. * @deferred_probe - entry in deferred_probe_list which is used to retry the
  88. * binding of drivers which were unable to get all the resources needed by
  89. * the device; typically because it depends on another driver getting
  90. * probed first.
  91. * @async_driver - pointer to device driver awaiting probe via async_probe
  92. * @device - pointer back to the struct device that this structure is
  93. * associated with.
  94. * @dead - This device is currently either in the process of or has been
  95. * removed from the system. Any asynchronous events scheduled for this
  96. * device should exit without taking any action.
  97. *
  98. * Nothing outside of the driver core should ever touch these fields.
  99. */
  100. struct device_private {
  101. struct klist klist_children;
  102. struct klist_node knode_parent;
  103. struct klist_node knode_driver;
  104. struct klist_node knode_bus;
  105. struct klist_node knode_class;
  106. struct list_head deferred_probe;
  107. const struct device_driver *async_driver;
  108. char *deferred_probe_reason;
  109. struct device *device;
  110. u8 dead:1;
  111. };
  112. #define to_device_private_parent(obj) \
  113. container_of(obj, struct device_private, knode_parent)
  114. #define to_device_private_driver(obj) \
  115. container_of(obj, struct device_private, knode_driver)
  116. #define to_device_private_bus(obj) \
  117. container_of(obj, struct device_private, knode_bus)
  118. #define to_device_private_class(obj) \
  119. container_of(obj, struct device_private, knode_class)
  120. /* initialisation functions */
  121. int devices_init(void);
  122. int buses_init(void);
  123. int classes_init(void);
  124. int firmware_init(void);
  125. #ifdef CONFIG_SYS_HYPERVISOR
  126. int hypervisor_init(void);
  127. #else
  128. static inline int hypervisor_init(void) { return 0; }
  129. #endif
  130. int platform_bus_init(void);
  131. void cpu_dev_init(void);
  132. void container_dev_init(void);
  133. #ifdef CONFIG_AUXILIARY_BUS
  134. void auxiliary_bus_init(void);
  135. #else
  136. static inline void auxiliary_bus_init(void) { }
  137. #endif
  138. struct kobject *virtual_device_parent(void);
  139. int bus_add_device(struct device *dev);
  140. void bus_probe_device(struct device *dev);
  141. void bus_remove_device(struct device *dev);
  142. void bus_notify(struct device *dev, enum bus_notifier_event value);
  143. bool bus_is_registered(const struct bus_type *bus);
  144. int bus_add_driver(struct device_driver *drv);
  145. void bus_remove_driver(struct device_driver *drv);
  146. void device_release_driver_internal(struct device *dev, const struct device_driver *drv,
  147. struct device *parent);
  148. void driver_detach(const struct device_driver *drv);
  149. void driver_deferred_probe_del(struct device *dev);
  150. void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf);
  151. static inline int driver_match_device(const struct device_driver *drv,
  152. struct device *dev)
  153. {
  154. return drv->bus->match ? drv->bus->match(dev, drv) : 1;
  155. }
  156. static inline void dev_sync_state(struct device *dev)
  157. {
  158. if (dev->bus->sync_state)
  159. dev->bus->sync_state(dev);
  160. else if (dev->driver && dev->driver->sync_state)
  161. dev->driver->sync_state(dev);
  162. }
  163. int driver_add_groups(const struct device_driver *drv, const struct attribute_group **groups);
  164. void driver_remove_groups(const struct device_driver *drv, const struct attribute_group **groups);
  165. void device_driver_detach(struct device *dev);
  166. static inline void device_set_driver(struct device *dev, const struct device_driver *drv)
  167. {
  168. /*
  169. * Majority (all?) read accesses to dev->driver happens either
  170. * while holding device lock or in bus/driver code that is only
  171. * invoked when the device is bound to a driver and there is no
  172. * concern of the pointer being changed while it is being read.
  173. * However when reading device's uevent file we read driver pointer
  174. * without taking device lock (so we do not block there for
  175. * arbitrary amount of time). We use WRITE_ONCE() here to prevent
  176. * tearing so that READ_ONCE() can safely be used in uevent code.
  177. */
  178. // FIXME - this cast should not be needed "soon"
  179. WRITE_ONCE(dev->driver, (struct device_driver *)drv);
  180. }
  181. int devres_release_all(struct device *dev);
  182. void device_block_probing(void);
  183. void device_unblock_probing(void);
  184. void deferred_probe_extend_timeout(void);
  185. void driver_deferred_probe_trigger(void);
  186. const char *device_get_devnode(const struct device *dev, umode_t *mode,
  187. kuid_t *uid, kgid_t *gid, const char **tmp);
  188. /* /sys/devices directory */
  189. extern struct kset *devices_kset;
  190. void devices_kset_move_last(struct device *dev);
  191. #if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
  192. int module_add_driver(struct module *mod, const struct device_driver *drv);
  193. void module_remove_driver(const struct device_driver *drv);
  194. #else
  195. static inline int module_add_driver(struct module *mod,
  196. struct device_driver *drv)
  197. {
  198. return 0;
  199. }
  200. static inline void module_remove_driver(struct device_driver *drv) { }
  201. #endif
  202. #ifdef CONFIG_DEVTMPFS
  203. int devtmpfs_init(void);
  204. #else
  205. static inline int devtmpfs_init(void) { return 0; }
  206. #endif
  207. #ifdef CONFIG_BLOCK
  208. extern const struct class block_class;
  209. static inline bool is_blockdev(struct device *dev)
  210. {
  211. return dev->class == &block_class;
  212. }
  213. #else
  214. static inline bool is_blockdev(struct device *dev) { return false; }
  215. #endif
  216. /* Device links support */
  217. int device_links_read_lock(void);
  218. void device_links_read_unlock(int idx);
  219. int device_links_read_lock_held(void);
  220. int device_links_check_suppliers(struct device *dev);
  221. void device_links_force_bind(struct device *dev);
  222. void device_links_driver_bound(struct device *dev);
  223. void device_links_driver_cleanup(struct device *dev);
  224. void device_links_no_driver(struct device *dev);
  225. bool device_links_busy(struct device *dev);
  226. void device_links_unbind_consumers(struct device *dev);
  227. void fw_devlink_drivers_done(void);
  228. void fw_devlink_probing_done(void);
  229. /* device pm support */
  230. void device_pm_move_to_tail(struct device *dev);
  231. #ifdef CONFIG_DEVTMPFS
  232. int devtmpfs_create_node(struct device *dev);
  233. int devtmpfs_delete_node(struct device *dev);
  234. #else
  235. static inline int devtmpfs_create_node(struct device *dev) { return 0; }
  236. static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
  237. #endif
  238. void software_node_notify(struct device *dev);
  239. void software_node_notify_remove(struct device *dev);