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