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