1 // SPDX-License-Identifier: GPL-2.0
3 * The class-specific portions of the driver model
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
11 * See Documentation/driver-api/driver-model/ for more information.
14 #ifndef _DEVICE_CLASS_H_
15 #define _DEVICE_CLASS_H_
17 #include <linux/kobject.h>
18 #include <linux/klist.h>
20 #include <linux/device/bus.h>
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
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
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.
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.
58 const struct attribute_group **class_groups;
59 const struct attribute_group **dev_groups;
60 struct kobject *dev_kobj;
62 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
63 char *(*devnode)(struct device *dev, umode_t *mode);
65 void (*class_release)(struct class *class);
66 void (*dev_release)(struct device *dev);
68 int (*shutdown_pre)(struct device *dev);
70 const struct kobj_ns_type_operations *ns_type;
71 const void *(*namespace)(struct device *dev);
73 void (*get_ownership)(struct device *dev, kuid_t *uid, kgid_t *gid);
75 const struct dev_pm_ops *pm;
77 struct subsys_private *p;
80 struct class_dev_iter {
82 const struct device_type *type;
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);
91 /* This is a #define to keep the compiler from merging different
92 * instances of the __key variable */
93 #define class_register(class) \
95 static struct lock_class_key __key; \
96 __class_register(class, &__key); \
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);
107 extern void class_dev_iter_init(struct class_dev_iter *iter,
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);
114 extern int class_for_each_device(struct class *class, struct device *start,
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 *));
122 * class_find_device_by_name - device iterator for locating a particular device
123 * of a specific name.
125 * @name: name of the device to match
127 static inline struct device *class_find_device_by_name(struct class *class,
130 return class_find_device(class, NULL, name, device_match_name);
134 * class_find_device_by_of_node : device iterator for locating a particular device
135 * matching the of_node.
137 * @np: of_node of the device to match.
139 static inline struct device *
140 class_find_device_by_of_node(struct class *class, const struct device_node *np)
142 return class_find_device(class, NULL, np, device_match_of_node);
146 * class_find_device_by_fwnode : device iterator for locating a particular device
147 * matching the fwnode.
149 * @fwnode: fwnode of the device to match.
151 static inline struct device *
152 class_find_device_by_fwnode(struct class *class,
153 const struct fwnode_handle *fwnode)
155 return class_find_device(class, NULL, fwnode, device_match_fwnode);
159 * class_find_device_by_devt : device iterator for locating a particular device
160 * matching the device type.
162 * @devt: device type of the device to match.
164 static inline struct device *class_find_device_by_devt(struct class *class,
167 return class_find_device(class, NULL, &devt, device_match_devt);
173 * class_find_device_by_acpi_dev : device iterator for locating a particular
174 * device matching the ACPI_COMPANION device.
176 * @adev: ACPI_COMPANION device to match.
178 static inline struct device *
179 class_find_device_by_acpi_dev(struct class *class, const struct acpi_device *adev)
181 return class_find_device(class, NULL, adev, device_match_acpi_dev);
184 static inline struct device *
185 class_find_device_by_acpi_dev(struct class *class, const void *adev)
191 struct class_attribute {
192 struct attribute attr;
193 ssize_t (*show)(struct class *class, struct class_attribute *attr,
195 ssize_t (*store)(struct class *class, struct class_attribute *attr,
196 const char *buf, size_t count);
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)
206 extern int __must_check class_create_file_ns(struct class *class,
207 const struct class_attribute *attr,
209 extern void class_remove_file_ns(struct class *class,
210 const struct class_attribute *attr,
213 static inline int __must_check class_create_file(struct class *class,
214 const struct class_attribute *attr)
216 return class_create_file_ns(class, attr, NULL);
219 static inline void class_remove_file(struct class *class,
220 const struct class_attribute *attr)
222 return class_remove_file_ns(class, attr, NULL);
225 /* Simple class attribute that is just a static string */
226 struct class_attribute_string {
227 struct class_attribute attr;
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)
238 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
241 struct class_interface {
242 struct list_head node;
245 int (*add_dev) (struct device *, struct class_interface *);
246 void (*remove_dev) (struct device *, struct class_interface *);
249 extern int __must_check class_interface_register(struct class_interface *);
250 extern void class_interface_unregister(struct class_interface *);
252 extern struct class * __must_check __class_create(struct module *owner,
254 struct lock_class_key *key);
255 extern void class_destroy(struct class *cls);
257 /* This is a #define to keep the compiler from merging different
258 * instances of the __key variable */
259 #define class_create(owner, name) \
261 static struct lock_class_key __key; \
262 __class_create(owner, name, &__key); \
266 #endif /* _DEVICE_CLASS_H_ */