Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * class.c - basic device class management |
| 3 | * |
| 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. |
| 8 | * |
| 9 | * This file is released under the GPLv2 |
| 10 | * |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/kdev_t.h> |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 18 | #include <linux/err.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 19 | #include <linux/slab.h> |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 20 | #include <linux/genhd.h> |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "base.h" |
| 23 | |
| 24 | #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 26 | static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr, |
| 27 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 29 | struct class_attribute *class_attr = to_class_attr(attr); |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 30 | struct class_private *cp = to_class(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 31 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | if (class_attr->show) |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 34 | ret = class_attr->show(cp->class, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | return ret; |
| 36 | } |
| 37 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 38 | static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr, |
| 39 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 41 | struct class_attribute *class_attr = to_class_attr(attr); |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 42 | struct class_private *cp = to_class(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 43 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | if (class_attr->store) |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 46 | ret = class_attr->store(cp->class, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | return ret; |
| 48 | } |
| 49 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 50 | static void class_release(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 52 | struct class_private *cp = to_class(kobj); |
| 53 | struct class *class = cp->class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | pr_debug("class '%s': release.\n", class->name); |
| 56 | |
| 57 | if (class->class_release) |
| 58 | class->class_release(class); |
| 59 | else |
| 60 | pr_debug("class '%s' does not have a release() function, " |
| 61 | "be careful\n", class->name); |
| 62 | } |
| 63 | |
| 64 | static struct sysfs_ops class_sysfs_ops = { |
| 65 | .show = class_attr_show, |
| 66 | .store = class_attr_store, |
| 67 | }; |
| 68 | |
Greg Kroah-Hartman | adc5680 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 69 | static struct kobj_type class_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | .sysfs_ops = &class_sysfs_ops, |
| 71 | .release = class_release, |
| 72 | }; |
| 73 | |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 74 | /* Hotplug events for classes go to the class class_subsys */ |
Greg Kroah-Hartman | 443dbf9 | 2007-10-29 23:22:26 -0500 | [diff] [blame] | 75 | static struct kset *class_kset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 78 | int class_create_file(struct class *cls, const struct class_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | int error; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 81 | if (cls) |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 82 | error = sysfs_create_file(&cls->p->class_subsys.kobj, |
| 83 | &attr->attr); |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 84 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | error = -EINVAL; |
| 86 | return error; |
| 87 | } |
| 88 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 89 | void class_remove_file(struct class *cls, const struct class_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 91 | if (cls) |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 92 | sysfs_remove_file(&cls->p->class_subsys.kobj, &attr->attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Greg Kroah-Hartman | 1740757 | 2006-05-02 16:59:59 +0200 | [diff] [blame] | 95 | static struct class *class_get(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
| 97 | if (cls) |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 98 | kset_get(&cls->p->class_subsys); |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 99 | return cls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 102 | static void class_put(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 104 | if (cls) |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 105 | kset_put(&cls->p->class_subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 108 | static int add_class_attrs(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | int i; |
| 111 | int error = 0; |
| 112 | |
| 113 | if (cls->class_attrs) { |
| 114 | for (i = 0; attr_name(cls->class_attrs[i]); i++) { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 115 | error = class_create_file(cls, &cls->class_attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (error) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 117 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 120 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return error; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 122 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | while (--i >= 0) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 124 | class_remove_file(cls, &cls->class_attrs[i]); |
| 125 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 128 | static void remove_class_attrs(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | int i; |
| 131 | |
| 132 | if (cls->class_attrs) { |
| 133 | for (i = 0; attr_name(cls->class_attrs[i]); i++) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 134 | class_remove_file(cls, &cls->class_attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Matthew Wilcox | d2a3b91 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 138 | int __class_register(struct class *cls, struct lock_class_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 140 | struct class_private *cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | int error; |
| 142 | |
| 143 | pr_debug("device class '%s': registering\n", cls->name); |
| 144 | |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 145 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); |
| 146 | if (!cp) |
| 147 | return -ENOMEM; |
Greg Kroah-Hartman | 97ae69f | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 148 | INIT_LIST_HEAD(&cp->class_devices); |
Greg Kroah-Hartman | 184f1f7 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 149 | INIT_LIST_HEAD(&cp->class_interfaces); |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 150 | kset_init(&cp->class_dirs); |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 151 | __mutex_init(&cp->class_mutex, "struct class mutex", key); |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 152 | error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name); |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 153 | if (error) { |
| 154 | kfree(cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return error; |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 158 | /* set the default /sys/dev directory for devices of this class */ |
| 159 | if (!cls->dev_kobj) |
| 160 | cls->dev_kobj = sysfs_dev_char_kobj; |
| 161 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 162 | #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 163 | /* let the block class directory show up in the root of sysfs */ |
| 164 | if (cls != &block_class) |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 165 | cp->class_subsys.kobj.kset = class_kset; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 166 | #else |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 167 | cp->class_subsys.kobj.kset = class_kset; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 168 | #endif |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 169 | cp->class_subsys.kobj.ktype = &class_ktype; |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 170 | cp->class = cls; |
| 171 | cls->p = cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 173 | error = kset_register(&cp->class_subsys); |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 174 | if (error) { |
| 175 | kfree(cp); |
| 176 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 178 | error = add_class_attrs(class_get(cls)); |
| 179 | class_put(cls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return error; |
| 181 | } |
Matthew Wilcox | d2a3b91 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 182 | EXPORT_SYMBOL_GPL(__class_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 184 | void class_unregister(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
| 186 | pr_debug("device class '%s': unregistering\n", cls->name); |
| 187 | remove_class_attrs(cls); |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 188 | kset_unregister(&cls->p->class_subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 191 | static void class_create_release(struct class *cls) |
| 192 | { |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 193 | pr_debug("%s called for %s\n", __func__, cls->name); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 194 | kfree(cls); |
| 195 | } |
| 196 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 197 | /** |
| 198 | * class_create - create a struct class structure |
| 199 | * @owner: pointer to the module that is to "own" this struct class |
| 200 | * @name: pointer to a string for the name of this class. |
Randy Dunlap | 0e241ff | 2008-07-24 16:58:42 -0700 | [diff] [blame^] | 201 | * @key: the lock_class_key for this class; used by mutex lock debugging |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 202 | * |
| 203 | * This is used to create a struct class pointer that can then be used |
Kay Sievers | c3b19ff | 2008-03-12 20:47:35 +0100 | [diff] [blame] | 204 | * in calls to device_create(). |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 205 | * |
| 206 | * Note, the pointer created here is to be destroyed when finished by |
| 207 | * making a call to class_destroy(). |
| 208 | */ |
Matthew Wilcox | d2a3b91 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 209 | struct class *__class_create(struct module *owner, const char *name, |
| 210 | struct lock_class_key *key) |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 211 | { |
| 212 | struct class *cls; |
| 213 | int retval; |
| 214 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 215 | cls = kzalloc(sizeof(*cls), GFP_KERNEL); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 216 | if (!cls) { |
| 217 | retval = -ENOMEM; |
| 218 | goto error; |
| 219 | } |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 220 | |
| 221 | cls->name = name; |
| 222 | cls->owner = owner; |
| 223 | cls->class_release = class_create_release; |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 224 | |
Matthew Wilcox | d2a3b91 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 225 | retval = __class_register(cls, key); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 226 | if (retval) |
| 227 | goto error; |
| 228 | |
| 229 | return cls; |
| 230 | |
| 231 | error: |
| 232 | kfree(cls); |
| 233 | return ERR_PTR(retval); |
| 234 | } |
Matthew Wilcox | d2a3b91 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 235 | EXPORT_SYMBOL_GPL(__class_create); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 236 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 237 | /** |
| 238 | * class_destroy - destroys a struct class structure |
Rolf Eike Beer | 92a0f86 | 2006-09-29 01:59:13 -0700 | [diff] [blame] | 239 | * @cls: pointer to the struct class that is to be destroyed |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 240 | * |
| 241 | * Note, the pointer to be destroyed must have been created with a call |
| 242 | * to class_create(). |
| 243 | */ |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 244 | void class_destroy(struct class *cls) |
| 245 | { |
| 246 | if ((cls == NULL) || (IS_ERR(cls))) |
| 247 | return; |
| 248 | |
| 249 | class_unregister(cls); |
| 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 252 | #ifdef CONFIG_SYSFS_DEPRECATED |
| 253 | char *make_class_name(const char *name, struct kobject *kobj) |
| 254 | { |
| 255 | char *class_name; |
| 256 | int size; |
| 257 | |
| 258 | size = strlen(name) + strlen(kobject_name(kobj)) + 2; |
| 259 | |
| 260 | class_name = kmalloc(size, GFP_KERNEL); |
| 261 | if (!class_name) |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 262 | return NULL; |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 263 | |
| 264 | strcpy(class_name, name); |
| 265 | strcat(class_name, ":"); |
| 266 | strcat(class_name, kobject_name(kobj)); |
| 267 | return class_name; |
| 268 | } |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 269 | #endif |
| 270 | |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 271 | /** |
| 272 | * class_for_each_device - device iterator |
| 273 | * @class: the class we're iterating |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 274 | * @start: the device to start with in the list, if any. |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 275 | * @data: data for the callback |
| 276 | * @fn: function to be called for each device |
| 277 | * |
| 278 | * Iterate over @class's list of devices, and call @fn for each, |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 279 | * passing it @data. If @start is set, the list iteration will start |
| 280 | * there, otherwise if it is NULL, the iteration starts at the |
| 281 | * beginning of the list. |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 282 | * |
| 283 | * We check the return of @fn each time. If it returns anything |
| 284 | * other than 0, we break out and return that value. |
| 285 | * |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 286 | * Note, we hold class->class_mutex in this function, so it can not be |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 287 | * re-acquired in @fn, otherwise it will self-deadlocking. For |
| 288 | * example, calls to add or remove class members would be verboten. |
| 289 | */ |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 290 | int class_for_each_device(struct class *class, struct device *start, |
| 291 | void *data, int (*fn)(struct device *, void *)) |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 292 | { |
| 293 | struct device *dev; |
| 294 | int error = 0; |
| 295 | |
| 296 | if (!class) |
| 297 | return -EINVAL; |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 298 | mutex_lock(&class->p->class_mutex); |
Greg Kroah-Hartman | 97ae69f | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 299 | list_for_each_entry(dev, &class->p->class_devices, node) { |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 300 | if (start) { |
| 301 | if (start == dev) |
| 302 | start = NULL; |
| 303 | continue; |
| 304 | } |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 305 | dev = get_device(dev); |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 306 | error = fn(dev, data); |
| 307 | put_device(dev); |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 308 | if (error) |
| 309 | break; |
| 310 | } |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 311 | mutex_unlock(&class->p->class_mutex); |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 312 | |
| 313 | return error; |
| 314 | } |
| 315 | EXPORT_SYMBOL_GPL(class_for_each_device); |
| 316 | |
| 317 | /** |
| 318 | * class_find_device - device iterator for locating a particular device |
| 319 | * @class: the class we're iterating |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 320 | * @start: Device to begin with |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 321 | * @data: data for the match function |
| 322 | * @match: function to check device |
| 323 | * |
| 324 | * This is similar to the class_for_each_dev() function above, but it |
| 325 | * returns a reference to a device that is 'found' for later use, as |
| 326 | * determined by the @match callback. |
| 327 | * |
| 328 | * The callback should return 0 if the device doesn't match and non-zero |
| 329 | * if it does. If the callback returns non-zero, this function will |
| 330 | * return to the caller and not iterate over any more devices. |
Randy Dunlap | a63ca8f | 2008-01-30 11:51:08 -0800 | [diff] [blame] | 331 | * |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 332 | * Note, you will need to drop the reference with put_device() after use. |
| 333 | * |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 334 | * We hold class->class_mutex in this function, so it can not be |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 335 | * re-acquired in @match, otherwise it will self-deadlocking. For |
| 336 | * example, calls to add or remove class members would be verboten. |
| 337 | */ |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 338 | struct device *class_find_device(struct class *class, struct device *start, |
| 339 | void *data, |
| 340 | int (*match)(struct device *, void *)) |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 341 | { |
| 342 | struct device *dev; |
| 343 | int found = 0; |
| 344 | |
| 345 | if (!class) |
| 346 | return NULL; |
| 347 | |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 348 | mutex_lock(&class->p->class_mutex); |
Greg Kroah-Hartman | 97ae69f | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 349 | list_for_each_entry(dev, &class->p->class_devices, node) { |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 350 | if (start) { |
| 351 | if (start == dev) |
| 352 | start = NULL; |
| 353 | continue; |
| 354 | } |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 355 | dev = get_device(dev); |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 356 | if (match(dev, data)) { |
| 357 | found = 1; |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 358 | break; |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 359 | } else |
| 360 | put_device(dev); |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 361 | } |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 362 | mutex_unlock(&class->p->class_mutex); |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 363 | |
| 364 | return found ? dev : NULL; |
| 365 | } |
| 366 | EXPORT_SYMBOL_GPL(class_find_device); |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | int class_interface_register(struct class_interface *class_intf) |
| 369 | { |
| 370 | struct class *parent; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 371 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | if (!class_intf || !class_intf->class) |
| 374 | return -ENODEV; |
| 375 | |
| 376 | parent = class_get(class_intf->class); |
| 377 | if (!parent) |
| 378 | return -EINVAL; |
| 379 | |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 380 | mutex_lock(&parent->p->class_mutex); |
Greg Kroah-Hartman | 184f1f7 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 381 | list_add_tail(&class_intf->node, &parent->p->class_interfaces); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 382 | if (class_intf->add_dev) { |
Greg Kroah-Hartman | 97ae69f | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 383 | list_for_each_entry(dev, &parent->p->class_devices, node) |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 384 | class_intf->add_dev(dev, class_intf); |
| 385 | } |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 386 | mutex_unlock(&parent->p->class_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | void class_interface_unregister(struct class_interface *class_intf) |
| 392 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 393 | struct class *parent = class_intf->class; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 394 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | if (!parent) |
| 397 | return; |
| 398 | |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 399 | mutex_lock(&parent->p->class_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | list_del_init(&class_intf->node); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 401 | if (class_intf->remove_dev) { |
Greg Kroah-Hartman | 97ae69f | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 402 | list_for_each_entry(dev, &parent->p->class_devices, node) |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 403 | class_intf->remove_dev(dev, class_intf); |
| 404 | } |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 405 | mutex_unlock(&parent->p->class_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | class_put(parent); |
| 408 | } |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | int __init classes_init(void) |
| 411 | { |
Greg Kroah-Hartman | 443dbf9 | 2007-10-29 23:22:26 -0500 | [diff] [blame] | 412 | class_kset = kset_create_and_add("class", NULL, NULL); |
| 413 | if (!class_kset) |
| 414 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | EXPORT_SYMBOL_GPL(class_create_file); |
| 419 | EXPORT_SYMBOL_GPL(class_remove_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | EXPORT_SYMBOL_GPL(class_unregister); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 421 | EXPORT_SYMBOL_GPL(class_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | EXPORT_SYMBOL_GPL(class_interface_register); |
| 424 | EXPORT_SYMBOL_GPL(class_interface_unregister); |