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 <mochel@osdl.org>
6  * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
7  * Copyright (c) 2008-2009 Novell Inc.
8  * Copyright (c) 2012-2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
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 #include <linux/android_kabi.h>
22 
23 struct device;
24 struct fwnode_handle;
25 
26 /**
27  * struct class - device classes
28  * @name:	Name of the class.
29  * @class_groups: Default attributes of this class.
30  * @dev_groups:	Default attributes of the devices that belong to the class.
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  *
45  * A class is a higher-level view of a device that abstracts out low-level
46  * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
47  * at the class level, they are all simply disks. Classes allow user space
48  * to work with devices based on what they do, rather than how they are
49  * connected or how they work.
50  */
51 struct class {
52 	const char		*name;
53 
54 	const struct attribute_group	**class_groups;
55 	const struct attribute_group	**dev_groups;
56 
57 	int (*dev_uevent)(const struct device *dev, struct kobj_uevent_env *env);
58 	char *(*devnode)(const struct device *dev, umode_t *mode);
59 
60 	void (*class_release)(const struct class *class);
61 	void (*dev_release)(struct device *dev);
62 
63 	int (*shutdown_pre)(struct device *dev);
64 
65 	const struct kobj_ns_type_operations *ns_type;
66 	const void *(*namespace)(const struct device *dev);
67 
68 	void (*get_ownership)(const struct device *dev, kuid_t *uid, kgid_t *gid);
69 
70 	const struct dev_pm_ops *pm;
71 
72 	ANDROID_KABI_RESERVE(1);
73 	ANDROID_KABI_RESERVE(2);
74 	ANDROID_KABI_RESERVE(3);
75 	ANDROID_KABI_RESERVE(4);
76 };
77 
78 struct class_dev_iter {
79 	struct klist_iter		ki;
80 	const struct device_type	*type;
81 	struct subsys_private		*sp;
82 };
83 
84 int __must_check class_register(const struct class *class);
85 void class_unregister(const struct class *class);
86 bool class_is_registered(const struct class *class);
87 
88 struct class_compat;
89 struct class_compat *class_compat_register(const char *name);
90 void class_compat_unregister(struct class_compat *cls);
91 int class_compat_create_link(struct class_compat *cls, struct device *dev,
92 			     struct device *device_link);
93 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
94 			      struct device *device_link);
95 
96 void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
97 			 const struct device *start, const struct device_type *type);
98 struct device *class_dev_iter_next(struct class_dev_iter *iter);
99 void class_dev_iter_exit(struct class_dev_iter *iter);
100 
101 int class_for_each_device(const struct class *class, const struct device *start, void *data,
102 			  int (*fn)(struct device *dev, void *data));
103 struct device *class_find_device(const struct class *class, const struct device *start,
104 				 const void *data, device_match_t match);
105 
106 /**
107  * class_find_device_by_name - device iterator for locating a particular device
108  * of a specific name.
109  * @class: class type
110  * @name: name of the device to match
111  */
class_find_device_by_name(const struct class * class,const char * name)112 static inline struct device *class_find_device_by_name(const struct class *class,
113 						       const char *name)
114 {
115 	return class_find_device(class, NULL, name, device_match_name);
116 }
117 
118 /**
119  * class_find_device_by_of_node : device iterator for locating a particular device
120  * matching the of_node.
121  * @class: class type
122  * @np: of_node of the device to match.
123  */
class_find_device_by_of_node(const struct class * class,const struct device_node * np)124 static inline struct device *class_find_device_by_of_node(const struct class *class,
125 							  const struct device_node *np)
126 {
127 	return class_find_device(class, NULL, np, device_match_of_node);
128 }
129 
130 /**
131  * class_find_device_by_fwnode : device iterator for locating a particular device
132  * matching the fwnode.
133  * @class: class type
134  * @fwnode: fwnode of the device to match.
135  */
class_find_device_by_fwnode(const struct class * class,const struct fwnode_handle * fwnode)136 static inline struct device *class_find_device_by_fwnode(const struct class *class,
137 							 const struct fwnode_handle *fwnode)
138 {
139 	return class_find_device(class, NULL, fwnode, device_match_fwnode);
140 }
141 
142 /**
143  * class_find_device_by_devt : device iterator for locating a particular device
144  * matching the device type.
145  * @class: class type
146  * @devt: device type of the device to match.
147  */
class_find_device_by_devt(const struct class * class,dev_t devt)148 static inline struct device *class_find_device_by_devt(const struct class *class,
149 						       dev_t devt)
150 {
151 	return class_find_device(class, NULL, &devt, device_match_devt);
152 }
153 
154 #ifdef CONFIG_ACPI
155 struct acpi_device;
156 /**
157  * class_find_device_by_acpi_dev : device iterator for locating a particular
158  * device matching the ACPI_COMPANION device.
159  * @class: class type
160  * @adev: ACPI_COMPANION device to match.
161  */
class_find_device_by_acpi_dev(const struct class * class,const struct acpi_device * adev)162 static inline struct device *class_find_device_by_acpi_dev(const struct class *class,
163 							   const struct acpi_device *adev)
164 {
165 	return class_find_device(class, NULL, adev, device_match_acpi_dev);
166 }
167 #else
class_find_device_by_acpi_dev(const struct class * class,const void * adev)168 static inline struct device *class_find_device_by_acpi_dev(const struct class *class,
169 							   const void *adev)
170 {
171 	return NULL;
172 }
173 #endif
174 
175 struct class_attribute {
176 	struct attribute attr;
177 	ssize_t (*show)(const struct class *class, const struct class_attribute *attr,
178 			char *buf);
179 	ssize_t (*store)(const struct class *class, const struct class_attribute *attr,
180 			 const char *buf, size_t count);
181 };
182 
183 #define CLASS_ATTR_RW(_name) \
184 	struct class_attribute class_attr_##_name = __ATTR_RW(_name)
185 #define CLASS_ATTR_RO(_name) \
186 	struct class_attribute class_attr_##_name = __ATTR_RO(_name)
187 #define CLASS_ATTR_WO(_name) \
188 	struct class_attribute class_attr_##_name = __ATTR_WO(_name)
189 
190 int __must_check class_create_file_ns(const struct class *class, const struct class_attribute *attr,
191 				      const void *ns);
192 void class_remove_file_ns(const struct class *class, const struct class_attribute *attr,
193 			  const void *ns);
194 
class_create_file(const struct class * class,const struct class_attribute * attr)195 static inline int __must_check class_create_file(const struct class *class,
196 						 const struct class_attribute *attr)
197 {
198 	return class_create_file_ns(class, attr, NULL);
199 }
200 
class_remove_file(const struct class * class,const struct class_attribute * attr)201 static inline void class_remove_file(const struct class *class,
202 				     const struct class_attribute *attr)
203 {
204 	return class_remove_file_ns(class, attr, NULL);
205 }
206 
207 /* Simple class attribute that is just a static string */
208 struct class_attribute_string {
209 	struct class_attribute attr;
210 	char *str;
211 };
212 
213 /* Currently read-only only */
214 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
215 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
216 #define CLASS_ATTR_STRING(_name, _mode, _str) \
217 	struct class_attribute_string class_attr_##_name = \
218 		_CLASS_ATTR_STRING(_name, _mode, _str)
219 
220 ssize_t show_class_attr_string(const struct class *class, const struct class_attribute *attr,
221 			       char *buf);
222 
223 struct class_interface {
224 	struct list_head	node;
225 	const struct class	*class;
226 
227 	int (*add_dev)		(struct device *dev);
228 	void (*remove_dev)	(struct device *dev);
229 };
230 
231 int __must_check class_interface_register(struct class_interface *);
232 void class_interface_unregister(struct class_interface *);
233 
234 struct class * __must_check class_create(const char *name);
235 void class_destroy(const struct class *cls);
236 
237 #endif	/* _DEVICE_CLASS_H_ */
238