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 extern struct subsystem devices_subsys;
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)
28 class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
30 struct class_attribute * class_attr = to_class_attr(attr);
31 struct class * dc = to_class(kobj);
35 ret = class_attr->show(dc, buf);
40 class_attr_store(struct kobject * kobj, struct attribute * attr,
41 const char * buf, size_t count)
43 struct class_attribute * class_attr = to_class_attr(attr);
44 struct class * dc = to_class(kobj);
47 if (class_attr->store)
48 ret = class_attr->store(dc, buf, count);
52 static void class_release(struct kobject * kobj)
54 struct class *class = to_class(kobj);
56 pr_debug("class '%s': release.\n", class->name);
58 if (class->class_release)
59 class->class_release(class);
61 pr_debug("class '%s' does not have a release() function, "
62 "be careful\n", class->name);
65 static struct sysfs_ops class_sysfs_ops = {
66 .show = class_attr_show,
67 .store = class_attr_store,
70 static struct kobj_type ktype_class = {
71 .sysfs_ops = &class_sysfs_ops,
72 .release = class_release,
75 /* Hotplug events for classes go to the class_obj subsys */
76 static decl_subsys(class, &ktype_class, NULL);
79 int class_create_file(struct class * cls, const struct class_attribute * attr)
83 error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr);
89 void class_remove_file(struct class * cls, const struct class_attribute * attr)
92 sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr);
95 static struct class *class_get(struct class *cls)
98 return container_of(subsys_get(&cls->subsys), struct class, subsys);
102 static void class_put(struct class * cls)
105 subsys_put(&cls->subsys);
109 static int add_class_attrs(struct class * cls)
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]);
125 class_remove_file(cls,&cls->class_attrs[i]);
129 static void remove_class_attrs(struct class * cls)
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]);
139 int class_register(struct class * cls)
143 pr_debug("device class '%s': registering\n", cls->name);
145 INIT_LIST_HEAD(&cls->children);
146 INIT_LIST_HEAD(&cls->devices);
147 INIT_LIST_HEAD(&cls->interfaces);
148 init_MUTEX(&cls->sem);
149 error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
153 subsys_set_kset(cls, class_subsys);
155 error = subsystem_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 subsystem_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 char **envp, int num_envp,
185 char *buffer, int buffer_size)
187 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
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.
196 * This is used to create a struct class pointer that can then be used
197 * in calls to class_device_create().
199 * Note, the pointer created here is to be destroyed when finished by
200 * making a call to class_destroy().
202 struct class *class_create(struct module *owner, const char *name)
207 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
215 cls->class_release = class_create_release;
216 cls->release = class_device_create_release;
218 retval = class_register(cls);
226 return ERR_PTR(retval);
230 * class_destroy - destroys a struct class structure
231 * @cs: pointer to the struct class that is to be destroyed
233 * Note, the pointer to be destroyed must have been created with a call
236 void class_destroy(struct class *cls)
238 if ((cls == NULL) || (IS_ERR(cls)))
241 class_unregister(cls);
244 /* Class Device Stuff */
246 int class_device_create_file(struct class_device * class_dev,
247 const struct class_device_attribute * attr)
251 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
255 void class_device_remove_file(struct class_device * class_dev,
256 const struct class_device_attribute * attr)
259 sysfs_remove_file(&class_dev->kobj, &attr->attr);
262 int class_device_create_bin_file(struct class_device *class_dev,
263 struct bin_attribute *attr)
267 error = sysfs_create_bin_file(&class_dev->kobj, attr);
271 void class_device_remove_bin_file(struct class_device *class_dev,
272 struct bin_attribute *attr)
275 sysfs_remove_bin_file(&class_dev->kobj, attr);
279 class_device_attr_show(struct kobject * kobj, struct attribute * attr,
282 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
283 struct class_device * cd = to_class_dev(kobj);
286 if (class_dev_attr->show)
287 ret = class_dev_attr->show(cd, buf);
292 class_device_attr_store(struct kobject * kobj, struct attribute * attr,
293 const char * buf, size_t count)
295 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
296 struct class_device * cd = to_class_dev(kobj);
299 if (class_dev_attr->store)
300 ret = class_dev_attr->store(cd, buf, count);
304 static struct sysfs_ops class_dev_sysfs_ops = {
305 .show = class_device_attr_show,
306 .store = class_device_attr_store,
309 static void class_dev_release(struct kobject * kobj)
311 struct class_device *cd = to_class_dev(kobj);
312 struct class * cls = cd->class;
314 pr_debug("device class '%s': release.\n", cd->class_id);
316 kfree(cd->devt_attr);
317 cd->devt_attr = NULL;
321 else if (cls->release)
324 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
325 "it is broken and must be fixed.\n",
331 static struct kobj_type ktype_class_device = {
332 .sysfs_ops = &class_dev_sysfs_ops,
333 .release = class_dev_release,
336 static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
338 struct kobj_type *ktype = get_ktype(kobj);
340 if (ktype == &ktype_class_device) {
341 struct class_device *class_dev = to_class_dev(kobj);
342 if (class_dev->class)
348 static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
350 struct class_device *class_dev = to_class_dev(kobj);
352 return class_dev->class->name;
355 static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp,
356 int num_envp, char *buffer, int buffer_size)
358 struct class_device *class_dev = to_class_dev(kobj);
363 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
365 if (class_dev->dev) {
366 /* add device, backing this class device (deprecated) */
367 struct device *dev = class_dev->dev;
368 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
370 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
371 &length, "PHYSDEVPATH=%s", path);
375 add_uevent_var(envp, num_envp, &i,
376 buffer, buffer_size, &length,
377 "PHYSDEVBUS=%s", dev->bus->name);
380 add_uevent_var(envp, num_envp, &i,
381 buffer, buffer_size, &length,
382 "PHYSDEVDRIVER=%s", dev->driver->name);
385 if (MAJOR(class_dev->devt)) {
386 add_uevent_var(envp, num_envp, &i,
387 buffer, buffer_size, &length,
388 "MAJOR=%u", MAJOR(class_dev->devt));
390 add_uevent_var(envp, num_envp, &i,
391 buffer, buffer_size, &length,
392 "MINOR=%u", MINOR(class_dev->devt));
395 /* terminate, set to next free slot, shrink available space */
399 buffer = &buffer[length];
400 buffer_size -= length;
402 if (class_dev->uevent) {
403 /* have the class device specific function add its stuff */
404 retval = class_dev->uevent(class_dev, envp, num_envp,
405 buffer, buffer_size);
407 pr_debug("class_dev->uevent() returned %d\n", retval);
408 } else if (class_dev->class->uevent) {
409 /* have the class specific function add its stuff */
410 retval = class_dev->class->uevent(class_dev, envp, num_envp,
411 buffer, buffer_size);
413 pr_debug("class->uevent() returned %d\n", retval);
419 static struct kset_uevent_ops class_uevent_ops = {
420 .filter = class_uevent_filter,
421 .name = class_uevent_name,
422 .uevent = class_uevent,
425 static decl_subsys(class_obj, &ktype_class_device, &class_uevent_ops);
428 static int class_device_add_attrs(struct class_device * cd)
432 struct class * cls = cd->class;
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]);
446 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
450 static void class_device_remove_attrs(struct class_device * cd)
453 struct class * cls = cd->class;
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]);
461 static int class_device_add_groups(struct class_device * cd)
467 for (i = 0; cd->groups[i]; i++) {
468 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
471 sysfs_remove_group(&cd->kobj, cd->groups[i]);
480 static void class_device_remove_groups(struct class_device * cd)
484 for (i = 0; cd->groups[i]; i++) {
485 sysfs_remove_group(&cd->kobj, cd->groups[i]);
490 static ssize_t show_dev(struct class_device *class_dev, char *buf)
492 return print_dev_t(buf, class_dev->devt);
495 static ssize_t store_uevent(struct class_device *class_dev,
496 const char *buf, size_t count)
498 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
502 void class_device_initialize(struct class_device *class_dev)
504 kobj_set_kset_s(class_dev, class_obj_subsys);
505 kobject_init(&class_dev->kobj);
506 INIT_LIST_HEAD(&class_dev->node);
509 char *make_class_name(const char *name, struct kobject *kobj)
514 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
516 class_name = kmalloc(size, GFP_KERNEL);
518 return ERR_PTR(-ENOMEM);
520 strcpy(class_name, name);
521 strcat(class_name, ":");
522 strcat(class_name, kobject_name(kobj));
526 int class_device_add(struct class_device *class_dev)
528 struct class *parent_class = NULL;
529 struct class_device *parent_class_dev = NULL;
530 struct class_interface *class_intf;
531 char *class_name = NULL;
534 class_dev = class_device_get(class_dev);
538 if (!strlen(class_dev->class_id))
541 parent_class = class_get(class_dev->class);
545 parent_class_dev = class_device_get(class_dev->parent);
547 pr_debug("CLASS: registering class device: ID = '%s'\n",
548 class_dev->class_id);
550 /* first, register with generic layer. */
551 error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
555 if (parent_class_dev)
556 class_dev->kobj.parent = &parent_class_dev->kobj;
558 class_dev->kobj.parent = &parent_class->subsys.kset.kobj;
560 error = kobject_add(&class_dev->kobj);
564 /* add the needed attributes to this device */
565 sysfs_create_link(&class_dev->kobj, &parent_class->subsys.kset.kobj, "subsystem");
566 class_dev->uevent_attr.attr.name = "uevent";
567 class_dev->uevent_attr.attr.mode = S_IWUSR;
568 class_dev->uevent_attr.attr.owner = parent_class->owner;
569 class_dev->uevent_attr.store = store_uevent;
570 error = class_device_create_file(class_dev, &class_dev->uevent_attr);
574 if (MAJOR(class_dev->devt)) {
575 struct class_device_attribute *attr;
576 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
581 attr->attr.name = "dev";
582 attr->attr.mode = S_IRUGO;
583 attr->attr.owner = parent_class->owner;
584 attr->show = show_dev;
585 error = class_device_create_file(class_dev, attr);
591 class_dev->devt_attr = attr;
594 error = class_device_add_attrs(class_dev);
598 if (class_dev->dev) {
599 class_name = make_class_name(class_dev->class->name,
601 error = sysfs_create_link(&class_dev->kobj,
602 &class_dev->dev->kobj, "device");
605 error = sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
611 error = class_device_add_groups(class_dev);
615 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
617 /* notify any interfaces this device is now here */
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) {
622 class_intf->add(class_dev, class_intf);
624 up(&parent_class->sem);
630 sysfs_remove_link(&class_dev->kobj, class_name);
633 sysfs_remove_link(&class_dev->kobj, "device");
635 class_device_remove_attrs(class_dev);
637 if (class_dev->devt_attr)
638 class_device_remove_file(class_dev, class_dev->devt_attr);
640 class_device_remove_file(class_dev, &class_dev->uevent_attr);
642 kobject_del(&class_dev->kobj);
645 class_device_put(parent_class_dev);
646 class_put(parent_class);
648 class_device_put(class_dev);
653 int class_device_register(struct class_device *class_dev)
655 class_device_initialize(class_dev);
656 return class_device_add(class_dev);
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.
662 * @parent: pointer to the parent struct class_device of this new device, if any.
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
667 * This function can be used by char device classes. A struct
668 * class_device will be created in sysfs, registered to the specified
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.
678 * Note: the struct class passed to this function must have previously
679 * been created with a call to class_create().
681 struct class_device *class_device_create(struct class *cls,
682 struct class_device *parent,
684 struct device *device,
685 const char *fmt, ...)
688 struct class_device *class_dev = NULL;
689 int retval = -ENODEV;
691 if (cls == NULL || IS_ERR(cls))
694 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
700 class_dev->devt = devt;
701 class_dev->dev = device;
702 class_dev->class = cls;
703 class_dev->parent = parent;
704 class_dev->release = class_device_create_release;
705 class_dev->uevent = class_device_create_uevent;
708 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
710 retval = class_device_register(class_dev);
718 return ERR_PTR(retval);
721 void class_device_del(struct class_device *class_dev)
723 struct class *parent_class = class_dev->class;
724 struct class_device *parent_device = class_dev->parent;
725 struct class_interface *class_intf;
726 char *class_name = NULL;
729 down(&parent_class->sem);
730 list_del_init(&class_dev->node);
731 list_for_each_entry(class_intf, &parent_class->interfaces, node)
732 if (class_intf->remove)
733 class_intf->remove(class_dev, class_intf);
734 up(&parent_class->sem);
737 if (class_dev->dev) {
738 class_name = make_class_name(class_dev->class->name,
740 sysfs_remove_link(&class_dev->kobj, "device");
741 sysfs_remove_link(&class_dev->dev->kobj, class_name);
743 sysfs_remove_link(&class_dev->kobj, "subsystem");
744 class_device_remove_file(class_dev, &class_dev->uevent_attr);
745 if (class_dev->devt_attr)
746 class_device_remove_file(class_dev, class_dev->devt_attr);
747 class_device_remove_attrs(class_dev);
748 class_device_remove_groups(class_dev);
750 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
751 kobject_del(&class_dev->kobj);
753 class_device_put(parent_device);
754 class_put(parent_class);
758 void class_device_unregister(struct class_device *class_dev)
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);
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.
771 * This call unregisters and cleans up a class device that was created with a
772 * call to class_device_create()
774 void class_device_destroy(struct class *cls, dev_t devt)
776 struct class_device *class_dev = NULL;
777 struct class_device *class_dev_tmp;
780 list_for_each_entry(class_dev_tmp, &cls->children, node) {
781 if (class_dev_tmp->devt == devt) {
782 class_dev = class_dev_tmp;
789 class_device_unregister(class_dev);
792 int class_device_rename(struct class_device *class_dev, char *new_name)
795 char *old_class_name = NULL, *new_class_name = NULL;
797 class_dev = class_device_get(class_dev);
801 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
805 old_class_name = make_class_name(class_dev->class->name,
808 strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
810 error = kobject_rename(&class_dev->kobj, new_name);
812 if (class_dev->dev) {
813 new_class_name = make_class_name(class_dev->class->name,
815 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
817 sysfs_remove_link(&class_dev->dev->kobj, old_class_name);
819 class_device_put(class_dev);
821 kfree(old_class_name);
822 kfree(new_class_name);
827 struct class_device * class_device_get(struct class_device *class_dev)
830 return to_class_dev(kobject_get(&class_dev->kobj));
834 void class_device_put(struct class_device *class_dev)
837 kobject_put(&class_dev->kobj);
841 int class_interface_register(struct class_interface *class_intf)
843 struct class *parent;
844 struct class_device *class_dev;
847 if (!class_intf || !class_intf->class)
850 parent = class_get(class_intf->class);
855 list_add_tail(&class_intf->node, &parent->interfaces);
856 if (class_intf->add) {
857 list_for_each_entry(class_dev, &parent->children, node)
858 class_intf->add(class_dev, class_intf);
860 if (class_intf->add_dev) {
861 list_for_each_entry(dev, &parent->devices, node)
862 class_intf->add_dev(dev, class_intf);
869 void class_interface_unregister(struct class_interface *class_intf)
871 struct class * parent = class_intf->class;
872 struct class_device *class_dev;
879 list_del_init(&class_intf->node);
880 if (class_intf->remove) {
881 list_for_each_entry(class_dev, &parent->children, node)
882 class_intf->remove(class_dev, class_intf);
884 if (class_intf->remove_dev) {
885 list_for_each_entry(dev, &parent->devices, node)
886 class_intf->remove_dev(dev, class_intf);
893 int virtual_device_parent(struct device *dev)
898 if (!dev->class->virtual_dir) {
899 static struct kobject *virtual_dir = NULL;
902 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
903 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
906 dev->kobj.parent = dev->class->virtual_dir;
910 int __init classes_init(void)
914 retval = subsystem_register(&class_subsys);
918 /* ick, this is ugly, the things we go through to keep from showing up
920 subsystem_init(&class_obj_subsys);
921 if (!class_obj_subsys.kset.subsys)
922 class_obj_subsys.kset.subsys = &class_obj_subsys;
926 EXPORT_SYMBOL_GPL(class_create_file);
927 EXPORT_SYMBOL_GPL(class_remove_file);
928 EXPORT_SYMBOL_GPL(class_register);
929 EXPORT_SYMBOL_GPL(class_unregister);
930 EXPORT_SYMBOL_GPL(class_create);
931 EXPORT_SYMBOL_GPL(class_destroy);
933 EXPORT_SYMBOL_GPL(class_device_register);
934 EXPORT_SYMBOL_GPL(class_device_unregister);
935 EXPORT_SYMBOL_GPL(class_device_initialize);
936 EXPORT_SYMBOL_GPL(class_device_add);
937 EXPORT_SYMBOL_GPL(class_device_del);
938 EXPORT_SYMBOL_GPL(class_device_get);
939 EXPORT_SYMBOL_GPL(class_device_put);
940 EXPORT_SYMBOL_GPL(class_device_create);
941 EXPORT_SYMBOL_GPL(class_device_destroy);
942 EXPORT_SYMBOL_GPL(class_device_create_file);
943 EXPORT_SYMBOL_GPL(class_device_remove_file);
944 EXPORT_SYMBOL_GPL(class_device_create_bin_file);
945 EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
947 EXPORT_SYMBOL_GPL(class_interface_register);
948 EXPORT_SYMBOL_GPL(class_interface_unregister);