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> |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 21 | #include <linux/genhd.h> |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 22 | #include <linux/kallsyms.h> |
Matthew Wilcox | 6188e10 | 2008-04-18 22:21:05 -0400 | [diff] [blame] | 23 | #include <linux/semaphore.h> |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Shaohua Li | 401097e | 2009-05-12 13:37:57 -0700 | [diff] [blame] | 25 | #include <linux/async.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include "base.h" |
| 28 | #include "power/power.h" |
| 29 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 30 | int (*platform_notify)(struct device *dev) = NULL; |
| 31 | int (*platform_notify_remove)(struct device *dev) = NULL; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 32 | static struct kobject *dev_kobj; |
| 33 | struct kobject *sysfs_dev_char_kobj; |
| 34 | struct kobject *sysfs_dev_block_kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 36 | #ifdef CONFIG_BLOCK |
| 37 | static inline int device_is_not_partition(struct device *dev) |
| 38 | { |
| 39 | return !(dev->type == &part_type); |
| 40 | } |
| 41 | #else |
| 42 | static inline int device_is_not_partition(struct device *dev) |
| 43 | { |
| 44 | return 1; |
| 45 | } |
| 46 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 48 | /** |
| 49 | * dev_driver_string - Return a device's driver name, if at all possible |
| 50 | * @dev: struct device to get the name of |
| 51 | * |
| 52 | * Will return the device's driver's name if it is bound to a device. If |
| 53 | * the device is not bound to a device, it will return the name of the bus |
| 54 | * it is attached to. If it is not attached to a bus either, an empty |
| 55 | * string will be returned. |
| 56 | */ |
Jean Delvare | bf9ca69 | 2008-07-30 12:29:21 -0700 | [diff] [blame] | 57 | const char *dev_driver_string(const struct device *dev) |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 58 | { |
Alan Stern | 3589972 | 2009-12-04 11:06:57 -0500 | [diff] [blame] | 59 | struct device_driver *drv; |
| 60 | |
| 61 | /* dev->driver can change to NULL underneath us because of unbinding, |
| 62 | * so be careful about accessing it. dev->bus and dev->class should |
| 63 | * never change once they are set, so they don't need special care. |
| 64 | */ |
| 65 | drv = ACCESS_ONCE(dev->driver); |
| 66 | return drv ? drv->name : |
Jean Delvare | a456b70 | 2007-03-09 16:33:10 +0100 | [diff] [blame] | 67 | (dev->bus ? dev->bus->name : |
| 68 | (dev->class ? dev->class->name : "")); |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 69 | } |
Matthew Wilcox | 310a922 | 2006-09-23 23:35:04 -0600 | [diff] [blame] | 70 | EXPORT_SYMBOL(dev_driver_string); |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #define to_dev(obj) container_of(obj, struct device, kobj) |
| 73 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
| 74 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 75 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
| 76 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 78 | struct device_attribute *dev_attr = to_dev_attr(attr); |
| 79 | struct device *dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 80 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | if (dev_attr->show) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 83 | ret = dev_attr->show(dev, dev_attr, buf); |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 84 | if (ret >= (ssize_t)PAGE_SIZE) { |
| 85 | print_symbol("dev_attr_show: %s returned bad count\n", |
| 86 | (unsigned long)dev_attr->show); |
| 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return ret; |
| 89 | } |
| 90 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 91 | static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 92 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 94 | struct device_attribute *dev_attr = to_dev_attr(attr); |
| 95 | struct device *dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 96 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | if (dev_attr->store) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 99 | ret = dev_attr->store(dev, dev_attr, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static struct sysfs_ops dev_sysfs_ops = { |
| 104 | .show = dev_attr_show, |
| 105 | .store = dev_attr_store, |
| 106 | }; |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * device_release - free device structure. |
| 111 | * @kobj: device's kobject. |
| 112 | * |
| 113 | * This is called once the reference count for the object |
| 114 | * reaches 0. We forward the call to the device's release |
| 115 | * method, which should handle actually freeing the structure. |
| 116 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 117 | static void device_release(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 119 | struct device *dev = to_dev(kobj); |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 120 | struct device_private *p = dev->p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | if (dev->release) |
| 123 | dev->release(dev); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 124 | else if (dev->type && dev->type->release) |
| 125 | dev->type->release(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 126 | else if (dev->class && dev->class->dev_release) |
| 127 | dev->class->dev_release(dev); |
Arjan van de Ven | f810a5c | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 128 | else |
| 129 | WARN(1, KERN_ERR "Device '%s' does not have a release() " |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 130 | "function, it is broken and must be fixed.\n", |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 131 | dev_name(dev)); |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 132 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Greg Kroah-Hartman | 8f4afc4 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 135 | static struct kobj_type device_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | .release = device_release, |
| 137 | .sysfs_ops = &dev_sysfs_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 141 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | struct kobj_type *ktype = get_ktype(kobj); |
| 144 | |
Greg Kroah-Hartman | 8f4afc4 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 145 | if (ktype == &device_ktype) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | struct device *dev = to_dev(kobj); |
| 147 | if (dev->bus) |
| 148 | return 1; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 149 | if (dev->class) |
| 150 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 155 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | struct device *dev = to_dev(kobj); |
| 158 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 159 | if (dev->bus) |
| 160 | return dev->bus->name; |
| 161 | if (dev->class) |
| 162 | return dev->class->name; |
| 163 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 166 | static int dev_uevent(struct kset *kset, struct kobject *kobj, |
| 167 | struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | struct device *dev = to_dev(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | int retval = 0; |
| 171 | |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 172 | /* add device node properties if present */ |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 173 | if (MAJOR(dev->devt)) { |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 174 | const char *tmp; |
| 175 | const char *name; |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 176 | mode_t mode = 0; |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 177 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 178 | add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); |
| 179 | add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 180 | name = device_get_devnode(dev, &mode, &tmp); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 181 | if (name) { |
| 182 | add_uevent_var(env, "DEVNAME=%s", name); |
| 183 | kfree(tmp); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 184 | if (mode) |
| 185 | add_uevent_var(env, "DEVMODE=%#o", mode & 0777); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 186 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Kay Sievers | 414264f | 2007-03-12 21:08:57 +0100 | [diff] [blame] | 189 | if (dev->type && dev->type->name) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 190 | add_uevent_var(env, "DEVTYPE=%s", dev->type->name); |
Kay Sievers | 414264f | 2007-03-12 21:08:57 +0100 | [diff] [blame] | 191 | |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 192 | if (dev->driver) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 193 | add_uevent_var(env, "DRIVER=%s", dev->driver->name); |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 194 | |
Kay Sievers | a87cb2a | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 195 | #ifdef CONFIG_SYSFS_DEPRECATED |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 196 | if (dev->class) { |
| 197 | struct device *parent = dev->parent; |
| 198 | |
| 199 | /* find first bus device in parent chain */ |
| 200 | while (parent && !parent->bus) |
| 201 | parent = parent->parent; |
| 202 | if (parent && parent->bus) { |
| 203 | const char *path; |
| 204 | |
| 205 | path = kobject_get_path(&parent->kobj, GFP_KERNEL); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 206 | if (path) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 207 | add_uevent_var(env, "PHYSDEVPATH=%s", path); |
Kay Sievers | 2c7afd1 | 2007-05-01 13:46:26 +0200 | [diff] [blame] | 208 | kfree(path); |
| 209 | } |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 210 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 211 | add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name); |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 212 | |
| 213 | if (parent->driver) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 214 | add_uevent_var(env, "PHYSDEVDRIVER=%s", |
| 215 | parent->driver->name); |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 216 | } |
| 217 | } else if (dev->bus) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 218 | add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 219 | |
| 220 | if (dev->driver) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 221 | add_uevent_var(env, "PHYSDEVDRIVER=%s", |
| 222 | dev->driver->name); |
Kay Sievers | d81d9d6 | 2006-08-13 06:17:09 +0200 | [diff] [blame] | 223 | } |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 224 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 226 | /* have the bus specific function add its stuff */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 227 | if (dev->bus && dev->bus->uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 228 | retval = dev->bus->uevent(dev, env); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 229 | if (retval) |
Greg Kroah-Hartman | 7dc72b2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 230 | pr_debug("device: '%s': %s: bus uevent() returned %d\n", |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 231 | dev_name(dev), __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 234 | /* have the class specific function add its stuff */ |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 235 | if (dev->class && dev->class->dev_uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 236 | retval = dev->class->dev_uevent(dev, env); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 237 | if (retval) |
Greg Kroah-Hartman | 7dc72b2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 238 | pr_debug("device: '%s': %s: class uevent() " |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 239 | "returned %d\n", dev_name(dev), |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 240 | __func__, retval); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 243 | /* have the device type specific fuction add its stuff */ |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 244 | if (dev->type && dev->type->uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 245 | retval = dev->type->uevent(dev, env); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 246 | if (retval) |
Greg Kroah-Hartman | 7dc72b2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 247 | pr_debug("device: '%s': %s: dev_type uevent() " |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 248 | "returned %d\n", dev_name(dev), |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 249 | __func__, retval); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return retval; |
| 253 | } |
| 254 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 255 | static struct kset_uevent_ops device_uevent_ops = { |
| 256 | .filter = dev_uevent_filter, |
| 257 | .name = dev_uevent_name, |
| 258 | .uevent = dev_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | }; |
| 260 | |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 261 | static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, |
| 262 | char *buf) |
| 263 | { |
| 264 | struct kobject *top_kobj; |
| 265 | struct kset *kset; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 266 | struct kobj_uevent_env *env = NULL; |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 267 | int i; |
| 268 | size_t count = 0; |
| 269 | int retval; |
| 270 | |
| 271 | /* search the kset, the device belongs to */ |
| 272 | top_kobj = &dev->kobj; |
Kay Sievers | 5c5daf6 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 273 | while (!top_kobj->kset && top_kobj->parent) |
| 274 | top_kobj = top_kobj->parent; |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 275 | if (!top_kobj->kset) |
| 276 | goto out; |
Kay Sievers | 5c5daf6 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 277 | |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 278 | kset = top_kobj->kset; |
| 279 | if (!kset->uevent_ops || !kset->uevent_ops->uevent) |
| 280 | goto out; |
| 281 | |
| 282 | /* respect filter */ |
| 283 | if (kset->uevent_ops && kset->uevent_ops->filter) |
| 284 | if (!kset->uevent_ops->filter(kset, &dev->kobj)) |
| 285 | goto out; |
| 286 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 287 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
| 288 | if (!env) |
Greg Kroah-Hartman | c7308c8 | 2007-05-02 14:14:11 +0200 | [diff] [blame] | 289 | return -ENOMEM; |
| 290 | |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 291 | /* let the kset specific function add its keys */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 292 | retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 293 | if (retval) |
| 294 | goto out; |
| 295 | |
| 296 | /* copy keys to file */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 297 | for (i = 0; i < env->envp_idx; i++) |
| 298 | count += sprintf(&buf[count], "%s\n", env->envp[i]); |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 299 | out: |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 300 | kfree(env); |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 301 | return count; |
| 302 | } |
| 303 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 304 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
| 305 | const char *buf, size_t count) |
| 306 | { |
Kay Sievers | 60a96a5 | 2007-07-08 22:29:26 +0200 | [diff] [blame] | 307 | enum kobject_action action; |
| 308 | |
Kay Sievers | 3f5468c | 2010-01-14 22:54:37 +0100 | [diff] [blame] | 309 | if (kobject_action_type(buf, count, &action) == 0) |
Kay Sievers | 60a96a5 | 2007-07-08 22:29:26 +0200 | [diff] [blame] | 310 | kobject_uevent(&dev->kobj, action); |
Kay Sievers | 3f5468c | 2010-01-14 22:54:37 +0100 | [diff] [blame] | 311 | else |
| 312 | dev_err(dev, "uevent: unknown action-string\n"); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 313 | return count; |
| 314 | } |
| 315 | |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 316 | static struct device_attribute uevent_attr = |
| 317 | __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); |
| 318 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 319 | static int device_add_attributes(struct device *dev, |
| 320 | struct device_attribute *attrs) |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 321 | { |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 322 | int error = 0; |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 323 | int i; |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 324 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 325 | if (attrs) { |
| 326 | for (i = 0; attr_name(attrs[i]); i++) { |
| 327 | error = device_create_file(dev, &attrs[i]); |
| 328 | if (error) |
| 329 | break; |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 330 | } |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 331 | if (error) |
| 332 | while (--i >= 0) |
| 333 | device_remove_file(dev, &attrs[i]); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 334 | } |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 335 | return error; |
| 336 | } |
| 337 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 338 | static void device_remove_attributes(struct device *dev, |
| 339 | struct device_attribute *attrs) |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 340 | { |
| 341 | int i; |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 342 | |
| 343 | if (attrs) |
| 344 | for (i = 0; attr_name(attrs[i]); i++) |
| 345 | device_remove_file(dev, &attrs[i]); |
| 346 | } |
| 347 | |
| 348 | static int device_add_groups(struct device *dev, |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 349 | const struct attribute_group **groups) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 350 | { |
| 351 | int error = 0; |
| 352 | int i; |
| 353 | |
| 354 | if (groups) { |
| 355 | for (i = 0; groups[i]; i++) { |
| 356 | error = sysfs_create_group(&dev->kobj, groups[i]); |
| 357 | if (error) { |
| 358 | while (--i >= 0) |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 359 | sysfs_remove_group(&dev->kobj, |
| 360 | groups[i]); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 361 | break; |
| 362 | } |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 365 | return error; |
| 366 | } |
| 367 | |
| 368 | static void device_remove_groups(struct device *dev, |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 369 | const struct attribute_group **groups) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 370 | { |
| 371 | int i; |
| 372 | |
| 373 | if (groups) |
| 374 | for (i = 0; groups[i]; i++) |
| 375 | sysfs_remove_group(&dev->kobj, groups[i]); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 378 | static int device_add_attrs(struct device *dev) |
| 379 | { |
| 380 | struct class *class = dev->class; |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 381 | struct device_type *type = dev->type; |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 382 | int error; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 383 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 384 | if (class) { |
| 385 | error = device_add_attributes(dev, class->dev_attrs); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 386 | if (error) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 387 | return error; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 388 | } |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 389 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 390 | if (type) { |
| 391 | error = device_add_groups(dev, type->groups); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 392 | if (error) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 393 | goto err_remove_class_attrs; |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 396 | error = device_add_groups(dev, dev->groups); |
| 397 | if (error) |
| 398 | goto err_remove_type_groups; |
| 399 | |
| 400 | return 0; |
| 401 | |
| 402 | err_remove_type_groups: |
| 403 | if (type) |
| 404 | device_remove_groups(dev, type->groups); |
| 405 | err_remove_class_attrs: |
| 406 | if (class) |
| 407 | device_remove_attributes(dev, class->dev_attrs); |
| 408 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 409 | return error; |
| 410 | } |
| 411 | |
| 412 | static void device_remove_attrs(struct device *dev) |
| 413 | { |
| 414 | struct class *class = dev->class; |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 415 | struct device_type *type = dev->type; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 416 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 417 | device_remove_groups(dev, dev->groups); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 418 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 419 | if (type) |
| 420 | device_remove_groups(dev, type->groups); |
| 421 | |
| 422 | if (class) |
| 423 | device_remove_attributes(dev, class->dev_attrs); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 427 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
| 428 | char *buf) |
| 429 | { |
| 430 | return print_dev_t(buf, dev->devt); |
| 431 | } |
| 432 | |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 433 | static struct device_attribute devt_attr = |
| 434 | __ATTR(dev, S_IRUGO, show_dev, NULL); |
| 435 | |
Greg Kroah-Hartman | 881c6cf | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 436 | /* kset to create /sys/devices/ */ |
| 437 | struct kset *devices_kset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 440 | * device_create_file - create sysfs attribute file for device. |
| 441 | * @dev: device. |
| 442 | * @attr: device attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | */ |
Phil Carmody | 26579ab | 2009-12-18 15:34:19 +0200 | [diff] [blame] | 444 | int device_create_file(struct device *dev, |
| 445 | const struct device_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
| 447 | int error = 0; |
Cornelia Huck | 0c98b19 | 2008-01-31 10:39:38 +0100 | [diff] [blame] | 448 | if (dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | return error; |
| 451 | } |
| 452 | |
| 453 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 454 | * device_remove_file - remove sysfs attribute file. |
| 455 | * @dev: device. |
| 456 | * @attr: device attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | */ |
Phil Carmody | 26579ab | 2009-12-18 15:34:19 +0200 | [diff] [blame] | 458 | void device_remove_file(struct device *dev, |
| 459 | const struct device_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
Cornelia Huck | 0c98b19 | 2008-01-31 10:39:38 +0100 | [diff] [blame] | 461 | if (dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | sysfs_remove_file(&dev->kobj, &attr->attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 465 | /** |
| 466 | * device_create_bin_file - create sysfs binary attribute file for device. |
| 467 | * @dev: device. |
| 468 | * @attr: device binary attribute descriptor. |
| 469 | */ |
Phil Carmody | 66ecb92 | 2009-12-18 15:34:20 +0200 | [diff] [blame] | 470 | int device_create_bin_file(struct device *dev, |
| 471 | const struct bin_attribute *attr) |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 472 | { |
| 473 | int error = -EINVAL; |
| 474 | if (dev) |
| 475 | error = sysfs_create_bin_file(&dev->kobj, attr); |
| 476 | return error; |
| 477 | } |
| 478 | EXPORT_SYMBOL_GPL(device_create_bin_file); |
| 479 | |
| 480 | /** |
| 481 | * device_remove_bin_file - remove sysfs binary attribute file |
| 482 | * @dev: device. |
| 483 | * @attr: device binary attribute descriptor. |
| 484 | */ |
Phil Carmody | 66ecb92 | 2009-12-18 15:34:20 +0200 | [diff] [blame] | 485 | void device_remove_bin_file(struct device *dev, |
| 486 | const struct bin_attribute *attr) |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 487 | { |
| 488 | if (dev) |
| 489 | sysfs_remove_bin_file(&dev->kobj, attr); |
| 490 | } |
| 491 | EXPORT_SYMBOL_GPL(device_remove_bin_file); |
| 492 | |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 493 | /** |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 494 | * device_schedule_callback_owner - helper to schedule a callback for a device |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 495 | * @dev: device. |
| 496 | * @func: callback function to invoke later. |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 497 | * @owner: module owning the callback routine |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 498 | * |
| 499 | * Attribute methods must not unregister themselves or their parent device |
| 500 | * (which would amount to the same thing). Attempts to do so will deadlock, |
| 501 | * since unregistration is mutually exclusive with driver callbacks. |
| 502 | * |
| 503 | * Instead methods can call this routine, which will attempt to allocate |
| 504 | * and schedule a workqueue request to call back @func with @dev as its |
| 505 | * argument in the workqueue's process context. @dev will be pinned until |
| 506 | * @func returns. |
| 507 | * |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 508 | * This routine is usually called via the inline device_schedule_callback(), |
| 509 | * which automatically sets @owner to THIS_MODULE. |
| 510 | * |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 511 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 512 | * be allocated, -ENODEV if a reference to @owner isn't available. |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 513 | * |
| 514 | * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an |
| 515 | * underlying sysfs routine (since it is intended for use by attribute |
| 516 | * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. |
| 517 | */ |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 518 | int device_schedule_callback_owner(struct device *dev, |
| 519 | void (*func)(struct device *), struct module *owner) |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 520 | { |
| 521 | return sysfs_schedule_callback(&dev->kobj, |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 522 | (void (*)(void *)) func, dev, owner); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 523 | } |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 524 | EXPORT_SYMBOL_GPL(device_schedule_callback_owner); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 525 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 526 | static void klist_children_get(struct klist_node *n) |
| 527 | { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 528 | struct device_private *p = to_device_private_parent(n); |
| 529 | struct device *dev = p->device; |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 530 | |
| 531 | get_device(dev); |
| 532 | } |
| 533 | |
| 534 | static void klist_children_put(struct klist_node *n) |
| 535 | { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 536 | struct device_private *p = to_device_private_parent(n); |
| 537 | struct device *dev = p->device; |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 538 | |
| 539 | put_device(dev); |
| 540 | } |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 543 | * device_initialize - init device structure. |
| 544 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 546 | * This prepares the device for use by other layers by initializing |
| 547 | * its fields. |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 548 | * It is the first half of device_register(), if called by |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 549 | * that function, though it can also be called separately, so one |
| 550 | * may use @dev's fields. In particular, get_device()/put_device() |
| 551 | * may be used for reference counting of @dev after calling this |
| 552 | * function. |
| 553 | * |
| 554 | * NOTE: Use put_device() to give up your reference instead of freeing |
| 555 | * @dev directly once you have called this function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | void device_initialize(struct device *dev) |
| 558 | { |
Greg Kroah-Hartman | 881c6cf | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 559 | dev->kobj.kset = devices_kset; |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 560 | kobject_init(&dev->kobj, &device_ktype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | INIT_LIST_HEAD(&dev->dma_pools); |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 562 | init_MUTEX(&dev->sem); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 563 | spin_lock_init(&dev->devres_lock); |
| 564 | INIT_LIST_HEAD(&dev->devres_head); |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 565 | device_init_wakeup(dev, 0); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 566 | device_pm_init(dev); |
Christoph Hellwig | 8734813 | 2006-12-06 20:32:33 -0800 | [diff] [blame] | 567 | set_dev_node(dev, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 570 | #ifdef CONFIG_SYSFS_DEPRECATED |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 571 | static struct kobject *get_device_parent(struct device *dev, |
| 572 | struct device *parent) |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 573 | { |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 574 | /* class devices without a parent live in /sys/class/<classname>/ */ |
Dmitry Torokhov | 3eb215d | 2007-10-07 12:22:21 -0400 | [diff] [blame] | 575 | if (dev->class && (!parent || parent->class != dev->class)) |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 576 | return &dev->class->p->class_subsys.kobj; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 577 | /* all other devices keep their parent */ |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 578 | else if (parent) |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 579 | return &parent->kobj; |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 580 | |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 581 | return NULL; |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 582 | } |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 583 | |
| 584 | static inline void cleanup_device_parent(struct device *dev) {} |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 585 | static inline void cleanup_glue_dir(struct device *dev, |
| 586 | struct kobject *glue_dir) {} |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 587 | #else |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 588 | static struct kobject *virtual_device_parent(struct device *dev) |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 589 | { |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 590 | static struct kobject *virtual_dir = NULL; |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 591 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 592 | if (!virtual_dir) |
Greg Kroah-Hartman | 4ff6abf | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 593 | virtual_dir = kobject_create_and_add("virtual", |
Greg Kroah-Hartman | 881c6cf | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 594 | &devices_kset->kobj); |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 595 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 596 | return virtual_dir; |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 599 | static struct kobject *get_device_parent(struct device *dev, |
| 600 | struct device *parent) |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 601 | { |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 602 | int retval; |
| 603 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 604 | if (dev->class) { |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 605 | static DEFINE_MUTEX(gdp_mutex); |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 606 | struct kobject *kobj = NULL; |
| 607 | struct kobject *parent_kobj; |
| 608 | struct kobject *k; |
| 609 | |
| 610 | /* |
| 611 | * If we have no parent, we live in "virtual". |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 612 | * Class-devices with a non class-device as parent, live |
| 613 | * in a "glue" directory to prevent namespace collisions. |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 614 | */ |
| 615 | if (parent == NULL) |
| 616 | parent_kobj = virtual_device_parent(dev); |
| 617 | else if (parent->class) |
| 618 | return &parent->kobj; |
| 619 | else |
| 620 | parent_kobj = &parent->kobj; |
| 621 | |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 622 | mutex_lock(&gdp_mutex); |
| 623 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 624 | /* find our class-directory at the parent and reference it */ |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 625 | spin_lock(&dev->class->p->class_dirs.list_lock); |
| 626 | list_for_each_entry(k, &dev->class->p->class_dirs.list, entry) |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 627 | if (k->parent == parent_kobj) { |
| 628 | kobj = kobject_get(k); |
| 629 | break; |
| 630 | } |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 631 | spin_unlock(&dev->class->p->class_dirs.list_lock); |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 632 | if (kobj) { |
| 633 | mutex_unlock(&gdp_mutex); |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 634 | return kobj; |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 635 | } |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 636 | |
| 637 | /* or create a new class-directory at the parent device */ |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 638 | k = kobject_create(); |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 639 | if (!k) { |
| 640 | mutex_unlock(&gdp_mutex); |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 641 | return NULL; |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 642 | } |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 643 | k->kset = &dev->class->p->class_dirs; |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 644 | retval = kobject_add(k, parent_kobj, "%s", dev->class->name); |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 645 | if (retval < 0) { |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 646 | mutex_unlock(&gdp_mutex); |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 647 | kobject_put(k); |
| 648 | return NULL; |
| 649 | } |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 650 | /* do not emit an uevent for this simple "glue" directory */ |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 651 | mutex_unlock(&gdp_mutex); |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 652 | return k; |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | if (parent) |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 656 | return &parent->kobj; |
| 657 | return NULL; |
| 658 | } |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 659 | |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 660 | static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 661 | { |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 662 | /* see if we live in a "glue" directory */ |
Cornelia Huck | c1fe539 | 2008-02-27 15:38:23 +0100 | [diff] [blame] | 663 | if (!glue_dir || !dev->class || |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 664 | glue_dir->kset != &dev->class->p->class_dirs) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 665 | return; |
| 666 | |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 667 | kobject_put(glue_dir); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 668 | } |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 669 | |
| 670 | static void cleanup_device_parent(struct device *dev) |
| 671 | { |
| 672 | cleanup_glue_dir(dev, dev->kobj.parent); |
| 673 | } |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 674 | #endif |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 675 | |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 676 | static void setup_parent(struct device *dev, struct device *parent) |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 677 | { |
| 678 | struct kobject *kobj; |
| 679 | kobj = get_device_parent(dev, parent); |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 680 | if (kobj) |
| 681 | dev->kobj.parent = kobj; |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 682 | } |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 683 | |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 684 | static int device_add_class_symlinks(struct device *dev) |
| 685 | { |
| 686 | int error; |
| 687 | |
| 688 | if (!dev->class) |
| 689 | return 0; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 690 | |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 691 | error = sysfs_create_link(&dev->kobj, |
| 692 | &dev->class->p->class_subsys.kobj, |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 693 | "subsystem"); |
| 694 | if (error) |
| 695 | goto out; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 696 | |
| 697 | #ifdef CONFIG_SYSFS_DEPRECATED |
| 698 | /* stacked class devices need a symlink in the class directory */ |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 699 | if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 700 | device_is_not_partition(dev)) { |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 701 | error = sysfs_create_link(&dev->class->p->class_subsys.kobj, |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 702 | &dev->kobj, dev_name(dev)); |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 703 | if (error) |
| 704 | goto out_subsys; |
| 705 | } |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 706 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 707 | if (dev->parent && device_is_not_partition(dev)) { |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 708 | struct device *parent = dev->parent; |
| 709 | char *class_name; |
Dmitry Torokhov | 4f01a75 | 2007-09-18 22:46:50 -0700 | [diff] [blame] | 710 | |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 711 | /* |
| 712 | * stacked class devices have the 'device' link |
| 713 | * pointing to the bus device instead of the parent |
| 714 | */ |
| 715 | while (parent->class && !parent->bus && parent->parent) |
| 716 | parent = parent->parent; |
Dmitry Torokhov | 4f01a75 | 2007-09-18 22:46:50 -0700 | [diff] [blame] | 717 | |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 718 | error = sysfs_create_link(&dev->kobj, |
| 719 | &parent->kobj, |
| 720 | "device"); |
| 721 | if (error) |
| 722 | goto out_busid; |
Dmitry Torokhov | 4f01a75 | 2007-09-18 22:46:50 -0700 | [diff] [blame] | 723 | |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 724 | class_name = make_class_name(dev->class->name, |
| 725 | &dev->kobj); |
| 726 | if (class_name) |
| 727 | error = sysfs_create_link(&dev->parent->kobj, |
| 728 | &dev->kobj, class_name); |
| 729 | kfree(class_name); |
| 730 | if (error) |
| 731 | goto out_device; |
| 732 | } |
| 733 | return 0; |
| 734 | |
| 735 | out_device: |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 736 | if (dev->parent && device_is_not_partition(dev)) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 737 | sysfs_remove_link(&dev->kobj, "device"); |
| 738 | out_busid: |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 739 | if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 740 | device_is_not_partition(dev)) |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 741 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 742 | dev_name(dev)); |
Dmitry Torokhov | 4f01a75 | 2007-09-18 22:46:50 -0700 | [diff] [blame] | 743 | #else |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 744 | /* link in the class directory pointing to the device */ |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 745 | error = sysfs_create_link(&dev->class->p->class_subsys.kobj, |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 746 | &dev->kobj, dev_name(dev)); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 747 | if (error) |
| 748 | goto out_subsys; |
| 749 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 750 | if (dev->parent && device_is_not_partition(dev)) { |
Dmitry Torokhov | 4f01a75 | 2007-09-18 22:46:50 -0700 | [diff] [blame] | 751 | error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
| 752 | "device"); |
| 753 | if (error) |
| 754 | goto out_busid; |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 755 | } |
| 756 | return 0; |
| 757 | |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 758 | out_busid: |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 759 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 760 | #endif |
| 761 | |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 762 | out_subsys: |
| 763 | sysfs_remove_link(&dev->kobj, "subsystem"); |
| 764 | out: |
| 765 | return error; |
| 766 | } |
| 767 | |
| 768 | static void device_remove_class_symlinks(struct device *dev) |
| 769 | { |
| 770 | if (!dev->class) |
| 771 | return; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 772 | |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 773 | #ifdef CONFIG_SYSFS_DEPRECATED |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 774 | if (dev->parent && device_is_not_partition(dev)) { |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 775 | char *class_name; |
| 776 | |
| 777 | class_name = make_class_name(dev->class->name, &dev->kobj); |
| 778 | if (class_name) { |
| 779 | sysfs_remove_link(&dev->parent->kobj, class_name); |
| 780 | kfree(class_name); |
| 781 | } |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 782 | sysfs_remove_link(&dev->kobj, "device"); |
| 783 | } |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 784 | |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 785 | if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 786 | device_is_not_partition(dev)) |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 787 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 788 | dev_name(dev)); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 789 | #else |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 790 | if (dev->parent && device_is_not_partition(dev)) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 791 | sysfs_remove_link(&dev->kobj, "device"); |
| 792 | |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 793 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 794 | #endif |
| 795 | |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 796 | sysfs_remove_link(&dev->kobj, "subsystem"); |
| 797 | } |
| 798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | /** |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 800 | * dev_set_name - set a device name |
| 801 | * @dev: device |
Randy Dunlap | 4623236 | 2008-06-04 21:40:43 -0700 | [diff] [blame] | 802 | * @fmt: format string for the device's name |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 803 | */ |
| 804 | int dev_set_name(struct device *dev, const char *fmt, ...) |
| 805 | { |
| 806 | va_list vargs; |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 807 | int err; |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 808 | |
| 809 | va_start(vargs, fmt); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 810 | err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 811 | va_end(vargs); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 812 | return err; |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 813 | } |
| 814 | EXPORT_SYMBOL_GPL(dev_set_name); |
| 815 | |
| 816 | /** |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 817 | * device_to_dev_kobj - select a /sys/dev/ directory for the device |
| 818 | * @dev: device |
| 819 | * |
| 820 | * By default we select char/ for new entries. Setting class->dev_obj |
| 821 | * to NULL prevents an entry from being created. class->dev_kobj must |
| 822 | * be set (or cleared) before any devices are registered to the class |
| 823 | * otherwise device_create_sys_dev_entry() and |
| 824 | * device_remove_sys_dev_entry() will disagree about the the presence |
| 825 | * of the link. |
| 826 | */ |
| 827 | static struct kobject *device_to_dev_kobj(struct device *dev) |
| 828 | { |
| 829 | struct kobject *kobj; |
| 830 | |
| 831 | if (dev->class) |
| 832 | kobj = dev->class->dev_kobj; |
| 833 | else |
| 834 | kobj = sysfs_dev_char_kobj; |
| 835 | |
| 836 | return kobj; |
| 837 | } |
| 838 | |
| 839 | static int device_create_sys_dev_entry(struct device *dev) |
| 840 | { |
| 841 | struct kobject *kobj = device_to_dev_kobj(dev); |
| 842 | int error = 0; |
| 843 | char devt_str[15]; |
| 844 | |
| 845 | if (kobj) { |
| 846 | format_dev_t(devt_str, dev->devt); |
| 847 | error = sysfs_create_link(kobj, &dev->kobj, devt_str); |
| 848 | } |
| 849 | |
| 850 | return error; |
| 851 | } |
| 852 | |
| 853 | static void device_remove_sys_dev_entry(struct device *dev) |
| 854 | { |
| 855 | struct kobject *kobj = device_to_dev_kobj(dev); |
| 856 | char devt_str[15]; |
| 857 | |
| 858 | if (kobj) { |
| 859 | format_dev_t(devt_str, dev->devt); |
| 860 | sysfs_remove_link(kobj, devt_str); |
| 861 | } |
| 862 | } |
| 863 | |
Greg Kroah-Hartman | b402843 | 2009-05-11 14:16:57 -0700 | [diff] [blame] | 864 | int device_private_init(struct device *dev) |
| 865 | { |
| 866 | dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); |
| 867 | if (!dev->p) |
| 868 | return -ENOMEM; |
| 869 | dev->p->device = dev; |
| 870 | klist_init(&dev->p->klist_children, klist_children_get, |
| 871 | klist_children_put); |
| 872 | return 0; |
| 873 | } |
| 874 | |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 875 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 876 | * device_add - add device to device hierarchy. |
| 877 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 879 | * This is part 2 of device_register(), though may be called |
| 880 | * separately _iff_ device_initialize() has been called separately. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 882 | * This adds @dev to the kobject hierarchy via kobject_add(), adds it |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 883 | * to the global and sibling lists for the device, then |
| 884 | * adds it to the other relevant subsystems of the driver model. |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 885 | * |
| 886 | * NOTE: _Never_ directly free @dev after calling this function, even |
| 887 | * if it returned an error! Always use put_device() to give up your |
| 888 | * reference instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | */ |
| 890 | int device_add(struct device *dev) |
| 891 | { |
| 892 | struct device *parent = NULL; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 893 | struct class_interface *class_intf; |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 894 | int error = -EINVAL; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | dev = get_device(dev); |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 897 | if (!dev) |
| 898 | goto done; |
| 899 | |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 900 | if (!dev->p) { |
Greg Kroah-Hartman | b402843 | 2009-05-11 14:16:57 -0700 | [diff] [blame] | 901 | error = device_private_init(dev); |
| 902 | if (error) |
| 903 | goto done; |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 904 | } |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 905 | |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 906 | /* |
| 907 | * for statically allocated devices, which should all be converted |
| 908 | * some day, we need to initialize the name. We prevent reading back |
| 909 | * the name, and force the use of dev_name() |
| 910 | */ |
| 911 | if (dev->init_name) { |
Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 912 | dev_set_name(dev, "%s", dev->init_name); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 913 | dev->init_name = NULL; |
| 914 | } |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 915 | |
Thomas Gleixner | e6309e7 | 2009-12-10 19:32:49 +0000 | [diff] [blame] | 916 | if (!dev_name(dev)) { |
| 917 | error = -EINVAL; |
Kay Sievers | 5c8563d | 2009-05-28 14:24:07 -0700 | [diff] [blame] | 918 | goto name_error; |
Thomas Gleixner | e6309e7 | 2009-12-10 19:32:49 +0000 | [diff] [blame] | 919 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 921 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Greg Kroah-Hartman | c205ef4 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | parent = get_device(dev->parent); |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 924 | setup_parent(dev, parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 926 | /* use parent numa_node */ |
| 927 | if (parent) |
| 928 | set_dev_node(dev, dev_to_node(parent)); |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | /* first, register with generic layer. */ |
Kay Sievers | 8a577ff | 2009-04-18 15:05:45 -0700 | [diff] [blame] | 931 | /* we require the name to be set before, and pass NULL */ |
| 932 | error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 933 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | goto Error; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 935 | |
Brian Walsh | 3702264 | 2006-08-14 22:43:19 -0700 | [diff] [blame] | 936 | /* notify platform of device entry */ |
| 937 | if (platform_notify) |
| 938 | platform_notify(dev); |
| 939 | |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 940 | error = device_create_file(dev, &uevent_attr); |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 941 | if (error) |
| 942 | goto attrError; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 943 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 944 | if (MAJOR(dev->devt)) { |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 945 | error = device_create_file(dev, &devt_attr); |
| 946 | if (error) |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 947 | goto ueventattrError; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 948 | |
| 949 | error = device_create_sys_dev_entry(dev); |
| 950 | if (error) |
| 951 | goto devtattrError; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 952 | |
| 953 | devtmpfs_create_node(dev); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 956 | error = device_add_class_symlinks(dev); |
| 957 | if (error) |
| 958 | goto SymlinkError; |
Cornelia Huck | dc0afa8 | 2007-07-09 11:39:18 -0700 | [diff] [blame] | 959 | error = device_add_attrs(dev); |
| 960 | if (error) |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 961 | goto AttrsError; |
Cornelia Huck | dc0afa8 | 2007-07-09 11:39:18 -0700 | [diff] [blame] | 962 | error = bus_add_device(dev); |
| 963 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | goto BusError; |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 965 | error = dpm_sysfs_add(dev); |
Rafael J. Wysocki | 57eee3d | 2008-03-12 00:59:38 +0100 | [diff] [blame] | 966 | if (error) |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 967 | goto DPMError; |
| 968 | device_pm_add(dev); |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 969 | |
| 970 | /* Notify clients of device addition. This call must come |
| 971 | * after dpm_sysf_add() and before kobject_uevent(). |
| 972 | */ |
| 973 | if (dev->bus) |
| 974 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
| 975 | BUS_NOTIFY_ADD_DEVICE, dev); |
| 976 | |
Cornelia Huck | 83b5fb4c | 2007-03-29 11:12:11 +0200 | [diff] [blame] | 977 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Alan Stern | 2023c61 | 2009-07-30 15:27:18 -0400 | [diff] [blame] | 978 | bus_probe_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | if (parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 980 | klist_add_tail(&dev->p->knode_parent, |
| 981 | &parent->p->klist_children); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 983 | if (dev->class) { |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 984 | mutex_lock(&dev->class->p->class_mutex); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 985 | /* tie the class to the device */ |
Tejun Heo | 5a3ceb8 | 2008-08-25 19:50:19 +0200 | [diff] [blame] | 986 | klist_add_tail(&dev->knode_class, |
| 987 | &dev->class->p->class_devices); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 988 | |
| 989 | /* notify any interfaces that the device is here */ |
Greg Kroah-Hartman | 184f1f7 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 990 | list_for_each_entry(class_intf, |
| 991 | &dev->class->p->class_interfaces, node) |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 992 | if (class_intf->add_dev) |
| 993 | class_intf->add_dev(dev, class_intf); |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 994 | mutex_unlock(&dev->class->p->class_mutex); |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 995 | } |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 996 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | put_device(dev); |
| 998 | return error; |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 999 | DPMError: |
Rafael J. Wysocki | 57eee3d | 2008-03-12 00:59:38 +0100 | [diff] [blame] | 1000 | bus_remove_device(dev); |
| 1001 | BusError: |
James Simmons | 82f0cf9 | 2007-02-21 17:44:51 +0000 | [diff] [blame] | 1002 | device_remove_attrs(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1003 | AttrsError: |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 1004 | device_remove_class_symlinks(dev); |
| 1005 | SymlinkError: |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 1006 | if (MAJOR(dev->devt)) |
Kay Sievers | ad72956 | 2009-10-28 19:51:37 +0100 | [diff] [blame] | 1007 | devtmpfs_delete_node(dev); |
| 1008 | if (MAJOR(dev->devt)) |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 1009 | device_remove_sys_dev_entry(dev); |
| 1010 | devtattrError: |
| 1011 | if (MAJOR(dev->devt)) |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 1012 | device_remove_file(dev, &devt_attr); |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 1013 | ueventattrError: |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 1014 | device_remove_file(dev, &uevent_attr); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1015 | attrError: |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1016 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | kobject_del(&dev->kobj); |
| 1018 | Error: |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 1019 | cleanup_device_parent(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (parent) |
| 1021 | put_device(parent); |
Kay Sievers | 5c8563d | 2009-05-28 14:24:07 -0700 | [diff] [blame] | 1022 | name_error: |
| 1023 | kfree(dev->p); |
| 1024 | dev->p = NULL; |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 1025 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1029 | * device_register - register a device with the system. |
| 1030 | * @dev: pointer to the device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1032 | * This happens in two clean steps - initialize the device |
| 1033 | * and add it to the system. The two steps can be called |
| 1034 | * separately, but this is the easiest and most common. |
| 1035 | * I.e. you should only call the two helpers separately if |
| 1036 | * have a clearly defined need to use and refcount the device |
| 1037 | * before it is added to the hierarchy. |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 1038 | * |
| 1039 | * NOTE: _Never_ directly free @dev after calling this function, even |
| 1040 | * if it returned an error! Always use put_device() to give up the |
| 1041 | * reference initialized in this function instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | int device_register(struct device *dev) |
| 1044 | { |
| 1045 | device_initialize(dev); |
| 1046 | return device_add(dev); |
| 1047 | } |
| 1048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1050 | * get_device - increment reference count for device. |
| 1051 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1053 | * This simply forwards the call to kobject_get(), though |
| 1054 | * we do take care to provide for the case that we get a NULL |
| 1055 | * pointer passed in. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1057 | struct device *get_device(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | { |
| 1059 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; |
| 1060 | } |
| 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1063 | * put_device - decrement reference count. |
| 1064 | * @dev: device in question. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1066 | void put_device(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1068 | /* might_sleep(); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | if (dev) |
| 1070 | kobject_put(&dev->kobj); |
| 1071 | } |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1074 | * device_del - delete device from system. |
| 1075 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1077 | * This is the first part of the device unregistration |
| 1078 | * sequence. This removes the device from the lists we control |
| 1079 | * from here, has it removed from the other driver model |
| 1080 | * subsystems it was added to in device_add(), and removes it |
| 1081 | * from the kobject hierarchy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1083 | * NOTE: this should be called manually _iff_ device_add() was |
| 1084 | * also called manually. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1086 | void device_del(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1088 | struct device *parent = dev->parent; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 1089 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 1091 | /* Notify clients of device removal. This call must come |
| 1092 | * before dpm_sysfs_remove(). |
| 1093 | */ |
| 1094 | if (dev->bus) |
| 1095 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
| 1096 | BUS_NOTIFY_DEL_DEVICE, dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1097 | device_pm_remove(dev); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 1098 | dpm_sysfs_remove(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | if (parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1100 | klist_del(&dev->p->knode_parent); |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 1101 | if (MAJOR(dev->devt)) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1102 | devtmpfs_delete_node(dev); |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 1103 | device_remove_sys_dev_entry(dev); |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 1104 | device_remove_file(dev, &devt_attr); |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 1105 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 1106 | if (dev->class) { |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 1107 | device_remove_class_symlinks(dev); |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 1108 | |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 1109 | mutex_lock(&dev->class->p->class_mutex); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 1110 | /* notify any interfaces that the device is now gone */ |
Greg Kroah-Hartman | 184f1f7 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 1111 | list_for_each_entry(class_intf, |
| 1112 | &dev->class->p->class_interfaces, node) |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 1113 | if (class_intf->remove_dev) |
| 1114 | class_intf->remove_dev(dev, class_intf); |
| 1115 | /* remove the device from the class list */ |
Tejun Heo | 5a3ceb8 | 2008-08-25 19:50:19 +0200 | [diff] [blame] | 1116 | klist_del(&dev->knode_class); |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 1117 | mutex_unlock(&dev->class->p->class_mutex); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 1118 | } |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 1119 | device_remove_file(dev, &uevent_attr); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1120 | device_remove_attrs(dev); |
Benjamin Herrenschmidt | 2895353 | 2006-11-08 19:46:14 -0800 | [diff] [blame] | 1121 | bus_remove_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
Tejun Heo | 2f8d16a | 2007-03-09 19:34:19 +0900 | [diff] [blame] | 1123 | /* |
| 1124 | * Some platform devices are driven without driver attached |
| 1125 | * and managed resources may have been acquired. Make sure |
| 1126 | * all resources are released. |
| 1127 | */ |
| 1128 | devres_release_all(dev); |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | /* Notify the platform of the removal, in case they |
| 1131 | * need to do anything... |
| 1132 | */ |
| 1133 | if (platform_notify_remove) |
| 1134 | platform_notify_remove(dev); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1135 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 1136 | cleanup_device_parent(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | kobject_del(&dev->kobj); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 1138 | put_device(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1142 | * device_unregister - unregister device from system. |
| 1143 | * @dev: device going away. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1145 | * We do this in two parts, like we do device_register(). First, |
| 1146 | * we remove it from all the subsystems with device_del(), then |
| 1147 | * we decrement the reference count via put_device(). If that |
| 1148 | * is the final reference count, the device will be cleaned up |
| 1149 | * via device_release() above. Otherwise, the structure will |
| 1150 | * stick around until the final reference to the device is dropped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1152 | void device_unregister(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | { |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 1154 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | device_del(dev); |
| 1156 | put_device(dev); |
| 1157 | } |
| 1158 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1159 | static struct device *next_device(struct klist_iter *i) |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 1160 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1161 | struct klist_node *n = klist_next(i); |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1162 | struct device *dev = NULL; |
| 1163 | struct device_private *p; |
| 1164 | |
| 1165 | if (n) { |
| 1166 | p = to_device_private_parent(n); |
| 1167 | dev = p->device; |
| 1168 | } |
| 1169 | return dev; |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | /** |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1173 | * device_get_devnode - path of device node file |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1174 | * @dev: device |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1175 | * @mode: returned file access mode |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1176 | * @tmp: possibly allocated string |
| 1177 | * |
| 1178 | * Return the relative path of a possible device node. |
| 1179 | * Non-default names may need to allocate a memory to compose |
| 1180 | * a name. This memory is returned in tmp and needs to be |
| 1181 | * freed by the caller. |
| 1182 | */ |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1183 | const char *device_get_devnode(struct device *dev, |
| 1184 | mode_t *mode, const char **tmp) |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1185 | { |
| 1186 | char *s; |
| 1187 | |
| 1188 | *tmp = NULL; |
| 1189 | |
| 1190 | /* the device type may provide a specific name */ |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1191 | if (dev->type && dev->type->devnode) |
| 1192 | *tmp = dev->type->devnode(dev, mode); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1193 | if (*tmp) |
| 1194 | return *tmp; |
| 1195 | |
| 1196 | /* the class may provide a specific name */ |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1197 | if (dev->class && dev->class->devnode) |
| 1198 | *tmp = dev->class->devnode(dev, mode); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1199 | if (*tmp) |
| 1200 | return *tmp; |
| 1201 | |
| 1202 | /* return name without allocation, tmp == NULL */ |
| 1203 | if (strchr(dev_name(dev), '!') == NULL) |
| 1204 | return dev_name(dev); |
| 1205 | |
| 1206 | /* replace '!' in the name with '/' */ |
| 1207 | *tmp = kstrdup(dev_name(dev), GFP_KERNEL); |
| 1208 | if (!*tmp) |
| 1209 | return NULL; |
| 1210 | while ((s = strchr(*tmp, '!'))) |
| 1211 | s[0] = '/'; |
| 1212 | return *tmp; |
| 1213 | } |
| 1214 | |
| 1215 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1216 | * device_for_each_child - device child iterator. |
| 1217 | * @parent: parent struct device. |
| 1218 | * @data: data for the callback. |
| 1219 | * @fn: function to be called for each device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1221 | * Iterate over @parent's child devices, and call @fn for each, |
| 1222 | * passing it @data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1224 | * We check the return of @fn each time. If it returns anything |
| 1225 | * other than 0, we break out and return that value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1227 | int device_for_each_child(struct device *parent, void *data, |
| 1228 | int (*fn)(struct device *dev, void *data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | { |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 1230 | struct klist_iter i; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1231 | struct device *child; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | int error = 0; |
| 1233 | |
Greg Kroah-Hartman | 014c90d | 2009-04-15 16:00:12 -0700 | [diff] [blame] | 1234 | if (!parent->p) |
| 1235 | return 0; |
| 1236 | |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1237 | klist_iter_init(&parent->p->klist_children, &i); |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 1238 | while ((child = next_device(&i)) && !error) |
| 1239 | error = fn(child, data); |
| 1240 | klist_iter_exit(&i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | return error; |
| 1242 | } |
| 1243 | |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 1244 | /** |
| 1245 | * device_find_child - device iterator for locating a particular device. |
| 1246 | * @parent: parent struct device |
| 1247 | * @data: Data to pass to match function |
| 1248 | * @match: Callback function to check device |
| 1249 | * |
| 1250 | * This is similar to the device_for_each_child() function above, but it |
| 1251 | * returns a reference to a device that is 'found' for later use, as |
| 1252 | * determined by the @match callback. |
| 1253 | * |
| 1254 | * The callback should return 0 if the device doesn't match and non-zero |
| 1255 | * if it does. If the callback returns non-zero and a reference to the |
| 1256 | * current device can be obtained, this function will return to the caller |
| 1257 | * and not iterate over any more devices. |
| 1258 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1259 | struct device *device_find_child(struct device *parent, void *data, |
| 1260 | int (*match)(struct device *dev, void *data)) |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 1261 | { |
| 1262 | struct klist_iter i; |
| 1263 | struct device *child; |
| 1264 | |
| 1265 | if (!parent) |
| 1266 | return NULL; |
| 1267 | |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1268 | klist_iter_init(&parent->p->klist_children, &i); |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 1269 | while ((child = next_device(&i))) |
| 1270 | if (match(child, data) && get_device(child)) |
| 1271 | break; |
| 1272 | klist_iter_exit(&i); |
| 1273 | return child; |
| 1274 | } |
| 1275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | int __init devices_init(void) |
| 1277 | { |
Greg Kroah-Hartman | 881c6cf | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 1278 | devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); |
| 1279 | if (!devices_kset) |
| 1280 | return -ENOMEM; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 1281 | dev_kobj = kobject_create_and_add("dev", NULL); |
| 1282 | if (!dev_kobj) |
| 1283 | goto dev_kobj_err; |
| 1284 | sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); |
| 1285 | if (!sysfs_dev_block_kobj) |
| 1286 | goto block_kobj_err; |
| 1287 | sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); |
| 1288 | if (!sysfs_dev_char_kobj) |
| 1289 | goto char_kobj_err; |
| 1290 | |
Greg Kroah-Hartman | 881c6cf | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 1291 | return 0; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 1292 | |
| 1293 | char_kobj_err: |
| 1294 | kobject_put(sysfs_dev_block_kobj); |
| 1295 | block_kobj_err: |
| 1296 | kobject_put(dev_kobj); |
| 1297 | dev_kobj_err: |
| 1298 | kset_unregister(devices_kset); |
| 1299 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | EXPORT_SYMBOL_GPL(device_for_each_child); |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 1303 | EXPORT_SYMBOL_GPL(device_find_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
| 1305 | EXPORT_SYMBOL_GPL(device_initialize); |
| 1306 | EXPORT_SYMBOL_GPL(device_add); |
| 1307 | EXPORT_SYMBOL_GPL(device_register); |
| 1308 | |
| 1309 | EXPORT_SYMBOL_GPL(device_del); |
| 1310 | EXPORT_SYMBOL_GPL(device_unregister); |
| 1311 | EXPORT_SYMBOL_GPL(get_device); |
| 1312 | EXPORT_SYMBOL_GPL(put_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
| 1314 | EXPORT_SYMBOL_GPL(device_create_file); |
| 1315 | EXPORT_SYMBOL_GPL(device_remove_file); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1316 | |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 1317 | struct root_device |
| 1318 | { |
| 1319 | struct device dev; |
| 1320 | struct module *owner; |
| 1321 | }; |
| 1322 | |
| 1323 | #define to_root_device(dev) container_of(dev, struct root_device, dev) |
| 1324 | |
| 1325 | static void root_device_release(struct device *dev) |
| 1326 | { |
| 1327 | kfree(to_root_device(dev)); |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * __root_device_register - allocate and register a root device |
| 1332 | * @name: root device name |
| 1333 | * @owner: owner module of the root device, usually THIS_MODULE |
| 1334 | * |
| 1335 | * This function allocates a root device and registers it |
| 1336 | * using device_register(). In order to free the returned |
| 1337 | * device, use root_device_unregister(). |
| 1338 | * |
| 1339 | * Root devices are dummy devices which allow other devices |
| 1340 | * to be grouped under /sys/devices. Use this function to |
| 1341 | * allocate a root device and then use it as the parent of |
| 1342 | * any device which should appear under /sys/devices/{name} |
| 1343 | * |
| 1344 | * The /sys/devices/{name} directory will also contain a |
| 1345 | * 'module' symlink which points to the @owner directory |
| 1346 | * in sysfs. |
| 1347 | * |
| 1348 | * Note: You probably want to use root_device_register(). |
| 1349 | */ |
| 1350 | struct device *__root_device_register(const char *name, struct module *owner) |
| 1351 | { |
| 1352 | struct root_device *root; |
| 1353 | int err = -ENOMEM; |
| 1354 | |
| 1355 | root = kzalloc(sizeof(struct root_device), GFP_KERNEL); |
| 1356 | if (!root) |
| 1357 | return ERR_PTR(err); |
| 1358 | |
Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 1359 | err = dev_set_name(&root->dev, "%s", name); |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 1360 | if (err) { |
| 1361 | kfree(root); |
| 1362 | return ERR_PTR(err); |
| 1363 | } |
| 1364 | |
| 1365 | root->dev.release = root_device_release; |
| 1366 | |
| 1367 | err = device_register(&root->dev); |
| 1368 | if (err) { |
| 1369 | put_device(&root->dev); |
| 1370 | return ERR_PTR(err); |
| 1371 | } |
| 1372 | |
| 1373 | #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */ |
| 1374 | if (owner) { |
| 1375 | struct module_kobject *mk = &owner->mkobj; |
| 1376 | |
| 1377 | err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); |
| 1378 | if (err) { |
| 1379 | device_unregister(&root->dev); |
| 1380 | return ERR_PTR(err); |
| 1381 | } |
| 1382 | root->owner = owner; |
| 1383 | } |
| 1384 | #endif |
| 1385 | |
| 1386 | return &root->dev; |
| 1387 | } |
| 1388 | EXPORT_SYMBOL_GPL(__root_device_register); |
| 1389 | |
| 1390 | /** |
| 1391 | * root_device_unregister - unregister and free a root device |
Randy Dunlap | 7cbcf22 | 2009-01-20 16:29:13 -0800 | [diff] [blame] | 1392 | * @dev: device going away |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 1393 | * |
| 1394 | * This function unregisters and cleans up a device that was created by |
| 1395 | * root_device_register(). |
| 1396 | */ |
| 1397 | void root_device_unregister(struct device *dev) |
| 1398 | { |
| 1399 | struct root_device *root = to_root_device(dev); |
| 1400 | |
| 1401 | if (root->owner) |
| 1402 | sysfs_remove_link(&root->dev.kobj, "module"); |
| 1403 | |
| 1404 | device_unregister(dev); |
| 1405 | } |
| 1406 | EXPORT_SYMBOL_GPL(root_device_unregister); |
| 1407 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1408 | |
| 1409 | static void device_create_release(struct device *dev) |
| 1410 | { |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 1411 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1412 | kfree(dev); |
| 1413 | } |
| 1414 | |
| 1415 | /** |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 1416 | * device_create_vargs - creates a device and registers it with sysfs |
| 1417 | * @class: pointer to the struct class that this device should be registered to |
| 1418 | * @parent: pointer to the parent struct device of this new device, if any |
| 1419 | * @devt: the dev_t for the char device to be added |
| 1420 | * @drvdata: the data to be added to the device for callbacks |
| 1421 | * @fmt: string for the device's name |
| 1422 | * @args: va_list for the device's name |
| 1423 | * |
| 1424 | * This function can be used by char device classes. A struct device |
| 1425 | * will be created in sysfs, registered to the specified class. |
| 1426 | * |
| 1427 | * A "dev" file will be created, showing the dev_t for the device, if |
| 1428 | * the dev_t is not 0,0. |
| 1429 | * If a pointer to a parent struct device is passed in, the newly created |
| 1430 | * struct device will be a child of that device in sysfs. |
| 1431 | * The pointer to the struct device will be returned from the call. |
| 1432 | * Any further sysfs files that might be required can be created using this |
| 1433 | * pointer. |
| 1434 | * |
| 1435 | * Note: the struct class passed to this function must have previously |
| 1436 | * been created with a call to class_create(). |
| 1437 | */ |
| 1438 | struct device *device_create_vargs(struct class *class, struct device *parent, |
| 1439 | dev_t devt, void *drvdata, const char *fmt, |
| 1440 | va_list args) |
| 1441 | { |
| 1442 | struct device *dev = NULL; |
| 1443 | int retval = -ENODEV; |
| 1444 | |
| 1445 | if (class == NULL || IS_ERR(class)) |
| 1446 | goto error; |
| 1447 | |
| 1448 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1449 | if (!dev) { |
| 1450 | retval = -ENOMEM; |
| 1451 | goto error; |
| 1452 | } |
| 1453 | |
| 1454 | dev->devt = devt; |
| 1455 | dev->class = class; |
| 1456 | dev->parent = parent; |
| 1457 | dev->release = device_create_release; |
| 1458 | dev_set_drvdata(dev, drvdata); |
| 1459 | |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 1460 | retval = kobject_set_name_vargs(&dev->kobj, fmt, args); |
| 1461 | if (retval) |
| 1462 | goto error; |
| 1463 | |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 1464 | retval = device_register(dev); |
| 1465 | if (retval) |
| 1466 | goto error; |
| 1467 | |
| 1468 | return dev; |
| 1469 | |
| 1470 | error: |
Cornelia Huck | 286661b | 2008-09-03 18:26:41 +0200 | [diff] [blame] | 1471 | put_device(dev); |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 1472 | return ERR_PTR(retval); |
| 1473 | } |
| 1474 | EXPORT_SYMBOL_GPL(device_create_vargs); |
| 1475 | |
| 1476 | /** |
Greg Kroah-Hartman | 4e10673 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1477 | * device_create - creates a device and registers it with sysfs |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 1478 | * @class: pointer to the struct class that this device should be registered to |
| 1479 | * @parent: pointer to the parent struct device of this new device, if any |
| 1480 | * @devt: the dev_t for the char device to be added |
| 1481 | * @drvdata: the data to be added to the device for callbacks |
| 1482 | * @fmt: string for the device's name |
| 1483 | * |
| 1484 | * This function can be used by char device classes. A struct device |
| 1485 | * will be created in sysfs, registered to the specified class. |
| 1486 | * |
| 1487 | * A "dev" file will be created, showing the dev_t for the device, if |
| 1488 | * the dev_t is not 0,0. |
| 1489 | * If a pointer to a parent struct device is passed in, the newly created |
| 1490 | * struct device will be a child of that device in sysfs. |
| 1491 | * The pointer to the struct device will be returned from the call. |
| 1492 | * Any further sysfs files that might be required can be created using this |
| 1493 | * pointer. |
| 1494 | * |
| 1495 | * Note: the struct class passed to this function must have previously |
| 1496 | * been created with a call to class_create(). |
| 1497 | */ |
Greg Kroah-Hartman | 4e10673 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1498 | struct device *device_create(struct class *class, struct device *parent, |
| 1499 | dev_t devt, void *drvdata, const char *fmt, ...) |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 1500 | { |
| 1501 | va_list vargs; |
| 1502 | struct device *dev; |
| 1503 | |
| 1504 | va_start(vargs, fmt); |
| 1505 | dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); |
| 1506 | va_end(vargs); |
| 1507 | return dev; |
| 1508 | } |
Greg Kroah-Hartman | 4e10673 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1509 | EXPORT_SYMBOL_GPL(device_create); |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 1510 | |
Dave Young | cd35449 | 2008-01-28 16:56:11 +0800 | [diff] [blame] | 1511 | static int __match_devt(struct device *dev, void *data) |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1512 | { |
Dave Young | cd35449 | 2008-01-28 16:56:11 +0800 | [diff] [blame] | 1513 | dev_t *devt = data; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1514 | |
Dave Young | cd35449 | 2008-01-28 16:56:11 +0800 | [diff] [blame] | 1515 | return dev->devt == *devt; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1516 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1517 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1518 | /** |
| 1519 | * device_destroy - removes a device that was created with device_create() |
| 1520 | * @class: pointer to the struct class that this device was registered with |
| 1521 | * @devt: the dev_t of the device that was previously registered |
| 1522 | * |
| 1523 | * This call unregisters and cleans up a device that was created with a |
| 1524 | * call to device_create(). |
| 1525 | */ |
| 1526 | void device_destroy(struct class *class, dev_t devt) |
| 1527 | { |
| 1528 | struct device *dev; |
| 1529 | |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 1530 | dev = class_find_device(class, NULL, &devt, __match_devt); |
Dave Young | cd35449 | 2008-01-28 16:56:11 +0800 | [diff] [blame] | 1531 | if (dev) { |
| 1532 | put_device(dev); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1533 | device_unregister(dev); |
Dave Young | cd35449 | 2008-01-28 16:56:11 +0800 | [diff] [blame] | 1534 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1535 | } |
| 1536 | EXPORT_SYMBOL_GPL(device_destroy); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1537 | |
| 1538 | /** |
| 1539 | * device_rename - renames a device |
| 1540 | * @dev: the pointer to the struct device to be renamed |
| 1541 | * @new_name: the new name of the device |
Eric W. Biederman | 030c1d2 | 2008-05-08 14:41:00 -0700 | [diff] [blame] | 1542 | * |
| 1543 | * It is the responsibility of the caller to provide mutual |
| 1544 | * exclusion between two different calls of device_rename |
| 1545 | * on the same device to ensure that new_name is valid and |
| 1546 | * won't conflict with other devices. |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1547 | */ |
| 1548 | int device_rename(struct device *dev, char *new_name) |
| 1549 | { |
| 1550 | char *old_class_name = NULL; |
| 1551 | char *new_class_name = NULL; |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 1552 | char *old_device_name = NULL; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1553 | int error; |
| 1554 | |
| 1555 | dev = get_device(dev); |
| 1556 | if (!dev) |
| 1557 | return -EINVAL; |
| 1558 | |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 1559 | pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev), |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 1560 | __func__, new_name); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1561 | |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 1562 | #ifdef CONFIG_SYSFS_DEPRECATED |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1563 | if ((dev->class) && (dev->parent)) |
| 1564 | old_class_name = make_class_name(dev->class->name, &dev->kobj); |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 1565 | #endif |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1566 | |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 1567 | old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 1568 | if (!old_device_name) { |
| 1569 | error = -ENOMEM; |
| 1570 | goto out; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1571 | } |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1572 | |
| 1573 | error = kobject_rename(&dev->kobj, new_name); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 1574 | if (error) |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 1575 | goto out; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1576 | |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 1577 | #ifdef CONFIG_SYSFS_DEPRECATED |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1578 | if (old_class_name) { |
| 1579 | new_class_name = make_class_name(dev->class->name, &dev->kobj); |
| 1580 | if (new_class_name) { |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 1581 | error = sysfs_create_link_nowarn(&dev->parent->kobj, |
| 1582 | &dev->kobj, |
| 1583 | new_class_name); |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 1584 | if (error) |
| 1585 | goto out; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1586 | sysfs_remove_link(&dev->parent->kobj, old_class_name); |
| 1587 | } |
| 1588 | } |
Kay Sievers | 60b8cab | 2007-10-26 20:07:44 +0200 | [diff] [blame] | 1589 | #else |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1590 | if (dev->class) { |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 1591 | error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj, |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 1592 | &dev->kobj, dev_name(dev)); |
Stephen Hemminger | 0599ad5 | 2008-05-14 22:34:16 -0700 | [diff] [blame] | 1593 | if (error) |
| 1594 | goto out; |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 1595 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, |
Greg Kroah-Hartman | 7c71448 | 2008-01-22 18:17:41 -0500 | [diff] [blame] | 1596 | old_device_name); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1597 | } |
Kay Sievers | 60b8cab | 2007-10-26 20:07:44 +0200 | [diff] [blame] | 1598 | #endif |
| 1599 | |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 1600 | out: |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1601 | put_device(dev); |
| 1602 | |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1603 | kfree(new_class_name); |
Jesper Juhl | 952ab43 | 2006-09-28 23:56:01 +0200 | [diff] [blame] | 1604 | kfree(old_class_name); |
Cornelia Huck | 2ee97caf | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 1605 | kfree(old_device_name); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 1606 | |
| 1607 | return error; |
| 1608 | } |
Johannes Berg | a2807db | 2007-02-28 12:38:31 +0100 | [diff] [blame] | 1609 | EXPORT_SYMBOL_GPL(device_rename); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1610 | |
| 1611 | static int device_move_class_links(struct device *dev, |
| 1612 | struct device *old_parent, |
| 1613 | struct device *new_parent) |
| 1614 | { |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 1615 | int error = 0; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1616 | #ifdef CONFIG_SYSFS_DEPRECATED |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1617 | char *class_name; |
| 1618 | |
| 1619 | class_name = make_class_name(dev->class->name, &dev->kobj); |
| 1620 | if (!class_name) { |
Cornelia Huck | cb360bb | 2006-11-27 10:35:05 +0100 | [diff] [blame] | 1621 | error = -ENOMEM; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1622 | goto out; |
| 1623 | } |
| 1624 | if (old_parent) { |
| 1625 | sysfs_remove_link(&dev->kobj, "device"); |
| 1626 | sysfs_remove_link(&old_parent->kobj, class_name); |
| 1627 | } |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1628 | if (new_parent) { |
| 1629 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, |
| 1630 | "device"); |
| 1631 | if (error) |
| 1632 | goto out; |
| 1633 | error = sysfs_create_link(&new_parent->kobj, &dev->kobj, |
| 1634 | class_name); |
| 1635 | if (error) |
| 1636 | sysfs_remove_link(&dev->kobj, "device"); |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1637 | } else |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1638 | error = 0; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1639 | out: |
| 1640 | kfree(class_name); |
| 1641 | return error; |
| 1642 | #else |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 1643 | if (old_parent) |
| 1644 | sysfs_remove_link(&dev->kobj, "device"); |
| 1645 | if (new_parent) |
| 1646 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, |
| 1647 | "device"); |
| 1648 | return error; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1649 | #endif |
| 1650 | } |
| 1651 | |
| 1652 | /** |
| 1653 | * device_move - moves a device to a new parent |
| 1654 | * @dev: the pointer to the struct device to be moved |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1655 | * @new_parent: the new parent of the device (can by NULL) |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 1656 | * @dpm_order: how to reorder the dpm_list |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1657 | */ |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 1658 | int device_move(struct device *dev, struct device *new_parent, |
| 1659 | enum dpm_order dpm_order) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1660 | { |
| 1661 | int error; |
| 1662 | struct device *old_parent; |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1663 | struct kobject *new_parent_kobj; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1664 | |
| 1665 | dev = get_device(dev); |
| 1666 | if (!dev) |
| 1667 | return -EINVAL; |
| 1668 | |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 1669 | device_pm_lock(); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1670 | new_parent = get_device(new_parent); |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1671 | new_parent_kobj = get_device_parent(dev, new_parent); |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 1672 | |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 1673 | pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), |
| 1674 | __func__, new_parent ? dev_name(new_parent) : "<NULL>"); |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1675 | error = kobject_move(&dev->kobj, new_parent_kobj); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1676 | if (error) { |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 1677 | cleanup_glue_dir(dev, new_parent_kobj); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1678 | put_device(new_parent); |
| 1679 | goto out; |
| 1680 | } |
| 1681 | old_parent = dev->parent; |
| 1682 | dev->parent = new_parent; |
| 1683 | if (old_parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1684 | klist_remove(&dev->p->knode_parent); |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 1685 | if (new_parent) { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1686 | klist_add_tail(&dev->p->knode_parent, |
| 1687 | &new_parent->p->klist_children); |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 1688 | set_dev_node(dev, dev_to_node(new_parent)); |
| 1689 | } |
| 1690 | |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1691 | if (!dev->class) |
| 1692 | goto out_put; |
| 1693 | error = device_move_class_links(dev, old_parent, new_parent); |
| 1694 | if (error) { |
| 1695 | /* We ignore errors on cleanup since we're hosed anyway... */ |
| 1696 | device_move_class_links(dev, new_parent, old_parent); |
| 1697 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 1698 | if (new_parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1699 | klist_remove(&dev->p->knode_parent); |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 1700 | dev->parent = old_parent; |
| 1701 | if (old_parent) { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1702 | klist_add_tail(&dev->p->knode_parent, |
| 1703 | &old_parent->p->klist_children); |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 1704 | set_dev_node(dev, dev_to_node(old_parent)); |
| 1705 | } |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1706 | } |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 1707 | cleanup_glue_dir(dev, new_parent_kobj); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1708 | put_device(new_parent); |
| 1709 | goto out; |
| 1710 | } |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 1711 | switch (dpm_order) { |
| 1712 | case DPM_ORDER_NONE: |
| 1713 | break; |
| 1714 | case DPM_ORDER_DEV_AFTER_PARENT: |
| 1715 | device_pm_move_after(dev, new_parent); |
| 1716 | break; |
| 1717 | case DPM_ORDER_PARENT_BEFORE_DEV: |
| 1718 | device_pm_move_before(new_parent, dev); |
| 1719 | break; |
| 1720 | case DPM_ORDER_DEV_LAST: |
| 1721 | device_pm_move_last(dev); |
| 1722 | break; |
| 1723 | } |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1724 | out_put: |
| 1725 | put_device(old_parent); |
| 1726 | out: |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 1727 | device_pm_unlock(); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1728 | put_device(dev); |
| 1729 | return error; |
| 1730 | } |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1731 | EXPORT_SYMBOL_GPL(device_move); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 1732 | |
| 1733 | /** |
| 1734 | * device_shutdown - call ->shutdown() on each device to shutdown. |
| 1735 | */ |
| 1736 | void device_shutdown(void) |
| 1737 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1738 | struct device *dev, *devn; |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 1739 | |
| 1740 | list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list, |
| 1741 | kobj.entry) { |
| 1742 | if (dev->bus && dev->bus->shutdown) { |
| 1743 | dev_dbg(dev, "shutdown\n"); |
| 1744 | dev->bus->shutdown(dev); |
| 1745 | } else if (dev->driver && dev->driver->shutdown) { |
| 1746 | dev_dbg(dev, "shutdown\n"); |
| 1747 | dev->driver->shutdown(dev); |
| 1748 | } |
| 1749 | } |
Shaohua Li | 401097e | 2009-05-12 13:37:57 -0700 | [diff] [blame] | 1750 | async_synchronize_full(); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 1751 | } |