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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "base.h" |
| 21 | |
| 22 | #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] | 23 | #define to_class(obj) container_of(obj, struct class, subsys.kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | static ssize_t |
| 26 | class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) |
| 27 | { |
| 28 | struct class_attribute * class_attr = to_class_attr(attr); |
| 29 | struct class * dc = to_class(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 30 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | if (class_attr->show) |
| 33 | ret = class_attr->show(dc, buf); |
| 34 | return ret; |
| 35 | } |
| 36 | |
| 37 | static ssize_t |
| 38 | class_attr_store(struct kobject * kobj, struct attribute * attr, |
| 39 | const char * buf, size_t count) |
| 40 | { |
| 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 | |
| 50 | static void class_release(struct kobject * kobj) |
| 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 | adc5680 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 74 | static decl_subsys(class, &class_ktype, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | |
| 77 | int class_create_file(struct class * cls, const struct class_attribute * attr) |
| 78 | { |
| 79 | int error; |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } else |
| 83 | error = -EINVAL; |
| 84 | return error; |
| 85 | } |
| 86 | |
| 87 | void class_remove_file(struct class * cls, const struct class_attribute * attr) |
| 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 | 1ef4cfa | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 96 | return container_of(kset_get(&cls->subsys), struct class, subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
| 99 | |
Greg Kroah-Hartman | 1740757 | 2006-05-02 16:59:59 +0200 | [diff] [blame] | 100 | static void class_put(struct class * cls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 102 | if (cls) |
Greg Kroah-Hartman | 6e9d930 | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 103 | kset_put(&cls->subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | static int add_class_attrs(struct class * cls) |
| 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++) { |
| 114 | error = class_create_file(cls,&cls->class_attrs[i]); |
| 115 | if (error) |
| 116 | goto Err; |
| 117 | } |
| 118 | } |
| 119 | Done: |
| 120 | return error; |
| 121 | Err: |
| 122 | while (--i >= 0) |
| 123 | class_remove_file(cls,&cls->class_attrs[i]); |
| 124 | goto Done; |
| 125 | } |
| 126 | |
| 127 | static void remove_class_attrs(struct class * cls) |
| 128 | { |
| 129 | int i; |
| 130 | |
| 131 | if (cls->class_attrs) { |
| 132 | for (i = 0; attr_name(cls->class_attrs[i]); i++) |
| 133 | class_remove_file(cls,&cls->class_attrs[i]); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | int class_register(struct class * cls) |
| 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 | d6b05b8 | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 152 | cls->subsys.kobj.kset = &class_subsys; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | error = subsystem_register(&cls->subsys); |
| 155 | if (!error) { |
| 156 | error = add_class_attrs(class_get(cls)); |
| 157 | class_put(cls); |
| 158 | } |
| 159 | return error; |
| 160 | } |
| 161 | |
| 162 | void class_unregister(struct class * cls) |
| 163 | { |
| 164 | pr_debug("device class '%s': unregistering\n", cls->name); |
| 165 | remove_class_attrs(cls); |
| 166 | subsystem_unregister(&cls->subsys); |
| 167 | } |
| 168 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 169 | static void class_create_release(struct class *cls) |
| 170 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 171 | pr_debug("%s called for %s\n", __FUNCTION__, cls->name); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 172 | kfree(cls); |
| 173 | } |
| 174 | |
| 175 | static void class_device_create_release(struct class_device *class_dev) |
| 176 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 177 | 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] | 178 | kfree(class_dev); |
| 179 | } |
| 180 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 181 | /* needed to allow these devices to have parent class devices */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 182 | static int class_device_create_uevent(struct class_device *class_dev, |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 183 | struct kobj_uevent_env *env) |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 184 | { |
| 185 | pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id); |
| 186 | return 0; |
| 187 | } |
| 188 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 189 | /** |
| 190 | * class_create - create a struct class structure |
| 191 | * @owner: pointer to the module that is to "own" this struct class |
| 192 | * @name: pointer to a string for the name of this class. |
| 193 | * |
| 194 | * This is used to create a struct class pointer that can then be used |
| 195 | * in calls to class_device_create(). |
| 196 | * |
| 197 | * Note, the pointer created here is to be destroyed when finished by |
| 198 | * making a call to class_destroy(). |
| 199 | */ |
Miguel Ojeda Sandonis | ab7d737 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 200 | struct class *class_create(struct module *owner, const char *name) |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 201 | { |
| 202 | struct class *cls; |
| 203 | int retval; |
| 204 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 205 | cls = kzalloc(sizeof(*cls), GFP_KERNEL); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 206 | if (!cls) { |
| 207 | retval = -ENOMEM; |
| 208 | goto error; |
| 209 | } |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 210 | |
| 211 | cls->name = name; |
| 212 | cls->owner = owner; |
| 213 | cls->class_release = class_create_release; |
| 214 | cls->release = class_device_create_release; |
| 215 | |
| 216 | retval = class_register(cls); |
| 217 | if (retval) |
| 218 | goto error; |
| 219 | |
| 220 | return cls; |
| 221 | |
| 222 | error: |
| 223 | kfree(cls); |
| 224 | return ERR_PTR(retval); |
| 225 | } |
| 226 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 227 | /** |
| 228 | * class_destroy - destroys a struct class structure |
Rolf Eike Beer | 92a0f86 | 2006-09-29 01:59:13 -0700 | [diff] [blame] | 229 | * @cls: pointer to the struct class that is to be destroyed |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 230 | * |
| 231 | * Note, the pointer to be destroyed must have been created with a call |
| 232 | * to class_create(). |
| 233 | */ |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 234 | void class_destroy(struct class *cls) |
| 235 | { |
| 236 | if ((cls == NULL) || (IS_ERR(cls))) |
| 237 | return; |
| 238 | |
| 239 | class_unregister(cls); |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | /* Class Device Stuff */ |
| 243 | |
| 244 | int class_device_create_file(struct class_device * class_dev, |
| 245 | const struct class_device_attribute * attr) |
| 246 | { |
| 247 | int error = -EINVAL; |
| 248 | if (class_dev) |
| 249 | error = sysfs_create_file(&class_dev->kobj, &attr->attr); |
| 250 | return error; |
| 251 | } |
| 252 | |
| 253 | void class_device_remove_file(struct class_device * class_dev, |
| 254 | const struct class_device_attribute * attr) |
| 255 | { |
| 256 | if (class_dev) |
| 257 | sysfs_remove_file(&class_dev->kobj, &attr->attr); |
| 258 | } |
| 259 | |
Greg Kroah-Hartman | d919fd4 | 2007-10-31 12:51:29 -0700 | [diff] [blame] | 260 | int class_device_create_bin_file(struct class_device *class_dev, |
| 261 | struct bin_attribute *attr) |
| 262 | { |
| 263 | int error = -EINVAL; |
| 264 | if (class_dev) |
| 265 | error = sysfs_create_bin_file(&class_dev->kobj, attr); |
| 266 | return error; |
| 267 | } |
| 268 | |
| 269 | void class_device_remove_bin_file(struct class_device *class_dev, |
| 270 | struct bin_attribute *attr) |
| 271 | { |
| 272 | if (class_dev) |
| 273 | sysfs_remove_bin_file(&class_dev->kobj, attr); |
| 274 | } |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | static ssize_t |
| 277 | class_device_attr_show(struct kobject * kobj, struct attribute * attr, |
| 278 | char * buf) |
| 279 | { |
| 280 | struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); |
| 281 | struct class_device * cd = to_class_dev(kobj); |
| 282 | ssize_t ret = 0; |
| 283 | |
| 284 | if (class_dev_attr->show) |
| 285 | ret = class_dev_attr->show(cd, buf); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static ssize_t |
| 290 | class_device_attr_store(struct kobject * kobj, struct attribute * attr, |
| 291 | const char * buf, size_t count) |
| 292 | { |
| 293 | struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); |
| 294 | struct class_device * cd = to_class_dev(kobj); |
| 295 | ssize_t ret = 0; |
| 296 | |
| 297 | if (class_dev_attr->store) |
| 298 | ret = class_dev_attr->store(cd, buf, count); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | static struct sysfs_ops class_dev_sysfs_ops = { |
| 303 | .show = class_device_attr_show, |
| 304 | .store = class_device_attr_store, |
| 305 | }; |
| 306 | |
| 307 | static void class_dev_release(struct kobject * kobj) |
| 308 | { |
| 309 | struct class_device *cd = to_class_dev(kobj); |
| 310 | struct class * cls = cd->class; |
| 311 | |
| 312 | pr_debug("device class '%s': release.\n", cd->class_id); |
| 313 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 314 | if (cd->release) |
| 315 | cd->release(cd); |
| 316 | else if (cls->release) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | cls->release(cd); |
| 318 | else { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 319 | printk(KERN_ERR "Class Device '%s' does not have a release() function, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | "it is broken and must be fixed.\n", |
| 321 | cd->class_id); |
| 322 | WARN_ON(1); |
| 323 | } |
| 324 | } |
| 325 | |
Greg Kroah-Hartman | adc5680 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 326 | static struct kobj_type class_device_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | .sysfs_ops = &class_dev_sysfs_ops, |
| 328 | .release = class_dev_release, |
| 329 | }; |
| 330 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 331 | static int class_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | struct kobj_type *ktype = get_ktype(kobj); |
| 334 | |
Greg Kroah-Hartman | adc5680 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 335 | if (ktype == &class_device_ktype) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | struct class_device *class_dev = to_class_dev(kobj); |
| 337 | if (class_dev->class) |
| 338 | return 1; |
| 339 | } |
| 340 | return 0; |
| 341 | } |
| 342 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 343 | static const char *class_uevent_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
| 345 | struct class_device *class_dev = to_class_dev(kobj); |
| 346 | |
| 347 | return class_dev->class->name; |
| 348 | } |
| 349 | |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 350 | #ifdef CONFIG_SYSFS_DEPRECATED |
| 351 | char *make_class_name(const char *name, struct kobject *kobj) |
| 352 | { |
| 353 | char *class_name; |
| 354 | int size; |
| 355 | |
| 356 | size = strlen(name) + strlen(kobject_name(kobj)) + 2; |
| 357 | |
| 358 | class_name = kmalloc(size, GFP_KERNEL); |
| 359 | if (!class_name) |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 360 | return NULL; |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 361 | |
| 362 | strcpy(class_name, name); |
| 363 | strcat(class_name, ":"); |
| 364 | strcat(class_name, kobject_name(kobj)); |
| 365 | return class_name; |
| 366 | } |
| 367 | |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 368 | static int make_deprecated_class_device_links(struct class_device *class_dev) |
| 369 | { |
| 370 | char *class_name; |
| 371 | int error; |
| 372 | |
| 373 | if (!class_dev->dev) |
| 374 | return 0; |
| 375 | |
| 376 | class_name = make_class_name(class_dev->class->name, &class_dev->kobj); |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 377 | if (class_name) |
| 378 | error = sysfs_create_link(&class_dev->dev->kobj, |
| 379 | &class_dev->kobj, class_name); |
| 380 | else |
| 381 | error = -ENOMEM; |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 382 | kfree(class_name); |
| 383 | return error; |
| 384 | } |
| 385 | |
| 386 | static void remove_deprecated_class_device_links(struct class_device *class_dev) |
| 387 | { |
| 388 | char *class_name; |
| 389 | |
| 390 | if (!class_dev->dev) |
| 391 | return; |
| 392 | |
| 393 | class_name = make_class_name(class_dev->class->name, &class_dev->kobj); |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 394 | if (class_name) |
| 395 | sysfs_remove_link(&class_dev->dev->kobj, class_name); |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 396 | kfree(class_name); |
| 397 | } |
| 398 | #else |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 399 | static inline int make_deprecated_class_device_links(struct class_device *cd) |
| 400 | { return 0; } |
| 401 | static void remove_deprecated_class_device_links(struct class_device *cd) |
| 402 | { } |
| 403 | #endif |
| 404 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 405 | static int class_uevent(struct kset *kset, struct kobject *kobj, |
| 406 | struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
| 408 | struct class_device *class_dev = to_class_dev(kobj); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 409 | struct device *dev = class_dev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | int retval = 0; |
| 411 | |
| 412 | pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id); |
| 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (MAJOR(class_dev->devt)) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 415 | add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 417 | add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 420 | if (dev) { |
| 421 | const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL); |
| 422 | if (path) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 423 | add_uevent_var(env, "PHYSDEVPATH=%s", path); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 424 | kfree(path); |
| 425 | } |
| 426 | |
| 427 | if (dev->bus) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 428 | add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 429 | |
| 430 | if (dev->driver) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 431 | add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 434 | if (class_dev->uevent) { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 435 | /* have the class device specific function add its stuff */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 436 | retval = class_dev->uevent(class_dev, env); |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 437 | if (retval) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 438 | pr_debug("class_dev->uevent() returned %d\n", retval); |
| 439 | } else if (class_dev->class->uevent) { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 440 | /* have the class specific function add its stuff */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 441 | retval = class_dev->class->uevent(class_dev, env); |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 442 | if (retval) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 443 | pr_debug("class->uevent() returned %d\n", retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | return retval; |
| 447 | } |
| 448 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 449 | static struct kset_uevent_ops class_uevent_ops = { |
| 450 | .filter = class_uevent_filter, |
| 451 | .name = class_uevent_name, |
| 452 | .uevent = class_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
Greg Kroah-Hartman | adc5680 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 455 | static decl_subsys(class_obj, &class_device_ktype, &class_uevent_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
| 457 | |
| 458 | static int class_device_add_attrs(struct class_device * cd) |
| 459 | { |
| 460 | int i; |
| 461 | int error = 0; |
| 462 | struct class * cls = cd->class; |
| 463 | |
| 464 | if (cls->class_dev_attrs) { |
| 465 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) { |
| 466 | error = class_device_create_file(cd, |
| 467 | &cls->class_dev_attrs[i]); |
| 468 | if (error) |
| 469 | goto Err; |
| 470 | } |
| 471 | } |
| 472 | Done: |
| 473 | return error; |
| 474 | Err: |
| 475 | while (--i >= 0) |
| 476 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); |
| 477 | goto Done; |
| 478 | } |
| 479 | |
| 480 | static void class_device_remove_attrs(struct class_device * cd) |
| 481 | { |
| 482 | int i; |
| 483 | struct class * cls = cd->class; |
| 484 | |
| 485 | if (cls->class_dev_attrs) { |
| 486 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) |
| 487 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); |
| 488 | } |
| 489 | } |
| 490 | |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 491 | static int class_device_add_groups(struct class_device * cd) |
| 492 | { |
| 493 | int i; |
| 494 | int error = 0; |
| 495 | |
| 496 | if (cd->groups) { |
| 497 | for (i = 0; cd->groups[i]; i++) { |
| 498 | error = sysfs_create_group(&cd->kobj, cd->groups[i]); |
| 499 | if (error) { |
| 500 | while (--i >= 0) |
| 501 | sysfs_remove_group(&cd->kobj, cd->groups[i]); |
| 502 | goto out; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | out: |
| 507 | return error; |
| 508 | } |
| 509 | |
| 510 | static void class_device_remove_groups(struct class_device * cd) |
| 511 | { |
| 512 | int i; |
| 513 | if (cd->groups) { |
| 514 | for (i = 0; cd->groups[i]; i++) { |
| 515 | sysfs_remove_group(&cd->kobj, cd->groups[i]); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | static ssize_t show_dev(struct class_device *class_dev, char *buf) |
| 521 | { |
| 522 | return print_dev_t(buf, class_dev->devt); |
| 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 525 | static struct class_device_attribute class_devt_attr = |
| 526 | __ATTR(dev, S_IRUGO, show_dev, NULL); |
| 527 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 528 | static ssize_t store_uevent(struct class_device *class_dev, |
| 529 | const char *buf, size_t count) |
| 530 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 531 | kobject_uevent(&class_dev->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 532 | return count; |
| 533 | } |
| 534 | |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 535 | static struct class_device_attribute class_uevent_attr = |
| 536 | __ATTR(uevent, S_IWUSR, NULL, store_uevent); |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | void class_device_initialize(struct class_device *class_dev) |
| 539 | { |
| 540 | kobj_set_kset_s(class_dev, class_obj_subsys); |
| 541 | kobject_init(&class_dev->kobj); |
| 542 | INIT_LIST_HEAD(&class_dev->node); |
| 543 | } |
| 544 | |
| 545 | int class_device_add(struct class_device *class_dev) |
| 546 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 547 | struct class *parent_class = NULL; |
| 548 | struct class_device *parent_class_dev = NULL; |
| 549 | struct class_interface *class_intf; |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 550 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | class_dev = class_device_get(class_dev); |
| 553 | if (!class_dev) |
| 554 | return -EINVAL; |
| 555 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 556 | if (!strlen(class_dev->class_id)) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 557 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 559 | parent_class = class_get(class_dev->class); |
| 560 | if (!parent_class) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 561 | goto out1; |
| 562 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 563 | parent_class_dev = class_device_get(class_dev->parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
| 565 | pr_debug("CLASS: registering class device: ID = '%s'\n", |
| 566 | class_dev->class_id); |
| 567 | |
| 568 | /* first, register with generic layer. */ |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 569 | error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id); |
| 570 | if (error) |
| 571 | goto out2; |
| 572 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 573 | if (parent_class_dev) |
| 574 | class_dev->kobj.parent = &parent_class_dev->kobj; |
| 575 | else |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 576 | class_dev->kobj.parent = &parent_class->subsys.kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 578 | error = kobject_add(&class_dev->kobj); |
| 579 | if (error) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 580 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 582 | /* add the needed attributes to this device */ |
Cornelia Huck | f0e1761 | 2006-09-22 11:37:00 +0200 | [diff] [blame] | 583 | error = sysfs_create_link(&class_dev->kobj, |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 584 | &parent_class->subsys.kobj, "subsystem"); |
Cornelia Huck | f0e1761 | 2006-09-22 11:37:00 +0200 | [diff] [blame] | 585 | if (error) |
| 586 | goto out3; |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 587 | |
| 588 | error = class_device_create_file(class_dev, &class_uevent_attr); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 589 | if (error) |
| 590 | goto out3; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 591 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 592 | if (MAJOR(class_dev->devt)) { |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 593 | error = class_device_create_file(class_dev, &class_devt_attr); |
| 594 | if (error) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 595 | goto out4; |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 598 | error = class_device_add_attrs(class_dev); |
| 599 | if (error) |
| 600 | goto out5; |
| 601 | |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 602 | if (class_dev->dev) { |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 603 | error = sysfs_create_link(&class_dev->kobj, |
| 604 | &class_dev->dev->kobj, "device"); |
| 605 | if (error) |
| 606 | goto out6; |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 607 | } |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 608 | |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 609 | error = class_device_add_groups(class_dev); |
| 610 | if (error) |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 611 | goto out7; |
| 612 | |
| 613 | error = make_deprecated_class_device_links(class_dev); |
| 614 | if (error) |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 615 | goto out8; |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 616 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 617 | kobject_uevent(&class_dev->kobj, KOBJ_ADD); |
Dmitry Torokhov | dbe9035 | 2005-09-15 02:01:37 -0500 | [diff] [blame] | 618 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 619 | /* notify any interfaces this device is now here */ |
Jayachandran C | a143889 | 2006-04-03 12:31:53 -0700 | [diff] [blame] | 620 | down(&parent_class->sem); |
| 621 | list_add_tail(&class_dev->node, &parent_class->children); |
| 622 | list_for_each_entry(class_intf, &parent_class->interfaces, node) { |
| 623 | if (class_intf->add) |
| 624 | class_intf->add(class_dev, class_intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
Jayachandran C | a143889 | 2006-04-03 12:31:53 -0700 | [diff] [blame] | 626 | up(&parent_class->sem); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 627 | |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 628 | goto out1; |
| 629 | |
| 630 | out8: |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 631 | class_device_remove_groups(class_dev); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 632 | out7: |
| 633 | if (class_dev->dev) |
| 634 | sysfs_remove_link(&class_dev->kobj, "device"); |
| 635 | out6: |
| 636 | class_device_remove_attrs(class_dev); |
| 637 | out5: |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 638 | if (MAJOR(class_dev->devt)) |
| 639 | class_device_remove_file(class_dev, &class_devt_attr); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 640 | out4: |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 641 | class_device_remove_file(class_dev, &class_uevent_attr); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 642 | out3: |
| 643 | kobject_del(&class_dev->kobj); |
| 644 | out2: |
| 645 | if(parent_class_dev) |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 646 | class_device_put(parent_class_dev); |
Stephen Hemminger | b7fe4a6 | 2006-04-26 09:53:14 -0700 | [diff] [blame] | 647 | class_put(parent_class); |
| 648 | out1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | class_device_put(class_dev); |
| 650 | return error; |
| 651 | } |
| 652 | |
| 653 | int class_device_register(struct class_device *class_dev) |
| 654 | { |
| 655 | class_device_initialize(class_dev); |
| 656 | return class_device_add(class_dev); |
| 657 | } |
| 658 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 659 | /** |
| 660 | * 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] | 661 | * @cls: pointer to the struct class that this device should be registered to. |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 662 | * @parent: pointer to the parent struct class_device of this new device, if any. |
Rolf Eike Beer | 92a0f86 | 2006-09-29 01:59:13 -0700 | [diff] [blame] | 663 | * @devt: the dev_t for the char device to be added. |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 664 | * @device: a pointer to a struct device that is assiociated with this class device. |
| 665 | * @fmt: string for the class device's name |
| 666 | * |
| 667 | * This function can be used by char device classes. A struct |
| 668 | * class_device will be created in sysfs, registered to the specified |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 669 | * class. |
| 670 | * A "dev" file will be created, showing the dev_t for the device, if |
| 671 | * the dev_t is not 0,0. |
| 672 | * If a pointer to a parent struct class_device is passed in, the newly |
| 673 | * created struct class_device will be a child of that device in sysfs. |
| 674 | * The pointer to the struct class_device will be returned from the |
| 675 | * call. Any further sysfs files that might be required can be created |
| 676 | * using this pointer. |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 677 | * |
| 678 | * Note: the struct class passed to this function must have previously |
| 679 | * been created with a call to class_create(). |
| 680 | */ |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 681 | struct class_device *class_device_create(struct class *cls, |
| 682 | struct class_device *parent, |
| 683 | dev_t devt, |
Dmitry Torokhov | ddd5d35 | 2006-08-10 01:18:18 -0400 | [diff] [blame] | 684 | struct device *device, |
| 685 | const char *fmt, ...) |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 686 | { |
| 687 | va_list args; |
| 688 | struct class_device *class_dev = NULL; |
| 689 | int retval = -ENODEV; |
| 690 | |
| 691 | if (cls == NULL || IS_ERR(cls)) |
| 692 | goto error; |
| 693 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 694 | class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 695 | if (!class_dev) { |
| 696 | retval = -ENOMEM; |
| 697 | goto error; |
| 698 | } |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 699 | |
| 700 | class_dev->devt = devt; |
| 701 | class_dev->dev = device; |
| 702 | class_dev->class = cls; |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 703 | class_dev->parent = parent; |
| 704 | class_dev->release = class_device_create_release; |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 705 | class_dev->uevent = class_device_create_uevent; |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 706 | |
| 707 | va_start(args, fmt); |
| 708 | vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args); |
| 709 | va_end(args); |
| 710 | retval = class_device_register(class_dev); |
| 711 | if (retval) |
| 712 | goto error; |
| 713 | |
| 714 | return class_dev; |
| 715 | |
| 716 | error: |
| 717 | kfree(class_dev); |
| 718 | return ERR_PTR(retval); |
| 719 | } |
| 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | void class_device_del(struct class_device *class_dev) |
| 722 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 723 | struct class *parent_class = class_dev->class; |
| 724 | struct class_device *parent_device = class_dev->parent; |
| 725 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 727 | if (parent_class) { |
| 728 | down(&parent_class->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | list_del_init(&class_dev->node); |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 730 | list_for_each_entry(class_intf, &parent_class->interfaces, node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if (class_intf->remove) |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 732 | class_intf->remove(class_dev, class_intf); |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 733 | up(&parent_class->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 736 | if (class_dev->dev) { |
Kay Sievers | 805fab4 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 737 | remove_deprecated_class_device_links(class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | sysfs_remove_link(&class_dev->kobj, "device"); |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 739 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 740 | sysfs_remove_link(&class_dev->kobj, "subsystem"); |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 741 | class_device_remove_file(class_dev, &class_uevent_attr); |
| 742 | if (MAJOR(class_dev->devt)) |
| 743 | class_device_remove_file(class_dev, &class_devt_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | class_device_remove_attrs(class_dev); |
Stephen Hemminger | 1498221 | 2006-05-06 17:55:11 -0700 | [diff] [blame] | 745 | class_device_remove_groups(class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 747 | kobject_uevent(&class_dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | kobject_del(&class_dev->kobj); |
| 749 | |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 750 | class_device_put(parent_device); |
| 751 | class_put(parent_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | void class_device_unregister(struct class_device *class_dev) |
| 755 | { |
| 756 | pr_debug("CLASS: Unregistering class device. ID = '%s'\n", |
| 757 | class_dev->class_id); |
| 758 | class_device_del(class_dev); |
| 759 | class_device_put(class_dev); |
| 760 | } |
| 761 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 762 | /** |
| 763 | * class_device_destroy - removes a class device that was created with class_device_create() |
| 764 | * @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] | 765 | * @devt: the dev_t of the device that was previously registered. |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 766 | * |
| 767 | * This call unregisters and cleans up a class device that was created with a |
| 768 | * call to class_device_create() |
| 769 | */ |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 770 | void class_device_destroy(struct class *cls, dev_t devt) |
| 771 | { |
| 772 | struct class_device *class_dev = NULL; |
| 773 | struct class_device *class_dev_tmp; |
| 774 | |
| 775 | down(&cls->sem); |
| 776 | list_for_each_entry(class_dev_tmp, &cls->children, node) { |
| 777 | if (class_dev_tmp->devt == devt) { |
| 778 | class_dev = class_dev_tmp; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | up(&cls->sem); |
| 783 | |
| 784 | if (class_dev) |
| 785 | class_device_unregister(class_dev); |
| 786 | } |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | struct class_device * class_device_get(struct class_device *class_dev) |
| 789 | { |
| 790 | if (class_dev) |
| 791 | return to_class_dev(kobject_get(&class_dev->kobj)); |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | void class_device_put(struct class_device *class_dev) |
| 796 | { |
Greg Kroah-Hartman | 51d172d | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 797 | if (class_dev) |
| 798 | kobject_put(&class_dev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | |
| 802 | int class_interface_register(struct class_interface *class_intf) |
| 803 | { |
| 804 | struct class *parent; |
| 805 | struct class_device *class_dev; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 806 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
| 808 | if (!class_intf || !class_intf->class) |
| 809 | return -ENODEV; |
| 810 | |
| 811 | parent = class_get(class_intf->class); |
| 812 | if (!parent) |
| 813 | return -EINVAL; |
| 814 | |
| 815 | down(&parent->sem); |
| 816 | list_add_tail(&class_intf->node, &parent->interfaces); |
| 817 | if (class_intf->add) { |
| 818 | list_for_each_entry(class_dev, &parent->children, node) |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 819 | class_intf->add(class_dev, class_intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 821 | if (class_intf->add_dev) { |
| 822 | list_for_each_entry(dev, &parent->devices, node) |
| 823 | class_intf->add_dev(dev, class_intf); |
| 824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | up(&parent->sem); |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | void class_interface_unregister(struct class_interface *class_intf) |
| 831 | { |
| 832 | struct class * parent = class_intf->class; |
| 833 | struct class_device *class_dev; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 834 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | if (!parent) |
| 837 | return; |
| 838 | |
| 839 | down(&parent->sem); |
| 840 | list_del_init(&class_intf->node); |
| 841 | if (class_intf->remove) { |
| 842 | list_for_each_entry(class_dev, &parent->children, node) |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 843 | class_intf->remove(class_dev, class_intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 845 | if (class_intf->remove_dev) { |
| 846 | list_for_each_entry(dev, &parent->devices, node) |
| 847 | class_intf->remove_dev(dev, class_intf); |
| 848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | up(&parent->sem); |
| 850 | |
| 851 | class_put(parent); |
| 852 | } |
| 853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | int __init classes_init(void) |
| 855 | { |
| 856 | int retval; |
| 857 | |
| 858 | retval = subsystem_register(&class_subsys); |
| 859 | if (retval) |
| 860 | return retval; |
| 861 | |
| 862 | /* ick, this is ugly, the things we go through to keep from showing up |
| 863 | * in sysfs... */ |
Greg Kroah-Hartman | e4bc166 | 2007-09-26 11:12:00 -0700 | [diff] [blame] | 864 | kset_init(&class_obj_subsys); |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 865 | if (!class_obj_subsys.kobj.parent) |
| 866 | class_obj_subsys.kobj.parent = &class_obj_subsys.kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | EXPORT_SYMBOL_GPL(class_create_file); |
| 871 | EXPORT_SYMBOL_GPL(class_remove_file); |
| 872 | EXPORT_SYMBOL_GPL(class_register); |
| 873 | EXPORT_SYMBOL_GPL(class_unregister); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 874 | EXPORT_SYMBOL_GPL(class_create); |
| 875 | EXPORT_SYMBOL_GPL(class_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | |
| 877 | EXPORT_SYMBOL_GPL(class_device_register); |
| 878 | EXPORT_SYMBOL_GPL(class_device_unregister); |
| 879 | EXPORT_SYMBOL_GPL(class_device_initialize); |
| 880 | EXPORT_SYMBOL_GPL(class_device_add); |
| 881 | EXPORT_SYMBOL_GPL(class_device_del); |
| 882 | EXPORT_SYMBOL_GPL(class_device_get); |
| 883 | EXPORT_SYMBOL_GPL(class_device_put); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 884 | EXPORT_SYMBOL_GPL(class_device_create); |
| 885 | EXPORT_SYMBOL_GPL(class_device_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | EXPORT_SYMBOL_GPL(class_device_create_file); |
| 887 | EXPORT_SYMBOL_GPL(class_device_remove_file); |
Greg Kroah-Hartman | d919fd4 | 2007-10-31 12:51:29 -0700 | [diff] [blame] | 888 | EXPORT_SYMBOL_GPL(class_device_create_bin_file); |
| 889 | EXPORT_SYMBOL_GPL(class_device_remove_bin_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | |
| 891 | EXPORT_SYMBOL_GPL(class_interface_register); |
| 892 | EXPORT_SYMBOL_GPL(class_interface_unregister); |