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 /** 16 * struct subsys_private - structure to hold the private to the driver core 17 * portions of the bus_type/class structure. 18 * @subsys: the struct kset that defines this subsystem 19 * @devices_kset: the subsystem's 'devices' directory 20 * @interfaces: list of subsystem interfaces associated 21 * @mutex: protect the devices, and interfaces lists. 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 * @drivers_autoprobe: gate whether new devices are automatically attached to 28 * registered drivers, or new drivers automatically attach 29 * to existing devices. 30 * @no_drivers: gate whether drivers can be registered. 31 * @bus: pointer back to the struct bus_type that this structure is associated 32 * with. 33 * @dev_root: Default device to use as the parent. 34 * @glue_dirs: "glue" directory to put in-between the parent device to 35 * avoid namespace conflicts 36 * @class: pointer back to the struct class that this structure is associated 37 * with. 38 * @lock_key: Lock class key for use by the lock validator 39 * 40 * This structure is the one that is the actual kobject allowing struct 41 * bus_type/class to be statically allocated safely. Nothing outside of the 42 * driver core should ever touch these fields. 43 */ 44 struct subsys_private { 45 struct kset subsys; 46 struct kset *devices_kset; 47 struct list_head interfaces; 48 struct mutex mutex; 49 50 struct kset *drivers_kset; 51 struct klist klist_devices; 52 struct klist klist_drivers; 53 struct blocking_notifier_head bus_notifier; 54 unsigned int drivers_autoprobe:1; 55 unsigned int no_drivers:1; 56 const struct bus_type *bus; 57 struct device *dev_root; 58 59 struct kset glue_dirs; 60 const struct class *class; 61 62 struct lock_class_key lock_key; 63 }; 64 #define to_subsys_private(obj) container_of_const(obj, struct subsys_private, subsys.kobj) 65 66 static inline struct subsys_private *subsys_get(struct subsys_private *sp) 67 { 68 if (sp) 69 kset_get(&sp->subsys); 70 return sp; 71 } 72 73 static inline void subsys_put(struct subsys_private *sp) 74 { 75 if (sp) 76 kset_put(&sp->subsys); 77 } 78 79 struct subsys_private *bus_to_subsys(const struct bus_type *bus); 80 struct subsys_private *class_to_subsys(const struct class *class); 81 82 struct driver_private { 83 struct kobject kobj; 84 struct klist klist_devices; 85 struct klist_node knode_bus; 86 struct module_kobject *mkobj; 87 struct device_driver *driver; 88 }; 89 #define to_driver(obj) container_of(obj, struct driver_private, kobj) 90 91 /** 92 * struct device_private - structure to hold the private to the driver core 93 * portions of the device structure. 94 * @klist_children: klist containing all children of this device 95 * @knode_parent: node in sibling list 96 * @knode_driver: node in driver list 97 * @knode_bus: node in bus list 98 * @knode_class: node in class list 99 * @deferred_probe: entry in deferred_probe_list which is used to retry the 100 * binding of drivers which were unable to get all the 101 * resources needed by the device; typically because it depends 102 * on another driver getting probed first. 103 * @async_driver: pointer to device driver awaiting probe via async_probe 104 * @deferred_probe_reason: capture the -EPROBE_DEFER message emitted with 105 * dev_err_probe() for later retrieval via debugfs 106 * @device: pointer back to the struct device that this structure is 107 * associated with. 108 * @dead: This device is currently either in the process of or has been 109 * removed from the system. Any asynchronous events scheduled for this 110 * device should exit without taking any action. 111 * 112 * Nothing outside of the driver core should ever touch these fields. 113 */ 114 struct device_private { 115 struct klist klist_children; 116 struct klist_node knode_parent; 117 struct klist_node knode_driver; 118 struct klist_node knode_bus; 119 struct klist_node knode_class; 120 struct list_head deferred_probe; 121 const struct device_driver *async_driver; 122 char *deferred_probe_reason; 123 struct device *device; 124 u8 dead:1; 125 }; 126 #define to_device_private_parent(obj) \ 127 container_of(obj, struct device_private, knode_parent) 128 #define to_device_private_driver(obj) \ 129 container_of(obj, struct device_private, knode_driver) 130 #define to_device_private_bus(obj) \ 131 container_of(obj, struct device_private, knode_bus) 132 #define to_device_private_class(obj) \ 133 container_of(obj, struct device_private, knode_class) 134 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 155 struct kobject *virtual_device_parent(void); 156 157 int bus_add_device(struct device *dev); 158 void bus_probe_device(struct device *dev); 159 void bus_probe_device_deferred(struct device *dev); 160 void bus_remove_device(struct device *dev); 161 void bus_notify(struct device *dev, enum bus_notifier_event value); 162 bool bus_is_registered(const struct bus_type *bus); 163 164 int bus_add_driver(struct device_driver *drv); 165 void bus_remove_driver(struct device_driver *drv); 166 void device_release_driver_internal(struct device *dev, const struct device_driver *drv, 167 struct device *parent); 168 169 void driver_detach(const struct device_driver *drv); 170 void driver_deferred_probe_del(struct device *dev); 171 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf); 172 static inline int driver_match_device(const struct device_driver *drv, 173 struct device *dev) 174 { 175 return drv->bus->match ? drv->bus->match(dev, drv) : 1; 176 } 177 178 static inline bool dev_has_sync_state(struct device *dev) 179 { 180 struct device_driver *drv; 181 182 if (!dev) 183 return false; 184 drv = READ_ONCE(dev->driver); 185 if (drv && drv->sync_state) 186 return true; 187 if (dev->bus && dev->bus->sync_state) 188 return true; 189 return false; 190 } 191 192 static inline void dev_sync_state(struct device *dev) 193 { 194 if (dev->bus->sync_state) 195 dev->bus->sync_state(dev); 196 else if (dev->driver && dev->driver->sync_state) 197 dev->driver->sync_state(dev); 198 } 199 200 int driver_add_groups(const struct device_driver *drv, 201 const struct attribute_group *const *groups); 202 void driver_remove_groups(const struct device_driver *drv, 203 const struct attribute_group *const *groups); 204 void device_driver_detach(struct device *dev); 205 206 static inline void device_set_driver(struct device *dev, const struct device_driver *drv) 207 { 208 /* 209 * Majority (all?) read accesses to dev->driver happens either 210 * while holding device lock or in bus/driver code that is only 211 * invoked when the device is bound to a driver and there is no 212 * concern of the pointer being changed while it is being read. 213 * However when reading device's uevent file we read driver pointer 214 * without taking device lock (so we do not block there for 215 * arbitrary amount of time). We use WRITE_ONCE() here to prevent 216 * tearing so that READ_ONCE() can safely be used in uevent code. 217 */ 218 // FIXME - this cast should not be needed "soon" 219 WRITE_ONCE(dev->driver, (struct device_driver *)drv); 220 } 221 222 struct devres_node; 223 typedef void (*dr_node_release_t)(struct device *dev, struct devres_node *node); 224 typedef void (*dr_node_free_t)(struct devres_node *node); 225 226 struct devres_node { 227 struct list_head entry; 228 dr_node_release_t release; 229 dr_node_free_t free_node; 230 const char *name; 231 size_t size; 232 }; 233 234 void devres_node_init(struct devres_node *node, dr_node_release_t release, 235 dr_node_free_t free_node); 236 void devres_node_add(struct device *dev, struct devres_node *node); 237 bool devres_node_remove(struct device *dev, struct devres_node *node); 238 void devres_set_node_dbginfo(struct devres_node *node, const char *name, 239 size_t size); 240 void devres_for_each_res(struct device *dev, dr_release_t release, 241 dr_match_t match, void *match_data, 242 void (*fn)(struct device *, void *, void *), 243 void *data); 244 int devres_release_all(struct device *dev); 245 void device_block_probing(void); 246 void device_unblock_probing(void); 247 void deferred_probe_extend_timeout(void); 248 void driver_deferred_probe_trigger(void); 249 const char *device_get_devnode(const struct device *dev, umode_t *mode, 250 kuid_t *uid, kgid_t *gid, const char **tmp); 251 252 /* /sys/devices directory */ 253 extern struct kset *devices_kset; 254 void devices_kset_move_last(struct device *dev); 255 256 #if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS) 257 int module_add_driver(struct module *mod, const struct device_driver *drv); 258 void module_remove_driver(const struct device_driver *drv); 259 #else 260 static inline int module_add_driver(struct module *mod, 261 struct device_driver *drv) 262 { 263 return 0; 264 } 265 static inline void module_remove_driver(struct device_driver *drv) { } 266 #endif 267 268 #ifdef CONFIG_DEVTMPFS 269 int devtmpfs_init(void); 270 #else 271 static inline int devtmpfs_init(void) { return 0; } 272 #endif 273 274 #ifdef CONFIG_BLOCK 275 extern const struct class block_class; 276 static inline bool is_blockdev(struct device *dev) 277 { 278 return dev->class == &block_class; 279 } 280 #else 281 static inline bool is_blockdev(struct device *dev) { return false; } 282 #endif 283 284 /* Device links support */ 285 int device_links_read_lock(void); 286 void device_links_read_unlock(int idx); 287 int device_links_read_lock_held(void); 288 int device_links_check_suppliers(struct device *dev); 289 void device_links_force_bind(struct device *dev); 290 void device_links_driver_bound(struct device *dev); 291 void device_links_driver_cleanup(struct device *dev); 292 void device_links_no_driver(struct device *dev); 293 bool device_links_busy(struct device *dev); 294 void device_links_unbind_consumers(struct device *dev); 295 bool device_link_flag_is_sync_state_only(u32 flags); 296 void fw_devlink_drivers_done(void); 297 void fw_devlink_probing_done(void); 298 299 #define dev_for_each_link_to_supplier(__link, __dev) \ 300 list_for_each_entry_srcu(__link, &(__dev)->links.suppliers, c_node, \ 301 device_links_read_lock_held()) 302 303 #define dev_for_each_link_to_consumer(__link, __dev) \ 304 list_for_each_entry_srcu(__link, &(__dev)->links.consumers, s_node, \ 305 device_links_read_lock_held()) 306 307 /* device pm support */ 308 void device_pm_move_to_tail(struct device *dev); 309 310 #ifdef CONFIG_DEVTMPFS 311 int devtmpfs_create_node(struct device *dev); 312 int devtmpfs_delete_node(struct device *dev); 313 #else 314 static inline int devtmpfs_create_node(struct device *dev) { return 0; } 315 static inline int devtmpfs_delete_node(struct device *dev) { return 0; } 316 #endif 317 318 void software_node_init(void); 319 void software_node_notify(struct device *dev); 320 void software_node_notify_remove(struct device *dev); 321 322 #ifdef CONFIG_PINCTRL 323 int pinctrl_bind_pins(struct device *dev); 324 #else 325 static inline int pinctrl_bind_pins(struct device *dev) 326 { 327 return 0; 328 } 329 #endif /* CONFIG_PINCTRL */ 330