1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
25367601bSGreg Kroah-Hartman /*
35367601bSGreg Kroah-Hartman * Copyright (c) 2001-2003 Patrick Mochel <[email protected]>
45367601bSGreg Kroah-Hartman * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]>
55367601bSGreg Kroah-Hartman * Copyright (c) 2008-2012 Novell Inc.
65367601bSGreg Kroah-Hartman * Copyright (c) 2012-2019 Greg Kroah-Hartman <[email protected]>
75367601bSGreg Kroah-Hartman * Copyright (c) 2012-2019 Linux Foundation
85367601bSGreg Kroah-Hartman *
95367601bSGreg Kroah-Hartman * Core driver model functions and structures that should not be
105367601bSGreg Kroah-Hartman * shared outside of the drivers/base/ directory.
115367601bSGreg Kroah-Hartman *
125367601bSGreg Kroah-Hartman */
13ba33162aSPaul Gortmaker #include <linux/notifier.h>
14a1bdc7aaSBen Dooks
15c6f7e72aSGreg Kroah-Hartman /**
166b6e39a6SKay Sievers * struct subsys_private - structure to hold the private to the driver core portions of the bus_type/class structure.
17c6f7e72aSGreg Kroah-Hartman *
186b6e39a6SKay Sievers * @subsys - the struct kset that defines this subsystem
19ca22e56dSKay Sievers * @devices_kset - the subsystem's 'devices' directory
20ca22e56dSKay Sievers * @interfaces - list of subsystem interfaces associated
21ca22e56dSKay Sievers * @mutex - protect the devices, and interfaces lists.
226b6e39a6SKay Sievers *
236b6e39a6SKay Sievers * @drivers_kset - the list of drivers associated
24c6f7e72aSGreg Kroah-Hartman * @klist_devices - the klist to iterate over the @devices_kset
25c6f7e72aSGreg Kroah-Hartman * @klist_drivers - the klist to iterate over the @drivers_kset
26c6f7e72aSGreg Kroah-Hartman * @bus_notifier - the bus notifier list for anything that cares about things
27c6f7e72aSGreg Kroah-Hartman * on this bus.
28c6f7e72aSGreg Kroah-Hartman * @bus - pointer back to the struct bus_type that this structure is associated
29c6f7e72aSGreg Kroah-Hartman * with.
309cc61e5fSGreg Kroah-Hartman * @dev_root: Default device to use as the parent.
31c6f7e72aSGreg Kroah-Hartman *
326b6e39a6SKay Sievers * @glue_dirs - "glue" directory to put in-between the parent device to
336b6e39a6SKay Sievers * avoid namespace conflicts
346b6e39a6SKay Sievers * @class - pointer back to the struct class that this structure is associated
356b6e39a6SKay Sievers * with.
3600945520SGreg Kroah-Hartman * @lock_key: Lock class key for use by the lock validator
376b6e39a6SKay Sievers *
38c6f7e72aSGreg Kroah-Hartman * This structure is the one that is the actual kobject allowing struct
396b6e39a6SKay Sievers * bus_type/class to be statically allocated safely. Nothing outside of the
406b6e39a6SKay Sievers * driver core should ever touch these fields.
41c6f7e72aSGreg Kroah-Hartman */
426b6e39a6SKay Sievers struct subsys_private {
43c6f7e72aSGreg Kroah-Hartman struct kset subsys;
44c6f7e72aSGreg Kroah-Hartman struct kset *devices_kset;
45ca22e56dSKay Sievers struct list_head interfaces;
46ca22e56dSKay Sievers struct mutex mutex;
476b6e39a6SKay Sievers
486b6e39a6SKay Sievers struct kset *drivers_kset;
49c6f7e72aSGreg Kroah-Hartman struct klist klist_devices;
50c6f7e72aSGreg Kroah-Hartman struct klist klist_drivers;
51c6f7e72aSGreg Kroah-Hartman struct blocking_notifier_head bus_notifier;
52c6f7e72aSGreg Kroah-Hartman unsigned int drivers_autoprobe:1;
5300c4a3c4SGreg Kroah-Hartman const struct bus_type *bus;
549cc61e5fSGreg Kroah-Hartman struct device *dev_root;
556b6e39a6SKay Sievers
566b6e39a6SKay Sievers struct kset glue_dirs;
5743a7206bSGreg Kroah-Hartman const struct class *class;
5837e98d9bSGreg Kroah-Hartman
5937e98d9bSGreg Kroah-Hartman struct lock_class_key lock_key;
60c6f7e72aSGreg Kroah-Hartman };
617bbb89b4SGreg Kroah-Hartman #define to_subsys_private(obj) container_of_const(obj, struct subsys_private, subsys.kobj)
62a1bdc7aaSBen Dooks
subsys_get(struct subsys_private * sp)63789be03aSGreg Kroah-Hartman static inline struct subsys_private *subsys_get(struct subsys_private *sp)
64789be03aSGreg Kroah-Hartman {
65789be03aSGreg Kroah-Hartman if (sp)
66789be03aSGreg Kroah-Hartman kset_get(&sp->subsys);
67789be03aSGreg Kroah-Hartman return sp;
68789be03aSGreg Kroah-Hartman }
69789be03aSGreg Kroah-Hartman
subsys_put(struct subsys_private * sp)70789be03aSGreg Kroah-Hartman static inline void subsys_put(struct subsys_private *sp)
71789be03aSGreg Kroah-Hartman {
72789be03aSGreg Kroah-Hartman if (sp)
73789be03aSGreg Kroah-Hartman kset_put(&sp->subsys);
74789be03aSGreg Kroah-Hartman }
75789be03aSGreg Kroah-Hartman
76*18daa524SDmitry Torokhov struct subsys_private *bus_to_subsys(const struct bus_type *bus);
777d90e81aSGreg Kroah-Hartman struct subsys_private *class_to_subsys(const struct class *class);
787d90e81aSGreg Kroah-Hartman
79e5dd1278SGreg Kroah-Hartman struct driver_private {
80e5dd1278SGreg Kroah-Hartman struct kobject kobj;
81e5dd1278SGreg Kroah-Hartman struct klist klist_devices;
82e5dd1278SGreg Kroah-Hartman struct klist_node knode_bus;
83e5dd1278SGreg Kroah-Hartman struct module_kobject *mkobj;
84e5dd1278SGreg Kroah-Hartman struct device_driver *driver;
85e5dd1278SGreg Kroah-Hartman };
86e5dd1278SGreg Kroah-Hartman #define to_driver(obj) container_of(obj, struct driver_private, kobj)
87c6f7e72aSGreg Kroah-Hartman
88fb069a5dSGreg Kroah-Hartman /**
89fb069a5dSGreg Kroah-Hartman * struct device_private - structure to hold the private to the driver core portions of the device structure.
90fb069a5dSGreg Kroah-Hartman *
91f791b8c8SGreg Kroah-Hartman * @klist_children - klist containing all children of this device
92f791b8c8SGreg Kroah-Hartman * @knode_parent - node in sibling list
938940b4f3SGreg Kroah-Hartman * @knode_driver - node in driver list
94ae1b4171SGreg Kroah-Hartman * @knode_bus - node in bus list
95570d0200SWei Yang * @knode_class - node in class list
96ef8a3fd6SGreg Kroah-Hartman * @deferred_probe - entry in deferred_probe_list which is used to retry the
97ef8a3fd6SGreg Kroah-Hartman * binding of drivers which were unable to get all the resources needed by
98ef8a3fd6SGreg Kroah-Hartman * the device; typically because it depends on another driver getting
99ef8a3fd6SGreg Kroah-Hartman * probed first.
100ef0ff683SAlexander Duyck * @async_driver - pointer to device driver awaiting probe via async_probe
10182b2c3c5STomeu Vizoso * @device - pointer back to the struct device that this structure is
102fb069a5dSGreg Kroah-Hartman * associated with.
1033451a495SAlexander Duyck * @dead - This device is currently either in the process of or has been
1043451a495SAlexander Duyck * removed from the system. Any asynchronous events scheduled for this
1053451a495SAlexander Duyck * device should exit without taking any action.
106fb069a5dSGreg Kroah-Hartman *
107fb069a5dSGreg Kroah-Hartman * Nothing outside of the driver core should ever touch these fields.
108fb069a5dSGreg Kroah-Hartman */
109fb069a5dSGreg Kroah-Hartman struct device_private {
110f791b8c8SGreg Kroah-Hartman struct klist klist_children;
111f791b8c8SGreg Kroah-Hartman struct klist_node knode_parent;
1128940b4f3SGreg Kroah-Hartman struct klist_node knode_driver;
113ae1b4171SGreg Kroah-Hartman struct klist_node knode_bus;
114570d0200SWei Yang struct klist_node knode_class;
115ef8a3fd6SGreg Kroah-Hartman struct list_head deferred_probe;
116c6c631d2SGreg Kroah-Hartman const struct device_driver *async_driver;
117d090b70eSAndrzej Hajda char *deferred_probe_reason;
118fb069a5dSGreg Kroah-Hartman struct device *device;
1193451a495SAlexander Duyck u8 dead:1;
120fb069a5dSGreg Kroah-Hartman };
121f791b8c8SGreg Kroah-Hartman #define to_device_private_parent(obj) \
122f791b8c8SGreg Kroah-Hartman container_of(obj, struct device_private, knode_parent)
1238940b4f3SGreg Kroah-Hartman #define to_device_private_driver(obj) \
1248940b4f3SGreg Kroah-Hartman container_of(obj, struct device_private, knode_driver)
125ae1b4171SGreg Kroah-Hartman #define to_device_private_bus(obj) \
126ae1b4171SGreg Kroah-Hartman container_of(obj, struct device_private, knode_bus)
127570d0200SWei Yang #define to_device_private_class(obj) \
128570d0200SWei Yang container_of(obj, struct device_private, knode_class)
129fb069a5dSGreg Kroah-Hartman
130c6f7e72aSGreg Kroah-Hartman /* initialisation functions */
1318da5b970SGreg Kroah-Hartman int devices_init(void);
1328da5b970SGreg Kroah-Hartman int buses_init(void);
1338da5b970SGreg Kroah-Hartman int classes_init(void);
1348da5b970SGreg Kroah-Hartman int firmware_init(void);
1354039483fSMichael Holzheu #ifdef CONFIG_SYS_HYPERVISOR
1368da5b970SGreg Kroah-Hartman int hypervisor_init(void);
1374039483fSMichael Holzheu #else
hypervisor_init(void)1384039483fSMichael Holzheu static inline int hypervisor_init(void) { return 0; }
1394039483fSMichael Holzheu #endif
1408da5b970SGreg Kroah-Hartman int platform_bus_init(void);
14135fa2d88SGreg Kroah-Hartman int faux_bus_init(void);
1428da5b970SGreg Kroah-Hartman void cpu_dev_init(void);
1438da5b970SGreg Kroah-Hartman void container_dev_init(void);
144471b12c4SDave Jiang #ifdef CONFIG_AUXILIARY_BUS
1458da5b970SGreg Kroah-Hartman void auxiliary_bus_init(void);
146471b12c4SDave Jiang #else
auxiliary_bus_init(void)147471b12c4SDave Jiang static inline void auxiliary_bus_init(void) { }
148471b12c4SDave Jiang #endif
149a1bdc7aaSBen Dooks
1500314647dSZijun Hu struct kobject *virtual_device_parent(void);
151d73ce004STejun Heo
1528da5b970SGreg Kroah-Hartman int bus_add_device(struct device *dev);
1538da5b970SGreg Kroah-Hartman void bus_probe_device(struct device *dev);
1548da5b970SGreg Kroah-Hartman void bus_remove_device(struct device *dev);
155ed9f9181SGreg Kroah-Hartman void bus_notify(struct device *dev, enum bus_notifier_event value);
15663b823d7SGreg Kroah-Hartman bool bus_is_registered(const struct bus_type *bus);
1571da177e4SLinus Torvalds
1588da5b970SGreg Kroah-Hartman int bus_add_driver(struct device_driver *drv);
1598da5b970SGreg Kroah-Hartman void bus_remove_driver(struct device_driver *drv);
16033ebea9bSGreg Kroah-Hartman void device_release_driver_internal(struct device *dev, const struct device_driver *drv,
1619ed98953SRafael J. Wysocki struct device *parent);
1621da177e4SLinus Torvalds
163f6e98ef5SGreg Kroah-Hartman void driver_detach(const struct device_driver *drv);
1648da5b970SGreg Kroah-Hartman void driver_deferred_probe_del(struct device *dev);
1658da5b970SGreg Kroah-Hartman void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf);
driver_match_device(const struct device_driver * drv,struct device * dev)166269e974eSGreg Kroah-Hartman static inline int driver_match_device(const struct device_driver *drv,
16749b420a1SMing Lei struct device *dev)
16849b420a1SMing Lei {
169d69d8048SGreg Kroah-Hartman return drv->bus->match ? drv->bus->match(dev, drv) : 1;
17049b420a1SMing Lei }
17107e4a3e2S[email protected]
dev_sync_state(struct device * dev)172f8fb5766SSaravana Kannan static inline void dev_sync_state(struct device *dev)
173f8fb5766SSaravana Kannan {
174f8fb5766SSaravana Kannan if (dev->bus->sync_state)
175f8fb5766SSaravana Kannan dev->bus->sync_state(dev);
176f8fb5766SSaravana Kannan else if (dev->driver && dev->driver->sync_state)
177f8fb5766SSaravana Kannan dev->driver->sync_state(dev);
178f8fb5766SSaravana Kannan }
179f8fb5766SSaravana Kannan
1800725e8f9SGreg Kroah-Hartman int driver_add_groups(const struct device_driver *drv, const struct attribute_group **groups);
1810725e8f9SGreg Kroah-Hartman void driver_remove_groups(const struct device_driver *drv, const struct attribute_group **groups);
182ed88747cSAlexander Duyck void device_driver_detach(struct device *dev);
183ed0617b5SGreg Kroah-Hartman
device_set_driver(struct device * dev,const struct device_driver * drv)18404d3e546SDmitry Torokhov static inline void device_set_driver(struct device *dev, const struct device_driver *drv)
18504d3e546SDmitry Torokhov {
186*18daa524SDmitry Torokhov /*
187*18daa524SDmitry Torokhov * Majority (all?) read accesses to dev->driver happens either
188*18daa524SDmitry Torokhov * while holding device lock or in bus/driver code that is only
189*18daa524SDmitry Torokhov * invoked when the device is bound to a driver and there is no
190*18daa524SDmitry Torokhov * concern of the pointer being changed while it is being read.
191*18daa524SDmitry Torokhov * However when reading device's uevent file we read driver pointer
192*18daa524SDmitry Torokhov * without taking device lock (so we do not block there for
193*18daa524SDmitry Torokhov * arbitrary amount of time). We use WRITE_ONCE() here to prevent
194*18daa524SDmitry Torokhov * tearing so that READ_ONCE() can safely be used in uevent code.
195*18daa524SDmitry Torokhov */
19604d3e546SDmitry Torokhov // FIXME - this cast should not be needed "soon"
197*18daa524SDmitry Torokhov WRITE_ONCE(dev->driver, (struct device_driver *)drv);
19804d3e546SDmitry Torokhov }
19904d3e546SDmitry Torokhov
2008da5b970SGreg Kroah-Hartman int devres_release_all(struct device *dev);
2018da5b970SGreg Kroah-Hartman void device_block_probing(void);
2028da5b970SGreg Kroah-Hartman void device_unblock_probing(void);
2038da5b970SGreg Kroah-Hartman void deferred_probe_extend_timeout(void);
2048da5b970SGreg Kroah-Hartman void driver_deferred_probe_trigger(void);
20542bb5be8SGreg Kroah-Hartman const char *device_get_devnode(const struct device *dev, umode_t *mode,
20642bb5be8SGreg Kroah-Hartman kuid_t *uid, kgid_t *gid, const char **tmp);
207823bccfcSGreg Kroah-Hartman
208ca22e56dSKay Sievers /* /sys/devices directory */
209881c6cfdSGreg Kroah-Hartman extern struct kset *devices_kset;
2108da5b970SGreg Kroah-Hartman void devices_kset_move_last(struct device *dev);
211c63469a3SGreg Kroah-Hartman
21292b42141SRandy Dunlap #if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
21367c1ba55SGreg Kroah-Hartman int module_add_driver(struct module *mod, const struct device_driver *drv);
21467c1ba55SGreg Kroah-Hartman void module_remove_driver(const struct device_driver *drv);
215c63469a3SGreg Kroah-Hartman #else
module_add_driver(struct module * mod,struct device_driver * drv)21685d2b0aaSArnd Bergmann static inline int module_add_driver(struct module *mod,
21785d2b0aaSArnd Bergmann struct device_driver *drv)
21885d2b0aaSArnd Bergmann {
21985d2b0aaSArnd Bergmann return 0;
22085d2b0aaSArnd Bergmann }
module_remove_driver(struct device_driver * drv)221c63469a3SGreg Kroah-Hartman static inline void module_remove_driver(struct device_driver *drv) { }
222c63469a3SGreg Kroah-Hartman #endif
2232b2af54aSKay Sievers
2242b2af54aSKay Sievers #ifdef CONFIG_DEVTMPFS
2258da5b970SGreg Kroah-Hartman int devtmpfs_init(void);
2262b2af54aSKay Sievers #else
devtmpfs_init(void)2272b2af54aSKay Sievers static inline int devtmpfs_init(void) { return 0; }
2282b2af54aSKay Sievers #endif
2299ed98953SRafael J. Wysocki
230d6bdbbdfSGreg Kroah-Hartman #ifdef CONFIG_BLOCK
231f8c7511dSRicardo B. Marliere extern const struct class block_class;
is_blockdev(struct device * dev)232d6bdbbdfSGreg Kroah-Hartman static inline bool is_blockdev(struct device *dev)
233d6bdbbdfSGreg Kroah-Hartman {
234d6bdbbdfSGreg Kroah-Hartman return dev->class == &block_class;
235d6bdbbdfSGreg Kroah-Hartman }
236d6bdbbdfSGreg Kroah-Hartman #else
is_blockdev(struct device * dev)237d6bdbbdfSGreg Kroah-Hartman static inline bool is_blockdev(struct device *dev) { return false; }
238d6bdbbdfSGreg Kroah-Hartman #endif
239d6bdbbdfSGreg Kroah-Hartman
2409ed98953SRafael J. Wysocki /* Device links support */
2418da5b970SGreg Kroah-Hartman int device_links_read_lock(void);
2428da5b970SGreg Kroah-Hartman void device_links_read_unlock(int idx);
2438da5b970SGreg Kroah-Hartman int device_links_read_lock_held(void);
2448da5b970SGreg Kroah-Hartman int device_links_check_suppliers(struct device *dev);
2458da5b970SGreg Kroah-Hartman void device_links_force_bind(struct device *dev);
2468da5b970SGreg Kroah-Hartman void device_links_driver_bound(struct device *dev);
2478da5b970SGreg Kroah-Hartman void device_links_driver_cleanup(struct device *dev);
2488da5b970SGreg Kroah-Hartman void device_links_no_driver(struct device *dev);
2498da5b970SGreg Kroah-Hartman bool device_links_busy(struct device *dev);
2508da5b970SGreg Kroah-Hartman void device_links_unbind_consumers(struct device *dev);
2518da5b970SGreg Kroah-Hartman void fw_devlink_drivers_done(void);
2528da5b970SGreg Kroah-Hartman void fw_devlink_probing_done(void);
253494fd7b7SFeng Kan
254494fd7b7SFeng Kan /* device pm support */
255494fd7b7SFeng Kan void device_pm_move_to_tail(struct device *dev);
256cf901a1cSGreg Kroah-Hartman
257cf901a1cSGreg Kroah-Hartman #ifdef CONFIG_DEVTMPFS
258cf901a1cSGreg Kroah-Hartman int devtmpfs_create_node(struct device *dev);
259d3583f06SGreg Kroah-Hartman int devtmpfs_delete_node(struct device *dev);
260cf901a1cSGreg Kroah-Hartman #else
devtmpfs_create_node(struct device * dev)261cf901a1cSGreg Kroah-Hartman static inline int devtmpfs_create_node(struct device *dev) { return 0; }
devtmpfs_delete_node(struct device * dev)262d3583f06SGreg Kroah-Hartman static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
263cf901a1cSGreg Kroah-Hartman #endif
264384f5a85SRafael J. Wysocki
265384f5a85SRafael J. Wysocki void software_node_notify(struct device *dev);
266384f5a85SRafael J. Wysocki void software_node_notify_remove(struct device *dev);
267