blob: cc5e28c8885ce1b78d2b0453e9068c8abf0dde64 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * class.c - basic device class management
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2003-2004 Greg Kroah-Hartman
7 * Copyright (c) 2003-2004 IBM Corp.
8 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/string.h>
17#include <linux/kdev_t.h>
gregkh@suse.dee9ba6362005-03-15 11:54:21 -080018#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/slab.h>
Kay Sieversedfaa7c2007-05-21 22:08:01 +020020#include <linux/genhd.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "base.h"
23
24#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080026static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
27 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080029 struct class_attribute *class_attr = to_class_attr(attr);
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -050030 struct class_private *cp = to_class(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050031 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 if (class_attr->show)
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -050034 ret = class_attr->show(cp->class, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 return ret;
36}
37
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080038static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
39 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080041 struct class_attribute *class_attr = to_class_attr(attr);
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -050042 struct class_private *cp = to_class(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050043 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if (class_attr->store)
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -050046 ret = class_attr->store(cp->class, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return ret;
48}
49
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080050static void class_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -050052 struct class_private *cp = to_class(kobj);
53 struct class *class = cp->class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 pr_debug("class '%s': release.\n", class->name);
56
57 if (class->class_release)
58 class->class_release(class);
59 else
60 pr_debug("class '%s' does not have a release() function, "
61 "be careful\n", class->name);
62}
63
64static struct sysfs_ops class_sysfs_ops = {
65 .show = class_attr_show,
66 .store = class_attr_store,
67};
68
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -060069static struct kobj_type class_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .sysfs_ops = &class_sysfs_ops,
71 .release = class_release,
72};
73
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -070074/* Hotplug events for classes go to the class class_subsys */
Greg Kroah-Hartman443dbf92007-10-29 23:22:26 -050075static struct kset *class_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080078int class_create_file(struct class *cls, const struct class_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 int error;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080081 if (cls)
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -070082 error = sysfs_create_file(&cls->p->class_subsys.kobj,
83 &attr->attr);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080084 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 error = -EINVAL;
86 return error;
87}
88
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080089void class_remove_file(struct class *cls, const struct class_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 if (cls)
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -070092 sysfs_remove_file(&cls->p->class_subsys.kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Greg Kroah-Hartman17407572006-05-02 16:59:59 +020095static struct class *class_get(struct class *cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 if (cls)
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -070098 kset_get(&cls->p->class_subsys);
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -050099 return cls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800102static void class_put(struct class *cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700104 if (cls)
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700105 kset_put(&cls->p->class_subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800108static int add_class_attrs(struct class *cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 int i;
111 int error = 0;
112
113 if (cls->class_attrs) {
114 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800115 error = class_create_file(cls, &cls->class_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (error)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800117 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 }
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800120done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return error;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800122error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800124 class_remove_file(cls, &cls->class_attrs[i]);
125 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800128static void remove_class_attrs(struct class *cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 int i;
131
132 if (cls->class_attrs) {
133 for (i = 0; attr_name(cls->class_attrs[i]); i++)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800134 class_remove_file(cls, &cls->class_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136}
137
Matthew Wilcoxd2a3b912008-05-28 09:28:39 -0700138int __class_register(struct class *cls, struct lock_class_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500140 struct class_private *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int error;
142
143 pr_debug("device class '%s': registering\n", cls->name);
144
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500145 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
146 if (!cp)
147 return -ENOMEM;
Greg Kroah-Hartman97ae69f2008-05-28 09:28:39 -0700148 INIT_LIST_HEAD(&cp->class_devices);
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -0700149 INIT_LIST_HEAD(&cp->class_interfaces);
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500150 kset_init(&cp->class_dirs);
Dave Youngf75b1c62008-05-28 09:28:39 -0700151 __mutex_init(&cp->class_mutex, "struct class mutex", key);
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700152 error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name);
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500153 if (error) {
154 kfree(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return error;
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Dan Williamse105b8b2008-04-21 10:51:07 -0700158 /* set the default /sys/dev directory for devices of this class */
159 if (!cls->dev_kobj)
160 cls->dev_kobj = sysfs_dev_char_kobj;
161
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800162#if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200163 /* let the block class directory show up in the root of sysfs */
164 if (cls != &block_class)
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700165 cp->class_subsys.kobj.kset = class_kset;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200166#else
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700167 cp->class_subsys.kobj.kset = class_kset;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200168#endif
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700169 cp->class_subsys.kobj.ktype = &class_ktype;
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500170 cp->class = cls;
171 cls->p = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700173 error = kset_register(&cp->class_subsys);
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500174 if (error) {
175 kfree(cp);
176 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500178 error = add_class_attrs(class_get(cls));
179 class_put(cls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return error;
181}
Matthew Wilcoxd2a3b912008-05-28 09:28:39 -0700182EXPORT_SYMBOL_GPL(__class_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800184void class_unregister(struct class *cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 pr_debug("device class '%s': unregistering\n", cls->name);
187 remove_class_attrs(cls);
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700188 kset_unregister(&cls->p->class_subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800191static void class_create_release(struct class *cls)
192{
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800193 pr_debug("%s called for %s\n", __func__, cls->name);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800194 kfree(cls);
195}
196
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800197/**
198 * class_create - create a struct class structure
199 * @owner: pointer to the module that is to "own" this struct class
200 * @name: pointer to a string for the name of this class.
Randy Dunlap0e241ff2008-07-24 16:58:42 -0700201 * @key: the lock_class_key for this class; used by mutex lock debugging
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800202 *
203 * This is used to create a struct class pointer that can then be used
Kay Sieversc3b19ff2008-03-12 20:47:35 +0100204 * in calls to device_create().
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800205 *
206 * Note, the pointer created here is to be destroyed when finished by
207 * making a call to class_destroy().
208 */
Matthew Wilcoxd2a3b912008-05-28 09:28:39 -0700209struct class *__class_create(struct module *owner, const char *name,
210 struct lock_class_key *key)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800211{
212 struct class *cls;
213 int retval;
214
Jiri Slaby4aed0642005-09-13 01:25:01 -0700215 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800216 if (!cls) {
217 retval = -ENOMEM;
218 goto error;
219 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800220
221 cls->name = name;
222 cls->owner = owner;
223 cls->class_release = class_create_release;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800224
Matthew Wilcoxd2a3b912008-05-28 09:28:39 -0700225 retval = __class_register(cls, key);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800226 if (retval)
227 goto error;
228
229 return cls;
230
231error:
232 kfree(cls);
233 return ERR_PTR(retval);
234}
Matthew Wilcoxd2a3b912008-05-28 09:28:39 -0700235EXPORT_SYMBOL_GPL(__class_create);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800236
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800237/**
238 * class_destroy - destroys a struct class structure
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700239 * @cls: pointer to the struct class that is to be destroyed
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800240 *
241 * Note, the pointer to be destroyed must have been created with a call
242 * to class_create().
243 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800244void class_destroy(struct class *cls)
245{
246 if ((cls == NULL) || (IS_ERR(cls)))
247 return;
248
249 class_unregister(cls);
250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Kay Sievers805fab42006-09-14 11:23:28 +0200252#ifdef CONFIG_SYSFS_DEPRECATED
253char *make_class_name(const char *name, struct kobject *kobj)
254{
255 char *class_name;
256 int size;
257
258 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
259
260 class_name = kmalloc(size, GFP_KERNEL);
261 if (!class_name)
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100262 return NULL;
Kay Sievers805fab42006-09-14 11:23:28 +0200263
264 strcpy(class_name, name);
265 strcat(class_name, ":");
266 strcat(class_name, kobject_name(kobj));
267 return class_name;
268}
Kay Sievers805fab42006-09-14 11:23:28 +0200269#endif
270
Dave Youngfd048972008-01-22 15:27:08 +0800271/**
272 * class_for_each_device - device iterator
273 * @class: the class we're iterating
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400274 * @start: the device to start with in the list, if any.
Dave Youngfd048972008-01-22 15:27:08 +0800275 * @data: data for the callback
276 * @fn: function to be called for each device
277 *
278 * Iterate over @class's list of devices, and call @fn for each,
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400279 * passing it @data. If @start is set, the list iteration will start
280 * there, otherwise if it is NULL, the iteration starts at the
281 * beginning of the list.
Dave Youngfd048972008-01-22 15:27:08 +0800282 *
283 * We check the return of @fn each time. If it returns anything
284 * other than 0, we break out and return that value.
285 *
Dave Youngf75b1c62008-05-28 09:28:39 -0700286 * Note, we hold class->class_mutex in this function, so it can not be
Dave Youngfd048972008-01-22 15:27:08 +0800287 * re-acquired in @fn, otherwise it will self-deadlocking. For
288 * example, calls to add or remove class members would be verboten.
289 */
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400290int class_for_each_device(struct class *class, struct device *start,
291 void *data, int (*fn)(struct device *, void *))
Dave Youngfd048972008-01-22 15:27:08 +0800292{
293 struct device *dev;
294 int error = 0;
295
296 if (!class)
297 return -EINVAL;
David Brownell7c225032008-08-06 18:52:44 -0700298 if (!class->p) {
299 WARN(1, "%s called for class '%s' before it was initialized",
300 __func__, class->name);
301 return -EINVAL;
302 }
303
Dave Youngf75b1c62008-05-28 09:28:39 -0700304 mutex_lock(&class->p->class_mutex);
Greg Kroah-Hartman97ae69f2008-05-28 09:28:39 -0700305 list_for_each_entry(dev, &class->p->class_devices, node) {
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400306 if (start) {
307 if (start == dev)
308 start = NULL;
309 continue;
310 }
Dave Youngfd048972008-01-22 15:27:08 +0800311 dev = get_device(dev);
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400312 error = fn(dev, data);
313 put_device(dev);
Dave Youngfd048972008-01-22 15:27:08 +0800314 if (error)
315 break;
316 }
Dave Youngf75b1c62008-05-28 09:28:39 -0700317 mutex_unlock(&class->p->class_mutex);
Dave Youngfd048972008-01-22 15:27:08 +0800318
319 return error;
320}
321EXPORT_SYMBOL_GPL(class_for_each_device);
322
323/**
324 * class_find_device - device iterator for locating a particular device
325 * @class: the class we're iterating
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400326 * @start: Device to begin with
Dave Youngfd048972008-01-22 15:27:08 +0800327 * @data: data for the match function
328 * @match: function to check device
329 *
330 * This is similar to the class_for_each_dev() function above, but it
331 * returns a reference to a device that is 'found' for later use, as
332 * determined by the @match callback.
333 *
334 * The callback should return 0 if the device doesn't match and non-zero
335 * if it does. If the callback returns non-zero, this function will
336 * return to the caller and not iterate over any more devices.
Randy Dunlapa63ca8f2008-01-30 11:51:08 -0800337 *
Dave Youngfd048972008-01-22 15:27:08 +0800338 * Note, you will need to drop the reference with put_device() after use.
339 *
Dave Youngf75b1c62008-05-28 09:28:39 -0700340 * We hold class->class_mutex in this function, so it can not be
Dave Youngfd048972008-01-22 15:27:08 +0800341 * re-acquired in @match, otherwise it will self-deadlocking. For
342 * example, calls to add or remove class members would be verboten.
343 */
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400344struct device *class_find_device(struct class *class, struct device *start,
345 void *data,
346 int (*match)(struct device *, void *))
Dave Youngfd048972008-01-22 15:27:08 +0800347{
348 struct device *dev;
349 int found = 0;
350
351 if (!class)
352 return NULL;
David Brownell7c225032008-08-06 18:52:44 -0700353 if (!class->p) {
354 WARN(1, "%s called for class '%s' before it was initialized",
355 __func__, class->name);
356 return NULL;
357 }
Dave Youngfd048972008-01-22 15:27:08 +0800358
Dave Youngf75b1c62008-05-28 09:28:39 -0700359 mutex_lock(&class->p->class_mutex);
Greg Kroah-Hartman97ae69f2008-05-28 09:28:39 -0700360 list_for_each_entry(dev, &class->p->class_devices, node) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400361 if (start) {
362 if (start == dev)
363 start = NULL;
364 continue;
365 }
Dave Youngfd048972008-01-22 15:27:08 +0800366 dev = get_device(dev);
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400367 if (match(dev, data)) {
368 found = 1;
Dave Youngfd048972008-01-22 15:27:08 +0800369 break;
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400370 } else
371 put_device(dev);
Dave Youngfd048972008-01-22 15:27:08 +0800372 }
Dave Youngf75b1c62008-05-28 09:28:39 -0700373 mutex_unlock(&class->p->class_mutex);
Dave Youngfd048972008-01-22 15:27:08 +0800374
375 return found ? dev : NULL;
376}
377EXPORT_SYMBOL_GPL(class_find_device);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379int class_interface_register(struct class_interface *class_intf)
380{
381 struct class *parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200382 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (!class_intf || !class_intf->class)
385 return -ENODEV;
386
387 parent = class_get(class_intf->class);
388 if (!parent)
389 return -EINVAL;
390
Dave Youngf75b1c62008-05-28 09:28:39 -0700391 mutex_lock(&parent->p->class_mutex);
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -0700392 list_add_tail(&class_intf->node, &parent->p->class_interfaces);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200393 if (class_intf->add_dev) {
Greg Kroah-Hartman97ae69f2008-05-28 09:28:39 -0700394 list_for_each_entry(dev, &parent->p->class_devices, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200395 class_intf->add_dev(dev, class_intf);
396 }
Dave Youngf75b1c62008-05-28 09:28:39 -0700397 mutex_unlock(&parent->p->class_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400}
401
402void class_interface_unregister(struct class_interface *class_intf)
403{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800404 struct class *parent = class_intf->class;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200405 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (!parent)
408 return;
409
Dave Youngf75b1c62008-05-28 09:28:39 -0700410 mutex_lock(&parent->p->class_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 list_del_init(&class_intf->node);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200412 if (class_intf->remove_dev) {
Greg Kroah-Hartman97ae69f2008-05-28 09:28:39 -0700413 list_for_each_entry(dev, &parent->p->class_devices, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200414 class_intf->remove_dev(dev, class_intf);
415 }
Dave Youngf75b1c62008-05-28 09:28:39 -0700416 mutex_unlock(&parent->p->class_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 class_put(parent);
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421int __init classes_init(void)
422{
Greg Kroah-Hartman443dbf92007-10-29 23:22:26 -0500423 class_kset = kset_create_and_add("class", NULL, NULL);
424 if (!class_kset)
425 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427}
428
429EXPORT_SYMBOL_GPL(class_create_file);
430EXPORT_SYMBOL_GPL(class_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431EXPORT_SYMBOL_GPL(class_unregister);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800432EXPORT_SYMBOL_GPL(class_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434EXPORT_SYMBOL_GPL(class_interface_register);
435EXPORT_SYMBOL_GPL(class_interface_unregister);