blob: abea76c36a4b96a10201bb4eeef5c0650e6644ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010023#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080024#include <linux/kallsyms.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070025#include <linux/mutex.h>
Shaohua Li401097e2009-05-12 13:37:57 -070026#include <linux/async.h>
Peter Chenaf8db152011-11-15 21:52:29 +010027#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020028#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "base.h"
31#include "power/power.h"
32
Andi Kleene52eec12010-09-08 16:54:17 +020033#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
39static __init int sysfs_deprecated_setup(char *arg)
40{
41 return strict_strtol(arg, 10, &sysfs_deprecated);
42}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080046int (*platform_notify)(struct device *dev) = NULL;
47int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070048static struct kobject *dev_kobj;
49struct kobject *sysfs_dev_char_kobj;
50struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080052#ifdef CONFIG_BLOCK
53static inline int device_is_not_partition(struct device *dev)
54{
55 return !(dev->type == &part_type);
56}
57#else
58static inline int device_is_not_partition(struct device *dev)
59{
60 return 1;
61}
62#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Alan Stern3e956372006-06-16 17:10:48 -040064/**
65 * dev_driver_string - Return a device's driver name, if at all possible
66 * @dev: struct device to get the name of
67 *
68 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +080069 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -040070 * it is attached to. If it is not attached to a bus either, an empty
71 * string will be returned.
72 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070073const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040074{
Alan Stern35899722009-12-04 11:06:57 -050075 struct device_driver *drv;
76
77 /* dev->driver can change to NULL underneath us because of unbinding,
78 * so be careful about accessing it. dev->bus and dev->class should
79 * never change once they are set, so they don't need special care.
80 */
81 drv = ACCESS_ONCE(dev->driver);
82 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +010083 (dev->bus ? dev->bus->name :
84 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040085}
Matthew Wilcox310a9222006-09-23 23:35:04 -060086EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
89
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080090static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
91 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080093 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +020094 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050095 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040098 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080099 if (ret >= (ssize_t)PAGE_SIZE) {
100 print_symbol("dev_attr_show: %s returned bad count\n",
101 (unsigned long)dev_attr->show);
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return ret;
104}
105
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800106static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
107 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800109 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200110 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500111 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400114 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return ret;
116}
117
Emese Revfy52cf25d2010-01-19 02:58:23 +0100118static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .show = dev_attr_show,
120 .store = dev_attr_store,
121};
122
Kay Sieversca22e562011-12-14 14:29:38 -0800123#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
124
125ssize_t device_store_ulong(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t size)
128{
129 struct dev_ext_attribute *ea = to_ext_attr(attr);
130 char *end;
131 unsigned long new = simple_strtoul(buf, &end, 0);
132 if (end == buf)
133 return -EINVAL;
134 *(unsigned long *)(ea->var) = new;
135 /* Always return full write size even if we didn't consume all */
136 return size;
137}
138EXPORT_SYMBOL_GPL(device_store_ulong);
139
140ssize_t device_show_ulong(struct device *dev,
141 struct device_attribute *attr,
142 char *buf)
143{
144 struct dev_ext_attribute *ea = to_ext_attr(attr);
145 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
146}
147EXPORT_SYMBOL_GPL(device_show_ulong);
148
149ssize_t device_store_int(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t size)
152{
153 struct dev_ext_attribute *ea = to_ext_attr(attr);
154 char *end;
155 long new = simple_strtol(buf, &end, 0);
156 if (end == buf || new > INT_MAX || new < INT_MIN)
157 return -EINVAL;
158 *(int *)(ea->var) = new;
159 /* Always return full write size even if we didn't consume all */
160 return size;
161}
162EXPORT_SYMBOL_GPL(device_store_int);
163
164ssize_t device_show_int(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
168 struct dev_ext_attribute *ea = to_ext_attr(attr);
169
170 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
171}
172EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/**
175 * device_release - free device structure.
176 * @kobj: device's kobject.
177 *
178 * This is called once the reference count for the object
179 * reaches 0. We forward the call to the device's release
180 * method, which should handle actually freeing the structure.
181 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800182static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200184 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800185 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Ming Leia525a3d2012-07-25 01:42:29 +0800187 /*
188 * Some platform devices are driven without driver attached
189 * and managed resources may have been acquired. Make sure
190 * all resources are released.
191 *
192 * Drivers still can add resources into device after device
193 * is deleted but alive, so release devres here to avoid
194 * possible memory leak.
195 */
196 devres_release_all(dev);
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (dev->release)
199 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200200 else if (dev->type && dev->type->release)
201 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700202 else if (dev->class && dev->class->dev_release)
203 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700204 else
205 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800206 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100207 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800208 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700211static const void *device_namespace(struct kobject *kobj)
212{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200213 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700214 const void *ns = NULL;
215
216 if (dev->class && dev->class->ns_type)
217 ns = dev->class->namespace(dev);
218
219 return ns;
220}
221
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600222static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .release = device_release,
224 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700225 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
228
Kay Sievers312c0042005-11-16 09:00:00 +0100229static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct kobj_type *ktype = get_ktype(kobj);
232
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600233 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200234 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (dev->bus)
236 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700237 if (dev->class)
238 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 return 0;
241}
242
Kay Sievers312c0042005-11-16 09:00:00 +0100243static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200245 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700247 if (dev->bus)
248 return dev->bus->name;
249 if (dev->class)
250 return dev->class->name;
251 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Kay Sievers7eff2e72007-08-14 15:15:12 +0200254static int dev_uevent(struct kset *kset, struct kobject *kobj,
255 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200257 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int retval = 0;
259
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200260 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700261 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200262 const char *tmp;
263 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400264 umode_t mode = 0;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200265
Kay Sievers7eff2e72007-08-14 15:15:12 +0200266 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
267 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sieverse454cea2009-09-18 23:01:12 +0200268 name = device_get_devnode(dev, &mode, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200269 if (name) {
270 add_uevent_var(env, "DEVNAME=%s", name);
271 kfree(tmp);
Kay Sieverse454cea2009-09-18 23:01:12 +0200272 if (mode)
273 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200274 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700275 }
276
Kay Sievers414264f2007-03-12 21:08:57 +0100277 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200278 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100279
Kay Sievers239378f2006-10-07 21:54:55 +0200280 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200281 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200282
Grant Likely07d57a32012-02-01 11:22:22 -0700283 /* Add common DT information about the device */
284 of_device_uevent(dev, env);
285
Kay Sievers7eff2e72007-08-14 15:15:12 +0200286 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100287 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200288 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200289 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800290 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100291 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Kay Sievers7eff2e72007-08-14 15:15:12 +0200294 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700295 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200296 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200297 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800298 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100299 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800300 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200301 }
302
Stefan Weileef35c22010-08-06 21:11:15 +0200303 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200304 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200305 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200306 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800307 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100308 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800309 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700310 }
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return retval;
313}
314
Emese Revfy9cd43612009-12-31 14:52:51 +0100315static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100316 .filter = dev_uevent_filter,
317 .name = dev_uevent_name,
318 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
Kay Sievers16574dc2007-04-06 01:40:38 +0200321static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
322 char *buf)
323{
324 struct kobject *top_kobj;
325 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200326 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200327 int i;
328 size_t count = 0;
329 int retval;
330
331 /* search the kset, the device belongs to */
332 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200333 while (!top_kobj->kset && top_kobj->parent)
334 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200335 if (!top_kobj->kset)
336 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200337
Kay Sievers16574dc2007-04-06 01:40:38 +0200338 kset = top_kobj->kset;
339 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
340 goto out;
341
342 /* respect filter */
343 if (kset->uevent_ops && kset->uevent_ops->filter)
344 if (!kset->uevent_ops->filter(kset, &dev->kobj))
345 goto out;
346
Kay Sievers7eff2e72007-08-14 15:15:12 +0200347 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
348 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200349 return -ENOMEM;
350
Kay Sievers16574dc2007-04-06 01:40:38 +0200351 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200352 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200353 if (retval)
354 goto out;
355
356 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200357 for (i = 0; i < env->envp_idx; i++)
358 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200359out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200360 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200361 return count;
362}
363
Kay Sieversa7fd6702005-10-01 14:49:43 +0200364static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
365 const char *buf, size_t count)
366{
Kay Sievers60a96a52007-07-08 22:29:26 +0200367 enum kobject_action action;
368
Kay Sievers3f5468c2010-01-14 22:54:37 +0100369 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200370 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100371 else
372 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200373 return count;
374}
375
Tejun Heoad6a1e12007-06-14 03:45:17 +0900376static struct device_attribute uevent_attr =
377 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
378
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500379static int device_add_attributes(struct device *dev,
380 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700381{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700382 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500383 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700384
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500385 if (attrs) {
386 for (i = 0; attr_name(attrs[i]); i++) {
387 error = device_create_file(dev, &attrs[i]);
388 if (error)
389 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700390 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500391 if (error)
392 while (--i >= 0)
393 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700394 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700395 return error;
396}
397
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500398static void device_remove_attributes(struct device *dev,
399 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700400{
401 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500402
403 if (attrs)
404 for (i = 0; attr_name(attrs[i]); i++)
405 device_remove_file(dev, &attrs[i]);
406}
407
Stefan Achatzc97415a2010-11-26 19:57:29 +0000408static int device_add_bin_attributes(struct device *dev,
409 struct bin_attribute *attrs)
410{
411 int error = 0;
412 int i;
413
414 if (attrs) {
415 for (i = 0; attr_name(attrs[i]); i++) {
416 error = device_create_bin_file(dev, &attrs[i]);
417 if (error)
418 break;
419 }
420 if (error)
421 while (--i >= 0)
422 device_remove_bin_file(dev, &attrs[i]);
423 }
424 return error;
425}
426
427static void device_remove_bin_attributes(struct device *dev,
428 struct bin_attribute *attrs)
429{
430 int i;
431
432 if (attrs)
433 for (i = 0; attr_name(attrs[i]); i++)
434 device_remove_bin_file(dev, &attrs[i]);
435}
436
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500437static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700438 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500439{
440 int error = 0;
441 int i;
442
443 if (groups) {
444 for (i = 0; groups[i]; i++) {
445 error = sysfs_create_group(&dev->kobj, groups[i]);
446 if (error) {
447 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800448 sysfs_remove_group(&dev->kobj,
449 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500450 break;
451 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700452 }
453 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500454 return error;
455}
456
457static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700458 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500459{
460 int i;
461
462 if (groups)
463 for (i = 0; groups[i]; i++)
464 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700465}
466
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700467static int device_add_attrs(struct device *dev)
468{
469 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700470 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500471 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700472
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500473 if (class) {
474 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200475 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500476 return error;
Stefan Achatzc97415a2010-11-26 19:57:29 +0000477 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
478 if (error)
479 goto err_remove_class_attrs;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700480 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200481
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500482 if (type) {
483 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200484 if (error)
Stefan Achatzc97415a2010-11-26 19:57:29 +0000485 goto err_remove_class_bin_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200486 }
487
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500488 error = device_add_groups(dev, dev->groups);
489 if (error)
490 goto err_remove_type_groups;
491
492 return 0;
493
494 err_remove_type_groups:
495 if (type)
496 device_remove_groups(dev, type->groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000497 err_remove_class_bin_attrs:
498 if (class)
499 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500500 err_remove_class_attrs:
501 if (class)
502 device_remove_attributes(dev, class->dev_attrs);
503
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700504 return error;
505}
506
507static void device_remove_attrs(struct device *dev)
508{
509 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700510 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700511
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500512 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200513
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500514 if (type)
515 device_remove_groups(dev, type->groups);
516
Stefan Achatzc97415a2010-11-26 19:57:29 +0000517 if (class) {
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500518 device_remove_attributes(dev, class->dev_attrs);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000519 device_remove_bin_attributes(dev, class->dev_bin_attrs);
520 }
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700521}
522
523
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700524static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
525 char *buf)
526{
527 return print_dev_t(buf, dev->devt);
528}
529
Tejun Heoad6a1e12007-06-14 03:45:17 +0900530static struct device_attribute devt_attr =
531 __ATTR(dev, S_IRUGO, show_dev, NULL);
532
Kay Sieversca22e562011-12-14 14:29:38 -0800533/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600534struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800537 * device_create_file - create sysfs attribute file for device.
538 * @dev: device.
539 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200541int device_create_file(struct device *dev,
542 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 int error = 0;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100545 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 error = sysfs_create_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return error;
548}
549
550/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800551 * device_remove_file - remove sysfs attribute file.
552 * @dev: device.
553 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200555void device_remove_file(struct device *dev,
556 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100558 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700562/**
563 * device_create_bin_file - create sysfs binary attribute file for device.
564 * @dev: device.
565 * @attr: device binary attribute descriptor.
566 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200567int device_create_bin_file(struct device *dev,
568 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700569{
570 int error = -EINVAL;
571 if (dev)
572 error = sysfs_create_bin_file(&dev->kobj, attr);
573 return error;
574}
575EXPORT_SYMBOL_GPL(device_create_bin_file);
576
577/**
578 * device_remove_bin_file - remove sysfs binary attribute file
579 * @dev: device.
580 * @attr: device binary attribute descriptor.
581 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200582void device_remove_bin_file(struct device *dev,
583 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700584{
585 if (dev)
586 sysfs_remove_bin_file(&dev->kobj, attr);
587}
588EXPORT_SYMBOL_GPL(device_remove_bin_file);
589
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400590/**
Alan Stern523ded72007-04-26 00:12:04 -0700591 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400592 * @dev: device.
593 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700594 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400595 *
596 * Attribute methods must not unregister themselves or their parent device
597 * (which would amount to the same thing). Attempts to do so will deadlock,
598 * since unregistration is mutually exclusive with driver callbacks.
599 *
600 * Instead methods can call this routine, which will attempt to allocate
601 * and schedule a workqueue request to call back @func with @dev as its
602 * argument in the workqueue's process context. @dev will be pinned until
603 * @func returns.
604 *
Alan Stern523ded72007-04-26 00:12:04 -0700605 * This routine is usually called via the inline device_schedule_callback(),
606 * which automatically sets @owner to THIS_MODULE.
607 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400608 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700609 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400610 *
611 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
612 * underlying sysfs routine (since it is intended for use by attribute
613 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
614 */
Alan Stern523ded72007-04-26 00:12:04 -0700615int device_schedule_callback_owner(struct device *dev,
616 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400617{
618 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700619 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400620}
Alan Stern523ded72007-04-26 00:12:04 -0700621EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400622
James Bottomley34bb61f2005-09-06 16:56:51 -0700623static void klist_children_get(struct klist_node *n)
624{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800625 struct device_private *p = to_device_private_parent(n);
626 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700627
628 get_device(dev);
629}
630
631static void klist_children_put(struct klist_node *n)
632{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800633 struct device_private *p = to_device_private_parent(n);
634 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700635
636 put_device(dev);
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800640 * device_initialize - init device structure.
641 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
Cornelia Huck57394112008-09-03 18:26:40 +0200643 * This prepares the device for use by other layers by initializing
644 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800645 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200646 * that function, though it can also be called separately, so one
647 * may use @dev's fields. In particular, get_device()/put_device()
648 * may be used for reference counting of @dev after calling this
649 * function.
650 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500651 * All fields in @dev must be initialized by the caller to 0, except
652 * for those explicitly set to some other value. The simplest
653 * approach is to use kzalloc() to allocate the structure containing
654 * @dev.
655 *
Cornelia Huck57394112008-09-03 18:26:40 +0200656 * NOTE: Use put_device() to give up your reference instead of freeing
657 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659void device_initialize(struct device *dev)
660{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600661 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700662 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000664 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100665 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900666 spin_lock_init(&dev->devres_lock);
667 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400668 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800669 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Kay Sievers86406242007-03-14 03:25:56 +0100672static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700673{
Kay Sievers86406242007-03-14 03:25:56 +0100674 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700675
Kay Sievers86406242007-03-14 03:25:56 +0100676 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800677 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600678 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700679
Kay Sievers86406242007-03-14 03:25:56 +0100680 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700681}
682
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700683struct class_dir {
684 struct kobject kobj;
685 struct class *class;
686};
687
688#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
689
690static void class_dir_release(struct kobject *kobj)
691{
692 struct class_dir *dir = to_class_dir(kobj);
693 kfree(dir);
694}
695
696static const
697struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
698{
699 struct class_dir *dir = to_class_dir(kobj);
700 return dir->class->ns_type;
701}
702
703static struct kobj_type class_dir_ktype = {
704 .release = class_dir_release,
705 .sysfs_ops = &kobj_sysfs_ops,
706 .child_ns_type = class_dir_child_ns_type
707};
708
709static struct kobject *
710class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
711{
712 struct class_dir *dir;
713 int retval;
714
715 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
716 if (!dir)
717 return NULL;
718
719 dir->class = class;
720 kobject_init(&dir->kobj, &class_dir_ktype);
721
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100722 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700723
724 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
725 if (retval < 0) {
726 kobject_put(&dir->kobj);
727 return NULL;
728 }
729 return &dir->kobj;
730}
731
732
Kay Sieversda231fd2007-11-21 17:29:15 +0100733static struct kobject *get_device_parent(struct device *dev,
734 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100735{
Kay Sievers86406242007-03-14 03:25:56 +0100736 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900737 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100738 struct kobject *kobj = NULL;
739 struct kobject *parent_kobj;
740 struct kobject *k;
741
Randy Dunlapead454f2010-09-24 14:36:49 -0700742#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700743 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200744 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700745 if (parent && parent->class == &block_class)
746 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100747 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -0700748 }
Randy Dunlapead454f2010-09-24 14:36:49 -0700749#endif
Andi Kleene52eec12010-09-08 16:54:17 +0200750
Kay Sievers86406242007-03-14 03:25:56 +0100751 /*
752 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100753 * Class-devices with a non class-device as parent, live
754 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100755 */
756 if (parent == NULL)
757 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700758 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100759 return &parent->kobj;
760 else
761 parent_kobj = &parent->kobj;
762
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900763 mutex_lock(&gdp_mutex);
764
Kay Sievers86406242007-03-14 03:25:56 +0100765 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100766 spin_lock(&dev->class->p->glue_dirs.list_lock);
767 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100768 if (k->parent == parent_kobj) {
769 kobj = kobject_get(k);
770 break;
771 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100772 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900773 if (kobj) {
774 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100775 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900776 }
Kay Sievers86406242007-03-14 03:25:56 +0100777
778 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700779 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100780 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900781 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800782 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100783 }
784
Kay Sieversca22e562011-12-14 14:29:38 -0800785 /* subsystems can specify a default root directory for their devices */
786 if (!parent && dev->bus && dev->bus->dev_root)
787 return &dev->bus->dev_root->kobj;
788
Kay Sievers86406242007-03-14 03:25:56 +0100789 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100790 return &parent->kobj;
791 return NULL;
792}
Kay Sieversda231fd2007-11-21 17:29:15 +0100793
Cornelia Huck63b69712008-01-21 16:09:44 +0100794static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100795{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100796 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100797 if (!glue_dir || !dev->class ||
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100798 glue_dir->kset != &dev->class->p->glue_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100799 return;
800
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100801 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100802}
Cornelia Huck63b69712008-01-21 16:09:44 +0100803
804static void cleanup_device_parent(struct device *dev)
805{
806 cleanup_glue_dir(dev, dev->kobj.parent);
807}
Kay Sievers86406242007-03-14 03:25:56 +0100808
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700809static int device_add_class_symlinks(struct device *dev)
810{
811 int error;
812
813 if (!dev->class)
814 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100815
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700816 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100817 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700818 "subsystem");
819 if (error)
820 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100821
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800822 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700823 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
824 "device");
825 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700826 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700827 }
Kay Sievers39aba962010-09-04 22:33:14 -0700828
Randy Dunlapead454f2010-09-24 14:36:49 -0700829#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700830 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200831 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700832 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -0700833#endif
Kay Sievers39aba962010-09-04 22:33:14 -0700834
835 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100836 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -0700837 &dev->kobj, dev_name(dev));
838 if (error)
839 goto out_device;
840
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700841 return 0;
842
Kay Sievers39aba962010-09-04 22:33:14 -0700843out_device:
844 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100845
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700846out_subsys:
847 sysfs_remove_link(&dev->kobj, "subsystem");
848out:
849 return error;
850}
851
852static void device_remove_class_symlinks(struct device *dev)
853{
854 if (!dev->class)
855 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100856
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800857 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100858 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700859 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -0700860#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +0200861 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700862 return;
Randy Dunlapead454f2010-09-24 14:36:49 -0700863#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100864 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000868 * dev_set_name - set a device name
869 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700870 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000871 */
872int dev_set_name(struct device *dev, const char *fmt, ...)
873{
874 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100875 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000876
877 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100878 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000879 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100880 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000881}
882EXPORT_SYMBOL_GPL(dev_set_name);
883
884/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700885 * device_to_dev_kobj - select a /sys/dev/ directory for the device
886 * @dev: device
887 *
888 * By default we select char/ for new entries. Setting class->dev_obj
889 * to NULL prevents an entry from being created. class->dev_kobj must
890 * be set (or cleared) before any devices are registered to the class
891 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +0200892 * device_remove_sys_dev_entry() will disagree about the presence of
893 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -0700894 */
895static struct kobject *device_to_dev_kobj(struct device *dev)
896{
897 struct kobject *kobj;
898
899 if (dev->class)
900 kobj = dev->class->dev_kobj;
901 else
902 kobj = sysfs_dev_char_kobj;
903
904 return kobj;
905}
906
907static int device_create_sys_dev_entry(struct device *dev)
908{
909 struct kobject *kobj = device_to_dev_kobj(dev);
910 int error = 0;
911 char devt_str[15];
912
913 if (kobj) {
914 format_dev_t(devt_str, dev->devt);
915 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
916 }
917
918 return error;
919}
920
921static void device_remove_sys_dev_entry(struct device *dev)
922{
923 struct kobject *kobj = device_to_dev_kobj(dev);
924 char devt_str[15];
925
926 if (kobj) {
927 format_dev_t(devt_str, dev->devt);
928 sysfs_remove_link(kobj, devt_str);
929 }
930}
931
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700932int device_private_init(struct device *dev)
933{
934 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
935 if (!dev->p)
936 return -ENOMEM;
937 dev->p->device = dev;
938 klist_init(&dev->p->klist_children, klist_children_get,
939 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -0800940 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700941 return 0;
942}
943
Dan Williamse105b8b2008-04-21 10:51:07 -0700944/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800945 * device_add - add device to device hierarchy.
946 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800948 * This is part 2 of device_register(), though may be called
949 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 *
Cornelia Huck57394112008-09-03 18:26:40 +0200951 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800952 * to the global and sibling lists for the device, then
953 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200954 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500955 * Do not call this routine or device_register() more than once for
956 * any device structure. The driver model core is not designed to work
957 * with devices that get unregistered and then spring back to life.
958 * (Among other things, it's very hard to guarantee that all references
959 * to the previous incarnation of @dev have been dropped.) Allocate
960 * and register a fresh new struct device instead.
961 *
Cornelia Huck57394112008-09-03 18:26:40 +0200962 * NOTE: _Never_ directly free @dev after calling this function, even
963 * if it returned an error! Always use put_device() to give up your
964 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
966int device_add(struct device *dev)
967{
968 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -0800969 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200970 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700971 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700974 if (!dev)
975 goto done;
976
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800977 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700978 error = device_private_init(dev);
979 if (error)
980 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800981 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800982
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100983 /*
984 * for statically allocated devices, which should all be converted
985 * some day, we need to initialize the name. We prevent reading back
986 * the name, and force the use of dev_name()
987 */
988 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700989 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100990 dev->init_name = NULL;
991 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700992
Kay Sieversca22e562011-12-14 14:29:38 -0800993 /* subsystems can specify simple device enumeration */
994 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
995 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
996
Thomas Gleixnere6309e72009-12-10 19:32:49 +0000997 if (!dev_name(dev)) {
998 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -0700999 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001002 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001005 kobj = get_device_parent(dev, parent);
1006 if (kobj)
1007 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Yinghai Lu0d358f22008-02-19 03:20:41 -08001009 /* use parent numa_node */
1010 if (parent)
1011 set_dev_node(dev, dev_to_node(parent));
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001014 /* we require the name to be set before, and pass NULL */
1015 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001016 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001018
Brian Walsh37022642006-08-14 22:43:19 -07001019 /* notify platform of device entry */
1020 if (platform_notify)
1021 platform_notify(dev);
1022
Tejun Heoad6a1e12007-06-14 03:45:17 +09001023 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001024 if (error)
1025 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001026
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001027 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +09001028 error = device_create_file(dev, &devt_attr);
1029 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +02001030 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -07001031
1032 error = device_create_sys_dev_entry(dev);
1033 if (error)
1034 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +02001035
1036 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001037 }
1038
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001039 error = device_add_class_symlinks(dev);
1040 if (error)
1041 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001042 error = device_add_attrs(dev);
1043 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001044 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001045 error = bus_add_device(dev);
1046 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001048 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001049 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001050 goto DPMError;
1051 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001052
1053 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001054 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001055 */
1056 if (dev->bus)
1057 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1058 BUS_NOTIFY_ADD_DEVICE, dev);
1059
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001060 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001061 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001063 klist_add_tail(&dev->p->knode_parent,
1064 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001066 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001067 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001068 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001069 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001070 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001071
1072 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001073 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001074 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001075 if (class_intf->add_dev)
1076 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001077 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001078 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001079done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 put_device(dev);
1081 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -04001082 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001083 bus_remove_device(dev);
1084 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001085 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001086 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001087 device_remove_class_symlinks(dev);
1088 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001089 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +01001090 devtmpfs_delete_node(dev);
1091 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -07001092 device_remove_sys_dev_entry(dev);
1093 devtattrError:
1094 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +09001095 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001096 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001097 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001098 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001099 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 kobject_del(&dev->kobj);
1101 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001102 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (parent)
1104 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001105name_error:
1106 kfree(dev->p);
1107 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001108 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001112 * device_register - register a device with the system.
1113 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001115 * This happens in two clean steps - initialize the device
1116 * and add it to the system. The two steps can be called
1117 * separately, but this is the easiest and most common.
1118 * I.e. you should only call the two helpers separately if
1119 * have a clearly defined need to use and refcount the device
1120 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001121 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001122 * For more information, see the kerneldoc for device_initialize()
1123 * and device_add().
1124 *
Cornelia Huck57394112008-09-03 18:26:40 +02001125 * NOTE: _Never_ directly free @dev after calling this function, even
1126 * if it returned an error! Always use put_device() to give up the
1127 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129int device_register(struct device *dev)
1130{
1131 device_initialize(dev);
1132 return device_add(dev);
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001136 * get_device - increment reference count for device.
1137 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001139 * This simply forwards the call to kobject_get(), though
1140 * we do take care to provide for the case that we get a NULL
1141 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001143struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001145 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001149 * put_device - decrement reference count.
1150 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001152void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001154 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (dev)
1156 kobject_put(&dev->kobj);
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001160 * device_del - delete device from system.
1161 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001163 * This is the first part of the device unregistration
1164 * sequence. This removes the device from the lists we control
1165 * from here, has it removed from the other driver model
1166 * subsystems it was added to in device_add(), and removes it
1167 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001169 * NOTE: this should be called manually _iff_ device_add() was
1170 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001172void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001174 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001175 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Alan Sternec0676ee2008-12-05 14:10:31 -05001177 /* Notify clients of device removal. This call must come
1178 * before dpm_sysfs_remove().
1179 */
1180 if (dev->bus)
1181 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1182 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001183 device_pm_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001184 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001186 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001187 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001188 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001189 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001190 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001191 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001192 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001193 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001194
Kay Sieversca22e562011-12-14 14:29:38 -08001195 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001196 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001197 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001198 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001199 if (class_intf->remove_dev)
1200 class_intf->remove_dev(dev, class_intf);
1201 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001202 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001203 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001204 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001205 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001206 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001207 bus_remove_device(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001208 driver_deferred_probe_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 /* Notify the platform of the removal, in case they
1211 * need to do anything...
1212 */
1213 if (platform_notify_remove)
1214 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001215 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001216 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001218 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001222 * device_unregister - unregister device from system.
1223 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001225 * We do this in two parts, like we do device_register(). First,
1226 * we remove it from all the subsystems with device_del(), then
1227 * we decrement the reference count via put_device(). If that
1228 * is the final reference count, the device will be cleaned up
1229 * via device_release() above. Otherwise, the structure will
1230 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001232void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001234 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 device_del(dev);
1236 put_device(dev);
1237}
1238
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001239static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001240{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001241 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001242 struct device *dev = NULL;
1243 struct device_private *p;
1244
1245 if (n) {
1246 p = to_device_private_parent(n);
1247 dev = p->device;
1248 }
1249 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001253 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001254 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001255 * @mode: returned file access mode
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001256 * @tmp: possibly allocated string
1257 *
1258 * Return the relative path of a possible device node.
1259 * Non-default names may need to allocate a memory to compose
1260 * a name. This memory is returned in tmp and needs to be
1261 * freed by the caller.
1262 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001263const char *device_get_devnode(struct device *dev,
Al Viro2c9ede52011-07-23 20:24:48 -04001264 umode_t *mode, const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001265{
1266 char *s;
1267
1268 *tmp = NULL;
1269
1270 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001271 if (dev->type && dev->type->devnode)
1272 *tmp = dev->type->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001273 if (*tmp)
1274 return *tmp;
1275
1276 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001277 if (dev->class && dev->class->devnode)
1278 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001279 if (*tmp)
1280 return *tmp;
1281
1282 /* return name without allocation, tmp == NULL */
1283 if (strchr(dev_name(dev), '!') == NULL)
1284 return dev_name(dev);
1285
1286 /* replace '!' in the name with '/' */
1287 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1288 if (!*tmp)
1289 return NULL;
1290 while ((s = strchr(*tmp, '!')))
1291 s[0] = '/';
1292 return *tmp;
1293}
1294
1295/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001296 * device_for_each_child - device child iterator.
1297 * @parent: parent struct device.
1298 * @data: data for the callback.
1299 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001301 * Iterate over @parent's child devices, and call @fn for each,
1302 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001304 * We check the return of @fn each time. If it returns anything
1305 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001307int device_for_each_child(struct device *parent, void *data,
1308 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001310 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001311 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int error = 0;
1313
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001314 if (!parent->p)
1315 return 0;
1316
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001317 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001318 while ((child = next_device(&i)) && !error)
1319 error = fn(child, data);
1320 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return error;
1322}
1323
Cornelia Huck5ab69982006-11-16 15:42:07 +01001324/**
1325 * device_find_child - device iterator for locating a particular device.
1326 * @parent: parent struct device
1327 * @data: Data to pass to match function
1328 * @match: Callback function to check device
1329 *
1330 * This is similar to the device_for_each_child() function above, but it
1331 * returns a reference to a device that is 'found' for later use, as
1332 * determined by the @match callback.
1333 *
1334 * The callback should return 0 if the device doesn't match and non-zero
1335 * if it does. If the callback returns non-zero and a reference to the
1336 * current device can be obtained, this function will return to the caller
1337 * and not iterate over any more devices.
1338 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001339struct device *device_find_child(struct device *parent, void *data,
1340 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001341{
1342 struct klist_iter i;
1343 struct device *child;
1344
1345 if (!parent)
1346 return NULL;
1347
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001348 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001349 while ((child = next_device(&i)))
1350 if (match(child, data) && get_device(child))
1351 break;
1352 klist_iter_exit(&i);
1353 return child;
1354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356int __init devices_init(void)
1357{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001358 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1359 if (!devices_kset)
1360 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001361 dev_kobj = kobject_create_and_add("dev", NULL);
1362 if (!dev_kobj)
1363 goto dev_kobj_err;
1364 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1365 if (!sysfs_dev_block_kobj)
1366 goto block_kobj_err;
1367 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1368 if (!sysfs_dev_char_kobj)
1369 goto char_kobj_err;
1370
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001371 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001372
1373 char_kobj_err:
1374 kobject_put(sysfs_dev_block_kobj);
1375 block_kobj_err:
1376 kobject_put(dev_kobj);
1377 dev_kobj_err:
1378 kset_unregister(devices_kset);
1379 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001383EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385EXPORT_SYMBOL_GPL(device_initialize);
1386EXPORT_SYMBOL_GPL(device_add);
1387EXPORT_SYMBOL_GPL(device_register);
1388
1389EXPORT_SYMBOL_GPL(device_del);
1390EXPORT_SYMBOL_GPL(device_unregister);
1391EXPORT_SYMBOL_GPL(get_device);
1392EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394EXPORT_SYMBOL_GPL(device_create_file);
1395EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001396
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05001397struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001398 struct device dev;
1399 struct module *owner;
1400};
1401
Ferenc Wagner481e2072011-01-07 15:17:47 +01001402inline struct root_device *to_root_device(struct device *d)
1403{
1404 return container_of(d, struct root_device, dev);
1405}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001406
1407static void root_device_release(struct device *dev)
1408{
1409 kfree(to_root_device(dev));
1410}
1411
1412/**
1413 * __root_device_register - allocate and register a root device
1414 * @name: root device name
1415 * @owner: owner module of the root device, usually THIS_MODULE
1416 *
1417 * This function allocates a root device and registers it
1418 * using device_register(). In order to free the returned
1419 * device, use root_device_unregister().
1420 *
1421 * Root devices are dummy devices which allow other devices
1422 * to be grouped under /sys/devices. Use this function to
1423 * allocate a root device and then use it as the parent of
1424 * any device which should appear under /sys/devices/{name}
1425 *
1426 * The /sys/devices/{name} directory will also contain a
1427 * 'module' symlink which points to the @owner directory
1428 * in sysfs.
1429 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001430 * Returns &struct device pointer on success, or ERR_PTR() on error.
1431 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001432 * Note: You probably want to use root_device_register().
1433 */
1434struct device *__root_device_register(const char *name, struct module *owner)
1435{
1436 struct root_device *root;
1437 int err = -ENOMEM;
1438
1439 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1440 if (!root)
1441 return ERR_PTR(err);
1442
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001443 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001444 if (err) {
1445 kfree(root);
1446 return ERR_PTR(err);
1447 }
1448
1449 root->dev.release = root_device_release;
1450
1451 err = device_register(&root->dev);
1452 if (err) {
1453 put_device(&root->dev);
1454 return ERR_PTR(err);
1455 }
1456
Christoph Egger1d9e8822010-05-17 16:57:58 +02001457#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001458 if (owner) {
1459 struct module_kobject *mk = &owner->mkobj;
1460
1461 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1462 if (err) {
1463 device_unregister(&root->dev);
1464 return ERR_PTR(err);
1465 }
1466 root->owner = owner;
1467 }
1468#endif
1469
1470 return &root->dev;
1471}
1472EXPORT_SYMBOL_GPL(__root_device_register);
1473
1474/**
1475 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001476 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001477 *
1478 * This function unregisters and cleans up a device that was created by
1479 * root_device_register().
1480 */
1481void root_device_unregister(struct device *dev)
1482{
1483 struct root_device *root = to_root_device(dev);
1484
1485 if (root->owner)
1486 sysfs_remove_link(&root->dev.kobj, "module");
1487
1488 device_unregister(dev);
1489}
1490EXPORT_SYMBOL_GPL(root_device_unregister);
1491
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001492
1493static void device_create_release(struct device *dev)
1494{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001495 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001496 kfree(dev);
1497}
1498
1499/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001500 * device_create_vargs - creates a device and registers it with sysfs
1501 * @class: pointer to the struct class that this device should be registered to
1502 * @parent: pointer to the parent struct device of this new device, if any
1503 * @devt: the dev_t for the char device to be added
1504 * @drvdata: the data to be added to the device for callbacks
1505 * @fmt: string for the device's name
1506 * @args: va_list for the device's name
1507 *
1508 * This function can be used by char device classes. A struct device
1509 * will be created in sysfs, registered to the specified class.
1510 *
1511 * A "dev" file will be created, showing the dev_t for the device, if
1512 * the dev_t is not 0,0.
1513 * If a pointer to a parent struct device is passed in, the newly created
1514 * struct device will be a child of that device in sysfs.
1515 * The pointer to the struct device will be returned from the call.
1516 * Any further sysfs files that might be required can be created using this
1517 * pointer.
1518 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001519 * Returns &struct device pointer on success, or ERR_PTR() on error.
1520 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001521 * Note: the struct class passed to this function must have previously
1522 * been created with a call to class_create().
1523 */
1524struct device *device_create_vargs(struct class *class, struct device *parent,
1525 dev_t devt, void *drvdata, const char *fmt,
1526 va_list args)
1527{
1528 struct device *dev = NULL;
1529 int retval = -ENODEV;
1530
1531 if (class == NULL || IS_ERR(class))
1532 goto error;
1533
1534 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1535 if (!dev) {
1536 retval = -ENOMEM;
1537 goto error;
1538 }
1539
1540 dev->devt = devt;
1541 dev->class = class;
1542 dev->parent = parent;
1543 dev->release = device_create_release;
1544 dev_set_drvdata(dev, drvdata);
1545
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001546 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1547 if (retval)
1548 goto error;
1549
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001550 retval = device_register(dev);
1551 if (retval)
1552 goto error;
1553
1554 return dev;
1555
1556error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001557 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001558 return ERR_PTR(retval);
1559}
1560EXPORT_SYMBOL_GPL(device_create_vargs);
1561
1562/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001563 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001564 * @class: pointer to the struct class that this device should be registered to
1565 * @parent: pointer to the parent struct device of this new device, if any
1566 * @devt: the dev_t for the char device to be added
1567 * @drvdata: the data to be added to the device for callbacks
1568 * @fmt: string for the device's name
1569 *
1570 * This function can be used by char device classes. A struct device
1571 * will be created in sysfs, registered to the specified class.
1572 *
1573 * A "dev" file will be created, showing the dev_t for the device, if
1574 * the dev_t is not 0,0.
1575 * If a pointer to a parent struct device is passed in, the newly created
1576 * struct device will be a child of that device in sysfs.
1577 * The pointer to the struct device will be returned from the call.
1578 * Any further sysfs files that might be required can be created using this
1579 * pointer.
1580 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001581 * Returns &struct device pointer on success, or ERR_PTR() on error.
1582 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001583 * Note: the struct class passed to this function must have previously
1584 * been created with a call to class_create().
1585 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001586struct device *device_create(struct class *class, struct device *parent,
1587 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001588{
1589 va_list vargs;
1590 struct device *dev;
1591
1592 va_start(vargs, fmt);
1593 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1594 va_end(vargs);
1595 return dev;
1596}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001597EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001598
Dave Youngcd354492008-01-28 16:56:11 +08001599static int __match_devt(struct device *dev, void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001600{
Dave Youngcd354492008-01-28 16:56:11 +08001601 dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001602
Dave Youngcd354492008-01-28 16:56:11 +08001603 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001604}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001605
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001606/**
1607 * device_destroy - removes a device that was created with device_create()
1608 * @class: pointer to the struct class that this device was registered with
1609 * @devt: the dev_t of the device that was previously registered
1610 *
1611 * This call unregisters and cleans up a device that was created with a
1612 * call to device_create().
1613 */
1614void device_destroy(struct class *class, dev_t devt)
1615{
1616 struct device *dev;
1617
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001618 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001619 if (dev) {
1620 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001621 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001622 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001623}
1624EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001625
1626/**
1627 * device_rename - renames a device
1628 * @dev: the pointer to the struct device to be renamed
1629 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001630 *
1631 * It is the responsibility of the caller to provide mutual
1632 * exclusion between two different calls of device_rename
1633 * on the same device to ensure that new_name is valid and
1634 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11001635 *
Timur Tabia5462512010-12-13 14:08:52 -06001636 * Note: Don't call this function. Currently, the networking layer calls this
1637 * function, but that will change. The following text from Kay Sievers offers
1638 * some insight:
1639 *
1640 * Renaming devices is racy at many levels, symlinks and other stuff are not
1641 * replaced atomically, and you get a "move" uevent, but it's not easy to
1642 * connect the event to the old and new device. Device nodes are not renamed at
1643 * all, there isn't even support for that in the kernel now.
1644 *
1645 * In the meantime, during renaming, your target name might be taken by another
1646 * driver, creating conflicts. Or the old name is taken directly after you
1647 * renamed it -- then you get events for the same DEVPATH, before you even see
1648 * the "move" event. It's just a mess, and nothing new should ever rely on
1649 * kernel device renaming. Besides that, it's not even implemented now for
1650 * other things than (driver-core wise very simple) network devices.
1651 *
1652 * We are currently about to change network renaming in udev to completely
1653 * disallow renaming of devices in the same namespace as the kernel uses,
1654 * because we can't solve the problems properly, that arise with swapping names
1655 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1656 * be allowed to some other name than eth[0-9]*, for the aforementioned
1657 * reasons.
1658 *
1659 * Make up a "real" name in the driver before you register anything, or add
1660 * some other attributes for userspace to find the device, or use udev to add
1661 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1662 * don't even want to get into that and try to implement the missing pieces in
1663 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001664 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001665int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001666{
1667 char *old_class_name = NULL;
1668 char *new_class_name = NULL;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001669 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001670 int error;
1671
1672 dev = get_device(dev);
1673 if (!dev)
1674 return -EINVAL;
1675
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001676 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001677 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001678
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001679 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001680 if (!old_device_name) {
1681 error = -ENOMEM;
1682 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001683 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001684
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001685 if (dev->class) {
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001686 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001687 &dev->kobj, old_device_name, new_name);
1688 if (error)
1689 goto out;
1690 }
Kay Sievers39aba962010-09-04 22:33:14 -07001691
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001692 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001693 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001694 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001695
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001696out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001697 put_device(dev);
1698
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001699 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001700 kfree(old_class_name);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001701 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001702
1703 return error;
1704}
Johannes Berga2807db2007-02-28 12:38:31 +01001705EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001706
1707static int device_move_class_links(struct device *dev,
1708 struct device *old_parent,
1709 struct device *new_parent)
1710{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001711 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001712
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001713 if (old_parent)
1714 sysfs_remove_link(&dev->kobj, "device");
1715 if (new_parent)
1716 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1717 "device");
1718 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001719}
1720
1721/**
1722 * device_move - moves a device to a new parent
1723 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001724 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001725 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001726 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001727int device_move(struct device *dev, struct device *new_parent,
1728 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001729{
1730 int error;
1731 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001732 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001733
1734 dev = get_device(dev);
1735 if (!dev)
1736 return -EINVAL;
1737
Cornelia Huckffa6a702009-03-04 12:44:00 +01001738 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001739 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001740 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001741
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001742 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1743 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001744 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001745 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001746 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001747 put_device(new_parent);
1748 goto out;
1749 }
1750 old_parent = dev->parent;
1751 dev->parent = new_parent;
1752 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001753 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001754 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001755 klist_add_tail(&dev->p->knode_parent,
1756 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001757 set_dev_node(dev, dev_to_node(new_parent));
1758 }
1759
Rabin Vincentbdd40342012-04-23 09:16:36 +02001760 if (dev->class) {
1761 error = device_move_class_links(dev, old_parent, new_parent);
1762 if (error) {
1763 /* We ignore errors on cleanup since we're hosed anyway... */
1764 device_move_class_links(dev, new_parent, old_parent);
1765 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1766 if (new_parent)
1767 klist_remove(&dev->p->knode_parent);
1768 dev->parent = old_parent;
1769 if (old_parent) {
1770 klist_add_tail(&dev->p->knode_parent,
1771 &old_parent->p->klist_children);
1772 set_dev_node(dev, dev_to_node(old_parent));
1773 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08001774 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001775 cleanup_glue_dir(dev, new_parent_kobj);
1776 put_device(new_parent);
1777 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01001778 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001779 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001780 switch (dpm_order) {
1781 case DPM_ORDER_NONE:
1782 break;
1783 case DPM_ORDER_DEV_AFTER_PARENT:
1784 device_pm_move_after(dev, new_parent);
1785 break;
1786 case DPM_ORDER_PARENT_BEFORE_DEV:
1787 device_pm_move_before(new_parent, dev);
1788 break;
1789 case DPM_ORDER_DEV_LAST:
1790 device_pm_move_last(dev);
1791 break;
1792 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001793
Cornelia Huck8a824722006-11-20 17:07:51 +01001794 put_device(old_parent);
1795out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001796 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001797 put_device(dev);
1798 return error;
1799}
Cornelia Huck8a824722006-11-20 17:07:51 +01001800EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001801
1802/**
1803 * device_shutdown - call ->shutdown() on each device to shutdown.
1804 */
1805void device_shutdown(void)
1806{
Hugh Daschbach62458382010-03-22 10:36:37 -07001807 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001808
Hugh Daschbach62458382010-03-22 10:36:37 -07001809 spin_lock(&devices_kset->list_lock);
1810 /*
1811 * Walk the devices list backward, shutting down each in turn.
1812 * Beware that device unplug events may also start pulling
1813 * devices offline, even as the system is shutting down.
1814 */
1815 while (!list_empty(&devices_kset->list)) {
1816 dev = list_entry(devices_kset->list.prev, struct device,
1817 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08001818
1819 /*
1820 * hold reference count of device's parent to
1821 * prevent it from being freed because parent's
1822 * lock is to be held
1823 */
1824 get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07001825 get_device(dev);
1826 /*
1827 * Make sure the device is off the kset list, in the
1828 * event that dev->*->shutdown() doesn't remove it.
1829 */
1830 list_del_init(&dev->kobj.entry);
1831 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01001832
Ming Leid1c6c032012-06-22 18:01:40 +08001833 /* hold lock to avoid race with probe/release */
1834 if (dev->parent)
1835 device_lock(dev->parent);
1836 device_lock(dev);
1837
Alan Sternfe6b91f2011-12-06 23:24:52 +01001838 /* Don't allow any more runtime suspends */
1839 pm_runtime_get_noresume(dev);
1840 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07001841
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001842 if (dev->bus && dev->bus->shutdown) {
1843 dev_dbg(dev, "shutdown\n");
1844 dev->bus->shutdown(dev);
1845 } else if (dev->driver && dev->driver->shutdown) {
1846 dev_dbg(dev, "shutdown\n");
1847 dev->driver->shutdown(dev);
1848 }
Ming Leid1c6c032012-06-22 18:01:40 +08001849
1850 device_unlock(dev);
1851 if (dev->parent)
1852 device_unlock(dev->parent);
1853
Hugh Daschbach62458382010-03-22 10:36:37 -07001854 put_device(dev);
Ming Leid1c6c032012-06-22 18:01:40 +08001855 put_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07001856
1857 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001858 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001859 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07001860 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001861}
Joe Perches99bcf212010-06-27 01:02:34 +00001862
1863/*
1864 * Device logging functions
1865 */
1866
1867#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07001868static int
1869create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00001870{
Kay Sieversc4e00da2012-05-03 02:29:59 +02001871 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07001872 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00001873
Kay Sieversc4e00da2012-05-03 02:29:59 +02001874 if (dev->class)
1875 subsys = dev->class->name;
1876 else if (dev->bus)
1877 subsys = dev->bus->name;
1878 else
Joe Perches798efc62012-09-12 20:11:29 -07001879 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02001880
Joe Perches798efc62012-09-12 20:11:29 -07001881 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Kay Sieversc4e00da2012-05-03 02:29:59 +02001882
1883 /*
1884 * Add device identifier DEVICE=:
1885 * b12:8 block dev_t
1886 * c127:3 char dev_t
1887 * n8 netdev ifindex
1888 * +sound:card0 subsystem:devname
1889 */
1890 if (MAJOR(dev->devt)) {
1891 char c;
1892
1893 if (strcmp(subsys, "block") == 0)
1894 c = 'b';
1895 else
1896 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07001897 pos++;
1898 pos += snprintf(hdr + pos, hdrlen - pos,
1899 "DEVICE=%c%u:%u",
1900 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02001901 } else if (strcmp(subsys, "net") == 0) {
1902 struct net_device *net = to_net_dev(dev);
1903
Joe Perches798efc62012-09-12 20:11:29 -07001904 pos++;
1905 pos += snprintf(hdr + pos, hdrlen - pos,
1906 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02001907 } else {
Joe Perches798efc62012-09-12 20:11:29 -07001908 pos++;
1909 pos += snprintf(hdr + pos, hdrlen - pos,
1910 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02001911 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06001912
Joe Perches798efc62012-09-12 20:11:29 -07001913 return pos;
Joe Perches99bcf212010-06-27 01:02:34 +00001914}
Joe Perches798efc62012-09-12 20:11:29 -07001915EXPORT_SYMBOL(create_syslog_header);
1916
Joe Perches05e4e5b2012-09-12 20:13:37 -07001917int dev_vprintk_emit(int level, const struct device *dev,
1918 const char *fmt, va_list args)
1919{
1920 char hdr[128];
1921 size_t hdrlen;
1922
1923 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
1924
1925 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
1926}
1927EXPORT_SYMBOL(dev_vprintk_emit);
1928
1929int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1930{
1931 va_list args;
1932 int r;
1933
1934 va_start(args, fmt);
1935
1936 r = dev_vprintk_emit(level, dev, fmt, args);
1937
1938 va_end(args);
1939
1940 return r;
1941}
1942EXPORT_SYMBOL(dev_printk_emit);
1943
Joe Perches798efc62012-09-12 20:11:29 -07001944static int __dev_printk(const char *level, const struct device *dev,
1945 struct va_format *vaf)
1946{
Joe Perches798efc62012-09-12 20:11:29 -07001947 if (!dev)
1948 return printk("%s(NULL device *): %pV", level, vaf);
1949
Joe Perches666f3552012-09-12 20:14:11 -07001950 return dev_printk_emit(level[1] - '0', dev,
1951 "%s %s: %pV",
1952 dev_driver_string(dev), dev_name(dev), vaf);
Joe Perches798efc62012-09-12 20:11:29 -07001953}
Joe Perches99bcf212010-06-27 01:02:34 +00001954
1955int dev_printk(const char *level, const struct device *dev,
1956 const char *fmt, ...)
1957{
1958 struct va_format vaf;
1959 va_list args;
1960 int r;
1961
1962 va_start(args, fmt);
1963
1964 vaf.fmt = fmt;
1965 vaf.va = &args;
1966
1967 r = __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07001968
Joe Perches99bcf212010-06-27 01:02:34 +00001969 va_end(args);
1970
1971 return r;
1972}
1973EXPORT_SYMBOL(dev_printk);
1974
1975#define define_dev_printk_level(func, kern_level) \
1976int func(const struct device *dev, const char *fmt, ...) \
1977{ \
1978 struct va_format vaf; \
1979 va_list args; \
1980 int r; \
1981 \
1982 va_start(args, fmt); \
1983 \
1984 vaf.fmt = fmt; \
1985 vaf.va = &args; \
1986 \
1987 r = __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07001988 \
Joe Perches99bcf212010-06-27 01:02:34 +00001989 va_end(args); \
1990 \
1991 return r; \
1992} \
1993EXPORT_SYMBOL(func);
1994
1995define_dev_printk_level(dev_emerg, KERN_EMERG);
1996define_dev_printk_level(dev_alert, KERN_ALERT);
1997define_dev_printk_level(dev_crit, KERN_CRIT);
1998define_dev_printk_level(dev_err, KERN_ERR);
1999define_dev_printk_level(dev_warn, KERN_WARNING);
2000define_dev_printk_level(dev_notice, KERN_NOTICE);
2001define_dev_printk_level(_dev_info, KERN_INFO);
2002
2003#endif