class.c (cecdd52a3dd312564f81a39df08378b7b39a2654) class.c (75a2d4226b53710380d1017b3f4c88f937ddba78)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * class.c - basic device class management
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2003-2004 Greg Kroah-Hartman
8 * Copyright (c) 2003-2004 IBM Corp.

--- 73 unchanged lines hidden (view full) ---

82 .release = class_release,
83 .child_ns_type = class_child_ns_type,
84};
85
86/* Hotplug events for classes go to the class subsys */
87static struct kset *class_kset;
88
89
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * class.c - basic device class management
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2003-2004 Greg Kroah-Hartman
8 * Copyright (c) 2003-2004 IBM Corp.

--- 73 unchanged lines hidden (view full) ---

82 .release = class_release,
83 .child_ns_type = class_child_ns_type,
84};
85
86/* Hotplug events for classes go to the class subsys */
87static struct kset *class_kset;
88
89
90int class_create_file_ns(struct class *cls, const struct class_attribute *attr,
90int class_create_file_ns(const struct class *cls, const struct class_attribute *attr,
91 const void *ns)
92{
93 int error;
94
95 if (cls)
96 error = sysfs_create_file_ns(&cls->p->subsys.kobj,
97 &attr->attr, ns);
98 else
99 error = -EINVAL;
100 return error;
101}
102EXPORT_SYMBOL_GPL(class_create_file_ns);
103
91 const void *ns)
92{
93 int error;
94
95 if (cls)
96 error = sysfs_create_file_ns(&cls->p->subsys.kobj,
97 &attr->attr, ns);
98 else
99 error = -EINVAL;
100 return error;
101}
102EXPORT_SYMBOL_GPL(class_create_file_ns);
103
104void class_remove_file_ns(struct class *cls, const struct class_attribute *attr,
104void class_remove_file_ns(const struct class *cls, const struct class_attribute *attr,
105 const void *ns)
106{
107 if (cls)
108 sysfs_remove_file_ns(&cls->p->subsys.kobj, &attr->attr, ns);
109}
110EXPORT_SYMBOL_GPL(class_remove_file_ns);
111
112static struct class *class_get(struct class *cls)

--- 24 unchanged lines hidden (view full) ---

137
138static void klist_class_dev_put(struct klist_node *n)
139{
140 struct device *dev = klist_class_to_dev(n);
141
142 put_device(dev);
143}
144
105 const void *ns)
106{
107 if (cls)
108 sysfs_remove_file_ns(&cls->p->subsys.kobj, &attr->attr, ns);
109}
110EXPORT_SYMBOL_GPL(class_remove_file_ns);
111
112static struct class *class_get(struct class *cls)

--- 24 unchanged lines hidden (view full) ---

137
138static void klist_class_dev_put(struct klist_node *n)
139{
140 struct device *dev = klist_class_to_dev(n);
141
142 put_device(dev);
143}
144
145static int class_add_groups(struct class *cls,
145static int class_add_groups(const struct class *cls,
146 const struct attribute_group **groups)
147{
148 return sysfs_create_groups(&cls->p->subsys.kobj, groups);
149}
150
146 const struct attribute_group **groups)
147{
148 return sysfs_create_groups(&cls->p->subsys.kobj, groups);
149}
150
151static void class_remove_groups(struct class *cls,
151static void class_remove_groups(const struct class *cls,
152 const struct attribute_group **groups)
153{
154 return sysfs_remove_groups(&cls->p->subsys.kobj, groups);
155}
156
152 const struct attribute_group **groups)
153{
154 return sysfs_remove_groups(&cls->p->subsys.kobj, groups);
155}
156
157int __class_register(struct class *cls, struct lock_class_key *key)
157int class_register(struct class *cls)
158{
159 struct subsys_private *cp;
158{
159 struct subsys_private *cp;
160 struct lock_class_key *key;
160 int error;
161
162 pr_debug("device class '%s': registering\n", cls->name);
163
164 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
165 if (!cp)
166 return -ENOMEM;
167 klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
168 INIT_LIST_HEAD(&cp->interfaces);
169 kset_init(&cp->glue_dirs);
161 int error;
162
163 pr_debug("device class '%s': registering\n", cls->name);
164
165 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
166 if (!cp)
167 return -ENOMEM;
168 klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
169 INIT_LIST_HEAD(&cp->interfaces);
170 kset_init(&cp->glue_dirs);
171 key = &cp->lock_key;
172 lockdep_register_key(key);
170 __mutex_init(&cp->mutex, "subsys mutex", key);
171 error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
172 if (error) {
173 kfree(cp);
174 return error;
175 }
176
177 /* set the default /sys/dev directory for devices of this class */
178 if (!cls->dev_kobj)
179 cls->dev_kobj = sysfs_dev_char_kobj;
180
173 __mutex_init(&cp->mutex, "subsys mutex", key);
174 error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
175 if (error) {
176 kfree(cp);
177 return error;
178 }
179
180 /* set the default /sys/dev directory for devices of this class */
181 if (!cls->dev_kobj)
182 cls->dev_kobj = sysfs_dev_char_kobj;
183
181#if defined(CONFIG_BLOCK)
182 /* let the block class directory show up in the root of sysfs */
183 if (!sysfs_deprecated || cls != &block_class)
184 cp->subsys.kobj.kset = class_kset;
185#else
186 cp->subsys.kobj.kset = class_kset;
184 cp->subsys.kobj.kset = class_kset;
187#endif
188 cp->subsys.kobj.ktype = &class_ktype;
189 cp->class = cls;
190 cls->p = cp;
191
192 error = kset_register(&cp->subsys);
193 if (error)
194 goto err_out;
195

--- 6 unchanged lines hidden (view full) ---

202 }
203 return 0;
204
205err_out:
206 kfree(cp);
207 cls->p = NULL;
208 return error;
209}
185 cp->subsys.kobj.ktype = &class_ktype;
186 cp->class = cls;
187 cls->p = cp;
188
189 error = kset_register(&cp->subsys);
190 if (error)
191 goto err_out;
192

