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