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