xref: /OK3568_Linux_fs/kernel/drivers/base/base.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
4*4882a593Smuzhiyun  * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
5*4882a593Smuzhiyun  * Copyright (c) 2008-2012 Novell Inc.
6*4882a593Smuzhiyun  * Copyright (c) 2012-2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
7*4882a593Smuzhiyun  * Copyright (c) 2012-2019 Linux Foundation
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Core driver model functions and structures that should not be
10*4882a593Smuzhiyun  * shared outside of the drivers/base/ directory.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun #include <linux/notifier.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /**
16*4882a593Smuzhiyun  * struct subsys_private - structure to hold the private to the driver core portions of the bus_type/class structure.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * @subsys - the struct kset that defines this subsystem
19*4882a593Smuzhiyun  * @devices_kset - the subsystem's 'devices' directory
20*4882a593Smuzhiyun  * @interfaces - list of subsystem interfaces associated
21*4882a593Smuzhiyun  * @mutex - protect the devices, and interfaces lists.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * @drivers_kset - the list of drivers associated
24*4882a593Smuzhiyun  * @klist_devices - the klist to iterate over the @devices_kset
25*4882a593Smuzhiyun  * @klist_drivers - the klist to iterate over the @drivers_kset
26*4882a593Smuzhiyun  * @bus_notifier - the bus notifier list for anything that cares about things
27*4882a593Smuzhiyun  *                 on this bus.
28*4882a593Smuzhiyun  * @bus - pointer back to the struct bus_type that this structure is associated
29*4882a593Smuzhiyun  *        with.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * @glue_dirs - "glue" directory to put in-between the parent device to
32*4882a593Smuzhiyun  *              avoid namespace conflicts
33*4882a593Smuzhiyun  * @class - pointer back to the struct class that this structure is associated
34*4882a593Smuzhiyun  *          with.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * This structure is the one that is the actual kobject allowing struct
37*4882a593Smuzhiyun  * bus_type/class to be statically allocated safely.  Nothing outside of the
38*4882a593Smuzhiyun  * driver core should ever touch these fields.
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun struct subsys_private {
41*4882a593Smuzhiyun 	struct kset subsys;
42*4882a593Smuzhiyun 	struct kset *devices_kset;
43*4882a593Smuzhiyun 	struct list_head interfaces;
44*4882a593Smuzhiyun 	struct mutex mutex;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	struct kset *drivers_kset;
47*4882a593Smuzhiyun 	struct klist klist_devices;
48*4882a593Smuzhiyun 	struct klist klist_drivers;
49*4882a593Smuzhiyun 	struct blocking_notifier_head bus_notifier;
50*4882a593Smuzhiyun 	unsigned int drivers_autoprobe:1;
51*4882a593Smuzhiyun 	struct bus_type *bus;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	struct kset glue_dirs;
54*4882a593Smuzhiyun 	struct class *class;
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun #define to_subsys_private(obj) container_of(obj, struct subsys_private, subsys.kobj)
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun struct driver_private {
59*4882a593Smuzhiyun 	struct kobject kobj;
60*4882a593Smuzhiyun 	struct klist klist_devices;
61*4882a593Smuzhiyun 	struct klist_node knode_bus;
62*4882a593Smuzhiyun 	struct module_kobject *mkobj;
63*4882a593Smuzhiyun 	struct device_driver *driver;
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun #define to_driver(obj) container_of(obj, struct driver_private, kobj)
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /**
68*4882a593Smuzhiyun  * struct device_private - structure to hold the private to the driver core portions of the device structure.
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * @klist_children - klist containing all children of this device
71*4882a593Smuzhiyun  * @knode_parent - node in sibling list
72*4882a593Smuzhiyun  * @knode_driver - node in driver list
73*4882a593Smuzhiyun  * @knode_bus - node in bus list
74*4882a593Smuzhiyun  * @knode_class - node in class list
75*4882a593Smuzhiyun  * @deferred_probe - entry in deferred_probe_list which is used to retry the
76*4882a593Smuzhiyun  *	binding of drivers which were unable to get all the resources needed by
77*4882a593Smuzhiyun  *	the device; typically because it depends on another driver getting
78*4882a593Smuzhiyun  *	probed first.
79*4882a593Smuzhiyun  * @async_driver - pointer to device driver awaiting probe via async_probe
80*4882a593Smuzhiyun  * @device - pointer back to the struct device that this structure is
81*4882a593Smuzhiyun  * associated with.
82*4882a593Smuzhiyun  * @dead - This device is currently either in the process of or has been
83*4882a593Smuzhiyun  *	removed from the system. Any asynchronous events scheduled for this
84*4882a593Smuzhiyun  *	device should exit without taking any action.
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  * Nothing outside of the driver core should ever touch these fields.
87*4882a593Smuzhiyun  */
88*4882a593Smuzhiyun struct device_private {
89*4882a593Smuzhiyun 	struct klist klist_children;
90*4882a593Smuzhiyun 	struct klist_node knode_parent;
91*4882a593Smuzhiyun 	struct klist_node knode_driver;
92*4882a593Smuzhiyun 	struct klist_node knode_bus;
93*4882a593Smuzhiyun 	struct klist_node knode_class;
94*4882a593Smuzhiyun 	struct list_head deferred_probe;
95*4882a593Smuzhiyun 	struct device_driver *async_driver;
96*4882a593Smuzhiyun 	char *deferred_probe_reason;
97*4882a593Smuzhiyun 	struct device *device;
98*4882a593Smuzhiyun 	u8 dead:1;
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun #define to_device_private_parent(obj)	\
101*4882a593Smuzhiyun 	container_of(obj, struct device_private, knode_parent)
102*4882a593Smuzhiyun #define to_device_private_driver(obj)	\
103*4882a593Smuzhiyun 	container_of(obj, struct device_private, knode_driver)
104*4882a593Smuzhiyun #define to_device_private_bus(obj)	\
105*4882a593Smuzhiyun 	container_of(obj, struct device_private, knode_bus)
106*4882a593Smuzhiyun #define to_device_private_class(obj)	\
107*4882a593Smuzhiyun 	container_of(obj, struct device_private, knode_class)
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /* initialisation functions */
110*4882a593Smuzhiyun extern int devices_init(void);
111*4882a593Smuzhiyun extern int buses_init(void);
112*4882a593Smuzhiyun extern int classes_init(void);
113*4882a593Smuzhiyun extern int firmware_init(void);
114*4882a593Smuzhiyun #ifdef CONFIG_SYS_HYPERVISOR
115*4882a593Smuzhiyun extern int hypervisor_init(void);
116*4882a593Smuzhiyun #else
hypervisor_init(void)117*4882a593Smuzhiyun static inline int hypervisor_init(void) { return 0; }
118*4882a593Smuzhiyun #endif
119*4882a593Smuzhiyun extern int platform_bus_init(void);
120*4882a593Smuzhiyun extern void cpu_dev_init(void);
121*4882a593Smuzhiyun extern void container_dev_init(void);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun struct kobject *virtual_device_parent(struct device *dev);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun extern int bus_add_device(struct device *dev);
126*4882a593Smuzhiyun extern void bus_probe_device(struct device *dev);
127*4882a593Smuzhiyun extern void bus_remove_device(struct device *dev);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun extern int bus_add_driver(struct device_driver *drv);
130*4882a593Smuzhiyun extern void bus_remove_driver(struct device_driver *drv);
131*4882a593Smuzhiyun extern void device_release_driver_internal(struct device *dev,
132*4882a593Smuzhiyun 					   struct device_driver *drv,
133*4882a593Smuzhiyun 					   struct device *parent);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun extern void driver_detach(struct device_driver *drv);
136*4882a593Smuzhiyun extern int driver_probe_device(struct device_driver *drv, struct device *dev);
137*4882a593Smuzhiyun extern void driver_deferred_probe_del(struct device *dev);
138*4882a593Smuzhiyun extern void device_set_deferred_probe_reason(const struct device *dev,
139*4882a593Smuzhiyun 					     struct va_format *vaf);
driver_match_device(struct device_driver * drv,struct device * dev)140*4882a593Smuzhiyun static inline int driver_match_device(struct device_driver *drv,
141*4882a593Smuzhiyun 				      struct device *dev)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	return drv->bus->match ? drv->bus->match(dev, drv) : 1;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun extern bool driver_allows_async_probing(struct device_driver *drv);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun extern int driver_add_groups(struct device_driver *drv,
148*4882a593Smuzhiyun 			     const struct attribute_group **groups);
149*4882a593Smuzhiyun extern void driver_remove_groups(struct device_driver *drv,
150*4882a593Smuzhiyun 				 const struct attribute_group **groups);
151*4882a593Smuzhiyun int device_driver_attach(struct device_driver *drv, struct device *dev);
152*4882a593Smuzhiyun void device_driver_detach(struct device *dev);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun extern char *make_class_name(const char *name, struct kobject *kobj);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun extern int devres_release_all(struct device *dev);
157*4882a593Smuzhiyun extern void device_block_probing(void);
158*4882a593Smuzhiyun extern void device_unblock_probing(void);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /* /sys/devices directory */
161*4882a593Smuzhiyun extern struct kset *devices_kset;
162*4882a593Smuzhiyun extern void devices_kset_move_last(struct device *dev);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun #if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
165*4882a593Smuzhiyun extern void module_add_driver(struct module *mod, struct device_driver *drv);
166*4882a593Smuzhiyun extern void module_remove_driver(struct device_driver *drv);
167*4882a593Smuzhiyun #else
module_add_driver(struct module * mod,struct device_driver * drv)168*4882a593Smuzhiyun static inline void module_add_driver(struct module *mod,
169*4882a593Smuzhiyun 				     struct device_driver *drv) { }
module_remove_driver(struct device_driver * drv)170*4882a593Smuzhiyun static inline void module_remove_driver(struct device_driver *drv) { }
171*4882a593Smuzhiyun #endif
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun #ifdef CONFIG_DEVTMPFS
174*4882a593Smuzhiyun extern int devtmpfs_init(void);
175*4882a593Smuzhiyun #else
devtmpfs_init(void)176*4882a593Smuzhiyun static inline int devtmpfs_init(void) { return 0; }
177*4882a593Smuzhiyun #endif
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /* Device links support */
180*4882a593Smuzhiyun extern int device_links_read_lock(void);
181*4882a593Smuzhiyun extern void device_links_read_unlock(int idx);
182*4882a593Smuzhiyun extern int device_links_read_lock_held(void);
183*4882a593Smuzhiyun extern int device_links_check_suppliers(struct device *dev);
184*4882a593Smuzhiyun extern void device_links_driver_bound(struct device *dev);
185*4882a593Smuzhiyun extern void device_links_driver_cleanup(struct device *dev);
186*4882a593Smuzhiyun extern void device_links_no_driver(struct device *dev);
187*4882a593Smuzhiyun extern bool device_links_busy(struct device *dev);
188*4882a593Smuzhiyun extern void device_links_unbind_consumers(struct device *dev);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /* device pm support */
191*4882a593Smuzhiyun void device_pm_move_to_tail(struct device *dev);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun #ifdef CONFIG_DEVTMPFS
194*4882a593Smuzhiyun int devtmpfs_create_node(struct device *dev);
195*4882a593Smuzhiyun int devtmpfs_delete_node(struct device *dev);
196*4882a593Smuzhiyun #else
devtmpfs_create_node(struct device * dev)197*4882a593Smuzhiyun static inline int devtmpfs_create_node(struct device *dev) { return 0; }
devtmpfs_delete_node(struct device * dev)198*4882a593Smuzhiyun static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
199*4882a593Smuzhiyun #endif
200