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>
22 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
23 #define to_class(obj) container_of(obj, struct class, subsys.kset.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
.kset
.kobj
, &attr
->attr
);
87 void class_remove_file(struct class * cls
, const struct class_attribute
* attr
)
90 sysfs_remove_file(&cls
->subsys
.kset
.kobj
, &attr
->attr
);
93 struct class * class_get(struct class * cls
)
96 return container_of(subsys_get(&cls
->subsys
), struct class, subsys
);
100 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
->interfaces
);
145 init_MUTEX(&cls
->sem
);
146 error
= kobject_set_name(&cls
->subsys
.kset
.kobj
, "%s", cls
->name
);
150 subsys_set_kset(cls
, class_subsys
);
152 error
= subsystem_register(&cls
->subsys
);
154 error
= add_class_attrs(class_get(cls
));
160 void class_unregister(struct class * cls
)
162 pr_debug("device class '%s': unregistering\n", cls
->name
);
163 remove_class_attrs(cls
);
164 subsystem_unregister(&cls
->subsys
);
167 static void class_create_release(struct class *cls
)
169 pr_debug("%s called for %s\n", __FUNCTION__
, cls
->name
);
173 static void class_device_create_release(struct class_device
*class_dev
)
175 pr_debug("%s called for %s\n", __FUNCTION__
, class_dev
->class_id
);
179 /* needed to allow these devices to have parent class devices */
180 static int class_device_create_hotplug(struct class_device
*class_dev
,
181 char **envp
, int num_envp
,
182 char *buffer
, int buffer_size
)
184 pr_debug("%s called for %s\n", __FUNCTION__
, class_dev
->class_id
);
189 * class_create - create a struct class structure
190 * @owner: pointer to the module that is to "own" this struct class
191 * @name: pointer to a string for the name of this class.
193 * This is used to create a struct class pointer that can then be used
194 * in calls to class_device_create().
196 * Note, the pointer created here is to be destroyed when finished by
197 * making a call to class_destroy().
199 struct class *class_create(struct module
*owner
, char *name
)
204 cls
= kzalloc(sizeof(*cls
), GFP_KERNEL
);
212 cls
->class_release
= class_create_release
;
213 cls
->release
= class_device_create_release
;
215 retval
= class_register(cls
);
223 return ERR_PTR(retval
);
227 * class_destroy - destroys a struct class structure
228 * @cs: pointer to the struct class that is to be destroyed
230 * Note, the pointer to be destroyed must have been created with a call
233 void class_destroy(struct class *cls
)
235 if ((cls
== NULL
) || (IS_ERR(cls
)))
238 class_unregister(cls
);
241 /* Class Device Stuff */
243 int class_device_create_file(struct class_device
* class_dev
,
244 const struct class_device_attribute
* attr
)
248 error
= sysfs_create_file(&class_dev
->kobj
, &attr
->attr
);
252 void class_device_remove_file(struct class_device
* class_dev
,
253 const struct class_device_attribute
* attr
)
256 sysfs_remove_file(&class_dev
->kobj
, &attr
->attr
);
259 int class_device_create_bin_file(struct class_device
*class_dev
,
260 struct bin_attribute
*attr
)
264 error
= sysfs_create_bin_file(&class_dev
->kobj
, attr
);
268 void class_device_remove_bin_file(struct class_device
*class_dev
,
269 struct bin_attribute
*attr
)
272 sysfs_remove_bin_file(&class_dev
->kobj
, attr
);
276 class_device_attr_show(struct kobject
* kobj
, struct attribute
* attr
,
279 struct class_device_attribute
* class_dev_attr
= to_class_dev_attr(attr
);
280 struct class_device
* cd
= to_class_dev(kobj
);
283 if (class_dev_attr
->show
)
284 ret
= class_dev_attr
->show(cd
, buf
);
289 class_device_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
290 const char * buf
, size_t count
)
292 struct class_device_attribute
* class_dev_attr
= to_class_dev_attr(attr
);
293 struct class_device
* cd
= to_class_dev(kobj
);
296 if (class_dev_attr
->store
)
297 ret
= class_dev_attr
->store(cd
, buf
, count
);
301 static struct sysfs_ops class_dev_sysfs_ops
= {
302 .show
= class_device_attr_show
,
303 .store
= class_device_attr_store
,
306 static void class_dev_release(struct kobject
* kobj
)
308 struct class_device
*cd
= to_class_dev(kobj
);
309 struct class * cls
= cd
->class;
311 pr_debug("device class '%s': release.\n", cd
->class_id
);
313 kfree(cd
->devt_attr
);
314 cd
->devt_attr
= NULL
;
318 else if (cls
->release
)
321 printk(KERN_ERR
"Class Device '%s' does not have a release() function, "
322 "it is broken and must be fixed.\n",
328 static struct kobj_type ktype_class_device
= {
329 .sysfs_ops
= &class_dev_sysfs_ops
,
330 .release
= class_dev_release
,
333 static int class_hotplug_filter(struct kset
*kset
, struct kobject
*kobj
)
335 struct kobj_type
*ktype
= get_ktype(kobj
);
337 if (ktype
== &ktype_class_device
) {
338 struct class_device
*class_dev
= to_class_dev(kobj
);
339 if (class_dev
->class)
345 static const char *class_hotplug_name(struct kset
*kset
, struct kobject
*kobj
)
347 struct class_device
*class_dev
= to_class_dev(kobj
);
349 return class_dev
->class->name
;
352 static int class_hotplug(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
353 int num_envp
, char *buffer
, int buffer_size
)
355 struct class_device
*class_dev
= to_class_dev(kobj
);
360 pr_debug("%s - name = %s\n", __FUNCTION__
, class_dev
->class_id
);
362 if (class_dev
->dev
) {
363 /* add physical device, backing this device */
364 struct device
*dev
= class_dev
->dev
;
365 char *path
= kobject_get_path(&dev
->kobj
, GFP_KERNEL
);
367 add_hotplug_env_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
368 &length
, "PHYSDEVPATH=%s", path
);
372 add_hotplug_env_var(envp
, num_envp
, &i
,
373 buffer
, buffer_size
, &length
,
374 "PHYSDEVBUS=%s", dev
->bus
->name
);
377 add_hotplug_env_var(envp
, num_envp
, &i
,
378 buffer
, buffer_size
, &length
,
379 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
382 if (MAJOR(class_dev
->devt
)) {
383 add_hotplug_env_var(envp
, num_envp
, &i
,
384 buffer
, buffer_size
, &length
,
385 "MAJOR=%u", MAJOR(class_dev
->devt
));
387 add_hotplug_env_var(envp
, num_envp
, &i
,
388 buffer
, buffer_size
, &length
,
389 "MINOR=%u", MINOR(class_dev
->devt
));
392 /* terminate, set to next free slot, shrink available space */
396 buffer
= &buffer
[length
];
397 buffer_size
-= length
;
399 if (class_dev
->hotplug
) {
400 /* have the class device specific function add its stuff */
401 retval
= class_dev
->hotplug(class_dev
, envp
, num_envp
,
402 buffer
, buffer_size
);
404 pr_debug("class_dev->hotplug() returned %d\n", retval
);
405 } else if (class_dev
->class->hotplug
) {
406 /* have the class specific function add its stuff */
407 retval
= class_dev
->class->hotplug(class_dev
, envp
, num_envp
,
408 buffer
, buffer_size
);
410 pr_debug("class->hotplug() returned %d\n", retval
);
416 static struct kset_hotplug_ops class_hotplug_ops
= {
417 .filter
= class_hotplug_filter
,
418 .name
= class_hotplug_name
,
419 .hotplug
= class_hotplug
,
422 static decl_subsys(class_obj
, &ktype_class_device
, &class_hotplug_ops
);
425 static int class_device_add_attrs(struct class_device
* cd
)
429 struct class * cls
= cd
->class;
431 if (cls
->class_dev_attrs
) {
432 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++) {
433 error
= class_device_create_file(cd
,
434 &cls
->class_dev_attrs
[i
]);
443 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
447 static void class_device_remove_attrs(struct class_device
* cd
)
450 struct class * cls
= cd
->class;
452 if (cls
->class_dev_attrs
) {
453 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++)
454 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
458 static ssize_t
show_dev(struct class_device
*class_dev
, char *buf
)
460 return print_dev_t(buf
, class_dev
->devt
);
463 static ssize_t
store_uevent(struct class_device
*class_dev
,
464 const char *buf
, size_t count
)
466 kobject_hotplug(&class_dev
->kobj
, KOBJ_ADD
);
470 void class_device_initialize(struct class_device
*class_dev
)
472 kobj_set_kset_s(class_dev
, class_obj_subsys
);
473 kobject_init(&class_dev
->kobj
);
474 INIT_LIST_HEAD(&class_dev
->node
);
477 static char *make_class_name(struct class_device
*class_dev
)
482 size
= strlen(class_dev
->class->name
) +
483 strlen(kobject_name(&class_dev
->kobj
)) + 2;
485 name
= kmalloc(size
, GFP_KERNEL
);
487 return ERR_PTR(-ENOMEM
);
489 strcpy(name
, class_dev
->class->name
);
491 strcat(name
, kobject_name(&class_dev
->kobj
));
495 int class_device_add(struct class_device
*class_dev
)
497 struct class *parent_class
= NULL
;
498 struct class_device
*parent_class_dev
= NULL
;
499 struct class_interface
*class_intf
;
500 char *class_name
= NULL
;
503 class_dev
= class_device_get(class_dev
);
507 if (!strlen(class_dev
->class_id
))
510 parent_class
= class_get(class_dev
->class);
513 parent_class_dev
= class_device_get(class_dev
->parent
);
515 pr_debug("CLASS: registering class device: ID = '%s'\n",
516 class_dev
->class_id
);
518 /* first, register with generic layer. */
519 kobject_set_name(&class_dev
->kobj
, "%s", class_dev
->class_id
);
520 if (parent_class_dev
)
521 class_dev
->kobj
.parent
= &parent_class_dev
->kobj
;
523 class_dev
->kobj
.parent
= &parent_class
->subsys
.kset
.kobj
;
525 error
= kobject_add(&class_dev
->kobj
);
529 /* add the needed attributes to this device */
530 class_dev
->uevent_attr
.attr
.name
= "uevent";
531 class_dev
->uevent_attr
.attr
.mode
= S_IWUSR
;
532 class_dev
->uevent_attr
.attr
.owner
= parent_class
->owner
;
533 class_dev
->uevent_attr
.store
= store_uevent
;
534 class_device_create_file(class_dev
, &class_dev
->uevent_attr
);
536 if (MAJOR(class_dev
->devt
)) {
537 struct class_device_attribute
*attr
;
538 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
541 kobject_del(&class_dev
->kobj
);
544 attr
->attr
.name
= "dev";
545 attr
->attr
.mode
= S_IRUGO
;
546 attr
->attr
.owner
= parent_class
->owner
;
547 attr
->show
= show_dev
;
548 class_device_create_file(class_dev
, attr
);
549 class_dev
->devt_attr
= attr
;
552 class_device_add_attrs(class_dev
);
553 if (class_dev
->dev
) {
554 class_name
= make_class_name(class_dev
);
555 sysfs_create_link(&class_dev
->kobj
,
556 &class_dev
->dev
->kobj
, "device");
557 sysfs_create_link(&class_dev
->dev
->kobj
, &class_dev
->kobj
,
561 kobject_hotplug(&class_dev
->kobj
, KOBJ_ADD
);
563 /* notify any interfaces this device is now here */
565 down(&parent_class
->sem
);
566 list_add_tail(&class_dev
->node
, &parent_class
->children
);
567 list_for_each_entry(class_intf
, &parent_class
->interfaces
, node
)
569 class_intf
->add(class_dev
, class_intf
);
570 up(&parent_class
->sem
);
575 class_put(parent_class
);
576 class_device_put(parent_class_dev
);
578 class_device_put(class_dev
);
583 int class_device_register(struct class_device
*class_dev
)
585 class_device_initialize(class_dev
);
586 return class_device_add(class_dev
);
590 * class_device_create - creates a class device and registers it with sysfs
591 * @cs: pointer to the struct class that this device should be registered to.
592 * @parent: pointer to the parent struct class_device of this new device, if any.
593 * @dev: the dev_t for the char device to be added.
594 * @device: a pointer to a struct device that is assiociated with this class device.
595 * @fmt: string for the class device's name
597 * This function can be used by char device classes. A struct
598 * class_device will be created in sysfs, registered to the specified
600 * A "dev" file will be created, showing the dev_t for the device, if
601 * the dev_t is not 0,0.
602 * If a pointer to a parent struct class_device is passed in, the newly
603 * created struct class_device will be a child of that device in sysfs.
604 * The pointer to the struct class_device will be returned from the
605 * call. Any further sysfs files that might be required can be created
606 * using this pointer.
608 * Note: the struct class passed to this function must have previously
609 * been created with a call to class_create().
611 struct class_device
*class_device_create(struct class *cls
,
612 struct class_device
*parent
,
614 struct device
*device
, char *fmt
, ...)
617 struct class_device
*class_dev
= NULL
;
618 int retval
= -ENODEV
;
620 if (cls
== NULL
|| IS_ERR(cls
))
623 class_dev
= kzalloc(sizeof(*class_dev
), GFP_KERNEL
);
629 class_dev
->devt
= devt
;
630 class_dev
->dev
= device
;
631 class_dev
->class = cls
;
632 class_dev
->parent
= parent
;
633 class_dev
->release
= class_device_create_release
;
634 class_dev
->hotplug
= class_device_create_hotplug
;
637 vsnprintf(class_dev
->class_id
, BUS_ID_SIZE
, fmt
, args
);
639 retval
= class_device_register(class_dev
);
647 return ERR_PTR(retval
);
650 void class_device_del(struct class_device
*class_dev
)
652 struct class *parent_class
= class_dev
->class;
653 struct class_device
*parent_device
= class_dev
->parent
;
654 struct class_interface
*class_intf
;
655 char *class_name
= NULL
;
658 down(&parent_class
->sem
);
659 list_del_init(&class_dev
->node
);
660 list_for_each_entry(class_intf
, &parent_class
->interfaces
, node
)
661 if (class_intf
->remove
)
662 class_intf
->remove(class_dev
, class_intf
);
663 up(&parent_class
->sem
);
666 if (class_dev
->dev
) {
667 class_name
= make_class_name(class_dev
);
668 sysfs_remove_link(&class_dev
->kobj
, "device");
669 sysfs_remove_link(&class_dev
->dev
->kobj
, class_name
);
671 class_device_remove_file(class_dev
, &class_dev
->uevent_attr
);
672 if (class_dev
->devt_attr
)
673 class_device_remove_file(class_dev
, class_dev
->devt_attr
);
674 class_device_remove_attrs(class_dev
);
676 kobject_hotplug(&class_dev
->kobj
, KOBJ_REMOVE
);
677 kobject_del(&class_dev
->kobj
);
679 class_device_put(parent_device
);
680 class_put(parent_class
);
684 void class_device_unregister(struct class_device
*class_dev
)
686 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
687 class_dev
->class_id
);
688 class_device_del(class_dev
);
689 class_device_put(class_dev
);
693 * class_device_destroy - removes a class device that was created with class_device_create()
694 * @cls: the pointer to the struct class that this device was registered * with.
695 * @dev: the dev_t of the device that was previously registered.
697 * This call unregisters and cleans up a class device that was created with a
698 * call to class_device_create()
700 void class_device_destroy(struct class *cls
, dev_t devt
)
702 struct class_device
*class_dev
= NULL
;
703 struct class_device
*class_dev_tmp
;
706 list_for_each_entry(class_dev_tmp
, &cls
->children
, node
) {
707 if (class_dev_tmp
->devt
== devt
) {
708 class_dev
= class_dev_tmp
;
715 class_device_unregister(class_dev
);
718 int class_device_rename(struct class_device
*class_dev
, char *new_name
)
721 char *old_class_name
= NULL
, *new_class_name
= NULL
;
723 class_dev
= class_device_get(class_dev
);
727 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev
->class_id
,
731 old_class_name
= make_class_name(class_dev
);
733 strlcpy(class_dev
->class_id
, new_name
, KOBJ_NAME_LEN
);
735 error
= kobject_rename(&class_dev
->kobj
, new_name
);
737 if (class_dev
->dev
) {
738 new_class_name
= make_class_name(class_dev
);
739 sysfs_create_link(&class_dev
->dev
->kobj
, &class_dev
->kobj
,
741 sysfs_remove_link(&class_dev
->dev
->kobj
, old_class_name
);
743 class_device_put(class_dev
);
745 kfree(old_class_name
);
746 kfree(new_class_name
);
751 struct class_device
* class_device_get(struct class_device
*class_dev
)
754 return to_class_dev(kobject_get(&class_dev
->kobj
));
758 void class_device_put(struct class_device
*class_dev
)
761 kobject_put(&class_dev
->kobj
);
765 int class_interface_register(struct class_interface
*class_intf
)
767 struct class *parent
;
768 struct class_device
*class_dev
;
770 if (!class_intf
|| !class_intf
->class)
773 parent
= class_get(class_intf
->class);
778 list_add_tail(&class_intf
->node
, &parent
->interfaces
);
779 if (class_intf
->add
) {
780 list_for_each_entry(class_dev
, &parent
->children
, node
)
781 class_intf
->add(class_dev
, class_intf
);
788 void class_interface_unregister(struct class_interface
*class_intf
)
790 struct class * parent
= class_intf
->class;
791 struct class_device
*class_dev
;
797 list_del_init(&class_intf
->node
);
798 if (class_intf
->remove
) {
799 list_for_each_entry(class_dev
, &parent
->children
, node
)
800 class_intf
->remove(class_dev
, class_intf
);
809 int __init
classes_init(void)
813 retval
= subsystem_register(&class_subsys
);
817 /* ick, this is ugly, the things we go through to keep from showing up
819 subsystem_init(&class_obj_subsys
);
820 if (!class_obj_subsys
.kset
.subsys
)
821 class_obj_subsys
.kset
.subsys
= &class_obj_subsys
;
825 EXPORT_SYMBOL_GPL(class_create_file
);
826 EXPORT_SYMBOL_GPL(class_remove_file
);
827 EXPORT_SYMBOL_GPL(class_register
);
828 EXPORT_SYMBOL_GPL(class_unregister
);
829 EXPORT_SYMBOL_GPL(class_get
);
830 EXPORT_SYMBOL_GPL(class_put
);
831 EXPORT_SYMBOL_GPL(class_create
);
832 EXPORT_SYMBOL_GPL(class_destroy
);
834 EXPORT_SYMBOL_GPL(class_device_register
);
835 EXPORT_SYMBOL_GPL(class_device_unregister
);
836 EXPORT_SYMBOL_GPL(class_device_initialize
);
837 EXPORT_SYMBOL_GPL(class_device_add
);
838 EXPORT_SYMBOL_GPL(class_device_del
);
839 EXPORT_SYMBOL_GPL(class_device_get
);
840 EXPORT_SYMBOL_GPL(class_device_put
);
841 EXPORT_SYMBOL_GPL(class_device_create
);
842 EXPORT_SYMBOL_GPL(class_device_destroy
);
843 EXPORT_SYMBOL_GPL(class_device_create_file
);
844 EXPORT_SYMBOL_GPL(class_device_remove_file
);
845 EXPORT_SYMBOL_GPL(class_device_create_bin_file
);
846 EXPORT_SYMBOL_GPL(class_device_remove_bin_file
);
848 EXPORT_SYMBOL_GPL(class_interface_register
);
849 EXPORT_SYMBOL_GPL(class_interface_unregister
);