Commit | Line | Data |
---|---|---|
989d42e8 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
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. | |
1da177e4 LT |
9 | */ |
10 | ||
a8ae6085 | 11 | #include <linux/device/class.h> |
1da177e4 LT |
12 | #include <linux/device.h> |
13 | #include <linux/module.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/string.h> | |
16 | #include <linux/kdev_t.h> | |
e9ba6365 | 17 | #include <linux/err.h> |
4e57b681 | 18 | #include <linux/slab.h> |
322cbb50 | 19 | #include <linux/blkdev.h> |
f75b1c60 | 20 | #include <linux/mutex.h> |
1da177e4 LT |
21 | #include "base.h" |
22 | ||
884f8ce4 GKH |
23 | /* /sys/class */ |
24 | static struct kset *class_kset; | |
25 | ||
1da177e4 | 26 | #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) |
1da177e4 | 27 | |
884f8ce4 GKH |
28 | /** |
29 | * class_to_subsys - Turn a struct class into a struct subsys_private | |
30 | * | |
31 | * @class: pointer to the struct bus_type to look up | |
32 | * | |
33 | * The driver core internals need to work on the subsys_private structure, not | |
34 | * the external struct class pointer. This function walks the list of | |
35 | * registered classes in the system and finds the matching one and returns the | |
36 | * internal struct subsys_private that relates to that class. | |
37 | * | |
38 | * Note, the reference count of the return value is INCREMENTED if it is not | |
39 | * NULL. A call to subsys_put() must be done when finished with the pointer in | |
40 | * order for it to be properly freed. | |
41 | */ | |
7d90e81a | 42 | struct subsys_private *class_to_subsys(const struct class *class) |
884f8ce4 GKH |
43 | { |
44 | struct subsys_private *sp = NULL; | |
45 | struct kobject *kobj; | |
46 | ||
47 | if (!class || !class_kset) | |
48 | return NULL; | |
49 | ||
50 | spin_lock(&class_kset->list_lock); | |
51 | ||
52 | if (list_empty(&class_kset->list)) | |
53 | goto done; | |
54 | ||
55 | list_for_each_entry(kobj, &class_kset->list, entry) { | |
56 | struct kset *kset = container_of(kobj, struct kset, kobj); | |
57 | ||
58 | sp = container_of_const(kset, struct subsys_private, subsys); | |
59 | if (sp->class == class) | |
60 | goto done; | |
61 | } | |
62 | sp = NULL; | |
63 | done: | |
64 | sp = subsys_get(sp); | |
65 | spin_unlock(&class_kset->list_lock); | |
66 | return sp; | |
67 | } | |
68 | ||
4a3ad20c GKH |
69 | static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr, |
70 | char *buf) | |
1da177e4 | 71 | { |
4a3ad20c | 72 | struct class_attribute *class_attr = to_class_attr(attr); |
6b6e39a6 | 73 | struct subsys_private *cp = to_subsys_private(kobj); |
4a0c20bf | 74 | ssize_t ret = -EIO; |
1da177e4 LT |
75 | |
76 | if (class_attr->show) | |
28812fe1 | 77 | ret = class_attr->show(cp->class, class_attr, buf); |
1da177e4 LT |
78 | return ret; |
79 | } | |
80 | ||
4a3ad20c GKH |
81 | static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr, |
82 | const char *buf, size_t count) | |
1da177e4 | 83 | { |
4a3ad20c | 84 | struct class_attribute *class_attr = to_class_attr(attr); |
6b6e39a6 | 85 | struct subsys_private *cp = to_subsys_private(kobj); |
4a0c20bf | 86 | ssize_t ret = -EIO; |
1da177e4 LT |
87 | |
88 | if (class_attr->store) | |
28812fe1 | 89 | ret = class_attr->store(cp->class, class_attr, buf, count); |
1da177e4 LT |
90 | return ret; |
91 | } | |
92 | ||
4a3ad20c | 93 | static void class_release(struct kobject *kobj) |
1da177e4 | 94 | { |
6b6e39a6 | 95 | struct subsys_private *cp = to_subsys_private(kobj); |
43a7206b | 96 | const struct class *class = cp->class; |
1da177e4 LT |
97 | |
98 | pr_debug("class '%s': release.\n", class->name); | |
99 | ||
100 | if (class->class_release) | |
101 | class->class_release(class); | |
102 | else | |
103 | pr_debug("class '%s' does not have a release() function, " | |
104 | "be careful\n", class->name); | |
18d19c96 | 105 | |
f326ea63 | 106 | lockdep_unregister_key(&cp->lock_key); |
18d19c96 | 107 | kfree(cp); |
1da177e4 LT |
108 | } |
109 | ||
02a476d9 | 110 | static const struct kobj_ns_type_operations *class_child_ns_type(const struct kobject *kobj) |
bc451f20 | 111 | { |
7bbb89b4 | 112 | const struct subsys_private *cp = to_subsys_private(kobj); |
43a7206b | 113 | const struct class *class = cp->class; |
bc451f20 EB |
114 | |
115 | return class->ns_type; | |
116 | } | |
117 | ||
52cf25d0 | 118 | static const struct sysfs_ops class_sysfs_ops = { |
672d82c1 EB |
119 | .show = class_attr_show, |
120 | .store = class_attr_store, | |
1da177e4 LT |
121 | }; |
122 | ||
c83d9ab4 | 123 | static const struct kobj_type class_ktype = { |
1da177e4 LT |
124 | .sysfs_ops = &class_sysfs_ops, |
125 | .release = class_release, | |
bc451f20 | 126 | .child_ns_type = class_child_ns_type, |
1da177e4 LT |
127 | }; |
128 | ||
80842a92 | 129 | int class_create_file_ns(const struct class *cls, const struct class_attribute *attr, |
58292cbe | 130 | const void *ns) |
1da177e4 | 131 | { |
7b884b7f | 132 | struct subsys_private *sp = class_to_subsys(cls); |
1da177e4 | 133 | int error; |
d34898de | 134 | |
7b884b7f GKH |
135 | if (!sp) |
136 | return -EINVAL; | |
137 | ||
138 | error = sysfs_create_file_ns(&sp->subsys.kobj, &attr->attr, ns); | |
139 | subsys_put(sp); | |
140 | ||
1da177e4 LT |
141 | return error; |
142 | } | |
779aeb73 | 143 | EXPORT_SYMBOL_GPL(class_create_file_ns); |
1da177e4 | 144 | |
80842a92 | 145 | void class_remove_file_ns(const struct class *cls, const struct class_attribute *attr, |
58292cbe | 146 | const void *ns) |
1da177e4 | 147 | { |
7b884b7f GKH |
148 | struct subsys_private *sp = class_to_subsys(cls); |
149 | ||
150 | if (!sp) | |
151 | return; | |
152 | ||
153 | sysfs_remove_file_ns(&sp->subsys.kobj, &attr->attr, ns); | |
154 | subsys_put(sp); | |
1da177e4 | 155 | } |
779aeb73 | 156 | EXPORT_SYMBOL_GPL(class_remove_file_ns); |
1da177e4 | 157 | |
570d0200 WY |
158 | static struct device *klist_class_to_dev(struct klist_node *n) |
159 | { | |
160 | struct device_private *p = to_device_private_class(n); | |
161 | return p->device; | |
162 | } | |
163 | ||
5a3ceb86 TH |
164 | static void klist_class_dev_get(struct klist_node *n) |
165 | { | |
570d0200 | 166 | struct device *dev = klist_class_to_dev(n); |
5a3ceb86 TH |
167 | |
168 | get_device(dev); | |
169 | } | |
170 | ||
171 | static void klist_class_dev_put(struct klist_node *n) | |
172 | { | |
570d0200 | 173 | struct device *dev = klist_class_to_dev(n); |
5a3ceb86 TH |
174 | |
175 | put_device(dev); | |
176 | } | |
177 | ||
43a7206b | 178 | int class_register(const struct class *cls) |
1da177e4 | 179 | { |
6b6e39a6 | 180 | struct subsys_private *cp; |
dcfbb67e | 181 | struct lock_class_key *key; |
1da177e4 LT |
182 | int error; |
183 | ||
184 | pr_debug("device class '%s': registering\n", cls->name); | |
185 | ||
a169a663 ZH |
186 | if (cls->ns_type && !cls->namespace) { |
187 | pr_err("%s: class '%s' does not have namespace\n", | |
188 | __func__, cls->name); | |
189 | return -EINVAL; | |
190 | } | |
191 | if (!cls->ns_type && cls->namespace) { | |
192 | pr_err("%s: class '%s' does not have ns_type\n", | |
193 | __func__, cls->name); | |
194 | return -EINVAL; | |
195 | } | |
196 | ||
7c71448b GKH |
197 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); |
198 | if (!cp) | |
199 | return -ENOMEM; | |
6b6e39a6 | 200 | klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put); |
ca22e56d | 201 | INIT_LIST_HEAD(&cp->interfaces); |
6b6e39a6 | 202 | kset_init(&cp->glue_dirs); |
dcfbb67e GKH |
203 | key = &cp->lock_key; |
204 | lockdep_register_key(key); | |
ca22e56d | 205 | __mutex_init(&cp->mutex, "subsys mutex", key); |
6b6e39a6 | 206 | error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name); |
28f2d57d ML |
207 | if (error) |
208 | goto err_out; | |
1da177e4 | 209 | |
6b6e39a6 | 210 | cp->subsys.kobj.kset = class_kset; |
6b6e39a6 | 211 | cp->subsys.kobj.ktype = &class_ktype; |
7c71448b | 212 | cp->class = cls; |
1da177e4 | 213 | |
6b6e39a6 | 214 | error = kset_register(&cp->subsys); |
0b2a1a39 RW |
215 | if (error) |
216 | goto err_out; | |
217 | ||
7b884b7f | 218 | error = sysfs_create_groups(&cp->subsys.kobj, cls->class_groups); |
8c3e8a6b YY |
219 | if (error) { |
220 | kobject_del(&cp->subsys.kobj); | |
221 | kfree_const(cp->subsys.kobj.name); | |
0b2a1a39 | 222 | goto err_out; |
8c3e8a6b | 223 | } |
0b2a1a39 RW |
224 | return 0; |
225 | ||
226 | err_out: | |
93ec4a3b | 227 | lockdep_unregister_key(key); |
0b2a1a39 | 228 | kfree(cp); |
1da177e4 LT |
229 | return error; |
230 | } | |
dcfbb67e | 231 | EXPORT_SYMBOL_GPL(class_register); |
1da177e4 | 232 | |
517d4927 | 233 | void class_unregister(const struct class *cls) |
1da177e4 | 234 | { |
7b884b7f GKH |
235 | struct subsys_private *sp = class_to_subsys(cls); |
236 | ||
237 | if (!sp) | |
238 | return; | |
239 | ||
1da177e4 | 240 | pr_debug("device class '%s': unregistering\n", cls->name); |
7b884b7f GKH |
241 | |
242 | sysfs_remove_groups(&sp->subsys.kobj, cls->class_groups); | |
243 | kset_unregister(&sp->subsys); | |
244 | subsys_put(sp); | |
1da177e4 | 245 | } |
779aeb73 | 246 | EXPORT_SYMBOL_GPL(class_unregister); |
1da177e4 | 247 | |
979207ca | 248 | static void class_create_release(const struct class *cls) |
e9ba6365 | 249 | { |
2b3a302a | 250 | pr_debug("%s called for %s\n", __func__, cls->name); |
e9ba6365 | 251 | kfree(cls); |
252 | } | |
253 | ||
2fc68447 | 254 | /** |
dcfbb67e | 255 | * class_create - create a struct class structure |
2fc68447 | 256 | * @name: pointer to a string for the name of this class. |
257 | * | |
258 | * This is used to create a struct class pointer that can then be used | |
c3b19ff0 | 259 | * in calls to device_create(). |
2fc68447 | 260 | * |
f0eae0ed JN |
261 | * Returns &struct class pointer on success, or ERR_PTR() on error. |
262 | * | |
2fc68447 | 263 | * Note, the pointer created here is to be destroyed when finished by |
264 | * making a call to class_destroy(). | |
265 | */ | |
dcfbb67e | 266 | struct class *class_create(const char *name) |
e9ba6365 | 267 | { |
268 | struct class *cls; | |
269 | int retval; | |
270 | ||
4aed0644 | 271 | cls = kzalloc(sizeof(*cls), GFP_KERNEL); |
e9ba6365 | 272 | if (!cls) { |
273 | retval = -ENOMEM; | |
274 | goto error; | |
275 | } | |
e9ba6365 | 276 | |
277 | cls->name = name; | |
e9ba6365 | 278 | cls->class_release = class_create_release; |
e9ba6365 | 279 | |
dcfbb67e | 280 | retval = class_register(cls); |
e9ba6365 | 281 | if (retval) |
282 | goto error; | |
283 | ||
284 | return cls; | |
285 | ||
286 | error: | |
287 | kfree(cls); | |
288 | return ERR_PTR(retval); | |
289 | } | |
dcfbb67e | 290 | EXPORT_SYMBOL_GPL(class_create); |
e9ba6365 | 291 | |
2fc68447 | 292 | /** |
293 | * class_destroy - destroys a struct class structure | |
92a0f861 | 294 | * @cls: pointer to the struct class that is to be destroyed |
2fc68447 | 295 | * |
296 | * Note, the pointer to be destroyed must have been created with a call | |
297 | * to class_create(). | |
298 | */ | |
517d4927 | 299 | void class_destroy(const struct class *cls) |
e9ba6365 | 300 | { |
e9628e01 | 301 | if (IS_ERR_OR_NULL(cls)) |
e9ba6365 | 302 | return; |
303 | ||
304 | class_unregister(cls); | |
305 | } | |
779aeb73 | 306 | EXPORT_SYMBOL_GPL(class_destroy); |
1da177e4 | 307 | |
5a3ceb86 TH |
308 | /** |
309 | * class_dev_iter_init - initialize class device iterator | |
310 | * @iter: class iterator to initialize | |
311 | * @class: the class we wanna iterate over | |
312 | * @start: the device to start iterating from, if any | |
313 | * @type: device_type of the devices to iterate over, NULL for all | |
314 | * | |
315 | * Initialize class iterator @iter such that it iterates over devices | |
316 | * of @class. If @start is set, the list iteration will start there, | |
317 | * otherwise if it is NULL, the iteration starts at the beginning of | |
318 | * the list. | |
319 | */ | |
a2fd6e42 GKH |
320 | void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class, |
321 | const struct device *start, const struct device_type *type) | |
5a3ceb86 | 322 | { |
7b884b7f | 323 | struct subsys_private *sp = class_to_subsys(class); |
5a3ceb86 TH |
324 | struct klist_node *start_knode = NULL; |
325 | ||
e128f82f ZH |
326 | memset(iter, 0, sizeof(*iter)); |
327 | if (!sp) { | |
328 | pr_crit("%s: class %p was not registered yet\n", | |
329 | __func__, class); | |
7b884b7f | 330 | return; |
e128f82f | 331 | } |
7b884b7f | 332 | |
5a3ceb86 | 333 | if (start) |
570d0200 | 334 | start_knode = &start->p->knode_class; |
7b884b7f | 335 | klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode); |
5a3ceb86 | 336 | iter->type = type; |
ddaf098e | 337 | iter->sp = sp; |
5a3ceb86 TH |
338 | } |
339 | EXPORT_SYMBOL_GPL(class_dev_iter_init); | |
340 | ||
341 | /** | |
342 | * class_dev_iter_next - iterate to the next device | |
343 | * @iter: class iterator to proceed | |
344 | * | |
345 | * Proceed @iter to the next device and return it. Returns NULL if | |
346 | * iteration is complete. | |
347 | * | |
348 | * The returned device is referenced and won't be released till | |
349 | * iterator is proceed to the next device or exited. The caller is | |
350 | * free to do whatever it wants to do with the device including | |
351 | * calling back into class code. | |
352 | */ | |
353 | struct device *class_dev_iter_next(struct class_dev_iter *iter) | |
354 | { | |
355 | struct klist_node *knode; | |
356 | struct device *dev; | |
357 | ||
e128f82f ZH |
358 | if (!iter->sp) |
359 | return NULL; | |
360 | ||
5a3ceb86 TH |
361 | while (1) { |
362 | knode = klist_next(&iter->ki); | |
363 | if (!knode) | |
364 | return NULL; | |
570d0200 | 365 | dev = klist_class_to_dev(knode); |
5a3ceb86 TH |
366 | if (!iter->type || iter->type == dev->type) |
367 | return dev; | |
368 | } | |
369 | } | |
370 | EXPORT_SYMBOL_GPL(class_dev_iter_next); | |
371 | ||
372 | /** | |
373 | * class_dev_iter_exit - finish iteration | |
374 | * @iter: class iterator to finish | |
375 | * | |
376 | * Finish an iteration. Always call this function after iteration is | |
377 | * complete whether the iteration ran till the end or not. | |
378 | */ | |
379 | void class_dev_iter_exit(struct class_dev_iter *iter) | |
380 | { | |
381 | klist_iter_exit(&iter->ki); | |
ddaf098e | 382 | subsys_put(iter->sp); |
5a3ceb86 TH |
383 | } |
384 | EXPORT_SYMBOL_GPL(class_dev_iter_exit); | |
385 | ||
fd04897b DY |
386 | /** |
387 | * class_for_each_device - device iterator | |
388 | * @class: the class we're iterating | |
93562b53 | 389 | * @start: the device to start with in the list, if any. |
fd04897b DY |
390 | * @data: data for the callback |
391 | * @fn: function to be called for each device | |
392 | * | |
393 | * Iterate over @class's list of devices, and call @fn for each, | |
93562b53 GKH |
394 | * passing it @data. If @start is set, the list iteration will start |
395 | * there, otherwise if it is NULL, the iteration starts at the | |
396 | * beginning of the list. | |
fd04897b DY |
397 | * |
398 | * We check the return of @fn each time. If it returns anything | |
399 | * other than 0, we break out and return that value. | |
400 | * | |
5a3ceb86 TH |
401 | * @fn is allowed to do anything including calling back into class |
402 | * code. There's no locking restriction. | |
fd04897b | 403 | */ |
69df024e | 404 | int class_for_each_device(const struct class *class, const struct device *start, |
767b74e0 | 405 | void *data, device_iter_t fn) |
fd04897b | 406 | { |
7b884b7f | 407 | struct subsys_private *sp = class_to_subsys(class); |
5a3ceb86 | 408 | struct class_dev_iter iter; |
fd04897b DY |
409 | struct device *dev; |
410 | int error = 0; | |
411 | ||
412 | if (!class) | |
413 | return -EINVAL; | |
7b884b7f | 414 | if (!sp) { |
f659e8fb | 415 | WARN(1, "%s called for class '%s' before it was registered", |
7c225035 DB |
416 | __func__, class->name); |
417 | return -EINVAL; | |
418 | } | |
419 | ||
5a3ceb86 TH |
420 | class_dev_iter_init(&iter, class, start, NULL); |
421 | while ((dev = class_dev_iter_next(&iter))) { | |
93562b53 | 422 | error = fn(dev, data); |
fd04897b DY |
423 | if (error) |
424 | break; | |
425 | } | |
5a3ceb86 | 426 | class_dev_iter_exit(&iter); |
7b884b7f | 427 | subsys_put(sp); |
fd04897b DY |
428 | |
429 | return error; | |
430 | } | |
431 | EXPORT_SYMBOL_GPL(class_for_each_device); | |
432 | ||
433 | /** | |
434 | * class_find_device - device iterator for locating a particular device | |
435 | * @class: the class we're iterating | |
695794ae | 436 | * @start: Device to begin with |
fd04897b DY |
437 | * @data: data for the match function |
438 | * @match: function to check device | |
439 | * | |
440 | * This is similar to the class_for_each_dev() function above, but it | |
441 | * returns a reference to a device that is 'found' for later use, as | |
442 | * determined by the @match callback. | |
443 | * | |
444 | * The callback should return 0 if the device doesn't match and non-zero | |
445 | * if it does. If the callback returns non-zero, this function will | |
446 | * return to the caller and not iterate over any more devices. | |
a63ca8f6 | 447 | * |
fd04897b DY |
448 | * Note, you will need to drop the reference with put_device() after use. |
449 | * | |
12077754 | 450 | * @match is allowed to do anything including calling back into class |
5a3ceb86 | 451 | * code. There's no locking restriction. |
fd04897b | 452 | */ |
cf41015e | 453 | struct device *class_find_device(const struct class *class, const struct device *start, |
b45ed06f | 454 | const void *data, device_match_t match) |
fd04897b | 455 | { |
7b884b7f | 456 | struct subsys_private *sp = class_to_subsys(class); |
5a3ceb86 | 457 | struct class_dev_iter iter; |
fd04897b | 458 | struct device *dev; |
fd04897b DY |
459 | |
460 | if (!class) | |
461 | return NULL; | |
7b884b7f | 462 | if (!sp) { |
f659e8fb | 463 | WARN(1, "%s called for class '%s' before it was registered", |
7c225035 DB |
464 | __func__, class->name); |
465 | return NULL; | |
466 | } | |
fd04897b | 467 | |
5a3ceb86 TH |
468 | class_dev_iter_init(&iter, class, start, NULL); |
469 | while ((dev = class_dev_iter_next(&iter))) { | |
695794ae | 470 | if (match(dev, data)) { |
5a3ceb86 | 471 | get_device(dev); |
fd04897b | 472 | break; |
5a3ceb86 | 473 | } |
fd04897b | 474 | } |
5a3ceb86 | 475 | class_dev_iter_exit(&iter); |
7b884b7f | 476 | subsys_put(sp); |
fd04897b | 477 | |
5a3ceb86 | 478 | return dev; |
fd04897b DY |
479 | } |
480 | EXPORT_SYMBOL_GPL(class_find_device); | |
481 | ||
1da177e4 LT |
482 | int class_interface_register(struct class_interface *class_intf) |
483 | { | |
7b884b7f | 484 | struct subsys_private *sp; |
884f8ce4 | 485 | const struct class *parent; |
5a3ceb86 | 486 | struct class_dev_iter iter; |
c47ed219 | 487 | struct device *dev; |
1da177e4 LT |
488 | |
489 | if (!class_intf || !class_intf->class) | |
490 | return -ENODEV; | |
491 | ||
7b884b7f GKH |
492 | parent = class_intf->class; |
493 | sp = class_to_subsys(parent); | |
494 | if (!sp) | |
1da177e4 LT |
495 | return -EINVAL; |
496 | ||
7b884b7f GKH |
497 | /* |
498 | * Reference in sp is now incremented and will be dropped when | |
499 | * the interface is removed in the call to class_interface_unregister() | |
500 | */ | |
501 | ||
502 | mutex_lock(&sp->mutex); | |
503 | list_add_tail(&class_intf->node, &sp->interfaces); | |
c47ed219 | 504 | if (class_intf->add_dev) { |
5a3ceb86 TH |
505 | class_dev_iter_init(&iter, parent, NULL, NULL); |
506 | while ((dev = class_dev_iter_next(&iter))) | |
2243acd5 | 507 | class_intf->add_dev(dev); |
5a3ceb86 | 508 | class_dev_iter_exit(&iter); |
c47ed219 | 509 | } |
7b884b7f | 510 | mutex_unlock(&sp->mutex); |
1da177e4 LT |
511 | |
512 | return 0; | |
513 | } | |
779aeb73 | 514 | EXPORT_SYMBOL_GPL(class_interface_register); |
1da177e4 LT |
515 | |
516 | void class_interface_unregister(struct class_interface *class_intf) | |
517 | { | |
7b884b7f | 518 | struct subsys_private *sp; |
6b0d49be | 519 | const struct class *parent = class_intf->class; |
5a3ceb86 | 520 | struct class_dev_iter iter; |
c47ed219 | 521 | struct device *dev; |
1da177e4 LT |
522 | |
523 | if (!parent) | |
524 | return; | |
525 | ||
7b884b7f GKH |
526 | sp = class_to_subsys(parent); |
527 | if (!sp) | |
528 | return; | |
529 | ||
530 | mutex_lock(&sp->mutex); | |
1da177e4 | 531 | list_del_init(&class_intf->node); |
c47ed219 | 532 | if (class_intf->remove_dev) { |
5a3ceb86 TH |
533 | class_dev_iter_init(&iter, parent, NULL, NULL); |
534 | while ((dev = class_dev_iter_next(&iter))) | |
2243acd5 | 535 | class_intf->remove_dev(dev); |
5a3ceb86 | 536 | class_dev_iter_exit(&iter); |
c47ed219 | 537 | } |
7b884b7f | 538 | mutex_unlock(&sp->mutex); |
1da177e4 | 539 | |
7b884b7f GKH |
540 | /* |
541 | * Decrement the reference count twice, once for the class_to_subsys() | |
542 | * call in the start of this function, and the second one from the | |
543 | * reference increment in class_interface_register() | |
544 | */ | |
545 | subsys_put(sp); | |
546 | subsys_put(sp); | |
1da177e4 | 547 | } |
779aeb73 | 548 | EXPORT_SYMBOL_GPL(class_interface_unregister); |
1da177e4 | 549 | |
75a2d422 GKH |
550 | ssize_t show_class_attr_string(const struct class *class, |
551 | const struct class_attribute *attr, char *buf) | |
869dfc87 AK |
552 | { |
553 | struct class_attribute_string *cs; | |
d34898de | 554 | |
869dfc87 | 555 | cs = container_of(attr, struct class_attribute_string, attr); |
948b3edb | 556 | return sysfs_emit(buf, "%s\n", cs->str); |
869dfc87 AK |
557 | } |
558 | ||
559 | EXPORT_SYMBOL_GPL(show_class_attr_string); | |
560 | ||
46227094 JD |
561 | struct class_compat { |
562 | struct kobject *kobj; | |
563 | }; | |
564 | ||
565 | /** | |
566 | * class_compat_register - register a compatibility class | |
567 | * @name: the name of the class | |
568 | * | |
569 | * Compatibility class are meant as a temporary user-space compatibility | |
570 | * workaround when converting a family of class devices to a bus devices. | |
571 | */ | |
572 | struct class_compat *class_compat_register(const char *name) | |
573 | { | |
574 | struct class_compat *cls; | |
575 | ||
576 | cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL); | |
577 | if (!cls) | |
578 | return NULL; | |
579 | cls->kobj = kobject_create_and_add(name, &class_kset->kobj); | |
580 | if (!cls->kobj) { | |
581 | kfree(cls); | |
582 | return NULL; | |
583 | } | |
584 | return cls; | |
585 | } | |
586 | EXPORT_SYMBOL_GPL(class_compat_register); | |
587 | ||
588 | /** | |
589 | * class_compat_unregister - unregister a compatibility class | |
590 | * @cls: the class to unregister | |
591 | */ | |
592 | void class_compat_unregister(struct class_compat *cls) | |
593 | { | |
594 | kobject_put(cls->kobj); | |
595 | kfree(cls); | |
596 | } | |
597 | EXPORT_SYMBOL_GPL(class_compat_unregister); | |
598 | ||
599 | /** | |
600 | * class_compat_create_link - create a compatibility class device link to | |
601 | * a bus device | |
602 | * @cls: the compatibility class | |
603 | * @dev: the target bus device | |
46227094 | 604 | */ |
827ed8b1 | 605 | int class_compat_create_link(struct class_compat *cls, struct device *dev) |
46227094 | 606 | { |
827ed8b1 | 607 | return sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev)); |
46227094 JD |
608 | } |
609 | EXPORT_SYMBOL_GPL(class_compat_create_link); | |
610 | ||
611 | /** | |
612 | * class_compat_remove_link - remove a compatibility class device link to | |
613 | * a bus device | |
614 | * @cls: the compatibility class | |
615 | * @dev: the target bus device | |
46227094 | 616 | */ |
827ed8b1 | 617 | void class_compat_remove_link(struct class_compat *cls, struct device *dev) |
46227094 | 618 | { |
46227094 JD |
619 | sysfs_remove_link(cls->kobj, dev_name(dev)); |
620 | } | |
621 | EXPORT_SYMBOL_GPL(class_compat_remove_link); | |
622 | ||
6f14c022 GKH |
623 | /** |
624 | * class_is_registered - determine if at this moment in time, a class is | |
625 | * registered in the driver core or not. | |
626 | * @class: the class to check | |
627 | * | |
628 | * Returns a boolean to state if the class is registered in the driver core | |
629 | * or not. Note that the value could switch right after this call is made, | |
630 | * so only use this in places where you "know" it is safe to do so (usually | |
631 | * to determine if the specific class has been registered yet or not). | |
632 | * | |
633 | * Be careful in using this. | |
634 | */ | |
635 | bool class_is_registered(const struct class *class) | |
636 | { | |
637 | struct subsys_private *sp = class_to_subsys(class); | |
638 | bool is_initialized = false; | |
639 | ||
640 | if (sp) { | |
641 | is_initialized = true; | |
642 | subsys_put(sp); | |
643 | } | |
644 | return is_initialized; | |
645 | } | |
646 | EXPORT_SYMBOL_GPL(class_is_registered); | |
647 | ||
1da177e4 LT |
648 | int __init classes_init(void) |
649 | { | |
443dbf90 GKH |
650 | class_kset = kset_create_and_add("class", NULL, NULL); |
651 | if (!class_kset) | |
652 | return -ENOMEM; | |
1da177e4 LT |
653 | return 0; |
654 | } |