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