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