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