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