Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * class.c - basic device class management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2002-3 Patrick Mochel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2002-3 Open Source Development Labs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (c) 2003-2004 Greg Kroah-Hartman
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (c) 2003-2004 IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/device/class.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kdev_t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/genhd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "base.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 			       char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct class_attribute *class_attr = to_class_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct subsys_private *cp = to_subsys_private(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	ssize_t ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (class_attr->show)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		ret = class_attr->show(cp->class, class_attr, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 				const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct class_attribute *class_attr = to_class_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct subsys_private *cp = to_subsys_private(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	ssize_t ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (class_attr->store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		ret = class_attr->store(cp->class, class_attr, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static void class_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct subsys_private *cp = to_subsys_private(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct class *class = cp->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	pr_debug("class '%s': release.\n", class->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (class->class_release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		class->class_release(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		pr_debug("class '%s' does not have a release() function, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			 "be careful\n", class->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	kfree(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static const struct kobj_ns_type_operations *class_child_ns_type(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct subsys_private *cp = to_subsys_private(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct class *class = cp->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return class->ns_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static const struct sysfs_ops class_sysfs_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	.show	   = class_attr_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	.store	   = class_attr_store,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static struct kobj_type class_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	.sysfs_ops	= &class_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	.release	= class_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	.child_ns_type	= class_child_ns_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /* Hotplug events for classes go to the class subsys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static struct kset *class_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) int class_create_file_ns(struct class *cls, const struct class_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			 const void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		error = sysfs_create_file_ns(&cls->p->subsys.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 					     &attr->attr, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) void class_remove_file_ns(struct class *cls, const struct class_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			  const void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		sysfs_remove_file_ns(&cls->p->subsys.kobj, &attr->attr, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static struct class *class_get(struct class *cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		kset_get(&cls->p->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return cls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static void class_put(struct class *cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		kset_put(&cls->p->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static struct device *klist_class_to_dev(struct klist_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct device_private *p = to_device_private_class(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return p->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static void klist_class_dev_get(struct klist_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct device *dev = klist_class_to_dev(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static void klist_class_dev_put(struct klist_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct device *dev = klist_class_to_dev(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int class_add_groups(struct class *cls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			    const struct attribute_group **groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	return sysfs_create_groups(&cls->p->subsys.kobj, groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static void class_remove_groups(struct class *cls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				const struct attribute_group **groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return sysfs_remove_groups(&cls->p->subsys.kobj, groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int __class_register(struct class *cls, struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct subsys_private *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	pr_debug("device class '%s': registering\n", cls->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (!cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	INIT_LIST_HEAD(&cp->interfaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	kset_init(&cp->glue_dirs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	__mutex_init(&cp->mutex, "subsys mutex", key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		kfree(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	/* set the default /sys/dev directory for devices of this class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (!cls->dev_kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		cls->dev_kobj = sysfs_dev_char_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) #if defined(CONFIG_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/* let the block class directory show up in the root of sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (!sysfs_deprecated || cls != &block_class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		cp->subsys.kobj.kset = class_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	cp->subsys.kobj.kset = class_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	cp->subsys.kobj.ktype = &class_ktype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	cp->class = cls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	cls->p = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	error = kset_register(&cp->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		kfree(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	error = class_add_groups(class_get(cls), cls->class_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	class_put(cls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) EXPORT_SYMBOL_GPL(__class_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) void class_unregister(struct class *cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	pr_debug("device class '%s': unregistering\n", cls->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	class_remove_groups(cls, cls->class_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	kset_unregister(&cls->p->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static void class_create_release(struct class *cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	pr_debug("%s called for %s\n", __func__, cls->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	kfree(cls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * class_create - create a struct class structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * @owner: pointer to the module that is to "own" this struct class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  * @name: pointer to a string for the name of this class.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  * @key: the lock_class_key for this class; used by mutex lock debugging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  * This is used to create a struct class pointer that can then be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * in calls to device_create().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * Returns &struct class pointer on success, or ERR_PTR() on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * Note, the pointer created here is to be destroyed when finished by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  * making a call to class_destroy().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct class *__class_create(struct module *owner, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			     struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct class *cls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	cls = kzalloc(sizeof(*cls), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (!cls) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	cls->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	cls->owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	cls->class_release = class_create_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	retval = __class_register(cls, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return cls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	kfree(cls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	return ERR_PTR(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) EXPORT_SYMBOL_GPL(__class_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * class_destroy - destroys a struct class structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * @cls: pointer to the struct class that is to be destroyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * Note, the pointer to be destroyed must have been created with a call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  * to class_create().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) void class_destroy(struct class *cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if ((cls == NULL) || (IS_ERR(cls)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	class_unregister(cls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)  * class_dev_iter_init - initialize class device iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  * @iter: class iterator to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  * @class: the class we wanna iterate over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  * @start: the device to start iterating from, if any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  * @type: device_type of the devices to iterate over, NULL for all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  * Initialize class iterator @iter such that it iterates over devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  * of @class.  If @start is set, the list iteration will start there,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)  * otherwise if it is NULL, the iteration starts at the beginning of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)  * the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			 struct device *start, const struct device_type *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct klist_node *start_knode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		start_knode = &start->p->knode_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	iter->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) EXPORT_SYMBOL_GPL(class_dev_iter_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * class_dev_iter_next - iterate to the next device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  * @iter: class iterator to proceed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  * Proceed @iter to the next device and return it.  Returns NULL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  * iteration is complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  * The returned device is referenced and won't be released till
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * iterator is proceed to the next device or exited.  The caller is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  * free to do whatever it wants to do with the device including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * calling back into class code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct device *class_dev_iter_next(struct class_dev_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	struct klist_node *knode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		knode = klist_next(&iter->ki);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		if (!knode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		dev = klist_class_to_dev(knode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		if (!iter->type || iter->type == dev->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) EXPORT_SYMBOL_GPL(class_dev_iter_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * class_dev_iter_exit - finish iteration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  * @iter: class iterator to finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  * Finish an iteration.  Always call this function after iteration is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  * complete whether the iteration ran till the end or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) void class_dev_iter_exit(struct class_dev_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	klist_iter_exit(&iter->ki);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) EXPORT_SYMBOL_GPL(class_dev_iter_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  * class_for_each_device - device iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  * @class: the class we're iterating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  * @start: the device to start with in the list, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  * @data: data for the callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  * @fn: function to be called for each device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  * Iterate over @class's list of devices, and call @fn for each,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * passing it @data.  If @start is set, the list iteration will start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * there, otherwise if it is NULL, the iteration starts at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * beginning of the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * We check the return of @fn each time. If it returns anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  * other than 0, we break out and return that value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)  * @fn is allowed to do anything including calling back into class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  * code.  There's no locking restriction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int class_for_each_device(struct class *class, struct device *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			  void *data, int (*fn)(struct device *, void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	struct class_dev_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (!class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (!class->p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		WARN(1, "%s called for class '%s' before it was initialized",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		     __func__, class->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	class_dev_iter_init(&iter, class, start, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	while ((dev = class_dev_iter_next(&iter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		error = fn(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	class_dev_iter_exit(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) EXPORT_SYMBOL_GPL(class_for_each_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  * class_find_device - device iterator for locating a particular device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  * @class: the class we're iterating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  * @start: Device to begin with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  * @data: data for the match function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  * @match: function to check device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  * This is similar to the class_for_each_dev() function above, but it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  * returns a reference to a device that is 'found' for later use, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  * determined by the @match callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)  * The callback should return 0 if the device doesn't match and non-zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  * if it does.  If the callback returns non-zero, this function will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)  * return to the caller and not iterate over any more devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)  * Note, you will need to drop the reference with put_device() after use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)  * @match is allowed to do anything including calling back into class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)  * code.  There's no locking restriction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct device *class_find_device(struct class *class, struct device *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 				 const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 				 int (*match)(struct device *, const void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	struct class_dev_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	if (!class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (!class->p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		WARN(1, "%s called for class '%s' before it was initialized",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		     __func__, class->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	class_dev_iter_init(&iter, class, start, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	while ((dev = class_dev_iter_next(&iter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		if (match(dev, data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	class_dev_iter_exit(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) EXPORT_SYMBOL_GPL(class_find_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) int class_interface_register(struct class_interface *class_intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	struct class *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	struct class_dev_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (!class_intf || !class_intf->class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	parent = class_get(class_intf->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	mutex_lock(&parent->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	list_add_tail(&class_intf->node, &parent->p->interfaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	if (class_intf->add_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		class_dev_iter_init(&iter, parent, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		while ((dev = class_dev_iter_next(&iter)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			class_intf->add_dev(dev, class_intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		class_dev_iter_exit(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	mutex_unlock(&parent->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) void class_interface_unregister(struct class_interface *class_intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	struct class *parent = class_intf->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	struct class_dev_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	mutex_lock(&parent->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	list_del_init(&class_intf->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (class_intf->remove_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		class_dev_iter_init(&iter, parent, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		while ((dev = class_dev_iter_next(&iter)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			class_intf->remove_dev(dev, class_intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		class_dev_iter_exit(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	mutex_unlock(&parent->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	class_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ssize_t show_class_attr_string(struct class *class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			       struct class_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	struct class_attribute_string *cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	cs = container_of(attr, struct class_attribute_string, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	return sysfs_emit(buf, "%s\n", cs->str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) EXPORT_SYMBOL_GPL(show_class_attr_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct class_compat {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	struct kobject *kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  * class_compat_register - register a compatibility class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * @name: the name of the class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * Compatibility class are meant as a temporary user-space compatibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  * workaround when converting a family of class devices to a bus devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct class_compat *class_compat_register(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	struct class_compat *cls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (!cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (!cls->kobj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		kfree(cls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	return cls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) EXPORT_SYMBOL_GPL(class_compat_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)  * class_compat_unregister - unregister a compatibility class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)  * @cls: the class to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) void class_compat_unregister(struct class_compat *cls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	kobject_put(cls->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	kfree(cls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) EXPORT_SYMBOL_GPL(class_compat_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)  * class_compat_create_link - create a compatibility class device link to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)  *			      a bus device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)  * @cls: the compatibility class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)  * @dev: the target bus device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)  * @device_link: an optional device to which a "device" link should be created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) int class_compat_create_link(struct class_compat *cls, struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			     struct device *device_link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	 * Optionally add a "device" link (typically to the parent), as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	 * class device would have one and we want to provide as much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	 * backwards compatibility as possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (device_link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		error = sysfs_create_link(&dev->kobj, &device_link->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 					  "device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 			sysfs_remove_link(cls->kobj, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) EXPORT_SYMBOL_GPL(class_compat_create_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)  * class_compat_remove_link - remove a compatibility class device link to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)  *			      a bus device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)  * @cls: the compatibility class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)  * @dev: the target bus device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)  * @device_link: an optional device to which a "device" link was previously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)  * 		 created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) void class_compat_remove_link(struct class_compat *cls, struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			      struct device *device_link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if (device_link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		sysfs_remove_link(&dev->kobj, "device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	sysfs_remove_link(cls->kobj, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) EXPORT_SYMBOL_GPL(class_compat_remove_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) int __init classes_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	class_kset = kset_create_and_add("class", NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	if (!class_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) EXPORT_SYMBOL_GPL(class_create_file_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) EXPORT_SYMBOL_GPL(class_remove_file_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) EXPORT_SYMBOL_GPL(class_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) EXPORT_SYMBOL_GPL(class_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) EXPORT_SYMBOL_GPL(class_interface_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) EXPORT_SYMBOL_GPL(class_interface_unregister);