blob: 6bee6af8d8e1d74b574640e44013c2ccf7c51bd2 [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>
Kay Sieversda231fd2007-11-21 17:29:15 +010021#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080022#include <linux/kallsyms.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040023#include <linux/semaphore.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070024#include <linux/mutex.h>
Shaohua Li401097e2009-05-12 13:37:57 -070025#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "base.h"
28#include "power/power.h"
29
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080030int (*platform_notify)(struct device *dev) = NULL;
31int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070032static struct kobject *dev_kobj;
33struct kobject *sysfs_dev_char_kobj;
34struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080036#ifdef CONFIG_BLOCK
37static inline int device_is_not_partition(struct device *dev)
38{
39 return !(dev->type == &part_type);
40}
41#else
42static inline int device_is_not_partition(struct device *dev)
43{
44 return 1;
45}
46#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Alan Stern3e956372006-06-16 17:10:48 -040048/**
49 * dev_driver_string - Return a device's driver name, if at all possible
50 * @dev: struct device to get the name of
51 *
52 * Will return the device's driver's name if it is bound to a device. If
53 * the device is not bound to a device, it will return the name of the bus
54 * it is attached to. If it is not attached to a bus either, an empty
55 * string will be returned.
56 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070057const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040058{
59 return dev->driver ? dev->driver->name :
Jean Delvarea456b702007-03-09 16:33:10 +010060 (dev->bus ? dev->bus->name :
61 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040062}
Matthew Wilcox310a9222006-09-23 23:35:04 -060063EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define to_dev(obj) container_of(obj, struct device, kobj)
66#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
67
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080068static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
69 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080071 struct device_attribute *dev_attr = to_dev_attr(attr);
72 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050073 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040076 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080077 if (ret >= (ssize_t)PAGE_SIZE) {
78 print_symbol("dev_attr_show: %s returned bad count\n",
79 (unsigned long)dev_attr->show);
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return ret;
82}
83
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080084static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
85 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080087 struct device_attribute *dev_attr = to_dev_attr(attr);
88 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050089 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040092 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return ret;
94}
95
96static struct sysfs_ops dev_sysfs_ops = {
97 .show = dev_attr_show,
98 .store = dev_attr_store,
99};
100
101
102/**
103 * device_release - free device structure.
104 * @kobj: device's kobject.
105 *
106 * This is called once the reference count for the object
107 * reaches 0. We forward the call to the device's release
108 * method, which should handle actually freeing the structure.
109 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800110static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800112 struct device *dev = to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800113 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (dev->release)
116 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200117 else if (dev->type && dev->type->release)
118 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700119 else if (dev->class && dev->class->dev_release)
120 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700121 else
122 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800123 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100124 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800125 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600128static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .release = device_release,
130 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133
Kay Sievers312c0042005-11-16 09:00:00 +0100134static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 struct kobj_type *ktype = get_ktype(kobj);
137
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600138 if (ktype == &device_ktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct device *dev = to_dev(kobj);
140 if (dev->bus)
141 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700142 if (dev->class)
143 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 return 0;
146}
147
Kay Sievers312c0042005-11-16 09:00:00 +0100148static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct device *dev = to_dev(kobj);
151
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700152 if (dev->bus)
153 return dev->bus->name;
154 if (dev->class)
155 return dev->class->name;
156 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Kay Sievers7eff2e72007-08-14 15:15:12 +0200159static int dev_uevent(struct kset *kset, struct kobject *kobj,
160 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct device *dev = to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int retval = 0;
164
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200165 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700166 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200167 const char *tmp;
168 const char *name;
Kay Sieverse454cea2009-09-18 23:01:12 +0200169 mode_t mode = 0;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200170
Kay Sievers7eff2e72007-08-14 15:15:12 +0200171 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
172 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sieverse454cea2009-09-18 23:01:12 +0200173 name = device_get_devnode(dev, &mode, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200174 if (name) {
175 add_uevent_var(env, "DEVNAME=%s", name);
176 kfree(tmp);
Kay Sieverse454cea2009-09-18 23:01:12 +0200177 if (mode)
178 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200179 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700180 }
181
Kay Sievers414264f2007-03-12 21:08:57 +0100182 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200183 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100184
Kay Sievers239378f2006-10-07 21:54:55 +0200185 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200186 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200187
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200188#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200189 if (dev->class) {
190 struct device *parent = dev->parent;
191
192 /* find first bus device in parent chain */
193 while (parent && !parent->bus)
194 parent = parent->parent;
195 if (parent && parent->bus) {
196 const char *path;
197
198 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200199 if (path) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200200 add_uevent_var(env, "PHYSDEVPATH=%s", path);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200201 kfree(path);
202 }
Kay Sievers239378f2006-10-07 21:54:55 +0200203
Kay Sievers7eff2e72007-08-14 15:15:12 +0200204 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200205
206 if (parent->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200207 add_uevent_var(env, "PHYSDEVDRIVER=%s",
208 parent->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200209 }
210 } else if (dev->bus) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200211 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200212
213 if (dev->driver)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800214 add_uevent_var(env, "PHYSDEVDRIVER=%s",
215 dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200216 }
Kay Sievers239378f2006-10-07 21:54:55 +0200217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Kay Sievers7eff2e72007-08-14 15:15:12 +0200219 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100220 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200221 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200222 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800223 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100224 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
Kay Sievers7eff2e72007-08-14 15:15:12 +0200227 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700228 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200229 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200230 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800231 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100232 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800233 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200234 }
235
Kay Sievers7eff2e72007-08-14 15:15:12 +0200236 /* have the device type specific fuction add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200237 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200238 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200239 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800240 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100241 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800242 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700243 }
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return retval;
246}
247
Kay Sievers312c0042005-11-16 09:00:00 +0100248static struct kset_uevent_ops device_uevent_ops = {
249 .filter = dev_uevent_filter,
250 .name = dev_uevent_name,
251 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
Kay Sievers16574dc2007-04-06 01:40:38 +0200254static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
255 char *buf)
256{
257 struct kobject *top_kobj;
258 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200259 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200260 int i;
261 size_t count = 0;
262 int retval;
263
264 /* search the kset, the device belongs to */
265 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200266 while (!top_kobj->kset && top_kobj->parent)
267 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200268 if (!top_kobj->kset)
269 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200270
Kay Sievers16574dc2007-04-06 01:40:38 +0200271 kset = top_kobj->kset;
272 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
273 goto out;
274
275 /* respect filter */
276 if (kset->uevent_ops && kset->uevent_ops->filter)
277 if (!kset->uevent_ops->filter(kset, &dev->kobj))
278 goto out;
279
Kay Sievers7eff2e72007-08-14 15:15:12 +0200280 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
281 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200282 return -ENOMEM;
283
Kay Sievers16574dc2007-04-06 01:40:38 +0200284 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200285 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200286 if (retval)
287 goto out;
288
289 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200290 for (i = 0; i < env->envp_idx; i++)
291 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200292out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200293 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200294 return count;
295}
296
Kay Sieversa7fd6702005-10-01 14:49:43 +0200297static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
298 const char *buf, size_t count)
299{
Kay Sievers60a96a52007-07-08 22:29:26 +0200300 enum kobject_action action;
301
Kay Sievers5c5daf62007-08-12 20:43:55 +0200302 if (kobject_action_type(buf, count, &action) == 0) {
Kay Sievers60a96a52007-07-08 22:29:26 +0200303 kobject_uevent(&dev->kobj, action);
304 goto out;
305 }
306
307 dev_err(dev, "uevent: unsupported action-string; this will "
308 "be ignored in a future kernel version\n");
Kay Sievers312c0042005-11-16 09:00:00 +0100309 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sievers60a96a52007-07-08 22:29:26 +0200310out:
Kay Sieversa7fd6702005-10-01 14:49:43 +0200311 return count;
312}
313
Tejun Heoad6a1e12007-06-14 03:45:17 +0900314static struct device_attribute uevent_attr =
315 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
316
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500317static int device_add_attributes(struct device *dev,
318 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700319{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700320 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500321 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700322
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500323 if (attrs) {
324 for (i = 0; attr_name(attrs[i]); i++) {
325 error = device_create_file(dev, &attrs[i]);
326 if (error)
327 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700328 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500329 if (error)
330 while (--i >= 0)
331 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700332 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700333 return error;
334}
335
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500336static void device_remove_attributes(struct device *dev,
337 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700338{
339 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500340
341 if (attrs)
342 for (i = 0; attr_name(attrs[i]); i++)
343 device_remove_file(dev, &attrs[i]);
344}
345
346static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700347 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500348{
349 int error = 0;
350 int i;
351
352 if (groups) {
353 for (i = 0; groups[i]; i++) {
354 error = sysfs_create_group(&dev->kobj, groups[i]);
355 if (error) {
356 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800357 sysfs_remove_group(&dev->kobj,
358 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500359 break;
360 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700361 }
362 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500363 return error;
364}
365
366static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700367 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500368{
369 int i;
370
371 if (groups)
372 for (i = 0; groups[i]; i++)
373 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700374}
375
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700376static int device_add_attrs(struct device *dev)
377{
378 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200379 struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500380 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700381
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500382 if (class) {
383 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200384 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500385 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700386 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200387
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500388 if (type) {
389 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200390 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500391 goto err_remove_class_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200392 }
393
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500394 error = device_add_groups(dev, dev->groups);
395 if (error)
396 goto err_remove_type_groups;
397
398 return 0;
399
400 err_remove_type_groups:
401 if (type)
402 device_remove_groups(dev, type->groups);
403 err_remove_class_attrs:
404 if (class)
405 device_remove_attributes(dev, class->dev_attrs);
406
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700407 return error;
408}
409
410static void device_remove_attrs(struct device *dev)
411{
412 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200413 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700414
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500415 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200416
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500417 if (type)
418 device_remove_groups(dev, type->groups);
419
420 if (class)
421 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700422}
423
424
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700425static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
426 char *buf)
427{
428 return print_dev_t(buf, dev->devt);
429}
430
Tejun Heoad6a1e12007-06-14 03:45:17 +0900431static struct device_attribute devt_attr =
432 __ATTR(dev, S_IRUGO, show_dev, NULL);
433
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600434/* kset to create /sys/devices/ */
435struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800438 * device_create_file - create sysfs attribute file for device.
439 * @dev: device.
440 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800442int device_create_file(struct device *dev, struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 int error = 0;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100445 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 error = sysfs_create_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return error;
448}
449
450/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800451 * device_remove_file - remove sysfs attribute file.
452 * @dev: device.
453 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800455void device_remove_file(struct device *dev, struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100457 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700461/**
462 * device_create_bin_file - create sysfs binary attribute file for device.
463 * @dev: device.
464 * @attr: device binary attribute descriptor.
465 */
466int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
467{
468 int error = -EINVAL;
469 if (dev)
470 error = sysfs_create_bin_file(&dev->kobj, attr);
471 return error;
472}
473EXPORT_SYMBOL_GPL(device_create_bin_file);
474
475/**
476 * device_remove_bin_file - remove sysfs binary attribute file
477 * @dev: device.
478 * @attr: device binary attribute descriptor.
479 */
480void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
481{
482 if (dev)
483 sysfs_remove_bin_file(&dev->kobj, attr);
484}
485EXPORT_SYMBOL_GPL(device_remove_bin_file);
486
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400487/**
Alan Stern523ded72007-04-26 00:12:04 -0700488 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400489 * @dev: device.
490 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700491 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400492 *
493 * Attribute methods must not unregister themselves or their parent device
494 * (which would amount to the same thing). Attempts to do so will deadlock,
495 * since unregistration is mutually exclusive with driver callbacks.
496 *
497 * Instead methods can call this routine, which will attempt to allocate
498 * and schedule a workqueue request to call back @func with @dev as its
499 * argument in the workqueue's process context. @dev will be pinned until
500 * @func returns.
501 *
Alan Stern523ded72007-04-26 00:12:04 -0700502 * This routine is usually called via the inline device_schedule_callback(),
503 * which automatically sets @owner to THIS_MODULE.
504 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400505 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700506 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400507 *
508 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
509 * underlying sysfs routine (since it is intended for use by attribute
510 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
511 */
Alan Stern523ded72007-04-26 00:12:04 -0700512int device_schedule_callback_owner(struct device *dev,
513 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400514{
515 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700516 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400517}
Alan Stern523ded72007-04-26 00:12:04 -0700518EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400519
James Bottomley34bb61f2005-09-06 16:56:51 -0700520static void klist_children_get(struct klist_node *n)
521{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800522 struct device_private *p = to_device_private_parent(n);
523 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700524
525 get_device(dev);
526}
527
528static void klist_children_put(struct klist_node *n)
529{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800530 struct device_private *p = to_device_private_parent(n);
531 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700532
533 put_device(dev);
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800537 * device_initialize - init device structure.
538 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Cornelia Huck57394112008-09-03 18:26:40 +0200540 * This prepares the device for use by other layers by initializing
541 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800542 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200543 * that function, though it can also be called separately, so one
544 * may use @dev's fields. In particular, get_device()/put_device()
545 * may be used for reference counting of @dev after calling this
546 * function.
547 *
548 * NOTE: Use put_device() to give up your reference instead of freeing
549 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551void device_initialize(struct device *dev)
552{
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600553 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700554 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 INIT_LIST_HEAD(&dev->dma_pools);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800556 init_MUTEX(&dev->sem);
Tejun Heo9ac78492007-01-20 16:00:26 +0900557 spin_lock_init(&dev->devres_lock);
558 INIT_LIST_HEAD(&dev->devres_head);
David Brownell0ac85242005-09-12 19:39:34 -0700559 device_init_wakeup(dev, 0);
Alan Stern3b98aea2008-08-07 13:06:12 -0400560 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800561 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100564#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sieversda231fd2007-11-21 17:29:15 +0100565static struct kobject *get_device_parent(struct device *dev,
566 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100567{
Kay Sieversda231fd2007-11-21 17:29:15 +0100568 /* class devices without a parent live in /sys/class/<classname>/ */
Dmitry Torokhov3eb215d2007-10-07 12:22:21 -0400569 if (dev->class && (!parent || parent->class != dev->class))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700570 return &dev->class->p->class_subsys.kobj;
Kay Sieversda231fd2007-11-21 17:29:15 +0100571 /* all other devices keep their parent */
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100572 else if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100573 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100574
Cornelia Huckc744aea2007-01-08 20:16:44 +0100575 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100576}
Kay Sieversda231fd2007-11-21 17:29:15 +0100577
578static inline void cleanup_device_parent(struct device *dev) {}
Cornelia Huck63b69712008-01-21 16:09:44 +0100579static inline void cleanup_glue_dir(struct device *dev,
580 struct kobject *glue_dir) {}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100581#else
Kay Sievers86406242007-03-14 03:25:56 +0100582static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700583{
Kay Sievers86406242007-03-14 03:25:56 +0100584 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700585
Kay Sievers86406242007-03-14 03:25:56 +0100586 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800587 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600588 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700589
Kay Sievers86406242007-03-14 03:25:56 +0100590 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700591}
592
Kay Sieversda231fd2007-11-21 17:29:15 +0100593static struct kobject *get_device_parent(struct device *dev,
594 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100595{
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800596 int retval;
597
Kay Sievers86406242007-03-14 03:25:56 +0100598 if (dev->class) {
599 struct kobject *kobj = NULL;
600 struct kobject *parent_kobj;
601 struct kobject *k;
602
603 /*
604 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100605 * Class-devices with a non class-device as parent, live
606 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100607 */
608 if (parent == NULL)
609 parent_kobj = virtual_device_parent(dev);
610 else if (parent->class)
611 return &parent->kobj;
612 else
613 parent_kobj = &parent->kobj;
614
615 /* find our class-directory at the parent and reference it */
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500616 spin_lock(&dev->class->p->class_dirs.list_lock);
617 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100618 if (k->parent == parent_kobj) {
619 kobj = kobject_get(k);
620 break;
621 }
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500622 spin_unlock(&dev->class->p->class_dirs.list_lock);
Kay Sievers86406242007-03-14 03:25:56 +0100623 if (kobj)
624 return kobj;
625
626 /* or create a new class-directory at the parent device */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800627 k = kobject_create();
628 if (!k)
629 return NULL;
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500630 k->kset = &dev->class->p->class_dirs;
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700631 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800632 if (retval < 0) {
633 kobject_put(k);
634 return NULL;
635 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100636 /* do not emit an uevent for this simple "glue" directory */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800637 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100638 }
639
640 if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100641 return &parent->kobj;
642 return NULL;
643}
Kay Sieversda231fd2007-11-21 17:29:15 +0100644
Cornelia Huck63b69712008-01-21 16:09:44 +0100645static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100646{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100647 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100648 if (!glue_dir || !dev->class ||
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500649 glue_dir->kset != &dev->class->p->class_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100650 return;
651
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100652 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100653}
Cornelia Huck63b69712008-01-21 16:09:44 +0100654
655static void cleanup_device_parent(struct device *dev)
656{
657 cleanup_glue_dir(dev, dev->kobj.parent);
658}
Cornelia Huckc744aea2007-01-08 20:16:44 +0100659#endif
Kay Sievers86406242007-03-14 03:25:56 +0100660
Cornelia Huck63b69712008-01-21 16:09:44 +0100661static void setup_parent(struct device *dev, struct device *parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100662{
663 struct kobject *kobj;
664 kobj = get_device_parent(dev, parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +0100665 if (kobj)
666 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100667}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100668
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700669static int device_add_class_symlinks(struct device *dev)
670{
671 int error;
672
673 if (!dev->class)
674 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100675
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700676 error = sysfs_create_link(&dev->kobj,
677 &dev->class->p->class_subsys.kobj,
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700678 "subsystem");
679 if (error)
680 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100681
682#ifdef CONFIG_SYSFS_DEPRECATED
683 /* stacked class devices need a symlink in the class directory */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700684 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800685 device_is_not_partition(dev)) {
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700686 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100687 &dev->kobj, dev_name(dev));
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700688 if (error)
689 goto out_subsys;
690 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100691
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800692 if (dev->parent && device_is_not_partition(dev)) {
Kay Sieversda231fd2007-11-21 17:29:15 +0100693 struct device *parent = dev->parent;
694 char *class_name;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700695
Kay Sieversda231fd2007-11-21 17:29:15 +0100696 /*
697 * stacked class devices have the 'device' link
698 * pointing to the bus device instead of the parent
699 */
700 while (parent->class && !parent->bus && parent->parent)
701 parent = parent->parent;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700702
Kay Sieversda231fd2007-11-21 17:29:15 +0100703 error = sysfs_create_link(&dev->kobj,
704 &parent->kobj,
705 "device");
706 if (error)
707 goto out_busid;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700708
Kay Sieversda231fd2007-11-21 17:29:15 +0100709 class_name = make_class_name(dev->class->name,
710 &dev->kobj);
711 if (class_name)
712 error = sysfs_create_link(&dev->parent->kobj,
713 &dev->kobj, class_name);
714 kfree(class_name);
715 if (error)
716 goto out_device;
717 }
718 return 0;
719
720out_device:
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800721 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100722 sysfs_remove_link(&dev->kobj, "device");
723out_busid:
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700724 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800725 device_is_not_partition(dev))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700726 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100727 dev_name(dev));
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700728#else
Kay Sieversda231fd2007-11-21 17:29:15 +0100729 /* link in the class directory pointing to the device */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700730 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100731 &dev->kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100732 if (error)
733 goto out_subsys;
734
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800735 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700736 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
737 "device");
738 if (error)
739 goto out_busid;
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700740 }
741 return 0;
742
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700743out_busid:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100744 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100745#endif
746
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700747out_subsys:
748 sysfs_remove_link(&dev->kobj, "subsystem");
749out:
750 return error;
751}
752
753static void device_remove_class_symlinks(struct device *dev)
754{
755 if (!dev->class)
756 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100757
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700758#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800759 if (dev->parent && device_is_not_partition(dev)) {
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700760 char *class_name;
761
762 class_name = make_class_name(dev->class->name, &dev->kobj);
763 if (class_name) {
764 sysfs_remove_link(&dev->parent->kobj, class_name);
765 kfree(class_name);
766 }
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700767 sysfs_remove_link(&dev->kobj, "device");
768 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100769
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700770 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800771 device_is_not_partition(dev))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700772 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100773 dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100774#else
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800775 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100776 sysfs_remove_link(&dev->kobj, "device");
777
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100778 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100779#endif
780
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700781 sysfs_remove_link(&dev->kobj, "subsystem");
782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000785 * dev_set_name - set a device name
786 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700787 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000788 */
789int dev_set_name(struct device *dev, const char *fmt, ...)
790{
791 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100792 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000793
794 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100795 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000796 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100797 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000798}
799EXPORT_SYMBOL_GPL(dev_set_name);
800
801/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700802 * device_to_dev_kobj - select a /sys/dev/ directory for the device
803 * @dev: device
804 *
805 * By default we select char/ for new entries. Setting class->dev_obj
806 * to NULL prevents an entry from being created. class->dev_kobj must
807 * be set (or cleared) before any devices are registered to the class
808 * otherwise device_create_sys_dev_entry() and
809 * device_remove_sys_dev_entry() will disagree about the the presence
810 * of the link.
811 */
812static struct kobject *device_to_dev_kobj(struct device *dev)
813{
814 struct kobject *kobj;
815
816 if (dev->class)
817 kobj = dev->class->dev_kobj;
818 else
819 kobj = sysfs_dev_char_kobj;
820
821 return kobj;
822}
823
824static int device_create_sys_dev_entry(struct device *dev)
825{
826 struct kobject *kobj = device_to_dev_kobj(dev);
827 int error = 0;
828 char devt_str[15];
829
830 if (kobj) {
831 format_dev_t(devt_str, dev->devt);
832 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
833 }
834
835 return error;
836}
837
838static void device_remove_sys_dev_entry(struct device *dev)
839{
840 struct kobject *kobj = device_to_dev_kobj(dev);
841 char devt_str[15];
842
843 if (kobj) {
844 format_dev_t(devt_str, dev->devt);
845 sysfs_remove_link(kobj, devt_str);
846 }
847}
848
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700849int device_private_init(struct device *dev)
850{
851 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
852 if (!dev->p)
853 return -ENOMEM;
854 dev->p->device = dev;
855 klist_init(&dev->p->klist_children, klist_children_get,
856 klist_children_put);
857 return 0;
858}
859
Dan Williamse105b8b2008-04-21 10:51:07 -0700860/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800861 * device_add - add device to device hierarchy.
862 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800864 * This is part 2 of device_register(), though may be called
865 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *
Cornelia Huck57394112008-09-03 18:26:40 +0200867 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800868 * to the global and sibling lists for the device, then
869 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200870 *
871 * NOTE: _Never_ directly free @dev after calling this function, even
872 * if it returned an error! Always use put_device() to give up your
873 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
875int device_add(struct device *dev)
876{
877 struct device *parent = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200878 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700879 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700882 if (!dev)
883 goto done;
884
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800885 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700886 error = device_private_init(dev);
887 if (error)
888 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800889 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800890
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100891 /*
892 * for statically allocated devices, which should all be converted
893 * some day, we need to initialize the name. We prevent reading back
894 * the name, and force the use of dev_name()
895 */
896 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700897 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100898 dev->init_name = NULL;
899 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700900
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100901 if (!dev_name(dev))
Kay Sievers5c8563d2009-05-28 14:24:07 -0700902 goto name_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100904 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 parent = get_device(dev->parent);
Cornelia Huck63b69712008-01-21 16:09:44 +0100907 setup_parent(dev, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Yinghai Lu0d358f22008-02-19 03:20:41 -0800909 /* use parent numa_node */
910 if (parent)
911 set_dev_node(dev, dev_to_node(parent));
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -0700914 /* we require the name to be set before, and pass NULL */
915 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100916 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200918
Brian Walsh37022642006-08-14 22:43:19 -0700919 /* notify platform of device entry */
920 if (platform_notify)
921 platform_notify(dev);
922
Tejun Heoad6a1e12007-06-14 03:45:17 +0900923 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200924 if (error)
925 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200926
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700927 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900928 error = device_create_file(dev, &devt_attr);
929 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +0200930 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -0700931
932 error = device_create_sys_dev_entry(dev);
933 if (error)
934 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +0200935
936 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700937 }
938
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700939 error = device_add_class_symlinks(dev);
940 if (error)
941 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700942 error = device_add_attrs(dev);
943 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700944 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700945 error = bus_add_device(dev);
946 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -0400948 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100949 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -0400950 goto DPMError;
951 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -0500952
953 /* Notify clients of device addition. This call must come
954 * after dpm_sysf_add() and before kobject_uevent().
955 */
956 if (dev->bus)
957 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
958 BUS_NOTIFY_ADD_DEVICE, dev);
959
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200960 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -0400961 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800963 klist_add_tail(&dev->p->knode_parent,
964 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700966 if (dev->class) {
Dave Youngf75b1c62008-05-28 09:28:39 -0700967 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200968 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +0200969 klist_add_tail(&dev->knode_class,
970 &dev->class->p->class_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200971
972 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -0700973 list_for_each_entry(class_intf,
974 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200975 if (class_intf->add_dev)
976 class_intf->add_dev(dev, class_intf);
Dave Youngf75b1c62008-05-28 09:28:39 -0700977 mutex_unlock(&dev->class->p->class_mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700978 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700979done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 put_device(dev);
981 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -0400982 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100983 bus_remove_device(dev);
984 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +0000985 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700986 AttrsError:
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700987 device_remove_class_symlinks(dev);
988 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900989 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -0700990 device_remove_sys_dev_entry(dev);
991 devtattrError:
992 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +0900993 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200994 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900995 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700996 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100997 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 kobject_del(&dev->kobj);
999 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001000 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (parent)
1002 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001003name_error:
1004 kfree(dev->p);
1005 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001006 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001010 * device_register - register a device with the system.
1011 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001013 * This happens in two clean steps - initialize the device
1014 * and add it to the system. The two steps can be called
1015 * separately, but this is the easiest and most common.
1016 * I.e. you should only call the two helpers separately if
1017 * have a clearly defined need to use and refcount the device
1018 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001019 *
1020 * NOTE: _Never_ directly free @dev after calling this function, even
1021 * if it returned an error! Always use put_device() to give up the
1022 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024int device_register(struct device *dev)
1025{
1026 device_initialize(dev);
1027 return device_add(dev);
1028}
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001031 * get_device - increment reference count for device.
1032 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001034 * This simply forwards the call to kobject_get(), though
1035 * we do take care to provide for the case that we get a NULL
1036 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001038struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001044 * put_device - decrement reference count.
1045 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001047void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001049 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (dev)
1051 kobject_put(&dev->kobj);
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001055 * device_del - delete device from system.
1056 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001058 * This is the first part of the device unregistration
1059 * sequence. This removes the device from the lists we control
1060 * from here, has it removed from the other driver model
1061 * subsystems it was added to in device_add(), and removes it
1062 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001064 * NOTE: this should be called manually _iff_ device_add() was
1065 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001067void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001069 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001070 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Alan Sternec0676ee2008-12-05 14:10:31 -05001072 /* Notify clients of device removal. This call must come
1073 * before dpm_sysfs_remove().
1074 */
1075 if (dev->bus)
1076 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1077 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001078 device_pm_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001079 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001081 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001082 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001083 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001084 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001085 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001086 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001087 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001088 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001089
Dave Youngf75b1c62008-05-28 09:28:39 -07001090 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001091 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001092 list_for_each_entry(class_intf,
1093 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001094 if (class_intf->remove_dev)
1095 class_intf->remove_dev(dev, class_intf);
1096 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001097 klist_del(&dev->knode_class);
Dave Youngf75b1c62008-05-28 09:28:39 -07001098 mutex_unlock(&dev->class->p->class_mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001099 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001100 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001101 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001102 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Tejun Heo2f8d16a2007-03-09 19:34:19 +09001104 /*
1105 * Some platform devices are driven without driver attached
1106 * and managed resources may have been acquired. Make sure
1107 * all resources are released.
1108 */
1109 devres_release_all(dev);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* Notify the platform of the removal, in case they
1112 * need to do anything...
1113 */
1114 if (platform_notify_remove)
1115 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001116 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001117 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001119 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001123 * device_unregister - unregister device from system.
1124 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001126 * We do this in two parts, like we do device_register(). First,
1127 * we remove it from all the subsystems with device_del(), then
1128 * we decrement the reference count via put_device(). If that
1129 * is the final reference count, the device will be cleaned up
1130 * via device_release() above. Otherwise, the structure will
1131 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001133void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001135 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 device_del(dev);
1137 put_device(dev);
1138}
1139
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001140static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001141{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001142 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001143 struct device *dev = NULL;
1144 struct device_private *p;
1145
1146 if (n) {
1147 p = to_device_private_parent(n);
1148 dev = p->device;
1149 }
1150 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001154 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001155 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001156 * @mode: returned file access mode
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001157 * @tmp: possibly allocated string
1158 *
1159 * Return the relative path of a possible device node.
1160 * Non-default names may need to allocate a memory to compose
1161 * a name. This memory is returned in tmp and needs to be
1162 * freed by the caller.
1163 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001164const char *device_get_devnode(struct device *dev,
1165 mode_t *mode, const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001166{
1167 char *s;
1168
1169 *tmp = NULL;
1170
1171 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001172 if (dev->type && dev->type->devnode)
1173 *tmp = dev->type->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001174 if (*tmp)
1175 return *tmp;
1176
1177 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001178 if (dev->class && dev->class->devnode)
1179 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001180 if (*tmp)
1181 return *tmp;
1182
1183 /* return name without allocation, tmp == NULL */
1184 if (strchr(dev_name(dev), '!') == NULL)
1185 return dev_name(dev);
1186
1187 /* replace '!' in the name with '/' */
1188 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1189 if (!*tmp)
1190 return NULL;
1191 while ((s = strchr(*tmp, '!')))
1192 s[0] = '/';
1193 return *tmp;
1194}
1195
1196/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001197 * device_for_each_child - device child iterator.
1198 * @parent: parent struct device.
1199 * @data: data for the callback.
1200 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001202 * Iterate over @parent's child devices, and call @fn for each,
1203 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001205 * We check the return of @fn each time. If it returns anything
1206 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001208int device_for_each_child(struct device *parent, void *data,
1209 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001211 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001212 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int error = 0;
1214
Greg Kroah-Hartman014c90d2009-04-15 16:00:12 -07001215 if (!parent->p)
1216 return 0;
1217
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001218 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001219 while ((child = next_device(&i)) && !error)
1220 error = fn(child, data);
1221 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return error;
1223}
1224
Cornelia Huck5ab69982006-11-16 15:42:07 +01001225/**
1226 * device_find_child - device iterator for locating a particular device.
1227 * @parent: parent struct device
1228 * @data: Data to pass to match function
1229 * @match: Callback function to check device
1230 *
1231 * This is similar to the device_for_each_child() function above, but it
1232 * returns a reference to a device that is 'found' for later use, as
1233 * determined by the @match callback.
1234 *
1235 * The callback should return 0 if the device doesn't match and non-zero
1236 * if it does. If the callback returns non-zero and a reference to the
1237 * current device can be obtained, this function will return to the caller
1238 * and not iterate over any more devices.
1239 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001240struct device *device_find_child(struct device *parent, void *data,
1241 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001242{
1243 struct klist_iter i;
1244 struct device *child;
1245
1246 if (!parent)
1247 return NULL;
1248
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001249 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001250 while ((child = next_device(&i)))
1251 if (match(child, data) && get_device(child))
1252 break;
1253 klist_iter_exit(&i);
1254 return child;
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257int __init devices_init(void)
1258{
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -06001259 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1260 if (!devices_kset)
1261 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001262 dev_kobj = kobject_create_and_add("dev", NULL);
1263 if (!dev_kobj)
1264 goto dev_kobj_err;
1265 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1266 if (!sysfs_dev_block_kobj)
1267 goto block_kobj_err;
1268 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1269 if (!sysfs_dev_char_kobj)
1270 goto char_kobj_err;
1271
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -06001272 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001273
1274 char_kobj_err:
1275 kobject_put(sysfs_dev_block_kobj);
1276 block_kobj_err:
1277 kobject_put(dev_kobj);
1278 dev_kobj_err:
1279 kset_unregister(devices_kset);
1280 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001284EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286EXPORT_SYMBOL_GPL(device_initialize);
1287EXPORT_SYMBOL_GPL(device_add);
1288EXPORT_SYMBOL_GPL(device_register);
1289
1290EXPORT_SYMBOL_GPL(device_del);
1291EXPORT_SYMBOL_GPL(device_unregister);
1292EXPORT_SYMBOL_GPL(get_device);
1293EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295EXPORT_SYMBOL_GPL(device_create_file);
1296EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001297
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001298struct root_device
1299{
1300 struct device dev;
1301 struct module *owner;
1302};
1303
1304#define to_root_device(dev) container_of(dev, struct root_device, dev)
1305
1306static void root_device_release(struct device *dev)
1307{
1308 kfree(to_root_device(dev));
1309}
1310
1311/**
1312 * __root_device_register - allocate and register a root device
1313 * @name: root device name
1314 * @owner: owner module of the root device, usually THIS_MODULE
1315 *
1316 * This function allocates a root device and registers it
1317 * using device_register(). In order to free the returned
1318 * device, use root_device_unregister().
1319 *
1320 * Root devices are dummy devices which allow other devices
1321 * to be grouped under /sys/devices. Use this function to
1322 * allocate a root device and then use it as the parent of
1323 * any device which should appear under /sys/devices/{name}
1324 *
1325 * The /sys/devices/{name} directory will also contain a
1326 * 'module' symlink which points to the @owner directory
1327 * in sysfs.
1328 *
1329 * Note: You probably want to use root_device_register().
1330 */
1331struct device *__root_device_register(const char *name, struct module *owner)
1332{
1333 struct root_device *root;
1334 int err = -ENOMEM;
1335
1336 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1337 if (!root)
1338 return ERR_PTR(err);
1339
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001340 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001341 if (err) {
1342 kfree(root);
1343 return ERR_PTR(err);
1344 }
1345
1346 root->dev.release = root_device_release;
1347
1348 err = device_register(&root->dev);
1349 if (err) {
1350 put_device(&root->dev);
1351 return ERR_PTR(err);
1352 }
1353
1354#ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1355 if (owner) {
1356 struct module_kobject *mk = &owner->mkobj;
1357
1358 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1359 if (err) {
1360 device_unregister(&root->dev);
1361 return ERR_PTR(err);
1362 }
1363 root->owner = owner;
1364 }
1365#endif
1366
1367 return &root->dev;
1368}
1369EXPORT_SYMBOL_GPL(__root_device_register);
1370
1371/**
1372 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001373 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001374 *
1375 * This function unregisters and cleans up a device that was created by
1376 * root_device_register().
1377 */
1378void root_device_unregister(struct device *dev)
1379{
1380 struct root_device *root = to_root_device(dev);
1381
1382 if (root->owner)
1383 sysfs_remove_link(&root->dev.kobj, "module");
1384
1385 device_unregister(dev);
1386}
1387EXPORT_SYMBOL_GPL(root_device_unregister);
1388
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001389
1390static void device_create_release(struct device *dev)
1391{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001392 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001393 kfree(dev);
1394}
1395
1396/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001397 * device_create_vargs - creates a device and registers it with sysfs
1398 * @class: pointer to the struct class that this device should be registered to
1399 * @parent: pointer to the parent struct device of this new device, if any
1400 * @devt: the dev_t for the char device to be added
1401 * @drvdata: the data to be added to the device for callbacks
1402 * @fmt: string for the device's name
1403 * @args: va_list for the device's name
1404 *
1405 * This function can be used by char device classes. A struct device
1406 * will be created in sysfs, registered to the specified class.
1407 *
1408 * A "dev" file will be created, showing the dev_t for the device, if
1409 * the dev_t is not 0,0.
1410 * If a pointer to a parent struct device is passed in, the newly created
1411 * struct device will be a child of that device in sysfs.
1412 * The pointer to the struct device will be returned from the call.
1413 * Any further sysfs files that might be required can be created using this
1414 * pointer.
1415 *
1416 * Note: the struct class passed to this function must have previously
1417 * been created with a call to class_create().
1418 */
1419struct device *device_create_vargs(struct class *class, struct device *parent,
1420 dev_t devt, void *drvdata, const char *fmt,
1421 va_list args)
1422{
1423 struct device *dev = NULL;
1424 int retval = -ENODEV;
1425
1426 if (class == NULL || IS_ERR(class))
1427 goto error;
1428
1429 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1430 if (!dev) {
1431 retval = -ENOMEM;
1432 goto error;
1433 }
1434
1435 dev->devt = devt;
1436 dev->class = class;
1437 dev->parent = parent;
1438 dev->release = device_create_release;
1439 dev_set_drvdata(dev, drvdata);
1440
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001441 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1442 if (retval)
1443 goto error;
1444
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001445 retval = device_register(dev);
1446 if (retval)
1447 goto error;
1448
1449 return dev;
1450
1451error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001452 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001453 return ERR_PTR(retval);
1454}
1455EXPORT_SYMBOL_GPL(device_create_vargs);
1456
1457/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001458 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001459 * @class: pointer to the struct class that this device should be registered to
1460 * @parent: pointer to the parent struct device of this new device, if any
1461 * @devt: the dev_t for the char device to be added
1462 * @drvdata: the data to be added to the device for callbacks
1463 * @fmt: string for the device's name
1464 *
1465 * This function can be used by char device classes. A struct device
1466 * will be created in sysfs, registered to the specified class.
1467 *
1468 * A "dev" file will be created, showing the dev_t for the device, if
1469 * the dev_t is not 0,0.
1470 * If a pointer to a parent struct device is passed in, the newly created
1471 * struct device will be a child of that device in sysfs.
1472 * The pointer to the struct device will be returned from the call.
1473 * Any further sysfs files that might be required can be created using this
1474 * pointer.
1475 *
1476 * Note: the struct class passed to this function must have previously
1477 * been created with a call to class_create().
1478 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001479struct device *device_create(struct class *class, struct device *parent,
1480 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001481{
1482 va_list vargs;
1483 struct device *dev;
1484
1485 va_start(vargs, fmt);
1486 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1487 va_end(vargs);
1488 return dev;
1489}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001490EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001491
Dave Youngcd354492008-01-28 16:56:11 +08001492static int __match_devt(struct device *dev, void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001493{
Dave Youngcd354492008-01-28 16:56:11 +08001494 dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001495
Dave Youngcd354492008-01-28 16:56:11 +08001496 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001497}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001498
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001499/**
1500 * device_destroy - removes a device that was created with device_create()
1501 * @class: pointer to the struct class that this device was registered with
1502 * @devt: the dev_t of the device that was previously registered
1503 *
1504 * This call unregisters and cleans up a device that was created with a
1505 * call to device_create().
1506 */
1507void device_destroy(struct class *class, dev_t devt)
1508{
1509 struct device *dev;
1510
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001511 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001512 if (dev) {
1513 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001514 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001515 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001516}
1517EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001518
1519/**
1520 * device_rename - renames a device
1521 * @dev: the pointer to the struct device to be renamed
1522 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001523 *
1524 * It is the responsibility of the caller to provide mutual
1525 * exclusion between two different calls of device_rename
1526 * on the same device to ensure that new_name is valid and
1527 * won't conflict with other devices.
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001528 */
1529int device_rename(struct device *dev, char *new_name)
1530{
1531 char *old_class_name = NULL;
1532 char *new_class_name = NULL;
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001533 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001534 int error;
1535
1536 dev = get_device(dev);
1537 if (!dev)
1538 return -EINVAL;
1539
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001540 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001541 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001542
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001543#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001544 if ((dev->class) && (dev->parent))
1545 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001546#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001547
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001548 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001549 if (!old_device_name) {
1550 error = -ENOMEM;
1551 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001552 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001553
1554 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001555 if (error)
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001556 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001557
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001558#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001559 if (old_class_name) {
1560 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1561 if (new_class_name) {
Cornelia Huck36ce6da2008-06-10 11:09:08 +02001562 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1563 &dev->kobj,
1564 new_class_name);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001565 if (error)
1566 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001567 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1568 }
1569 }
Kay Sievers60b8cab2007-10-26 20:07:44 +02001570#else
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001571 if (dev->class) {
Cornelia Huck36ce6da2008-06-10 11:09:08 +02001572 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001573 &dev->kobj, dev_name(dev));
Stephen Hemminger0599ad52008-05-14 22:34:16 -07001574 if (error)
1575 goto out;
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001576 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -05001577 old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001578 }
Kay Sievers60b8cab2007-10-26 20:07:44 +02001579#endif
1580
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001581out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001582 put_device(dev);
1583
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001584 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001585 kfree(old_class_name);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001586 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001587
1588 return error;
1589}
Johannes Berga2807db2007-02-28 12:38:31 +01001590EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001591
1592static int device_move_class_links(struct device *dev,
1593 struct device *old_parent,
1594 struct device *new_parent)
1595{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001596 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001597#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huck8a824722006-11-20 17:07:51 +01001598 char *class_name;
1599
1600 class_name = make_class_name(dev->class->name, &dev->kobj);
1601 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001602 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001603 goto out;
1604 }
1605 if (old_parent) {
1606 sysfs_remove_link(&dev->kobj, "device");
1607 sysfs_remove_link(&old_parent->kobj, class_name);
1608 }
Cornelia Huckc744aea2007-01-08 20:16:44 +01001609 if (new_parent) {
1610 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1611 "device");
1612 if (error)
1613 goto out;
1614 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1615 class_name);
1616 if (error)
1617 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001618 } else
Cornelia Huckc744aea2007-01-08 20:16:44 +01001619 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001620out:
1621 kfree(class_name);
1622 return error;
1623#else
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001624 if (old_parent)
1625 sysfs_remove_link(&dev->kobj, "device");
1626 if (new_parent)
1627 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1628 "device");
1629 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001630#endif
1631}
1632
1633/**
1634 * device_move - moves a device to a new parent
1635 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aea2007-01-08 20:16:44 +01001636 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001637 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001638 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001639int device_move(struct device *dev, struct device *new_parent,
1640 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001641{
1642 int error;
1643 struct device *old_parent;
Cornelia Huckc744aea2007-01-08 20:16:44 +01001644 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001645
1646 dev = get_device(dev);
1647 if (!dev)
1648 return -EINVAL;
1649
Cornelia Huckffa6a702009-03-04 12:44:00 +01001650 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001651 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001652 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001653
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001654 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1655 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aea2007-01-08 20:16:44 +01001656 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001657 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001658 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001659 put_device(new_parent);
1660 goto out;
1661 }
1662 old_parent = dev->parent;
1663 dev->parent = new_parent;
1664 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001665 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001666 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001667 klist_add_tail(&dev->p->knode_parent,
1668 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001669 set_dev_node(dev, dev_to_node(new_parent));
1670 }
1671
Cornelia Huck8a824722006-11-20 17:07:51 +01001672 if (!dev->class)
1673 goto out_put;
1674 error = device_move_class_links(dev, old_parent, new_parent);
1675 if (error) {
1676 /* We ignore errors on cleanup since we're hosed anyway... */
1677 device_move_class_links(dev, new_parent, old_parent);
1678 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +01001679 if (new_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001680 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001681 dev->parent = old_parent;
1682 if (old_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001683 klist_add_tail(&dev->p->knode_parent,
1684 &old_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001685 set_dev_node(dev, dev_to_node(old_parent));
1686 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001687 }
Cornelia Huck63b69712008-01-21 16:09:44 +01001688 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001689 put_device(new_parent);
1690 goto out;
1691 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001692 switch (dpm_order) {
1693 case DPM_ORDER_NONE:
1694 break;
1695 case DPM_ORDER_DEV_AFTER_PARENT:
1696 device_pm_move_after(dev, new_parent);
1697 break;
1698 case DPM_ORDER_PARENT_BEFORE_DEV:
1699 device_pm_move_before(new_parent, dev);
1700 break;
1701 case DPM_ORDER_DEV_LAST:
1702 device_pm_move_last(dev);
1703 break;
1704 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001705out_put:
1706 put_device(old_parent);
1707out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001708 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001709 put_device(dev);
1710 return error;
1711}
Cornelia Huck8a824722006-11-20 17:07:51 +01001712EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001713
1714/**
1715 * device_shutdown - call ->shutdown() on each device to shutdown.
1716 */
1717void device_shutdown(void)
1718{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001719 struct device *dev, *devn;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001720
1721 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1722 kobj.entry) {
1723 if (dev->bus && dev->bus->shutdown) {
1724 dev_dbg(dev, "shutdown\n");
1725 dev->bus->shutdown(dev);
1726 } else if (dev->driver && dev->driver->shutdown) {
1727 dev_dbg(dev, "shutdown\n");
1728 dev->driver->shutdown(dev);
1729 }
1730 }
Dan Williamse105b8b2008-04-21 10:51:07 -07001731 kobject_put(sysfs_dev_char_kobj);
1732 kobject_put(sysfs_dev_block_kobj);
1733 kobject_put(dev_kobj);
Shaohua Li401097e2009-05-12 13:37:57 -07001734 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001735}