--- 6 unchanged lines hidden (view full) ---

199 }
200 return 0;
201
202err_out:
203 kfree(cp);
204 cls->p = NULL;
205 return error;
206}
210EXPORT_SYMBOL_GPL(__class_register);
207EXPORT_SYMBOL_GPL(class_register);
211
208
212void class_unregister(struct class *cls)
209void class_unregister(const struct class *cls)
213{
214 pr_debug("device class '%s': unregistering\n", cls->name);
215 class_remove_groups(cls, cls->class_groups);
216 kset_unregister(&cls->p->subsys);
217}
218EXPORT_SYMBOL_GPL(class_unregister);
219
220static void class_create_release(struct class *cls)
221{
222 pr_debug("%s called for %s\n", __func__, cls->name);
223 kfree(cls);
224}
225
226/**
210{
211 pr_debug("device class '%s': unregistering\n", cls->name);
212 class_remove_groups(cls, cls->class_groups);
213 kset_unregister(&cls->p->subsys);
214}
215EXPORT_SYMBOL_GPL(class_unregister);
216
217static void class_create_release(struct class *cls)
218{
219 pr_debug("%s called for %s\n", __func__, cls->name);
220 kfree(cls);
221}
222
223/**
227 * __class_create - create a struct class structure
228 * @owner: pointer to the module that is to "own" this struct class
224 * class_create - create a struct class structure
229 * @name: pointer to a string for the name of this class.
225 * @name: pointer to a string for the name of this class.
230 * @key: the lock_class_key for this class; used by mutex lock debugging
231 *
232 * This is used to create a struct class pointer that can then be used
233 * in calls to device_create().
234 *
235 * Returns &struct class pointer on success, or ERR_PTR() on error.
236 *
237 * Note, the pointer created here is to be destroyed when finished by
238 * making a call to class_destroy().
239 */
226 *
227 * This is used to create a struct class pointer that can then be used
228 * in calls to device_create().
229 *
230 * Returns &struct class pointer on success, or ERR_PTR() on error.
231 *
232 * Note, the pointer created here is to be destroyed when finished by
233 * making a call to class_destroy().
234 */
240struct class *__class_create(struct module *owner, const char *name,
241 struct lock_class_key *key)
235struct class *class_create(const char *name)
242{
243 struct class *cls;
244 int retval;
245
246 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
247 if (!cls) {
248 retval = -ENOMEM;
249 goto error;
250 }
251
252 cls->name = name;
236{
237 struct class *cls;
238 int retval;
239
240 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
241 if (!cls) {
242 retval = -ENOMEM;
243 goto error;
244 }
245
246 cls->name = name;
253 cls->owner = owner;
254 cls->class_release = class_create_release;
255
247 cls->class_release = class_create_release;
248
256 retval = __class_register(cls, key);
249 retval = class_register(cls);
257 if (retval)
258 goto error;
259
260 return cls;
261
262error:
263 kfree(cls);
264 return ERR_PTR(retval);
265}
250 if (retval)
251 goto error;
252
253 return cls;
254
255error:
256 kfree(cls);
257 return ERR_PTR(retval);
258}
266EXPORT_SYMBOL_GPL(__class_create);
259EXPORT_SYMBOL_GPL(class_create);
267
268/**
269 * class_destroy - destroys a struct class structure
270 * @cls: pointer to the struct class that is to be destroyed
271 *
272 * Note, the pointer to be destroyed must have been created with a call
273 * to class_create().
274 */
260
261/**
262 * class_destroy - destroys a struct class structure
263 * @cls: pointer to the struct class that is to be destroyed
264 *
265 * Note, the pointer to be destroyed must have been created with a call
266 * to class_create().
267 */
275void class_destroy(struct class *cls)
268void class_destroy(const struct class *cls)
276{
277 if (IS_ERR_OR_NULL(cls))
278 return;
279
280 class_unregister(cls);
281}
282EXPORT_SYMBOL_GPL(class_destroy);
283

--- 4 unchanged lines hidden (view full) ---

