xref: /linux/drivers/base/base.h (revision 99676aed1fec109d62822e21a06760eb098dc5f4)
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 void dev_sync_state(struct device *dev)
176 {
177 	if (dev->bus->sync_state)
178 		dev->bus->sync_state(dev);
179 	else if (dev->driver && dev->driver->sync_state)
180 		dev->driver->sync_state(dev);
181 }
182 
183 int driver_add_groups(const struct device_driver *drv, const struct attribute_group **groups);
184 void driver_remove_groups(const struct device_driver *drv, const struct attribute_group **groups);
185 void device_driver_detach(struct device *dev);
186 
187 static inline void device_set_driver(struct device *dev, const struct device_driver *drv)
188 {
189 	/*
190 	 * Majority (all?) read accesses to dev->driver happens either
191 	 * while holding device lock or in bus/driver code that is only
192 	 * invoked when the device is bound to a driver and there is no
193 	 * concern of the pointer being changed while it is being read.
194 	 * However when reading device's uevent file we read driver pointer
195 	 * without taking device lock (so we do not block there for
196 	 * arbitrary amount of time). We use WRITE_ONCE() here to prevent
197 	 * tearing so that READ_ONCE() can safely be used in uevent code.
198 	 */
199 	// FIXME - this cast should not be needed "soon"
200 	WRITE_ONCE(dev->driver, (struct device_driver *)drv);
201 }
202 
203 struct devres_node;
204 typedef void (*dr_node_release_t)(struct device *dev, struct devres_node *node);
205 typedef void (*dr_node_free_t)(struct devres_node *node);
206 
207 struct devres_node {
208 	struct list_head		entry;
209 	dr_node_release_t		release;
210 	dr_node_free_t			free_node;
211 	const char			*name;
212 	size_t				size;
213 };
214 
215 void devres_node_init(struct devres_node *node, dr_node_release_t release,
216 		      dr_node_free_t free_node);
217 void devres_node_add(struct device *dev, struct devres_node *node);
218 bool devres_node_remove(struct device *dev, struct devres_node *node);
219 void devres_set_node_dbginfo(struct devres_node *node, const char *name,
220 			     size_t size);
221 void devres_for_each_res(struct device *dev, dr_release_t release,
222 			 dr_match_t match, void *match_data,
223 			 void (*fn)(struct device *, void *, void *),
224 			 void *data);
225 int devres_release_all(struct device *dev);
226 void device_block_probing(void);
227 void device_unblock_probing(void);
228 void deferred_probe_extend_timeout(void);
229 void driver_deferred_probe_trigger(void);
230 const char *device_get_devnode(const struct device *dev, umode_t *mode,
231 			       kuid_t *uid, kgid_t *gid, const char **tmp);
232 
233 /* /sys/devices directory */
234 extern struct kset *devices_kset;
235 void devices_kset_move_last(struct device *dev);
236 
237 #if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
238 int module_add_driver(struct module *mod, const struct device_driver *drv);
239 void module_remove_driver(const struct device_driver *drv);
240 #else
241 static inline int module_add_driver(struct module *mod,
242 				    struct device_driver *drv)
243 {
244 	return 0;
245 }
246 static inline void module_remove_driver(struct device_driver *drv) { }
247 #endif
248 
249 #ifdef CONFIG_DEVTMPFS
250 int devtmpfs_init(void);
251 #else
252 static inline int devtmpfs_init(void) { return 0; }
253 #endif
254 
255 #ifdef CONFIG_BLOCK
256 extern const struct class block_class;
257 static inline bool is_blockdev(struct device *dev)
258 {
259 	return dev->class == &block_class;
260 }
261 #else
262 static inline bool is_blockdev(struct device *dev) { return false; }
263 #endif
264 
265 /* Device links support */
266 int device_links_read_lock(void);
267 void device_links_read_unlock(int idx);
268 int device_links_read_lock_held(void);
269 int device_links_check_suppliers(struct device *dev);
270 void device_links_force_bind(struct device *dev);
271 void device_links_driver_bound(struct device *dev);
272 void device_links_driver_cleanup(struct device *dev);
273 void device_links_no_driver(struct device *dev);
274 bool device_links_busy(struct device *dev);
275 void device_links_unbind_consumers(struct device *dev);
276 bool device_link_flag_is_sync_state_only(u32 flags);
277 void fw_devlink_drivers_done(void);
278 void fw_devlink_probing_done(void);
279 
280 #define dev_for_each_link_to_supplier(__link, __dev)	\
281 	list_for_each_entry_srcu(__link, &(__dev)->links.suppliers, c_node, \
282 				 device_links_read_lock_held())
283 
284 #define dev_for_each_link_to_consumer(__link, __dev)	\
285 	list_for_each_entry_srcu(__link, &(__dev)->links.consumers, s_node, \
286 				 device_links_read_lock_held())
287 
288 /* device pm support */
289 void device_pm_move_to_tail(struct device *dev);
290 
291 #ifdef CONFIG_DEVTMPFS
292 int devtmpfs_create_node(struct device *dev);
293 int devtmpfs_delete_node(struct device *dev);
294 #else
295 static inline int devtmpfs_create_node(struct device *dev) { return 0; }
296 static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
297 #endif
298 
299 void software_node_init(void);
300 void software_node_notify(struct device *dev);
301 void software_node_notify_remove(struct device *dev);
302 
303 #ifdef CONFIG_PINCTRL
304 int pinctrl_bind_pins(struct device *dev);
305 #else
306 static inline int pinctrl_bind_pins(struct device *dev)
307 {
308 	return 0;
309 }
310 #endif /* CONFIG_PINCTRL */
311