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