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