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