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