xref: /linux/drivers/base/base.h (revision 1cf996ac307e4d8d86f07d72b55528df88b56ce6)
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 #ifdef CONFIG_RUST
90 /**
91  * struct driver_type - Representation of a Rust driver type.
92  */
93 struct driver_type {
94 	/**
95 	 * @id: Representation of core::any::TypeId.
96 	 */
97 	u8 id[16];
98 } __packed;
99 #endif
100 
101 /**
102  * struct device_private - structure to hold the private to the driver core
103  *			   portions of the device structure.
104  * @klist_children: klist containing all children of this device
105  * @knode_parent: node in sibling list
106  * @knode_driver: node in driver list
107  * @knode_bus: node in bus list
108  * @knode_class: node in class list
109  * @deferred_probe: entry in deferred_probe_list which is used to retry the
110  *		    binding of drivers which were unable to get all the
111  *		    resources needed by the device; typically because it depends
112  *		    on another driver getting probed first.
113  * @async_driver: pointer to device driver awaiting probe via async_probe
114  * @deferred_probe_reason: capture the -EPROBE_DEFER message emitted with
115  *			   dev_err_probe() for later retrieval via debugfs
116  * @device: pointer back to the struct device that this structure is
117  *	    associated with.
118  * @driver_type: The type of the bound Rust driver.
119  * @dead: This device is currently either in the process of or has been
120  *	  removed from the system. Any asynchronous events scheduled for this
121  *	  device should exit without taking any action.
122  *
123  * Nothing outside of the driver core should ever touch these fields.
124  */
125 struct device_private {
126 	struct klist klist_children;
127 	struct klist_node knode_parent;
128 	struct klist_node knode_driver;
129 	struct klist_node knode_bus;
130 	struct klist_node knode_class;
131 	struct list_head deferred_probe;
132 	const struct device_driver *async_driver;
133 	char *deferred_probe_reason;
134 	struct device *device;
135 #ifdef CONFIG_RUST
136 	struct driver_type driver_type;
137 #endif
138 	u8 dead:1;
139 };
140 #define to_device_private_parent(obj)	\
141 	container_of(obj, struct device_private, knode_parent)
142 #define to_device_private_driver(obj)	\
143 	container_of(obj, struct device_private, knode_driver)
144 #define to_device_private_bus(obj)	\
145 	container_of(obj, struct device_private, knode_bus)
146 #define to_device_private_class(obj)	\
147 	container_of(obj, struct device_private, knode_class)
148 
149 /* initialisation functions */
150 int devices_init(void);
151 int buses_init(void);
152 int classes_init(void);
153 int firmware_init(void);
154 #ifdef CONFIG_SYS_HYPERVISOR
155 int hypervisor_init(void);
156 #else
157 static inline int hypervisor_init(void) { return 0; }
158 #endif
159 int platform_bus_init(void);
160 int faux_bus_init(void);
161 void cpu_dev_init(void);
162 void container_dev_init(void);
163 #ifdef CONFIG_AUXILIARY_BUS
164 void auxiliary_bus_init(void);
165 #else
166 static inline void auxiliary_bus_init(void) { }
167 #endif
168 
169 struct kobject *virtual_device_parent(void);
170 
171 int bus_add_device(struct device *dev);
172 void bus_probe_device(struct device *dev);
173 void bus_remove_device(struct device *dev);
174 void bus_notify(struct device *dev, enum bus_notifier_event value);
175 bool bus_is_registered(const struct bus_type *bus);
176 
177 int bus_add_driver(struct device_driver *drv);
178 void bus_remove_driver(struct device_driver *drv);
179 void device_release_driver_internal(struct device *dev, const struct device_driver *drv,
180 				    struct device *parent);
181 
182 void driver_detach(const struct device_driver *drv);
183 void driver_deferred_probe_del(struct device *dev);
184 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf);
185 static inline int driver_match_device(const struct device_driver *drv,
186 				      struct device *dev)
187 {
188 	return drv->bus->match ? drv->bus->match(dev, drv) : 1;
189 }
190 
191 static inline void dev_sync_state(struct device *dev)
192 {
193 	if (dev->bus->sync_state)
194 		dev->bus->sync_state(dev);
195 	else if (dev->driver && dev->driver->sync_state)
196 		dev->driver->sync_state(dev);
197 }
198 
199 int driver_add_groups(const struct device_driver *drv, const struct attribute_group **groups);
200 void driver_remove_groups(const struct device_driver *drv, const struct attribute_group **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