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