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 kset_init(&cls
->class_dirs
);
149 init_MUTEX(&cls
->sem
);
150 error
= kobject_set_name(&cls
->subsys
.kset
.kobj
, "%s", cls
->name
);
154 subsys_set_kset(cls
, class_subsys
);
156 error
= subsystem_register(&cls
->subsys
);
158 error
= add_class_attrs(class_get(cls
));
164 void class_unregister(struct class * cls
)
166 pr_debug("device class '%s': unregistering\n", cls
->name
);
167 remove_class_attrs(cls
);
168 subsystem_unregister(&cls
->subsys
);
171 static void class_create_release(struct class *cls
)
173 pr_debug("%s called for %s\n", __FUNCTION__
, cls
->name
);
177 static void class_device_create_release(struct class_device
*class_dev
)
179 pr_debug("%s called for %s\n", __FUNCTION__
, class_dev
->class_id
);
183 /* needed to allow these devices to have parent class devices */
184 static int class_device_create_uevent(struct class_device
*class_dev
,
185 char **envp
, int num_envp
,
186 char *buffer
, int buffer_size
)
188 pr_debug("%s called for %s\n", __FUNCTION__
, class_dev
->class_id
);
193 * class_create - create a struct class structure
194 * @owner: pointer to the module that is to "own" this struct class
195 * @name: pointer to a string for the name of this class.
197 * This is used to create a struct class pointer that can then be used
198 * in calls to class_device_create().
200 * Note, the pointer created here is to be destroyed when finished by
201 * making a call to class_destroy().
203 struct class *class_create(struct module
*owner
, const char *name
)
208 cls
= kzalloc(sizeof(*cls
), GFP_KERNEL
);
216 cls
->class_release
= class_create_release
;
217 cls
->release
= class_device_create_release
;
219 retval
= class_register(cls
);
227 return ERR_PTR(retval
);
231 * class_destroy - destroys a struct class structure
232 * @cls: pointer to the struct class that is to be destroyed
234 * Note, the pointer to be destroyed must have been created with a call
237 void class_destroy(struct class *cls
)
239 if ((cls
== NULL
) || (IS_ERR(cls
)))
242 class_unregister(cls
);
245 /* Class Device Stuff */
247 int class_device_create_file(struct class_device
* class_dev
,
248 const struct class_device_attribute
* attr
)
252 error
= sysfs_create_file(&class_dev
->kobj
, &attr
->attr
);
256 void class_device_remove_file(struct class_device
* class_dev
,
257 const struct class_device_attribute
* attr
)
260 sysfs_remove_file(&class_dev
->kobj
, &attr
->attr
);
263 int class_device_create_bin_file(struct class_device
*class_dev
,
264 struct bin_attribute
*attr
)
268 error
= sysfs_create_bin_file(&class_dev
->kobj
, attr
);
272 void class_device_remove_bin_file(struct class_device
*class_dev
,
273 struct bin_attribute
*attr
)
276 sysfs_remove_bin_file(&class_dev
->kobj
, attr
);
280 class_device_attr_show(struct kobject
* kobj
, struct attribute
* attr
,
283 struct class_device_attribute
* class_dev_attr
= to_class_dev_attr(attr
);
284 struct class_device
* cd
= to_class_dev(kobj
);
287 if (class_dev_attr
->show
)
288 ret
= class_dev_attr
->show(cd
, buf
);
293 class_device_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
294 const char * buf
, size_t count
)
296 struct class_device_attribute
* class_dev_attr
= to_class_dev_attr(attr
);
297 struct class_device
* cd
= to_class_dev(kobj
);
300 if (class_dev_attr
->store
)
301 ret
= class_dev_attr
->store(cd
, buf
, count
);
305 static struct sysfs_ops class_dev_sysfs_ops
= {
306 .show
= class_device_attr_show
,
307 .store
= class_device_attr_store
,
310 static void class_dev_release(struct kobject
* kobj
)
312 struct class_device
*cd
= to_class_dev(kobj
);
313 struct class * cls
= cd
->class;
315 pr_debug("device class '%s': release.\n", cd
->class_id
);
317 kfree(cd
->devt_attr
);
318 cd
->devt_attr
= NULL
;
322 else if (cls
->release
)
325 printk(KERN_ERR
"Class Device '%s' does not have a release() function, "
326 "it is broken and must be fixed.\n",
332 static struct kobj_type ktype_class_device
= {
333 .sysfs_ops
= &class_dev_sysfs_ops
,
334 .release
= class_dev_release
,
337 static int class_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
339 struct kobj_type
*ktype
= get_ktype(kobj
);
341 if (ktype
== &ktype_class_device
) {
342 struct class_device
*class_dev
= to_class_dev(kobj
);
343 if (class_dev
->class)
349 static const char *class_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
351 struct class_device
*class_dev
= to_class_dev(kobj
);
353 return class_dev
->class->name
;
356 #ifdef CONFIG_SYSFS_DEPRECATED
357 char *make_class_name(const char *name
, struct kobject
*kobj
)
362 size
= strlen(name
) + strlen(kobject_name(kobj
)) + 2;
364 class_name
= kmalloc(size
, GFP_KERNEL
);
368 strcpy(class_name
, name
);
369 strcat(class_name
, ":");
370 strcat(class_name
, kobject_name(kobj
));
374 static int deprecated_class_uevent(char **envp
, int num_envp
, int *cur_index
,
375 char *buffer
, int buffer_size
,
377 struct class_device
*class_dev
)
379 struct device
*dev
= class_dev
->dev
;
385 /* add device, backing this class device (deprecated) */
386 path
= kobject_get_path(&dev
->kobj
, GFP_KERNEL
);
388 add_uevent_var(envp
, num_envp
, cur_index
, buffer
, buffer_size
,
389 cur_len
, "PHYSDEVPATH=%s", path
);
393 add_uevent_var(envp
, num_envp
, cur_index
,
394 buffer
, buffer_size
, cur_len
,
395 "PHYSDEVBUS=%s", dev
->bus
->name
);
398 add_uevent_var(envp
, num_envp
, cur_index
,
399 buffer
, buffer_size
, cur_len
,
400 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
404 static int make_deprecated_class_device_links(struct class_device
*class_dev
)
412 class_name
= make_class_name(class_dev
->class->name
, &class_dev
->kobj
);
414 error
= sysfs_create_link(&class_dev
->dev
->kobj
,
415 &class_dev
->kobj
, class_name
);
422 static void remove_deprecated_class_device_links(struct class_device
*class_dev
)
429 class_name
= make_class_name(class_dev
->class->name
, &class_dev
->kobj
);
431 sysfs_remove_link(&class_dev
->dev
->kobj
, class_name
);
435 static inline int deprecated_class_uevent(char **envp
, int num_envp
,
436 int *cur_index
, char *buffer
,
437 int buffer_size
, int *cur_len
,
438 struct class_device
*class_dev
)
440 static inline int make_deprecated_class_device_links(struct class_device
*cd
)
442 static void remove_deprecated_class_device_links(struct class_device
*cd
)
446 static int class_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
447 int num_envp
, char *buffer
, int buffer_size
)
449 struct class_device
*class_dev
= to_class_dev(kobj
);
454 pr_debug("%s - name = %s\n", __FUNCTION__
, class_dev
->class_id
);
456 deprecated_class_uevent(envp
, num_envp
, &i
, buffer
, buffer_size
,
459 if (MAJOR(class_dev
->devt
)) {
460 add_uevent_var(envp
, num_envp
, &i
,
461 buffer
, buffer_size
, &length
,
462 "MAJOR=%u", MAJOR(class_dev
->devt
));
464 add_uevent_var(envp
, num_envp
, &i
,
465 buffer
, buffer_size
, &length
,
466 "MINOR=%u", MINOR(class_dev
->devt
));
469 /* terminate, set to next free slot, shrink available space */
473 buffer
= &buffer
[length
];
474 buffer_size
-= length
;
476 if (class_dev
->uevent
) {
477 /* have the class device specific function add its stuff */
478 retval
= class_dev
->uevent(class_dev
, envp
, num_envp
,
479 buffer
, buffer_size
);
481 pr_debug("class_dev->uevent() returned %d\n", retval
);
482 } else if (class_dev
->class->uevent
) {
483 /* have the class specific function add its stuff */
484 retval
= class_dev
->class->uevent(class_dev
, envp
, num_envp
,
485 buffer
, buffer_size
);
487 pr_debug("class->uevent() returned %d\n", retval
);
493 static struct kset_uevent_ops class_uevent_ops
= {
494 .filter
= class_uevent_filter
,
495 .name
= class_uevent_name
,
496 .uevent
= class_uevent
,
499 static decl_subsys(class_obj
, &ktype_class_device
, &class_uevent_ops
);
502 static int class_device_add_attrs(struct class_device
* cd
)
506 struct class * cls
= cd
->class;
508 if (cls
->class_dev_attrs
) {
509 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++) {
510 error
= class_device_create_file(cd
,
511 &cls
->class_dev_attrs
[i
]);
520 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
524 static void class_device_remove_attrs(struct class_device
* cd
)
527 struct class * cls
= cd
->class;
529 if (cls
->class_dev_attrs
) {
530 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++)
531 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
535 static int class_device_add_groups(struct class_device
* cd
)
541 for (i
= 0; cd
->groups
[i
]; i
++) {
542 error
= sysfs_create_group(&cd
->kobj
, cd
->groups
[i
]);
545 sysfs_remove_group(&cd
->kobj
, cd
->groups
[i
]);
554 static void class_device_remove_groups(struct class_device
* cd
)
558 for (i
= 0; cd
->groups
[i
]; i
++) {
559 sysfs_remove_group(&cd
->kobj
, cd
->groups
[i
]);
564 static ssize_t
show_dev(struct class_device
*class_dev
, char *buf
)
566 return print_dev_t(buf
, class_dev
->devt
);
569 static ssize_t
store_uevent(struct class_device
*class_dev
,
570 const char *buf
, size_t count
)
572 kobject_uevent(&class_dev
->kobj
, KOBJ_ADD
);
576 void class_device_initialize(struct class_device
*class_dev
)
578 kobj_set_kset_s(class_dev
, class_obj_subsys
);
579 kobject_init(&class_dev
->kobj
);
580 INIT_LIST_HEAD(&class_dev
->node
);
583 int class_device_add(struct class_device
*class_dev
)
585 struct class *parent_class
= NULL
;
586 struct class_device
*parent_class_dev
= NULL
;
587 struct class_interface
*class_intf
;
590 class_dev
= class_device_get(class_dev
);
594 if (!strlen(class_dev
->class_id
))
597 parent_class
= class_get(class_dev
->class);
601 parent_class_dev
= class_device_get(class_dev
->parent
);
603 pr_debug("CLASS: registering class device: ID = '%s'\n",
604 class_dev
->class_id
);
606 /* first, register with generic layer. */
607 error
= kobject_set_name(&class_dev
->kobj
, "%s", class_dev
->class_id
);
611 if (parent_class_dev
)
612 class_dev
->kobj
.parent
= &parent_class_dev
->kobj
;
614 class_dev
->kobj
.parent
= &parent_class
->subsys
.kset
.kobj
;
616 error
= kobject_add(&class_dev
->kobj
);
620 /* add the needed attributes to this device */
621 error
= sysfs_create_link(&class_dev
->kobj
,
622 &parent_class
->subsys
.kset
.kobj
, "subsystem");
625 class_dev
->uevent_attr
.attr
.name
= "uevent";
626 class_dev
->uevent_attr
.attr
.mode
= S_IWUSR
;
627 class_dev
->uevent_attr
.attr
.owner
= parent_class
->owner
;
628 class_dev
->uevent_attr
.store
= store_uevent
;
629 error
= class_device_create_file(class_dev
, &class_dev
->uevent_attr
);
633 if (MAJOR(class_dev
->devt
)) {
634 struct class_device_attribute
*attr
;
635 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
640 attr
->attr
.name
= "dev";
641 attr
->attr
.mode
= S_IRUGO
;
642 attr
->attr
.owner
= parent_class
->owner
;
643 attr
->show
= show_dev
;
644 error
= class_device_create_file(class_dev
, attr
);
650 class_dev
->devt_attr
= attr
;
653 error
= class_device_add_attrs(class_dev
);
657 if (class_dev
->dev
) {
658 error
= sysfs_create_link(&class_dev
->kobj
,
659 &class_dev
->dev
->kobj
, "device");
664 error
= class_device_add_groups(class_dev
);
668 error
= make_deprecated_class_device_links(class_dev
);
672 kobject_uevent(&class_dev
->kobj
, KOBJ_ADD
);
674 /* notify any interfaces this device is now here */
675 down(&parent_class
->sem
);
676 list_add_tail(&class_dev
->node
, &parent_class
->children
);
677 list_for_each_entry(class_intf
, &parent_class
->interfaces
, node
) {
679 class_intf
->add(class_dev
, class_intf
);
681 up(&parent_class
->sem
);
686 class_device_remove_groups(class_dev
);
689 sysfs_remove_link(&class_dev
->kobj
, "device");
691 class_device_remove_attrs(class_dev
);
693 if (class_dev
->devt_attr
)
694 class_device_remove_file(class_dev
, class_dev
->devt_attr
);
696 class_device_remove_file(class_dev
, &class_dev
->uevent_attr
);
698 kobject_del(&class_dev
->kobj
);
701 class_device_put(parent_class_dev
);
702 class_put(parent_class
);
704 class_device_put(class_dev
);
708 int class_device_register(struct class_device
*class_dev
)
710 class_device_initialize(class_dev
);
711 return class_device_add(class_dev
);
715 * class_device_create - creates a class device and registers it with sysfs
716 * @cls: pointer to the struct class that this device should be registered to.
717 * @parent: pointer to the parent struct class_device of this new device, if any.
718 * @devt: the dev_t for the char device to be added.
719 * @device: a pointer to a struct device that is assiociated with this class device.
720 * @fmt: string for the class device's name
722 * This function can be used by char device classes. A struct
723 * class_device will be created in sysfs, registered to the specified
725 * A "dev" file will be created, showing the dev_t for the device, if
726 * the dev_t is not 0,0.
727 * If a pointer to a parent struct class_device is passed in, the newly
728 * created struct class_device will be a child of that device in sysfs.
729 * The pointer to the struct class_device will be returned from the
730 * call. Any further sysfs files that might be required can be created
731 * using this pointer.
733 * Note: the struct class passed to this function must have previously
734 * been created with a call to class_create().
736 struct class_device
*class_device_create(struct class *cls
,
737 struct class_device
*parent
,
739 struct device
*device
,
740 const char *fmt
, ...)
743 struct class_device
*class_dev
= NULL
;
744 int retval
= -ENODEV
;
746 if (cls
== NULL
|| IS_ERR(cls
))
749 class_dev
= kzalloc(sizeof(*class_dev
), GFP_KERNEL
);
755 class_dev
->devt
= devt
;
756 class_dev
->dev
= device
;
757 class_dev
->class = cls
;
758 class_dev
->parent
= parent
;
759 class_dev
->release
= class_device_create_release
;
760 class_dev
->uevent
= class_device_create_uevent
;
763 vsnprintf(class_dev
->class_id
, BUS_ID_SIZE
, fmt
, args
);
765 retval
= class_device_register(class_dev
);
773 return ERR_PTR(retval
);
776 void class_device_del(struct class_device
*class_dev
)
778 struct class *parent_class
= class_dev
->class;
779 struct class_device
*parent_device
= class_dev
->parent
;
780 struct class_interface
*class_intf
;
783 down(&parent_class
->sem
);
784 list_del_init(&class_dev
->node
);
785 list_for_each_entry(class_intf
, &parent_class
->interfaces
, node
)
786 if (class_intf
->remove
)
787 class_intf
->remove(class_dev
, class_intf
);
788 up(&parent_class
->sem
);
791 if (class_dev
->dev
) {
792 remove_deprecated_class_device_links(class_dev
);
793 sysfs_remove_link(&class_dev
->kobj
, "device");
795 sysfs_remove_link(&class_dev
->kobj
, "subsystem");
796 class_device_remove_file(class_dev
, &class_dev
->uevent_attr
);
797 if (class_dev
->devt_attr
)
798 class_device_remove_file(class_dev
, class_dev
->devt_attr
);
799 class_device_remove_attrs(class_dev
);
800 class_device_remove_groups(class_dev
);
802 kobject_uevent(&class_dev
->kobj
, KOBJ_REMOVE
);
803 kobject_del(&class_dev
->kobj
);
805 class_device_put(parent_device
);
806 class_put(parent_class
);
809 void class_device_unregister(struct class_device
*class_dev
)
811 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
812 class_dev
->class_id
);
813 class_device_del(class_dev
);
814 class_device_put(class_dev
);
818 * class_device_destroy - removes a class device that was created with class_device_create()
819 * @cls: the pointer to the struct class that this device was registered * with.
820 * @devt: the dev_t of the device that was previously registered.
822 * This call unregisters and cleans up a class device that was created with a
823 * call to class_device_create()
825 void class_device_destroy(struct class *cls
, dev_t devt
)
827 struct class_device
*class_dev
= NULL
;
828 struct class_device
*class_dev_tmp
;
831 list_for_each_entry(class_dev_tmp
, &cls
->children
, node
) {
832 if (class_dev_tmp
->devt
== devt
) {
833 class_dev
= class_dev_tmp
;
840 class_device_unregister(class_dev
);
843 struct class_device
* class_device_get(struct class_device
*class_dev
)
846 return to_class_dev(kobject_get(&class_dev
->kobj
));
850 void class_device_put(struct class_device
*class_dev
)
853 kobject_put(&class_dev
->kobj
);
857 int class_interface_register(struct class_interface
*class_intf
)
859 struct class *parent
;
860 struct class_device
*class_dev
;
863 if (!class_intf
|| !class_intf
->class)
866 parent
= class_get(class_intf
->class);
871 list_add_tail(&class_intf
->node
, &parent
->interfaces
);
872 if (class_intf
->add
) {
873 list_for_each_entry(class_dev
, &parent
->children
, node
)
874 class_intf
->add(class_dev
, class_intf
);
876 if (class_intf
->add_dev
) {
877 list_for_each_entry(dev
, &parent
->devices
, node
)
878 class_intf
->add_dev(dev
, class_intf
);
885 void class_interface_unregister(struct class_interface
*class_intf
)
887 struct class * parent
= class_intf
->class;
888 struct class_device
*class_dev
;
895 list_del_init(&class_intf
->node
);
896 if (class_intf
->remove
) {
897 list_for_each_entry(class_dev
, &parent
->children
, node
)
898 class_intf
->remove(class_dev
, class_intf
);
900 if (class_intf
->remove_dev
) {
901 list_for_each_entry(dev
, &parent
->devices
, node
)
902 class_intf
->remove_dev(dev
, class_intf
);
909 int __init
classes_init(void)
913 retval
= subsystem_register(&class_subsys
);
917 /* ick, this is ugly, the things we go through to keep from showing up
919 subsystem_init(&class_obj_subsys
);
920 if (!class_obj_subsys
.kset
.subsys
)
921 class_obj_subsys
.kset
.subsys
= &class_obj_subsys
;
925 EXPORT_SYMBOL_GPL(class_create_file
);
926 EXPORT_SYMBOL_GPL(class_remove_file
);
927 EXPORT_SYMBOL_GPL(class_register
);
928 EXPORT_SYMBOL_GPL(class_unregister
);
929 EXPORT_SYMBOL_GPL(class_create
);
930 EXPORT_SYMBOL_GPL(class_destroy
);
932 EXPORT_SYMBOL_GPL(class_device_register
);
933 EXPORT_SYMBOL_GPL(class_device_unregister
);
934 EXPORT_SYMBOL_GPL(class_device_initialize
);
935 EXPORT_SYMBOL_GPL(class_device_add
);
936 EXPORT_SYMBOL_GPL(class_device_del
);
937 EXPORT_SYMBOL_GPL(class_device_get
);
938 EXPORT_SYMBOL_GPL(class_device_put
);
939 EXPORT_SYMBOL_GPL(class_device_create
);
940 EXPORT_SYMBOL_GPL(class_device_destroy
);
941 EXPORT_SYMBOL_GPL(class_device_create_file
);
942 EXPORT_SYMBOL_GPL(class_device_remove_file
);
943 EXPORT_SYMBOL_GPL(class_device_create_bin_file
);
944 EXPORT_SYMBOL_GPL(class_device_remove_bin_file
);
946 EXPORT_SYMBOL_GPL(class_interface_register
);
947 EXPORT_SYMBOL_GPL(class_interface_unregister
);