288 * @start: the device to start iterating from, if any
289 * @type: device_type of the devices to iterate over, NULL for all
290 *
291 * Initialize class iterator @iter such that it iterates over devices
292 * of @class. If @start is set, the list iteration will start there,
293 * otherwise if it is NULL, the iteration starts at the beginning of
294 * the list.
295 */
269{
270 if (IS_ERR_OR_NULL(cls))
271 return;
272
273 class_unregister(cls);
274}
275EXPORT_SYMBOL_GPL(class_destroy);
276

--- 4 unchanged lines hidden (view full) ---

281 * @start: the device to start iterating from, if any
282 * @type: device_type of the devices to iterate over, NULL for all
283 *
284 * Initialize class iterator @iter such that it iterates over devices
285 * of @class. If @start is set, the list iteration will start there,
286 * otherwise if it is NULL, the iteration starts at the beginning of
287 * the list.
288 */
296void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
297 struct device *start, const struct device_type *type)
289void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
290 const struct device *start, const struct device_type *type)
298{
299 struct klist_node *start_knode = NULL;
300
301 if (start)
302 start_knode = &start->p->knode_class;
303 klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
304 iter->type = type;
305}

--- 53 unchanged lines hidden (view full) ---

359 * beginning of the list.
360 *
361 * We check the return of @fn each time. If it returns anything
362 * other than 0, we break out and return that value.
363 *
364 * @fn is allowed to do anything including calling back into class
365 * code. There's no locking restriction.
366 */
291{
292 struct klist_node *start_knode = NULL;
293
294 if (start)
295 start_knode = &start->p->knode_class;
296 klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
297 iter->type = type;
298}

--- 53 unchanged lines hidden (view full) ---

352 * beginning of the list.
353 *
354 * We check the return of @fn each time. If it returns anything
355 * other than 0, we break out and return that value.
356 *
357 * @fn is allowed to do anything including calling back into class
358 * code. There's no locking restriction.
359 */
367int class_for_each_device(struct class *class, struct device *start,
360int class_for_each_device(const struct class *class, const struct device *start,
368 void *data, int (*fn)(struct device *, void *))
369{
370 struct class_dev_iter iter;
371 struct device *dev;
372 int error = 0;
373
374 if (!class)
375 return -EINVAL;

--- 30 unchanged lines hidden (view full) ---

406 * if it does. If the callback returns non-zero, this function will
407 * return to the caller and not iterate over any more devices.
408 *
409 * Note, you will need to drop the reference with put_device() after use.
410 *
411 * @match is allowed to do anything including calling back into class
412 * code. There's no locking restriction.
413 */
361 void *data, int (*fn)(struct device *, void *))
362{
363 struct class_dev_iter iter;
364 struct device *dev;
365 int error = 0;
366
367 if (!class)
368 return -EINVAL;

--- 30 unchanged lines hidden (view full) ---

399 * if it does. If the callback returns non-zero, this function will
400 * return to the caller and not iterate over any more devices.
401 *
402 * Note, you will need to drop the reference with put_device() after use.
403 *
404 * @match is allowed to do anything including calling back into class
405 * code. There's no locking restriction.
406 */
414struct device *class_find_device(struct class *class, struct device *start,
407struct device *class_find_device(const struct class *class, const struct device *start,
415 const void *data,
416 int (*match)(struct device *, const void *))
417{
418 struct class_dev_iter iter;
419 struct device *dev;
420
421 if (!class)
422 return NULL;

--- 61 unchanged lines hidden (view full) ---

484 class_dev_iter_exit(&iter);
485 }
486 mutex_unlock(&parent->p->mutex);
487
488 class_put(parent);
489}
490EXPORT_SYMBOL_GPL(class_interface_unregister);
491
408 const void *data,
409 int (*match)(struct device *, const void *))
410{
411 struct class_dev_iter iter;
412 struct device *dev;
413
414 if (!class)
415 return NULL;

--- 61 unchanged lines hidden (view full) ---

477 class_dev_iter_exit(&iter);
478 }
479 mutex_unlock(&parent->p->mutex);
480
481 class_put(parent);
482}
483EXPORT_SYMBOL_GPL(class_interface_unregister);
484
492ssize_t show_class_attr_string(struct class *class,
493 struct class_attribute *attr, char *buf)
485ssize_t show_class_attr_string(const struct class *class,
486 const struct class_attribute *attr, char *buf)
494{
495 struct class_attribute_string *cs;
496
497 cs = container_of(attr, struct class_attribute_string, attr);
498 return sysfs_emit(buf, "%s\n", cs->str);
499}
500
501EXPORT_SYMBOL_GPL(show_class_attr_string);

--- 95 unchanged lines hidden ---
487{
488 struct class_attribute_string *cs;
489
490 cs = container_of(attr, struct class_attribute_string, attr);
491 return sysfs_emit(buf, "%s\n", cs->str);
492}
493
494EXPORT_SYMBOL_GPL(show_class_attr_string);

--- 95 unchanged lines hidden ---