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/config.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/string.h>
18 #include <linux/kdev_t.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
23 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
24 #define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
27 class_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
29 struct class_attribute
* class_attr
= to_class_attr(attr
);
30 struct class * dc
= to_class(kobj
);
34 ret
= class_attr
->show(dc
, buf
);
39 class_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
40 const char * buf
, size_t count
)
42 struct class_attribute
* class_attr
= to_class_attr(attr
);
43 struct class * dc
= to_class(kobj
);
46 if (class_attr
->store
)
47 ret
= class_attr
->store(dc
, buf
, count
);
51 static void class_release(struct kobject
* kobj
)
53 struct class *class = to_class(kobj
);
55 pr_debug("class '%s': release.\n", class->name
);
57 if (class->class_release
)
58 class->class_release(class);
60 pr_debug("class '%s' does not have a release() function, "
61 "be careful\n", class->name
);
64 static struct sysfs_ops class_sysfs_ops
= {
65 .show
= class_attr_show
,
66 .store
= class_attr_store
,
69 static struct kobj_type ktype_class
= {
70 .sysfs_ops
= &class_sysfs_ops
,
71 .release
= class_release
,
74 /* Hotplug events for classes go to the class_obj subsys */
75 static decl_subsys(class, &ktype_class
, NULL
);
78 int class_create_file(struct class * cls
, const struct class_attribute
* attr
)
82 error
= sysfs_create_file(&cls
->subsys
.kset
.kobj
, &attr
->attr
);
88 void class_remove_file(struct class * cls
, const struct class_attribute
* attr
)
91 sysfs_remove_file(&cls
->subsys
.kset
.kobj
, &attr
->attr
);
94 static struct class *class_get(struct class *cls
)
97 return container_of(subsys_get(&cls
->subsys
), struct class, subsys
);
101 static void class_put(struct class * cls
)
104 subsys_put(&cls
->subsys
);
108 static int add_class_attrs(struct class * cls
)
113 if (cls
->class_attrs
) {
114 for (i
= 0; attr_name(cls
->class_attrs
[i
]); i
++) {
115 error
= class_create_file(cls
,&cls
->class_attrs
[i
]);
124 class_remove_file(cls
,&cls
->class_attrs
[i
]);
128 static void remove_class_attrs(struct class * cls
)
132 if (cls
->class_attrs
) {
133 for (i
= 0; attr_name(cls
->class_attrs
[i
]); i
++)
134 class_remove_file(cls
,&cls
->class_attrs
[i
]);
138 int class_register(struct class * cls
)
142 pr_debug("device class '%s': registering\n", cls
->name
);
144 INIT_LIST_HEAD(&cls
->children
);
145 INIT_LIST_HEAD(&cls
->devices
);
146 INIT_LIST_HEAD(&cls
->interfaces
);
147 init_MUTEX(&cls
->sem
);
148 error
= kobject_set_name(&cls
->subsys
.kset
.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
, 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 * @cs: 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 static int class_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
355 int num_envp
, char *buffer
, int buffer_size
)
357 struct class_device
*class_dev
= to_class_dev(kobj
);
362 pr_debug("%s - name = %s\n", __FUNCTION__
, class_dev
->class_id
);
364 if (class_dev
->dev
) {
365 /* add physical device, backing this device */
366 struct device
*dev
= class_dev
->dev
;
367 char *path
= kobject_get_path(&dev
->kobj
, GFP_KERNEL
);
369 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
370 &length
, "PHYSDEVPATH=%s", path
);
374 add_uevent_var(envp
, num_envp
, &i
,
375 buffer
, buffer_size
, &length
,
376 "PHYSDEVBUS=%s", dev
->bus
->name
);
379 add_uevent_var(envp
, num_envp
, &i
,
380 buffer
, buffer_size
, &length
,
381 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
384 if (MAJOR(class_dev
->devt
)) {
385 add_uevent_var(envp
, num_envp
, &i
,
386 buffer
, buffer_size
, &length
,
387 "MAJOR=%u", MAJOR(class_dev
->devt
));
389 add_uevent_var(envp
, num_envp
, &i
,
390 buffer
, buffer_size
, &length
,
391 "MINOR=%u", MINOR(class_dev
->devt
));
394 /* terminate, set to next free slot, shrink available space */
398 buffer
= &buffer
[length
];
399 buffer_size
-= length
;
401 if (class_dev
->uevent
) {
402 /* have the class device specific function add its stuff */
403 retval
= class_dev
->uevent(class_dev
, envp
, num_envp
,
404 buffer
, buffer_size
);
406 pr_debug("class_dev->uevent() returned %d\n", retval
);
407 } else if (class_dev
->class->uevent
) {
408 /* have the class specific function add its stuff */
409 retval
= class_dev
->class->uevent(class_dev
, envp
, num_envp
,
410 buffer
, buffer_size
);
412 pr_debug("class->uevent() returned %d\n", retval
);
418 static struct kset_uevent_ops class_uevent_ops
= {
419 .filter
= class_uevent_filter
,
420 .name
= class_uevent_name
,
421 .uevent
= class_uevent
,
424 static decl_subsys(class_obj
, &ktype_class_device
, &class_uevent_ops
);
427 static int class_device_add_attrs(struct class_device
* cd
)
431 struct class * cls
= cd
->class;
433 if (cls
->class_dev_attrs
) {
434 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++) {
435 error
= class_device_create_file(cd
,
436 &cls
->class_dev_attrs
[i
]);
445 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
449 static void class_device_remove_attrs(struct class_device
* cd
)
452 struct class * cls
= cd
->class;
454 if (cls
->class_dev_attrs
) {
455 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++)
456 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
460 static int class_device_add_groups(struct class_device
* cd
)
466 for (i
= 0; cd
->groups
[i
]; i
++) {
467 error
= sysfs_create_group(&cd
->kobj
, cd
->groups
[i
]);
470 sysfs_remove_group(&cd
->kobj
, cd
->groups
[i
]);
479 static void class_device_remove_groups(struct class_device
* cd
)
483 for (i
= 0; cd
->groups
[i
]; i
++) {
484 sysfs_remove_group(&cd
->kobj
, cd
->groups
[i
]);
489 static ssize_t
show_dev(struct class_device
*class_dev
, char *buf
)
491 return print_dev_t(buf
, class_dev
->devt
);
494 static ssize_t
store_uevent(struct class_device
*class_dev
,
495 const char *buf
, size_t count
)
497 kobject_uevent(&class_dev
->kobj
, KOBJ_ADD
);
501 void class_device_initialize(struct class_device
*class_dev
)
503 kobj_set_kset_s(class_dev
, class_obj_subsys
);
504 kobject_init(&class_dev
->kobj
);
505 INIT_LIST_HEAD(&class_dev
->node
);
508 char *make_class_name(const char *name
, struct kobject
*kobj
)
513 size
= strlen(name
) + strlen(kobject_name(kobj
)) + 2;
515 class_name
= kmalloc(size
, GFP_KERNEL
);
517 return ERR_PTR(-ENOMEM
);
519 strcpy(class_name
, name
);
520 strcat(class_name
, ":");
521 strcat(class_name
, kobject_name(kobj
));
525 int class_device_add(struct class_device
*class_dev
)
527 struct class *parent_class
= NULL
;
528 struct class_device
*parent_class_dev
= NULL
;
529 struct class_interface
*class_intf
;
530 char *class_name
= NULL
;
533 class_dev
= class_device_get(class_dev
);
537 if (!strlen(class_dev
->class_id
))
540 parent_class
= class_get(class_dev
->class);
544 parent_class_dev
= class_device_get(class_dev
->parent
);
546 pr_debug("CLASS: registering class device: ID = '%s'\n",
547 class_dev
->class_id
);
549 /* first, register with generic layer. */
550 error
= kobject_set_name(&class_dev
->kobj
, "%s", class_dev
->class_id
);
554 if (parent_class_dev
)
555 class_dev
->kobj
.parent
= &parent_class_dev
->kobj
;
557 class_dev
->kobj
.parent
= &parent_class
->subsys
.kset
.kobj
;
559 error
= kobject_add(&class_dev
->kobj
);
563 /* add the needed attributes to this device */
564 sysfs_create_link(&class_dev
->kobj
, &parent_class
->subsys
.kset
.kobj
, "subsystem");
565 class_dev
->uevent_attr
.attr
.name
= "uevent";
566 class_dev
->uevent_attr
.attr
.mode
= S_IWUSR
;
567 class_dev
->uevent_attr
.attr
.owner
= parent_class
->owner
;
568 class_dev
->uevent_attr
.store
= store_uevent
;
569 error
= class_device_create_file(class_dev
, &class_dev
->uevent_attr
);
573 if (MAJOR(class_dev
->devt
)) {
574 struct class_device_attribute
*attr
;
575 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
580 attr
->attr
.name
= "dev";
581 attr
->attr
.mode
= S_IRUGO
;
582 attr
->attr
.owner
= parent_class
->owner
;
583 attr
->show
= show_dev
;
584 error
= class_device_create_file(class_dev
, attr
);
590 class_dev
->devt_attr
= attr
;
593 error
= class_device_add_attrs(class_dev
);
597 if (class_dev
->dev
) {
598 class_name
= make_class_name(class_dev
->class->name
,
600 error
= sysfs_create_link(&class_dev
->kobj
,
601 &class_dev
->dev
->kobj
, "device");
604 error
= sysfs_create_link(&class_dev
->dev
->kobj
, &class_dev
->kobj
,
610 error
= class_device_add_groups(class_dev
);
614 kobject_uevent(&class_dev
->kobj
, KOBJ_ADD
);
616 /* notify any interfaces this device is now here */
617 down(&parent_class
->sem
);
618 list_add_tail(&class_dev
->node
, &parent_class
->children
);
619 list_for_each_entry(class_intf
, &parent_class
->interfaces
, node
) {
621 class_intf
->add(class_dev
, class_intf
);
623 up(&parent_class
->sem
);
629 sysfs_remove_link(&class_dev
->kobj
, class_name
);
632 sysfs_remove_link(&class_dev
->kobj
, "device");
634 class_device_remove_attrs(class_dev
);
636 if (class_dev
->devt_attr
)
637 class_device_remove_file(class_dev
, class_dev
->devt_attr
);
639 class_device_remove_file(class_dev
, &class_dev
->uevent_attr
);
641 kobject_del(&class_dev
->kobj
);
644 class_device_put(parent_class_dev
);
645 class_put(parent_class
);
647 class_device_put(class_dev
);
652 int class_device_register(struct class_device
*class_dev
)
654 class_device_initialize(class_dev
);
655 return class_device_add(class_dev
);
659 * class_device_create - creates a class device and registers it with sysfs
660 * @cs: pointer to the struct class that this device should be registered to.
661 * @parent: pointer to the parent struct class_device of this new device, if any.
662 * @dev: the dev_t for the char device to be added.
663 * @device: a pointer to a struct device that is assiociated with this class device.
664 * @fmt: string for the class device's name
666 * This function can be used by char device classes. A struct
667 * class_device will be created in sysfs, registered to the specified
669 * A "dev" file will be created, showing the dev_t for the device, if
670 * the dev_t is not 0,0.
671 * If a pointer to a parent struct class_device is passed in, the newly
672 * created struct class_device will be a child of that device in sysfs.
673 * The pointer to the struct class_device will be returned from the
674 * call. Any further sysfs files that might be required can be created
675 * using this pointer.
677 * Note: the struct class passed to this function must have previously
678 * been created with a call to class_create().
680 struct class_device
*class_device_create(struct class *cls
,
681 struct class_device
*parent
,
683 struct device
*device
, char *fmt
, ...)
686 struct class_device
*class_dev
= NULL
;
687 int retval
= -ENODEV
;
689 if (cls
== NULL
|| IS_ERR(cls
))
692 class_dev
= kzalloc(sizeof(*class_dev
), GFP_KERNEL
);
698 class_dev
->devt
= devt
;
699 class_dev
->dev
= device
;
700 class_dev
->class = cls
;
701 class_dev
->parent
= parent
;
702 class_dev
->release
= class_device_create_release
;
703 class_dev
->uevent
= class_device_create_uevent
;
706 vsnprintf(class_dev
->class_id
, BUS_ID_SIZE
, fmt
, args
);
708 retval
= class_device_register(class_dev
);
716 return ERR_PTR(retval
);
719 void class_device_del(struct class_device
*class_dev
)
721 struct class *parent_class
= class_dev
->class;
722 struct class_device
*parent_device
= class_dev
->parent
;
723 struct class_interface
*class_intf
;
724 char *class_name
= NULL
;
727 down(&parent_class
->sem
);
728 list_del_init(&class_dev
->node
);
729 list_for_each_entry(class_intf
, &parent_class
->interfaces
, node
)
730 if (class_intf
->remove
)
731 class_intf
->remove(class_dev
, class_intf
);
732 up(&parent_class
->sem
);
735 if (class_dev
->dev
) {
736 class_name
= make_class_name(class_dev
->class->name
,
738 sysfs_remove_link(&class_dev
->kobj
, "device");
739 sysfs_remove_link(&class_dev
->dev
->kobj
, class_name
);
741 sysfs_remove_link(&class_dev
->kobj
, "subsystem");
742 class_device_remove_file(class_dev
, &class_dev
->uevent_attr
);
743 if (class_dev
->devt_attr
)
744 class_device_remove_file(class_dev
, class_dev
->devt_attr
);
745 class_device_remove_attrs(class_dev
);
746 class_device_remove_groups(class_dev
);
748 kobject_uevent(&class_dev
->kobj
, KOBJ_REMOVE
);
749 kobject_del(&class_dev
->kobj
);
751 class_device_put(parent_device
);
752 class_put(parent_class
);
756 void class_device_unregister(struct class_device
*class_dev
)
758 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
759 class_dev
->class_id
);
760 class_device_del(class_dev
);
761 class_device_put(class_dev
);
765 * class_device_destroy - removes a class device that was created with class_device_create()
766 * @cls: the pointer to the struct class that this device was registered * with.
767 * @dev: the dev_t of the device that was previously registered.
769 * This call unregisters and cleans up a class device that was created with a
770 * call to class_device_create()
772 void class_device_destroy(struct class *cls
, dev_t devt
)
774 struct class_device
*class_dev
= NULL
;
775 struct class_device
*class_dev_tmp
;
778 list_for_each_entry(class_dev_tmp
, &cls
->children
, node
) {
779 if (class_dev_tmp
->devt
== devt
) {
780 class_dev
= class_dev_tmp
;
787 class_device_unregister(class_dev
);
790 int class_device_rename(struct class_device
*class_dev
, char *new_name
)
793 char *old_class_name
= NULL
, *new_class_name
= NULL
;
795 class_dev
= class_device_get(class_dev
);
799 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev
->class_id
,
803 old_class_name
= make_class_name(class_dev
->class->name
,
806 strlcpy(class_dev
->class_id
, new_name
, KOBJ_NAME_LEN
);
808 error
= kobject_rename(&class_dev
->kobj
, new_name
);
810 if (class_dev
->dev
) {
811 new_class_name
= make_class_name(class_dev
->class->name
,
813 sysfs_create_link(&class_dev
->dev
->kobj
, &class_dev
->kobj
,
815 sysfs_remove_link(&class_dev
->dev
->kobj
, old_class_name
);
817 class_device_put(class_dev
);
819 kfree(old_class_name
);
820 kfree(new_class_name
);
825 struct class_device
* class_device_get(struct class_device
*class_dev
)
828 return to_class_dev(kobject_get(&class_dev
->kobj
));
832 void class_device_put(struct class_device
*class_dev
)
835 kobject_put(&class_dev
->kobj
);
839 int class_interface_register(struct class_interface
*class_intf
)
841 struct class *parent
;
842 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
);
862 void class_interface_unregister(struct class_interface
*class_intf
)
864 struct class * parent
= class_intf
->class;
865 struct class_device
*class_dev
;
871 list_del_init(&class_intf
->node
);
872 if (class_intf
->remove
) {
873 list_for_each_entry(class_dev
, &parent
->children
, node
)
874 class_intf
->remove(class_dev
, class_intf
);
883 int __init
classes_init(void)
887 retval
= subsystem_register(&class_subsys
);
891 /* ick, this is ugly, the things we go through to keep from showing up
893 subsystem_init(&class_obj_subsys
);
894 if (!class_obj_subsys
.kset
.subsys
)
895 class_obj_subsys
.kset
.subsys
= &class_obj_subsys
;
899 EXPORT_SYMBOL_GPL(class_create_file
);
900 EXPORT_SYMBOL_GPL(class_remove_file
);
901 EXPORT_SYMBOL_GPL(class_register
);
902 EXPORT_SYMBOL_GPL(class_unregister
);
903 EXPORT_SYMBOL_GPL(class_create
);
904 EXPORT_SYMBOL_GPL(class_destroy
);
906 EXPORT_SYMBOL_GPL(class_device_register
);
907 EXPORT_SYMBOL_GPL(class_device_unregister
);
908 EXPORT_SYMBOL_GPL(class_device_initialize
);
909 EXPORT_SYMBOL_GPL(class_device_add
);
910 EXPORT_SYMBOL_GPL(class_device_del
);
911 EXPORT_SYMBOL_GPL(class_device_get
);
912 EXPORT_SYMBOL_GPL(class_device_put
);
913 EXPORT_SYMBOL_GPL(class_device_create
);
914 EXPORT_SYMBOL_GPL(class_device_destroy
);
915 EXPORT_SYMBOL_GPL(class_device_create_file
);
916 EXPORT_SYMBOL_GPL(class_device_remove_file
);
917 EXPORT_SYMBOL_GPL(class_device_create_bin_file
);
918 EXPORT_SYMBOL_GPL(class_device_remove_bin_file
);
920 EXPORT_SYMBOL_GPL(class_interface_register
);
921 EXPORT_SYMBOL_GPL(class_interface_unregister
);