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 struct kset *class_kset;
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_kset;
153 cls->subsys.kobj.ktype = &class_ktype;
155 error = kset_register(&cls->subsys);
157 error = add_class_attrs(class_get(cls));
163 void class_unregister(struct class * cls)
165 pr_debug("device class '%s': unregistering\n", cls->name);
166 remove_class_attrs(cls);
167 kset_unregister(&cls->subsys);
170 static void class_create_release(struct class *cls)
172 pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
176 static void class_device_create_release(struct class_device *class_dev)
178 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
182 /* needed to allow these devices to have parent class devices */
183 static int class_device_create_uevent(struct class_device *class_dev,
184 struct kobj_uevent_env *env)
186 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
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.
195 * This is used to create a struct class pointer that can then be used
196 * in calls to class_device_create().
198 * Note, the pointer created here is to be destroyed when finished by
199 * making a call to class_destroy().
201 struct class *class_create(struct module *owner, const char *name)
206 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
214 cls->class_release = class_create_release;
215 cls->release = class_device_create_release;
217 retval = class_register(cls);
225 return ERR_PTR(retval);
229 * class_destroy - destroys a struct class structure
230 * @cls: pointer to the struct class that is to be destroyed
232 * Note, the pointer to be destroyed must have been created with a call
235 void class_destroy(struct class *cls)
237 if ((cls == NULL) || (IS_ERR(cls)))
240 class_unregister(cls);
243 /* Class Device Stuff */
245 int class_device_create_file(struct class_device * class_dev,
246 const struct class_device_attribute * attr)
250 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
254 void class_device_remove_file(struct class_device * class_dev,
255 const struct class_device_attribute * attr)
258 sysfs_remove_file(&class_dev->kobj, &attr->attr);
261 int class_device_create_bin_file(struct class_device *class_dev,
262 struct bin_attribute *attr)
266 error = sysfs_create_bin_file(&class_dev->kobj, attr);
270 void class_device_remove_bin_file(struct class_device *class_dev,
271 struct bin_attribute *attr)
274 sysfs_remove_bin_file(&class_dev->kobj, attr);
278 class_device_attr_show(struct kobject * kobj, struct attribute * attr,
281 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
282 struct class_device * cd = to_class_dev(kobj);
285 if (class_dev_attr->show)
286 ret = class_dev_attr->show(cd, buf);
291 class_device_attr_store(struct kobject * kobj, struct attribute * attr,
292 const char * buf, size_t count)
294 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
295 struct class_device * cd = to_class_dev(kobj);
298 if (class_dev_attr->store)
299 ret = class_dev_attr->store(cd, buf, count);
303 static struct sysfs_ops class_dev_sysfs_ops = {
304 .show = class_device_attr_show,
305 .store = class_device_attr_store,
308 static void class_dev_release(struct kobject * kobj)
310 struct class_device *cd = to_class_dev(kobj);
311 struct class * cls = cd->class;
313 pr_debug("device class '%s': release.\n", cd->class_id);
317 else if (cls->release)
320 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
321 "it is broken and must be fixed.\n",
327 static struct kobj_type class_device_ktype = {
328 .sysfs_ops = &class_dev_sysfs_ops,
329 .release = class_dev_release,
332 static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
334 struct kobj_type *ktype = get_ktype(kobj);
336 if (ktype == &class_device_ktype) {
337 struct class_device *class_dev = to_class_dev(kobj);
338 if (class_dev->class)
344 static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
346 struct class_device *class_dev = to_class_dev(kobj);
348 return class_dev->class->name;
351 #ifdef CONFIG_SYSFS_DEPRECATED
352 char *make_class_name(const char *name, struct kobject *kobj)
357 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
359 class_name = kmalloc(size, GFP_KERNEL);
363 strcpy(class_name, name);
364 strcat(class_name, ":");
365 strcat(class_name, kobject_name(kobj));
369 static int make_deprecated_class_device_links(struct class_device *class_dev)
377 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
379 error = sysfs_create_link(&class_dev->dev->kobj,
380 &class_dev->kobj, class_name);
387 static void remove_deprecated_class_device_links(struct class_device *class_dev)
394 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
396 sysfs_remove_link(&class_dev->dev->kobj, class_name);
400 static inline int make_deprecated_class_device_links(struct class_device *cd)
402 static void remove_deprecated_class_device_links(struct class_device *cd)
406 static int class_uevent(struct kset *kset, struct kobject *kobj,
407 struct kobj_uevent_env *env)
409 struct class_device *class_dev = to_class_dev(kobj);
410 struct device *dev = class_dev->dev;
413 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
415 if (MAJOR(class_dev->devt)) {
416 add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
418 add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
422 const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
424 add_uevent_var(env, "PHYSDEVPATH=%s", path);
429 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
432 add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
435 if (class_dev->uevent) {
436 /* have the class device specific function add its stuff */
437 retval = class_dev->uevent(class_dev, env);
439 pr_debug("class_dev->uevent() returned %d\n", retval);
440 } else if (class_dev->class->uevent) {
441 /* have the class specific function add its stuff */
442 retval = class_dev->class->uevent(class_dev, env);
444 pr_debug("class->uevent() returned %d\n", retval);
450 static struct kset_uevent_ops class_uevent_ops = {
451 .filter = class_uevent_filter,
452 .name = class_uevent_name,
453 .uevent = class_uevent,
457 * DO NOT copy how this is created, kset_create_and_add() should be
458 * called, but this is a hold-over from the old-way and will be deleted
461 static struct kset class_obj_subsys = {
462 .kobj = { .k_name = "class_obj", },
463 .uevent_ops = &class_uevent_ops,
466 static int class_device_add_attrs(struct class_device * cd)
470 struct class * cls = cd->class;
472 if (cls->class_dev_attrs) {
473 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
474 error = class_device_create_file(cd,
475 &cls->class_dev_attrs[i]);
484 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
488 static void class_device_remove_attrs(struct class_device * cd)
491 struct class * cls = cd->class;
493 if (cls->class_dev_attrs) {
494 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
495 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
499 static int class_device_add_groups(struct class_device * cd)
505 for (i = 0; cd->groups[i]; i++) {
506 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
509 sysfs_remove_group(&cd->kobj, cd->groups[i]);
518 static void class_device_remove_groups(struct class_device * cd)
522 for (i = 0; cd->groups[i]; i++) {
523 sysfs_remove_group(&cd->kobj, cd->groups[i]);
528 static ssize_t show_dev(struct class_device *class_dev, char *buf)
530 return print_dev_t(buf, class_dev->devt);
533 static struct class_device_attribute class_devt_attr =
534 __ATTR(dev, S_IRUGO, show_dev, NULL);
536 static ssize_t store_uevent(struct class_device *class_dev,
537 const char *buf, size_t count)
539 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
543 static struct class_device_attribute class_uevent_attr =
544 __ATTR(uevent, S_IWUSR, NULL, store_uevent);
546 void class_device_initialize(struct class_device *class_dev)
548 class_dev->kobj.kset = &class_obj_subsys;
549 kobject_init_ng(&class_dev->kobj, &class_device_ktype);
550 INIT_LIST_HEAD(&class_dev->node);
553 int class_device_add(struct class_device *class_dev)
555 struct class *parent_class = NULL;
556 struct class_device *parent_class_dev = NULL;
557 struct class_interface *class_intf;
560 class_dev = class_device_get(class_dev);
564 if (!strlen(class_dev->class_id))
567 parent_class = class_get(class_dev->class);
571 parent_class_dev = class_device_get(class_dev->parent);
573 pr_debug("CLASS: registering class device: ID = '%s'\n",
574 class_dev->class_id);
576 /* first, register with generic layer. */
577 if (parent_class_dev)
578 class_dev->kobj.parent = &parent_class_dev->kobj;
580 class_dev->kobj.parent = &parent_class->subsys.kobj;
582 error = kobject_add_ng(&class_dev->kobj, class_dev->kobj.parent,
583 "%s", class_dev->class_id);
587 /* add the needed attributes to this device */
588 error = sysfs_create_link(&class_dev->kobj,
589 &parent_class->subsys.kobj, "subsystem");
593 error = class_device_create_file(class_dev, &class_uevent_attr);
597 if (MAJOR(class_dev->devt)) {
598 error = class_device_create_file(class_dev, &class_devt_attr);
603 error = class_device_add_attrs(class_dev);
607 if (class_dev->dev) {
608 error = sysfs_create_link(&class_dev->kobj,
609 &class_dev->dev->kobj, "device");
614 error = class_device_add_groups(class_dev);
618 error = make_deprecated_class_device_links(class_dev);
622 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
624 /* notify any interfaces this device is now here */
625 down(&parent_class->sem);
626 list_add_tail(&class_dev->node, &parent_class->children);
627 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
629 class_intf->add(class_dev, class_intf);
631 up(&parent_class->sem);
636 class_device_remove_groups(class_dev);
639 sysfs_remove_link(&class_dev->kobj, "device");
641 class_device_remove_attrs(class_dev);
643 if (MAJOR(class_dev->devt))
644 class_device_remove_file(class_dev, &class_devt_attr);
646 class_device_remove_file(class_dev, &class_uevent_attr);
648 kobject_del(&class_dev->kobj);
651 class_device_put(parent_class_dev);
652 class_put(parent_class);
654 class_device_put(class_dev);
658 int class_device_register(struct class_device *class_dev)
660 class_device_initialize(class_dev);
661 return class_device_add(class_dev);
665 * class_device_create - creates a class device and registers it with sysfs
666 * @cls: pointer to the struct class that this device should be registered to.
667 * @parent: pointer to the parent struct class_device of this new device, if any.
668 * @devt: the dev_t for the char device to be added.
669 * @device: a pointer to a struct device that is assiociated with this class device.
670 * @fmt: string for the class device's name
672 * This function can be used by char device classes. A struct
673 * class_device will be created in sysfs, registered to the specified
675 * A "dev" file will be created, showing the dev_t for the device, if
676 * the dev_t is not 0,0.
677 * If a pointer to a parent struct class_device is passed in, the newly
678 * created struct class_device will be a child of that device in sysfs.
679 * The pointer to the struct class_device will be returned from the
680 * call. Any further sysfs files that might be required can be created
681 * using this pointer.
683 * Note: the struct class passed to this function must have previously
684 * been created with a call to class_create().
686 struct class_device *class_device_create(struct class *cls,
687 struct class_device *parent,
689 struct device *device,
690 const char *fmt, ...)
693 struct class_device *class_dev = NULL;
694 int retval = -ENODEV;
696 if (cls == NULL || IS_ERR(cls))
699 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
705 class_dev->devt = devt;
706 class_dev->dev = device;
707 class_dev->class = cls;
708 class_dev->parent = parent;
709 class_dev->release = class_device_create_release;
710 class_dev->uevent = class_device_create_uevent;
713 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
715 retval = class_device_register(class_dev);
723 return ERR_PTR(retval);
726 void class_device_del(struct class_device *class_dev)
728 struct class *parent_class = class_dev->class;
729 struct class_device *parent_device = class_dev->parent;
730 struct class_interface *class_intf;
733 down(&parent_class->sem);
734 list_del_init(&class_dev->node);
735 list_for_each_entry(class_intf, &parent_class->interfaces, node)
736 if (class_intf->remove)
737 class_intf->remove(class_dev, class_intf);
738 up(&parent_class->sem);
741 if (class_dev->dev) {
742 remove_deprecated_class_device_links(class_dev);
743 sysfs_remove_link(&class_dev->kobj, "device");
745 sysfs_remove_link(&class_dev->kobj, "subsystem");
746 class_device_remove_file(class_dev, &class_uevent_attr);
747 if (MAJOR(class_dev->devt))
748 class_device_remove_file(class_dev, &class_devt_attr);
749 class_device_remove_attrs(class_dev);
750 class_device_remove_groups(class_dev);
752 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
753 kobject_del(&class_dev->kobj);
755 class_device_put(parent_device);
756 class_put(parent_class);
759 void class_device_unregister(struct class_device *class_dev)
761 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
762 class_dev->class_id);
763 class_device_del(class_dev);
764 class_device_put(class_dev);
768 * class_device_destroy - removes a class device that was created with class_device_create()
769 * @cls: the pointer to the struct class that this device was registered * with.
770 * @devt: the dev_t of the device that was previously registered.
772 * This call unregisters and cleans up a class device that was created with a
773 * call to class_device_create()
775 void class_device_destroy(struct class *cls, dev_t devt)
777 struct class_device *class_dev = NULL;
778 struct class_device *class_dev_tmp;
781 list_for_each_entry(class_dev_tmp, &cls->children, node) {
782 if (class_dev_tmp->devt == devt) {
783 class_dev = class_dev_tmp;
790 class_device_unregister(class_dev);
793 struct class_device * class_device_get(struct class_device *class_dev)
796 return to_class_dev(kobject_get(&class_dev->kobj));
800 void class_device_put(struct class_device *class_dev)
803 kobject_put(&class_dev->kobj);
807 int class_interface_register(struct class_interface *class_intf)
809 struct class *parent;
810 struct class_device *class_dev;
813 if (!class_intf || !class_intf->class)
816 parent = class_get(class_intf->class);
821 list_add_tail(&class_intf->node, &parent->interfaces);
822 if (class_intf->add) {
823 list_for_each_entry(class_dev, &parent->children, node)
824 class_intf->add(class_dev, class_intf);
826 if (class_intf->add_dev) {
827 list_for_each_entry(dev, &parent->devices, node)
828 class_intf->add_dev(dev, class_intf);
835 void class_interface_unregister(struct class_interface *class_intf)
837 struct class * parent = class_intf->class;
838 struct class_device *class_dev;
845 list_del_init(&class_intf->node);
846 if (class_intf->remove) {
847 list_for_each_entry(class_dev, &parent->children, node)
848 class_intf->remove(class_dev, class_intf);
850 if (class_intf->remove_dev) {
851 list_for_each_entry(dev, &parent->devices, node)
852 class_intf->remove_dev(dev, class_intf);
859 int __init classes_init(void)
861 class_kset = kset_create_and_add("class", NULL, NULL);
865 /* ick, this is ugly, the things we go through to keep from showing up
867 kset_init(&class_obj_subsys);
868 if (!class_obj_subsys.kobj.parent)
869 class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
873 EXPORT_SYMBOL_GPL(class_create_file);
874 EXPORT_SYMBOL_GPL(class_remove_file);
875 EXPORT_SYMBOL_GPL(class_register);
876 EXPORT_SYMBOL_GPL(class_unregister);
877 EXPORT_SYMBOL_GPL(class_create);
878 EXPORT_SYMBOL_GPL(class_destroy);
880 EXPORT_SYMBOL_GPL(class_device_register);
881 EXPORT_SYMBOL_GPL(class_device_unregister);
882 EXPORT_SYMBOL_GPL(class_device_initialize);
883 EXPORT_SYMBOL_GPL(class_device_add);
884 EXPORT_SYMBOL_GPL(class_device_del);
885 EXPORT_SYMBOL_GPL(class_device_get);
886 EXPORT_SYMBOL_GPL(class_device_put);
887 EXPORT_SYMBOL_GPL(class_device_create);
888 EXPORT_SYMBOL_GPL(class_device_destroy);
889 EXPORT_SYMBOL_GPL(class_device_create_file);
890 EXPORT_SYMBOL_GPL(class_device_remove_file);
891 EXPORT_SYMBOL_GPL(class_device_create_bin_file);
892 EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
894 EXPORT_SYMBOL_GPL(class_interface_register);
895 EXPORT_SYMBOL_GPL(class_interface_unregister);