kobject: remove struct kobj_type from struct kset
[linux-2.6.git] / drivers / base / class.c
1 /*
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
13 #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>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include "base.h"
21
22 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
23 #define to_class(obj) container_of(obj, struct class, subsys.kobj)
24
25 static ssize_t
26 class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
27 {
28         struct class_attribute * class_attr = to_class_attr(attr);
29         struct class * dc = to_class(kobj);
30         ssize_t ret = -EIO;
31
32         if (class_attr->show)
33                 ret = class_attr->show(dc, buf);
34         return ret;
35 }
36
37 static ssize_t
38 class_attr_store(struct kobject * kobj, struct attribute * attr,
39                  const char * buf, size_t count)
40 {
41         struct class_attribute * class_attr = to_class_attr(attr);
42         struct class * dc = to_class(kobj);
43         ssize_t ret = -EIO;
44
45         if (class_attr->store)
46                 ret = class_attr->store(dc, buf, count);
47         return ret;
48 }
49
50 static void class_release(struct kobject * kobj)
51 {
52         struct class *class = to_class(kobj);
53
54         pr_debug("class '%s': release.\n", class->name);
55
56         if (class->class_release)
57                 class->class_release(class);
58         else
59                 pr_debug("class '%s' does not have a release() function, "
60                          "be careful\n", class->name);
61 }
62
63 static struct sysfs_ops class_sysfs_ops = {
64         .show   = class_attr_show,
65         .store  = class_attr_store,
66 };
67
68 static struct kobj_type class_ktype = {
69         .sysfs_ops      = &class_sysfs_ops,
70         .release        = class_release,
71 };
72
73 /* Hotplug events for classes go to the class_obj subsys */
74 static decl_subsys(class, NULL);
75
76
77 int class_create_file(struct class * cls, const struct class_attribute * attr)
78 {
79         int error;
80         if (cls) {
81                 error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
82         } else
83                 error = -EINVAL;
84         return error;
85 }
86
87 void class_remove_file(struct class * cls, const struct class_attribute * attr)
88 {
89         if (cls)
90                 sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
91 }
92
93 static struct class *class_get(struct class *cls)
94 {
95         if (cls)
96                 return container_of(kset_get(&cls->subsys), struct class, subsys);
97         return NULL;
98 }
99
100 static void class_put(struct class * cls)
101 {
102         if (cls)
103                 kset_put(&cls->subsys);
104 }
105
106
107 static int add_class_attrs(struct class * cls)
108 {
109         int i;
110         int error = 0;
111
112         if (cls->class_attrs) {
113                 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
114                         error = class_create_file(cls,&cls->class_attrs[i]);
115                         if (error)
116                                 goto Err;
117                 }
118         }
119  Done:
120         return error;
121  Err:
122         while (--i >= 0)
123                 class_remove_file(cls,&cls->class_attrs[i]);
124         goto Done;
125 }
126
127 static void remove_class_attrs(struct class * cls)
128 {
129         int i;
130
131         if (cls->class_attrs) {
132                 for (i = 0; attr_name(cls->class_attrs[i]); i++)
133                         class_remove_file(cls,&cls->class_attrs[i]);
134         }
135 }
136
137 int class_register(struct class * cls)
138 {
139         int error;
140
141         pr_debug("device class '%s': registering\n", cls->name);
142
143         INIT_LIST_HEAD(&cls->children);
144         INIT_LIST_HEAD(&cls->devices);
145         INIT_LIST_HEAD(&cls->interfaces);
146         kset_init(&cls->class_dirs);
147         init_MUTEX(&cls->sem);
148         error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
149         if (error)
150                 return error;
151
152         cls->subsys.kobj.kset = &class_subsys;
153         cls->subsys.kobj.ktype = &class_ktype;
154
155         error = subsystem_register(&cls->subsys);
156         if (!error) {
157                 error = add_class_attrs(class_get(cls));
158                 class_put(cls);
159         }
160         return error;
161 }
162
163 void class_unregister(struct class * cls)
164 {
165         pr_debug("device class '%s': unregistering\n", cls->name);
166         remove_class_attrs(cls);
167         subsystem_unregister(&cls->subsys);
168 }
169
170 static void class_create_release(struct class *cls)
171 {
172         pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
173         kfree(cls);
174 }
175
176 static void class_device_create_release(struct class_device *class_dev)
177 {
178         pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
179         kfree(class_dev);
180 }
181
182 /* needed to allow these devices to have parent class devices */
183 static int class_device_create_uevent(struct class_device *class_dev,
184                                       struct kobj_uevent_env *env)
185 {
186         pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
187         return 0;
188 }
189
190 /**
191  * class_create - create a struct class structure
192  * @owner: pointer to the module that is to "own" this struct class
193  * @name: pointer to a string for the name of this class.
194  *
195  * This is used to create a struct class pointer that can then be used
196  * in calls to class_device_create().
197  *
198  * Note, the pointer created here is to be destroyed when finished by
199  * making a call to class_destroy().
200  */
201 struct class *class_create(struct module *owner, const char *name)
202 {
203         struct class *cls;
204         int retval;
205
206         cls = kzalloc(sizeof(*cls), GFP_KERNEL);
207         if (!cls) {
208                 retval = -ENOMEM;
209                 goto error;
210         }
211
212         cls->name = name;
213         cls->owner = owner;
214         cls->class_release = class_create_release;
215         cls->release = class_device_create_release;
216
217         retval = class_register(cls);
218         if (retval)
219                 goto error;
220
221         return cls;
222
223 error:
224         kfree(cls);
225         return ERR_PTR(retval);
226 }
227
228 /**
229  * class_destroy - destroys a struct class structure
230  * @cls: pointer to the struct class that is to be destroyed
231  *
232  * Note, the pointer to be destroyed must have been created with a call
233  * to class_create().
234  */
235 void class_destroy(struct class *cls)
236 {
237         if ((cls == NULL) || (IS_ERR(cls)))
238                 return;
239
240         class_unregister(cls);
241 }
242
243 /* Class Device Stuff */
244
245 int class_device_create_file(struct class_device * class_dev,
246                              const struct class_device_attribute * attr)
247 {
248         int error = -EINVAL;
249         if (class_dev)
250                 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
251         return error;
252 }
253
254 void class_device_remove_file(struct class_device * class_dev,
255                               const struct class_device_attribute * attr)
256 {
257         if (class_dev)
258                 sysfs_remove_file(&class_dev->kobj, &attr->attr);
259 }
260
261 int class_device_create_bin_file(struct class_device *class_dev,
262                                  struct bin_attribute *attr)
263 {
264         int error = -EINVAL;
265         if (class_dev)
266                 error = sysfs_create_bin_file(&class_dev->kobj, attr);
267         return error;
268 }
269
270 void class_device_remove_bin_file(struct class_device *class_dev,
271                                   struct bin_attribute *attr)
272 {
273         if (class_dev)
274                 sysfs_remove_bin_file(&class_dev->kobj, attr);
275 }
276
277 static ssize_t
278 class_device_attr_show(struct kobject * kobj, struct attribute * attr,
279                        char * buf)
280 {
281         struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
282         struct class_device * cd = to_class_dev(kobj);
283         ssize_t ret = 0;
284
285         if (class_dev_attr->show)
286                 ret = class_dev_attr->show(cd, buf);
287         return ret;
288 }
289
290 static ssize_t
291 class_device_attr_store(struct kobject * kobj, struct attribute * attr,
292                         const char * buf, size_t count)
293 {
294         struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
295         struct class_device * cd = to_class_dev(kobj);
296         ssize_t ret = 0;
297
298         if (class_dev_attr->store)
299                 ret = class_dev_attr->store(cd, buf, count);
300         return ret;
301 }
302
303 static struct sysfs_ops class_dev_sysfs_ops = {
304         .show   = class_device_attr_show,
305         .store  = class_device_attr_store,
306 };
307
308 static void class_dev_release(struct kobject * kobj)
309 {
310         struct class_device *cd = to_class_dev(kobj);
311         struct class * cls = cd->class;
312
313         pr_debug("device class '%s': release.\n", cd->class_id);
314
315         if (cd->release)
316                 cd->release(cd);
317         else if (cls->release)
318                 cls->release(cd);
319         else {
320                 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
321                         "it is broken and must be fixed.\n",
322                         cd->class_id);
323                 WARN_ON(1);
324         }
325 }
326
327 static struct kobj_type class_device_ktype = {
328         .sysfs_ops      = &class_dev_sysfs_ops,
329         .release        = class_dev_release,
330 };
331
332 static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
333 {
334         struct kobj_type *ktype = get_ktype(kobj);
335
336         if (ktype == &class_device_ktype) {
337                 struct class_device *class_dev = to_class_dev(kobj);
338                 if (class_dev->class)
339                         return 1;
340         }
341         return 0;
342 }
343
344 static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
345 {
346         struct class_device *class_dev = to_class_dev(kobj);
347
348         return class_dev->class->name;
349 }
350
351 #ifdef CONFIG_SYSFS_DEPRECATED
352 char *make_class_name(const char *name, struct kobject *kobj)
353 {
354         char *class_name;
355         int size;
356
357         size = strlen(name) + strlen(kobject_name(kobj)) + 2;
358
359         class_name = kmalloc(size, GFP_KERNEL);
360         if (!class_name)
361                 return NULL;
362
363         strcpy(class_name, name);
364         strcat(class_name, ":");
365         strcat(class_name, kobject_name(kobj));
366         return class_name;
367 }
368
369 static int make_deprecated_class_device_links(struct class_device *class_dev)
370 {
371         char *class_name;
372         int error;
373
374         if (!class_dev->dev)
375                 return 0;
376
377         class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
378         if (class_name)
379                 error = sysfs_create_link(&class_dev->dev->kobj,
380                                           &class_dev->kobj, class_name);
381         else
382                 error = -ENOMEM;
383         kfree(class_name);
384         return error;
385 }
386
387 static void remove_deprecated_class_device_links(struct class_device *class_dev)
388 {
389         char *class_name;
390
391         if (!class_dev->dev)
392                 return;
393
394         class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
395         if (class_name)
396                 sysfs_remove_link(&class_dev->dev->kobj, class_name);
397         kfree(class_name);
398 }
399 #else
400 static inline int make_deprecated_class_device_links(struct class_device *cd)
401 { return 0; }
402 static void remove_deprecated_class_device_links(struct class_device *cd)
403 { }
404 #endif
405
406 static int class_uevent(struct kset *kset, struct kobject *kobj,
407                         struct kobj_uevent_env *env)
408 {
409         struct class_device *class_dev = to_class_dev(kobj);
410         struct device *dev = class_dev->dev;
411         int retval = 0;
412
413         pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
414
415         if (MAJOR(class_dev->devt)) {
416                 add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
417
418                 add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
419         }
420
421         if (dev) {
422                 const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
423                 if (path) {
424                         add_uevent_var(env, "PHYSDEVPATH=%s", path);
425                         kfree(path);
426                 }
427
428                 if (dev->bus)
429                         add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
430
431                 if (dev->driver)
432                         add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
433         }
434
435         if (class_dev->uevent) {
436                 /* have the class device specific function add its stuff */
437                 retval = class_dev->uevent(class_dev, env);
438                 if (retval)
439                         pr_debug("class_dev->uevent() returned %d\n", retval);
440         } else if (class_dev->class->uevent) {
441                 /* have the class specific function add its stuff */
442                 retval = class_dev->class->uevent(class_dev, env);
443                 if (retval)
444                         pr_debug("class->uevent() returned %d\n", retval);
445         }
446
447         return retval;
448 }
449
450 static struct kset_uevent_ops class_uevent_ops = {
451         .filter =       class_uevent_filter,
452         .name =         class_uevent_name,
453         .uevent =       class_uevent,
454 };
455
456 static decl_subsys(class_obj, &class_uevent_ops);
457
458
459 static int class_device_add_attrs(struct class_device * cd)
460 {
461         int i;
462         int error = 0;
463         struct class * cls = cd->class;
464
465         if (cls->class_dev_attrs) {
466                 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
467                         error = class_device_create_file(cd,
468                                                          &cls->class_dev_attrs[i]);
469                         if (error)
470                                 goto Err;
471                 }
472         }
473  Done:
474         return error;
475  Err:
476         while (--i >= 0)
477                 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
478         goto Done;
479 }
480
481 static void class_device_remove_attrs(struct class_device * cd)
482 {
483         int i;
484         struct class * cls = cd->class;
485
486         if (cls->class_dev_attrs) {
487                 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
488                         class_device_remove_file(cd,&cls->class_dev_attrs[i]);
489         }
490 }
491
492 static int class_device_add_groups(struct class_device * cd)
493 {
494         int i;
495         int error = 0;
496
497         if (cd->groups) {
498                 for (i = 0; cd->groups[i]; i++) {
499                         error = sysfs_create_group(&cd->kobj, cd->groups[i]);
500                         if (error) {
501                                 while (--i >= 0)
502                                         sysfs_remove_group(&cd->kobj, cd->groups[i]);
503                                 goto out;
504                         }
505                 }
506         }
507 out:
508         return error;
509 }
510
511 static void class_device_remove_groups(struct class_device * cd)
512 {
513         int i;
514         if (cd->groups) {
515                 for (i = 0; cd->groups[i]; i++) {
516                         sysfs_remove_group(&cd->kobj, cd->groups[i]);
517                 }
518         }
519 }
520
521 static ssize_t show_dev(struct class_device *class_dev, char *buf)
522 {
523         return print_dev_t(buf, class_dev->devt);
524 }
525
526 static struct class_device_attribute class_devt_attr =
527         __ATTR(dev, S_IRUGO, show_dev, NULL);
528
529 static ssize_t store_uevent(struct class_device *class_dev,
530                             const char *buf, size_t count)
531 {
532         kobject_uevent(&class_dev->kobj, KOBJ_ADD);
533         return count;
534 }
535
536 static struct class_device_attribute class_uevent_attr =
537         __ATTR(uevent, S_IWUSR, NULL, store_uevent);
538
539 void class_device_initialize(struct class_device *class_dev)
540 {
541         class_dev->kobj.kset = &class_obj_subsys;
542         class_dev->kobj.ktype = &class_device_ktype;
543         kobject_init(&class_dev->kobj);
544         INIT_LIST_HEAD(&class_dev->node);
545 }
546
547 int class_device_add(struct class_device *class_dev)
548 {
549         struct class *parent_class = NULL;
550         struct class_device *parent_class_dev = NULL;
551         struct class_interface *class_intf;
552         int error = -EINVAL;
553
554         class_dev = class_device_get(class_dev);
555         if (!class_dev)
556                 return -EINVAL;
557
558         if (!strlen(class_dev->class_id))
559                 goto out1;
560
561         parent_class = class_get(class_dev->class);
562         if (!parent_class)
563                 goto out1;
564
565         parent_class_dev = class_device_get(class_dev->parent);
566
567         pr_debug("CLASS: registering class device: ID = '%s'\n",
568                  class_dev->class_id);
569
570         /* first, register with generic layer. */
571         error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
572         if (error)
573                 goto out2;
574
575         if (parent_class_dev)
576                 class_dev->kobj.parent = &parent_class_dev->kobj;
577         else
578                 class_dev->kobj.parent = &parent_class->subsys.kobj;
579
580         error = kobject_add(&class_dev->kobj);
581         if (error)
582                 goto out2;
583
584         /* add the needed attributes to this device */
585         error = sysfs_create_link(&class_dev->kobj,
586                                   &parent_class->subsys.kobj, "subsystem");
587         if (error)
588                 goto out3;
589
590         error = class_device_create_file(class_dev, &class_uevent_attr);
591         if (error)
592                 goto out3;
593
594         if (MAJOR(class_dev->devt)) {
595                 error = class_device_create_file(class_dev, &class_devt_attr);
596                 if (error)
597                         goto out4;
598         }
599
600         error = class_device_add_attrs(class_dev);
601         if (error)
602                 goto out5;
603
604         if (class_dev->dev) {
605                 error = sysfs_create_link(&class_dev->kobj,
606                                           &class_dev->dev->kobj, "device");
607                 if (error)
608                         goto out6;
609         }
610
611         error = class_device_add_groups(class_dev);
612         if (error)
613                 goto out7;
614
615         error = make_deprecated_class_device_links(class_dev);
616         if (error)
617                 goto out8;
618
619         kobject_uevent(&class_dev->kobj, KOBJ_ADD);
620
621         /* notify any interfaces this device is now here */
622         down(&parent_class->sem);
623         list_add_tail(&class_dev->node, &parent_class->children);
624         list_for_each_entry(class_intf, &parent_class->interfaces, node) {
625                 if (class_intf->add)
626                         class_intf->add(class_dev, class_intf);
627         }
628         up(&parent_class->sem);
629
630         goto out1;
631
632  out8:
633         class_device_remove_groups(class_dev);
634  out7:
635         if (class_dev->dev)
636                 sysfs_remove_link(&class_dev->kobj, "device");
637  out6:
638         class_device_remove_attrs(class_dev);
639  out5:
640         if (MAJOR(class_dev->devt))
641                 class_device_remove_file(class_dev, &class_devt_attr);
642  out4:
643         class_device_remove_file(class_dev, &class_uevent_attr);
644  out3:
645         kobject_del(&class_dev->kobj);
646  out2:
647         if(parent_class_dev)
648                 class_device_put(parent_class_dev);
649         class_put(parent_class);
650  out1:
651         class_device_put(class_dev);
652         return error;
653 }
654
655 int class_device_register(struct class_device *class_dev)
656 {
657         class_device_initialize(class_dev);
658         return class_device_add(class_dev);
659 }
660
661 /**
662  * class_device_create - creates a class device and registers it with sysfs
663  * @cls: pointer to the struct class that this device should be registered to.
664  * @parent: pointer to the parent struct class_device of this new device, if any.
665  * @devt: the dev_t for the char device to be added.
666  * @device: a pointer to a struct device that is assiociated with this class device.
667  * @fmt: string for the class device's name
668  *
669  * This function can be used by char device classes.  A struct
670  * class_device will be created in sysfs, registered to the specified
671  * class.
672  * A "dev" file will be created, showing the dev_t for the device, if
673  * the dev_t is not 0,0.
674  * If a pointer to a parent struct class_device is passed in, the newly
675  * created struct class_device will be a child of that device in sysfs.
676  * The pointer to the struct class_device will be returned from the
677  * call.  Any further sysfs files that might be required can be created
678  * using this pointer.
679  *
680  * Note: the struct class passed to this function must have previously
681  * been created with a call to class_create().
682  */
683 struct class_device *class_device_create(struct class *cls,
684                                          struct class_device *parent,
685                                          dev_t devt,
686                                          struct device *device,
687                                          const char *fmt, ...)
688 {
689         va_list args;
690         struct class_device *class_dev = NULL;
691         int retval = -ENODEV;
692
693         if (cls == NULL || IS_ERR(cls))
694                 goto error;
695
696         class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
697         if (!class_dev) {
698                 retval = -ENOMEM;
699                 goto error;
700         }
701
702         class_dev->devt = devt;
703         class_dev->dev = device;
704         class_dev->class = cls;
705         class_dev->parent = parent;
706         class_dev->release = class_device_create_release;
707         class_dev->uevent = class_device_create_uevent;
708
709         va_start(args, fmt);
710         vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
711         va_end(args);
712         retval = class_device_register(class_dev);
713         if (retval)
714                 goto error;
715
716         return class_dev;
717
718 error:
719         kfree(class_dev);
720         return ERR_PTR(retval);
721 }
722
723 void class_device_del(struct class_device *class_dev)
724 {
725         struct class *parent_class = class_dev->class;
726         struct class_device *parent_device = class_dev->parent;
727         struct class_interface *class_intf;
728
729         if (parent_class) {
730                 down(&parent_class->sem);
731                 list_del_init(&class_dev->node);
732                 list_for_each_entry(class_intf, &parent_class->interfaces, node)
733                         if (class_intf->remove)
734                                 class_intf->remove(class_dev, class_intf);
735                 up(&parent_class->sem);
736         }
737
738         if (class_dev->dev) {
739                 remove_deprecated_class_device_links(class_dev);
740                 sysfs_remove_link(&class_dev->kobj, "device");
741         }
742         sysfs_remove_link(&class_dev->kobj, "subsystem");
743         class_device_remove_file(class_dev, &class_uevent_attr);
744         if (MAJOR(class_dev->devt))
745                 class_device_remove_file(class_dev, &class_devt_attr);
746         class_device_remove_attrs(class_dev);
747         class_device_remove_groups(class_dev);
748
749         kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
750         kobject_del(&class_dev->kobj);
751
752         class_device_put(parent_device);
753         class_put(parent_class);
754 }
755
756 void class_device_unregister(struct class_device *class_dev)
757 {
758         pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
759                  class_dev->class_id);
760         class_device_del(class_dev);
761         class_device_put(class_dev);
762 }
763
764 /**
765  * class_device_destroy - removes a class device that was created with class_device_create()
766  * @cls: the pointer to the struct class that this device was registered * with.
767  * @devt: the dev_t of the device that was previously registered.
768  *
769  * This call unregisters and cleans up a class device that was created with a
770  * call to class_device_create()
771  */
772 void class_device_destroy(struct class *cls, dev_t devt)
773 {
774         struct class_device *class_dev = NULL;
775         struct class_device *class_dev_tmp;
776
777         down(&cls->sem);
778         list_for_each_entry(class_dev_tmp, &cls->children, node) {
779                 if (class_dev_tmp->devt == devt) {
780                         class_dev = class_dev_tmp;
781                         break;
782                 }
783         }
784         up(&cls->sem);
785
786         if (class_dev)
787                 class_device_unregister(class_dev);
788 }
789
790 struct class_device * class_device_get(struct class_device *class_dev)
791 {
792         if (class_dev)
793                 return to_class_dev(kobject_get(&class_dev->kobj));
794         return NULL;
795 }
796
797 void class_device_put(struct class_device *class_dev)
798 {
799         if (class_dev)
800                 kobject_put(&class_dev->kobj);
801 }
802
803
804 int class_interface_register(struct class_interface *class_intf)
805 {
806         struct class *parent;
807         struct class_device *class_dev;
808         struct device *dev;
809
810         if (!class_intf || !class_intf->class)
811                 return -ENODEV;
812
813         parent = class_get(class_intf->class);
814         if (!parent)
815                 return -EINVAL;
816
817         down(&parent->sem);
818         list_add_tail(&class_intf->node, &parent->interfaces);
819         if (class_intf->add) {
820                 list_for_each_entry(class_dev, &parent->children, node)
821                         class_intf->add(class_dev, class_intf);
822         }
823         if (class_intf->add_dev) {
824                 list_for_each_entry(dev, &parent->devices, node)
825                         class_intf->add_dev(dev, class_intf);
826         }
827         up(&parent->sem);
828
829         return 0;
830 }
831
832 void class_interface_unregister(struct class_interface *class_intf)
833 {
834         struct class * parent = class_intf->class;
835         struct class_device *class_dev;
836         struct device *dev;
837
838         if (!parent)
839                 return;
840
841         down(&parent->sem);
842         list_del_init(&class_intf->node);
843         if (class_intf->remove) {
844                 list_for_each_entry(class_dev, &parent->children, node)
845                         class_intf->remove(class_dev, class_intf);
846         }
847         if (class_intf->remove_dev) {
848                 list_for_each_entry(dev, &parent->devices, node)
849                         class_intf->remove_dev(dev, class_intf);
850         }
851         up(&parent->sem);
852
853         class_put(parent);
854 }
855
856 int __init classes_init(void)
857 {
858         int retval;
859
860         retval = subsystem_register(&class_subsys);
861         if (retval)
862                 return retval;
863
864         /* ick, this is ugly, the things we go through to keep from showing up
865          * in sysfs... */
866         kset_init(&class_obj_subsys);
867         if (!class_obj_subsys.kobj.parent)
868                 class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
869         return 0;
870 }
871
872 EXPORT_SYMBOL_GPL(class_create_file);
873 EXPORT_SYMBOL_GPL(class_remove_file);
874 EXPORT_SYMBOL_GPL(class_register);
875 EXPORT_SYMBOL_GPL(class_unregister);
876 EXPORT_SYMBOL_GPL(class_create);
877 EXPORT_SYMBOL_GPL(class_destroy);
878
879 EXPORT_SYMBOL_GPL(class_device_register);
880 EXPORT_SYMBOL_GPL(class_device_unregister);
881 EXPORT_SYMBOL_GPL(class_device_initialize);
882 EXPORT_SYMBOL_GPL(class_device_add);
883 EXPORT_SYMBOL_GPL(class_device_del);
884 EXPORT_SYMBOL_GPL(class_device_get);
885 EXPORT_SYMBOL_GPL(class_device_put);
886 EXPORT_SYMBOL_GPL(class_device_create);
887 EXPORT_SYMBOL_GPL(class_device_destroy);
888 EXPORT_SYMBOL_GPL(class_device_create_file);
889 EXPORT_SYMBOL_GPL(class_device_remove_file);
890 EXPORT_SYMBOL_GPL(class_device_create_bin_file);
891 EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
892
893 EXPORT_SYMBOL_GPL(class_interface_register);
894 EXPORT_SYMBOL_GPL(class_interface_unregister);