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 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 176 | /** |
| 177 | * class_create - create a struct class structure |
| 178 | * @owner: pointer to the module that is to "own" this struct class |
| 179 | * @name: pointer to a string for the name of this class. |
| 180 | * |
| 181 | * This is used to create a struct class pointer that can then be used |
| 182 | * in calls to class_device_create(). |
| 183 | * |
| 184 | * Note, the pointer created here is to be destroyed when finished by |
| 185 | * making a call to class_destroy(). |
| 186 | */ |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 187 | struct class *class_create(struct module *owner, char *name) |
| 188 | { |
| 189 | struct class *cls; |
| 190 | int retval; |
| 191 | |
| 192 | cls = kmalloc(sizeof(struct class), GFP_KERNEL); |
| 193 | if (!cls) { |
| 194 | retval = -ENOMEM; |
| 195 | goto error; |
| 196 | } |
| 197 | memset(cls, 0x00, sizeof(struct class)); |
| 198 | |
| 199 | cls->name = name; |
| 200 | cls->owner = owner; |
| 201 | cls->class_release = class_create_release; |
| 202 | cls->release = class_device_create_release; |
| 203 | |
| 204 | retval = class_register(cls); |
| 205 | if (retval) |
| 206 | goto error; |
| 207 | |
| 208 | return cls; |
| 209 | |
| 210 | error: |
| 211 | kfree(cls); |
| 212 | return ERR_PTR(retval); |
| 213 | } |
| 214 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 215 | /** |
| 216 | * class_destroy - destroys a struct class structure |
| 217 | * @cs: pointer to the struct class that is to be destroyed |
| 218 | * |
| 219 | * Note, the pointer to be destroyed must have been created with a call |
| 220 | * to class_create(). |
| 221 | */ |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 222 | void class_destroy(struct class *cls) |
| 223 | { |
| 224 | if ((cls == NULL) || (IS_ERR(cls))) |
| 225 | return; |
| 226 | |
| 227 | class_unregister(cls); |
| 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | /* Class Device Stuff */ |
| 231 | |
| 232 | int class_device_create_file(struct class_device * class_dev, |
| 233 | const struct class_device_attribute * attr) |
| 234 | { |
| 235 | int error = -EINVAL; |
| 236 | if (class_dev) |
| 237 | error = sysfs_create_file(&class_dev->kobj, &attr->attr); |
| 238 | return error; |
| 239 | } |
| 240 | |
| 241 | void class_device_remove_file(struct class_device * class_dev, |
| 242 | const struct class_device_attribute * attr) |
| 243 | { |
| 244 | if (class_dev) |
| 245 | sysfs_remove_file(&class_dev->kobj, &attr->attr); |
| 246 | } |
| 247 | |
| 248 | int class_device_create_bin_file(struct class_device *class_dev, |
| 249 | struct bin_attribute *attr) |
| 250 | { |
| 251 | int error = -EINVAL; |
| 252 | if (class_dev) |
| 253 | error = sysfs_create_bin_file(&class_dev->kobj, attr); |
| 254 | return error; |
| 255 | } |
| 256 | |
| 257 | void class_device_remove_bin_file(struct class_device *class_dev, |
| 258 | struct bin_attribute *attr) |
| 259 | { |
| 260 | if (class_dev) |
| 261 | sysfs_remove_bin_file(&class_dev->kobj, attr); |
| 262 | } |
| 263 | |
| 264 | static ssize_t |
| 265 | class_device_attr_show(struct kobject * kobj, struct attribute * attr, |
| 266 | char * buf) |
| 267 | { |
| 268 | struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); |
| 269 | struct class_device * cd = to_class_dev(kobj); |
| 270 | ssize_t ret = 0; |
| 271 | |
| 272 | if (class_dev_attr->show) |
| 273 | ret = class_dev_attr->show(cd, buf); |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | static ssize_t |
| 278 | class_device_attr_store(struct kobject * kobj, struct attribute * attr, |
| 279 | const char * buf, size_t count) |
| 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->store) |
| 286 | ret = class_dev_attr->store(cd, buf, count); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static struct sysfs_ops class_dev_sysfs_ops = { |
| 291 | .show = class_device_attr_show, |
| 292 | .store = class_device_attr_store, |
| 293 | }; |
| 294 | |
| 295 | static void class_dev_release(struct kobject * kobj) |
| 296 | { |
| 297 | struct class_device *cd = to_class_dev(kobj); |
| 298 | struct class * cls = cd->class; |
| 299 | |
| 300 | pr_debug("device class '%s': release.\n", cd->class_id); |
| 301 | |
Maneesh Soni | 208f3d6 | 2005-08-16 15:15:48 -0700 | [diff] [blame] | 302 | if (cd->devt_attr) { |
| 303 | kfree(cd->devt_attr); |
| 304 | cd->devt_attr = NULL; |
| 305 | } |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | if (cls->release) |
| 308 | cls->release(cd); |
| 309 | else { |
| 310 | printk(KERN_ERR "Device class '%s' does not have a release() function, " |
| 311 | "it is broken and must be fixed.\n", |
| 312 | cd->class_id); |
| 313 | WARN_ON(1); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | static struct kobj_type ktype_class_device = { |
| 318 | .sysfs_ops = &class_dev_sysfs_ops, |
| 319 | .release = class_dev_release, |
| 320 | }; |
| 321 | |
| 322 | static int class_hotplug_filter(struct kset *kset, struct kobject *kobj) |
| 323 | { |
| 324 | struct kobj_type *ktype = get_ktype(kobj); |
| 325 | |
| 326 | if (ktype == &ktype_class_device) { |
| 327 | struct class_device *class_dev = to_class_dev(kobj); |
| 328 | if (class_dev->class) |
| 329 | return 1; |
| 330 | } |
| 331 | return 0; |
| 332 | } |
| 333 | |
Dmitry Torokhov | 419cab3 | 2005-04-26 02:32:54 -0500 | [diff] [blame] | 334 | static const char *class_hotplug_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
| 336 | struct class_device *class_dev = to_class_dev(kobj); |
| 337 | |
| 338 | return class_dev->class->name; |
| 339 | } |
| 340 | |
| 341 | static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp, |
| 342 | int num_envp, char *buffer, int buffer_size) |
| 343 | { |
| 344 | struct class_device *class_dev = to_class_dev(kobj); |
| 345 | int i = 0; |
| 346 | int length = 0; |
| 347 | int retval = 0; |
| 348 | |
| 349 | pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id); |
| 350 | |
| 351 | if (class_dev->dev) { |
| 352 | /* add physical device, backing this device */ |
| 353 | struct device *dev = class_dev->dev; |
| 354 | char *path = kobject_get_path(&dev->kobj, GFP_KERNEL); |
| 355 | |
| 356 | add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, |
| 357 | &length, "PHYSDEVPATH=%s", path); |
| 358 | kfree(path); |
| 359 | |
| 360 | if (dev->bus) |
| 361 | add_hotplug_env_var(envp, num_envp, &i, |
| 362 | buffer, buffer_size, &length, |
| 363 | "PHYSDEVBUS=%s", dev->bus->name); |
| 364 | |
| 365 | if (dev->driver) |
| 366 | add_hotplug_env_var(envp, num_envp, &i, |
| 367 | buffer, buffer_size, &length, |
| 368 | "PHYSDEVDRIVER=%s", dev->driver->name); |
| 369 | } |
| 370 | |
| 371 | if (MAJOR(class_dev->devt)) { |
| 372 | add_hotplug_env_var(envp, num_envp, &i, |
| 373 | buffer, buffer_size, &length, |
| 374 | "MAJOR=%u", MAJOR(class_dev->devt)); |
| 375 | |
| 376 | add_hotplug_env_var(envp, num_envp, &i, |
| 377 | buffer, buffer_size, &length, |
| 378 | "MINOR=%u", MINOR(class_dev->devt)); |
| 379 | } |
| 380 | |
| 381 | /* terminate, set to next free slot, shrink available space */ |
| 382 | envp[i] = NULL; |
| 383 | envp = &envp[i]; |
| 384 | num_envp -= i; |
| 385 | buffer = &buffer[length]; |
| 386 | buffer_size -= length; |
| 387 | |
| 388 | if (class_dev->class->hotplug) { |
| 389 | /* have the bus specific function add its stuff */ |
| 390 | retval = class_dev->class->hotplug (class_dev, envp, num_envp, |
| 391 | buffer, buffer_size); |
| 392 | if (retval) { |
| 393 | pr_debug ("%s - hotplug() returned %d\n", |
| 394 | __FUNCTION__, retval); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return retval; |
| 399 | } |
| 400 | |
| 401 | static struct kset_hotplug_ops class_hotplug_ops = { |
| 402 | .filter = class_hotplug_filter, |
| 403 | .name = class_hotplug_name, |
| 404 | .hotplug = class_hotplug, |
| 405 | }; |
| 406 | |
| 407 | static decl_subsys(class_obj, &ktype_class_device, &class_hotplug_ops); |
| 408 | |
| 409 | |
| 410 | static int class_device_add_attrs(struct class_device * cd) |
| 411 | { |
| 412 | int i; |
| 413 | int error = 0; |
| 414 | struct class * cls = cd->class; |
| 415 | |
| 416 | if (cls->class_dev_attrs) { |
| 417 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) { |
| 418 | error = class_device_create_file(cd, |
| 419 | &cls->class_dev_attrs[i]); |
| 420 | if (error) |
| 421 | goto Err; |
| 422 | } |
| 423 | } |
| 424 | Done: |
| 425 | return error; |
| 426 | Err: |
| 427 | while (--i >= 0) |
| 428 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); |
| 429 | goto Done; |
| 430 | } |
| 431 | |
| 432 | static void class_device_remove_attrs(struct class_device * cd) |
| 433 | { |
| 434 | int i; |
| 435 | struct class * cls = cd->class; |
| 436 | |
| 437 | if (cls->class_dev_attrs) { |
| 438 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) |
| 439 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | static ssize_t show_dev(struct class_device *class_dev, char *buf) |
| 444 | { |
| 445 | return print_dev_t(buf, class_dev->devt); |
| 446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
| 448 | void class_device_initialize(struct class_device *class_dev) |
| 449 | { |
| 450 | kobj_set_kset_s(class_dev, class_obj_subsys); |
| 451 | kobject_init(&class_dev->kobj); |
| 452 | INIT_LIST_HEAD(&class_dev->node); |
| 453 | } |
| 454 | |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 455 | static char *make_class_name(struct class_device *class_dev) |
| 456 | { |
| 457 | char *name; |
| 458 | int size; |
| 459 | |
| 460 | size = strlen(class_dev->class->name) + |
| 461 | strlen(kobject_name(&class_dev->kobj)) + 2; |
| 462 | |
| 463 | name = kmalloc(size, GFP_KERNEL); |
| 464 | if (!name) |
| 465 | return ERR_PTR(-ENOMEM); |
| 466 | |
| 467 | strcpy(name, class_dev->class->name); |
| 468 | strcat(name, ":"); |
| 469 | strcat(name, kobject_name(&class_dev->kobj)); |
| 470 | return name; |
| 471 | } |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | int class_device_add(struct class_device *class_dev) |
| 474 | { |
| 475 | struct class * parent = NULL; |
| 476 | struct class_interface * class_intf; |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 477 | char *class_name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | int error; |
| 479 | |
| 480 | class_dev = class_device_get(class_dev); |
| 481 | if (!class_dev) |
| 482 | return -EINVAL; |
| 483 | |
| 484 | if (!strlen(class_dev->class_id)) { |
| 485 | error = -EINVAL; |
| 486 | goto register_done; |
| 487 | } |
| 488 | |
| 489 | parent = class_get(class_dev->class); |
| 490 | |
| 491 | pr_debug("CLASS: registering class device: ID = '%s'\n", |
| 492 | class_dev->class_id); |
| 493 | |
| 494 | /* first, register with generic layer. */ |
| 495 | kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id); |
| 496 | if (parent) |
| 497 | class_dev->kobj.parent = &parent->subsys.kset.kobj; |
| 498 | |
| 499 | if ((error = kobject_add(&class_dev->kobj))) |
| 500 | goto register_done; |
| 501 | |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 502 | /* add the needed attributes to this device */ |
| 503 | if (MAJOR(class_dev->devt)) { |
| 504 | struct class_device_attribute *attr; |
| 505 | attr = kmalloc(sizeof(*attr), GFP_KERNEL); |
| 506 | if (!attr) { |
| 507 | error = -ENOMEM; |
| 508 | kobject_del(&class_dev->kobj); |
| 509 | goto register_done; |
| 510 | } |
| 511 | memset(attr, sizeof(*attr), 0x00); |
| 512 | attr->attr.name = "dev"; |
| 513 | attr->attr.mode = S_IRUGO; |
| 514 | attr->attr.owner = parent->owner; |
| 515 | attr->show = show_dev; |
| 516 | attr->store = NULL; |
| 517 | class_device_create_file(class_dev, attr); |
| 518 | class_dev->devt_attr = attr; |
| 519 | } |
| 520 | |
| 521 | class_device_add_attrs(class_dev); |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 522 | if (class_dev->dev) { |
| 523 | class_name = make_class_name(class_dev); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 524 | sysfs_create_link(&class_dev->kobj, |
| 525 | &class_dev->dev->kobj, "device"); |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 526 | sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj, |
| 527 | class_name); |
| 528 | } |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 529 | |
| 530 | /* notify any interfaces this device is now here */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (parent) { |
| 532 | down(&parent->sem); |
| 533 | list_add_tail(&class_dev->node, &parent->children); |
| 534 | list_for_each_entry(class_intf, &parent->interfaces, node) |
| 535 | if (class_intf->add) |
| 536 | class_intf->add(class_dev); |
| 537 | up(&parent->sem); |
| 538 | } |
kay.sievers@vrfy.org | 0700f56 | 2005-04-18 21:57:35 -0700 | [diff] [blame] | 539 | kobject_hotplug(&class_dev->kobj, KOBJ_ADD); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | register_done: |
| 542 | if (error && parent) |
| 543 | class_put(parent); |
| 544 | class_device_put(class_dev); |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 545 | kfree(class_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | return error; |
| 547 | } |
| 548 | |
| 549 | int class_device_register(struct class_device *class_dev) |
| 550 | { |
| 551 | class_device_initialize(class_dev); |
| 552 | return class_device_add(class_dev); |
| 553 | } |
| 554 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 555 | /** |
| 556 | * class_device_create - creates a class device and registers it with sysfs |
| 557 | * @cs: pointer to the struct class that this device should be registered to. |
| 558 | * @dev: the dev_t for the char device to be added. |
| 559 | * @device: a pointer to a struct device that is assiociated with this class device. |
| 560 | * @fmt: string for the class device's name |
| 561 | * |
| 562 | * This function can be used by char device classes. A struct |
| 563 | * class_device will be created in sysfs, registered to the specified |
| 564 | * class. A "dev" file will be created, showing the dev_t for the |
| 565 | * device. The pointer to the struct class_device will be returned from |
| 566 | * the call. Any further sysfs files that might be required can be |
| 567 | * created using this pointer. |
| 568 | * |
| 569 | * Note: the struct class passed to this function must have previously |
| 570 | * been created with a call to class_create(). |
| 571 | */ |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 572 | struct class_device *class_device_create(struct class *cls, dev_t devt, |
| 573 | struct device *device, char *fmt, ...) |
| 574 | { |
| 575 | va_list args; |
| 576 | struct class_device *class_dev = NULL; |
| 577 | int retval = -ENODEV; |
| 578 | |
| 579 | if (cls == NULL || IS_ERR(cls)) |
| 580 | goto error; |
| 581 | |
| 582 | class_dev = kmalloc(sizeof(struct class_device), GFP_KERNEL); |
| 583 | if (!class_dev) { |
| 584 | retval = -ENOMEM; |
| 585 | goto error; |
| 586 | } |
| 587 | memset(class_dev, 0x00, sizeof(struct class_device)); |
| 588 | |
| 589 | class_dev->devt = devt; |
| 590 | class_dev->dev = device; |
| 591 | class_dev->class = cls; |
| 592 | |
| 593 | va_start(args, fmt); |
| 594 | vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args); |
| 595 | va_end(args); |
| 596 | retval = class_device_register(class_dev); |
| 597 | if (retval) |
| 598 | goto error; |
| 599 | |
| 600 | return class_dev; |
| 601 | |
| 602 | error: |
| 603 | kfree(class_dev); |
| 604 | return ERR_PTR(retval); |
| 605 | } |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | void class_device_del(struct class_device *class_dev) |
| 608 | { |
| 609 | struct class * parent = class_dev->class; |
| 610 | struct class_interface * class_intf; |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 611 | char *class_name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | if (parent) { |
| 614 | down(&parent->sem); |
| 615 | list_del_init(&class_dev->node); |
| 616 | list_for_each_entry(class_intf, &parent->interfaces, node) |
| 617 | if (class_intf->remove) |
| 618 | class_intf->remove(class_dev); |
| 619 | up(&parent->sem); |
| 620 | } |
| 621 | |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 622 | if (class_dev->dev) { |
| 623 | class_name = make_class_name(class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | sysfs_remove_link(&class_dev->kobj, "device"); |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 625 | sysfs_remove_link(&class_dev->dev->kobj, class_name); |
| 626 | } |
Maneesh Soni | 208f3d6 | 2005-08-16 15:15:48 -0700 | [diff] [blame] | 627 | if (class_dev->devt_attr) |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 628 | class_device_remove_file(class_dev, class_dev->devt_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | class_device_remove_attrs(class_dev); |
| 630 | |
kay.sievers@vrfy.org | 0700f56 | 2005-04-18 21:57:35 -0700 | [diff] [blame] | 631 | kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | kobject_del(&class_dev->kobj); |
| 633 | |
| 634 | if (parent) |
| 635 | class_put(parent); |
Dmitry Torokhov | 76d1ce0 | 2005-07-10 01:21:24 -0500 | [diff] [blame] | 636 | kfree(class_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | void class_device_unregister(struct class_device *class_dev) |
| 640 | { |
| 641 | pr_debug("CLASS: Unregistering class device. ID = '%s'\n", |
| 642 | class_dev->class_id); |
| 643 | class_device_del(class_dev); |
| 644 | class_device_put(class_dev); |
| 645 | } |
| 646 | |
gregkh@suse.de | 2fc6844 | 2005-03-23 10:02:56 -0800 | [diff] [blame] | 647 | /** |
| 648 | * class_device_destroy - removes a class device that was created with class_device_create() |
| 649 | * @cls: the pointer to the struct class that this device was registered * with. |
| 650 | * @dev: the dev_t of the device that was previously registered. |
| 651 | * |
| 652 | * This call unregisters and cleans up a class device that was created with a |
| 653 | * call to class_device_create() |
| 654 | */ |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 655 | void class_device_destroy(struct class *cls, dev_t devt) |
| 656 | { |
| 657 | struct class_device *class_dev = NULL; |
| 658 | struct class_device *class_dev_tmp; |
| 659 | |
| 660 | down(&cls->sem); |
| 661 | list_for_each_entry(class_dev_tmp, &cls->children, node) { |
| 662 | if (class_dev_tmp->devt == devt) { |
| 663 | class_dev = class_dev_tmp; |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | up(&cls->sem); |
| 668 | |
| 669 | if (class_dev) |
| 670 | class_device_unregister(class_dev); |
| 671 | } |
| 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | int class_device_rename(struct class_device *class_dev, char *new_name) |
| 674 | { |
| 675 | int error = 0; |
| 676 | |
| 677 | class_dev = class_device_get(class_dev); |
| 678 | if (!class_dev) |
| 679 | return -EINVAL; |
| 680 | |
| 681 | pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id, |
| 682 | new_name); |
| 683 | |
| 684 | strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN); |
| 685 | |
| 686 | error = kobject_rename(&class_dev->kobj, new_name); |
| 687 | |
| 688 | class_device_put(class_dev); |
| 689 | |
| 690 | return error; |
| 691 | } |
| 692 | |
| 693 | struct class_device * class_device_get(struct class_device *class_dev) |
| 694 | { |
| 695 | if (class_dev) |
| 696 | return to_class_dev(kobject_get(&class_dev->kobj)); |
| 697 | return NULL; |
| 698 | } |
| 699 | |
| 700 | void class_device_put(struct class_device *class_dev) |
| 701 | { |
| 702 | kobject_put(&class_dev->kobj); |
| 703 | } |
| 704 | |
| 705 | |
| 706 | int class_interface_register(struct class_interface *class_intf) |
| 707 | { |
| 708 | struct class *parent; |
| 709 | struct class_device *class_dev; |
| 710 | |
| 711 | if (!class_intf || !class_intf->class) |
| 712 | return -ENODEV; |
| 713 | |
| 714 | parent = class_get(class_intf->class); |
| 715 | if (!parent) |
| 716 | return -EINVAL; |
| 717 | |
| 718 | down(&parent->sem); |
| 719 | list_add_tail(&class_intf->node, &parent->interfaces); |
| 720 | if (class_intf->add) { |
| 721 | list_for_each_entry(class_dev, &parent->children, node) |
| 722 | class_intf->add(class_dev); |
| 723 | } |
| 724 | up(&parent->sem); |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | void class_interface_unregister(struct class_interface *class_intf) |
| 730 | { |
| 731 | struct class * parent = class_intf->class; |
| 732 | struct class_device *class_dev; |
| 733 | |
| 734 | if (!parent) |
| 735 | return; |
| 736 | |
| 737 | down(&parent->sem); |
| 738 | list_del_init(&class_intf->node); |
| 739 | if (class_intf->remove) { |
| 740 | list_for_each_entry(class_dev, &parent->children, node) |
| 741 | class_intf->remove(class_dev); |
| 742 | } |
| 743 | up(&parent->sem); |
| 744 | |
| 745 | class_put(parent); |
| 746 | } |
| 747 | |
| 748 | |
| 749 | |
| 750 | int __init classes_init(void) |
| 751 | { |
| 752 | int retval; |
| 753 | |
| 754 | retval = subsystem_register(&class_subsys); |
| 755 | if (retval) |
| 756 | return retval; |
| 757 | |
| 758 | /* ick, this is ugly, the things we go through to keep from showing up |
| 759 | * in sysfs... */ |
| 760 | subsystem_init(&class_obj_subsys); |
| 761 | if (!class_obj_subsys.kset.subsys) |
| 762 | class_obj_subsys.kset.subsys = &class_obj_subsys; |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | EXPORT_SYMBOL_GPL(class_create_file); |
| 767 | EXPORT_SYMBOL_GPL(class_remove_file); |
| 768 | EXPORT_SYMBOL_GPL(class_register); |
| 769 | EXPORT_SYMBOL_GPL(class_unregister); |
| 770 | EXPORT_SYMBOL_GPL(class_get); |
| 771 | EXPORT_SYMBOL_GPL(class_put); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 772 | EXPORT_SYMBOL_GPL(class_create); |
| 773 | EXPORT_SYMBOL_GPL(class_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | EXPORT_SYMBOL_GPL(class_device_register); |
| 776 | EXPORT_SYMBOL_GPL(class_device_unregister); |
| 777 | EXPORT_SYMBOL_GPL(class_device_initialize); |
| 778 | EXPORT_SYMBOL_GPL(class_device_add); |
| 779 | EXPORT_SYMBOL_GPL(class_device_del); |
| 780 | EXPORT_SYMBOL_GPL(class_device_get); |
| 781 | EXPORT_SYMBOL_GPL(class_device_put); |
gregkh@suse.de | e9ba636 | 2005-03-15 11:54:21 -0800 | [diff] [blame] | 782 | EXPORT_SYMBOL_GPL(class_device_create); |
| 783 | EXPORT_SYMBOL_GPL(class_device_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | EXPORT_SYMBOL_GPL(class_device_create_file); |
| 785 | EXPORT_SYMBOL_GPL(class_device_remove_file); |
| 786 | EXPORT_SYMBOL_GPL(class_device_create_bin_file); |
| 787 | EXPORT_SYMBOL_GPL(class_device_remove_bin_file); |
| 788 | |
| 789 | EXPORT_SYMBOL_GPL(class_interface_register); |
| 790 | EXPORT_SYMBOL_GPL(class_interface_unregister); |