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