base.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #ifdef CONFIG_RUST
  80. /**
  81. * struct driver_type - Representation of a Rust driver type.
  82. */
  83. struct driver_type {
  84. /**
  85. * @id: Representation of core::any::TypeId.
  86. */
  87. u8 id[16];
  88. } __packed;
  89. #endif
  90. /**
  91. * struct device_private - structure to hold the private to the driver core portions of the device structure.
  92. *
  93. * @klist_children - klist containing all children of this device
  94. * @knode_parent - node in sibling list
  95. * @knode_driver - node in driver list
  96. * @knode_bus - node in bus list
  97. * @knode_class - node in class list
  98. * @deferred_probe - entry in deferred_probe_list which is used to retry the
  99. * binding of drivers which were unable to get all the resources needed by
  100. * the device; typically because it depends on another driver getting
  101. * probed first.
  102. * @async_driver - pointer to device driver awaiting probe via async_probe
  103. * @device - pointer back to the struct device that this structure is
  104. * associated with.
  105. * @driver_type - The type of the bound Rust driver.
  106. * @dead - This device is currently either in the process of or has been
  107. * removed from the system. Any asynchronous events scheduled for this
  108. * device should exit without taking any action.
  109. *
  110. * Nothing outside of the driver core should ever touch these fields.
  111. */
  112. struct device_private {
  113. struct klist klist_children;
  114. struct klist_node knode_parent;
  115. struct klist_node knode_driver;
  116. struct klist_node knode_bus;
  117. struct klist_node knode_class;
  118. struct list_head deferred_probe;
  119. const struct device_driver *async_driver;
  120. char *deferred_probe_reason;
  121. struct device *device;
  122. #ifdef CONFIG_RUST
  123. struct driver_type driver_type;
  124. #endif
  125. u8 dead:1;
  126. };
  127. #define to_device_private_parent(obj) \
  128. container_of(obj, struct device_private, knode_parent)
  129. #define to_device_private_driver(obj) \
  130. container_of(obj, struct device_private, knode_driver)
  131. #define to_device_private_bus(obj) \
  132. container_of(obj, struct device_private, knode_bus)
  133. #define to_device_private_class(obj) \
  134. container_of(obj, struct device_private, knode_class)
  135. /* initialisation functions */
  136. int devices_init(void);
  137. int buses_init(void);
  138. int classes_init(void);
  139. int firmware_init(void);
  140. #ifdef CONFIG_SYS_HYPERVISOR
  141. int hypervisor_init(void);
  142. #else
  143. static inline int hypervisor_init(void) { return 0; }
  144. #endif
  145. int platform_bus_init(void);
  146. int faux_bus_init(void);
  147. void cpu_dev_init(void);
  148. void container_dev_init(void);
  149. #ifdef CONFIG_AUXILIARY_BUS
  150. void auxiliary_bus_init(void);
  151. #else
  152. static inline void auxiliary_bus_init(void) { }
  153. #endif
  154. struct kobject *virtual_device_parent(void);
  155. int bus_add_device(struct device *dev);
  156. void bus_probe_device(struct device *dev);
  157. void bus_remove_device(struct device *dev);
  158. void bus_notify(struct device *dev, enum bus_notifier_event value);
  159. bool bus_is_registered(const struct bus_type *bus);
  160. int bus_add_driver(struct device_driver *drv);
  161. void bus_remove_driver(struct device_driver *drv);
  162. void device_release_driver_internal(struct device *dev, const struct device_driver *drv,
  163. struct device *parent);
  164. void driver_detach(const struct device_driver *drv);
  165. void driver_deferred_probe_del(struct device *dev);
  166. void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf);
  167. static inline int driver_match_device(const struct device_driver *drv,
  168. struct device *dev)
  169. {
  170. return drv->bus->match ? drv->bus->match(dev, drv) : 1;
  171. }
  172. static inline void dev_sync_state(struct device *dev)
  173. {
  174. if (dev->bus->sync_state)
  175. dev->bus->sync_state(dev);
  176. else if (dev->driver && dev->driver->sync_state)
  177. dev->driver->sync_state(dev);
  178. }
  179. int driver_add_groups(const struct device_driver *drv, const struct attribute_group **groups);
  180. void driver_remove_groups(const struct device_driver *drv, const struct attribute_group **groups);
  181. void device_driver_detach(struct device *dev);
  182. static inline void device_set_driver(struct device *dev, const struct device_driver *drv)
  183. {
  184. /*
  185. * Majority (all?) read accesses to dev->driver happens either
  186. * while holding device lock or in bus/driver code that is only
  187. * invoked when the device is bound to a driver and there is no
  188. * concern of the pointer being changed while it is being read.
  189. * However when reading device's uevent file we read driver pointer
  190. * without taking device lock (so we do not block there for
  191. * arbitrary amount of time). We use WRITE_ONCE() here to prevent
  192. * tearing so that READ_ONCE() can safely be used in uevent code.
  193. */
  194. // FIXME - this cast should not be needed "soon"
  195. WRITE_ONCE(dev->driver, (struct device_driver *)drv);
  196. }
  197. void devres_for_each_res(struct device *dev, dr_release_t release,
  198. dr_match_t match, void *match_data,
  199. void (*fn)(struct device *, void *, void *),
  200. void *data);
  201. int devres_release_all(struct device *dev);
  202. void device_block_probing(void);
  203. void device_unblock_probing(void);
  204. void deferred_probe_extend_timeout(void);
  205. void driver_deferred_probe_trigger(void);
  206. const char *device_get_devnode(const struct device *dev, umode_t *mode,
  207. kuid_t *uid, kgid_t *gid, const char **tmp);
  208. /* /sys/devices directory */
  209. extern struct kset *devices_kset;
  210. void devices_kset_move_last(struct device *dev);
  211. #if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
  212. int module_add_driver(struct module *mod, const struct device_driver *drv);
  213. void module_remove_driver(const struct device_driver *drv);
  214. #else
  215. static inline int module_add_driver(struct module *mod,
  216. struct device_driver *drv)
  217. {
  218. return 0;
  219. }
  220. static inline void module_remove_driver(struct device_driver *drv) { }
  221. #endif
  222. #ifdef CONFIG_DEVTMPFS
  223. int devtmpfs_init(void);
  224. #else
  225. static inline int devtmpfs_init(void) { return 0; }
  226. #endif
  227. #ifdef CONFIG_BLOCK
  228. extern const struct class block_class;
  229. static inline bool is_blockdev(struct device *dev)
  230. {
  231. return dev->class == &block_class;
  232. }
  233. #else
  234. static inline bool is_blockdev(struct device *dev) { return false; }
  235. #endif
  236. /* Device links support */
  237. int device_links_read_lock(void);
  238. void device_links_read_unlock(int idx);
  239. int device_links_read_lock_held(void);
  240. int device_links_check_suppliers(struct device *dev);
  241. void device_links_force_bind(struct device *dev);
  242. void device_links_driver_bound(struct device *dev);
  243. void device_links_driver_cleanup(struct device *dev);
  244. void device_links_no_driver(struct device *dev);
  245. bool device_links_busy(struct device *dev);
  246. void device_links_unbind_consumers(struct device *dev);
  247. bool device_link_flag_is_sync_state_only(u32 flags);
  248. void fw_devlink_drivers_done(void);
  249. void fw_devlink_probing_done(void);
  250. #define dev_for_each_link_to_supplier(__link, __dev) \
  251. list_for_each_entry_srcu(__link, &(__dev)->links.suppliers, c_node, \
  252. device_links_read_lock_held())
  253. #define dev_for_each_link_to_consumer(__link, __dev) \
  254. list_for_each_entry_srcu(__link, &(__dev)->links.consumers, s_node, \
  255. device_links_read_lock_held())
  256. /* device pm support */
  257. void device_pm_move_to_tail(struct device *dev);
  258. #ifdef CONFIG_DEVTMPFS
  259. int devtmpfs_create_node(struct device *dev);
  260. int devtmpfs_delete_node(struct device *dev);
  261. #else
  262. static inline int devtmpfs_create_node(struct device *dev) { return 0; }
  263. static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
  264. #endif
  265. void software_node_notify(struct device *dev);
  266. void software_node_notify_remove(struct device *dev);
  267. #ifdef CONFIG_PINCTRL
  268. int pinctrl_bind_pins(struct device *dev);
  269. #else
  270. static inline int pinctrl_bind_pins(struct device *dev)
  271. {
  272. return 0;
  273. }
  274. #endif /* CONFIG_PINCTRL */