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