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