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