blob: 9630fbdf4e6c210eb09a92f4489bb55f60e3b3a7 [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>
Dave Youngf75b1c62008-05-28 09:28:39 -070023#include <linux/mutex.h>
Shaohua Li401097e2009-05-12 13:37:57 -070024#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "base.h"
27#include "power/power.h"
28
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080029int (*platform_notify)(struct device *dev) = NULL;
30int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070031static struct kobject *dev_kobj;
32struct kobject *sysfs_dev_char_kobj;
33struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080035#ifdef CONFIG_BLOCK
36static inline int device_is_not_partition(struct device *dev)
37{
38 return !(dev->type == &part_type);
39}
40#else
41static inline int device_is_not_partition(struct device *dev)
42{
43 return 1;
44}
45#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Alan Stern3e956372006-06-16 17:10:48 -040047/**
48 * dev_driver_string - Return a device's driver name, if at all possible
49 * @dev: struct device to get the name of
50 *
51 * Will return the device's driver's name if it is bound to a device. If
52 * the device is not bound to a device, it will return the name of the bus
53 * it is attached to. If it is not attached to a bus either, an empty
54 * string will be returned.
55 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070056const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040057{
Alan Stern35899722009-12-04 11:06:57 -050058 struct device_driver *drv;
59
60 /* dev->driver can change to NULL underneath us because of unbinding,
61 * so be careful about accessing it. dev->bus and dev->class should
62 * never change once they are set, so they don't need special care.
63 */
64 drv = ACCESS_ONCE(dev->driver);
65 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +010066 (dev->bus ? dev->bus->name :
67 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040068}
Matthew Wilcox310a9222006-09-23 23:35:04 -060069EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define to_dev(obj) container_of(obj, struct device, kobj)
72#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
73
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080074static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
75 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080077 struct device_attribute *dev_attr = to_dev_attr(attr);
78 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050079 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040082 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080083 if (ret >= (ssize_t)PAGE_SIZE) {
84 print_symbol("dev_attr_show: %s returned bad count\n",
85 (unsigned long)dev_attr->show);
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return ret;
88}
89
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080090static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
91 const char *buf, size_t count)
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);
94 struct device *dev = 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->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040098 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return ret;
100}
101
Emese Revfy52cf25d2010-01-19 02:58:23 +0100102static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .show = dev_attr_show,
104 .store = dev_attr_store,
105};
106
107
108/**
109 * device_release - free device structure.
110 * @kobj: device's kobject.
111 *
112 * This is called once the reference count for the object
113 * reaches 0. We forward the call to the device's release
114 * method, which should handle actually freeing the structure.
115 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800116static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800118 struct device *dev = to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800119 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (dev->release)
122 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200123 else if (dev->type && dev->type->release)
124 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700125 else if (dev->class && dev->class->dev_release)
126 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700127 else
128 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800129 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100130 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800131 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700134static const void *device_namespace(struct kobject *kobj)
135{
136 struct device *dev = to_dev(kobj);
137 const void *ns = NULL;
138
139 if (dev->class && dev->class->ns_type)
140 ns = dev->class->namespace(dev);
141
142 return ns;
143}
144
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600145static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .release = device_release,
147 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700148 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151
Kay Sievers312c0042005-11-16 09:00:00 +0100152static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct kobj_type *ktype = get_ktype(kobj);
155
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600156 if (ktype == &device_ktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct device *dev = to_dev(kobj);
158 if (dev->bus)
159 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700160 if (dev->class)
161 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 return 0;
164}
165
Kay Sievers312c0042005-11-16 09:00:00 +0100166static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct device *dev = to_dev(kobj);
169
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700170 if (dev->bus)
171 return dev->bus->name;
172 if (dev->class)
173 return dev->class->name;
174 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Kay Sievers7eff2e72007-08-14 15:15:12 +0200177static int dev_uevent(struct kset *kset, struct kobject *kobj,
178 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct device *dev = to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int retval = 0;
182
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200183 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700184 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200185 const char *tmp;
186 const char *name;
Kay Sieverse454cea2009-09-18 23:01:12 +0200187 mode_t mode = 0;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200188
Kay Sievers7eff2e72007-08-14 15:15:12 +0200189 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
190 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sieverse454cea2009-09-18 23:01:12 +0200191 name = device_get_devnode(dev, &mode, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200192 if (name) {
193 add_uevent_var(env, "DEVNAME=%s", name);
194 kfree(tmp);
Kay Sieverse454cea2009-09-18 23:01:12 +0200195 if (mode)
196 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200197 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700198 }
199
Kay Sievers414264f2007-03-12 21:08:57 +0100200 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200201 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100202
Kay Sievers239378f2006-10-07 21:54:55 +0200203 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200204 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200205
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200206#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200207 if (dev->class) {
208 struct device *parent = dev->parent;
209
210 /* find first bus device in parent chain */
211 while (parent && !parent->bus)
212 parent = parent->parent;
213 if (parent && parent->bus) {
214 const char *path;
215
216 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200217 if (path) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200218 add_uevent_var(env, "PHYSDEVPATH=%s", path);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200219 kfree(path);
220 }
Kay Sievers239378f2006-10-07 21:54:55 +0200221
Kay Sievers7eff2e72007-08-14 15:15:12 +0200222 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200223
224 if (parent->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200225 add_uevent_var(env, "PHYSDEVDRIVER=%s",
226 parent->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200227 }
228 } else if (dev->bus) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200229 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200230
231 if (dev->driver)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800232 add_uevent_var(env, "PHYSDEVDRIVER=%s",
233 dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200234 }
Kay Sievers239378f2006-10-07 21:54:55 +0200235#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Kay Sievers7eff2e72007-08-14 15:15:12 +0200237 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100238 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200239 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200240 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800241 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100242 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Kay Sievers7eff2e72007-08-14 15:15:12 +0200245 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700246 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200247 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200248 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800249 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100250 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800251 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200252 }
253
Kay Sievers7eff2e72007-08-14 15:15:12 +0200254 /* have the device type specific fuction add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200255 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200256 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200257 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800258 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100259 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800260 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700261 }
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return retval;
264}
265
Emese Revfy9cd43612009-12-31 14:52:51 +0100266static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100267 .filter = dev_uevent_filter,
268 .name = dev_uevent_name,
269 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270};
271
Kay Sievers16574dc2007-04-06 01:40:38 +0200272static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
273 char *buf)
274{
275 struct kobject *top_kobj;
276 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200277 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200278 int i;
279 size_t count = 0;
280 int retval;
281
282 /* search the kset, the device belongs to */
283 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200284 while (!top_kobj->kset && top_kobj->parent)
285 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200286 if (!top_kobj->kset)
287 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200288
Kay Sievers16574dc2007-04-06 01:40:38 +0200289 kset = top_kobj->kset;
290 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
291 goto out;
292
293 /* respect filter */
294 if (kset->uevent_ops && kset->uevent_ops->filter)
295 if (!kset->uevent_ops->filter(kset, &dev->kobj))
296 goto out;
297
Kay Sievers7eff2e72007-08-14 15:15:12 +0200298 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
299 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200300 return -ENOMEM;
301
Kay Sievers16574dc2007-04-06 01:40:38 +0200302 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200303 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200304 if (retval)
305 goto out;
306
307 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200308 for (i = 0; i < env->envp_idx; i++)
309 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200310out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200311 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200312 return count;
313}
314
Kay Sieversa7fd6702005-10-01 14:49:43 +0200315static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
316 const char *buf, size_t count)
317{
Kay Sievers60a96a52007-07-08 22:29:26 +0200318 enum kobject_action action;
319
Kay Sievers3f5468c2010-01-14 22:54:37 +0100320 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200321 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100322 else
323 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200324 return count;
325}
326
Tejun Heoad6a1e12007-06-14 03:45:17 +0900327static struct device_attribute uevent_attr =
328 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
329
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500330static int device_add_attributes(struct device *dev,
331 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700332{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700333 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500334 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700335
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500336 if (attrs) {
337 for (i = 0; attr_name(attrs[i]); i++) {
338 error = device_create_file(dev, &attrs[i]);
339 if (error)
340 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700341 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500342 if (error)
343 while (--i >= 0)
344 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700345 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700346 return error;
347}
348
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500349static void device_remove_attributes(struct device *dev,
350 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700351{
352 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500353
354 if (attrs)
355 for (i = 0; attr_name(attrs[i]); i++)
356 device_remove_file(dev, &attrs[i]);
357}
358
359static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700360 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500361{
362 int error = 0;
363 int i;
364
365 if (groups) {
366 for (i = 0; groups[i]; i++) {
367 error = sysfs_create_group(&dev->kobj, groups[i]);
368 if (error) {
369 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800370 sysfs_remove_group(&dev->kobj,
371 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500372 break;
373 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700374 }
375 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500376 return error;
377}
378
379static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700380 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500381{
382 int i;
383
384 if (groups)
385 for (i = 0; groups[i]; i++)
386 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700387}
388
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700389static int device_add_attrs(struct device *dev)
390{
391 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200392 struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500393 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700394
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500395 if (class) {
396 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200397 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500398 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700399 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200400
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500401 if (type) {
402 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200403 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500404 goto err_remove_class_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200405 }
406
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500407 error = device_add_groups(dev, dev->groups);
408 if (error)
409 goto err_remove_type_groups;
410
411 return 0;
412
413 err_remove_type_groups:
414 if (type)
415 device_remove_groups(dev, type->groups);
416 err_remove_class_attrs:
417 if (class)
418 device_remove_attributes(dev, class->dev_attrs);
419
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700420 return error;
421}
422
423static void device_remove_attrs(struct device *dev)
424{
425 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200426 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700427
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500428 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200429
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500430 if (type)
431 device_remove_groups(dev, type->groups);
432
433 if (class)
434 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700435}
436
437
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700438static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
439 char *buf)
440{
441 return print_dev_t(buf, dev->devt);
442}
443
Tejun Heoad6a1e12007-06-14 03:45:17 +0900444static struct device_attribute devt_attr =
445 __ATTR(dev, S_IRUGO, show_dev, NULL);
446
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600447/* kset to create /sys/devices/ */
448struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800451 * device_create_file - create sysfs attribute file for device.
452 * @dev: device.
453 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200455int device_create_file(struct device *dev,
456 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 int error = 0;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100459 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 error = sysfs_create_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return error;
462}
463
464/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800465 * device_remove_file - remove sysfs attribute file.
466 * @dev: device.
467 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200469void device_remove_file(struct device *dev,
470 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100472 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700476/**
477 * device_create_bin_file - create sysfs binary attribute file for device.
478 * @dev: device.
479 * @attr: device binary attribute descriptor.
480 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200481int device_create_bin_file(struct device *dev,
482 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700483{
484 int error = -EINVAL;
485 if (dev)
486 error = sysfs_create_bin_file(&dev->kobj, attr);
487 return error;
488}
489EXPORT_SYMBOL_GPL(device_create_bin_file);
490
491/**
492 * device_remove_bin_file - remove sysfs binary attribute file
493 * @dev: device.
494 * @attr: device binary attribute descriptor.
495 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200496void device_remove_bin_file(struct device *dev,
497 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700498{
499 if (dev)
500 sysfs_remove_bin_file(&dev->kobj, attr);
501}
502EXPORT_SYMBOL_GPL(device_remove_bin_file);
503
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400504/**
Alan Stern523ded72007-04-26 00:12:04 -0700505 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400506 * @dev: device.
507 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700508 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400509 *
510 * Attribute methods must not unregister themselves or their parent device
511 * (which would amount to the same thing). Attempts to do so will deadlock,
512 * since unregistration is mutually exclusive with driver callbacks.
513 *
514 * Instead methods can call this routine, which will attempt to allocate
515 * and schedule a workqueue request to call back @func with @dev as its
516 * argument in the workqueue's process context. @dev will be pinned until
517 * @func returns.
518 *
Alan Stern523ded72007-04-26 00:12:04 -0700519 * This routine is usually called via the inline device_schedule_callback(),
520 * which automatically sets @owner to THIS_MODULE.
521 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400522 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700523 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400524 *
525 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
526 * underlying sysfs routine (since it is intended for use by attribute
527 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
528 */
Alan Stern523ded72007-04-26 00:12:04 -0700529int device_schedule_callback_owner(struct device *dev,
530 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400531{
532 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700533 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400534}
Alan Stern523ded72007-04-26 00:12:04 -0700535EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400536
James Bottomley34bb61f2005-09-06 16:56:51 -0700537static void klist_children_get(struct klist_node *n)
538{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800539 struct device_private *p = to_device_private_parent(n);
540 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700541
542 get_device(dev);
543}
544
545static void klist_children_put(struct klist_node *n)
546{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800547 struct device_private *p = to_device_private_parent(n);
548 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700549
550 put_device(dev);
551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800554 * device_initialize - init device structure.
555 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
Cornelia Huck57394112008-09-03 18:26:40 +0200557 * This prepares the device for use by other layers by initializing
558 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800559 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200560 * that function, though it can also be called separately, so one
561 * may use @dev's fields. In particular, get_device()/put_device()
562 * may be used for reference counting of @dev after calling this
563 * function.
564 *
565 * NOTE: Use put_device() to give up your reference instead of freeing
566 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568void device_initialize(struct device *dev)
569{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600570 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700571 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000573 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100574 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900575 spin_lock_init(&dev->devres_lock);
576 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400577 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800578 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100581#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sieversda231fd2007-11-21 17:29:15 +0100582static struct kobject *get_device_parent(struct device *dev,
583 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100584{
Kay Sieversda231fd2007-11-21 17:29:15 +0100585 /* class devices without a parent live in /sys/class/<classname>/ */
Dmitry Torokhov3eb215d2007-10-07 12:22:21 -0400586 if (dev->class && (!parent || parent->class != dev->class))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700587 return &dev->class->p->class_subsys.kobj;
Kay Sieversda231fd2007-11-21 17:29:15 +0100588 /* all other devices keep their parent */
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100589 else if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100590 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100591
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100592 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100593}
Kay Sieversda231fd2007-11-21 17:29:15 +0100594
595static inline void cleanup_device_parent(struct device *dev) {}
Cornelia Huck63b69712008-01-21 16:09:44 +0100596static inline void cleanup_glue_dir(struct device *dev,
597 struct kobject *glue_dir) {}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100598#else
Kay Sievers86406242007-03-14 03:25:56 +0100599static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700600{
Kay Sievers86406242007-03-14 03:25:56 +0100601 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700602
Kay Sievers86406242007-03-14 03:25:56 +0100603 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800604 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600605 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700606
Kay Sievers86406242007-03-14 03:25:56 +0100607 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700608}
609
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700610struct class_dir {
611 struct kobject kobj;
612 struct class *class;
613};
614
615#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
616
617static void class_dir_release(struct kobject *kobj)
618{
619 struct class_dir *dir = to_class_dir(kobj);
620 kfree(dir);
621}
622
623static const
624struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
625{
626 struct class_dir *dir = to_class_dir(kobj);
627 return dir->class->ns_type;
628}
629
630static struct kobj_type class_dir_ktype = {
631 .release = class_dir_release,
632 .sysfs_ops = &kobj_sysfs_ops,
633 .child_ns_type = class_dir_child_ns_type
634};
635
636static struct kobject *
637class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
638{
639 struct class_dir *dir;
640 int retval;
641
642 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
643 if (!dir)
644 return NULL;
645
646 dir->class = class;
647 kobject_init(&dir->kobj, &class_dir_ktype);
648
649 dir->kobj.kset = &class->p->class_dirs;
650
651 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
652 if (retval < 0) {
653 kobject_put(&dir->kobj);
654 return NULL;
655 }
656 return &dir->kobj;
657}
658
659
Kay Sieversda231fd2007-11-21 17:29:15 +0100660static struct kobject *get_device_parent(struct device *dev,
661 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100662{
Kay Sievers86406242007-03-14 03:25:56 +0100663 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900664 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100665 struct kobject *kobj = NULL;
666 struct kobject *parent_kobj;
667 struct kobject *k;
668
669 /*
670 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100671 * Class-devices with a non class-device as parent, live
672 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100673 */
674 if (parent == NULL)
675 parent_kobj = virtual_device_parent(dev);
676 else if (parent->class)
677 return &parent->kobj;
678 else
679 parent_kobj = &parent->kobj;
680
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900681 mutex_lock(&gdp_mutex);
682
Kay Sievers86406242007-03-14 03:25:56 +0100683 /* find our class-directory at the parent and reference it */
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500684 spin_lock(&dev->class->p->class_dirs.list_lock);
685 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100686 if (k->parent == parent_kobj) {
687 kobj = kobject_get(k);
688 break;
689 }
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500690 spin_unlock(&dev->class->p->class_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900691 if (kobj) {
692 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100693 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900694 }
Kay Sievers86406242007-03-14 03:25:56 +0100695
696 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700697 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100698 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900699 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800700 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100701 }
702
703 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100704 return &parent->kobj;
705 return NULL;
706}
Kay Sieversda231fd2007-11-21 17:29:15 +0100707
Cornelia Huck63b69712008-01-21 16:09:44 +0100708static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100709{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100710 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100711 if (!glue_dir || !dev->class ||
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500712 glue_dir->kset != &dev->class->p->class_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100713 return;
714
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100715 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100716}
Cornelia Huck63b69712008-01-21 16:09:44 +0100717
718static void cleanup_device_parent(struct device *dev)
719{
720 cleanup_glue_dir(dev, dev->kobj.parent);
721}
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100722#endif
Kay Sievers86406242007-03-14 03:25:56 +0100723
Cornelia Huck63b69712008-01-21 16:09:44 +0100724static void setup_parent(struct device *dev, struct device *parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100725{
726 struct kobject *kobj;
727 kobj = get_device_parent(dev, parent);
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100728 if (kobj)
729 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100730}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100731
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700732static int device_add_class_symlinks(struct device *dev)
733{
734 int error;
735
736 if (!dev->class)
737 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100738
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700739 error = sysfs_create_link(&dev->kobj,
740 &dev->class->p->class_subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700741 "subsystem");
742 if (error)
743 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100744
745#ifdef CONFIG_SYSFS_DEPRECATED
746 /* stacked class devices need a symlink in the class directory */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700747 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800748 device_is_not_partition(dev)) {
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700749 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100750 &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700751 if (error)
752 goto out_subsys;
753 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100754
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800755 if (dev->parent && device_is_not_partition(dev)) {
Kay Sieversda231fd2007-11-21 17:29:15 +0100756 struct device *parent = dev->parent;
757 char *class_name;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700758
Kay Sieversda231fd2007-11-21 17:29:15 +0100759 /*
760 * stacked class devices have the 'device' link
761 * pointing to the bus device instead of the parent
762 */
763 while (parent->class && !parent->bus && parent->parent)
764 parent = parent->parent;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700765
Kay Sieversda231fd2007-11-21 17:29:15 +0100766 error = sysfs_create_link(&dev->kobj,
767 &parent->kobj,
768 "device");
769 if (error)
770 goto out_busid;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700771
Kay Sieversda231fd2007-11-21 17:29:15 +0100772 class_name = make_class_name(dev->class->name,
773 &dev->kobj);
774 if (class_name)
775 error = sysfs_create_link(&dev->parent->kobj,
776 &dev->kobj, class_name);
777 kfree(class_name);
778 if (error)
779 goto out_device;
780 }
781 return 0;
782
783out_device:
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800784 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100785 sysfs_remove_link(&dev->kobj, "device");
786out_busid:
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700787 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800788 device_is_not_partition(dev))
Eric W. Biedermanf349cf32010-03-30 11:31:29 -0700789 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100790 dev_name(dev));
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700791#else
Kay Sieversda231fd2007-11-21 17:29:15 +0100792 /* link in the class directory pointing to the device */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700793 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100794 &dev->kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100795 if (error)
796 goto out_subsys;
797
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800798 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700799 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
800 "device");
801 if (error)
802 goto out_busid;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700803 }
804 return 0;
805
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700806out_busid:
Eric W. Biedermanf349cf32010-03-30 11:31:29 -0700807 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100808#endif
809
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700810out_subsys:
811 sysfs_remove_link(&dev->kobj, "subsystem");
812out:
813 return error;
814}
815
816static void device_remove_class_symlinks(struct device *dev)
817{
818 if (!dev->class)
819 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100820
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700821#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800822 if (dev->parent && device_is_not_partition(dev)) {
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700823 char *class_name;
824
825 class_name = make_class_name(dev->class->name, &dev->kobj);
826 if (class_name) {
827 sysfs_remove_link(&dev->parent->kobj, class_name);
828 kfree(class_name);
829 }
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700830 sysfs_remove_link(&dev->kobj, "device");
831 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100832
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700833 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800834 device_is_not_partition(dev))
Eric W. Biedermanf349cf32010-03-30 11:31:29 -0700835 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100836 dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100837#else
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800838 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100839 sysfs_remove_link(&dev->kobj, "device");
840
Eric W. Biedermanf349cf32010-03-30 11:31:29 -0700841 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100842#endif
843
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700844 sysfs_remove_link(&dev->kobj, "subsystem");
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000848 * dev_set_name - set a device name
849 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700850 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000851 */
852int dev_set_name(struct device *dev, const char *fmt, ...)
853{
854 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100855 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000856
857 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100858 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000859 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100860 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000861}
862EXPORT_SYMBOL_GPL(dev_set_name);
863
864/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700865 * device_to_dev_kobj - select a /sys/dev/ directory for the device
866 * @dev: device
867 *
868 * By default we select char/ for new entries. Setting class->dev_obj
869 * to NULL prevents an entry from being created. class->dev_kobj must
870 * be set (or cleared) before any devices are registered to the class
871 * otherwise device_create_sys_dev_entry() and
872 * device_remove_sys_dev_entry() will disagree about the the presence
873 * of the link.
874 */
875static struct kobject *device_to_dev_kobj(struct device *dev)
876{
877 struct kobject *kobj;
878
879 if (dev->class)
880 kobj = dev->class->dev_kobj;
881 else
882 kobj = sysfs_dev_char_kobj;
883
884 return kobj;
885}
886
887static int device_create_sys_dev_entry(struct device *dev)
888{
889 struct kobject *kobj = device_to_dev_kobj(dev);
890 int error = 0;
891 char devt_str[15];
892
893 if (kobj) {
894 format_dev_t(devt_str, dev->devt);
895 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
896 }
897
898 return error;
899}
900
901static void device_remove_sys_dev_entry(struct device *dev)
902{
903 struct kobject *kobj = device_to_dev_kobj(dev);
904 char devt_str[15];
905
906 if (kobj) {
907 format_dev_t(devt_str, dev->devt);
908 sysfs_remove_link(kobj, devt_str);
909 }
910}
911
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700912int device_private_init(struct device *dev)
913{
914 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
915 if (!dev->p)
916 return -ENOMEM;
917 dev->p->device = dev;
918 klist_init(&dev->p->klist_children, klist_children_get,
919 klist_children_put);
920 return 0;
921}
922
Dan Williamse105b8b2008-04-21 10:51:07 -0700923/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800924 * device_add - add device to device hierarchy.
925 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800927 * This is part 2 of device_register(), though may be called
928 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
Cornelia Huck57394112008-09-03 18:26:40 +0200930 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800931 * to the global and sibling lists for the device, then
932 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200933 *
934 * NOTE: _Never_ directly free @dev after calling this function, even
935 * if it returned an error! Always use put_device() to give up your
936 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
938int device_add(struct device *dev)
939{
940 struct device *parent = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200941 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700942 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700945 if (!dev)
946 goto done;
947
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800948 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700949 error = device_private_init(dev);
950 if (error)
951 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800952 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800953
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100954 /*
955 * for statically allocated devices, which should all be converted
956 * some day, we need to initialize the name. We prevent reading back
957 * the name, and force the use of dev_name()
958 */
959 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700960 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100961 dev->init_name = NULL;
962 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700963
Thomas Gleixnere6309e72009-12-10 19:32:49 +0000964 if (!dev_name(dev)) {
965 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -0700966 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +0000967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100969 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 parent = get_device(dev->parent);
Cornelia Huck63b69712008-01-21 16:09:44 +0100972 setup_parent(dev, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Yinghai Lu0d358f22008-02-19 03:20:41 -0800974 /* use parent numa_node */
975 if (parent)
976 set_dev_node(dev, dev_to_node(parent));
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -0700979 /* we require the name to be set before, and pass NULL */
980 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100981 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200983
Brian Walsh37022642006-08-14 22:43:19 -0700984 /* notify platform of device entry */
985 if (platform_notify)
986 platform_notify(dev);
987
Tejun Heoad6a1e12007-06-14 03:45:17 +0900988 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200989 if (error)
990 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200991
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700992 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900993 error = device_create_file(dev, &devt_attr);
994 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +0200995 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -0700996
997 error = device_create_sys_dev_entry(dev);
998 if (error)
999 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +02001000
1001 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001002 }
1003
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001004 error = device_add_class_symlinks(dev);
1005 if (error)
1006 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001007 error = device_add_attrs(dev);
1008 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001009 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001010 error = bus_add_device(dev);
1011 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001013 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001014 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001015 goto DPMError;
1016 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001017
1018 /* Notify clients of device addition. This call must come
1019 * after dpm_sysf_add() and before kobject_uevent().
1020 */
1021 if (dev->bus)
1022 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1023 BUS_NOTIFY_ADD_DEVICE, dev);
1024
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001025 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001026 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001028 klist_add_tail(&dev->p->knode_parent,
1029 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001031 if (dev->class) {
Dave Youngf75b1c62008-05-28 09:28:39 -07001032 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001033 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001034 klist_add_tail(&dev->knode_class,
1035 &dev->class->p->class_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001036
1037 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001038 list_for_each_entry(class_intf,
1039 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001040 if (class_intf->add_dev)
1041 class_intf->add_dev(dev, class_intf);
Dave Youngf75b1c62008-05-28 09:28:39 -07001042 mutex_unlock(&dev->class->p->class_mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001043 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001044done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 put_device(dev);
1046 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -04001047 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001048 bus_remove_device(dev);
1049 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001050 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001051 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001052 device_remove_class_symlinks(dev);
1053 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001054 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +01001055 devtmpfs_delete_node(dev);
1056 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -07001057 device_remove_sys_dev_entry(dev);
1058 devtattrError:
1059 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +09001060 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001061 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001062 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001063 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001064 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 kobject_del(&dev->kobj);
1066 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001067 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (parent)
1069 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001070name_error:
1071 kfree(dev->p);
1072 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001073 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001077 * device_register - register a device with the system.
1078 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001080 * This happens in two clean steps - initialize the device
1081 * and add it to the system. The two steps can be called
1082 * separately, but this is the easiest and most common.
1083 * I.e. you should only call the two helpers separately if
1084 * have a clearly defined need to use and refcount the device
1085 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001086 *
1087 * NOTE: _Never_ directly free @dev after calling this function, even
1088 * if it returned an error! Always use put_device() to give up the
1089 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091int device_register(struct device *dev)
1092{
1093 device_initialize(dev);
1094 return device_add(dev);
1095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001098 * get_device - increment reference count for device.
1099 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001101 * This simply forwards the call to kobject_get(), though
1102 * we do take care to provide for the case that we get a NULL
1103 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001105struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001111 * put_device - decrement reference count.
1112 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001114void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001116 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (dev)
1118 kobject_put(&dev->kobj);
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001122 * device_del - delete device from system.
1123 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001125 * This is the first part of the device unregistration
1126 * sequence. This removes the device from the lists we control
1127 * from here, has it removed from the other driver model
1128 * subsystems it was added to in device_add(), and removes it
1129 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001131 * NOTE: this should be called manually _iff_ device_add() was
1132 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001134void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001136 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001137 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Alan Sternec0676ee2008-12-05 14:10:31 -05001139 /* Notify clients of device removal. This call must come
1140 * before dpm_sysfs_remove().
1141 */
1142 if (dev->bus)
1143 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1144 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001145 device_pm_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001146 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001148 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001149 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001150 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001151 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001152 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001153 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001154 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001155 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001156
Dave Youngf75b1c62008-05-28 09:28:39 -07001157 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001158 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001159 list_for_each_entry(class_intf,
1160 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001161 if (class_intf->remove_dev)
1162 class_intf->remove_dev(dev, class_intf);
1163 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001164 klist_del(&dev->knode_class);
Dave Youngf75b1c62008-05-28 09:28:39 -07001165 mutex_unlock(&dev->class->p->class_mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001166 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001167 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001168 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001169 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Tejun Heo2f8d16a2007-03-09 19:34:19 +09001171 /*
1172 * Some platform devices are driven without driver attached
1173 * and managed resources may have been acquired. Make sure
1174 * all resources are released.
1175 */
1176 devres_release_all(dev);
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /* Notify the platform of the removal, in case they
1179 * need to do anything...
1180 */
1181 if (platform_notify_remove)
1182 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001183 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001184 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001186 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001190 * device_unregister - unregister device from system.
1191 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001193 * We do this in two parts, like we do device_register(). First,
1194 * we remove it from all the subsystems with device_del(), then
1195 * we decrement the reference count via put_device(). If that
1196 * is the final reference count, the device will be cleaned up
1197 * via device_release() above. Otherwise, the structure will
1198 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001200void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001202 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 device_del(dev);
1204 put_device(dev);
1205}
1206
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001207static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001208{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001209 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001210 struct device *dev = NULL;
1211 struct device_private *p;
1212
1213 if (n) {
1214 p = to_device_private_parent(n);
1215 dev = p->device;
1216 }
1217 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001221 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001222 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001223 * @mode: returned file access mode
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001224 * @tmp: possibly allocated string
1225 *
1226 * Return the relative path of a possible device node.
1227 * Non-default names may need to allocate a memory to compose
1228 * a name. This memory is returned in tmp and needs to be
1229 * freed by the caller.
1230 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001231const char *device_get_devnode(struct device *dev,
1232 mode_t *mode, const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001233{
1234 char *s;
1235
1236 *tmp = NULL;
1237
1238 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001239 if (dev->type && dev->type->devnode)
1240 *tmp = dev->type->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001241 if (*tmp)
1242 return *tmp;
1243
1244 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001245 if (dev->class && dev->class->devnode)
1246 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001247 if (*tmp)
1248 return *tmp;
1249
1250 /* return name without allocation, tmp == NULL */
1251 if (strchr(dev_name(dev), '!') == NULL)
1252 return dev_name(dev);
1253
1254 /* replace '!' in the name with '/' */
1255 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1256 if (!*tmp)
1257 return NULL;
1258 while ((s = strchr(*tmp, '!')))
1259 s[0] = '/';
1260 return *tmp;
1261}
1262
1263/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001264 * device_for_each_child - device child iterator.
1265 * @parent: parent struct device.
1266 * @data: data for the callback.
1267 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001269 * Iterate over @parent's child devices, and call @fn for each,
1270 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001272 * We check the return of @fn each time. If it returns anything
1273 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001275int device_for_each_child(struct device *parent, void *data,
1276 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001278 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001279 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int error = 0;
1281
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001282 if (!parent->p)
1283 return 0;
1284
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001285 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001286 while ((child = next_device(&i)) && !error)
1287 error = fn(child, data);
1288 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return error;
1290}
1291
Cornelia Huck5ab69982006-11-16 15:42:07 +01001292/**
1293 * device_find_child - device iterator for locating a particular device.
1294 * @parent: parent struct device
1295 * @data: Data to pass to match function
1296 * @match: Callback function to check device
1297 *
1298 * This is similar to the device_for_each_child() function above, but it
1299 * returns a reference to a device that is 'found' for later use, as
1300 * determined by the @match callback.
1301 *
1302 * The callback should return 0 if the device doesn't match and non-zero
1303 * if it does. If the callback returns non-zero and a reference to the
1304 * current device can be obtained, this function will return to the caller
1305 * and not iterate over any more devices.
1306 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001307struct device *device_find_child(struct device *parent, void *data,
1308 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001309{
1310 struct klist_iter i;
1311 struct device *child;
1312
1313 if (!parent)
1314 return NULL;
1315
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001316 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001317 while ((child = next_device(&i)))
1318 if (match(child, data) && get_device(child))
1319 break;
1320 klist_iter_exit(&i);
1321 return child;
1322}
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324int __init devices_init(void)
1325{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001326 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1327 if (!devices_kset)
1328 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001329 dev_kobj = kobject_create_and_add("dev", NULL);
1330 if (!dev_kobj)
1331 goto dev_kobj_err;
1332 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1333 if (!sysfs_dev_block_kobj)
1334 goto block_kobj_err;
1335 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1336 if (!sysfs_dev_char_kobj)
1337 goto char_kobj_err;
1338
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001339 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001340
1341 char_kobj_err:
1342 kobject_put(sysfs_dev_block_kobj);
1343 block_kobj_err:
1344 kobject_put(dev_kobj);
1345 dev_kobj_err:
1346 kset_unregister(devices_kset);
1347 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
1350EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001351EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353EXPORT_SYMBOL_GPL(device_initialize);
1354EXPORT_SYMBOL_GPL(device_add);
1355EXPORT_SYMBOL_GPL(device_register);
1356
1357EXPORT_SYMBOL_GPL(device_del);
1358EXPORT_SYMBOL_GPL(device_unregister);
1359EXPORT_SYMBOL_GPL(get_device);
1360EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362EXPORT_SYMBOL_GPL(device_create_file);
1363EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001364
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001365struct root_device
1366{
1367 struct device dev;
1368 struct module *owner;
1369};
1370
1371#define to_root_device(dev) container_of(dev, struct root_device, dev)
1372
1373static void root_device_release(struct device *dev)
1374{
1375 kfree(to_root_device(dev));
1376}
1377
1378/**
1379 * __root_device_register - allocate and register a root device
1380 * @name: root device name
1381 * @owner: owner module of the root device, usually THIS_MODULE
1382 *
1383 * This function allocates a root device and registers it
1384 * using device_register(). In order to free the returned
1385 * device, use root_device_unregister().
1386 *
1387 * Root devices are dummy devices which allow other devices
1388 * to be grouped under /sys/devices. Use this function to
1389 * allocate a root device and then use it as the parent of
1390 * any device which should appear under /sys/devices/{name}
1391 *
1392 * The /sys/devices/{name} directory will also contain a
1393 * 'module' symlink which points to the @owner directory
1394 * in sysfs.
1395 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001396 * Returns &struct device pointer on success, or ERR_PTR() on error.
1397 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001398 * Note: You probably want to use root_device_register().
1399 */
1400struct device *__root_device_register(const char *name, struct module *owner)
1401{
1402 struct root_device *root;
1403 int err = -ENOMEM;
1404
1405 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1406 if (!root)
1407 return ERR_PTR(err);
1408
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001409 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001410 if (err) {
1411 kfree(root);
1412 return ERR_PTR(err);
1413 }
1414
1415 root->dev.release = root_device_release;
1416
1417 err = device_register(&root->dev);
1418 if (err) {
1419 put_device(&root->dev);
1420 return ERR_PTR(err);
1421 }
1422
Christoph Egger1d9e8822010-05-17 16:57:58 +02001423#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001424 if (owner) {
1425 struct module_kobject *mk = &owner->mkobj;
1426
1427 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1428 if (err) {
1429 device_unregister(&root->dev);
1430 return ERR_PTR(err);
1431 }
1432 root->owner = owner;
1433 }
1434#endif
1435
1436 return &root->dev;
1437}
1438EXPORT_SYMBOL_GPL(__root_device_register);
1439
1440/**
1441 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001442 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001443 *
1444 * This function unregisters and cleans up a device that was created by
1445 * root_device_register().
1446 */
1447void root_device_unregister(struct device *dev)
1448{
1449 struct root_device *root = to_root_device(dev);
1450
1451 if (root->owner)
1452 sysfs_remove_link(&root->dev.kobj, "module");
1453
1454 device_unregister(dev);
1455}
1456EXPORT_SYMBOL_GPL(root_device_unregister);
1457
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001458
1459static void device_create_release(struct device *dev)
1460{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001461 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001462 kfree(dev);
1463}
1464
1465/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001466 * device_create_vargs - creates a device and registers it with sysfs
1467 * @class: pointer to the struct class that this device should be registered to
1468 * @parent: pointer to the parent struct device of this new device, if any
1469 * @devt: the dev_t for the char device to be added
1470 * @drvdata: the data to be added to the device for callbacks
1471 * @fmt: string for the device's name
1472 * @args: va_list for the device's name
1473 *
1474 * This function can be used by char device classes. A struct device
1475 * will be created in sysfs, registered to the specified class.
1476 *
1477 * A "dev" file will be created, showing the dev_t for the device, if
1478 * the dev_t is not 0,0.
1479 * If a pointer to a parent struct device is passed in, the newly created
1480 * struct device will be a child of that device in sysfs.
1481 * The pointer to the struct device will be returned from the call.
1482 * Any further sysfs files that might be required can be created using this
1483 * pointer.
1484 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001485 * Returns &struct device pointer on success, or ERR_PTR() on error.
1486 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001487 * Note: the struct class passed to this function must have previously
1488 * been created with a call to class_create().
1489 */
1490struct device *device_create_vargs(struct class *class, struct device *parent,
1491 dev_t devt, void *drvdata, const char *fmt,
1492 va_list args)
1493{
1494 struct device *dev = NULL;
1495 int retval = -ENODEV;
1496
1497 if (class == NULL || IS_ERR(class))
1498 goto error;
1499
1500 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1501 if (!dev) {
1502 retval = -ENOMEM;
1503 goto error;
1504 }
1505
1506 dev->devt = devt;
1507 dev->class = class;
1508 dev->parent = parent;
1509 dev->release = device_create_release;
1510 dev_set_drvdata(dev, drvdata);
1511
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001512 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1513 if (retval)
1514 goto error;
1515
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001516 retval = device_register(dev);
1517 if (retval)
1518 goto error;
1519
1520 return dev;
1521
1522error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001523 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001524 return ERR_PTR(retval);
1525}
1526EXPORT_SYMBOL_GPL(device_create_vargs);
1527
1528/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001529 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001530 * @class: pointer to the struct class that this device should be registered to
1531 * @parent: pointer to the parent struct device of this new device, if any
1532 * @devt: the dev_t for the char device to be added
1533 * @drvdata: the data to be added to the device for callbacks
1534 * @fmt: string for the device's name
1535 *
1536 * This function can be used by char device classes. A struct device
1537 * will be created in sysfs, registered to the specified class.
1538 *
1539 * A "dev" file will be created, showing the dev_t for the device, if
1540 * the dev_t is not 0,0.
1541 * If a pointer to a parent struct device is passed in, the newly created
1542 * struct device will be a child of that device in sysfs.
1543 * The pointer to the struct device will be returned from the call.
1544 * Any further sysfs files that might be required can be created using this
1545 * pointer.
1546 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001547 * Returns &struct device pointer on success, or ERR_PTR() on error.
1548 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001549 * Note: the struct class passed to this function must have previously
1550 * been created with a call to class_create().
1551 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001552struct device *device_create(struct class *class, struct device *parent,
1553 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001554{
1555 va_list vargs;
1556 struct device *dev;
1557
1558 va_start(vargs, fmt);
1559 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1560 va_end(vargs);
1561 return dev;
1562}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001563EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001564
Dave Youngcd354492008-01-28 16:56:11 +08001565static int __match_devt(struct device *dev, void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001566{
Dave Youngcd354492008-01-28 16:56:11 +08001567 dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001568
Dave Youngcd354492008-01-28 16:56:11 +08001569 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001570}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001571
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001572/**
1573 * device_destroy - removes a device that was created with device_create()
1574 * @class: pointer to the struct class that this device was registered with
1575 * @devt: the dev_t of the device that was previously registered
1576 *
1577 * This call unregisters and cleans up a device that was created with a
1578 * call to device_create().
1579 */
1580void device_destroy(struct class *class, dev_t devt)
1581{
1582 struct device *dev;
1583
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001584 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001585 if (dev) {
1586 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001587 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001588 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001589}
1590EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001591
1592/**
1593 * device_rename - renames a device
1594 * @dev: the pointer to the struct device to be renamed
1595 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001596 *
1597 * It is the responsibility of the caller to provide mutual
1598 * exclusion between two different calls of device_rename
1599 * on the same device to ensure that new_name is valid and
1600 * won't conflict with other devices.
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001601 */
1602int device_rename(struct device *dev, char *new_name)
1603{
1604 char *old_class_name = NULL;
1605 char *new_class_name = NULL;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001606 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001607 int error;
1608
1609 dev = get_device(dev);
1610 if (!dev)
1611 return -EINVAL;
1612
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001613 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001614 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001615
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001616#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001617 if ((dev->class) && (dev->parent))
1618 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001619#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001620
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001621 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001622 if (!old_device_name) {
1623 error = -ENOMEM;
1624 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001625 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001626
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001627#ifndef CONFIG_SYSFS_DEPRECATED
1628 if (dev->class) {
1629 error = sysfs_rename_link(&dev->class->p->class_subsys.kobj,
1630 &dev->kobj, old_device_name, new_name);
1631 if (error)
1632 goto out;
1633 }
1634#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001635 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001636 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001637 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001638
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001639#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001640 if (old_class_name) {
1641 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1642 if (new_class_name) {
Eric W. Biederman2354dcc2010-02-12 19:22:26 -08001643 error = sysfs_rename_link(&dev->parent->kobj,
1644 &dev->kobj,
1645 old_class_name,
1646 new_class_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001647 }
1648 }
Kay Sievers60b8cab2007-10-26 20:07:44 +02001649#endif
1650
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001651out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001652 put_device(dev);
1653
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001654 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001655 kfree(old_class_name);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001656 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001657
1658 return error;
1659}
Johannes Berga2807db2007-02-28 12:38:31 +01001660EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001661
1662static int device_move_class_links(struct device *dev,
1663 struct device *old_parent,
1664 struct device *new_parent)
1665{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001666 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001667#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huck8a824722006-11-20 17:07:51 +01001668 char *class_name;
1669
1670 class_name = make_class_name(dev->class->name, &dev->kobj);
1671 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001672 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001673 goto out;
1674 }
1675 if (old_parent) {
1676 sysfs_remove_link(&dev->kobj, "device");
1677 sysfs_remove_link(&old_parent->kobj, class_name);
1678 }
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001679 if (new_parent) {
1680 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1681 "device");
1682 if (error)
1683 goto out;
1684 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1685 class_name);
1686 if (error)
1687 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001688 } else
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001689 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001690out:
1691 kfree(class_name);
1692 return error;
1693#else
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001694 if (old_parent)
1695 sysfs_remove_link(&dev->kobj, "device");
1696 if (new_parent)
1697 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1698 "device");
1699 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001700#endif
1701}
1702
1703/**
1704 * device_move - moves a device to a new parent
1705 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001706 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001707 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001708 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001709int device_move(struct device *dev, struct device *new_parent,
1710 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001711{
1712 int error;
1713 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001714 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001715
1716 dev = get_device(dev);
1717 if (!dev)
1718 return -EINVAL;
1719
Cornelia Huckffa6a702009-03-04 12:44:00 +01001720 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001721 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001722 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001723
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001724 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1725 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001726 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001727 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001728 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001729 put_device(new_parent);
1730 goto out;
1731 }
1732 old_parent = dev->parent;
1733 dev->parent = new_parent;
1734 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001735 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001736 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001737 klist_add_tail(&dev->p->knode_parent,
1738 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001739 set_dev_node(dev, dev_to_node(new_parent));
1740 }
1741
Cornelia Huck8a824722006-11-20 17:07:51 +01001742 if (!dev->class)
1743 goto out_put;
1744 error = device_move_class_links(dev, old_parent, new_parent);
1745 if (error) {
1746 /* We ignore errors on cleanup since we're hosed anyway... */
1747 device_move_class_links(dev, new_parent, old_parent);
1748 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001749 if (new_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001750 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001751 dev->parent = old_parent;
1752 if (old_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001753 klist_add_tail(&dev->p->knode_parent,
1754 &old_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001755 set_dev_node(dev, dev_to_node(old_parent));
1756 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001757 }
Cornelia Huck63b69712008-01-21 16:09:44 +01001758 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001759 put_device(new_parent);
1760 goto out;
1761 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001762 switch (dpm_order) {
1763 case DPM_ORDER_NONE:
1764 break;
1765 case DPM_ORDER_DEV_AFTER_PARENT:
1766 device_pm_move_after(dev, new_parent);
1767 break;
1768 case DPM_ORDER_PARENT_BEFORE_DEV:
1769 device_pm_move_before(new_parent, dev);
1770 break;
1771 case DPM_ORDER_DEV_LAST:
1772 device_pm_move_last(dev);
1773 break;
1774 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001775out_put:
1776 put_device(old_parent);
1777out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001778 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001779 put_device(dev);
1780 return error;
1781}
Cornelia Huck8a824722006-11-20 17:07:51 +01001782EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001783
1784/**
1785 * device_shutdown - call ->shutdown() on each device to shutdown.
1786 */
1787void device_shutdown(void)
1788{
Hugh Daschbach62458382010-03-22 10:36:37 -07001789 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001790
Hugh Daschbach62458382010-03-22 10:36:37 -07001791 spin_lock(&devices_kset->list_lock);
1792 /*
1793 * Walk the devices list backward, shutting down each in turn.
1794 * Beware that device unplug events may also start pulling
1795 * devices offline, even as the system is shutting down.
1796 */
1797 while (!list_empty(&devices_kset->list)) {
1798 dev = list_entry(devices_kset->list.prev, struct device,
1799 kobj.entry);
1800 get_device(dev);
1801 /*
1802 * Make sure the device is off the kset list, in the
1803 * event that dev->*->shutdown() doesn't remove it.
1804 */
1805 list_del_init(&dev->kobj.entry);
1806 spin_unlock(&devices_kset->list_lock);
1807
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001808 if (dev->bus && dev->bus->shutdown) {
1809 dev_dbg(dev, "shutdown\n");
1810 dev->bus->shutdown(dev);
1811 } else if (dev->driver && dev->driver->shutdown) {
1812 dev_dbg(dev, "shutdown\n");
1813 dev->driver->shutdown(dev);
1814 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001815 put_device(dev);
1816
1817 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001818 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001819 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07001820 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001821}