2 * class.c - basic device class management
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.
9 * This file is released under the GPLv2
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>
22 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
23 #define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
26 class_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
28 struct class_attribute
* class_attr
= to_class_attr(attr
);
29 struct class * dc
= to_class(kobj
);
33 ret
= class_attr
->show(dc
, buf
);
38 class_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
39 const char * buf
, size_t count
)
41 struct class_attribute
* class_attr
= to_class_attr(attr
);
42 struct class * dc
= to_class(kobj
);
45 if (class_attr
->store
)
46 ret
= class_attr
->store(dc
, buf
, count
);
50 static void class_release(struct kobject
* kobj
)
52 struct class *class = to_class(kobj
);
54 pr_debug("class '%s': release.\n", class->name
);
56 if (class->class_release
)
57 class->class_release(class);
59 pr_debug("class '%s' does not have a release() function, "
60 "be careful\n", class->name
);
63 static struct sysfs_ops class_sysfs_ops
= {
64 .show
= class_attr_show
,
65 .store
= class_attr_store
,
68 static struct kobj_type ktype_class
= {
69 .sysfs_ops
= &class_sysfs_ops
,
70 .release
= class_release
,
73 /* Hotplug events for classes go to the class_obj subsys */
74 static decl_subsys(class, &ktype_class
, NULL
);
77 int class_create_file(struct class * cls
, const struct class_attribute
* attr
)
81 error
= sysfs_create_file(&cls
->subsys
.kset
.kobj
, &attr
->attr
);
87 void class_remove_file(struct class * cls
, const struct class_attribute
* attr
)
90 sysfs_remove_file(&cls
->subsys
.kset
.kobj
, &attr
->attr
);
93 static struct class *class_get(struct class *cls
)
96 return container_of(subsys_get(&cls
->subsys
), struct class, subsys
);
100 static void class_put(struct class * cls
)
103 subsys_put(&cls
->subsys
);
107 static int add_class_attrs(struct class * cls
)
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
]);
123 class_remove_file(cls
,&cls
->class_attrs
[i
]);
127 static void remove_class_attrs(struct class * cls
)
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
]);
137 int class_register(struct class * cls
)
141 pr_debug("device class '%s': registering\n", cls
->name
);
143 INIT_LIST_HEAD(&cls
->children
);
144 INIT_LIST_HEAD(&cls
->devices
);
145 INIT_LIST_HEAD(&cls
->interfaces
);
146 init_MUTEX(&cls
->sem
);
147 error
= kobject_set_name(&cls
->subsys
.kset
.kobj
, "%s", cls
->name
);
151 subsys_set_kset(cls
, class_subsys
);
153 error
= subsystem_register(&cls
->subsys
);
155 error
= add_class_attrs(class_get(cls
));
161 void class_unregister(struct class * cls
)
163 pr_debug("device class '%s': unregistering\n", cls
->name
);
164 remove_class_attrs(cls
);
165 subsystem_unregister(&cls
->subsys
);
168 static void class_create_release(struct class *cls
)
170 pr_debug("%s called for %s\n", __FUNCTION__
, cls
->name
);
174 static void class_device_create_release(struct class_device
*class_dev
)
176 pr_debug("%s called for %s\n", __FUNCTION__
, class_dev
->class_id
);
180 /* needed to allow these devices to have parent class devices */
181 static int class_device_create_uevent(struct class_device
*class_dev
,
182 char **envp
, int num_envp
,
183 char *buffer
, int buffer_size
)
185 pr_debug("%s called for %s\n", __FUNCTION__
, class_dev
->class_id
);
190 * class_create - create a struct class structure
191 * @owner: pointer to the module that is to "own" this struct class
192 * @name: pointer to a string for the name of this class.
194 * This is used to create a struct class pointer that can then be used
195 * in calls to class_device_create().
197 * Note, the pointer created here is to be destroyed when finished by
198 * making a call to class_destroy().
200 struct class *class_create(struct module
*owner
, char *name
)
205 cls
= kzalloc(sizeof(*cls
), GFP_KERNEL
);
213 cls
->class_release
= class_create_release
;
214 cls
->release
= class_device_create_release
;
216 retval
= class_register(cls
);
224 return ERR_PTR(retval
);
228 * class_destroy - destroys a struct class structure
229 * @cs: pointer to the struct class that is to be destroyed
231 * Note, the pointer to be destroyed must have been created with a call
234 void class_destroy(struct class *cls
)
236 if ((cls
== NULL
) || (IS_ERR(cls
)))
239 class_unregister(cls
);
242 /* Class Device Stuff */
244 int class_device_create_file(struct class_device
* class_dev
,
245 const struct class_device_attribute
* attr
)
249 error
= sysfs_create_file(&class_dev
->kobj
, &attr
->attr
);
253 void class_device_remove_file(struct class_device
* class_dev
,
254 const struct class_device_attribute
* attr
)
257 sysfs_remove_file(&class_dev
->kobj
, &attr
->attr
);
260 int class_device_create_bin_file(struct class_device
*class_dev
,
261 struct bin_attribute
*attr
)
265 error
= sysfs_create_bin_file(&class_dev
->kobj
, attr
);
269 void class_device_remove_bin_file(struct class_device
*class_dev
,
270 struct bin_attribute
*attr
)
273 sysfs_remove_bin_file(&class_dev
->kobj
, attr
);
277 class_device_attr_show(struct kobject
* kobj
, struct attribute
* attr
,
280 struct class_device_attribute
* class_dev_attr
= to_class_dev_attr(attr
);
281 struct class_device
* cd
= to_class_dev(kobj
);
284 if (class_dev_attr
->show
)
285 ret
= class_dev_attr
->show(cd
, buf
);
290 class_device_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
291 const char * buf
, size_t count
)
293 struct class_device_attribute
* class_dev_attr
= to_class_dev_attr(attr
);
294 struct class_device
* cd
= to_class_dev(kobj
);
297 if (class_dev_attr
->store
)
298 ret
= class_dev_attr
->store(cd
, buf
, count
);
302 static struct sysfs_ops class_dev_sysfs_ops
= {
303 .show
= class_device_attr_show
,
304 .store
= class_device_attr_store
,
307 static void class_dev_release(struct kobject
* kobj
)
309 struct class_device
*cd
= to_class_dev(kobj
);
310 struct class * cls
= cd
->class;
312 pr_debug("device class '%s': release.\n", cd
->class_id
);
314 kfree(cd
->devt_attr
);
315 cd
->devt_attr
= NULL
;
319 else if (cls
->release
)
322 printk(KERN_ERR
"Class Device '%s' does not have a release() function, "
323 "it is broken and must be fixed.\n",
329 static struct kobj_type ktype_class_device
= {
330 .sysfs_ops
= &class_dev_sysfs_ops
,
331 .release
= class_dev_release
,
334 static int class_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
336 struct kobj_type
*ktype
= get_ktype(kobj
);
338 if (ktype
== &ktype_class_device
) {
339 struct class_device
*class_dev
= to_class_dev(kobj
);
340 if (class_dev
->class)
346 static const char *class_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
348 struct class_device
*class_dev
= to_class_dev(kobj
);
350 return class_dev
->class->name
;
353 static int class_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
354 int num_envp
, char *buffer
, int buffer_size
)
356 struct class_device
*class_dev
= to_class_dev(kobj
);
361 pr_debug("%s - name = %s\n", __FUNCTION__
, class_dev
->class_id
);
363 if (class_dev
->dev
) {
364 /* add physical device, backing this device */
365 struct device
*dev
= class_dev
->dev
;
366 char *path
= kobject_get_path(&dev
->kobj
, GFP_KERNEL
);
368 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
369 &length
, "PHYSDEVPATH=%s", path
);
373 add_uevent_var(envp
, num_envp
, &i
,
374 buffer
, buffer_size
, &length
,
375 "PHYSDEVBUS=%s", dev
->bus
->name
);
378 add_uevent_var(envp
, num_envp
, &i
,
379 buffer
, buffer_size
, &length
,
380 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
383 if (MAJOR(class_dev
->devt
)) {
384 add_uevent_var(envp
, num_envp
, &i
,
385 buffer
, buffer_size
, &length
,
386 "MAJOR=%u", MAJOR(class_dev
->devt
));
388 add_uevent_var(envp
, num_envp
, &i
,
389 buffer
, buffer_size
, &length
,
390 "MINOR=%u", MINOR(class_dev
->devt
));
393 /* terminate, set to next free slot, shrink available space */
397 buffer
= &buffer
[length
];
398 buffer_size
-= length
;
400 if (class_dev
->uevent
) {
401 /* have the class device specific function add its stuff */
402 retval
= class_dev
->uevent(class_dev
, envp
, num_envp
,
403 buffer
, buffer_size
);
405 pr_debug("class_dev->uevent() returned %d\n", retval
);
406 } else if (class_dev
->class->uevent
) {
407 /* have the class specific function add its stuff */
408 retval
= class_dev
->class->uevent(class_dev
, envp
, num_envp
,
409 buffer
, buffer_size
);
411 pr_debug("class->uevent() returned %d\n", retval
);
417 static struct kset_uevent_ops class_uevent_ops
= {
418 .filter
= class_uevent_filter
,
419 .name
= class_uevent_name
,
420 .uevent
= class_uevent
,
423 static decl_subsys(class_obj
, &ktype_class_device
, &class_uevent_ops
);
426 static int class_device_add_attrs(struct class_device
* cd
)
430 struct class * cls
= cd
->class;
432 if (cls
->class_dev_attrs
) {
433 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++) {
434 error
= class_device_create_file(cd
,
435 &cls
->class_dev_attrs
[i
]);
444 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
448 static void class_device_remove_attrs(struct class_device
* cd
)
451 struct class * cls
= cd
->class;
453 if (cls
->class_dev_attrs
) {
454 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++)
455 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
459 static int class_device_add_groups(struct class_device
* cd
)
465 for (i
= 0; cd
->groups
[i
]; i
++) {
466 error
= sysfs_create_group(&cd
->kobj
, cd
->groups
[i
]);
469 sysfs_remove_group(&cd
->kobj
, cd
->groups
[i
]);
478 static void class_device_remove_groups(struct class_device
* cd
)
482 for (i
= 0; cd
->groups
[i
]; i
++) {
483 sysfs_remove_group(&cd
->kobj
, cd
->groups
[i
]);
488 static ssize_t
show_dev(struct class_device
*class_dev
, char *buf
)
490 return print_dev_t(buf
, class_dev
->devt
);
493 static ssize_t
store_uevent(struct class_device
*class_dev
,
494 const char *buf
, size_t count
)
496 kobject_uevent(&class_dev
->kobj
, KOBJ_ADD
);
500 void class_device_initialize(struct class_device
*class_dev
)
502 kobj_set_kset_s(class_dev
, class_obj_subsys
);
503 kobject_init(&class_dev
->kobj
);
504 INIT_LIST_HEAD(&class_dev
->node
);
507 char *make_class_name(const char *name
, struct kobject
*kobj
)
512 size
= strlen(name
) + strlen(kobject_name(kobj
)) + 2;
514 class_name
= kmalloc(size
, GFP_KERNEL
);
516 return ERR_PTR(-ENOMEM
);
518 strcpy(class_name
, name
);
519 strcat(class_name
, ":");
520 strcat(class_name
, kobject_name(kobj
));
524 int class_device_add(struct class_device
*class_dev
)
526 struct class *parent_class
= NULL
;
527 struct class_device
*parent_class_dev
= NULL
;
528 struct class_interface
*class_intf
;
529 char *class_name
= NULL
;
532 class_dev
= class_device_get(class_dev
);
536 if (!strlen(class_dev
->class_id
))
539 parent_class
= class_get(class_dev
->class);
543 parent_class_dev
= class_device_get(class_dev
->parent
);
545 pr_debug("CLASS: registering class device: ID = '%s'\n",
546 class_dev
->class_id
);
548 /* first, register with generic layer. */
549 error
= kobject_set_name(&class_dev
->kobj
, "%s", class_dev
->class_id
);
553 if (parent_class_dev
)
554 class_dev
->kobj
.parent
= &parent_class_dev
->kobj
;
556 class_dev
->kobj
.parent
= &parent_class
->subsys
.kset
.kobj
;
558 error
= kobject_add(&class_dev
->kobj
);
562 /* add the needed attributes to this device */
563 sysfs_create_link(&class_dev
->kobj
, &parent_class
->subsys
.kset
.kobj
, "subsystem");
564 class_dev
->uevent_attr
.attr
.name
= "uevent";
565 class_dev
->uevent_attr
.attr
.mode
= S_IWUSR
;
566 class_dev
->uevent_attr
.attr
.owner
= parent_class
->owner
;
567 class_dev
->uevent_attr
.store
= store_uevent
;
568 error
= class_device_create_file(class_dev
, &class_dev
->uevent_attr
);
572 if (MAJOR(class_dev
->devt
)) {
573 struct class_device_attribute
*attr
;
574 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
579 attr
->attr
.name
= "dev";
580 attr
->attr
.mode
= S_IRUGO
;
581 attr
->attr
.owner
= parent_class
->owner
;
582 attr
->show
= show_dev
;
583 error
= class_device_create_file(class_dev
, attr
);
589 class_dev
->devt_attr
= attr
;
592 error
= class_device_add_attrs(class_dev
);
596 if (class_dev
->dev
) {
597 class_name
= make_class_name(class_dev
->class->name
,
599 error
= sysfs_create_link(&class_dev
->kobj
,
600 &class_dev
->dev
->kobj
, "device");
603 error
= sysfs_create_link(&class_dev
->dev
->kobj
, &class_dev
->kobj
,
609 error
= class_device_add_groups(class_dev
);
613 kobject_uevent(&class_dev
->kobj
, KOBJ_ADD
);
615 /* notify any interfaces this device is now here */
616 down(&parent_class
->sem
);
617 list_add_tail(&class_dev
->node
, &parent_class
->children
);
618 list_for_each_entry(class_intf
, &parent_class
->interfaces
, node
) {
620 class_intf
->add(class_dev
, class_intf
);
622 up(&parent_class
->sem
);
628 sysfs_remove_link(&class_dev
->kobj
, class_name
);
631 sysfs_remove_link(&class_dev
->kobj
, "device");
633 class_device_remove_attrs(class_dev
);
635 if (class_dev
->devt_attr
)
636 class_device_remove_file(class_dev
, class_dev
->devt_attr
);
638 class_device_remove_file(class_dev
, &class_dev
->uevent_attr
);
640 kobject_del(&class_dev
->kobj
);
643 class_device_put(parent_class_dev
);
644 class_put(parent_class
);
646 class_device_put(class_dev
);
651 int class_device_register(struct class_device
*class_dev
)
653 class_device_initialize(class_dev
);
654 return class_device_add(class_dev
);
658 * class_device_create - creates a class device and registers it with sysfs
659 * @cs: pointer to the struct class that this device should be registered to.
660 * @parent: pointer to the parent struct class_device of this new device, if any.
661 * @dev: the dev_t for the char device to be added.
662 * @device: a pointer to a struct device that is assiociated with this class device.
663 * @fmt: string for the class device's name
665 * This function can be used by char device classes. A struct
666 * class_device will be created in sysfs, registered to the specified
668 * A "dev" file will be created, showing the dev_t for the device, if
669 * the dev_t is not 0,0.
670 * If a pointer to a parent struct class_device is passed in, the newly
671 * created struct class_device will be a child of that device in sysfs.
672 * The pointer to the struct class_device will be returned from the
673 * call. Any further sysfs files that might be required can be created
674 * using this pointer.
676 * Note: the struct class passed to this function must have previously
677 * been created with a call to class_create().
679 struct class_device
*class_device_create(struct class *cls
,
680 struct class_device
*parent
,
682 struct device
*device
, char *fmt
, ...)
685 struct class_device
*class_dev
= NULL
;
686 int retval
= -ENODEV
;
688 if (cls
== NULL
|| IS_ERR(cls
))
691 class_dev
= kzalloc(sizeof(*class_dev
), GFP_KERNEL
);
697 class_dev
->devt
= devt
;
698 class_dev
->dev
= device
;
699 class_dev
->class = cls
;
700 class_dev
->parent
= parent
;
701 class_dev
->release
= class_device_create_release
;
702 class_dev
->uevent
= class_device_create_uevent
;
705 vsnprintf(class_dev
->class_id
, BUS_ID_SIZE
, fmt
, args
);
707 retval
= class_device_register(class_dev
);
715 return ERR_PTR(retval
);
718 void class_device_del(struct class_device
*class_dev
)
720 struct class *parent_class
= class_dev
->class;
721 struct class_device
*parent_device
= class_dev
->parent
;
722 struct class_interface
*class_intf
;
723 char *class_name
= NULL
;
726 down(&parent_class
->sem
);
727 list_del_init(&class_dev
->node
);
728 list_for_each_entry(class_intf
, &parent_class
->interfaces
, node
)
729 if (class_intf
->remove
)
730 class_intf
->remove(class_dev
, class_intf
);
731 up(&parent_class
->sem
);
734 if (class_dev
->dev
) {
735 class_name
= make_class_name(class_dev
->class->name
,
737 sysfs_remove_link(&class_dev
->kobj
, "device");
738 sysfs_remove_link(&class_dev
->dev
->kobj
, class_name
);
740 sysfs_remove_link(&class_dev
->kobj
, "subsystem");
741 class_device_remove_file(class_dev
, &class_dev
->uevent_attr
);
742 if (class_dev
->devt_attr
)
743 class_device_remove_file(class_dev
, class_dev
->devt_attr
);
744 class_device_remove_attrs(class_dev
);
745 class_device_remove_groups(class_dev
);
747 kobject_uevent(&class_dev
->kobj
, KOBJ_REMOVE
);
748 kobject_del(&class_dev
->kobj
);
750 class_device_put(parent_device
);
751 class_put(parent_class
);
755 void class_device_unregister(struct class_device
*class_dev
)
757 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
758 class_dev
->class_id
);
759 class_device_del(class_dev
);
760 class_device_put(class_dev
);
764 * class_device_destroy - removes a class device that was created with class_device_create()
765 * @cls: the pointer to the struct class that this device was registered * with.
766 * @dev: the dev_t of the device that was previously registered.
768 * This call unregisters and cleans up a class device that was created with a
769 * call to class_device_create()
771 void class_device_destroy(struct class *cls
, dev_t devt
)
773 struct class_device
*class_dev
= NULL
;
774 struct class_device
*class_dev_tmp
;
777 list_for_each_entry(class_dev_tmp
, &cls
->children
, node
) {
778 if (class_dev_tmp
->devt
== devt
) {
779 class_dev
= class_dev_tmp
;
786 class_device_unregister(class_dev
);
789 int class_device_rename(struct class_device
*class_dev
, char *new_name
)
792 char *old_class_name
= NULL
, *new_class_name
= NULL
;
794 class_dev
= class_device_get(class_dev
);
798 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev
->class_id
,
802 old_class_name
= make_class_name(class_dev
->class->name
,
805 strlcpy(class_dev
->class_id
, new_name
, KOBJ_NAME_LEN
);
807 error
= kobject_rename(&class_dev
->kobj
, new_name
);
809 if (class_dev
->dev
) {
810 new_class_name
= make_class_name(class_dev
->class->name
,
812 sysfs_create_link(&class_dev
->dev
->kobj
, &class_dev
->kobj
,
814 sysfs_remove_link(&class_dev
->dev
->kobj
, old_class_name
);
816 class_device_put(class_dev
);
818 kfree(old_class_name
);
819 kfree(new_class_name
);
824 struct class_device
* class_device_get(struct class_device
*class_dev
)
827 return to_class_dev(kobject_get(&class_dev
->kobj
));
831 void class_device_put(struct class_device
*class_dev
)
834 kobject_put(&class_dev
->kobj
);
838 int class_interface_register(struct class_interface
*class_intf
)
840 struct class *parent
;
841 struct class_device
*class_dev
;
843 if (!class_intf
|| !class_intf
->class)
846 parent
= class_get(class_intf
->class);
851 list_add_tail(&class_intf
->node
, &parent
->interfaces
);
852 if (class_intf
->add
) {
853 list_for_each_entry(class_dev
, &parent
->children
, node
)
854 class_intf
->add(class_dev
, class_intf
);
861 void class_interface_unregister(struct class_interface
*class_intf
)
863 struct class * parent
= class_intf
->class;
864 struct class_device
*class_dev
;
870 list_del_init(&class_intf
->node
);
871 if (class_intf
->remove
) {
872 list_for_each_entry(class_dev
, &parent
->children
, node
)
873 class_intf
->remove(class_dev
, class_intf
);
882 int __init
classes_init(void)
886 retval
= subsystem_register(&class_subsys
);
890 /* ick, this is ugly, the things we go through to keep from showing up
892 subsystem_init(&class_obj_subsys
);
893 if (!class_obj_subsys
.kset
.subsys
)
894 class_obj_subsys
.kset
.subsys
= &class_obj_subsys
;
898 EXPORT_SYMBOL_GPL(class_create_file
);
899 EXPORT_SYMBOL_GPL(class_remove_file
);
900 EXPORT_SYMBOL_GPL(class_register
);
901 EXPORT_SYMBOL_GPL(class_unregister
);
902 EXPORT_SYMBOL_GPL(class_create
);
903 EXPORT_SYMBOL_GPL(class_destroy
);
905 EXPORT_SYMBOL_GPL(class_device_register
);
906 EXPORT_SYMBOL_GPL(class_device_unregister
);
907 EXPORT_SYMBOL_GPL(class_device_initialize
);
908 EXPORT_SYMBOL_GPL(class_device_add
);
909 EXPORT_SYMBOL_GPL(class_device_del
);
910 EXPORT_SYMBOL_GPL(class_device_get
);
911 EXPORT_SYMBOL_GPL(class_device_put
);
912 EXPORT_SYMBOL_GPL(class_device_create
);
913 EXPORT_SYMBOL_GPL(class_device_destroy
);
914 EXPORT_SYMBOL_GPL(class_device_create_file
);
915 EXPORT_SYMBOL_GPL(class_device_remove_file
);
916 EXPORT_SYMBOL_GPL(class_device_create_bin_file
);
917 EXPORT_SYMBOL_GPL(class_device_remove_bin_file
);
919 EXPORT_SYMBOL_GPL(class_interface_register
);
920 EXPORT_SYMBOL_GPL(class_interface_unregister
);