xref: /linux-6.15/include/linux/device/class.h (revision 69df024e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * The class-specific portions of the driver model
4  *
5  * Copyright (c) 2001-2003 Patrick Mochel <[email protected]>
6  * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]>
7  * Copyright (c) 2008-2009 Novell Inc.
8  * Copyright (c) 2012-2019 Greg Kroah-Hartman <[email protected]>
9  * Copyright (c) 2012-2019 Linux Foundation
10  *
11  * See Documentation/driver-api/driver-model/ for more information.
12  */
13 
14 #ifndef _DEVICE_CLASS_H_
15 #define _DEVICE_CLASS_H_
16 
17 #include <linux/kobject.h>
18 #include <linux/klist.h>
19 #include <linux/pm.h>
20 #include <linux/device/bus.h>
21 
22 struct device;
23 struct fwnode_handle;
24 
25 /**
26  * struct class - device classes
27  * @name:	Name of the class.
28  * @class_groups: Default attributes of this class.
29  * @dev_groups:	Default attributes of the devices that belong to the class.
30  * @dev_kobj:	The kobject that represents this class and links it into the hierarchy.
31  * @dev_uevent:	Called when a device is added, removed from this class, or a
32  *		few other things that generate uevents to add the environment
33  *		variables.
34  * @devnode:	Callback to provide the devtmpfs.
35  * @class_release: Called to release this class.
36  * @dev_release: Called to release the device.
37  * @shutdown_pre: Called at shut-down time before driver shutdown.
38  * @ns_type:	Callbacks so sysfs can detemine namespaces.
39  * @namespace:	Namespace of the device belongs to this class.
40  * @get_ownership: Allows class to specify uid/gid of the sysfs directories
41  *		for the devices belonging to the class. Usually tied to
42  *		device's namespace.
43  * @pm:		The default device power management operations of this class.
44  * @p:		The private data of the driver core, no one other than the
45  *		driver core can touch this.
46  *
47  * A class is a higher-level view of a device that abstracts out low-level
48  * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
49  * at the class level, they are all simply disks. Classes allow user space
50  * to work with devices based on what they do, rather than how they are
51  * connected or how they work.
52  */
53 struct class {
54 	const char		*name;
55 
56 	const struct attribute_group	**class_groups;
57 	const struct attribute_group	**dev_groups;
58 	struct kobject			*dev_kobj;
59 
60 	int (*dev_uevent)(const struct device *dev, struct kobj_uevent_env *env);
61 	char *(*devnode)(const struct device *dev, umode_t *mode);
62 
63 	void (*class_release)(struct class *class);
64 	void (*dev_release)(struct device *dev);
65 
66 	int (*shutdown_pre)(struct device *dev);
67 
68 	const struct kobj_ns_type_operations *ns_type;
69 	const void *(*namespace)(const struct device *dev);
70 
71 	void (*get_ownership)(const struct device *dev, kuid_t *uid, kgid_t *gid);
72 
73 	const struct dev_pm_ops *pm;
74 
75 	struct subsys_private *p;
76 };
77 
78 struct class_dev_iter {
79 	struct klist_iter		ki;
80 	const struct device_type	*type;
81 };
82 
83 extern struct kobject *sysfs_dev_block_kobj;
84 extern struct kobject *sysfs_dev_char_kobj;
85 extern int __must_check __class_register(struct class *class,
86 					 struct lock_class_key *key);
87 extern void class_unregister(struct class *class);
88 
89 /* This is a #define to keep the compiler from merging different
90  * instances of the __key variable */
91 #define class_register(class)			\
92 ({						\
93 	static struct lock_class_key __key;	\
94 	__class_register(class, &__key);	\
95 })
96 
97 struct class_compat;
98 struct class_compat *class_compat_register(const char *name);
99 void class_compat_unregister(struct class_compat *cls);
100 int class_compat_create_link(struct class_compat *cls, struct device *dev,
101 			     struct device *device_link);
102 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
103 			      struct device *device_link);
104 
105 extern void class_dev_iter_init(struct class_dev_iter *iter,
106 				const struct class *class,
107 				const struct device *start,
108 				const struct device_type *type);
109 extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
110 extern void class_dev_iter_exit(struct class_dev_iter *iter);
111 
112 extern int class_for_each_device(const struct class *class, const struct device *start,
113 				 void *data,
114 				 int (*fn)(struct device *dev, void *data));
115 extern struct device *class_find_device(struct class *class,
116 					struct device *start, const void *data,
117 					int (*match)(struct device *, const void *));
118 
119 /**
120  * class_find_device_by_name - device iterator for locating a particular device
121  * of a specific name.
122  * @class: class type
123  * @name: name of the device to match
124  */
125 static inline struct device *class_find_device_by_name(struct class *class,
126 						       const char *name)
127 {
128 	return class_find_device(class, NULL, name, device_match_name);
129 }
130 
131 /**
132  * class_find_device_by_of_node : device iterator for locating a particular device
133  * matching the of_node.
134  * @class: class type
135  * @np: of_node of the device to match.
136  */
137 static inline struct device *
138 class_find_device_by_of_node(struct class *class, const struct device_node *np)
139 {
140 	return class_find_device(class, NULL, np, device_match_of_node);
141 }
142 
143 /**
144  * class_find_device_by_fwnode : device iterator for locating a particular device
145  * matching the fwnode.
146  * @class: class type
147  * @fwnode: fwnode of the device to match.
148  */
149 static inline struct device *
150 class_find_device_by_fwnode(struct class *class,
151 			    const struct fwnode_handle *fwnode)
152 {
153 	return class_find_device(class, NULL, fwnode, device_match_fwnode);
154 }
155 
156 /**
157  * class_find_device_by_devt : device iterator for locating a particular device
158  * matching the device type.
159  * @class: class type
160  * @devt: device type of the device to match.
161  */
162 static inline struct device *class_find_device_by_devt(struct class *class,
163 						       dev_t devt)
164 {
165 	return class_find_device(class, NULL, &devt, device_match_devt);
166 }
167 
168 #ifdef CONFIG_ACPI
169 struct acpi_device;
170 /**
171  * class_find_device_by_acpi_dev : device iterator for locating a particular
172  * device matching the ACPI_COMPANION device.
173  * @class: class type
174  * @adev: ACPI_COMPANION device to match.
175  */
176 static inline struct device *
177 class_find_device_by_acpi_dev(struct class *class, const struct acpi_device *adev)
178 {
179 	return class_find_device(class, NULL, adev, device_match_acpi_dev);
180 }
181 #else
182 static inline struct device *
183 class_find_device_by_acpi_dev(struct class *class, const void *adev)
184 {
185 	return NULL;
186 }
187 #endif
188 
189 struct class_attribute {
190 	struct attribute attr;
191 	ssize_t (*show)(struct class *class, struct class_attribute *attr,
192 			char *buf);
193 	ssize_t (*store)(struct class *class, struct class_attribute *attr,
194 			const char *buf, size_t count);
195 };
196 
197 #define CLASS_ATTR_RW(_name) \
198 	struct class_attribute class_attr_##_name = __ATTR_RW(_name)
199 #define CLASS_ATTR_RO(_name) \
200 	struct class_attribute class_attr_##_name = __ATTR_RO(_name)
201 #define CLASS_ATTR_WO(_name) \
202 	struct class_attribute class_attr_##_name = __ATTR_WO(_name)
203 
204 extern int __must_check class_create_file_ns(struct class *class,
205 					     const struct class_attribute *attr,
206 					     const void *ns);
207 extern void class_remove_file_ns(struct class *class,
208 				 const struct class_attribute *attr,
209 				 const void *ns);
210 
211 static inline int __must_check class_create_file(struct class *class,
212 					const struct class_attribute *attr)
213 {
214 	return class_create_file_ns(class, attr, NULL);
215 }
216 
217 static inline void class_remove_file(struct class *class,
218 				     const struct class_attribute *attr)
219 {
220 	return class_remove_file_ns(class, attr, NULL);
221 }
222 
223 /* Simple class attribute that is just a static string */
224 struct class_attribute_string {
225 	struct class_attribute attr;
226 	char *str;
227 };
228 
229 /* Currently read-only only */
230 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
231 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
232 #define CLASS_ATTR_STRING(_name, _mode, _str) \
233 	struct class_attribute_string class_attr_##_name = \
234 		_CLASS_ATTR_STRING(_name, _mode, _str)
235 
236 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
237                         char *buf);
238 
239 struct class_interface {
240 	struct list_head	node;
241 	struct class		*class;
242 
243 	int (*add_dev)		(struct device *, struct class_interface *);
244 	void (*remove_dev)	(struct device *, struct class_interface *);
245 };
246 
247 extern int __must_check class_interface_register(struct class_interface *);
248 extern void class_interface_unregister(struct class_interface *);
249 
250 extern struct class * __must_check __class_create(const char *name,
251 						  struct lock_class_key *key);
252 extern void class_destroy(struct class *cls);
253 
254 /* This is a #define to keep the compiler from merging different
255  * instances of the __key variable */
256 
257 /**
258  * class_create - create a struct class structure
259  * @name: pointer to a string for the name of this class.
260  *
261  * This is used to create a struct class pointer that can then be used
262  * in calls to device_create().
263  *
264  * Returns &struct class pointer on success, or ERR_PTR() on error.
265  *
266  * Note, the pointer created here is to be destroyed when finished by
267  * making a call to class_destroy().
268  */
269 #define class_create(name)			\
270 ({						\
271 	static struct lock_class_key __key;	\
272 	__class_create(name, &__key);		\
273 })
274 
275 
276 #endif	/* _DEVICE_CLASS_H_ */
277