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> |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 20 | #include <linux/notifier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/semaphore.h> |
| 23 | |
| 24 | #include "base.h" |
| 25 | #include "power/power.h" |
| 26 | |
| 27 | int (*platform_notify)(struct device * dev) = NULL; |
| 28 | int (*platform_notify_remove)(struct device * dev) = NULL; |
| 29 | |
| 30 | /* |
| 31 | * sysfs bindings for devices. |
| 32 | */ |
| 33 | |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 34 | /** |
| 35 | * dev_driver_string - Return a device's driver name, if at all possible |
| 36 | * @dev: struct device to get the name of |
| 37 | * |
| 38 | * Will return the device's driver's name if it is bound to a device. If |
| 39 | * the device is not bound to a device, it will return the name of the bus |
| 40 | * it is attached to. If it is not attached to a bus either, an empty |
| 41 | * string will be returned. |
| 42 | */ |
| 43 | const char *dev_driver_string(struct device *dev) |
| 44 | { |
| 45 | return dev->driver ? dev->driver->name : |
| 46 | (dev->bus ? dev->bus->name : ""); |
| 47 | } |
Matthew Wilcox | 310a922 | 2006-09-23 23:35:04 -0600 | [diff] [blame] | 48 | EXPORT_SYMBOL(dev_driver_string); |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #define to_dev(obj) container_of(obj, struct device, kobj) |
| 51 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static ssize_t |
| 54 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) |
| 55 | { |
| 56 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 57 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 58 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | if (dev_attr->show) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 61 | ret = dev_attr->show(dev, dev_attr, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | static ssize_t |
| 66 | dev_attr_store(struct kobject * kobj, struct attribute * attr, |
| 67 | const char * buf, size_t count) |
| 68 | { |
| 69 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 70 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 71 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | if (dev_attr->store) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 74 | ret = dev_attr->store(dev, dev_attr, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | static struct sysfs_ops dev_sysfs_ops = { |
| 79 | .show = dev_attr_show, |
| 80 | .store = dev_attr_store, |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * device_release - free device structure. |
| 86 | * @kobj: device's kobject. |
| 87 | * |
| 88 | * This is called once the reference count for the object |
| 89 | * reaches 0. We forward the call to the device's release |
| 90 | * method, which should handle actually freeing the structure. |
| 91 | */ |
| 92 | static void device_release(struct kobject * kobj) |
| 93 | { |
| 94 | struct device * dev = to_dev(kobj); |
| 95 | |
| 96 | if (dev->release) |
| 97 | dev->release(dev); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 98 | else if (dev->type && dev->type->release) |
| 99 | dev->type->release(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 100 | else if (dev->class && dev->class->dev_release) |
| 101 | dev->class->dev_release(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | else { |
| 103 | printk(KERN_ERR "Device '%s' does not have a release() function, " |
| 104 | "it is broken and must be fixed.\n", |
| 105 | dev->bus_id); |
| 106 | WARN_ON(1); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static struct kobj_type ktype_device = { |
| 111 | .release = device_release, |
| 112 | .sysfs_ops = &dev_sysfs_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 116 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | struct kobj_type *ktype = get_ktype(kobj); |
| 119 | |
| 120 | if (ktype == &ktype_device) { |
| 121 | struct device *dev = to_dev(kobj); |
| 122 | if (dev->bus) |
| 123 | return 1; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 124 | if (dev->class) |
| 125 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 130 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | struct device *dev = to_dev(kobj); |
| 133 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 134 | if (dev->bus) |
| 135 | return dev->bus->name; |
| 136 | if (dev->class) |
| 137 | return dev->class->name; |
| 138 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 141 | static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | int num_envp, char *buffer, int buffer_size) |
| 143 | { |
| 144 | struct device *dev = to_dev(kobj); |
| 145 | int i = 0; |
| 146 | int length = 0; |
| 147 | int retval = 0; |
| 148 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 149 | /* add the major/minor if present */ |
| 150 | if (MAJOR(dev->devt)) { |
| 151 | add_uevent_var(envp, num_envp, &i, |
| 152 | buffer, buffer_size, &length, |
| 153 | "MAJOR=%u", MAJOR(dev->devt)); |
| 154 | add_uevent_var(envp, num_envp, &i, |
| 155 | buffer, buffer_size, &length, |
| 156 | "MINOR=%u", MINOR(dev->devt)); |
| 157 | } |
| 158 | |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 159 | if (dev->driver) |
Kay Sievers | d81d9d6 | 2006-08-13 06:17:09 +0200 | [diff] [blame] | 160 | add_uevent_var(envp, num_envp, &i, |
| 161 | buffer, buffer_size, &length, |
| 162 | "DRIVER=%s", dev->driver->name); |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 163 | |
Kay Sievers | a87cb2a | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 164 | #ifdef CONFIG_SYSFS_DEPRECATED |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 165 | if (dev->class) { |
| 166 | struct device *parent = dev->parent; |
| 167 | |
| 168 | /* find first bus device in parent chain */ |
| 169 | while (parent && !parent->bus) |
| 170 | parent = parent->parent; |
| 171 | if (parent && parent->bus) { |
| 172 | const char *path; |
| 173 | |
| 174 | path = kobject_get_path(&parent->kobj, GFP_KERNEL); |
| 175 | add_uevent_var(envp, num_envp, &i, |
| 176 | buffer, buffer_size, &length, |
| 177 | "PHYSDEVPATH=%s", path); |
| 178 | kfree(path); |
| 179 | |
| 180 | add_uevent_var(envp, num_envp, &i, |
| 181 | buffer, buffer_size, &length, |
| 182 | "PHYSDEVBUS=%s", parent->bus->name); |
| 183 | |
| 184 | if (parent->driver) |
| 185 | add_uevent_var(envp, num_envp, &i, |
| 186 | buffer, buffer_size, &length, |
| 187 | "PHYSDEVDRIVER=%s", parent->driver->name); |
| 188 | } |
| 189 | } else if (dev->bus) { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 190 | add_uevent_var(envp, num_envp, &i, |
| 191 | buffer, buffer_size, &length, |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 192 | "PHYSDEVBUS=%s", dev->bus->name); |
| 193 | |
| 194 | if (dev->driver) |
| 195 | add_uevent_var(envp, num_envp, &i, |
| 196 | buffer, buffer_size, &length, |
| 197 | "PHYSDEVDRIVER=%s", dev->driver->name); |
Kay Sievers | d81d9d6 | 2006-08-13 06:17:09 +0200 | [diff] [blame] | 198 | } |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 199 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | /* terminate, set to next free slot, shrink available space */ |
| 202 | envp[i] = NULL; |
| 203 | envp = &envp[i]; |
| 204 | num_envp -= i; |
| 205 | buffer = &buffer[length]; |
| 206 | buffer_size -= length; |
| 207 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 208 | if (dev->bus && dev->bus->uevent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | /* have the bus specific function add its stuff */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 210 | retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 211 | if (retval) |
| 212 | pr_debug ("%s: bus uevent() returned %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | __FUNCTION__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 216 | if (dev->class && dev->class->dev_uevent) { |
| 217 | /* have the class specific function add its stuff */ |
| 218 | retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 219 | if (retval) |
| 220 | pr_debug("%s: class uevent() returned %d\n", |
| 221 | __FUNCTION__, retval); |
| 222 | } |
| 223 | |
| 224 | if (dev->type && dev->type->uevent) { |
| 225 | /* have the device type specific fuction add its stuff */ |
| 226 | retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size); |
| 227 | if (retval) |
| 228 | pr_debug("%s: dev_type uevent() returned %d\n", |
| 229 | __FUNCTION__, retval); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | return retval; |
| 233 | } |
| 234 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 235 | static struct kset_uevent_ops device_uevent_ops = { |
| 236 | .filter = dev_uevent_filter, |
| 237 | .name = dev_uevent_name, |
| 238 | .uevent = dev_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 241 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
| 242 | const char *buf, size_t count) |
| 243 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 244 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 245 | return count; |
| 246 | } |
| 247 | |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 248 | static int device_add_groups(struct device *dev) |
| 249 | { |
| 250 | int i; |
| 251 | int error = 0; |
| 252 | |
| 253 | if (dev->groups) { |
| 254 | for (i = 0; dev->groups[i]; i++) { |
| 255 | error = sysfs_create_group(&dev->kobj, dev->groups[i]); |
| 256 | if (error) { |
| 257 | while (--i >= 0) |
| 258 | sysfs_remove_group(&dev->kobj, dev->groups[i]); |
| 259 | goto out; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | out: |
| 264 | return error; |
| 265 | } |
| 266 | |
| 267 | static void device_remove_groups(struct device *dev) |
| 268 | { |
| 269 | int i; |
| 270 | if (dev->groups) { |
| 271 | for (i = 0; dev->groups[i]; i++) { |
| 272 | sysfs_remove_group(&dev->kobj, dev->groups[i]); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 277 | static int device_add_attrs(struct device *dev) |
| 278 | { |
| 279 | struct class *class = dev->class; |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 280 | struct device_type *type = dev->type; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 281 | int error = 0; |
| 282 | int i; |
| 283 | |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 284 | if (class && class->dev_attrs) { |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 285 | for (i = 0; attr_name(class->dev_attrs[i]); i++) { |
| 286 | error = device_create_file(dev, &class->dev_attrs[i]); |
| 287 | if (error) |
| 288 | break; |
| 289 | } |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 290 | if (error) |
| 291 | while (--i >= 0) |
| 292 | device_remove_file(dev, &class->dev_attrs[i]); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 293 | } |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 294 | |
| 295 | if (type && type->attrs) { |
| 296 | for (i = 0; attr_name(type->attrs[i]); i++) { |
| 297 | error = device_create_file(dev, &type->attrs[i]); |
| 298 | if (error) |
| 299 | break; |
| 300 | } |
| 301 | if (error) |
| 302 | while (--i >= 0) |
| 303 | device_remove_file(dev, &type->attrs[i]); |
| 304 | } |
| 305 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 306 | return error; |
| 307 | } |
| 308 | |
| 309 | static void device_remove_attrs(struct device *dev) |
| 310 | { |
| 311 | struct class *class = dev->class; |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 312 | struct device_type *type = dev->type; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 313 | int i; |
| 314 | |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 315 | if (class && class->dev_attrs) { |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 316 | for (i = 0; attr_name(class->dev_attrs[i]); i++) |
| 317 | device_remove_file(dev, &class->dev_attrs[i]); |
| 318 | } |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 319 | |
| 320 | if (type && type->attrs) { |
| 321 | for (i = 0; attr_name(type->attrs[i]); i++) |
| 322 | device_remove_file(dev, &type->attrs[i]); |
| 323 | } |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 327 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
| 328 | char *buf) |
| 329 | { |
| 330 | return print_dev_t(buf, dev->devt); |
| 331 | } |
| 332 | |
Martin Waitz | 0863afb | 2006-01-09 20:53:55 -0800 | [diff] [blame] | 333 | /* |
| 334 | * devices_subsys - structure to be registered with kobject core. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | */ |
| 336 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 337 | decl_subsys(devices, &ktype_device, &device_uevent_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | |
| 340 | /** |
| 341 | * device_create_file - create sysfs attribute file for device. |
| 342 | * @dev: device. |
| 343 | * @attr: device attribute descriptor. |
| 344 | */ |
| 345 | |
| 346 | int device_create_file(struct device * dev, struct device_attribute * attr) |
| 347 | { |
| 348 | int error = 0; |
| 349 | if (get_device(dev)) { |
| 350 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
| 351 | put_device(dev); |
| 352 | } |
| 353 | return error; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * device_remove_file - remove sysfs attribute file. |
| 358 | * @dev: device. |
| 359 | * @attr: device attribute descriptor. |
| 360 | */ |
| 361 | |
| 362 | void device_remove_file(struct device * dev, struct device_attribute * attr) |
| 363 | { |
| 364 | if (get_device(dev)) { |
| 365 | sysfs_remove_file(&dev->kobj, &attr->attr); |
| 366 | put_device(dev); |
| 367 | } |
| 368 | } |
| 369 | |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 370 | /** |
| 371 | * device_create_bin_file - create sysfs binary attribute file for device. |
| 372 | * @dev: device. |
| 373 | * @attr: device binary attribute descriptor. |
| 374 | */ |
| 375 | int device_create_bin_file(struct device *dev, struct bin_attribute *attr) |
| 376 | { |
| 377 | int error = -EINVAL; |
| 378 | if (dev) |
| 379 | error = sysfs_create_bin_file(&dev->kobj, attr); |
| 380 | return error; |
| 381 | } |
| 382 | EXPORT_SYMBOL_GPL(device_create_bin_file); |
| 383 | |
| 384 | /** |
| 385 | * device_remove_bin_file - remove sysfs binary attribute file |
| 386 | * @dev: device. |
| 387 | * @attr: device binary attribute descriptor. |
| 388 | */ |
| 389 | void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) |
| 390 | { |
| 391 | if (dev) |
| 392 | sysfs_remove_bin_file(&dev->kobj, attr); |
| 393 | } |
| 394 | EXPORT_SYMBOL_GPL(device_remove_bin_file); |
| 395 | |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 396 | /** |
| 397 | * device_schedule_callback - helper to schedule a callback for a device |
| 398 | * @dev: device. |
| 399 | * @func: callback function to invoke later. |
| 400 | * |
| 401 | * Attribute methods must not unregister themselves or their parent device |
| 402 | * (which would amount to the same thing). Attempts to do so will deadlock, |
| 403 | * since unregistration is mutually exclusive with driver callbacks. |
| 404 | * |
| 405 | * Instead methods can call this routine, which will attempt to allocate |
| 406 | * and schedule a workqueue request to call back @func with @dev as its |
| 407 | * argument in the workqueue's process context. @dev will be pinned until |
| 408 | * @func returns. |
| 409 | * |
| 410 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
| 411 | * be allocated. |
| 412 | * |
| 413 | * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an |
| 414 | * underlying sysfs routine (since it is intended for use by attribute |
| 415 | * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. |
| 416 | */ |
| 417 | int device_schedule_callback(struct device *dev, |
| 418 | void (*func)(struct device *)) |
| 419 | { |
| 420 | return sysfs_schedule_callback(&dev->kobj, |
| 421 | (void (*)(void *)) func, dev); |
| 422 | } |
| 423 | EXPORT_SYMBOL_GPL(device_schedule_callback); |
| 424 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 425 | static void klist_children_get(struct klist_node *n) |
| 426 | { |
| 427 | struct device *dev = container_of(n, struct device, knode_parent); |
| 428 | |
| 429 | get_device(dev); |
| 430 | } |
| 431 | |
| 432 | static void klist_children_put(struct klist_node *n) |
| 433 | { |
| 434 | struct device *dev = container_of(n, struct device, knode_parent); |
| 435 | |
| 436 | put_device(dev); |
| 437 | } |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | /** |
| 441 | * device_initialize - init device structure. |
| 442 | * @dev: device. |
| 443 | * |
| 444 | * This prepares the device for use by other layers, |
| 445 | * including adding it to the device hierarchy. |
| 446 | * It is the first half of device_register(), if called by |
| 447 | * that, though it can also be called separately, so one |
| 448 | * may use @dev's fields (e.g. the refcount). |
| 449 | */ |
| 450 | |
| 451 | void device_initialize(struct device *dev) |
| 452 | { |
| 453 | kobj_set_kset_s(dev, devices_subsys); |
| 454 | kobject_init(&dev->kobj); |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 455 | klist_init(&dev->klist_children, klist_children_get, |
| 456 | klist_children_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | INIT_LIST_HEAD(&dev->dma_pools); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 458 | INIT_LIST_HEAD(&dev->node); |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 459 | init_MUTEX(&dev->sem); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 460 | spin_lock_init(&dev->devres_lock); |
| 461 | INIT_LIST_HEAD(&dev->devres_head); |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 462 | device_init_wakeup(dev, 0); |
Christoph Hellwig | 8734813 | 2006-12-06 20:32:33 -0800 | [diff] [blame] | 463 | set_dev_node(dev, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 466 | #ifdef CONFIG_SYSFS_DEPRECATED |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 467 | static struct kobject * get_device_parent(struct device *dev, |
| 468 | struct device *parent) |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 469 | { |
| 470 | /* Set the parent to the class, not the parent device */ |
| 471 | /* this keeps sysfs from having a symlink to make old udevs happy */ |
| 472 | if (dev->class) |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 473 | return &dev->class->subsys.kset.kobj; |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 474 | else if (parent) |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 475 | return &parent->kobj; |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 476 | |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 477 | return NULL; |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 478 | } |
| 479 | #else |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 480 | static struct kobject * virtual_device_parent(struct device *dev) |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 481 | { |
| 482 | if (!dev->class) |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 483 | return ERR_PTR(-ENODEV); |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 484 | |
| 485 | if (!dev->class->virtual_dir) { |
| 486 | static struct kobject *virtual_dir = NULL; |
| 487 | |
| 488 | if (!virtual_dir) |
| 489 | virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual"); |
| 490 | dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name); |
| 491 | } |
| 492 | |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 493 | return dev->class->virtual_dir; |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 496 | static struct kobject * get_device_parent(struct device *dev, |
| 497 | struct device *parent) |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 498 | { |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 499 | /* if this is a class device, and has no parent, create one */ |
| 500 | if ((dev->class) && (parent == NULL)) { |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 501 | return virtual_device_parent(dev); |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 502 | } else if (parent) |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 503 | return &parent->kobj; |
| 504 | return NULL; |
| 505 | } |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 506 | |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 507 | #endif |
| 508 | static int setup_parent(struct device *dev, struct device *parent) |
| 509 | { |
| 510 | struct kobject *kobj; |
| 511 | kobj = get_device_parent(dev, parent); |
| 512 | if (IS_ERR(kobj)) |
| 513 | return PTR_ERR(kobj); |
| 514 | if (kobj) |
| 515 | dev->kobj.parent = kobj; |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 516 | return 0; |
| 517 | } |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | /** |
| 520 | * device_add - add device to device hierarchy. |
| 521 | * @dev: device. |
| 522 | * |
| 523 | * This is part 2 of device_register(), though may be called |
| 524 | * separately _iff_ device_initialize() has been called separately. |
| 525 | * |
| 526 | * This adds it to the kobject hierarchy via kobject_add(), adds it |
| 527 | * to the global and sibling lists for the device, then |
| 528 | * adds it to the other relevant subsystems of the driver model. |
| 529 | */ |
| 530 | int device_add(struct device *dev) |
| 531 | { |
| 532 | struct device *parent = NULL; |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 533 | char *class_name = NULL; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 534 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | int error = -EINVAL; |
| 536 | |
| 537 | dev = get_device(dev); |
| 538 | if (!dev || !strlen(dev->bus_id)) |
| 539 | goto Error; |
| 540 | |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 541 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); |
Greg Kroah-Hartman | c205ef4 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | parent = get_device(dev->parent); |
| 544 | |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 545 | error = setup_parent(dev, parent); |
| 546 | if (error) |
| 547 | goto Error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
| 549 | /* first, register with generic layer. */ |
| 550 | kobject_set_name(&dev->kobj, "%s", dev->bus_id); |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 551 | error = kobject_add(&dev->kobj); |
| 552 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | goto Error; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 554 | |
Brian Walsh | 3702264 | 2006-08-14 22:43:19 -0700 | [diff] [blame] | 555 | /* notify platform of device entry */ |
| 556 | if (platform_notify) |
| 557 | platform_notify(dev); |
| 558 | |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 559 | /* notify clients of device entry (new way) */ |
| 560 | if (dev->bus) |
| 561 | blocking_notifier_call_chain(&dev->bus->bus_notifier, |
| 562 | BUS_NOTIFY_ADD_DEVICE, dev); |
| 563 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 564 | dev->uevent_attr.attr.name = "uevent"; |
| 565 | dev->uevent_attr.attr.mode = S_IWUSR; |
| 566 | if (dev->driver) |
| 567 | dev->uevent_attr.attr.owner = dev->driver->owner; |
| 568 | dev->uevent_attr.store = store_uevent; |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 569 | error = device_create_file(dev, &dev->uevent_attr); |
| 570 | if (error) |
| 571 | goto attrError; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 572 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 573 | if (MAJOR(dev->devt)) { |
| 574 | struct device_attribute *attr; |
| 575 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
| 576 | if (!attr) { |
| 577 | error = -ENOMEM; |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 578 | goto ueventattrError; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 579 | } |
| 580 | attr->attr.name = "dev"; |
| 581 | attr->attr.mode = S_IRUGO; |
| 582 | if (dev->driver) |
| 583 | attr->attr.owner = dev->driver->owner; |
| 584 | attr->show = show_dev; |
| 585 | error = device_create_file(dev, attr); |
| 586 | if (error) { |
| 587 | kfree(attr); |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 588 | goto ueventattrError; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | dev->devt_attr = attr; |
| 592 | } |
| 593 | |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 594 | if (dev->class) { |
| 595 | sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj, |
| 596 | "subsystem"); |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 597 | /* If this is not a "fake" compatible device, then create the |
| 598 | * symlink from the class to the device. */ |
| 599 | if (dev->kobj.parent != &dev->class->subsys.kset.kobj) |
| 600 | sysfs_create_link(&dev->class->subsys.kset.kobj, |
| 601 | &dev->kobj, dev->bus_id); |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 602 | if (parent) { |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 603 | sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
| 604 | "device"); |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 605 | #ifdef CONFIG_SYSFS_DEPRECATED |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 606 | class_name = make_class_name(dev->class->name, |
| 607 | &dev->kobj); |
| 608 | if (class_name) |
| 609 | sysfs_create_link(&dev->parent->kobj, |
| 610 | &dev->kobj, class_name); |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 611 | #endif |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 612 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 613 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 614 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 615 | if ((error = device_add_attrs(dev))) |
| 616 | goto AttrsError; |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 617 | if ((error = device_add_groups(dev))) |
| 618 | goto GroupError; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if ((error = device_pm_add(dev))) |
| 620 | goto PMError; |
| 621 | if ((error = bus_add_device(dev))) |
| 622 | goto BusError; |
Kay Sievers | b7a3e81 | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 623 | if (!dev->uevent_suppress) |
| 624 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Alan Stern | f70fa62 | 2006-10-05 17:03:24 -0400 | [diff] [blame] | 625 | if ((error = bus_attach_device(dev))) |
| 626 | goto AttachError; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | if (parent) |
James Bottomley | d856f1e | 2005-08-19 09:14:01 -0400 | [diff] [blame] | 628 | klist_add_tail(&dev->knode_parent, &parent->klist_children); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 630 | if (dev->class) { |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 631 | down(&dev->class->sem); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 632 | /* tie the class to the device */ |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 633 | list_add_tail(&dev->node, &dev->class->devices); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 634 | |
| 635 | /* notify any interfaces that the device is here */ |
| 636 | list_for_each_entry(class_intf, &dev->class->interfaces, node) |
| 637 | if (class_intf->add_dev) |
| 638 | class_intf->add_dev(dev, class_intf); |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 639 | up(&dev->class->sem); |
| 640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | Done: |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 642 | kfree(class_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | put_device(dev); |
| 644 | return error; |
Alan Stern | f70fa62 | 2006-10-05 17:03:24 -0400 | [diff] [blame] | 645 | AttachError: |
| 646 | bus_remove_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | BusError: |
| 648 | device_pm_remove(dev); |
| 649 | PMError: |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 650 | if (dev->bus) |
| 651 | blocking_notifier_call_chain(&dev->bus->bus_notifier, |
| 652 | BUS_NOTIFY_DEL_DEVICE, dev); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 653 | device_remove_groups(dev); |
| 654 | GroupError: |
James Simmons | 82f0cf9 | 2007-02-21 17:44:51 +0000 | [diff] [blame] | 655 | device_remove_attrs(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 656 | AttrsError: |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 657 | if (dev->devt_attr) { |
| 658 | device_remove_file(dev, dev->devt_attr); |
| 659 | kfree(dev->devt_attr); |
| 660 | } |
James Simmons | 82f0cf9 | 2007-02-21 17:44:51 +0000 | [diff] [blame] | 661 | |
| 662 | if (dev->class) { |
| 663 | sysfs_remove_link(&dev->kobj, "subsystem"); |
| 664 | /* If this is not a "fake" compatible device, remove the |
| 665 | * symlink from the class to the device. */ |
| 666 | if (dev->kobj.parent != &dev->class->subsys.kset.kobj) |
| 667 | sysfs_remove_link(&dev->class->subsys.kset.kobj, |
| 668 | dev->bus_id); |
James Simmons | 82f0cf9 | 2007-02-21 17:44:51 +0000 | [diff] [blame] | 669 | if (parent) { |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 670 | #ifdef CONFIG_SYSFS_DEPRECATED |
James Simmons | 82f0cf9 | 2007-02-21 17:44:51 +0000 | [diff] [blame] | 671 | char *class_name = make_class_name(dev->class->name, |
| 672 | &dev->kobj); |
| 673 | if (class_name) |
| 674 | sysfs_remove_link(&dev->parent->kobj, |
| 675 | class_name); |
| 676 | kfree(class_name); |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 677 | #endif |
James Simmons | 82f0cf9 | 2007-02-21 17:44:51 +0000 | [diff] [blame] | 678 | sysfs_remove_link(&dev->kobj, "device"); |
| 679 | } |
James Simmons | 82f0cf9 | 2007-02-21 17:44:51 +0000 | [diff] [blame] | 680 | } |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 681 | ueventattrError: |
| 682 | device_remove_file(dev, &dev->uevent_attr); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 683 | attrError: |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 684 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | kobject_del(&dev->kobj); |
| 686 | Error: |
| 687 | if (parent) |
| 688 | put_device(parent); |
| 689 | goto Done; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | /** |
| 694 | * device_register - register a device with the system. |
| 695 | * @dev: pointer to the device structure |
| 696 | * |
| 697 | * This happens in two clean steps - initialize the device |
| 698 | * and add it to the system. The two steps can be called |
| 699 | * separately, but this is the easiest and most common. |
| 700 | * I.e. you should only call the two helpers separately if |
| 701 | * have a clearly defined need to use and refcount the device |
| 702 | * before it is added to the hierarchy. |
| 703 | */ |
| 704 | |
| 705 | int device_register(struct device *dev) |
| 706 | { |
| 707 | device_initialize(dev); |
| 708 | return device_add(dev); |
| 709 | } |
| 710 | |
| 711 | |
| 712 | /** |
| 713 | * get_device - increment reference count for device. |
| 714 | * @dev: device. |
| 715 | * |
| 716 | * This simply forwards the call to kobject_get(), though |
| 717 | * we do take care to provide for the case that we get a NULL |
| 718 | * pointer passed in. |
| 719 | */ |
| 720 | |
| 721 | struct device * get_device(struct device * dev) |
| 722 | { |
| 723 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; |
| 724 | } |
| 725 | |
| 726 | |
| 727 | /** |
| 728 | * put_device - decrement reference count. |
| 729 | * @dev: device in question. |
| 730 | */ |
| 731 | void put_device(struct device * dev) |
| 732 | { |
| 733 | if (dev) |
| 734 | kobject_put(&dev->kobj); |
| 735 | } |
| 736 | |
| 737 | |
| 738 | /** |
| 739 | * device_del - delete device from system. |
| 740 | * @dev: device. |
| 741 | * |
| 742 | * This is the first part of the device unregistration |
| 743 | * sequence. This removes the device from the lists we control |
| 744 | * from here, has it removed from the other driver model |
| 745 | * subsystems it was added to in device_add(), and removes it |
| 746 | * from the kobject hierarchy. |
| 747 | * |
| 748 | * NOTE: this should be called manually _iff_ device_add() was |
| 749 | * also called manually. |
| 750 | */ |
| 751 | |
| 752 | void device_del(struct device * dev) |
| 753 | { |
| 754 | struct device * parent = dev->parent; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 755 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | if (parent) |
Patrick Mochel | d62c0f9 | 2005-06-24 08:39:33 -0700 | [diff] [blame] | 758 | klist_del(&dev->knode_parent); |
Catalin Marinas | 82189b9 | 2006-11-25 11:09:30 -0800 | [diff] [blame] | 759 | if (dev->devt_attr) { |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 760 | device_remove_file(dev, dev->devt_attr); |
Catalin Marinas | 82189b9 | 2006-11-25 11:09:30 -0800 | [diff] [blame] | 761 | kfree(dev->devt_attr); |
| 762 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 763 | if (dev->class) { |
| 764 | sysfs_remove_link(&dev->kobj, "subsystem"); |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 765 | /* If this is not a "fake" compatible device, remove the |
| 766 | * symlink from the class to the device. */ |
| 767 | if (dev->kobj.parent != &dev->class->subsys.kset.kobj) |
| 768 | sysfs_remove_link(&dev->class->subsys.kset.kobj, |
| 769 | dev->bus_id); |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 770 | if (parent) { |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 771 | #ifdef CONFIG_SYSFS_DEPRECATED |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 772 | char *class_name = make_class_name(dev->class->name, |
| 773 | &dev->kobj); |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 774 | if (class_name) |
| 775 | sysfs_remove_link(&dev->parent->kobj, |
| 776 | class_name); |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 777 | kfree(class_name); |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 778 | #endif |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 779 | sysfs_remove_link(&dev->kobj, "device"); |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 780 | } |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 781 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 782 | down(&dev->class->sem); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 783 | /* notify any interfaces that the device is now gone */ |
| 784 | list_for_each_entry(class_intf, &dev->class->interfaces, node) |
| 785 | if (class_intf->remove_dev) |
| 786 | class_intf->remove_dev(dev, class_intf); |
| 787 | /* remove the device from the class list */ |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 788 | list_del_init(&dev->node); |
| 789 | up(&dev->class->sem); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 790 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 791 | device_remove_file(dev, &dev->uevent_attr); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 792 | device_remove_groups(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 793 | device_remove_attrs(dev); |
Benjamin Herrenschmidt | 2895353 | 2006-11-08 19:46:14 -0800 | [diff] [blame] | 794 | bus_remove_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
Tejun Heo | 2f8d16a | 2007-03-09 19:34:19 +0900 | [diff] [blame] | 796 | /* |
| 797 | * Some platform devices are driven without driver attached |
| 798 | * and managed resources may have been acquired. Make sure |
| 799 | * all resources are released. |
| 800 | */ |
| 801 | devres_release_all(dev); |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | /* Notify the platform of the removal, in case they |
| 804 | * need to do anything... |
| 805 | */ |
| 806 | if (platform_notify_remove) |
| 807 | platform_notify_remove(dev); |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 808 | if (dev->bus) |
| 809 | blocking_notifier_call_chain(&dev->bus->bus_notifier, |
| 810 | BUS_NOTIFY_DEL_DEVICE, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | device_pm_remove(dev); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 812 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | kobject_del(&dev->kobj); |
| 814 | if (parent) |
| 815 | put_device(parent); |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * device_unregister - unregister device from system. |
| 820 | * @dev: device going away. |
| 821 | * |
| 822 | * We do this in two parts, like we do device_register(). First, |
| 823 | * we remove it from all the subsystems with device_del(), then |
| 824 | * we decrement the reference count via put_device(). If that |
| 825 | * is the final reference count, the device will be cleaned up |
| 826 | * via device_release() above. Otherwise, the structure will |
| 827 | * stick around until the final reference to the device is dropped. |
| 828 | */ |
| 829 | void device_unregister(struct device * dev) |
| 830 | { |
| 831 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); |
| 832 | device_del(dev); |
| 833 | put_device(dev); |
| 834 | } |
| 835 | |
| 836 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 837 | static struct device * next_device(struct klist_iter * i) |
| 838 | { |
| 839 | struct klist_node * n = klist_next(i); |
| 840 | return n ? container_of(n, struct device, knode_parent) : NULL; |
| 841 | } |
| 842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | /** |
| 844 | * device_for_each_child - device child iterator. |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 845 | * @parent: parent struct device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | * @data: data for the callback. |
| 847 | * @fn: function to be called for each device. |
| 848 | * |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 849 | * Iterate over @parent's child devices, and call @fn for each, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | * passing it @data. |
| 851 | * |
| 852 | * We check the return of @fn each time. If it returns anything |
| 853 | * other than 0, we break out and return that value. |
| 854 | */ |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 855 | int device_for_each_child(struct device * parent, void * data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | int (*fn)(struct device *, void *)) |
| 857 | { |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 858 | struct klist_iter i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | struct device * child; |
| 860 | int error = 0; |
| 861 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 862 | klist_iter_init(&parent->klist_children, &i); |
| 863 | while ((child = next_device(&i)) && !error) |
| 864 | error = fn(child, data); |
| 865 | klist_iter_exit(&i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | return error; |
| 867 | } |
| 868 | |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 869 | /** |
| 870 | * device_find_child - device iterator for locating a particular device. |
| 871 | * @parent: parent struct device |
| 872 | * @data: Data to pass to match function |
| 873 | * @match: Callback function to check device |
| 874 | * |
| 875 | * This is similar to the device_for_each_child() function above, but it |
| 876 | * returns a reference to a device that is 'found' for later use, as |
| 877 | * determined by the @match callback. |
| 878 | * |
| 879 | * The callback should return 0 if the device doesn't match and non-zero |
| 880 | * if it does. If the callback returns non-zero and a reference to the |
| 881 | * current device can be obtained, this function will return to the caller |
| 882 | * and not iterate over any more devices. |
| 883 | */ |
| 884 | struct device * device_find_child(struct device *parent, void *data, |
| 885 | int (*match)(struct device *, void *)) |
| 886 | { |
| 887 | struct klist_iter i; |
| 888 | struct device *child; |
| 889 | |
| 890 | if (!parent) |
| 891 | return NULL; |
| 892 | |
| 893 | klist_iter_init(&parent->klist_children, &i); |
| 894 | while ((child = next_device(&i))) |
| 895 | if (match(child, data) && get_device(child)) |
| 896 | break; |
| 897 | klist_iter_exit(&i); |
| 898 | return child; |
| 899 | } |
| 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | int __init devices_init(void) |
| 902 | { |
| 903 | return subsystem_register(&devices_subsys); |
| 904 | } |
| 905 | |
| 906 | EXPORT_SYMBOL_GPL(device_for_each_child); |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 907 | EXPORT_SYMBOL_GPL(device_find_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
| 909 | EXPORT_SYMBOL_GPL(device_initialize); |
| 910 | EXPORT_SYMBOL_GPL(device_add); |
| 911 | EXPORT_SYMBOL_GPL(device_register); |
| 912 | |
| 913 | EXPORT_SYMBOL_GPL(device_del); |
| 914 | EXPORT_SYMBOL_GPL(device_unregister); |
| 915 | EXPORT_SYMBOL_GPL(get_device); |
| 916 | EXPORT_SYMBOL_GPL(put_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
| 918 | EXPORT_SYMBOL_GPL(device_create_file); |
| 919 | EXPORT_SYMBOL_GPL(device_remove_file); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 920 | |
| 921 | |
| 922 | static void device_create_release(struct device *dev) |
| 923 | { |
| 924 | pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); |
| 925 | kfree(dev); |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * device_create - creates a device and registers it with sysfs |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 930 | * @class: pointer to the struct class that this device should be registered to |
| 931 | * @parent: pointer to the parent struct device of this new device, if any |
| 932 | * @devt: the dev_t for the char device to be added |
| 933 | * @fmt: string for the device's name |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 934 | * |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 935 | * This function can be used by char device classes. A struct device |
| 936 | * will be created in sysfs, registered to the specified class. |
| 937 | * |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 938 | * A "dev" file will be created, showing the dev_t for the device, if |
| 939 | * the dev_t is not 0,0. |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 940 | * If a pointer to a parent struct device is passed in, the newly created |
| 941 | * struct device will be a child of that device in sysfs. |
| 942 | * The pointer to the struct device will be returned from the call. |
| 943 | * 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] | 944 | * pointer. |
| 945 | * |
| 946 | * Note: the struct class passed to this function must have previously |
| 947 | * been created with a call to class_create(). |
| 948 | */ |
| 949 | struct device *device_create(struct class *class, struct device *parent, |
Greg Kroah-Hartman | 5cbe5f8 | 2006-08-10 01:19:19 -0400 | [diff] [blame] | 950 | dev_t devt, const char *fmt, ...) |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 951 | { |
| 952 | va_list args; |
| 953 | struct device *dev = NULL; |
| 954 | int retval = -ENODEV; |
| 955 | |
| 956 | if (class == NULL || IS_ERR(class)) |
| 957 | goto error; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 958 | |
| 959 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 960 | if (!dev) { |
| 961 | retval = -ENOMEM; |
| 962 | goto error; |
| 963 | } |
| 964 | |
| 965 | dev->devt = devt; |
| 966 | dev->class = class; |
| 967 | dev->parent = parent; |
| 968 | dev->release = device_create_release; |
| 969 | |
| 970 | va_start(args, fmt); |
| 971 | vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); |
| 972 | va_end(args); |
| 973 | retval = device_register(dev); |
| 974 | if (retval) |
| 975 | goto error; |
| 976 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 977 | return dev; |
| 978 | |
| 979 | error: |
| 980 | kfree(dev); |
| 981 | return ERR_PTR(retval); |
| 982 | } |
| 983 | EXPORT_SYMBOL_GPL(device_create); |
| 984 | |
| 985 | /** |
| 986 | * device_destroy - removes a device that was created with device_create() |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 987 | * @class: pointer to the struct class that this device was registered with |
| 988 | * @devt: the dev_t of the device that was previously registered |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 989 | * |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 990 | * This call unregisters and cleans up a device that was created with a |
| 991 | * call to device_create(). |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 992 | */ |
| 993 | void device_destroy(struct class *class, dev_t devt) |
| 994 | { |
| 995 | struct device *dev = NULL; |
| 996 | struct device *dev_tmp; |
| 997 | |
| 998 | down(&class->sem); |
| 999 | list_for_each_entry(dev_tmp, &class->devices, node) { |
| 1000 | if (dev_tmp->devt == devt) { |
| 1001 | dev = dev_tmp; |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | up(&class->sem); |
| 1006 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 1007 | if (dev) |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1008 | device_unregister(dev); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1009 | } |
| 1010 | EXPORT_SYMBOL_GPL(device_destroy); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1011 | |
| 1012 | /** |
| 1013 | * device_rename - renames a device |
| 1014 | * @dev: the pointer to the struct device to be renamed |
| 1015 | * @new_name: the new name of the device |
| 1016 | */ |
| 1017 | int device_rename(struct device *dev, char *new_name) |
| 1018 | { |
| 1019 | char *old_class_name = NULL; |
| 1020 | char *new_class_name = NULL; |
| 1021 | char *old_symlink_name = NULL; |
| 1022 | int error; |
| 1023 | |
| 1024 | dev = get_device(dev); |
| 1025 | if (!dev) |
| 1026 | return -EINVAL; |
| 1027 | |
| 1028 | pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); |
| 1029 | |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 1030 | #ifdef CONFIG_SYSFS_DEPRECATED |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1031 | if ((dev->class) && (dev->parent)) |
| 1032 | old_class_name = make_class_name(dev->class->name, &dev->kobj); |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 1033 | #endif |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1034 | |
| 1035 | if (dev->class) { |
| 1036 | old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); |
Jesper Juhl | 952ab43 | 2006-09-28 23:56:01 +0200 | [diff] [blame] | 1037 | if (!old_symlink_name) { |
| 1038 | error = -ENOMEM; |
| 1039 | goto out_free_old_class; |
| 1040 | } |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1041 | strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); |
| 1042 | } |
| 1043 | |
| 1044 | strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); |
| 1045 | |
| 1046 | error = kobject_rename(&dev->kobj, new_name); |
| 1047 | |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 1048 | #ifdef CONFIG_SYSFS_DEPRECATED |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1049 | if (old_class_name) { |
| 1050 | new_class_name = make_class_name(dev->class->name, &dev->kobj); |
| 1051 | if (new_class_name) { |
| 1052 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, |
| 1053 | new_class_name); |
| 1054 | sysfs_remove_link(&dev->parent->kobj, old_class_name); |
| 1055 | } |
| 1056 | } |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 1057 | #endif |
| 1058 | |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1059 | if (dev->class) { |
| 1060 | sysfs_remove_link(&dev->class->subsys.kset.kobj, |
| 1061 | old_symlink_name); |
| 1062 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, |
| 1063 | dev->bus_id); |
| 1064 | } |
| 1065 | put_device(dev); |
| 1066 | |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1067 | kfree(new_class_name); |
| 1068 | kfree(old_symlink_name); |
Jesper Juhl | 952ab43 | 2006-09-28 23:56:01 +0200 | [diff] [blame] | 1069 | out_free_old_class: |
| 1070 | kfree(old_class_name); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1071 | |
| 1072 | return error; |
| 1073 | } |
Johannes Berg | a2807db | 2007-02-28 12:38:31 +0100 | [diff] [blame] | 1074 | EXPORT_SYMBOL_GPL(device_rename); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1075 | |
| 1076 | static int device_move_class_links(struct device *dev, |
| 1077 | struct device *old_parent, |
| 1078 | struct device *new_parent) |
| 1079 | { |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 1080 | int error = 0; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1081 | #ifdef CONFIG_SYSFS_DEPRECATED |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1082 | char *class_name; |
| 1083 | |
| 1084 | class_name = make_class_name(dev->class->name, &dev->kobj); |
| 1085 | if (!class_name) { |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 1086 | error = -ENOMEM; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1087 | goto out; |
| 1088 | } |
| 1089 | if (old_parent) { |
| 1090 | sysfs_remove_link(&dev->kobj, "device"); |
| 1091 | sysfs_remove_link(&old_parent->kobj, class_name); |
| 1092 | } |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1093 | if (new_parent) { |
| 1094 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, |
| 1095 | "device"); |
| 1096 | if (error) |
| 1097 | goto out; |
| 1098 | error = sysfs_create_link(&new_parent->kobj, &dev->kobj, |
| 1099 | class_name); |
| 1100 | if (error) |
| 1101 | sysfs_remove_link(&dev->kobj, "device"); |
| 1102 | } |
| 1103 | else |
| 1104 | error = 0; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1105 | out: |
| 1106 | kfree(class_name); |
| 1107 | return error; |
| 1108 | #else |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 1109 | if (old_parent) |
| 1110 | sysfs_remove_link(&dev->kobj, "device"); |
| 1111 | if (new_parent) |
| 1112 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, |
| 1113 | "device"); |
| 1114 | return error; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1115 | #endif |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * device_move - moves a device to a new parent |
| 1120 | * @dev: the pointer to the struct device to be moved |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1121 | * @new_parent: the new parent of the device (can by NULL) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1122 | */ |
| 1123 | int device_move(struct device *dev, struct device *new_parent) |
| 1124 | { |
| 1125 | int error; |
| 1126 | struct device *old_parent; |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1127 | struct kobject *new_parent_kobj; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1128 | |
| 1129 | dev = get_device(dev); |
| 1130 | if (!dev) |
| 1131 | return -EINVAL; |
| 1132 | |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1133 | new_parent = get_device(new_parent); |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1134 | new_parent_kobj = get_device_parent (dev, new_parent); |
| 1135 | if (IS_ERR(new_parent_kobj)) { |
| 1136 | error = PTR_ERR(new_parent_kobj); |
| 1137 | put_device(new_parent); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1138 | goto out; |
| 1139 | } |
| 1140 | pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id, |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1141 | new_parent ? new_parent->bus_id : "<NULL>"); |
| 1142 | error = kobject_move(&dev->kobj, new_parent_kobj); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1143 | if (error) { |
| 1144 | put_device(new_parent); |
| 1145 | goto out; |
| 1146 | } |
| 1147 | old_parent = dev->parent; |
| 1148 | dev->parent = new_parent; |
| 1149 | if (old_parent) |
Cornelia Huck | acf02d2 | 2006-11-22 17:49:39 +0100 | [diff] [blame] | 1150 | klist_remove(&dev->knode_parent); |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1151 | if (new_parent) |
| 1152 | klist_add_tail(&dev->knode_parent, &new_parent->klist_children); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1153 | if (!dev->class) |
| 1154 | goto out_put; |
| 1155 | error = device_move_class_links(dev, old_parent, new_parent); |
| 1156 | if (error) { |
| 1157 | /* We ignore errors on cleanup since we're hosed anyway... */ |
| 1158 | device_move_class_links(dev, new_parent, old_parent); |
| 1159 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1160 | if (new_parent) |
| 1161 | klist_remove(&dev->knode_parent); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1162 | if (old_parent) |
| 1163 | klist_add_tail(&dev->knode_parent, |
| 1164 | &old_parent->klist_children); |
| 1165 | } |
| 1166 | put_device(new_parent); |
| 1167 | goto out; |
| 1168 | } |
| 1169 | out_put: |
| 1170 | put_device(old_parent); |
| 1171 | out: |
| 1172 | put_device(dev); |
| 1173 | return error; |
| 1174 | } |
| 1175 | |
| 1176 | EXPORT_SYMBOL_GPL(device_move); |