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.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 class_ktype = {
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, &class_ktype, NULL);
77 int class_create_file(struct class * cls, const struct class_attribute * attr)
81 error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
87 void class_remove_file(struct class * cls, const struct class_attribute * attr)
90 sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
93 static struct class *class_get(struct class *cls)
96 return container_of(kset_get(&cls->subsys), struct class, subsys);
100 static void class_put(struct class * cls)
103 kset_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 kset_init(&cls->class_dirs);
147 init_MUTEX(&cls->sem);
148 error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
152 cls->subsys.kobj.kset = &class_subsys;
154 error = subsystem_register(&cls->subsys);
156 error = add_class_attrs(class_get(cls));
162 void class_unregister(struct class * cls)
164 pr_debug("device class '%s': unregistering\n", cls->name);
165 remove_class_attrs(cls);
166 subsystem_unregister(&cls->subsys);
169 static void class_create_release(struct class *cls)
171 pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
175 static void class_device_create_release(struct class_device *class_dev)
177 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
181 /* needed to allow these devices to have parent class devices */
182 static int class_device_create_uevent(struct class_device *class_dev,
183 struct kobj_uevent_env *env)
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, const 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 * @cls: 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);
261 class_device_attr_show(struct kobject * kobj, struct attribute * attr,
264 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
265 struct class_device * cd = to_class_dev(kobj);
268 if (class_dev_attr->show)
269 ret = class_dev_attr->show(cd, buf);
274 class_device_attr_store(struct kobject * kobj, struct attribute * attr,
275 const char * buf, size_t count)
277 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
278 struct class_device * cd = to_class_dev(kobj);
281 if (class_dev_attr->store)
282 ret = class_dev_attr->store(cd, buf, count);
286 static struct sysfs_ops class_dev_sysfs_ops = {
287 .show = class_device_attr_show,
288 .store = class_device_attr_store,
291 static void class_dev_release(struct kobject * kobj)
293 struct class_device *cd = to_class_dev(kobj);
294 struct class * cls = cd->class;
296 pr_debug("device class '%s': release.\n", cd->class_id);
300 else if (cls->release)
303 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
304 "it is broken and must be fixed.\n",
310 static struct kobj_type class_device_ktype = {
311 .sysfs_ops = &class_dev_sysfs_ops,
312 .release = class_dev_release,
315 static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
317 struct kobj_type *ktype = get_ktype(kobj);
319 if (ktype == &class_device_ktype) {
320 struct class_device *class_dev = to_class_dev(kobj);
321 if (class_dev->class)
327 static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
329 struct class_device *class_dev = to_class_dev(kobj);
331 return class_dev->class->name;
334 #ifdef CONFIG_SYSFS_DEPRECATED
335 char *make_class_name(const char *name, struct kobject *kobj)
340 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
342 class_name = kmalloc(size, GFP_KERNEL);
346 strcpy(class_name, name);
347 strcat(class_name, ":");
348 strcat(class_name, kobject_name(kobj));
352 static int make_deprecated_class_device_links(struct class_device *class_dev)
360 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
362 error = sysfs_create_link(&class_dev->dev->kobj,
363 &class_dev->kobj, class_name);
370 static void remove_deprecated_class_device_links(struct class_device *class_dev)
377 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
379 sysfs_remove_link(&class_dev->dev->kobj, class_name);
383 static inline int make_deprecated_class_device_links(struct class_device *cd)
385 static void remove_deprecated_class_device_links(struct class_device *cd)
389 static int class_uevent(struct kset *kset, struct kobject *kobj,
390 struct kobj_uevent_env *env)
392 struct class_device *class_dev = to_class_dev(kobj);
393 struct device *dev = class_dev->dev;
396 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
398 if (MAJOR(class_dev->devt)) {
399 add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
401 add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
405 const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
407 add_uevent_var(env, "PHYSDEVPATH=%s", path);
412 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
415 add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
418 if (class_dev->uevent) {
419 /* have the class device specific function add its stuff */
420 retval = class_dev->uevent(class_dev, env);
422 pr_debug("class_dev->uevent() returned %d\n", retval);
423 } else if (class_dev->class->uevent) {
424 /* have the class specific function add its stuff */
425 retval = class_dev->class->uevent(class_dev, env);
427 pr_debug("class->uevent() returned %d\n", retval);
433 static struct kset_uevent_ops class_uevent_ops = {
434 .filter = class_uevent_filter,
435 .name = class_uevent_name,
436 .uevent = class_uevent,
439 static decl_subsys(class_obj, &class_device_ktype, &class_uevent_ops);
442 static int class_device_add_attrs(struct class_device * cd)
446 struct class * cls = cd->class;
448 if (cls->class_dev_attrs) {
449 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
450 error = class_device_create_file(cd,
451 &cls->class_dev_attrs[i]);
460 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
464 static void class_device_remove_attrs(struct class_device * cd)
467 struct class * cls = cd->class;
469 if (cls->class_dev_attrs) {
470 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
471 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
475 static int class_device_add_groups(struct class_device * cd)
481 for (i = 0; cd->groups[i]; i++) {
482 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
485 sysfs_remove_group(&cd->kobj, cd->groups[i]);
494 static void class_device_remove_groups(struct class_device * cd)
498 for (i = 0; cd->groups[i]; i++) {
499 sysfs_remove_group(&cd->kobj, cd->groups[i]);
504 static ssize_t show_dev(struct class_device *class_dev, char *buf)
506 return print_dev_t(buf, class_dev->devt);
509 static struct class_device_attribute class_devt_attr =
510 __ATTR(dev, S_IRUGO, show_dev, NULL);
512 static ssize_t store_uevent(struct class_device *class_dev,
513 const char *buf, size_t count)
515 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
519 static struct class_device_attribute class_uevent_attr =
520 __ATTR(uevent, S_IWUSR, NULL, store_uevent);
522 void class_device_initialize(struct class_device *class_dev)
524 kobj_set_kset_s(class_dev, class_obj_subsys);
525 kobject_init(&class_dev->kobj);
526 INIT_LIST_HEAD(&class_dev->node);
529 int class_device_add(struct class_device *class_dev)
531 struct class *parent_class = NULL;
532 struct class_device *parent_class_dev = NULL;
533 struct class_interface *class_intf;
536 class_dev = class_device_get(class_dev);
540 if (!strlen(class_dev->class_id))
543 parent_class = class_get(class_dev->class);
547 parent_class_dev = class_device_get(class_dev->parent);
549 pr_debug("CLASS: registering class device: ID = '%s'\n",
550 class_dev->class_id);
552 /* first, register with generic layer. */
553 error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
557 if (parent_class_dev)
558 class_dev->kobj.parent = &parent_class_dev->kobj;
560 class_dev->kobj.parent = &parent_class->subsys.kobj;
562 error = kobject_add(&class_dev->kobj);
566 /* add the needed attributes to this device */
567 error = sysfs_create_link(&class_dev->kobj,
568 &parent_class->subsys.kobj, "subsystem");
572 error = class_device_create_file(class_dev, &class_uevent_attr);
576 if (MAJOR(class_dev->devt)) {
577 error = class_device_create_file(class_dev, &class_devt_attr);
582 error = class_device_add_attrs(class_dev);
586 if (class_dev->dev) {
587 error = sysfs_create_link(&class_dev->kobj,
588 &class_dev->dev->kobj, "device");
593 error = class_device_add_groups(class_dev);
597 error = make_deprecated_class_device_links(class_dev);
601 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
603 /* notify any interfaces this device is now here */
604 down(&parent_class->sem);
605 list_add_tail(&class_dev->node, &parent_class->children);
606 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
608 class_intf->add(class_dev, class_intf);
610 up(&parent_class->sem);
615 class_device_remove_groups(class_dev);
618 sysfs_remove_link(&class_dev->kobj, "device");
620 class_device_remove_attrs(class_dev);
622 if (MAJOR(class_dev->devt))
623 class_device_remove_file(class_dev, &class_devt_attr);
625 class_device_remove_file(class_dev, &class_uevent_attr);
627 kobject_del(&class_dev->kobj);
630 class_device_put(parent_class_dev);
631 class_put(parent_class);
633 class_device_put(class_dev);
637 int class_device_register(struct class_device *class_dev)
639 class_device_initialize(class_dev);
640 return class_device_add(class_dev);
644 * class_device_create - creates a class device and registers it with sysfs
645 * @cls: pointer to the struct class that this device should be registered to.
646 * @parent: pointer to the parent struct class_device of this new device, if any.
647 * @devt: the dev_t for the char device to be added.
648 * @device: a pointer to a struct device that is assiociated with this class device.
649 * @fmt: string for the class device's name
651 * This function can be used by char device classes. A struct
652 * class_device will be created in sysfs, registered to the specified
654 * A "dev" file will be created, showing the dev_t for the device, if
655 * the dev_t is not 0,0.
656 * If a pointer to a parent struct class_device is passed in, the newly
657 * created struct class_device will be a child of that device in sysfs.
658 * The pointer to the struct class_device will be returned from the
659 * call. Any further sysfs files that might be required can be created
660 * using this pointer.
662 * Note: the struct class passed to this function must have previously
663 * been created with a call to class_create().
665 struct class_device *class_device_create(struct class *cls,
666 struct class_device *parent,
668 struct device *device,
669 const char *fmt, ...)
672 struct class_device *class_dev = NULL;
673 int retval = -ENODEV;
675 if (cls == NULL || IS_ERR(cls))
678 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
684 class_dev->devt = devt;
685 class_dev->dev = device;
686 class_dev->class = cls;
687 class_dev->parent = parent;
688 class_dev->release = class_device_create_release;
689 class_dev->uevent = class_device_create_uevent;
692 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
694 retval = class_device_register(class_dev);
702 return ERR_PTR(retval);
705 void class_device_del(struct class_device *class_dev)
707 struct class *parent_class = class_dev->class;
708 struct class_device *parent_device = class_dev->parent;
709 struct class_interface *class_intf;
712 down(&parent_class->sem);
713 list_del_init(&class_dev->node);
714 list_for_each_entry(class_intf, &parent_class->interfaces, node)
715 if (class_intf->remove)
716 class_intf->remove(class_dev, class_intf);
717 up(&parent_class->sem);
720 if (class_dev->dev) {
721 remove_deprecated_class_device_links(class_dev);
722 sysfs_remove_link(&class_dev->kobj, "device");
724 sysfs_remove_link(&class_dev->kobj, "subsystem");
725 class_device_remove_file(class_dev, &class_uevent_attr);
726 if (MAJOR(class_dev->devt))
727 class_device_remove_file(class_dev, &class_devt_attr);
728 class_device_remove_attrs(class_dev);
729 class_device_remove_groups(class_dev);
731 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
732 kobject_del(&class_dev->kobj);
734 class_device_put(parent_device);
735 class_put(parent_class);
738 void class_device_unregister(struct class_device *class_dev)
740 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
741 class_dev->class_id);
742 class_device_del(class_dev);
743 class_device_put(class_dev);
747 * class_device_destroy - removes a class device that was created with class_device_create()
748 * @cls: the pointer to the struct class that this device was registered * with.
749 * @devt: the dev_t of the device that was previously registered.
751 * This call unregisters and cleans up a class device that was created with a
752 * call to class_device_create()
754 void class_device_destroy(struct class *cls, dev_t devt)
756 struct class_device *class_dev = NULL;
757 struct class_device *class_dev_tmp;
760 list_for_each_entry(class_dev_tmp, &cls->children, node) {
761 if (class_dev_tmp->devt == devt) {
762 class_dev = class_dev_tmp;
769 class_device_unregister(class_dev);
772 struct class_device * class_device_get(struct class_device *class_dev)
775 return to_class_dev(kobject_get(&class_dev->kobj));
779 void class_device_put(struct class_device *class_dev)
782 kobject_put(&class_dev->kobj);
786 int class_interface_register(struct class_interface *class_intf)
788 struct class *parent;
789 struct class_device *class_dev;
792 if (!class_intf || !class_intf->class)
795 parent = class_get(class_intf->class);
800 list_add_tail(&class_intf->node, &parent->interfaces);
801 if (class_intf->add) {
802 list_for_each_entry(class_dev, &parent->children, node)
803 class_intf->add(class_dev, class_intf);
805 if (class_intf->add_dev) {
806 list_for_each_entry(dev, &parent->devices, node)
807 class_intf->add_dev(dev, class_intf);
814 void class_interface_unregister(struct class_interface *class_intf)
816 struct class * parent = class_intf->class;
817 struct class_device *class_dev;
824 list_del_init(&class_intf->node);
825 if (class_intf->remove) {
826 list_for_each_entry(class_dev, &parent->children, node)
827 class_intf->remove(class_dev, class_intf);
829 if (class_intf->remove_dev) {
830 list_for_each_entry(dev, &parent->devices, node)
831 class_intf->remove_dev(dev, class_intf);
838 int __init classes_init(void)
842 retval = subsystem_register(&class_subsys);
846 /* ick, this is ugly, the things we go through to keep from showing up
848 kset_init(&class_obj_subsys);
849 if (!class_obj_subsys.kobj.parent)
850 class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
854 EXPORT_SYMBOL_GPL(class_create_file);
855 EXPORT_SYMBOL_GPL(class_remove_file);
856 EXPORT_SYMBOL_GPL(class_register);
857 EXPORT_SYMBOL_GPL(class_unregister);
858 EXPORT_SYMBOL_GPL(class_create);
859 EXPORT_SYMBOL_GPL(class_destroy);
861 EXPORT_SYMBOL_GPL(class_device_register);
862 EXPORT_SYMBOL_GPL(class_device_unregister);
863 EXPORT_SYMBOL_GPL(class_device_initialize);
864 EXPORT_SYMBOL_GPL(class_device_add);
865 EXPORT_SYMBOL_GPL(class_device_del);
866 EXPORT_SYMBOL_GPL(class_device_get);
867 EXPORT_SYMBOL_GPL(class_device_put);
868 EXPORT_SYMBOL_GPL(class_device_create);
869 EXPORT_SYMBOL_GPL(class_device_destroy);
870 EXPORT_SYMBOL_GPL(class_device_create_file);
871 EXPORT_SYMBOL_GPL(class_device_remove_file);
873 EXPORT_SYMBOL_GPL(class_interface_register);
874 EXPORT_SYMBOL_GPL(class_interface_unregister);