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