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