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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "base.h" |
| 22 | |
| 23 | #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 24 | #define to_class(obj) container_of(obj, struct class, subsys.kobj) |
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); |
| 30 | struct class *dc = 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) |
| 34 | ret = class_attr->show(dc, buf); |
| 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); |
| 42 | struct class *dc = 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) |
| 46 | ret = class_attr->store(dc, buf, count); |
| 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 | { |
| 52 | struct class *class = to_class(kobj); |
| 53 | |
| 54 | pr_debug("class '%s': release.\n", class->name); |
| 55 | |
| 56 | if (class->class_release) |
| 57 | class->class_release(class); |
| 58 | else |
| 59 | pr_debug("class '%s' does not have a release() function, " |
| 60 | "be careful\n", class->name); |
| 61 | } |
| 62 | |
| 63 | static struct sysfs_ops class_sysfs_ops = { |
| 64 | .show = class_attr_show, |
| 65 | .store = class_attr_store, |
| 66 | }; |
| 67 | |
Greg Kroah-Hartman | adc5680 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 68 | static struct kobj_type class_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | .sysfs_ops = &class_sysfs_ops, |
| 70 | .release = class_release, |
| 71 | }; |
| 72 | |
| 73 | /* Hotplug events for classes go to the class_obj subsys */ |
Greg Kroah-Hartman | 443dbf9 | 2007-10-29 23:22:26 -0500 | [diff] [blame] | 74 | static struct kset *class_kset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 77 | int class_create_file(struct class *cls, const struct class_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | int error; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 80 | if (cls) |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 81 | error = sysfs_create_file(&cls->subsys.kobj, &attr->attr); |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 82 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | error = -EINVAL; |
| 84 | return error; |
| 85 | } |
| 86 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 87 | void class_remove_file(struct class *cls, const struct class_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | if (cls) |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 90 | sysfs_remove_file(&cls->subsys.kobj, &attr->attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Greg Kroah-Hartman | 1740757 | 2006-05-02 16:59:59 +0200 | [diff] [blame] | 93 | static struct class *class_get(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| 95 | if (cls) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 96 | return container_of(kset_get(&cls->subsys), |
| 97 | struct class, subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return NULL; |
| 99 | } |
| 100 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 101 | static void class_put(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 103 | if (cls) |
Greg Kroah-Hartman | 6e9d930 | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 104 | kset_put(&cls->subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 107 | static int add_class_attrs(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | int i; |
| 110 | int error = 0; |
| 111 | |
| 112 | if (cls->class_attrs) { |
| 113 | for (i = 0; attr_name(cls->class_attrs[i]); i++) { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 114 | error = class_create_file(cls, &cls->class_attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (error) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 116 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 119 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return error; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 121 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | while (--i >= 0) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 123 | class_remove_file(cls, &cls->class_attrs[i]); |
| 124 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 127 | static void remove_class_attrs(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | int i; |
| 130 | |
| 131 | if (cls->class_attrs) { |
| 132 | for (i = 0; attr_name(cls->class_attrs[i]); i++) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 133 | class_remove_file(cls, &cls->class_attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 137 | int class_register(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | int error; |
| 140 | |
| 141 | pr_debug("device class '%s': registering\n", cls->name); |
| 142 | |
| 143 | INIT_LIST_HEAD(&cls->children); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 144 | INIT_LIST_HEAD(&cls->devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | INIT_LIST_HEAD(&cls->interfaces); |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 146 | kset_init(&cls->class_dirs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | init_MUTEX(&cls->sem); |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 148 | error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (error) |
| 150 | return error; |
| 151 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 152 | #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 153 | /* let the block class directory show up in the root of sysfs */ |
| 154 | if (cls != &block_class) |
| 155 | cls->subsys.kobj.kset = class_kset; |
| 156 | #else |
Greg Kroah-Hartman | 443dbf9 | 2007-10-29 23:22:26 -0500 | [diff] [blame] | 157 | cls->subsys.kobj.kset = class_kset; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 158 | #endif |
Greg Kroah-Hartman | 3514fac | 2007-10-16 10:11:44 -0600 | [diff] [blame] | 159 | cls->subsys.kobj.ktype = &class_ktype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Greg Kroah-Hartman | 2fb9113 | 2007-11-06 15:03:30 -0800 | [diff] [blame] | 161 | error = kset_register(&cls->subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (!error) { |
| 163 | error = add_class_attrs(class_get(cls)); |
| 164 | class_put(cls); |
| 165 | } |
| 166 | return error; |
| 167 | } |
| 168 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 169 | void class_unregister(struct class *cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
| 171 | pr_debug("device class '%s': unregistering\n", cls->name); |
| 172 | remove_class_attrs(cls); |
Greg Kroah-Hartman | 2fb9113 | 2007-11-06 15:03:30 -0800 | [diff] [blame] | 173 | kset_unregister(&cls->subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 176 | static void class_create_release(struct class *cls) |
| 177 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 178 | pr_debug("%s called for %s\n", __FUNCTION__, cls->name); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 179 | kfree(cls); |
| 180 | } |
| 181 | |
| 182 | static void class_device_create_release(struct class_device *class_dev) |
| 183 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 184 | pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 185 | kfree(class_dev); |
| 186 | } |
| 187 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 188 | /* needed to allow these devices to have parent class devices */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 189 | static int class_device_create_uevent(struct class_device *class_dev, |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 190 | struct kobj_uevent_env *env) |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 191 | { |
| 192 | pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id); |
| 193 | return 0; |
| 194 | } |
| 195 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 196 | /** |
| 197 | * class_create - create a struct class structure |
| 198 | * @owner: pointer to the module that is to "own" this struct class |
| 199 | * @name: pointer to a string for the name of this class. |
| 200 | * |
| 201 | * This is used to create a struct class pointer that can then be used |
| 202 | * in calls to class_device_create(). |
| 203 | * |
| 204 | * Note, the pointer created here is to be destroyed when finished by |
| 205 | * making a call to class_destroy(). |
| 206 | */ |
Miguel Ojeda Sandonis | ab7d737 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 207 | struct class *class_create(struct module *owner, const char *name) |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 208 | { |
| 209 | struct class *cls; |
| 210 | int retval; |
| 211 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 212 | cls = kzalloc(sizeof(*cls), GFP_KERNEL); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 213 | if (!cls) { |
| 214 | retval = -ENOMEM; |
| 215 | goto error; |
| 216 | } |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 217 | |
| 218 | cls->name = name; |
| 219 | cls->owner = owner; |
| 220 | cls->class_release = class_create_release; |
| 221 | cls->release = class_device_create_release; |
| 222 | |
| 223 | retval = class_register(cls); |
| 224 | if (retval) |
| 225 | goto error; |
| 226 | |
| 227 | return cls; |
| 228 | |
| 229 | error: |
| 230 | kfree(cls); |
| 231 | return ERR_PTR(retval); |
| 232 | } |
| 233 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 234 | /** |
| 235 | * class_destroy - destroys a struct class structure |
Rolf Eike Beer | 92a0f86 | 2006-09-29 01:59:13 -0700 | [diff] [blame] | 236 | * @cls: pointer to the struct class that is to be destroyed |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 237 | * |
| 238 | * Note, the pointer to be destroyed must have been created with a call |
| 239 | * to class_create(). |
| 240 | */ |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 241 | void class_destroy(struct class *cls) |
| 242 | { |
| 243 | if ((cls == NULL) || (IS_ERR(cls))) |
| 244 | return; |
| 245 | |
| 246 | class_unregister(cls); |
| 247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | /* Class Device Stuff */ |
| 250 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 251 | int class_device_create_file(struct class_device *class_dev, |
| 252 | const struct class_device_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
| 254 | int error = -EINVAL; |
| 255 | if (class_dev) |
| 256 | error = sysfs_create_file(&class_dev->kobj, &attr->attr); |
| 257 | return error; |
| 258 | } |
| 259 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 260 | void class_device_remove_file(struct class_device *class_dev, |
| 261 | const struct class_device_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
| 263 | if (class_dev) |
| 264 | sysfs_remove_file(&class_dev->kobj, &attr->attr); |
| 265 | } |
| 266 | |
Greg Kroah-Hartman | d919fd4 | 2007-10-31 12:51:29 -0700 | [diff] [blame] | 267 | int class_device_create_bin_file(struct class_device *class_dev, |
| 268 | struct bin_attribute *attr) |
| 269 | { |
| 270 | int error = -EINVAL; |
| 271 | if (class_dev) |
| 272 | error = sysfs_create_bin_file(&class_dev->kobj, attr); |
| 273 | return error; |
| 274 | } |
| 275 | |
| 276 | void class_device_remove_bin_file(struct class_device *class_dev, |
| 277 | struct bin_attribute *attr) |
| 278 | { |
| 279 | if (class_dev) |
| 280 | sysfs_remove_bin_file(&class_dev->kobj, attr); |
| 281 | } |
| 282 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 283 | static ssize_t class_device_attr_show(struct kobject *kobj, |
| 284 | struct attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 286 | struct class_device_attribute *class_dev_attr = to_class_dev_attr(attr); |
| 287 | struct class_device *cd = to_class_dev(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | ssize_t ret = 0; |
| 289 | |
| 290 | if (class_dev_attr->show) |
| 291 | ret = class_dev_attr->show(cd, buf); |
| 292 | return ret; |
| 293 | } |
| 294 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 295 | static ssize_t class_device_attr_store(struct kobject *kobj, |
| 296 | struct attribute *attr, |
| 297 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 299 | struct class_device_attribute *class_dev_attr = to_class_dev_attr(attr); |
| 300 | struct class_device *cd = to_class_dev(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | ssize_t ret = 0; |
| 302 | |
| 303 | if (class_dev_attr->store) |
| 304 | ret = class_dev_attr->store(cd, buf, count); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | static struct sysfs_ops class_dev_sysfs_ops = { |
| 309 | .show = class_device_attr_show, |
| 310 | .store = class_device_attr_store, |
| 311 | }; |
| 312 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 313 | static void class_dev_release(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | struct class_device *cd = to_class_dev(kobj); |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 316 | struct class *cls = cd->class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | pr_debug("device class '%s': release.\n", cd->class_id); |
| 319 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 320 | if (cd->release) |
| 321 | cd->release(cd); |
| 322 | else if (cls->release) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | cls->release(cd); |
| 324 | else { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 325 | printk(KERN_ERR "Class Device '%s' does not have a release() " |
| 326 | "function, it is broken and must be fixed.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | cd->class_id); |
| 328 | WARN_ON(1); |
| 329 | } |
| 330 | } |
| 331 | |
Greg Kroah-Hartman | adc5680 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 332 | static struct kobj_type class_device_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | .sysfs_ops = &class_dev_sysfs_ops, |
| 334 | .release = class_dev_release, |
| 335 | }; |
| 336 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 337 | static int class_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
| 339 | struct kobj_type *ktype = get_ktype(kobj); |
| 340 | |
Greg Kroah-Hartman | adc5680 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 341 | if (ktype == &class_device_ktype) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | struct class_device *class_dev = to_class_dev(kobj); |
| 343 | if (class_dev->class) |
| 344 | return 1; |
| 345 | } |
| 346 | return 0; |
| 347 | } |
| 348 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 349 | static const char *class_uevent_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
| 351 | struct class_device *class_dev = to_class_dev(kobj); |
| 352 | |
| 353 | return class_dev->class->name; |
| 354 | } |
| 355 | |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 356 | #ifdef CONFIG_SYSFS_DEPRECATED |
| 357 | char *make_class_name(const char *name, struct kobject *kobj) |
| 358 | { |
| 359 | char *class_name; |
| 360 | int size; |
| 361 | |
| 362 | size = strlen(name) + strlen(kobject_name(kobj)) + 2; |
| 363 | |
| 364 | class_name = kmalloc(size, GFP_KERNEL); |
| 365 | if (!class_name) |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 366 | return NULL; |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 367 | |
| 368 | strcpy(class_name, name); |
| 369 | strcat(class_name, ":"); |
| 370 | strcat(class_name, kobject_name(kobj)); |
| 371 | return class_name; |
| 372 | } |
| 373 | |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 374 | static int make_deprecated_class_device_links(struct class_device *class_dev) |
| 375 | { |
| 376 | char *class_name; |
| 377 | int error; |
| 378 | |
| 379 | if (!class_dev->dev) |
| 380 | return 0; |
| 381 | |
| 382 | class_name = make_class_name(class_dev->class->name, &class_dev->kobj); |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 383 | if (class_name) |
| 384 | error = sysfs_create_link(&class_dev->dev->kobj, |
| 385 | &class_dev->kobj, class_name); |
| 386 | else |
| 387 | error = -ENOMEM; |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 388 | kfree(class_name); |
| 389 | return error; |
| 390 | } |
| 391 | |
| 392 | static void remove_deprecated_class_device_links(struct class_device *class_dev) |
| 393 | { |
| 394 | char *class_name; |
| 395 | |
| 396 | if (!class_dev->dev) |
| 397 | return; |
| 398 | |
| 399 | class_name = make_class_name(class_dev->class->name, &class_dev->kobj); |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 400 | if (class_name) |
| 401 | sysfs_remove_link(&class_dev->dev->kobj, class_name); |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 402 | kfree(class_name); |
| 403 | } |
| 404 | #else |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 405 | static inline int make_deprecated_class_device_links(struct class_device *cd) |
| 406 | { return 0; } |
| 407 | static void remove_deprecated_class_device_links(struct class_device *cd) |
| 408 | { } |
| 409 | #endif |
| 410 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 411 | static int class_uevent(struct kset *kset, struct kobject *kobj, |
| 412 | struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
| 414 | struct class_device *class_dev = to_class_dev(kobj); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 415 | struct device *dev = class_dev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | int retval = 0; |
| 417 | |
| 418 | pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id); |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (MAJOR(class_dev->devt)) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 421 | add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 423 | add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 426 | if (dev) { |
| 427 | const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL); |
| 428 | if (path) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 429 | add_uevent_var(env, "PHYSDEVPATH=%s", path); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 430 | kfree(path); |
| 431 | } |
| 432 | |
| 433 | if (dev->bus) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 434 | add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 435 | |
| 436 | if (dev->driver) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 437 | add_uevent_var(env, "PHYSDEVDRIVER=%s", |
| 438 | dev->driver->name); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 441 | if (class_dev->uevent) { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 442 | /* have the class device specific function add its stuff */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 443 | retval = class_dev->uevent(class_dev, env); |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 444 | if (retval) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 445 | pr_debug("class_dev->uevent() returned %d\n", retval); |
| 446 | } else if (class_dev->class->uevent) { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 447 | /* have the class specific function add its stuff */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 448 | retval = class_dev->class->uevent(class_dev, env); |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 449 | if (retval) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 450 | pr_debug("class->uevent() returned %d\n", retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | return retval; |
| 454 | } |
| 455 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 456 | static struct kset_uevent_ops class_uevent_ops = { |
| 457 | .filter = class_uevent_filter, |
| 458 | .name = class_uevent_name, |
| 459 | .uevent = class_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | }; |
| 461 | |
Greg Kroah-Hartman | 5c03c7a | 2007-11-02 16:19:59 -0700 | [diff] [blame] | 462 | /* |
| 463 | * DO NOT copy how this is created, kset_create_and_add() should be |
| 464 | * called, but this is a hold-over from the old-way and will be deleted |
| 465 | * entirely soon. |
| 466 | */ |
| 467 | static struct kset class_obj_subsys = { |
Greg Kroah-Hartman | 5c03c7a | 2007-11-02 16:19:59 -0700 | [diff] [blame] | 468 | .uevent_ops = &class_uevent_ops, |
| 469 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 471 | static int class_device_add_attrs(struct class_device *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
| 473 | int i; |
| 474 | int error = 0; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 475 | struct class *cls = cd->class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | if (cls->class_dev_attrs) { |
| 478 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) { |
| 479 | error = class_device_create_file(cd, |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 480 | &cls->class_dev_attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (error) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 482 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 485 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | return error; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 487 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | while (--i >= 0) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 489 | class_device_remove_file(cd, &cls->class_dev_attrs[i]); |
| 490 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 493 | static void class_device_remove_attrs(struct class_device *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
| 495 | int i; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 496 | struct class *cls = cd->class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | if (cls->class_dev_attrs) { |
| 499 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 500 | class_device_remove_file(cd, &cls->class_dev_attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 504 | static int class_device_add_groups(struct class_device *cd) |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 505 | { |
| 506 | int i; |
| 507 | int error = 0; |
| 508 | |
| 509 | if (cd->groups) { |
| 510 | for (i = 0; cd->groups[i]; i++) { |
| 511 | error = sysfs_create_group(&cd->kobj, cd->groups[i]); |
| 512 | if (error) { |
| 513 | while (--i >= 0) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 514 | sysfs_remove_group(&cd->kobj, |
| 515 | cd->groups[i]); |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 516 | goto out; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | out: |
| 521 | return error; |
| 522 | } |
| 523 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 524 | static void class_device_remove_groups(struct class_device *cd) |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 525 | { |
| 526 | int i; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 527 | if (cd->groups) |
| 528 | for (i = 0; cd->groups[i]; i++) |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 529 | sysfs_remove_group(&cd->kobj, cd->groups[i]); |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | static ssize_t show_dev(struct class_device *class_dev, char *buf) |
| 533 | { |
| 534 | return print_dev_t(buf, class_dev->devt); |
| 535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 537 | static struct class_device_attribute class_devt_attr = |
| 538 | __ATTR(dev, S_IRUGO, show_dev, NULL); |
| 539 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 540 | static ssize_t store_uevent(struct class_device *class_dev, |
| 541 | const char *buf, size_t count) |
| 542 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 543 | kobject_uevent(&class_dev->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 544 | return count; |
| 545 | } |
| 546 | |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 547 | static struct class_device_attribute class_uevent_attr = |
| 548 | __ATTR(uevent, S_IWUSR, NULL, store_uevent); |
| 549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | void class_device_initialize(struct class_device *class_dev) |
| 551 | { |
Greg Kroah-Hartman | 3514fac | 2007-10-16 10:11:44 -0600 | [diff] [blame] | 552 | class_dev->kobj.kset = &class_obj_subsys; |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 553 | kobject_init(&class_dev->kobj, &class_device_ktype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | INIT_LIST_HEAD(&class_dev->node); |
| 555 | } |
| 556 | |
| 557 | int class_device_add(struct class_device *class_dev) |
| 558 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 559 | struct class *parent_class = NULL; |
| 560 | struct class_device *parent_class_dev = NULL; |
| 561 | struct class_interface *class_intf; |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 562 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | class_dev = class_device_get(class_dev); |
| 565 | if (!class_dev) |
| 566 | return -EINVAL; |
| 567 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 568 | if (!strlen(class_dev->class_id)) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 569 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 571 | parent_class = class_get(class_dev->class); |
| 572 | if (!parent_class) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 573 | goto out1; |
| 574 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 575 | parent_class_dev = class_device_get(class_dev->parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | pr_debug("CLASS: registering class device: ID = '%s'\n", |
| 578 | class_dev->class_id); |
| 579 | |
| 580 | /* first, register with generic layer. */ |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 581 | if (parent_class_dev) |
| 582 | class_dev->kobj.parent = &parent_class_dev->kobj; |
| 583 | else |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 584 | class_dev->kobj.parent = &parent_class->subsys.kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 586 | error = kobject_add(&class_dev->kobj, class_dev->kobj.parent, |
| 587 | "%s", class_dev->class_id); |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 588 | if (error) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 589 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 591 | /* add the needed attributes to this device */ |
Cornelia Huck | f0e1761 | 2006-09-22 11:37:00 +0200 | [diff] [blame] | 592 | error = sysfs_create_link(&class_dev->kobj, |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 593 | &parent_class->subsys.kobj, "subsystem"); |
Cornelia Huck | f0e1761 | 2006-09-22 11:37:00 +0200 | [diff] [blame] | 594 | if (error) |
| 595 | goto out3; |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 596 | |
| 597 | error = class_device_create_file(class_dev, &class_uevent_attr); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 598 | if (error) |
| 599 | goto out3; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 600 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 601 | if (MAJOR(class_dev->devt)) { |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 602 | error = class_device_create_file(class_dev, &class_devt_attr); |
| 603 | if (error) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 604 | goto out4; |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 607 | error = class_device_add_attrs(class_dev); |
| 608 | if (error) |
| 609 | goto out5; |
| 610 | |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 611 | if (class_dev->dev) { |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 612 | error = sysfs_create_link(&class_dev->kobj, |
| 613 | &class_dev->dev->kobj, "device"); |
| 614 | if (error) |
| 615 | goto out6; |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 616 | } |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 617 | |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 618 | error = class_device_add_groups(class_dev); |
| 619 | if (error) |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 620 | goto out7; |
| 621 | |
| 622 | error = make_deprecated_class_device_links(class_dev); |
| 623 | if (error) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 624 | goto out8; |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 625 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 626 | kobject_uevent(&class_dev->kobj, KOBJ_ADD); |
Dmitry Torokhov | dbe9035 | 2005-09-15 02:01:37 -0500 | [diff] [blame] | 627 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 628 | /* notify any interfaces this device is now here */ |
Jayachandran C | a143889 | 2006-04-03 12:31:53 -0700 | [diff] [blame] | 629 | down(&parent_class->sem); |
| 630 | list_add_tail(&class_dev->node, &parent_class->children); |
| 631 | list_for_each_entry(class_intf, &parent_class->interfaces, node) { |
| 632 | if (class_intf->add) |
| 633 | class_intf->add(class_dev, class_intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
Jayachandran C | a143889 | 2006-04-03 12:31:53 -0700 | [diff] [blame] | 635 | up(&parent_class->sem); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 636 | |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 637 | goto out1; |
| 638 | |
| 639 | out8: |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 640 | class_device_remove_groups(class_dev); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 641 | out7: |
| 642 | if (class_dev->dev) |
| 643 | sysfs_remove_link(&class_dev->kobj, "device"); |
| 644 | out6: |
| 645 | class_device_remove_attrs(class_dev); |
| 646 | out5: |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 647 | if (MAJOR(class_dev->devt)) |
| 648 | class_device_remove_file(class_dev, &class_devt_attr); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 649 | out4: |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 650 | class_device_remove_file(class_dev, &class_uevent_attr); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 651 | out3: |
| 652 | kobject_del(&class_dev->kobj); |
| 653 | out2: |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 654 | if (parent_class_dev) |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 655 | class_device_put(parent_class_dev); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 656 | class_put(parent_class); |
| 657 | out1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | class_device_put(class_dev); |
| 659 | return error; |
| 660 | } |
| 661 | |
| 662 | int class_device_register(struct class_device *class_dev) |
| 663 | { |
| 664 | class_device_initialize(class_dev); |
| 665 | return class_device_add(class_dev); |
| 666 | } |
| 667 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 668 | /** |
| 669 | * class_device_create - creates a class device and registers it with sysfs |
Rolf Eike Beer | 92a0f86 | 2006-09-29 01:59:13 -0700 | [diff] [blame] | 670 | * @cls: pointer to the struct class that this device should be registered to. |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 671 | * @parent: pointer to the parent struct class_device of this new device, if |
| 672 | * any. |
Rolf Eike Beer | 92a0f86 | 2006-09-29 01:59:13 -0700 | [diff] [blame] | 673 | * @devt: the dev_t for the char device to be added. |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 674 | * @device: a pointer to a struct device that is assiociated with this class |
| 675 | * device. |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 676 | * @fmt: string for the class device's name |
| 677 | * |
| 678 | * This function can be used by char device classes. A struct |
| 679 | * class_device will be created in sysfs, registered to the specified |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 680 | * class. |
| 681 | * A "dev" file will be created, showing the dev_t for the device, if |
| 682 | * the dev_t is not 0,0. |
| 683 | * If a pointer to a parent struct class_device is passed in, the newly |
| 684 | * created struct class_device will be a child of that device in sysfs. |
| 685 | * The pointer to the struct class_device will be returned from the |
| 686 | * call. Any further sysfs files that might be required can be created |
| 687 | * using this pointer. |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 688 | * |
| 689 | * Note: the struct class passed to this function must have previously |
| 690 | * been created with a call to class_create(). |
| 691 | */ |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 692 | struct class_device *class_device_create(struct class *cls, |
| 693 | struct class_device *parent, |
| 694 | dev_t devt, |
Dmitry Torokhov | ddd5d35 | 2006-08-10 01:18:18 -0400 | [diff] [blame] | 695 | struct device *device, |
| 696 | const char *fmt, ...) |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 697 | { |
| 698 | va_list args; |
| 699 | struct class_device *class_dev = NULL; |
| 700 | int retval = -ENODEV; |
| 701 | |
| 702 | if (cls == NULL || IS_ERR(cls)) |
| 703 | goto error; |
| 704 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 705 | class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 706 | if (!class_dev) { |
| 707 | retval = -ENOMEM; |
| 708 | goto error; |
| 709 | } |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 710 | |
| 711 | class_dev->devt = devt; |
| 712 | class_dev->dev = device; |
| 713 | class_dev->class = cls; |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 714 | class_dev->parent = parent; |
| 715 | class_dev->release = class_device_create_release; |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 716 | class_dev->uevent = class_device_create_uevent; |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 717 | |
| 718 | va_start(args, fmt); |
| 719 | vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args); |
| 720 | va_end(args); |
| 721 | retval = class_device_register(class_dev); |
| 722 | if (retval) |
| 723 | goto error; |
| 724 | |
| 725 | return class_dev; |
| 726 | |
| 727 | error: |
| 728 | kfree(class_dev); |
| 729 | return ERR_PTR(retval); |
| 730 | } |
| 731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | void class_device_del(struct class_device *class_dev) |
| 733 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 734 | struct class *parent_class = class_dev->class; |
| 735 | struct class_device *parent_device = class_dev->parent; |
| 736 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 738 | if (parent_class) { |
| 739 | down(&parent_class->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | list_del_init(&class_dev->node); |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 741 | list_for_each_entry(class_intf, &parent_class->interfaces, node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | if (class_intf->remove) |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 743 | class_intf->remove(class_dev, class_intf); |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 744 | up(&parent_class->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 747 | if (class_dev->dev) { |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 748 | remove_deprecated_class_device_links(class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | sysfs_remove_link(&class_dev->kobj, "device"); |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 750 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 751 | sysfs_remove_link(&class_dev->kobj, "subsystem"); |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 752 | class_device_remove_file(class_dev, &class_uevent_attr); |
| 753 | if (MAJOR(class_dev->devt)) |
| 754 | class_device_remove_file(class_dev, &class_devt_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | class_device_remove_attrs(class_dev); |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 756 | class_device_remove_groups(class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 758 | kobject_uevent(&class_dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | kobject_del(&class_dev->kobj); |
| 760 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 761 | class_device_put(parent_device); |
| 762 | class_put(parent_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void class_device_unregister(struct class_device *class_dev) |
| 766 | { |
| 767 | pr_debug("CLASS: Unregistering class device. ID = '%s'\n", |
| 768 | class_dev->class_id); |
| 769 | class_device_del(class_dev); |
| 770 | class_device_put(class_dev); |
| 771 | } |
| 772 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 773 | /** |
| 774 | * class_device_destroy - removes a class device that was created with class_device_create() |
| 775 | * @cls: the pointer to the struct class that this device was registered * with. |
Rolf Eike Beer | 92a0f86 | 2006-09-29 01:59:13 -0700 | [diff] [blame] | 776 | * @devt: the dev_t of the device that was previously registered. |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 777 | * |
| 778 | * This call unregisters and cleans up a class device that was created with a |
| 779 | * call to class_device_create() |
| 780 | */ |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 781 | void class_device_destroy(struct class *cls, dev_t devt) |
| 782 | { |
| 783 | struct class_device *class_dev = NULL; |
| 784 | struct class_device *class_dev_tmp; |
| 785 | |
| 786 | down(&cls->sem); |
| 787 | list_for_each_entry(class_dev_tmp, &cls->children, node) { |
| 788 | if (class_dev_tmp->devt == devt) { |
| 789 | class_dev = class_dev_tmp; |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | up(&cls->sem); |
| 794 | |
| 795 | if (class_dev) |
| 796 | class_device_unregister(class_dev); |
| 797 | } |
| 798 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 799 | struct class_device *class_device_get(struct class_device *class_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
| 801 | if (class_dev) |
| 802 | return to_class_dev(kobject_get(&class_dev->kobj)); |
| 803 | return NULL; |
| 804 | } |
| 805 | |
| 806 | void class_device_put(struct class_device *class_dev) |
| 807 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 808 | if (class_dev) |
| 809 | kobject_put(&class_dev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 812 | /** |
| 813 | * class_for_each_device - device iterator |
| 814 | * @class: the class we're iterating |
| 815 | * @data: data for the callback |
| 816 | * @fn: function to be called for each device |
| 817 | * |
| 818 | * Iterate over @class's list of devices, and call @fn for each, |
| 819 | * passing it @data. |
| 820 | * |
| 821 | * We check the return of @fn each time. If it returns anything |
| 822 | * other than 0, we break out and return that value. |
| 823 | * |
| 824 | * Note, we hold class->sem in this function, so it can not be |
| 825 | * re-acquired in @fn, otherwise it will self-deadlocking. For |
| 826 | * example, calls to add or remove class members would be verboten. |
| 827 | */ |
| 828 | int class_for_each_device(struct class *class, void *data, |
| 829 | int (*fn)(struct device *, void *)) |
| 830 | { |
| 831 | struct device *dev; |
| 832 | int error = 0; |
| 833 | |
| 834 | if (!class) |
| 835 | return -EINVAL; |
| 836 | down(&class->sem); |
| 837 | list_for_each_entry(dev, &class->devices, node) { |
| 838 | dev = get_device(dev); |
| 839 | if (dev) { |
| 840 | error = fn(dev, data); |
| 841 | put_device(dev); |
| 842 | } else |
| 843 | error = -ENODEV; |
| 844 | if (error) |
| 845 | break; |
| 846 | } |
| 847 | up(&class->sem); |
| 848 | |
| 849 | return error; |
| 850 | } |
| 851 | EXPORT_SYMBOL_GPL(class_for_each_device); |
| 852 | |
| 853 | /** |
| 854 | * class_find_device - device iterator for locating a particular device |
| 855 | * @class: the class we're iterating |
| 856 | * @data: data for the match function |
| 857 | * @match: function to check device |
| 858 | * |
| 859 | * This is similar to the class_for_each_dev() function above, but it |
| 860 | * returns a reference to a device that is 'found' for later use, as |
| 861 | * determined by the @match callback. |
| 862 | * |
| 863 | * The callback should return 0 if the device doesn't match and non-zero |
| 864 | * if it does. If the callback returns non-zero, this function will |
| 865 | * return to the caller and not iterate over any more devices. |
Randy Dunlap | a63ca8f | 2008-01-30 11:51:08 -0800 | [diff] [blame] | 866 | * |
Dave Young | fd04897 | 2008-01-22 15:27:08 +0800 | [diff] [blame] | 867 | * Note, you will need to drop the reference with put_device() after use. |
| 868 | * |
| 869 | * We hold class->sem in this function, so it can not be |
| 870 | * re-acquired in @match, otherwise it will self-deadlocking. For |
| 871 | * example, calls to add or remove class members would be verboten. |
| 872 | */ |
| 873 | struct device *class_find_device(struct class *class, void *data, |
| 874 | int (*match)(struct device *, void *)) |
| 875 | { |
| 876 | struct device *dev; |
| 877 | int found = 0; |
| 878 | |
| 879 | if (!class) |
| 880 | return NULL; |
| 881 | |
| 882 | down(&class->sem); |
| 883 | list_for_each_entry(dev, &class->devices, node) { |
| 884 | dev = get_device(dev); |
| 885 | if (dev) { |
| 886 | if (match(dev, data)) { |
| 887 | found = 1; |
| 888 | break; |
| 889 | } else |
| 890 | put_device(dev); |
| 891 | } else |
| 892 | break; |
| 893 | } |
| 894 | up(&class->sem); |
| 895 | |
| 896 | return found ? dev : NULL; |
| 897 | } |
| 898 | EXPORT_SYMBOL_GPL(class_find_device); |
| 899 | |
| 900 | /** |
| 901 | * class_find_child - device iterator for locating a particular class_device |
| 902 | * @class: the class we're iterating |
| 903 | * @data: data for the match function |
| 904 | * @match: function to check class_device |
| 905 | * |
| 906 | * This function returns a reference to a class_device that is 'found' for |
| 907 | * later use, as determined by the @match callback. |
| 908 | * |
| 909 | * The callback should return 0 if the class_device doesn't match and non-zero |
| 910 | * if it does. If the callback returns non-zero, this function will |
| 911 | * return to the caller and not iterate over any more class_devices. |
| 912 | * |
| 913 | * Note, you will need to drop the reference with class_device_put() after use. |
| 914 | * |
| 915 | * We hold class->sem in this function, so it can not be |
| 916 | * re-acquired in @match, otherwise it will self-deadlocking. For |
| 917 | * example, calls to add or remove class members would be verboten. |
| 918 | */ |
| 919 | struct class_device *class_find_child(struct class *class, void *data, |
| 920 | int (*match)(struct class_device *, void *)) |
| 921 | { |
| 922 | struct class_device *dev; |
| 923 | int found = 0; |
| 924 | |
| 925 | if (!class) |
| 926 | return NULL; |
| 927 | |
| 928 | down(&class->sem); |
| 929 | list_for_each_entry(dev, &class->children, node) { |
| 930 | dev = class_device_get(dev); |
| 931 | if (dev) { |
| 932 | if (match(dev, data)) { |
| 933 | found = 1; |
| 934 | break; |
| 935 | } else |
| 936 | class_device_put(dev); |
| 937 | } else |
| 938 | break; |
| 939 | } |
| 940 | up(&class->sem); |
| 941 | |
| 942 | return found ? dev : NULL; |
| 943 | } |
| 944 | EXPORT_SYMBOL_GPL(class_find_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | int class_interface_register(struct class_interface *class_intf) |
| 947 | { |
| 948 | struct class *parent; |
| 949 | struct class_device *class_dev; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 950 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
| 952 | if (!class_intf || !class_intf->class) |
| 953 | return -ENODEV; |
| 954 | |
| 955 | parent = class_get(class_intf->class); |
| 956 | if (!parent) |
| 957 | return -EINVAL; |
| 958 | |
| 959 | down(&parent->sem); |
| 960 | list_add_tail(&class_intf->node, &parent->interfaces); |
| 961 | if (class_intf->add) { |
| 962 | list_for_each_entry(class_dev, &parent->children, node) |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 963 | class_intf->add(class_dev, class_intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 965 | if (class_intf->add_dev) { |
| 966 | list_for_each_entry(dev, &parent->devices, node) |
| 967 | class_intf->add_dev(dev, class_intf); |
| 968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | up(&parent->sem); |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | void class_interface_unregister(struct class_interface *class_intf) |
| 975 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 976 | struct class *parent = class_intf->class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | struct class_device *class_dev; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 978 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
| 980 | if (!parent) |
| 981 | return; |
| 982 | |
| 983 | down(&parent->sem); |
| 984 | list_del_init(&class_intf->node); |
| 985 | if (class_intf->remove) { |
| 986 | list_for_each_entry(class_dev, &parent->children, node) |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 987 | class_intf->remove(class_dev, class_intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 989 | if (class_intf->remove_dev) { |
| 990 | list_for_each_entry(dev, &parent->devices, node) |
| 991 | class_intf->remove_dev(dev, class_intf); |
| 992 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | up(&parent->sem); |
| 994 | |
| 995 | class_put(parent); |
| 996 | } |
| 997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | int __init classes_init(void) |
| 999 | { |
Greg Kroah-Hartman | 443dbf9 | 2007-10-29 23:22:26 -0500 | [diff] [blame] | 1000 | class_kset = kset_create_and_add("class", NULL, NULL); |
| 1001 | if (!class_kset) |
| 1002 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
| 1004 | /* ick, this is ugly, the things we go through to keep from showing up |
| 1005 | * in sysfs... */ |
Greg Kroah-Hartman | e4bc166 | 2007-09-26 11:12:00 -0700 | [diff] [blame] | 1006 | kset_init(&class_obj_subsys); |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 1007 | kobject_set_name(&class_obj_subsys.kobj, "class_obj"); |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 1008 | if (!class_obj_subsys.kobj.parent) |
| 1009 | class_obj_subsys.kobj.parent = &class_obj_subsys.kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | EXPORT_SYMBOL_GPL(class_create_file); |
| 1014 | EXPORT_SYMBOL_GPL(class_remove_file); |
| 1015 | EXPORT_SYMBOL_GPL(class_register); |
| 1016 | EXPORT_SYMBOL_GPL(class_unregister); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 1017 | EXPORT_SYMBOL_GPL(class_create); |
| 1018 | EXPORT_SYMBOL_GPL(class_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
| 1020 | EXPORT_SYMBOL_GPL(class_device_register); |
| 1021 | EXPORT_SYMBOL_GPL(class_device_unregister); |
| 1022 | EXPORT_SYMBOL_GPL(class_device_initialize); |
| 1023 | EXPORT_SYMBOL_GPL(class_device_add); |
| 1024 | EXPORT_SYMBOL_GPL(class_device_del); |
| 1025 | EXPORT_SYMBOL_GPL(class_device_get); |
| 1026 | EXPORT_SYMBOL_GPL(class_device_put); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 1027 | EXPORT_SYMBOL_GPL(class_device_create); |
| 1028 | EXPORT_SYMBOL_GPL(class_device_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | EXPORT_SYMBOL_GPL(class_device_create_file); |
| 1030 | EXPORT_SYMBOL_GPL(class_device_remove_file); |
Greg Kroah-Hartman | d919fd4 | 2007-10-31 12:51:29 -0700 | [diff] [blame] | 1031 | EXPORT_SYMBOL_GPL(class_device_create_bin_file); |
| 1032 | EXPORT_SYMBOL_GPL(class_device_remove_bin_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | EXPORT_SYMBOL_GPL(class_interface_register); |
| 1035 | EXPORT_SYMBOL_GPL(class_interface_unregister); |