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 | |
| 13 | #include <linux/config.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/kdev_t.h> |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame^] | 19 | #include <linux/err.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) |
| 23 | #define to_class(obj) container_of(obj, struct class, subsys.kset.kobj) |
| 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) { |
| 81 | error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr); |
| 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) |
| 90 | sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr); |
| 91 | } |
| 92 | |
| 93 | struct class * class_get(struct class * cls) |
| 94 | { |
| 95 | if (cls) |
| 96 | return container_of(subsys_get(&cls->subsys), struct class, subsys); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | void class_put(struct class * cls) |
| 101 | { |
| 102 | subsys_put(&cls->subsys); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static int add_class_attrs(struct class * cls) |
| 107 | { |
| 108 | int i; |
| 109 | int error = 0; |
| 110 | |
| 111 | if (cls->class_attrs) { |
| 112 | for (i = 0; attr_name(cls->class_attrs[i]); i++) { |
| 113 | error = class_create_file(cls,&cls->class_attrs[i]); |
| 114 | if (error) |
| 115 | goto Err; |
| 116 | } |
| 117 | } |
| 118 | Done: |
| 119 | return error; |
| 120 | Err: |
| 121 | while (--i >= 0) |
| 122 | class_remove_file(cls,&cls->class_attrs[i]); |
| 123 | goto Done; |
| 124 | } |
| 125 | |
| 126 | static void remove_class_attrs(struct class * cls) |
| 127 | { |
| 128 | int i; |
| 129 | |
| 130 | if (cls->class_attrs) { |
| 131 | for (i = 0; attr_name(cls->class_attrs[i]); i++) |
| 132 | class_remove_file(cls,&cls->class_attrs[i]); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | int class_register(struct class * cls) |
| 137 | { |
| 138 | int error; |
| 139 | |
| 140 | pr_debug("device class '%s': registering\n", cls->name); |
| 141 | |
| 142 | INIT_LIST_HEAD(&cls->children); |
| 143 | INIT_LIST_HEAD(&cls->interfaces); |
| 144 | init_MUTEX(&cls->sem); |
| 145 | error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name); |
| 146 | if (error) |
| 147 | return error; |
| 148 | |
| 149 | subsys_set_kset(cls, class_subsys); |
| 150 | |
| 151 | error = subsystem_register(&cls->subsys); |
| 152 | if (!error) { |
| 153 | error = add_class_attrs(class_get(cls)); |
| 154 | class_put(cls); |
| 155 | } |
| 156 | return error; |
| 157 | } |
| 158 | |
| 159 | void class_unregister(struct class * cls) |
| 160 | { |
| 161 | pr_debug("device class '%s': unregistering\n", cls->name); |
| 162 | remove_class_attrs(cls); |
| 163 | subsystem_unregister(&cls->subsys); |
| 164 | } |
| 165 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame^] | 166 | static void class_create_release(struct class *cls) |
| 167 | { |
| 168 | kfree(cls); |
| 169 | } |
| 170 | |
| 171 | static void class_device_create_release(struct class_device *class_dev) |
| 172 | { |
| 173 | kfree(class_dev); |
| 174 | } |
| 175 | |
| 176 | struct class *class_create(struct module *owner, char *name) |
| 177 | { |
| 178 | struct class *cls; |
| 179 | int retval; |
| 180 | |
| 181 | cls = kmalloc(sizeof(struct class), GFP_KERNEL); |
| 182 | if (!cls) { |
| 183 | retval = -ENOMEM; |
| 184 | goto error; |
| 185 | } |
| 186 | memset(cls, 0x00, sizeof(struct class)); |
| 187 | |
| 188 | cls->name = name; |
| 189 | cls->owner = owner; |
| 190 | cls->class_release = class_create_release; |
| 191 | cls->release = class_device_create_release; |
| 192 | |
| 193 | retval = class_register(cls); |
| 194 | if (retval) |
| 195 | goto error; |
| 196 | |
| 197 | return cls; |
| 198 | |
| 199 | error: |
| 200 | kfree(cls); |
| 201 | return ERR_PTR(retval); |
| 202 | } |
| 203 | |
| 204 | void class_destroy(struct class *cls) |
| 205 | { |
| 206 | if ((cls == NULL) || (IS_ERR(cls))) |
| 207 | return; |
| 208 | |
| 209 | class_unregister(cls); |
| 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | /* Class Device Stuff */ |
| 213 | |
| 214 | int class_device_create_file(struct class_device * class_dev, |
| 215 | const struct class_device_attribute * attr) |
| 216 | { |
| 217 | int error = -EINVAL; |
| 218 | if (class_dev) |
| 219 | error = sysfs_create_file(&class_dev->kobj, &attr->attr); |
| 220 | return error; |
| 221 | } |
| 222 | |
| 223 | void class_device_remove_file(struct class_device * class_dev, |
| 224 | const struct class_device_attribute * attr) |
| 225 | { |
| 226 | if (class_dev) |
| 227 | sysfs_remove_file(&class_dev->kobj, &attr->attr); |
| 228 | } |
| 229 | |
| 230 | int class_device_create_bin_file(struct class_device *class_dev, |
| 231 | struct bin_attribute *attr) |
| 232 | { |
| 233 | int error = -EINVAL; |
| 234 | if (class_dev) |
| 235 | error = sysfs_create_bin_file(&class_dev->kobj, attr); |
| 236 | return error; |
| 237 | } |
| 238 | |
| 239 | void class_device_remove_bin_file(struct class_device *class_dev, |
| 240 | struct bin_attribute *attr) |
| 241 | { |
| 242 | if (class_dev) |
| 243 | sysfs_remove_bin_file(&class_dev->kobj, attr); |
| 244 | } |
| 245 | |
| 246 | static ssize_t |
| 247 | class_device_attr_show(struct kobject * kobj, struct attribute * attr, |
| 248 | char * buf) |
| 249 | { |
| 250 | struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); |
| 251 | struct class_device * cd = to_class_dev(kobj); |
| 252 | ssize_t ret = 0; |
| 253 | |
| 254 | if (class_dev_attr->show) |
| 255 | ret = class_dev_attr->show(cd, buf); |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | static ssize_t |
| 260 | class_device_attr_store(struct kobject * kobj, struct attribute * attr, |
| 261 | const char * buf, size_t count) |
| 262 | { |
| 263 | struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); |
| 264 | struct class_device * cd = to_class_dev(kobj); |
| 265 | ssize_t ret = 0; |
| 266 | |
| 267 | if (class_dev_attr->store) |
| 268 | ret = class_dev_attr->store(cd, buf, count); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | static struct sysfs_ops class_dev_sysfs_ops = { |
| 273 | .show = class_device_attr_show, |
| 274 | .store = class_device_attr_store, |
| 275 | }; |
| 276 | |
| 277 | static void class_dev_release(struct kobject * kobj) |
| 278 | { |
| 279 | struct class_device *cd = to_class_dev(kobj); |
| 280 | struct class * cls = cd->class; |
| 281 | |
| 282 | pr_debug("device class '%s': release.\n", cd->class_id); |
| 283 | |
| 284 | if (cls->release) |
| 285 | cls->release(cd); |
| 286 | else { |
| 287 | printk(KERN_ERR "Device class '%s' does not have a release() function, " |
| 288 | "it is broken and must be fixed.\n", |
| 289 | cd->class_id); |
| 290 | WARN_ON(1); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static struct kobj_type ktype_class_device = { |
| 295 | .sysfs_ops = &class_dev_sysfs_ops, |
| 296 | .release = class_dev_release, |
| 297 | }; |
| 298 | |
| 299 | static int class_hotplug_filter(struct kset *kset, struct kobject *kobj) |
| 300 | { |
| 301 | struct kobj_type *ktype = get_ktype(kobj); |
| 302 | |
| 303 | if (ktype == &ktype_class_device) { |
| 304 | struct class_device *class_dev = to_class_dev(kobj); |
| 305 | if (class_dev->class) |
| 306 | return 1; |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
Dmitry Torokhov | 419cab3 | 2005-04-26 02:32:54 -0500 | [diff] [blame] | 311 | static const char *class_hotplug_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
| 313 | struct class_device *class_dev = to_class_dev(kobj); |
| 314 | |
| 315 | return class_dev->class->name; |
| 316 | } |
| 317 | |
| 318 | static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp, |
| 319 | int num_envp, char *buffer, int buffer_size) |
| 320 | { |
| 321 | struct class_device *class_dev = to_class_dev(kobj); |
| 322 | int i = 0; |
| 323 | int length = 0; |
| 324 | int retval = 0; |
| 325 | |
| 326 | pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id); |
| 327 | |
| 328 | if (class_dev->dev) { |
| 329 | /* add physical device, backing this device */ |
| 330 | struct device *dev = class_dev->dev; |
| 331 | char *path = kobject_get_path(&dev->kobj, GFP_KERNEL); |
| 332 | |
| 333 | add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, |
| 334 | &length, "PHYSDEVPATH=%s", path); |
| 335 | kfree(path); |
| 336 | |
| 337 | if (dev->bus) |
| 338 | add_hotplug_env_var(envp, num_envp, &i, |
| 339 | buffer, buffer_size, &length, |
| 340 | "PHYSDEVBUS=%s", dev->bus->name); |
| 341 | |
| 342 | if (dev->driver) |
| 343 | add_hotplug_env_var(envp, num_envp, &i, |
| 344 | buffer, buffer_size, &length, |
| 345 | "PHYSDEVDRIVER=%s", dev->driver->name); |
| 346 | } |
| 347 | |
| 348 | if (MAJOR(class_dev->devt)) { |
| 349 | add_hotplug_env_var(envp, num_envp, &i, |
| 350 | buffer, buffer_size, &length, |
| 351 | "MAJOR=%u", MAJOR(class_dev->devt)); |
| 352 | |
| 353 | add_hotplug_env_var(envp, num_envp, &i, |
| 354 | buffer, buffer_size, &length, |
| 355 | "MINOR=%u", MINOR(class_dev->devt)); |
| 356 | } |
| 357 | |
| 358 | /* terminate, set to next free slot, shrink available space */ |
| 359 | envp[i] = NULL; |
| 360 | envp = &envp[i]; |
| 361 | num_envp -= i; |
| 362 | buffer = &buffer[length]; |
| 363 | buffer_size -= length; |
| 364 | |
| 365 | if (class_dev->class->hotplug) { |
| 366 | /* have the bus specific function add its stuff */ |
| 367 | retval = class_dev->class->hotplug (class_dev, envp, num_envp, |
| 368 | buffer, buffer_size); |
| 369 | if (retval) { |
| 370 | pr_debug ("%s - hotplug() returned %d\n", |
| 371 | __FUNCTION__, retval); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return retval; |
| 376 | } |
| 377 | |
| 378 | static struct kset_hotplug_ops class_hotplug_ops = { |
| 379 | .filter = class_hotplug_filter, |
| 380 | .name = class_hotplug_name, |
| 381 | .hotplug = class_hotplug, |
| 382 | }; |
| 383 | |
| 384 | static decl_subsys(class_obj, &ktype_class_device, &class_hotplug_ops); |
| 385 | |
| 386 | |
| 387 | static int class_device_add_attrs(struct class_device * cd) |
| 388 | { |
| 389 | int i; |
| 390 | int error = 0; |
| 391 | struct class * cls = cd->class; |
| 392 | |
| 393 | if (cls->class_dev_attrs) { |
| 394 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) { |
| 395 | error = class_device_create_file(cd, |
| 396 | &cls->class_dev_attrs[i]); |
| 397 | if (error) |
| 398 | goto Err; |
| 399 | } |
| 400 | } |
| 401 | Done: |
| 402 | return error; |
| 403 | Err: |
| 404 | while (--i >= 0) |
| 405 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); |
| 406 | goto Done; |
| 407 | } |
| 408 | |
| 409 | static void class_device_remove_attrs(struct class_device * cd) |
| 410 | { |
| 411 | int i; |
| 412 | struct class * cls = cd->class; |
| 413 | |
| 414 | if (cls->class_dev_attrs) { |
| 415 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) |
| 416 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | static ssize_t show_dev(struct class_device *class_dev, char *buf) |
| 421 | { |
| 422 | return print_dev_t(buf, class_dev->devt); |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | void class_device_initialize(struct class_device *class_dev) |
| 426 | { |
| 427 | kobj_set_kset_s(class_dev, class_obj_subsys); |
| 428 | kobject_init(&class_dev->kobj); |
| 429 | INIT_LIST_HEAD(&class_dev->node); |
| 430 | } |
| 431 | |
| 432 | int class_device_add(struct class_device *class_dev) |
| 433 | { |
| 434 | struct class * parent = NULL; |
| 435 | struct class_interface * class_intf; |
| 436 | int error; |
| 437 | |
| 438 | class_dev = class_device_get(class_dev); |
| 439 | if (!class_dev) |
| 440 | return -EINVAL; |
| 441 | |
| 442 | if (!strlen(class_dev->class_id)) { |
| 443 | error = -EINVAL; |
| 444 | goto register_done; |
| 445 | } |
| 446 | |
| 447 | parent = class_get(class_dev->class); |
| 448 | |
| 449 | pr_debug("CLASS: registering class device: ID = '%s'\n", |
| 450 | class_dev->class_id); |
| 451 | |
| 452 | /* first, register with generic layer. */ |
| 453 | kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id); |
| 454 | if (parent) |
| 455 | class_dev->kobj.parent = &parent->subsys.kset.kobj; |
| 456 | |
| 457 | if ((error = kobject_add(&class_dev->kobj))) |
| 458 | goto register_done; |
| 459 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame^] | 460 | /* add the needed attributes to this device */ |
| 461 | if (MAJOR(class_dev->devt)) { |
| 462 | struct class_device_attribute *attr; |
| 463 | attr = kmalloc(sizeof(*attr), GFP_KERNEL); |
| 464 | if (!attr) { |
| 465 | error = -ENOMEM; |
| 466 | kobject_del(&class_dev->kobj); |
| 467 | goto register_done; |
| 468 | } |
| 469 | memset(attr, sizeof(*attr), 0x00); |
| 470 | attr->attr.name = "dev"; |
| 471 | attr->attr.mode = S_IRUGO; |
| 472 | attr->attr.owner = parent->owner; |
| 473 | attr->show = show_dev; |
| 474 | attr->store = NULL; |
| 475 | class_device_create_file(class_dev, attr); |
| 476 | class_dev->devt_attr = attr; |
| 477 | } |
| 478 | |
| 479 | class_device_add_attrs(class_dev); |
| 480 | if (class_dev->dev) |
| 481 | sysfs_create_link(&class_dev->kobj, |
| 482 | &class_dev->dev->kobj, "device"); |
| 483 | |
| 484 | /* notify any interfaces this device is now here */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | if (parent) { |
| 486 | down(&parent->sem); |
| 487 | list_add_tail(&class_dev->node, &parent->children); |
| 488 | list_for_each_entry(class_intf, &parent->interfaces, node) |
| 489 | if (class_intf->add) |
| 490 | class_intf->add(class_dev); |
| 491 | up(&parent->sem); |
| 492 | } |
kay.sievers@vrfy.org | 0700f56 | 2005-04-18 21:57:35 -0700 | [diff] [blame] | 493 | kobject_hotplug(&class_dev->kobj, KOBJ_ADD); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame^] | 494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | register_done: |
| 496 | if (error && parent) |
| 497 | class_put(parent); |
| 498 | class_device_put(class_dev); |
| 499 | return error; |
| 500 | } |
| 501 | |
| 502 | int class_device_register(struct class_device *class_dev) |
| 503 | { |
| 504 | class_device_initialize(class_dev); |
| 505 | return class_device_add(class_dev); |
| 506 | } |
| 507 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame^] | 508 | struct class_device *class_device_create(struct class *cls, dev_t devt, |
| 509 | struct device *device, char *fmt, ...) |
| 510 | { |
| 511 | va_list args; |
| 512 | struct class_device *class_dev = NULL; |
| 513 | int retval = -ENODEV; |
| 514 | |
| 515 | if (cls == NULL || IS_ERR(cls)) |
| 516 | goto error; |
| 517 | |
| 518 | class_dev = kmalloc(sizeof(struct class_device), GFP_KERNEL); |
| 519 | if (!class_dev) { |
| 520 | retval = -ENOMEM; |
| 521 | goto error; |
| 522 | } |
| 523 | memset(class_dev, 0x00, sizeof(struct class_device)); |
| 524 | |
| 525 | class_dev->devt = devt; |
| 526 | class_dev->dev = device; |
| 527 | class_dev->class = cls; |
| 528 | |
| 529 | va_start(args, fmt); |
| 530 | vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args); |
| 531 | va_end(args); |
| 532 | retval = class_device_register(class_dev); |
| 533 | if (retval) |
| 534 | goto error; |
| 535 | |
| 536 | return class_dev; |
| 537 | |
| 538 | error: |
| 539 | kfree(class_dev); |
| 540 | return ERR_PTR(retval); |
| 541 | } |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | void class_device_del(struct class_device *class_dev) |
| 544 | { |
| 545 | struct class * parent = class_dev->class; |
| 546 | struct class_interface * class_intf; |
| 547 | |
| 548 | if (parent) { |
| 549 | down(&parent->sem); |
| 550 | list_del_init(&class_dev->node); |
| 551 | list_for_each_entry(class_intf, &parent->interfaces, node) |
| 552 | if (class_intf->remove) |
| 553 | class_intf->remove(class_dev); |
| 554 | up(&parent->sem); |
| 555 | } |
| 556 | |
| 557 | if (class_dev->dev) |
| 558 | sysfs_remove_link(&class_dev->kobj, "device"); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame^] | 559 | if (class_dev->devt_attr) { |
| 560 | class_device_remove_file(class_dev, class_dev->devt_attr); |
| 561 | kfree(class_dev->devt_attr); |
| 562 | class_dev->devt_attr = NULL; |
| 563 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | class_device_remove_attrs(class_dev); |
| 565 | |
kay.sievers@vrfy.org | 0700f56 | 2005-04-18 21:57:35 -0700 | [diff] [blame] | 566 | kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | kobject_del(&class_dev->kobj); |
| 568 | |
| 569 | if (parent) |
| 570 | class_put(parent); |
| 571 | } |
| 572 | |
| 573 | void class_device_unregister(struct class_device *class_dev) |
| 574 | { |
| 575 | pr_debug("CLASS: Unregistering class device. ID = '%s'\n", |
| 576 | class_dev->class_id); |
| 577 | class_device_del(class_dev); |
| 578 | class_device_put(class_dev); |
| 579 | } |
| 580 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame^] | 581 | void class_device_destroy(struct class *cls, dev_t devt) |
| 582 | { |
| 583 | struct class_device *class_dev = NULL; |
| 584 | struct class_device *class_dev_tmp; |
| 585 | |
| 586 | down(&cls->sem); |
| 587 | list_for_each_entry(class_dev_tmp, &cls->children, node) { |
| 588 | if (class_dev_tmp->devt == devt) { |
| 589 | class_dev = class_dev_tmp; |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | up(&cls->sem); |
| 594 | |
| 595 | if (class_dev) |
| 596 | class_device_unregister(class_dev); |
| 597 | } |
| 598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | int class_device_rename(struct class_device *class_dev, char *new_name) |
| 600 | { |
| 601 | int error = 0; |
| 602 | |
| 603 | class_dev = class_device_get(class_dev); |
| 604 | if (!class_dev) |
| 605 | return -EINVAL; |
| 606 | |
| 607 | pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id, |
| 608 | new_name); |
| 609 | |
| 610 | strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN); |
| 611 | |
| 612 | error = kobject_rename(&class_dev->kobj, new_name); |
| 613 | |
| 614 | class_device_put(class_dev); |
| 615 | |
| 616 | return error; |
| 617 | } |
| 618 | |
| 619 | struct class_device * class_device_get(struct class_device *class_dev) |
| 620 | { |
| 621 | if (class_dev) |
| 622 | return to_class_dev(kobject_get(&class_dev->kobj)); |
| 623 | return NULL; |
| 624 | } |
| 625 | |
| 626 | void class_device_put(struct class_device *class_dev) |
| 627 | { |
| 628 | kobject_put(&class_dev->kobj); |
| 629 | } |
| 630 | |
| 631 | |
| 632 | int class_interface_register(struct class_interface *class_intf) |
| 633 | { |
| 634 | struct class *parent; |
| 635 | struct class_device *class_dev; |
| 636 | |
| 637 | if (!class_intf || !class_intf->class) |
| 638 | return -ENODEV; |
| 639 | |
| 640 | parent = class_get(class_intf->class); |
| 641 | if (!parent) |
| 642 | return -EINVAL; |
| 643 | |
| 644 | down(&parent->sem); |
| 645 | list_add_tail(&class_intf->node, &parent->interfaces); |
| 646 | if (class_intf->add) { |
| 647 | list_for_each_entry(class_dev, &parent->children, node) |
| 648 | class_intf->add(class_dev); |
| 649 | } |
| 650 | up(&parent->sem); |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | void class_interface_unregister(struct class_interface *class_intf) |
| 656 | { |
| 657 | struct class * parent = class_intf->class; |
| 658 | struct class_device *class_dev; |
| 659 | |
| 660 | if (!parent) |
| 661 | return; |
| 662 | |
| 663 | down(&parent->sem); |
| 664 | list_del_init(&class_intf->node); |
| 665 | if (class_intf->remove) { |
| 666 | list_for_each_entry(class_dev, &parent->children, node) |
| 667 | class_intf->remove(class_dev); |
| 668 | } |
| 669 | up(&parent->sem); |
| 670 | |
| 671 | class_put(parent); |
| 672 | } |
| 673 | |
| 674 | |
| 675 | |
| 676 | int __init classes_init(void) |
| 677 | { |
| 678 | int retval; |
| 679 | |
| 680 | retval = subsystem_register(&class_subsys); |
| 681 | if (retval) |
| 682 | return retval; |
| 683 | |
| 684 | /* ick, this is ugly, the things we go through to keep from showing up |
| 685 | * in sysfs... */ |
| 686 | subsystem_init(&class_obj_subsys); |
| 687 | if (!class_obj_subsys.kset.subsys) |
| 688 | class_obj_subsys.kset.subsys = &class_obj_subsys; |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | EXPORT_SYMBOL_GPL(class_create_file); |
| 693 | EXPORT_SYMBOL_GPL(class_remove_file); |
| 694 | EXPORT_SYMBOL_GPL(class_register); |
| 695 | EXPORT_SYMBOL_GPL(class_unregister); |
| 696 | EXPORT_SYMBOL_GPL(class_get); |
| 697 | EXPORT_SYMBOL_GPL(class_put); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame^] | 698 | EXPORT_SYMBOL_GPL(class_create); |
| 699 | EXPORT_SYMBOL_GPL(class_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | EXPORT_SYMBOL_GPL(class_device_register); |
| 702 | EXPORT_SYMBOL_GPL(class_device_unregister); |
| 703 | EXPORT_SYMBOL_GPL(class_device_initialize); |
| 704 | EXPORT_SYMBOL_GPL(class_device_add); |
| 705 | EXPORT_SYMBOL_GPL(class_device_del); |
| 706 | EXPORT_SYMBOL_GPL(class_device_get); |
| 707 | EXPORT_SYMBOL_GPL(class_device_put); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame^] | 708 | EXPORT_SYMBOL_GPL(class_device_create); |
| 709 | EXPORT_SYMBOL_GPL(class_device_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | EXPORT_SYMBOL_GPL(class_device_create_file); |
| 711 | EXPORT_SYMBOL_GPL(class_device_remove_file); |
| 712 | EXPORT_SYMBOL_GPL(class_device_create_bin_file); |
| 713 | EXPORT_SYMBOL_GPL(class_device_remove_bin_file); |
| 714 | |
| 715 | EXPORT_SYMBOL_GPL(class_interface_register); |
| 716 | EXPORT_SYMBOL_GPL(class_interface_unregister); |