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 <[email protected]> 6 * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]> 7 * Copyright (c) 2008-2009 Novell Inc. 8 * Copyright (c) 2012-2019 Greg Kroah-Hartman <[email protected]> 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)(const struct device *dev, struct kobj_uevent_env *env); 63 char *(*devnode)(const 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)(const struct device *dev); 72 73 void (*get_ownership)(const 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 module *owner, 89 struct lock_class_key *key); 90 extern void class_unregister(struct class *class); 91 92 /* This is a #define to keep the compiler from merging different 93 * instances of the __key variable */ 94 #define class_register(class) \ 95 ({ \ 96 static struct lock_class_key __key; \ 97 __class_register(class, THIS_MODULE, &__key); \ 98 }) 99 100 struct class_compat; 101 struct class_compat *class_compat_register(const char *name); 102 void class_compat_unregister(struct class_compat *cls); 103 int class_compat_create_link(struct class_compat *cls, struct device *dev, 104 struct device *device_link); 105 void class_compat_remove_link(struct class_compat *cls, struct device *dev, 106 struct device *device_link); 107 108 extern void class_dev_iter_init(struct class_dev_iter *iter, 109 struct class *class, 110 struct device *start, 111 const struct device_type *type); 112 extern struct device *class_dev_iter_next(struct class_dev_iter *iter); 113 extern void class_dev_iter_exit(struct class_dev_iter *iter); 114 115 extern int class_for_each_device(struct class *class, struct device *start, 116 void *data, 117 int (*fn)(struct device *dev, void *data)); 118 extern struct device *class_find_device(struct class *class, 119 struct device *start, const void *data, 120 int (*match)(struct device *, const void *)); 121 122 /** 123 * class_find_device_by_name - device iterator for locating a particular device 124 * of a specific name. 125 * @class: class type 126 * @name: name of the device to match 127 */ 128 static inline struct device *class_find_device_by_name(struct class *class, 129 const char *name) 130 { 131 return class_find_device(class, NULL, name, device_match_name); 132 } 133 134 /** 135 * class_find_device_by_of_node : device iterator for locating a particular device 136 * matching the of_node. 137 * @class: class type 138 * @np: of_node of the device to match. 139 */ 140 static inline struct device * 141 class_find_device_by_of_node(struct class *class, const struct device_node *np) 142 { 143 return class_find_device(class, NULL, np, device_match_of_node); 144 } 145 146 /** 147 * class_find_device_by_fwnode : device iterator for locating a particular device 148 * matching the fwnode. 149 * @class: class type 150 * @fwnode: fwnode of the device to match. 151 */ 152 static inline struct device * 153 class_find_device_by_fwnode(struct class *class, 154 const struct fwnode_handle *fwnode) 155 { 156 return class_find_device(class, NULL, fwnode, device_match_fwnode); 157 } 158 159 /** 160 * class_find_device_by_devt : device iterator for locating a particular device 161 * matching the device type. 162 * @class: class type 163 * @devt: device type of the device to match. 164 */ 165 static inline struct device *class_find_device_by_devt(struct class *class, 166 dev_t devt) 167 { 168 return class_find_device(class, NULL, &devt, device_match_devt); 169 } 170 171 #ifdef CONFIG_ACPI 172 struct acpi_device; 173 /** 174 * class_find_device_by_acpi_dev : device iterator for locating a particular 175 * device matching the ACPI_COMPANION device. 176 * @class: class type 177 * @adev: ACPI_COMPANION device to match. 178 */ 179 static inline struct device * 180 class_find_device_by_acpi_dev(struct class *class, const struct acpi_device *adev) 181 { 182 return class_find_device(class, NULL, adev, device_match_acpi_dev); 183 } 184 #else 185 static inline struct device * 186 class_find_device_by_acpi_dev(struct class *class, const void *adev) 187 { 188 return NULL; 189 } 190 #endif 191 192 struct class_attribute { 193 struct attribute attr; 194 ssize_t (*show)(struct class *class, struct class_attribute *attr, 195 char *buf); 196 ssize_t (*store)(struct class *class, struct class_attribute *attr, 197 const char *buf, size_t count); 198 }; 199 200 #define CLASS_ATTR_RW(_name) \ 201 struct class_attribute class_attr_##_name = __ATTR_RW(_name) 202 #define CLASS_ATTR_RO(_name) \ 203 struct class_attribute class_attr_##_name = __ATTR_RO(_name) 204 #define CLASS_ATTR_WO(_name) \ 205 struct class_attribute class_attr_##_name = __ATTR_WO(_name) 206 207 extern int __must_check class_create_file_ns(struct class *class, 208 const struct class_attribute *attr, 209 const void *ns); 210 extern void class_remove_file_ns(struct class *class, 211 const struct class_attribute *attr, 212 const void *ns); 213 214 static inline int __must_check class_create_file(struct class *class, 215 const struct class_attribute *attr) 216 { 217 return class_create_file_ns(class, attr, NULL); 218 } 219 220 static inline void class_remove_file(struct class *class, 221 const struct class_attribute *attr) 222 { 223 return class_remove_file_ns(class, attr, NULL); 224 } 225 226 /* Simple class attribute that is just a static string */ 227 struct class_attribute_string { 228 struct class_attribute attr; 229 char *str; 230 }; 231 232 /* Currently read-only only */ 233 #define _CLASS_ATTR_STRING(_name, _mode, _str) \ 234 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str } 235 #define CLASS_ATTR_STRING(_name, _mode, _str) \ 236 struct class_attribute_string class_attr_##_name = \ 237 _CLASS_ATTR_STRING(_name, _mode, _str) 238 239 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr, 240 char *buf); 241 242 struct class_interface { 243 struct list_head node; 244 struct class *class; 245 246 int (*add_dev) (struct device *, struct class_interface *); 247 void (*remove_dev) (struct device *, struct class_interface *); 248 }; 249 250 extern int __must_check class_interface_register(struct class_interface *); 251 extern void class_interface_unregister(struct class_interface *); 252 253 extern struct class * __must_check __class_create(struct module *owner, 254 const char *name, 255 struct lock_class_key *key); 256 extern void class_destroy(struct class *cls); 257 258 /* This is a #define to keep the compiler from merging different 259 * instances of the __key variable */ 260 261 /** 262 * class_create - create a struct class structure 263 * @owner: pointer to the module that is to "own" this struct class 264 * @name: pointer to a string for the name of this class. 265 * 266 * This is used to create a struct class pointer that can then be used 267 * in calls to device_create(). 268 * 269 * Returns &struct class pointer on success, or ERR_PTR() on error. 270 * 271 * Note, the pointer created here is to be destroyed when finished by 272 * making a call to class_destroy(). 273 */ 274 #define class_create(owner, name) \ 275 ({ \ 276 static struct lock_class_key __key; \ 277 __class_create(owner, name, &__key); \ 278 }) 279 280 281 #endif /* _DEVICE_CLASS_H_ */ 282