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)
102 subsys_put(&cls->subsys);
106 static int add_class_attrs(struct class * cls)
111 if (cls->class_attrs) {
112 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
113 error = class_create_file(cls,&cls->class_attrs[i]);
122 class_remove_file(cls,&cls->class_attrs[i]);
126 static void remove_class_attrs(struct class * cls)
130 if (cls->class_attrs) {
131 for (i = 0; attr_name(cls->class_attrs[i]); i++)
132 class_remove_file(cls,&cls->class_attrs[i]);
136 int class_register(struct class * cls)
140 pr_debug("device class '%s': registering\n", cls->name);
142 INIT_LIST_HEAD(&cls->children);
143 INIT_LIST_HEAD(&cls->interfaces);
144 init_MUTEX(&cls->sem);
145 error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
149 subsys_set_kset(cls, class_subsys);
151 error = subsystem_register(&cls->subsys);
153 error = add_class_attrs(class_get(cls));
159 void class_unregister(struct class * cls)
161 pr_debug("device class '%s': unregistering\n", cls->name);
162 remove_class_attrs(cls);
163 subsystem_unregister(&cls->subsys);
166 static void class_create_release(struct class *cls)
171 static void class_device_create_release(struct class_device *class_dev)
177 * class_create - create a struct class structure
178 * @owner: pointer to the module that is to "own" this struct class
179 * @name: pointer to a string for the name of this class.
181 * This is used to create a struct class pointer that can then be used
182 * in calls to class_device_create().
184 * Note, the pointer created here is to be destroyed when finished by
185 * making a call to class_destroy().
187 struct class *class_create(struct module *owner, char *name)
192 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
200 cls->class_release = class_create_release;
201 cls->release = class_device_create_release;
203 retval = class_register(cls);
211 return ERR_PTR(retval);
215 * class_destroy - destroys a struct class structure
216 * @cs: pointer to the struct class that is to be destroyed
218 * Note, the pointer to be destroyed must have been created with a call
221 void class_destroy(struct class *cls)
223 if ((cls == NULL) || (IS_ERR(cls)))
226 class_unregister(cls);
229 /* Class Device Stuff */
231 int class_device_create_file(struct class_device * class_dev,
232 const struct class_device_attribute * attr)
236 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
240 void class_device_remove_file(struct class_device * class_dev,
241 const struct class_device_attribute * attr)
244 sysfs_remove_file(&class_dev->kobj, &attr->attr);
247 int class_device_create_bin_file(struct class_device *class_dev,
248 struct bin_attribute *attr)
252 error = sysfs_create_bin_file(&class_dev->kobj, attr);
256 void class_device_remove_bin_file(struct class_device *class_dev,
257 struct bin_attribute *attr)
260 sysfs_remove_bin_file(&class_dev->kobj, attr);
264 class_device_attr_show(struct kobject * kobj, struct attribute * attr,
267 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
268 struct class_device * cd = to_class_dev(kobj);
271 if (class_dev_attr->show)
272 ret = class_dev_attr->show(cd, buf);
277 class_device_attr_store(struct kobject * kobj, struct attribute * attr,
278 const char * buf, size_t count)
280 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
281 struct class_device * cd = to_class_dev(kobj);
284 if (class_dev_attr->store)
285 ret = class_dev_attr->store(cd, buf, count);
289 static struct sysfs_ops class_dev_sysfs_ops = {
290 .show = class_device_attr_show,
291 .store = class_device_attr_store,
294 static void class_dev_release(struct kobject * kobj)
296 struct class_device *cd = to_class_dev(kobj);
297 struct class * cls = cd->class;
299 pr_debug("device class '%s': release.\n", cd->class_id);
301 kfree(cd->devt_attr);
302 cd->devt_attr = NULL;
307 printk(KERN_ERR "Device class '%s' does not have a release() function, "
308 "it is broken and must be fixed.\n",
314 static struct kobj_type ktype_class_device = {
315 .sysfs_ops = &class_dev_sysfs_ops,
316 .release = class_dev_release,
319 static int class_hotplug_filter(struct kset *kset, struct kobject *kobj)
321 struct kobj_type *ktype = get_ktype(kobj);
323 if (ktype == &ktype_class_device) {
324 struct class_device *class_dev = to_class_dev(kobj);
325 if (class_dev->class)
331 static const char *class_hotplug_name(struct kset *kset, struct kobject *kobj)
333 struct class_device *class_dev = to_class_dev(kobj);
335 return class_dev->class->name;
338 static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
339 int num_envp, char *buffer, int buffer_size)
341 struct class_device *class_dev = to_class_dev(kobj);
346 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
348 if (class_dev->dev) {
349 /* add physical device, backing this device */
350 struct device *dev = class_dev->dev;
351 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
353 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
354 &length, "PHYSDEVPATH=%s", path);
358 add_hotplug_env_var(envp, num_envp, &i,
359 buffer, buffer_size, &length,
360 "PHYSDEVBUS=%s", dev->bus->name);
363 add_hotplug_env_var(envp, num_envp, &i,
364 buffer, buffer_size, &length,
365 "PHYSDEVDRIVER=%s", dev->driver->name);
368 if (MAJOR(class_dev->devt)) {
369 add_hotplug_env_var(envp, num_envp, &i,
370 buffer, buffer_size, &length,
371 "MAJOR=%u", MAJOR(class_dev->devt));
373 add_hotplug_env_var(envp, num_envp, &i,
374 buffer, buffer_size, &length,
375 "MINOR=%u", MINOR(class_dev->devt));
378 /* terminate, set to next free slot, shrink available space */
382 buffer = &buffer[length];
383 buffer_size -= length;
385 if (class_dev->class->hotplug) {
386 /* have the bus specific function add its stuff */
387 retval = class_dev->class->hotplug (class_dev, envp, num_envp,
388 buffer, buffer_size);
390 pr_debug ("%s - hotplug() returned %d\n",
391 __FUNCTION__, retval);
398 static struct kset_hotplug_ops class_hotplug_ops = {
399 .filter = class_hotplug_filter,
400 .name = class_hotplug_name,
401 .hotplug = class_hotplug,
404 static decl_subsys(class_obj, &ktype_class_device, &class_hotplug_ops);
407 static int class_device_add_attrs(struct class_device * cd)
411 struct class * cls = cd->class;
413 if (cls->class_dev_attrs) {
414 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
415 error = class_device_create_file(cd,
416 &cls->class_dev_attrs[i]);
425 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
429 static void class_device_remove_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 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
440 static ssize_t show_dev(struct class_device *class_dev, char *buf)
442 return print_dev_t(buf, class_dev->devt);
445 void class_device_initialize(struct class_device *class_dev)
447 kobj_set_kset_s(class_dev, class_obj_subsys);
448 kobject_init(&class_dev->kobj);
449 INIT_LIST_HEAD(&class_dev->node);
452 static char *make_class_name(struct class_device *class_dev)
457 size = strlen(class_dev->class->name) +
458 strlen(kobject_name(&class_dev->kobj)) + 2;
460 name = kmalloc(size, GFP_KERNEL);
462 return ERR_PTR(-ENOMEM);
464 strcpy(name, class_dev->class->name);
466 strcat(name, kobject_name(&class_dev->kobj));
470 int class_device_add(struct class_device *class_dev)
472 struct class * parent = NULL;
473 struct class_interface * class_intf;
474 char *class_name = NULL;
477 class_dev = class_device_get(class_dev);
481 if (!strlen(class_dev->class_id)) {
486 parent = class_get(class_dev->class);
488 pr_debug("CLASS: registering class device: ID = '%s'\n",
489 class_dev->class_id);
491 /* first, register with generic layer. */
492 kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
494 class_dev->kobj.parent = &parent->subsys.kset.kobj;
496 if ((error = kobject_add(&class_dev->kobj)))
499 /* add the needed attributes to this device */
500 if (MAJOR(class_dev->devt)) {
501 struct class_device_attribute *attr;
502 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
505 kobject_del(&class_dev->kobj);
509 attr->attr.name = "dev";
510 attr->attr.mode = S_IRUGO;
511 attr->attr.owner = parent->owner;
512 attr->show = show_dev;
514 class_device_create_file(class_dev, attr);
515 class_dev->devt_attr = attr;
518 class_device_add_attrs(class_dev);
519 if (class_dev->dev) {
520 class_name = make_class_name(class_dev);
521 sysfs_create_link(&class_dev->kobj,
522 &class_dev->dev->kobj, "device");
523 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
527 /* notify any interfaces this device is now here */
530 list_add_tail(&class_dev->node, &parent->children);
531 list_for_each_entry(class_intf, &parent->interfaces, node)
533 class_intf->add(class_dev);
536 kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
541 class_device_put(class_dev);
546 int class_device_register(struct class_device *class_dev)
548 class_device_initialize(class_dev);
549 return class_device_add(class_dev);
553 * class_device_create - creates a class device and registers it with sysfs
554 * @cs: pointer to the struct class that this device should be registered to.
555 * @dev: the dev_t for the char device to be added.
556 * @device: a pointer to a struct device that is assiociated with this class device.
557 * @fmt: string for the class device's name
559 * This function can be used by char device classes. A struct
560 * class_device will be created in sysfs, registered to the specified
561 * class. A "dev" file will be created, showing the dev_t for the
562 * device. The pointer to the struct class_device will be returned from
563 * the call. Any further sysfs files that might be required can be
564 * created using this pointer.
566 * Note: the struct class passed to this function must have previously
567 * been created with a call to class_create().
569 struct class_device *class_device_create(struct class *cls, dev_t devt,
570 struct device *device, char *fmt, ...)
573 struct class_device *class_dev = NULL;
574 int retval = -ENODEV;
576 if (cls == NULL || IS_ERR(cls))
579 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
585 class_dev->devt = devt;
586 class_dev->dev = device;
587 class_dev->class = cls;
590 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
592 retval = class_device_register(class_dev);
600 return ERR_PTR(retval);
603 void class_device_del(struct class_device *class_dev)
605 struct class * parent = class_dev->class;
606 struct class_interface * class_intf;
607 char *class_name = NULL;
611 list_del_init(&class_dev->node);
612 list_for_each_entry(class_intf, &parent->interfaces, node)
613 if (class_intf->remove)
614 class_intf->remove(class_dev);
618 if (class_dev->dev) {
619 class_name = make_class_name(class_dev);
620 sysfs_remove_link(&class_dev->kobj, "device");
621 sysfs_remove_link(&class_dev->dev->kobj, class_name);
623 if (class_dev->devt_attr)
624 class_device_remove_file(class_dev, class_dev->devt_attr);
625 class_device_remove_attrs(class_dev);
627 kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE);
628 kobject_del(&class_dev->kobj);
635 void class_device_unregister(struct class_device *class_dev)
637 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
638 class_dev->class_id);
639 class_device_del(class_dev);
640 class_device_put(class_dev);
644 * class_device_destroy - removes a class device that was created with class_device_create()
645 * @cls: the pointer to the struct class that this device was registered * with.
646 * @dev: the dev_t of the device that was previously registered.
648 * This call unregisters and cleans up a class device that was created with a
649 * call to class_device_create()
651 void class_device_destroy(struct class *cls, dev_t devt)
653 struct class_device *class_dev = NULL;
654 struct class_device *class_dev_tmp;
657 list_for_each_entry(class_dev_tmp, &cls->children, node) {
658 if (class_dev_tmp->devt == devt) {
659 class_dev = class_dev_tmp;
666 class_device_unregister(class_dev);
669 int class_device_rename(struct class_device *class_dev, char *new_name)
672 char *old_class_name = NULL, *new_class_name = NULL;
674 class_dev = class_device_get(class_dev);
678 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
682 old_class_name = make_class_name(class_dev);
684 strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
686 error = kobject_rename(&class_dev->kobj, new_name);
688 if (class_dev->dev) {
689 new_class_name = make_class_name(class_dev);
690 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
692 sysfs_remove_link(&class_dev->dev->kobj, old_class_name);
694 class_device_put(class_dev);
696 kfree(old_class_name);
697 kfree(new_class_name);
702 struct class_device * class_device_get(struct class_device *class_dev)
705 return to_class_dev(kobject_get(&class_dev->kobj));
709 void class_device_put(struct class_device *class_dev)
711 kobject_put(&class_dev->kobj);
715 int class_interface_register(struct class_interface *class_intf)
717 struct class *parent;
718 struct class_device *class_dev;
720 if (!class_intf || !class_intf->class)
723 parent = class_get(class_intf->class);
728 list_add_tail(&class_intf->node, &parent->interfaces);
729 if (class_intf->add) {
730 list_for_each_entry(class_dev, &parent->children, node)
731 class_intf->add(class_dev);
738 void class_interface_unregister(struct class_interface *class_intf)
740 struct class * parent = class_intf->class;
741 struct class_device *class_dev;
747 list_del_init(&class_intf->node);
748 if (class_intf->remove) {
749 list_for_each_entry(class_dev, &parent->children, node)
750 class_intf->remove(class_dev);
759 int __init classes_init(void)
763 retval = subsystem_register(&class_subsys);
767 /* ick, this is ugly, the things we go through to keep from showing up
769 subsystem_init(&class_obj_subsys);
770 if (!class_obj_subsys.kset.subsys)
771 class_obj_subsys.kset.subsys = &class_obj_subsys;
775 EXPORT_SYMBOL_GPL(class_create_file);
776 EXPORT_SYMBOL_GPL(class_remove_file);
777 EXPORT_SYMBOL_GPL(class_register);
778 EXPORT_SYMBOL_GPL(class_unregister);
779 EXPORT_SYMBOL_GPL(class_get);
780 EXPORT_SYMBOL_GPL(class_put);
781 EXPORT_SYMBOL_GPL(class_create);
782 EXPORT_SYMBOL_GPL(class_destroy);
784 EXPORT_SYMBOL_GPL(class_device_register);
785 EXPORT_SYMBOL_GPL(class_device_unregister);
786 EXPORT_SYMBOL_GPL(class_device_initialize);
787 EXPORT_SYMBOL_GPL(class_device_add);
788 EXPORT_SYMBOL_GPL(class_device_del);
789 EXPORT_SYMBOL_GPL(class_device_get);
790 EXPORT_SYMBOL_GPL(class_device_put);
791 EXPORT_SYMBOL_GPL(class_device_create);
792 EXPORT_SYMBOL_GPL(class_device_destroy);
793 EXPORT_SYMBOL_GPL(class_device_create_file);
794 EXPORT_SYMBOL_GPL(class_device_remove_file);
795 EXPORT_SYMBOL_GPL(class_device_create_bin_file);
796 EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
798 EXPORT_SYMBOL_GPL(class_interface_register);
799 EXPORT_SYMBOL_GPL(class_interface_unregister);