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