xref: /openbmc/linux/drivers/base/base.h (revision 9cc61e5f)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
25367601bSGreg Kroah-Hartman /*
35367601bSGreg Kroah-Hartman  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
45367601bSGreg Kroah-Hartman  * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
55367601bSGreg Kroah-Hartman  * Copyright (c) 2008-2012 Novell Inc.
65367601bSGreg Kroah-Hartman  * Copyright (c) 2012-2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
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.
30*9cc61e5fSGreg 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.
366b6e39a6SKay Sievers  *
37c6f7e72aSGreg Kroah-Hartman  * This structure is the one that is the actual kobject allowing struct
386b6e39a6SKay Sievers  * bus_type/class to be statically allocated safely.  Nothing outside of the
396b6e39a6SKay Sievers  * driver core should ever touch these fields.
40c6f7e72aSGreg Kroah-Hartman  */
416b6e39a6SKay Sievers struct subsys_private {
42c6f7e72aSGreg Kroah-Hartman 	struct kset subsys;
43c6f7e72aSGreg Kroah-Hartman 	struct kset *devices_kset;
44ca22e56dSKay Sievers 	struct list_head interfaces;
45ca22e56dSKay Sievers 	struct mutex mutex;
466b6e39a6SKay Sievers 
476b6e39a6SKay Sievers 	struct kset *drivers_kset;
48c6f7e72aSGreg Kroah-Hartman 	struct klist klist_devices;
49c6f7e72aSGreg Kroah-Hartman 	struct klist klist_drivers;
50c6f7e72aSGreg Kroah-Hartman 	struct blocking_notifier_head bus_notifier;
51c6f7e72aSGreg Kroah-Hartman 	unsigned int drivers_autoprobe:1;
52c6f7e72aSGreg Kroah-Hartman 	struct bus_type *bus;
53*9cc61e5fSGreg Kroah-Hartman 	struct device *dev_root;
546b6e39a6SKay Sievers 
556b6e39a6SKay Sievers 	struct kset glue_dirs;
566b6e39a6SKay Sievers 	struct class *class;
5737e98d9bSGreg Kroah-Hartman 
5837e98d9bSGreg Kroah-Hartman 	struct lock_class_key lock_key;
59c6f7e72aSGreg Kroah-Hartman };
607bbb89b4SGreg Kroah-Hartman #define to_subsys_private(obj) container_of_const(obj, struct subsys_private, subsys.kobj)
61a1bdc7aaSBen Dooks 
62789be03aSGreg Kroah-Hartman static inline struct subsys_private *subsys_get(struct subsys_private *sp)
63789be03aSGreg Kroah-Hartman {
64789be03aSGreg Kroah-Hartman 	if (sp)
65789be03aSGreg Kroah-Hartman 		kset_get(&sp->subsys);
66789be03aSGreg Kroah-Hartman 	return sp;
67789be03aSGreg Kroah-Hartman }
68789be03aSGreg Kroah-Hartman 
69789be03aSGreg Kroah-Hartman static inline void subsys_put(struct subsys_private *sp)
70789be03aSGreg Kroah-Hartman {
71789be03aSGreg Kroah-Hartman 	if (sp)
72789be03aSGreg Kroah-Hartman 		kset_put(&sp->subsys);
73789be03aSGreg Kroah-Hartman }
74789be03aSGreg Kroah-Hartman 
75e5dd1278SGreg Kroah-Hartman struct driver_private {
76e5dd1278SGreg Kroah-Hartman 	struct kobject kobj;
77e5dd1278SGreg Kroah-Hartman 	struct klist klist_devices;
78e5dd1278SGreg Kroah-Hartman 	struct klist_node knode_bus;
79e5dd1278SGreg Kroah-Hartman 	struct module_kobject *mkobj;
80e5dd1278SGreg Kroah-Hartman 	struct device_driver *driver;
81e5dd1278SGreg Kroah-Hartman };
82e5dd1278SGreg Kroah-Hartman #define to_driver(obj) container_of(obj, struct driver_private, kobj)
83c6f7e72aSGreg Kroah-Hartman 
84fb069a5dSGreg Kroah-Hartman /**
85fb069a5dSGreg Kroah-Hartman  * struct device_private - structure to hold the private to the driver core portions of the device structure.
86fb069a5dSGreg Kroah-Hartman  *
87f791b8c8SGreg Kroah-Hartman  * @klist_children - klist containing all children of this device
88f791b8c8SGreg Kroah-Hartman  * @knode_parent - node in sibling list
898940b4f3SGreg Kroah-Hartman  * @knode_driver - node in driver list
90ae1b4171SGreg Kroah-Hartman  * @knode_bus - node in bus list
91570d0200SWei Yang  * @knode_class - node in class list
92ef8a3fd6SGreg Kroah-Hartman  * @deferred_probe - entry in deferred_probe_list which is used to retry the
93ef8a3fd6SGreg Kroah-Hartman  *	binding of drivers which were unable to get all the resources needed by
94ef8a3fd6SGreg Kroah-Hartman  *	the device; typically because it depends on another driver getting
95ef8a3fd6SGreg Kroah-Hartman  *	probed first.
96ef0ff683SAlexander Duyck  * @async_driver - pointer to device driver awaiting probe via async_probe
9782b2c3c5STomeu Vizoso  * @device - pointer back to the struct device that this structure is
98fb069a5dSGreg Kroah-Hartman  * associated with.
993451a495SAlexander Duyck  * @dead - This device is currently either in the process of or has been
1003451a495SAlexander Duyck  *	removed from the system. Any asynchronous events scheduled for this
1013451a495SAlexander Duyck  *	device should exit without taking any action.
102fb069a5dSGreg Kroah-Hartman  *
103fb069a5dSGreg Kroah-Hartman  * Nothing outside of the driver core should ever touch these fields.
104fb069a5dSGreg Kroah-Hartman  */
105fb069a5dSGreg Kroah-Hartman struct device_private {
106f791b8c8SGreg Kroah-Hartman 	struct klist klist_children;
107f791b8c8SGreg Kroah-Hartman 	struct klist_node knode_parent;
1088940b4f3SGreg Kroah-Hartman 	struct klist_node knode_driver;
109ae1b4171SGreg Kroah-Hartman 	struct klist_node knode_bus;
110570d0200SWei Yang 	struct klist_node knode_class;
111ef8a3fd6SGreg Kroah-Hartman 	struct list_head deferred_probe;
112ef0ff683SAlexander Duyck 	struct device_driver *async_driver;
113d090b70eSAndrzej Hajda 	char *deferred_probe_reason;
114fb069a5dSGreg Kroah-Hartman 	struct device *device;
1153451a495SAlexander Duyck 	u8 dead:1;
116fb069a5dSGreg Kroah-Hartman };
117f791b8c8SGreg Kroah-Hartman #define to_device_private_parent(obj)	\
118f791b8c8SGreg Kroah-Hartman 	container_of(obj, struct device_private, knode_parent)
1198940b4f3SGreg Kroah-Hartman #define to_device_private_driver(obj)	\
1208940b4f3SGreg Kroah-Hartman 	container_of(obj, struct device_private, knode_driver)
121ae1b4171SGreg Kroah-Hartman #define to_device_private_bus(obj)	\
122ae1b4171SGreg Kroah-Hartman 	container_of(obj, struct device_private, knode_bus)
123570d0200SWei Yang #define to_device_private_class(obj)	\
124570d0200SWei Yang 	container_of(obj, struct device_private, knode_class)
125fb069a5dSGreg Kroah-Hartman 
126c6f7e72aSGreg Kroah-Hartman /* initialisation functions */
127a1bdc7aaSBen Dooks extern int devices_init(void);
128a1bdc7aaSBen Dooks extern int buses_init(void);
129a1bdc7aaSBen Dooks extern int classes_init(void);
130a1bdc7aaSBen Dooks extern int firmware_init(void);
1314039483fSMichael Holzheu #ifdef CONFIG_SYS_HYPERVISOR
1324039483fSMichael Holzheu extern int hypervisor_init(void);
1334039483fSMichael Holzheu #else
1344039483fSMichael Holzheu static inline int hypervisor_init(void) { return 0; }
1354039483fSMichael Holzheu #endif
136a1bdc7aaSBen Dooks extern int platform_bus_init(void);
137024f7846SBen Hutchings extern void cpu_dev_init(void);
138caa73ea1SRafael J. Wysocki extern void container_dev_init(void);
139471b12c4SDave Jiang #ifdef CONFIG_AUXILIARY_BUS
140471b12c4SDave Jiang extern void auxiliary_bus_init(void);
141471b12c4SDave Jiang #else
142471b12c4SDave Jiang static inline void auxiliary_bus_init(void) { }
143471b12c4SDave Jiang #endif
144a1bdc7aaSBen Dooks 
145d73ce004STejun Heo struct kobject *virtual_device_parent(struct device *dev);
146d73ce004STejun Heo 
1471da177e4SLinus Torvalds extern int bus_add_device(struct device *dev);
1482023c610SAlan Stern extern void bus_probe_device(struct device *dev);
1491da177e4SLinus Torvalds extern void bus_remove_device(struct device *dev);
150ed9f9181SGreg Kroah-Hartman void bus_notify(struct device *dev, enum bus_notifier_event value);
15163b823d7SGreg Kroah-Hartman bool bus_is_registered(const struct bus_type *bus);
1521da177e4SLinus Torvalds 
1534a3ad20cSGreg Kroah-Hartman extern int bus_add_driver(struct device_driver *drv);
1544a3ad20cSGreg Kroah-Hartman extern void bus_remove_driver(struct device_driver *drv);
1559ed98953SRafael J. Wysocki extern void device_release_driver_internal(struct device *dev,
1569ed98953SRafael J. Wysocki 					   struct device_driver *drv,
1579ed98953SRafael J. Wysocki 					   struct device *parent);
1581da177e4SLinus Torvalds 
15907e4a3e2Smochel@digitalimplant.org extern void driver_detach(struct device_driver *drv);
160d1c3414cSGrant Likely extern void driver_deferred_probe_del(struct device *dev);
161d090b70eSAndrzej Hajda extern void device_set_deferred_probe_reason(const struct device *dev,
162d090b70eSAndrzej Hajda 					     struct va_format *vaf);
16349b420a1SMing Lei static inline int driver_match_device(struct device_driver *drv,
16449b420a1SMing Lei 				      struct device *dev)
16549b420a1SMing Lei {
1665247aecfSMing Lei 	return drv->bus->match ? drv->bus->match(dev, drv) : 1;
16749b420a1SMing Lei }
16807e4a3e2Smochel@digitalimplant.org 
169f8fb5766SSaravana Kannan static inline void dev_sync_state(struct device *dev)
170f8fb5766SSaravana Kannan {
171f8fb5766SSaravana Kannan 	if (dev->bus->sync_state)
172f8fb5766SSaravana Kannan 		dev->bus->sync_state(dev);
173f8fb5766SSaravana Kannan 	else if (dev->driver && dev->driver->sync_state)
174f8fb5766SSaravana Kannan 		dev->driver->sync_state(dev);
175f8fb5766SSaravana Kannan }
176f8fb5766SSaravana Kannan 
177ed0617b5SGreg Kroah-Hartman extern int driver_add_groups(struct device_driver *drv,
178ed0617b5SGreg Kroah-Hartman 			     const struct attribute_group **groups);
179ed0617b5SGreg Kroah-Hartman extern void driver_remove_groups(struct device_driver *drv,
180ed0617b5SGreg Kroah-Hartman 				 const struct attribute_group **groups);
181ed88747cSAlexander Duyck void device_driver_detach(struct device *dev);
182ed0617b5SGreg Kroah-Hartman 
1832a013455SAdrian Bunk extern int devres_release_all(struct device *dev);
184013c074fSStrashko, Grygorii extern void device_block_probing(void);
185013c074fSStrashko, Grygorii extern void device_unblock_probing(void);
1862b28a1a8SSaravana Kannan extern void deferred_probe_extend_timeout(void);
1872f8c3ae8SSaravana Kannan extern void driver_deferred_probe_trigger(void);
18842bb5be8SGreg Kroah-Hartman const char *device_get_devnode(const struct device *dev, umode_t *mode,
18942bb5be8SGreg Kroah-Hartman 			       kuid_t *uid, kgid_t *gid, const char **tmp);
190823bccfcSGreg Kroah-Hartman 
191ca22e56dSKay Sievers /* /sys/devices directory */
192881c6cfdSGreg Kroah-Hartman extern struct kset *devices_kset;
19352cdbdd4SGrygorii Strashko extern void devices_kset_move_last(struct device *dev);
194c63469a3SGreg Kroah-Hartman 
19592b42141SRandy Dunlap #if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
196c63469a3SGreg Kroah-Hartman extern void module_add_driver(struct module *mod, struct device_driver *drv);
197c63469a3SGreg Kroah-Hartman extern void module_remove_driver(struct device_driver *drv);
198c63469a3SGreg Kroah-Hartman #else
199c63469a3SGreg Kroah-Hartman static inline void module_add_driver(struct module *mod,
200c63469a3SGreg Kroah-Hartman 				     struct device_driver *drv) { }
201c63469a3SGreg Kroah-Hartman static inline void module_remove_driver(struct device_driver *drv) { }
202c63469a3SGreg Kroah-Hartman #endif
2032b2af54aSKay Sievers 
2042b2af54aSKay Sievers #ifdef CONFIG_DEVTMPFS
2052b2af54aSKay Sievers extern int devtmpfs_init(void);
2062b2af54aSKay Sievers #else
2072b2af54aSKay Sievers static inline int devtmpfs_init(void) { return 0; }
2082b2af54aSKay Sievers #endif
2099ed98953SRafael J. Wysocki 
2109ed98953SRafael J. Wysocki /* Device links support */
2119ed98953SRafael J. Wysocki extern int device_links_read_lock(void);
2129ed98953SRafael J. Wysocki extern void device_links_read_unlock(int idx);
213c2fa1e1bSJoel Fernandes (Google) extern int device_links_read_lock_held(void);
2149ed98953SRafael J. Wysocki extern int device_links_check_suppliers(struct device *dev);
215b6f617dfSSaravana Kannan extern void device_links_force_bind(struct device *dev);
2169ed98953SRafael J. Wysocki extern void device_links_driver_bound(struct device *dev);
2179ed98953SRafael J. Wysocki extern void device_links_driver_cleanup(struct device *dev);
2189ed98953SRafael J. Wysocki extern void device_links_no_driver(struct device *dev);
2199ed98953SRafael J. Wysocki extern bool device_links_busy(struct device *dev);
2209ed98953SRafael J. Wysocki extern void device_links_unbind_consumers(struct device *dev);
221d46f3e3eSSaravana Kannan extern void fw_devlink_drivers_done(void);
222ffbe08a8SSaravana Kannan extern void fw_devlink_probing_done(void);
223494fd7b7SFeng Kan 
224494fd7b7SFeng Kan /* device pm support */
225494fd7b7SFeng Kan void device_pm_move_to_tail(struct device *dev);
226cf901a1cSGreg Kroah-Hartman 
227cf901a1cSGreg Kroah-Hartman #ifdef CONFIG_DEVTMPFS
228cf901a1cSGreg Kroah-Hartman int devtmpfs_create_node(struct device *dev);
229d3583f06SGreg Kroah-Hartman int devtmpfs_delete_node(struct device *dev);
230cf901a1cSGreg Kroah-Hartman #else
231cf901a1cSGreg Kroah-Hartman static inline int devtmpfs_create_node(struct device *dev) { return 0; }
232d3583f06SGreg Kroah-Hartman static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
233cf901a1cSGreg Kroah-Hartman #endif
234384f5a85SRafael J. Wysocki 
235384f5a85SRafael J. Wysocki void software_node_notify(struct device *dev);
236384f5a85SRafael J. Wysocki void software_node_notify_remove(struct device *dev);
237