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 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.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(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 kset_init(&cls->class_dirs);
147 init_MUTEX(&cls->sem);
148 error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
152 subsys_set_kset(cls, 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 char **envp, int num_envp,
184 char *buffer, int buffer_size)
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);
315 kfree(cd->devt_attr);
316 cd->devt_attr = NULL;
320 else if (cls->release)
323 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
324 "it is broken and must be fixed.\n",
330 static struct kobj_type ktype_class_device = {
331 .sysfs_ops = &class_dev_sysfs_ops,
332 .release = class_dev_release,
335 static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
337 struct kobj_type *ktype = get_ktype(kobj);
339 if (ktype == &ktype_class_device) {
340 struct class_device *class_dev = to_class_dev(kobj);
341 if (class_dev->class)
347 static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
349 struct class_device *class_dev = to_class_dev(kobj);
351 return class_dev->class->name;
354 #ifdef CONFIG_SYSFS_DEPRECATED
355 char *make_class_name(const char *name, struct kobject *kobj)
360 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
362 class_name = kmalloc(size, GFP_KERNEL);
366 strcpy(class_name, name);
367 strcat(class_name, ":");
368 strcat(class_name, kobject_name(kobj));
372 static int make_deprecated_class_device_links(struct class_device *class_dev)
380 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
382 error = sysfs_create_link(&class_dev->dev->kobj,
383 &class_dev->kobj, class_name);
390 static void remove_deprecated_class_device_links(struct class_device *class_dev)
397 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
399 sysfs_remove_link(&class_dev->dev->kobj, class_name);
403 static inline int make_deprecated_class_device_links(struct class_device *cd)
405 static void remove_deprecated_class_device_links(struct class_device *cd)
409 static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp,
410 int num_envp, char *buffer, int buffer_size)
412 struct class_device *class_dev = to_class_dev(kobj);
413 struct device *dev = class_dev->dev;
418 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
420 if (MAJOR(class_dev->devt)) {
421 add_uevent_var(envp, num_envp, &i,
422 buffer, buffer_size, &length,
423 "MAJOR=%u", MAJOR(class_dev->devt));
425 add_uevent_var(envp, num_envp, &i,
426 buffer, buffer_size, &length,
427 "MINOR=%u", MINOR(class_dev->devt));
431 const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
433 add_uevent_var(envp, num_envp, &i,
434 buffer, buffer_size, &length,
435 "PHYSDEVPATH=%s", path);
440 add_uevent_var(envp, num_envp, &i,
441 buffer, buffer_size, &length,
442 "PHYSDEVBUS=%s", dev->bus->name);
445 add_uevent_var(envp, num_envp, &i,
446 buffer, buffer_size, &length,
447 "PHYSDEVDRIVER=%s", dev->driver->name);
450 /* terminate, set to next free slot, shrink available space */
454 buffer = &buffer[length];
455 buffer_size -= length;
457 if (class_dev->uevent) {
458 /* have the class device specific function add its stuff */
459 retval = class_dev->uevent(class_dev, envp, num_envp,
460 buffer, buffer_size);
462 pr_debug("class_dev->uevent() returned %d\n", retval);
463 } else if (class_dev->class->uevent) {
464 /* have the class specific function add its stuff */
465 retval = class_dev->class->uevent(class_dev, envp, num_envp,
466 buffer, buffer_size);
468 pr_debug("class->uevent() returned %d\n", retval);
474 static struct kset_uevent_ops class_uevent_ops = {
475 .filter = class_uevent_filter,
476 .name = class_uevent_name,
477 .uevent = class_uevent,
480 static decl_subsys(class_obj, &ktype_class_device, &class_uevent_ops);
483 static int class_device_add_attrs(struct class_device * cd)
487 struct class * cls = cd->class;
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]);
501 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
505 static void class_device_remove_attrs(struct class_device * cd)
508 struct class * cls = cd->class;
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]);
516 static int class_device_add_groups(struct class_device * cd)
522 for (i = 0; cd->groups[i]; i++) {
523 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
526 sysfs_remove_group(&cd->kobj, cd->groups[i]);
535 static void class_device_remove_groups(struct class_device * cd)
539 for (i = 0; cd->groups[i]; i++) {
540 sysfs_remove_group(&cd->kobj, cd->groups[i]);
545 static ssize_t show_dev(struct class_device *class_dev, char *buf)
547 return print_dev_t(buf, class_dev->devt);
550 static ssize_t store_uevent(struct class_device *class_dev,
551 const char *buf, size_t count)
553 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
557 void class_device_initialize(struct class_device *class_dev)
559 kobj_set_kset_s(class_dev, class_obj_subsys);
560 kobject_init(&class_dev->kobj);
561 INIT_LIST_HEAD(&class_dev->node);
564 int class_device_add(struct class_device *class_dev)
566 struct class *parent_class = NULL;
567 struct class_device *parent_class_dev = NULL;
568 struct class_interface *class_intf;
571 class_dev = class_device_get(class_dev);
575 if (!strlen(class_dev->class_id))
578 parent_class = class_get(class_dev->class);
582 parent_class_dev = class_device_get(class_dev->parent);
584 pr_debug("CLASS: registering class device: ID = '%s'\n",
585 class_dev->class_id);
587 /* first, register with generic layer. */
588 error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
592 if (parent_class_dev)
593 class_dev->kobj.parent = &parent_class_dev->kobj;
595 class_dev->kobj.parent = &parent_class->subsys.kobj;
597 error = kobject_add(&class_dev->kobj);
601 /* add the needed attributes to this device */
602 error = sysfs_create_link(&class_dev->kobj,
603 &parent_class->subsys.kobj, "subsystem");
606 class_dev->uevent_attr.attr.name = "uevent";
607 class_dev->uevent_attr.attr.mode = S_IWUSR;
608 class_dev->uevent_attr.attr.owner = parent_class->owner;
609 class_dev->uevent_attr.store = store_uevent;
610 error = class_device_create_file(class_dev, &class_dev->uevent_attr);
614 if (MAJOR(class_dev->devt)) {
615 struct class_device_attribute *attr;
616 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
621 attr->attr.name = "dev";
622 attr->attr.mode = S_IRUGO;
623 attr->attr.owner = parent_class->owner;
624 attr->show = show_dev;
625 error = class_device_create_file(class_dev, attr);
631 class_dev->devt_attr = attr;
634 error = class_device_add_attrs(class_dev);
638 if (class_dev->dev) {
639 error = sysfs_create_link(&class_dev->kobj,
640 &class_dev->dev->kobj, "device");
645 error = class_device_add_groups(class_dev);
649 error = make_deprecated_class_device_links(class_dev);
653 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
655 /* notify any interfaces this device is now here */
656 down(&parent_class->sem);
657 list_add_tail(&class_dev->node, &parent_class->children);
658 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
660 class_intf->add(class_dev, class_intf);
662 up(&parent_class->sem);
667 class_device_remove_groups(class_dev);
670 sysfs_remove_link(&class_dev->kobj, "device");
672 class_device_remove_attrs(class_dev);
674 if (class_dev->devt_attr)
675 class_device_remove_file(class_dev, class_dev->devt_attr);
677 class_device_remove_file(class_dev, &class_dev->uevent_attr);
679 kobject_del(&class_dev->kobj);
682 class_device_put(parent_class_dev);
683 class_put(parent_class);
685 class_device_put(class_dev);
689 int class_device_register(struct class_device *class_dev)
691 class_device_initialize(class_dev);
692 return class_device_add(class_dev);
696 * class_device_create - creates a class device and registers it with sysfs
697 * @cls: pointer to the struct class that this device should be registered to.
698 * @parent: pointer to the parent struct class_device of this new device, if any.
699 * @devt: the dev_t for the char device to be added.
700 * @device: a pointer to a struct device that is assiociated with this class device.
701 * @fmt: string for the class device's name
703 * This function can be used by char device classes. A struct
704 * class_device will be created in sysfs, registered to the specified
706 * A "dev" file will be created, showing the dev_t for the device, if
707 * the dev_t is not 0,0.
708 * If a pointer to a parent struct class_device is passed in, the newly
709 * created struct class_device will be a child of that device in sysfs.
710 * The pointer to the struct class_device will be returned from the
711 * call. Any further sysfs files that might be required can be created
712 * using this pointer.
714 * Note: the struct class passed to this function must have previously
715 * been created with a call to class_create().
717 struct class_device *class_device_create(struct class *cls,
718 struct class_device *parent,
720 struct device *device,
721 const char *fmt, ...)
724 struct class_device *class_dev = NULL;
725 int retval = -ENODEV;
727 if (cls == NULL || IS_ERR(cls))
730 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
736 class_dev->devt = devt;
737 class_dev->dev = device;
738 class_dev->class = cls;
739 class_dev->parent = parent;
740 class_dev->release = class_device_create_release;
741 class_dev->uevent = class_device_create_uevent;
744 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
746 retval = class_device_register(class_dev);
754 return ERR_PTR(retval);
757 void class_device_del(struct class_device *class_dev)
759 struct class *parent_class = class_dev->class;
760 struct class_device *parent_device = class_dev->parent;
761 struct class_interface *class_intf;
764 down(&parent_class->sem);
765 list_del_init(&class_dev->node);
766 list_for_each_entry(class_intf, &parent_class->interfaces, node)
767 if (class_intf->remove)
768 class_intf->remove(class_dev, class_intf);
769 up(&parent_class->sem);
772 if (class_dev->dev) {
773 remove_deprecated_class_device_links(class_dev);
774 sysfs_remove_link(&class_dev->kobj, "device");
776 sysfs_remove_link(&class_dev->kobj, "subsystem");
777 class_device_remove_file(class_dev, &class_dev->uevent_attr);
778 if (class_dev->devt_attr)
779 class_device_remove_file(class_dev, class_dev->devt_attr);
780 class_device_remove_attrs(class_dev);
781 class_device_remove_groups(class_dev);
783 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
784 kobject_del(&class_dev->kobj);
786 class_device_put(parent_device);
787 class_put(parent_class);
790 void class_device_unregister(struct class_device *class_dev)
792 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
793 class_dev->class_id);
794 class_device_del(class_dev);
795 class_device_put(class_dev);
799 * class_device_destroy - removes a class device that was created with class_device_create()
800 * @cls: the pointer to the struct class that this device was registered * with.
801 * @devt: the dev_t of the device that was previously registered.
803 * This call unregisters and cleans up a class device that was created with a
804 * call to class_device_create()
806 void class_device_destroy(struct class *cls, dev_t devt)
808 struct class_device *class_dev = NULL;
809 struct class_device *class_dev_tmp;
812 list_for_each_entry(class_dev_tmp, &cls->children, node) {
813 if (class_dev_tmp->devt == devt) {
814 class_dev = class_dev_tmp;
821 class_device_unregister(class_dev);
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;
844 if (!class_intf || !class_intf->class)
847 parent = class_get(class_intf->class);
852 list_add_tail(&class_intf->node, &parent->interfaces);
853 if (class_intf->add) {
854 list_for_each_entry(class_dev, &parent->children, node)
855 class_intf->add(class_dev, class_intf);
857 if (class_intf->add_dev) {
858 list_for_each_entry(dev, &parent->devices, node)
859 class_intf->add_dev(dev, class_intf);
866 void class_interface_unregister(struct class_interface *class_intf)
868 struct class * parent = class_intf->class;
869 struct class_device *class_dev;
876 list_del_init(&class_intf->node);
877 if (class_intf->remove) {
878 list_for_each_entry(class_dev, &parent->children, node)
879 class_intf->remove(class_dev, class_intf);
881 if (class_intf->remove_dev) {
882 list_for_each_entry(dev, &parent->devices, node)
883 class_intf->remove_dev(dev, class_intf);
890 int __init classes_init(void)
894 retval = subsystem_register(&class_subsys);
898 /* ick, this is ugly, the things we go through to keep from showing up
900 subsystem_init(&class_obj_subsys);
901 if (!class_obj_subsys.kobj.parent)
902 class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
906 EXPORT_SYMBOL_GPL(class_create_file);
907 EXPORT_SYMBOL_GPL(class_remove_file);
908 EXPORT_SYMBOL_GPL(class_register);
909 EXPORT_SYMBOL_GPL(class_unregister);
910 EXPORT_SYMBOL_GPL(class_create);
911 EXPORT_SYMBOL_GPL(class_destroy);
913 EXPORT_SYMBOL_GPL(class_device_register);
914 EXPORT_SYMBOL_GPL(class_device_unregister);
915 EXPORT_SYMBOL_GPL(class_device_initialize);
916 EXPORT_SYMBOL_GPL(class_device_add);
917 EXPORT_SYMBOL_GPL(class_device_del);
918 EXPORT_SYMBOL_GPL(class_device_get);
919 EXPORT_SYMBOL_GPL(class_device_put);
920 EXPORT_SYMBOL_GPL(class_device_create);
921 EXPORT_SYMBOL_GPL(class_device_destroy);
922 EXPORT_SYMBOL_GPL(class_device_create_file);
923 EXPORT_SYMBOL_GPL(class_device_remove_file);
924 EXPORT_SYMBOL_GPL(class_device_create_bin_file);
925 EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
927 EXPORT_SYMBOL_GPL(class_interface_register);
928 EXPORT_SYMBOL_GPL(class_interface_unregister);