Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/core.c - core driver model code (device registration, etc) |
| 3 | * |
| 4 | * Copyright (c) 2002-3 Patrick Mochel |
| 5 | * Copyright (c) 2002-3 Open Source Development Labs |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 6 | * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> |
| 7 | * Copyright (c) 2006 Novell, Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/string.h> |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 19 | #include <linux/kdev_t.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/semaphore.h> |
| 22 | |
| 23 | #include "base.h" |
| 24 | #include "power/power.h" |
| 25 | |
| 26 | int (*platform_notify)(struct device * dev) = NULL; |
| 27 | int (*platform_notify_remove)(struct device * dev) = NULL; |
| 28 | |
| 29 | /* |
| 30 | * sysfs bindings for devices. |
| 31 | */ |
| 32 | |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 33 | /** |
| 34 | * dev_driver_string - Return a device's driver name, if at all possible |
| 35 | * @dev: struct device to get the name of |
| 36 | * |
| 37 | * Will return the device's driver's name if it is bound to a device. If |
| 38 | * the device is not bound to a device, it will return the name of the bus |
| 39 | * it is attached to. If it is not attached to a bus either, an empty |
| 40 | * string will be returned. |
| 41 | */ |
| 42 | const char *dev_driver_string(struct device *dev) |
| 43 | { |
| 44 | return dev->driver ? dev->driver->name : |
| 45 | (dev->bus ? dev->bus->name : ""); |
| 46 | } |
Matthew Wilcox | 310a922 | 2006-09-23 23:35:04 -0600 | [diff] [blame] | 47 | EXPORT_SYMBOL(dev_driver_string); |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #define to_dev(obj) container_of(obj, struct device, kobj) |
| 50 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static ssize_t |
| 53 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) |
| 54 | { |
| 55 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 56 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 57 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | if (dev_attr->show) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 60 | ret = dev_attr->show(dev, dev_attr, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | static ssize_t |
| 65 | dev_attr_store(struct kobject * kobj, struct attribute * attr, |
| 66 | const char * buf, size_t count) |
| 67 | { |
| 68 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 69 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 70 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | if (dev_attr->store) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 73 | ret = dev_attr->store(dev, dev_attr, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | static struct sysfs_ops dev_sysfs_ops = { |
| 78 | .show = dev_attr_show, |
| 79 | .store = dev_attr_store, |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * device_release - free device structure. |
| 85 | * @kobj: device's kobject. |
| 86 | * |
| 87 | * This is called once the reference count for the object |
| 88 | * reaches 0. We forward the call to the device's release |
| 89 | * method, which should handle actually freeing the structure. |
| 90 | */ |
| 91 | static void device_release(struct kobject * kobj) |
| 92 | { |
| 93 | struct device * dev = to_dev(kobj); |
| 94 | |
| 95 | if (dev->release) |
| 96 | dev->release(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 97 | else if (dev->class && dev->class->dev_release) |
| 98 | dev->class->dev_release(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | else { |
| 100 | printk(KERN_ERR "Device '%s' does not have a release() function, " |
| 101 | "it is broken and must be fixed.\n", |
| 102 | dev->bus_id); |
| 103 | WARN_ON(1); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static struct kobj_type ktype_device = { |
| 108 | .release = device_release, |
| 109 | .sysfs_ops = &dev_sysfs_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 113 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | struct kobj_type *ktype = get_ktype(kobj); |
| 116 | |
| 117 | if (ktype == &ktype_device) { |
| 118 | struct device *dev = to_dev(kobj); |
| 119 | if (dev->bus) |
| 120 | return 1; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 121 | if (dev->class) |
| 122 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 127 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | struct device *dev = to_dev(kobj); |
| 130 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 131 | if (dev->bus) |
| 132 | return dev->bus->name; |
| 133 | if (dev->class) |
| 134 | return dev->class->name; |
| 135 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 138 | static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | int num_envp, char *buffer, int buffer_size) |
| 140 | { |
| 141 | struct device *dev = to_dev(kobj); |
| 142 | int i = 0; |
| 143 | int length = 0; |
| 144 | int retval = 0; |
| 145 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 146 | /* add the major/minor if present */ |
| 147 | if (MAJOR(dev->devt)) { |
| 148 | add_uevent_var(envp, num_envp, &i, |
| 149 | buffer, buffer_size, &length, |
| 150 | "MAJOR=%u", MAJOR(dev->devt)); |
| 151 | add_uevent_var(envp, num_envp, &i, |
| 152 | buffer, buffer_size, &length, |
| 153 | "MINOR=%u", MINOR(dev->devt)); |
| 154 | } |
| 155 | |
Kay Sievers | d81d9d6 | 2006-08-13 06:17:09 +0200 | [diff] [blame] | 156 | /* add bus name (same as SUBSYSTEM, deprecated) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (dev->bus) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 158 | add_uevent_var(envp, num_envp, &i, |
| 159 | buffer, buffer_size, &length, |
| 160 | "PHYSDEVBUS=%s", dev->bus->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Kay Sievers | d81d9d6 | 2006-08-13 06:17:09 +0200 | [diff] [blame] | 162 | /* add driver name (PHYSDEV* values are deprecated)*/ |
| 163 | if (dev->driver) { |
| 164 | add_uevent_var(envp, num_envp, &i, |
| 165 | buffer, buffer_size, &length, |
| 166 | "DRIVER=%s", dev->driver->name); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 167 | add_uevent_var(envp, num_envp, &i, |
| 168 | buffer, buffer_size, &length, |
| 169 | "PHYSDEVDRIVER=%s", dev->driver->name); |
Kay Sievers | d81d9d6 | 2006-08-13 06:17:09 +0200 | [diff] [blame] | 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | /* terminate, set to next free slot, shrink available space */ |
| 173 | envp[i] = NULL; |
| 174 | envp = &envp[i]; |
| 175 | num_envp -= i; |
| 176 | buffer = &buffer[length]; |
| 177 | buffer_size -= length; |
| 178 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 179 | if (dev->bus && dev->bus->uevent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* have the bus specific function add its stuff */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 181 | retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | if (retval) { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 183 | pr_debug ("%s - uevent() returned %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | __FUNCTION__, retval); |
| 185 | } |
| 186 | } |
| 187 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 188 | if (dev->class && dev->class->dev_uevent) { |
| 189 | /* have the class specific function add its stuff */ |
| 190 | retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); |
| 191 | if (retval) { |
| 192 | pr_debug("%s - dev_uevent() returned %d\n", |
| 193 | __FUNCTION__, retval); |
| 194 | } |
| 195 | } |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return retval; |
| 198 | } |
| 199 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 200 | static struct kset_uevent_ops device_uevent_ops = { |
| 201 | .filter = dev_uevent_filter, |
| 202 | .name = dev_uevent_name, |
| 203 | .uevent = dev_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 206 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
| 207 | const char *buf, size_t count) |
| 208 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 209 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 210 | return count; |
| 211 | } |
| 212 | |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 213 | static int device_add_groups(struct device *dev) |
| 214 | { |
| 215 | int i; |
| 216 | int error = 0; |
| 217 | |
| 218 | if (dev->groups) { |
| 219 | for (i = 0; dev->groups[i]; i++) { |
| 220 | error = sysfs_create_group(&dev->kobj, dev->groups[i]); |
| 221 | if (error) { |
| 222 | while (--i >= 0) |
| 223 | sysfs_remove_group(&dev->kobj, dev->groups[i]); |
| 224 | goto out; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | out: |
| 229 | return error; |
| 230 | } |
| 231 | |
| 232 | static void device_remove_groups(struct device *dev) |
| 233 | { |
| 234 | int i; |
| 235 | if (dev->groups) { |
| 236 | for (i = 0; dev->groups[i]; i++) { |
| 237 | sysfs_remove_group(&dev->kobj, dev->groups[i]); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 242 | static int device_add_attrs(struct device *dev) |
| 243 | { |
| 244 | struct class *class = dev->class; |
| 245 | int error = 0; |
| 246 | int i; |
| 247 | |
| 248 | if (!class) |
| 249 | return 0; |
| 250 | |
| 251 | if (class->dev_attrs) { |
| 252 | for (i = 0; attr_name(class->dev_attrs[i]); i++) { |
| 253 | error = device_create_file(dev, &class->dev_attrs[i]); |
| 254 | if (error) |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | if (error) |
| 259 | while (--i >= 0) |
| 260 | device_remove_file(dev, &class->dev_attrs[i]); |
| 261 | return error; |
| 262 | } |
| 263 | |
| 264 | static void device_remove_attrs(struct device *dev) |
| 265 | { |
| 266 | struct class *class = dev->class; |
| 267 | int i; |
| 268 | |
| 269 | if (!class) |
| 270 | return; |
| 271 | |
| 272 | if (class->dev_attrs) { |
| 273 | for (i = 0; attr_name(class->dev_attrs[i]); i++) |
| 274 | device_remove_file(dev, &class->dev_attrs[i]); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 279 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
| 280 | char *buf) |
| 281 | { |
| 282 | return print_dev_t(buf, dev->devt); |
| 283 | } |
| 284 | |
Martin Waitz | 0863afb | 2006-01-09 20:53:55 -0800 | [diff] [blame] | 285 | /* |
| 286 | * devices_subsys - structure to be registered with kobject core. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | */ |
| 288 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 289 | decl_subsys(devices, &ktype_device, &device_uevent_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | |
| 292 | /** |
| 293 | * device_create_file - create sysfs attribute file for device. |
| 294 | * @dev: device. |
| 295 | * @attr: device attribute descriptor. |
| 296 | */ |
| 297 | |
| 298 | int device_create_file(struct device * dev, struct device_attribute * attr) |
| 299 | { |
| 300 | int error = 0; |
| 301 | if (get_device(dev)) { |
| 302 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
| 303 | put_device(dev); |
| 304 | } |
| 305 | return error; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * device_remove_file - remove sysfs attribute file. |
| 310 | * @dev: device. |
| 311 | * @attr: device attribute descriptor. |
| 312 | */ |
| 313 | |
| 314 | void device_remove_file(struct device * dev, struct device_attribute * attr) |
| 315 | { |
| 316 | if (get_device(dev)) { |
| 317 | sysfs_remove_file(&dev->kobj, &attr->attr); |
| 318 | put_device(dev); |
| 319 | } |
| 320 | } |
| 321 | |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 322 | /** |
| 323 | * device_create_bin_file - create sysfs binary attribute file for device. |
| 324 | * @dev: device. |
| 325 | * @attr: device binary attribute descriptor. |
| 326 | */ |
| 327 | int device_create_bin_file(struct device *dev, struct bin_attribute *attr) |
| 328 | { |
| 329 | int error = -EINVAL; |
| 330 | if (dev) |
| 331 | error = sysfs_create_bin_file(&dev->kobj, attr); |
| 332 | return error; |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(device_create_bin_file); |
| 335 | |
| 336 | /** |
| 337 | * device_remove_bin_file - remove sysfs binary attribute file |
| 338 | * @dev: device. |
| 339 | * @attr: device binary attribute descriptor. |
| 340 | */ |
| 341 | void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) |
| 342 | { |
| 343 | if (dev) |
| 344 | sysfs_remove_bin_file(&dev->kobj, attr); |
| 345 | } |
| 346 | EXPORT_SYMBOL_GPL(device_remove_bin_file); |
| 347 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 348 | static void klist_children_get(struct klist_node *n) |
| 349 | { |
| 350 | struct device *dev = container_of(n, struct device, knode_parent); |
| 351 | |
| 352 | get_device(dev); |
| 353 | } |
| 354 | |
| 355 | static void klist_children_put(struct klist_node *n) |
| 356 | { |
| 357 | struct device *dev = container_of(n, struct device, knode_parent); |
| 358 | |
| 359 | put_device(dev); |
| 360 | } |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * device_initialize - init device structure. |
| 365 | * @dev: device. |
| 366 | * |
| 367 | * This prepares the device for use by other layers, |
| 368 | * including adding it to the device hierarchy. |
| 369 | * It is the first half of device_register(), if called by |
| 370 | * that, though it can also be called separately, so one |
| 371 | * may use @dev's fields (e.g. the refcount). |
| 372 | */ |
| 373 | |
| 374 | void device_initialize(struct device *dev) |
| 375 | { |
| 376 | kobj_set_kset_s(dev, devices_subsys); |
| 377 | kobject_init(&dev->kobj); |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 378 | klist_init(&dev->klist_children, klist_children_get, |
| 379 | klist_children_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | INIT_LIST_HEAD(&dev->dma_pools); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 381 | INIT_LIST_HEAD(&dev->node); |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 382 | init_MUTEX(&dev->sem); |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 383 | device_init_wakeup(dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
| 387 | * device_add - add device to device hierarchy. |
| 388 | * @dev: device. |
| 389 | * |
| 390 | * This is part 2 of device_register(), though may be called |
| 391 | * separately _iff_ device_initialize() has been called separately. |
| 392 | * |
| 393 | * This adds it to the kobject hierarchy via kobject_add(), adds it |
| 394 | * to the global and sibling lists for the device, then |
| 395 | * adds it to the other relevant subsystems of the driver model. |
| 396 | */ |
| 397 | int device_add(struct device *dev) |
| 398 | { |
| 399 | struct device *parent = NULL; |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 400 | char *class_name = NULL; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 401 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | int error = -EINVAL; |
| 403 | |
| 404 | dev = get_device(dev); |
| 405 | if (!dev || !strlen(dev->bus_id)) |
| 406 | goto Error; |
| 407 | |
Greg Kroah-Hartman | c205ef4 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 408 | /* if this is a class device, and has no parent, create one */ |
| 409 | if ((dev->class) && (dev->parent == NULL)) { |
| 410 | error = virtual_device_parent(dev); |
| 411 | if (error) |
| 412 | goto Error; |
| 413 | } |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | parent = get_device(dev->parent); |
| 416 | |
| 417 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); |
| 418 | |
| 419 | /* first, register with generic layer. */ |
| 420 | kobject_set_name(&dev->kobj, "%s", dev->bus_id); |
| 421 | if (parent) |
| 422 | dev->kobj.parent = &parent->kobj; |
| 423 | |
| 424 | if ((error = kobject_add(&dev->kobj))) |
| 425 | goto Error; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 426 | |
Brian Walsh | 3702264 | 2006-08-14 22:43:19 -0700 | [diff] [blame] | 427 | /* notify platform of device entry */ |
| 428 | if (platform_notify) |
| 429 | platform_notify(dev); |
| 430 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 431 | dev->uevent_attr.attr.name = "uevent"; |
| 432 | dev->uevent_attr.attr.mode = S_IWUSR; |
| 433 | if (dev->driver) |
| 434 | dev->uevent_attr.attr.owner = dev->driver->owner; |
| 435 | dev->uevent_attr.store = store_uevent; |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 436 | error = device_create_file(dev, &dev->uevent_attr); |
| 437 | if (error) |
| 438 | goto attrError; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 439 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 440 | if (MAJOR(dev->devt)) { |
| 441 | struct device_attribute *attr; |
| 442 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
| 443 | if (!attr) { |
| 444 | error = -ENOMEM; |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 445 | goto ueventattrError; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 446 | } |
| 447 | attr->attr.name = "dev"; |
| 448 | attr->attr.mode = S_IRUGO; |
| 449 | if (dev->driver) |
| 450 | attr->attr.owner = dev->driver->owner; |
| 451 | attr->show = show_dev; |
| 452 | error = device_create_file(dev, attr); |
| 453 | if (error) { |
| 454 | kfree(attr); |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 455 | goto ueventattrError; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | dev->devt_attr = attr; |
| 459 | } |
| 460 | |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 461 | if (dev->class) { |
| 462 | sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj, |
| 463 | "subsystem"); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 464 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, |
| 465 | dev->bus_id); |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 466 | if (parent) { |
| 467 | sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device"); |
| 468 | class_name = make_class_name(dev->class->name, &dev->kobj); |
| 469 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name); |
| 470 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 471 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 472 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 473 | if ((error = device_add_attrs(dev))) |
| 474 | goto AttrsError; |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 475 | if ((error = device_add_groups(dev))) |
| 476 | goto GroupError; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | if ((error = device_pm_add(dev))) |
| 478 | goto PMError; |
| 479 | if ((error = bus_add_device(dev))) |
| 480 | goto BusError; |
Kay Sievers | 53877d0 | 2006-04-04 20:42:26 +0200 | [diff] [blame] | 481 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
| 482 | bus_attach_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | if (parent) |
James Bottomley | d856f1e | 2005-08-19 09:14:01 -0400 | [diff] [blame] | 484 | klist_add_tail(&dev->knode_parent, &parent->klist_children); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 486 | if (dev->class) { |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 487 | down(&dev->class->sem); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 488 | /* tie the class to the device */ |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 489 | list_add_tail(&dev->node, &dev->class->devices); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 490 | |
| 491 | /* notify any interfaces that the device is here */ |
| 492 | list_for_each_entry(class_intf, &dev->class->interfaces, node) |
| 493 | if (class_intf->add_dev) |
| 494 | class_intf->add_dev(dev, class_intf); |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 495 | up(&dev->class->sem); |
| 496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | Done: |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 498 | kfree(class_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | put_device(dev); |
| 500 | return error; |
| 501 | BusError: |
| 502 | device_pm_remove(dev); |
| 503 | PMError: |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 504 | device_remove_groups(dev); |
| 505 | GroupError: |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 506 | device_remove_attrs(dev); |
| 507 | AttrsError: |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 508 | if (dev->devt_attr) { |
| 509 | device_remove_file(dev, dev->devt_attr); |
| 510 | kfree(dev->devt_attr); |
| 511 | } |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 512 | ueventattrError: |
| 513 | device_remove_file(dev, &dev->uevent_attr); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 514 | attrError: |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 515 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | kobject_del(&dev->kobj); |
| 517 | Error: |
| 518 | if (parent) |
| 519 | put_device(parent); |
| 520 | goto Done; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /** |
| 525 | * device_register - register a device with the system. |
| 526 | * @dev: pointer to the device structure |
| 527 | * |
| 528 | * This happens in two clean steps - initialize the device |
| 529 | * and add it to the system. The two steps can be called |
| 530 | * separately, but this is the easiest and most common. |
| 531 | * I.e. you should only call the two helpers separately if |
| 532 | * have a clearly defined need to use and refcount the device |
| 533 | * before it is added to the hierarchy. |
| 534 | */ |
| 535 | |
| 536 | int device_register(struct device *dev) |
| 537 | { |
| 538 | device_initialize(dev); |
| 539 | return device_add(dev); |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /** |
| 544 | * get_device - increment reference count for device. |
| 545 | * @dev: device. |
| 546 | * |
| 547 | * This simply forwards the call to kobject_get(), though |
| 548 | * we do take care to provide for the case that we get a NULL |
| 549 | * pointer passed in. |
| 550 | */ |
| 551 | |
| 552 | struct device * get_device(struct device * dev) |
| 553 | { |
| 554 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | /** |
| 559 | * put_device - decrement reference count. |
| 560 | * @dev: device in question. |
| 561 | */ |
| 562 | void put_device(struct device * dev) |
| 563 | { |
| 564 | if (dev) |
| 565 | kobject_put(&dev->kobj); |
| 566 | } |
| 567 | |
| 568 | |
| 569 | /** |
| 570 | * device_del - delete device from system. |
| 571 | * @dev: device. |
| 572 | * |
| 573 | * This is the first part of the device unregistration |
| 574 | * sequence. This removes the device from the lists we control |
| 575 | * from here, has it removed from the other driver model |
| 576 | * subsystems it was added to in device_add(), and removes it |
| 577 | * from the kobject hierarchy. |
| 578 | * |
| 579 | * NOTE: this should be called manually _iff_ device_add() was |
| 580 | * also called manually. |
| 581 | */ |
| 582 | |
| 583 | void device_del(struct device * dev) |
| 584 | { |
| 585 | struct device * parent = dev->parent; |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 586 | char *class_name = NULL; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 587 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | if (parent) |
Patrick Mochel | d62c0f9 | 2005-06-24 08:39:33 -0700 | [diff] [blame] | 590 | klist_del(&dev->knode_parent); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 591 | if (dev->devt_attr) |
| 592 | device_remove_file(dev, dev->devt_attr); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 593 | if (dev->class) { |
| 594 | sysfs_remove_link(&dev->kobj, "subsystem"); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 595 | sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id); |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 596 | class_name = make_class_name(dev->class->name, &dev->kobj); |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 597 | if (parent) { |
| 598 | sysfs_remove_link(&dev->kobj, "device"); |
| 599 | sysfs_remove_link(&dev->parent->kobj, class_name); |
| 600 | } |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 601 | kfree(class_name); |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 602 | down(&dev->class->sem); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 603 | /* notify any interfaces that the device is now gone */ |
| 604 | list_for_each_entry(class_intf, &dev->class->interfaces, node) |
| 605 | if (class_intf->remove_dev) |
| 606 | class_intf->remove_dev(dev, class_intf); |
| 607 | /* remove the device from the class list */ |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 608 | list_del_init(&dev->node); |
| 609 | up(&dev->class->sem); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 610 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 611 | device_remove_file(dev, &dev->uevent_attr); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 612 | device_remove_groups(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 613 | device_remove_attrs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
| 615 | /* Notify the platform of the removal, in case they |
| 616 | * need to do anything... |
| 617 | */ |
| 618 | if (platform_notify_remove) |
| 619 | platform_notify_remove(dev); |
| 620 | bus_remove_device(dev); |
| 621 | device_pm_remove(dev); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 622 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | kobject_del(&dev->kobj); |
| 624 | if (parent) |
| 625 | put_device(parent); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * device_unregister - unregister device from system. |
| 630 | * @dev: device going away. |
| 631 | * |
| 632 | * We do this in two parts, like we do device_register(). First, |
| 633 | * we remove it from all the subsystems with device_del(), then |
| 634 | * we decrement the reference count via put_device(). If that |
| 635 | * is the final reference count, the device will be cleaned up |
| 636 | * via device_release() above. Otherwise, the structure will |
| 637 | * stick around until the final reference to the device is dropped. |
| 638 | */ |
| 639 | void device_unregister(struct device * dev) |
| 640 | { |
| 641 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); |
| 642 | device_del(dev); |
| 643 | put_device(dev); |
| 644 | } |
| 645 | |
| 646 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 647 | static struct device * next_device(struct klist_iter * i) |
| 648 | { |
| 649 | struct klist_node * n = klist_next(i); |
| 650 | return n ? container_of(n, struct device, knode_parent) : NULL; |
| 651 | } |
| 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | /** |
| 654 | * device_for_each_child - device child iterator. |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 655 | * @parent: parent struct device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | * @data: data for the callback. |
| 657 | * @fn: function to be called for each device. |
| 658 | * |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 659 | * Iterate over @parent's child devices, and call @fn for each, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | * passing it @data. |
| 661 | * |
| 662 | * We check the return of @fn each time. If it returns anything |
| 663 | * other than 0, we break out and return that value. |
| 664 | */ |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 665 | int device_for_each_child(struct device * parent, void * data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | int (*fn)(struct device *, void *)) |
| 667 | { |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 668 | struct klist_iter i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | struct device * child; |
| 670 | int error = 0; |
| 671 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 672 | klist_iter_init(&parent->klist_children, &i); |
| 673 | while ((child = next_device(&i)) && !error) |
| 674 | error = fn(child, data); |
| 675 | klist_iter_exit(&i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | return error; |
| 677 | } |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | int __init devices_init(void) |
| 680 | { |
| 681 | return subsystem_register(&devices_subsys); |
| 682 | } |
| 683 | |
| 684 | EXPORT_SYMBOL_GPL(device_for_each_child); |
| 685 | |
| 686 | EXPORT_SYMBOL_GPL(device_initialize); |
| 687 | EXPORT_SYMBOL_GPL(device_add); |
| 688 | EXPORT_SYMBOL_GPL(device_register); |
| 689 | |
| 690 | EXPORT_SYMBOL_GPL(device_del); |
| 691 | EXPORT_SYMBOL_GPL(device_unregister); |
| 692 | EXPORT_SYMBOL_GPL(get_device); |
| 693 | EXPORT_SYMBOL_GPL(put_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | EXPORT_SYMBOL_GPL(device_create_file); |
| 696 | EXPORT_SYMBOL_GPL(device_remove_file); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 697 | |
| 698 | |
| 699 | static void device_create_release(struct device *dev) |
| 700 | { |
| 701 | pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); |
| 702 | kfree(dev); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * device_create - creates a device and registers it with sysfs |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 707 | * @class: pointer to the struct class that this device should be registered to |
| 708 | * @parent: pointer to the parent struct device of this new device, if any |
| 709 | * @devt: the dev_t for the char device to be added |
| 710 | * @fmt: string for the device's name |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 711 | * |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 712 | * This function can be used by char device classes. A struct device |
| 713 | * will be created in sysfs, registered to the specified class. |
| 714 | * |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 715 | * A "dev" file will be created, showing the dev_t for the device, if |
| 716 | * the dev_t is not 0,0. |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 717 | * If a pointer to a parent struct device is passed in, the newly created |
| 718 | * struct device will be a child of that device in sysfs. |
| 719 | * The pointer to the struct device will be returned from the call. |
| 720 | * Any further sysfs files that might be required can be created using this |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 721 | * pointer. |
| 722 | * |
| 723 | * Note: the struct class passed to this function must have previously |
| 724 | * been created with a call to class_create(). |
| 725 | */ |
| 726 | struct device *device_create(struct class *class, struct device *parent, |
Greg Kroah-Hartman | 5cbe5f8 | 2006-08-10 01:19:19 -0400 | [diff] [blame] | 727 | dev_t devt, const char *fmt, ...) |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 728 | { |
| 729 | va_list args; |
| 730 | struct device *dev = NULL; |
| 731 | int retval = -ENODEV; |
| 732 | |
| 733 | if (class == NULL || IS_ERR(class)) |
| 734 | goto error; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 735 | |
| 736 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 737 | if (!dev) { |
| 738 | retval = -ENOMEM; |
| 739 | goto error; |
| 740 | } |
| 741 | |
| 742 | dev->devt = devt; |
| 743 | dev->class = class; |
| 744 | dev->parent = parent; |
| 745 | dev->release = device_create_release; |
| 746 | |
| 747 | va_start(args, fmt); |
| 748 | vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); |
| 749 | va_end(args); |
| 750 | retval = device_register(dev); |
| 751 | if (retval) |
| 752 | goto error; |
| 753 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 754 | return dev; |
| 755 | |
| 756 | error: |
| 757 | kfree(dev); |
| 758 | return ERR_PTR(retval); |
| 759 | } |
| 760 | EXPORT_SYMBOL_GPL(device_create); |
| 761 | |
| 762 | /** |
| 763 | * device_destroy - removes a device that was created with device_create() |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 764 | * @class: pointer to the struct class that this device was registered with |
| 765 | * @devt: the dev_t of the device that was previously registered |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 766 | * |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 767 | * This call unregisters and cleans up a device that was created with a |
| 768 | * call to device_create(). |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 769 | */ |
| 770 | void device_destroy(struct class *class, dev_t devt) |
| 771 | { |
| 772 | struct device *dev = NULL; |
| 773 | struct device *dev_tmp; |
| 774 | |
| 775 | down(&class->sem); |
| 776 | list_for_each_entry(dev_tmp, &class->devices, node) { |
| 777 | if (dev_tmp->devt == devt) { |
| 778 | dev = dev_tmp; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | up(&class->sem); |
| 783 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 784 | if (dev) |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 785 | device_unregister(dev); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 786 | } |
| 787 | EXPORT_SYMBOL_GPL(device_destroy); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 788 | |
| 789 | /** |
| 790 | * device_rename - renames a device |
| 791 | * @dev: the pointer to the struct device to be renamed |
| 792 | * @new_name: the new name of the device |
| 793 | */ |
| 794 | int device_rename(struct device *dev, char *new_name) |
| 795 | { |
| 796 | char *old_class_name = NULL; |
| 797 | char *new_class_name = NULL; |
| 798 | char *old_symlink_name = NULL; |
| 799 | int error; |
| 800 | |
| 801 | dev = get_device(dev); |
| 802 | if (!dev) |
| 803 | return -EINVAL; |
| 804 | |
| 805 | pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); |
| 806 | |
| 807 | if ((dev->class) && (dev->parent)) |
| 808 | old_class_name = make_class_name(dev->class->name, &dev->kobj); |
| 809 | |
| 810 | if (dev->class) { |
| 811 | old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); |
| 812 | if (!old_symlink_name) |
| 813 | return -ENOMEM; |
| 814 | strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); |
| 815 | } |
| 816 | |
| 817 | strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); |
| 818 | |
| 819 | error = kobject_rename(&dev->kobj, new_name); |
| 820 | |
| 821 | if (old_class_name) { |
| 822 | new_class_name = make_class_name(dev->class->name, &dev->kobj); |
| 823 | if (new_class_name) { |
| 824 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, |
| 825 | new_class_name); |
| 826 | sysfs_remove_link(&dev->parent->kobj, old_class_name); |
| 827 | } |
| 828 | } |
| 829 | if (dev->class) { |
| 830 | sysfs_remove_link(&dev->class->subsys.kset.kobj, |
| 831 | old_symlink_name); |
| 832 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, |
| 833 | dev->bus_id); |
| 834 | } |
| 835 | put_device(dev); |
| 836 | |
| 837 | kfree(old_class_name); |
| 838 | kfree(new_class_name); |
| 839 | kfree(old_symlink_name); |
| 840 | |
| 841 | return error; |
| 842 | } |