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