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