blob: f7f906f0a2f26c92932b13d5af5cf15493c56c0b [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
Andi Kleene52eec12010-09-08 16:54:17 +020029#ifdef CONFIG_SYSFS_DEPRECATED
30#ifdef CONFIG_SYSFS_DEPRECATED_V2
31long sysfs_deprecated = 1;
32#else
33long sysfs_deprecated = 0;
34#endif
35static __init int sysfs_deprecated_setup(char *arg)
36{
37 return strict_strtol(arg, 10, &sysfs_deprecated);
38}
39early_param("sysfs.deprecated", sysfs_deprecated_setup);
40#endif
41
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080042int (*platform_notify)(struct device *dev) = NULL;
43int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070044static struct kobject *dev_kobj;
45struct kobject *sysfs_dev_char_kobj;
46struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080048#ifdef CONFIG_BLOCK
49static inline int device_is_not_partition(struct device *dev)
50{
51 return !(dev->type == &part_type);
52}
53#else
54static inline int device_is_not_partition(struct device *dev)
55{
56 return 1;
57}
58#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Alan Stern3e956372006-06-16 17:10:48 -040060/**
61 * dev_driver_string - Return a device's driver name, if at all possible
62 * @dev: struct device to get the name of
63 *
64 * Will return the device's driver's name if it is bound to a device. If
65 * the device is not bound to a device, it will return the name of the bus
66 * it is attached to. If it is not attached to a bus either, an empty
67 * string will be returned.
68 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070069const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040070{
Alan Stern35899722009-12-04 11:06:57 -050071 struct device_driver *drv;
72
73 /* dev->driver can change to NULL underneath us because of unbinding,
74 * so be careful about accessing it. dev->bus and dev->class should
75 * never change once they are set, so they don't need special care.
76 */
77 drv = ACCESS_ONCE(dev->driver);
78 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +010079 (dev->bus ? dev->bus->name :
80 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040081}
Matthew Wilcox310a9222006-09-23 23:35:04 -060082EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define to_dev(obj) container_of(obj, struct device, kobj)
85#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
86
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080087static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
88 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080090 struct device_attribute *dev_attr = to_dev_attr(attr);
91 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050092 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040095 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080096 if (ret >= (ssize_t)PAGE_SIZE) {
97 print_symbol("dev_attr_show: %s returned bad count\n",
98 (unsigned long)dev_attr->show);
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return ret;
101}
102
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800103static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
104 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800106 struct device_attribute *dev_attr = to_dev_attr(attr);
107 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500108 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400111 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return ret;
113}
114
Emese Revfy52cf25d2010-01-19 02:58:23 +0100115static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .show = dev_attr_show,
117 .store = dev_attr_store,
118};
119
120
121/**
122 * device_release - free device structure.
123 * @kobj: device's kobject.
124 *
125 * This is called once the reference count for the object
126 * reaches 0. We forward the call to the device's release
127 * method, which should handle actually freeing the structure.
128 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800129static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800131 struct device *dev = to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800132 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 if (dev->release)
135 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200136 else if (dev->type && dev->type->release)
137 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700138 else if (dev->class && dev->class->dev_release)
139 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700140 else
141 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800142 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100143 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800144 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700147static const void *device_namespace(struct kobject *kobj)
148{
149 struct device *dev = to_dev(kobj);
150 const void *ns = NULL;
151
152 if (dev->class && dev->class->ns_type)
153 ns = dev->class->namespace(dev);
154
155 return ns;
156}
157
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600158static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .release = device_release,
160 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700161 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164
Kay Sievers312c0042005-11-16 09:00:00 +0100165static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct kobj_type *ktype = get_ktype(kobj);
168
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600169 if (ktype == &device_ktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct device *dev = to_dev(kobj);
171 if (dev->bus)
172 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700173 if (dev->class)
174 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 return 0;
177}
178
Kay Sievers312c0042005-11-16 09:00:00 +0100179static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct device *dev = to_dev(kobj);
182
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700183 if (dev->bus)
184 return dev->bus->name;
185 if (dev->class)
186 return dev->class->name;
187 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Kay Sievers7eff2e72007-08-14 15:15:12 +0200190static int dev_uevent(struct kset *kset, struct kobject *kobj,
191 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct device *dev = to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int retval = 0;
195
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200196 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700197 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200198 const char *tmp;
199 const char *name;
Kay Sieverse454cea2009-09-18 23:01:12 +0200200 mode_t mode = 0;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200201
Kay Sievers7eff2e72007-08-14 15:15:12 +0200202 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
203 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sieverse454cea2009-09-18 23:01:12 +0200204 name = device_get_devnode(dev, &mode, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200205 if (name) {
206 add_uevent_var(env, "DEVNAME=%s", name);
207 kfree(tmp);
Kay Sieverse454cea2009-09-18 23:01:12 +0200208 if (mode)
209 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200210 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700211 }
212
Kay Sievers414264f2007-03-12 21:08:57 +0100213 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200214 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100215
Kay Sievers239378f2006-10-07 21:54:55 +0200216 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200217 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200218
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
Emese Revfy9cd43612009-12-31 14:52:51 +0100248static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100249 .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 Sievers3f5468c2010-01-14 22:54:37 +0100302 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200303 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100304 else
305 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200306 return count;
307}
308
Tejun Heoad6a1e12007-06-14 03:45:17 +0900309static struct device_attribute uevent_attr =
310 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
311
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500312static int device_add_attributes(struct device *dev,
313 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700314{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700315 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500316 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700317
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500318 if (attrs) {
319 for (i = 0; attr_name(attrs[i]); i++) {
320 error = device_create_file(dev, &attrs[i]);
321 if (error)
322 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700323 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500324 if (error)
325 while (--i >= 0)
326 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700327 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700328 return error;
329}
330
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500331static void device_remove_attributes(struct device *dev,
332 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700333{
334 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500335
336 if (attrs)
337 for (i = 0; attr_name(attrs[i]); i++)
338 device_remove_file(dev, &attrs[i]);
339}
340
341static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700342 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500343{
344 int error = 0;
345 int i;
346
347 if (groups) {
348 for (i = 0; groups[i]; i++) {
349 error = sysfs_create_group(&dev->kobj, groups[i]);
350 if (error) {
351 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800352 sysfs_remove_group(&dev->kobj,
353 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500354 break;
355 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700356 }
357 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500358 return error;
359}
360
361static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700362 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500363{
364 int i;
365
366 if (groups)
367 for (i = 0; groups[i]; i++)
368 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700369}
370
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700371static int device_add_attrs(struct device *dev)
372{
373 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200374 struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500375 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700376
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500377 if (class) {
378 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200379 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500380 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700381 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200382
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500383 if (type) {
384 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200385 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500386 goto err_remove_class_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200387 }
388
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500389 error = device_add_groups(dev, dev->groups);
390 if (error)
391 goto err_remove_type_groups;
392
393 return 0;
394
395 err_remove_type_groups:
396 if (type)
397 device_remove_groups(dev, type->groups);
398 err_remove_class_attrs:
399 if (class)
400 device_remove_attributes(dev, class->dev_attrs);
401
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700402 return error;
403}
404
405static void device_remove_attrs(struct device *dev)
406{
407 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200408 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700409
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500410 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200411
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500412 if (type)
413 device_remove_groups(dev, type->groups);
414
415 if (class)
416 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700417}
418
419
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700420static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
421 char *buf)
422{
423 return print_dev_t(buf, dev->devt);
424}
425
Tejun Heoad6a1e12007-06-14 03:45:17 +0900426static struct device_attribute devt_attr =
427 __ATTR(dev, S_IRUGO, show_dev, NULL);
428
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600429/* kset to create /sys/devices/ */
430struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800433 * device_create_file - create sysfs attribute file for device.
434 * @dev: device.
435 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200437int device_create_file(struct device *dev,
438 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 int error = 0;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100441 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 error = sysfs_create_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return error;
444}
445
446/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800447 * device_remove_file - remove sysfs attribute file.
448 * @dev: device.
449 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200451void device_remove_file(struct device *dev,
452 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100454 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700458/**
459 * device_create_bin_file - create sysfs binary attribute file for device.
460 * @dev: device.
461 * @attr: device binary attribute descriptor.
462 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200463int device_create_bin_file(struct device *dev,
464 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700465{
466 int error = -EINVAL;
467 if (dev)
468 error = sysfs_create_bin_file(&dev->kobj, attr);
469 return error;
470}
471EXPORT_SYMBOL_GPL(device_create_bin_file);
472
473/**
474 * device_remove_bin_file - remove sysfs binary attribute file
475 * @dev: device.
476 * @attr: device binary attribute descriptor.
477 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200478void device_remove_bin_file(struct device *dev,
479 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700480{
481 if (dev)
482 sysfs_remove_bin_file(&dev->kobj, attr);
483}
484EXPORT_SYMBOL_GPL(device_remove_bin_file);
485
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400486/**
Alan Stern523ded72007-04-26 00:12:04 -0700487 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400488 * @dev: device.
489 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700490 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400491 *
492 * Attribute methods must not unregister themselves or their parent device
493 * (which would amount to the same thing). Attempts to do so will deadlock,
494 * since unregistration is mutually exclusive with driver callbacks.
495 *
496 * Instead methods can call this routine, which will attempt to allocate
497 * and schedule a workqueue request to call back @func with @dev as its
498 * argument in the workqueue's process context. @dev will be pinned until
499 * @func returns.
500 *
Alan Stern523ded72007-04-26 00:12:04 -0700501 * This routine is usually called via the inline device_schedule_callback(),
502 * which automatically sets @owner to THIS_MODULE.
503 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400504 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700505 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400506 *
507 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
508 * underlying sysfs routine (since it is intended for use by attribute
509 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
510 */
Alan Stern523ded72007-04-26 00:12:04 -0700511int device_schedule_callback_owner(struct device *dev,
512 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400513{
514 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700515 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400516}
Alan Stern523ded72007-04-26 00:12:04 -0700517EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400518
James Bottomley34bb61f2005-09-06 16:56:51 -0700519static void klist_children_get(struct klist_node *n)
520{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800521 struct device_private *p = to_device_private_parent(n);
522 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700523
524 get_device(dev);
525}
526
527static void klist_children_put(struct klist_node *n)
528{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800529 struct device_private *p = to_device_private_parent(n);
530 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700531
532 put_device(dev);
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800536 * device_initialize - init device structure.
537 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
Cornelia Huck57394112008-09-03 18:26:40 +0200539 * This prepares the device for use by other layers by initializing
540 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800541 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200542 * that function, though it can also be called separately, so one
543 * may use @dev's fields. In particular, get_device()/put_device()
544 * may be used for reference counting of @dev after calling this
545 * function.
546 *
547 * NOTE: Use put_device() to give up your reference instead of freeing
548 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550void device_initialize(struct device *dev)
551{
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600552 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700553 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000555 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100556 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900557 spin_lock_init(&dev->devres_lock);
558 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400559 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800560 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Kay Sievers86406242007-03-14 03:25:56 +0100563static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700564{
Kay Sievers86406242007-03-14 03:25:56 +0100565 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700566
Kay Sievers86406242007-03-14 03:25:56 +0100567 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800568 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600569 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700570
Kay Sievers86406242007-03-14 03:25:56 +0100571 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700572}
573
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700574struct class_dir {
575 struct kobject kobj;
576 struct class *class;
577};
578
579#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
580
581static void class_dir_release(struct kobject *kobj)
582{
583 struct class_dir *dir = to_class_dir(kobj);
584 kfree(dir);
585}
586
587static const
588struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
589{
590 struct class_dir *dir = to_class_dir(kobj);
591 return dir->class->ns_type;
592}
593
594static struct kobj_type class_dir_ktype = {
595 .release = class_dir_release,
596 .sysfs_ops = &kobj_sysfs_ops,
597 .child_ns_type = class_dir_child_ns_type
598};
599
600static struct kobject *
601class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
602{
603 struct class_dir *dir;
604 int retval;
605
606 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
607 if (!dir)
608 return NULL;
609
610 dir->class = class;
611 kobject_init(&dir->kobj, &class_dir_ktype);
612
613 dir->kobj.kset = &class->p->class_dirs;
614
615 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
616 if (retval < 0) {
617 kobject_put(&dir->kobj);
618 return NULL;
619 }
620 return &dir->kobj;
621}
622
623
Kay Sieversda231fd2007-11-21 17:29:15 +0100624static struct kobject *get_device_parent(struct device *dev,
625 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100626{
Kay Sievers86406242007-03-14 03:25:56 +0100627 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900628 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100629 struct kobject *kobj = NULL;
630 struct kobject *parent_kobj;
631 struct kobject *k;
632
Kay Sievers39aba962010-09-04 22:33:14 -0700633 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200634 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700635 if (parent && parent->class == &block_class)
636 return &parent->kobj;
637 return &block_class.p->class_subsys.kobj;
638 }
Andi Kleene52eec12010-09-08 16:54:17 +0200639
Kay Sievers86406242007-03-14 03:25:56 +0100640 /*
641 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100642 * Class-devices with a non class-device as parent, live
643 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100644 */
645 if (parent == NULL)
646 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700647 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100648 return &parent->kobj;
649 else
650 parent_kobj = &parent->kobj;
651
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900652 mutex_lock(&gdp_mutex);
653
Kay Sievers86406242007-03-14 03:25:56 +0100654 /* find our class-directory at the parent and reference it */
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500655 spin_lock(&dev->class->p->class_dirs.list_lock);
656 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100657 if (k->parent == parent_kobj) {
658 kobj = kobject_get(k);
659 break;
660 }
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500661 spin_unlock(&dev->class->p->class_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900662 if (kobj) {
663 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100664 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900665 }
Kay Sievers86406242007-03-14 03:25:56 +0100666
667 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700668 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100669 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900670 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800671 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100672 }
673
674 if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100675 return &parent->kobj;
676 return NULL;
677}
Kay Sieversda231fd2007-11-21 17:29:15 +0100678
Cornelia Huck63b69712008-01-21 16:09:44 +0100679static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100680{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100681 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100682 if (!glue_dir || !dev->class ||
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500683 glue_dir->kset != &dev->class->p->class_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100684 return;
685
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100686 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100687}
Cornelia Huck63b69712008-01-21 16:09:44 +0100688
689static void cleanup_device_parent(struct device *dev)
690{
691 cleanup_glue_dir(dev, dev->kobj.parent);
692}
Kay Sievers86406242007-03-14 03:25:56 +0100693
Cornelia Huck63b69712008-01-21 16:09:44 +0100694static void setup_parent(struct device *dev, struct device *parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100695{
696 struct kobject *kobj;
697 kobj = get_device_parent(dev, parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +0100698 if (kobj)
699 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100700}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100701
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700702static int device_add_class_symlinks(struct device *dev)
703{
704 int error;
705
706 if (!dev->class)
707 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100708
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700709 error = sysfs_create_link(&dev->kobj,
710 &dev->class->p->class_subsys.kobj,
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700711 "subsystem");
712 if (error)
713 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100714
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800715 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700716 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
717 "device");
718 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700719 goto out_subsys;
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700720 }
Kay Sievers39aba962010-09-04 22:33:14 -0700721
Kay Sievers39aba962010-09-04 22:33:14 -0700722 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200723 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700724 return 0;
Kay Sievers39aba962010-09-04 22:33:14 -0700725
726 /* link in the class directory pointing to the device */
727 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
728 &dev->kobj, dev_name(dev));
729 if (error)
730 goto out_device;
731
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700732 return 0;
733
Kay Sievers39aba962010-09-04 22:33:14 -0700734out_device:
735 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100736
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700737out_subsys:
738 sysfs_remove_link(&dev->kobj, "subsystem");
739out:
740 return error;
741}
742
743static void device_remove_class_symlinks(struct device *dev)
744{
745 if (!dev->class)
746 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100747
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800748 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100749 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700750 sysfs_remove_link(&dev->kobj, "subsystem");
Andi Kleene52eec12010-09-08 16:54:17 +0200751 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700752 return;
Kay Sievers39aba962010-09-04 22:33:14 -0700753 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000757 * dev_set_name - set a device name
758 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700759 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000760 */
761int dev_set_name(struct device *dev, const char *fmt, ...)
762{
763 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100764 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000765
766 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100767 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000768 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100769 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000770}
771EXPORT_SYMBOL_GPL(dev_set_name);
772
773/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700774 * device_to_dev_kobj - select a /sys/dev/ directory for the device
775 * @dev: device
776 *
777 * By default we select char/ for new entries. Setting class->dev_obj
778 * to NULL prevents an entry from being created. class->dev_kobj must
779 * be set (or cleared) before any devices are registered to the class
780 * otherwise device_create_sys_dev_entry() and
781 * device_remove_sys_dev_entry() will disagree about the the presence
782 * of the link.
783 */
784static struct kobject *device_to_dev_kobj(struct device *dev)
785{
786 struct kobject *kobj;
787
788 if (dev->class)
789 kobj = dev->class->dev_kobj;
790 else
791 kobj = sysfs_dev_char_kobj;
792
793 return kobj;
794}
795
796static int device_create_sys_dev_entry(struct device *dev)
797{
798 struct kobject *kobj = device_to_dev_kobj(dev);
799 int error = 0;
800 char devt_str[15];
801
802 if (kobj) {
803 format_dev_t(devt_str, dev->devt);
804 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
805 }
806
807 return error;
808}
809
810static void device_remove_sys_dev_entry(struct device *dev)
811{
812 struct kobject *kobj = device_to_dev_kobj(dev);
813 char devt_str[15];
814
815 if (kobj) {
816 format_dev_t(devt_str, dev->devt);
817 sysfs_remove_link(kobj, devt_str);
818 }
819}
820
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700821int device_private_init(struct device *dev)
822{
823 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
824 if (!dev->p)
825 return -ENOMEM;
826 dev->p->device = dev;
827 klist_init(&dev->p->klist_children, klist_children_get,
828 klist_children_put);
829 return 0;
830}
831
Dan Williamse105b8b2008-04-21 10:51:07 -0700832/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800833 * device_add - add device to device hierarchy.
834 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800836 * This is part 2 of device_register(), though may be called
837 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 *
Cornelia Huck57394112008-09-03 18:26:40 +0200839 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800840 * to the global and sibling lists for the device, then
841 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200842 *
843 * NOTE: _Never_ directly free @dev after calling this function, even
844 * if it returned an error! Always use put_device() to give up your
845 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
847int device_add(struct device *dev)
848{
849 struct device *parent = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200850 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700851 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700854 if (!dev)
855 goto done;
856
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800857 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700858 error = device_private_init(dev);
859 if (error)
860 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800861 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800862
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100863 /*
864 * for statically allocated devices, which should all be converted
865 * some day, we need to initialize the name. We prevent reading back
866 * the name, and force the use of dev_name()
867 */
868 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700869 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100870 dev->init_name = NULL;
871 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700872
Thomas Gleixnere6309e72009-12-10 19:32:49 +0000873 if (!dev_name(dev)) {
874 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -0700875 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +0000876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100878 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 parent = get_device(dev->parent);
Cornelia Huck63b69712008-01-21 16:09:44 +0100881 setup_parent(dev, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Yinghai Lu0d358f22008-02-19 03:20:41 -0800883 /* use parent numa_node */
884 if (parent)
885 set_dev_node(dev, dev_to_node(parent));
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -0700888 /* we require the name to be set before, and pass NULL */
889 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100890 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200892
Brian Walsh37022642006-08-14 22:43:19 -0700893 /* notify platform of device entry */
894 if (platform_notify)
895 platform_notify(dev);
896
Tejun Heoad6a1e12007-06-14 03:45:17 +0900897 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200898 if (error)
899 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200900
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700901 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900902 error = device_create_file(dev, &devt_attr);
903 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +0200904 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -0700905
906 error = device_create_sys_dev_entry(dev);
907 if (error)
908 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +0200909
910 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700911 }
912
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700913 error = device_add_class_symlinks(dev);
914 if (error)
915 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700916 error = device_add_attrs(dev);
917 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700918 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700919 error = bus_add_device(dev);
920 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -0400922 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100923 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -0400924 goto DPMError;
925 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -0500926
927 /* Notify clients of device addition. This call must come
928 * after dpm_sysf_add() and before kobject_uevent().
929 */
930 if (dev->bus)
931 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
932 BUS_NOTIFY_ADD_DEVICE, dev);
933
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200934 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -0400935 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800937 klist_add_tail(&dev->p->knode_parent,
938 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700940 if (dev->class) {
Dave Youngf75b1c62008-05-28 09:28:39 -0700941 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200942 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +0200943 klist_add_tail(&dev->knode_class,
944 &dev->class->p->class_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200945
946 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -0700947 list_for_each_entry(class_intf,
948 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200949 if (class_intf->add_dev)
950 class_intf->add_dev(dev, class_intf);
Dave Youngf75b1c62008-05-28 09:28:39 -0700951 mutex_unlock(&dev->class->p->class_mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700952 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700953done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 put_device(dev);
955 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -0400956 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100957 bus_remove_device(dev);
958 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +0000959 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700960 AttrsError:
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700961 device_remove_class_symlinks(dev);
962 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900963 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +0100964 devtmpfs_delete_node(dev);
965 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -0700966 device_remove_sys_dev_entry(dev);
967 devtattrError:
968 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +0900969 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200970 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900971 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700972 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100973 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 kobject_del(&dev->kobj);
975 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +0100976 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (parent)
978 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -0700979name_error:
980 kfree(dev->p);
981 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700982 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800986 * device_register - register a device with the system.
987 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800989 * This happens in two clean steps - initialize the device
990 * and add it to the system. The two steps can be called
991 * separately, but this is the easiest and most common.
992 * I.e. you should only call the two helpers separately if
993 * have a clearly defined need to use and refcount the device
994 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +0200995 *
996 * NOTE: _Never_ directly free @dev after calling this function, even
997 * if it returned an error! Always use put_device() to give up the
998 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000int device_register(struct device *dev)
1001{
1002 device_initialize(dev);
1003 return device_add(dev);
1004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001007 * get_device - increment reference count for device.
1008 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001010 * This simply forwards the call to kobject_get(), though
1011 * we do take care to provide for the case that we get a NULL
1012 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001014struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001020 * put_device - decrement reference count.
1021 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001023void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001025 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (dev)
1027 kobject_put(&dev->kobj);
1028}
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001031 * device_del - delete device from system.
1032 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001034 * This is the first part of the device unregistration
1035 * sequence. This removes the device from the lists we control
1036 * from here, has it removed from the other driver model
1037 * subsystems it was added to in device_add(), and removes it
1038 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001040 * NOTE: this should be called manually _iff_ device_add() was
1041 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001043void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001045 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001046 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Alan Sternec0676ee2008-12-05 14:10:31 -05001048 /* Notify clients of device removal. This call must come
1049 * before dpm_sysfs_remove().
1050 */
1051 if (dev->bus)
1052 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1053 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001054 device_pm_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001055 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001057 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001058 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001059 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001060 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001061 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001062 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001063 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001064 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001065
Dave Youngf75b1c62008-05-28 09:28:39 -07001066 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001067 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001068 list_for_each_entry(class_intf,
1069 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001070 if (class_intf->remove_dev)
1071 class_intf->remove_dev(dev, class_intf);
1072 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001073 klist_del(&dev->knode_class);
Dave Youngf75b1c62008-05-28 09:28:39 -07001074 mutex_unlock(&dev->class->p->class_mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001075 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001076 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001077 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001078 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Tejun Heo2f8d16a2007-03-09 19:34:19 +09001080 /*
1081 * Some platform devices are driven without driver attached
1082 * and managed resources may have been acquired. Make sure
1083 * all resources are released.
1084 */
1085 devres_release_all(dev);
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /* Notify the platform of the removal, in case they
1088 * need to do anything...
1089 */
1090 if (platform_notify_remove)
1091 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001092 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001093 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001095 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
1098/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001099 * device_unregister - unregister device from system.
1100 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001102 * We do this in two parts, like we do device_register(). First,
1103 * we remove it from all the subsystems with device_del(), then
1104 * we decrement the reference count via put_device(). If that
1105 * is the final reference count, the device will be cleaned up
1106 * via device_release() above. Otherwise, the structure will
1107 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001109void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001111 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 device_del(dev);
1113 put_device(dev);
1114}
1115
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001116static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001117{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001118 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001119 struct device *dev = NULL;
1120 struct device_private *p;
1121
1122 if (n) {
1123 p = to_device_private_parent(n);
1124 dev = p->device;
1125 }
1126 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001130 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001131 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001132 * @mode: returned file access mode
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001133 * @tmp: possibly allocated string
1134 *
1135 * Return the relative path of a possible device node.
1136 * Non-default names may need to allocate a memory to compose
1137 * a name. This memory is returned in tmp and needs to be
1138 * freed by the caller.
1139 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001140const char *device_get_devnode(struct device *dev,
1141 mode_t *mode, const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001142{
1143 char *s;
1144
1145 *tmp = NULL;
1146
1147 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001148 if (dev->type && dev->type->devnode)
1149 *tmp = dev->type->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001150 if (*tmp)
1151 return *tmp;
1152
1153 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001154 if (dev->class && dev->class->devnode)
1155 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001156 if (*tmp)
1157 return *tmp;
1158
1159 /* return name without allocation, tmp == NULL */
1160 if (strchr(dev_name(dev), '!') == NULL)
1161 return dev_name(dev);
1162
1163 /* replace '!' in the name with '/' */
1164 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1165 if (!*tmp)
1166 return NULL;
1167 while ((s = strchr(*tmp, '!')))
1168 s[0] = '/';
1169 return *tmp;
1170}
1171
1172/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001173 * device_for_each_child - device child iterator.
1174 * @parent: parent struct device.
1175 * @data: data for the callback.
1176 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001178 * Iterate over @parent's child devices, and call @fn for each,
1179 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001181 * We check the return of @fn each time. If it returns anything
1182 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001184int device_for_each_child(struct device *parent, void *data,
1185 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001187 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001188 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int error = 0;
1190
Greg Kroah-Hartman014c90d2009-04-15 16:00:12 -07001191 if (!parent->p)
1192 return 0;
1193
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001194 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001195 while ((child = next_device(&i)) && !error)
1196 error = fn(child, data);
1197 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return error;
1199}
1200
Cornelia Huck5ab69982006-11-16 15:42:07 +01001201/**
1202 * device_find_child - device iterator for locating a particular device.
1203 * @parent: parent struct device
1204 * @data: Data to pass to match function
1205 * @match: Callback function to check device
1206 *
1207 * This is similar to the device_for_each_child() function above, but it
1208 * returns a reference to a device that is 'found' for later use, as
1209 * determined by the @match callback.
1210 *
1211 * The callback should return 0 if the device doesn't match and non-zero
1212 * if it does. If the callback returns non-zero and a reference to the
1213 * current device can be obtained, this function will return to the caller
1214 * and not iterate over any more devices.
1215 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001216struct device *device_find_child(struct device *parent, void *data,
1217 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001218{
1219 struct klist_iter i;
1220 struct device *child;
1221
1222 if (!parent)
1223 return NULL;
1224
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001225 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001226 while ((child = next_device(&i)))
1227 if (match(child, data) && get_device(child))
1228 break;
1229 klist_iter_exit(&i);
1230 return child;
1231}
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233int __init devices_init(void)
1234{
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -06001235 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1236 if (!devices_kset)
1237 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001238 dev_kobj = kobject_create_and_add("dev", NULL);
1239 if (!dev_kobj)
1240 goto dev_kobj_err;
1241 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1242 if (!sysfs_dev_block_kobj)
1243 goto block_kobj_err;
1244 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1245 if (!sysfs_dev_char_kobj)
1246 goto char_kobj_err;
1247
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -06001248 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001249
1250 char_kobj_err:
1251 kobject_put(sysfs_dev_block_kobj);
1252 block_kobj_err:
1253 kobject_put(dev_kobj);
1254 dev_kobj_err:
1255 kset_unregister(devices_kset);
1256 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001260EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262EXPORT_SYMBOL_GPL(device_initialize);
1263EXPORT_SYMBOL_GPL(device_add);
1264EXPORT_SYMBOL_GPL(device_register);
1265
1266EXPORT_SYMBOL_GPL(device_del);
1267EXPORT_SYMBOL_GPL(device_unregister);
1268EXPORT_SYMBOL_GPL(get_device);
1269EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271EXPORT_SYMBOL_GPL(device_create_file);
1272EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001273
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001274struct root_device
1275{
1276 struct device dev;
1277 struct module *owner;
1278};
1279
1280#define to_root_device(dev) container_of(dev, struct root_device, dev)
1281
1282static void root_device_release(struct device *dev)
1283{
1284 kfree(to_root_device(dev));
1285}
1286
1287/**
1288 * __root_device_register - allocate and register a root device
1289 * @name: root device name
1290 * @owner: owner module of the root device, usually THIS_MODULE
1291 *
1292 * This function allocates a root device and registers it
1293 * using device_register(). In order to free the returned
1294 * device, use root_device_unregister().
1295 *
1296 * Root devices are dummy devices which allow other devices
1297 * to be grouped under /sys/devices. Use this function to
1298 * allocate a root device and then use it as the parent of
1299 * any device which should appear under /sys/devices/{name}
1300 *
1301 * The /sys/devices/{name} directory will also contain a
1302 * 'module' symlink which points to the @owner directory
1303 * in sysfs.
1304 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001305 * Returns &struct device pointer on success, or ERR_PTR() on error.
1306 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001307 * Note: You probably want to use root_device_register().
1308 */
1309struct device *__root_device_register(const char *name, struct module *owner)
1310{
1311 struct root_device *root;
1312 int err = -ENOMEM;
1313
1314 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1315 if (!root)
1316 return ERR_PTR(err);
1317
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001318 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001319 if (err) {
1320 kfree(root);
1321 return ERR_PTR(err);
1322 }
1323
1324 root->dev.release = root_device_release;
1325
1326 err = device_register(&root->dev);
1327 if (err) {
1328 put_device(&root->dev);
1329 return ERR_PTR(err);
1330 }
1331
Christoph Egger1d9e8822010-05-17 16:57:58 +02001332#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001333 if (owner) {
1334 struct module_kobject *mk = &owner->mkobj;
1335
1336 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1337 if (err) {
1338 device_unregister(&root->dev);
1339 return ERR_PTR(err);
1340 }
1341 root->owner = owner;
1342 }
1343#endif
1344
1345 return &root->dev;
1346}
1347EXPORT_SYMBOL_GPL(__root_device_register);
1348
1349/**
1350 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001351 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001352 *
1353 * This function unregisters and cleans up a device that was created by
1354 * root_device_register().
1355 */
1356void root_device_unregister(struct device *dev)
1357{
1358 struct root_device *root = to_root_device(dev);
1359
1360 if (root->owner)
1361 sysfs_remove_link(&root->dev.kobj, "module");
1362
1363 device_unregister(dev);
1364}
1365EXPORT_SYMBOL_GPL(root_device_unregister);
1366
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001367
1368static void device_create_release(struct device *dev)
1369{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001370 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001371 kfree(dev);
1372}
1373
1374/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001375 * device_create_vargs - creates a device and registers it with sysfs
1376 * @class: pointer to the struct class that this device should be registered to
1377 * @parent: pointer to the parent struct device of this new device, if any
1378 * @devt: the dev_t for the char device to be added
1379 * @drvdata: the data to be added to the device for callbacks
1380 * @fmt: string for the device's name
1381 * @args: va_list for the device's name
1382 *
1383 * This function can be used by char device classes. A struct device
1384 * will be created in sysfs, registered to the specified class.
1385 *
1386 * A "dev" file will be created, showing the dev_t for the device, if
1387 * the dev_t is not 0,0.
1388 * If a pointer to a parent struct device is passed in, the newly created
1389 * struct device will be a child of that device in sysfs.
1390 * The pointer to the struct device will be returned from the call.
1391 * Any further sysfs files that might be required can be created using this
1392 * pointer.
1393 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001394 * Returns &struct device pointer on success, or ERR_PTR() on error.
1395 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001396 * Note: the struct class passed to this function must have previously
1397 * been created with a call to class_create().
1398 */
1399struct device *device_create_vargs(struct class *class, struct device *parent,
1400 dev_t devt, void *drvdata, const char *fmt,
1401 va_list args)
1402{
1403 struct device *dev = NULL;
1404 int retval = -ENODEV;
1405
1406 if (class == NULL || IS_ERR(class))
1407 goto error;
1408
1409 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1410 if (!dev) {
1411 retval = -ENOMEM;
1412 goto error;
1413 }
1414
1415 dev->devt = devt;
1416 dev->class = class;
1417 dev->parent = parent;
1418 dev->release = device_create_release;
1419 dev_set_drvdata(dev, drvdata);
1420
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001421 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1422 if (retval)
1423 goto error;
1424
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001425 retval = device_register(dev);
1426 if (retval)
1427 goto error;
1428
1429 return dev;
1430
1431error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001432 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001433 return ERR_PTR(retval);
1434}
1435EXPORT_SYMBOL_GPL(device_create_vargs);
1436
1437/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001438 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001439 * @class: pointer to the struct class that this device should be registered to
1440 * @parent: pointer to the parent struct device of this new device, if any
1441 * @devt: the dev_t for the char device to be added
1442 * @drvdata: the data to be added to the device for callbacks
1443 * @fmt: string for the device's name
1444 *
1445 * This function can be used by char device classes. A struct device
1446 * will be created in sysfs, registered to the specified class.
1447 *
1448 * A "dev" file will be created, showing the dev_t for the device, if
1449 * the dev_t is not 0,0.
1450 * If a pointer to a parent struct device is passed in, the newly created
1451 * struct device will be a child of that device in sysfs.
1452 * The pointer to the struct device will be returned from the call.
1453 * Any further sysfs files that might be required can be created using this
1454 * pointer.
1455 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001456 * Returns &struct device pointer on success, or ERR_PTR() on error.
1457 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001458 * Note: the struct class passed to this function must have previously
1459 * been created with a call to class_create().
1460 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001461struct device *device_create(struct class *class, struct device *parent,
1462 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001463{
1464 va_list vargs;
1465 struct device *dev;
1466
1467 va_start(vargs, fmt);
1468 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1469 va_end(vargs);
1470 return dev;
1471}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001472EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001473
Dave Youngcd354492008-01-28 16:56:11 +08001474static int __match_devt(struct device *dev, void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001475{
Dave Youngcd354492008-01-28 16:56:11 +08001476 dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001477
Dave Youngcd354492008-01-28 16:56:11 +08001478 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001479}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001480
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001481/**
1482 * device_destroy - removes a device that was created with device_create()
1483 * @class: pointer to the struct class that this device was registered with
1484 * @devt: the dev_t of the device that was previously registered
1485 *
1486 * This call unregisters and cleans up a device that was created with a
1487 * call to device_create().
1488 */
1489void device_destroy(struct class *class, dev_t devt)
1490{
1491 struct device *dev;
1492
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001493 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001494 if (dev) {
1495 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001496 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001497 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001498}
1499EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001500
1501/**
1502 * device_rename - renames a device
1503 * @dev: the pointer to the struct device to be renamed
1504 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001505 *
1506 * It is the responsibility of the caller to provide mutual
1507 * exclusion between two different calls of device_rename
1508 * on the same device to ensure that new_name is valid and
1509 * won't conflict with other devices.
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001510 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001511int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001512{
1513 char *old_class_name = NULL;
1514 char *new_class_name = NULL;
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001515 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001516 int error;
1517
1518 dev = get_device(dev);
1519 if (!dev)
1520 return -EINVAL;
1521
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001522 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001523 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001524
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001525 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001526 if (!old_device_name) {
1527 error = -ENOMEM;
1528 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001529 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001530
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001531 if (dev->class) {
1532 error = sysfs_rename_link(&dev->class->p->class_subsys.kobj,
1533 &dev->kobj, old_device_name, new_name);
1534 if (error)
1535 goto out;
1536 }
Kay Sievers39aba962010-09-04 22:33:14 -07001537
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001538 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001539 if (error)
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001540 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001541
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001542out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001543 put_device(dev);
1544
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001545 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001546 kfree(old_class_name);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001547 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001548
1549 return error;
1550}
Johannes Berga2807db2007-02-28 12:38:31 +01001551EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001552
1553static int device_move_class_links(struct device *dev,
1554 struct device *old_parent,
1555 struct device *new_parent)
1556{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001557 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001558
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001559 if (old_parent)
1560 sysfs_remove_link(&dev->kobj, "device");
1561 if (new_parent)
1562 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1563 "device");
1564 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001565}
1566
1567/**
1568 * device_move - moves a device to a new parent
1569 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aea2007-01-08 20:16:44 +01001570 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001571 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001572 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001573int device_move(struct device *dev, struct device *new_parent,
1574 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001575{
1576 int error;
1577 struct device *old_parent;
Cornelia Huckc744aea2007-01-08 20:16:44 +01001578 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001579
1580 dev = get_device(dev);
1581 if (!dev)
1582 return -EINVAL;
1583
Cornelia Huckffa6a702009-03-04 12:44:00 +01001584 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001585 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001586 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001587
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001588 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1589 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aea2007-01-08 20:16:44 +01001590 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001591 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001592 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001593 put_device(new_parent);
1594 goto out;
1595 }
1596 old_parent = dev->parent;
1597 dev->parent = new_parent;
1598 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001599 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001600 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001601 klist_add_tail(&dev->p->knode_parent,
1602 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001603 set_dev_node(dev, dev_to_node(new_parent));
1604 }
1605
Cornelia Huck8a824722006-11-20 17:07:51 +01001606 if (!dev->class)
1607 goto out_put;
1608 error = device_move_class_links(dev, old_parent, new_parent);
1609 if (error) {
1610 /* We ignore errors on cleanup since we're hosed anyway... */
1611 device_move_class_links(dev, new_parent, old_parent);
1612 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +01001613 if (new_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001614 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001615 dev->parent = old_parent;
1616 if (old_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001617 klist_add_tail(&dev->p->knode_parent,
1618 &old_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001619 set_dev_node(dev, dev_to_node(old_parent));
1620 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001621 }
Cornelia Huck63b69712008-01-21 16:09:44 +01001622 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001623 put_device(new_parent);
1624 goto out;
1625 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001626 switch (dpm_order) {
1627 case DPM_ORDER_NONE:
1628 break;
1629 case DPM_ORDER_DEV_AFTER_PARENT:
1630 device_pm_move_after(dev, new_parent);
1631 break;
1632 case DPM_ORDER_PARENT_BEFORE_DEV:
1633 device_pm_move_before(new_parent, dev);
1634 break;
1635 case DPM_ORDER_DEV_LAST:
1636 device_pm_move_last(dev);
1637 break;
1638 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001639out_put:
1640 put_device(old_parent);
1641out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001642 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001643 put_device(dev);
1644 return error;
1645}
Cornelia Huck8a824722006-11-20 17:07:51 +01001646EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001647
1648/**
1649 * device_shutdown - call ->shutdown() on each device to shutdown.
1650 */
1651void device_shutdown(void)
1652{
Hugh Daschbach62458382010-03-22 10:36:37 -07001653 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001654
Hugh Daschbach62458382010-03-22 10:36:37 -07001655 spin_lock(&devices_kset->list_lock);
1656 /*
1657 * Walk the devices list backward, shutting down each in turn.
1658 * Beware that device unplug events may also start pulling
1659 * devices offline, even as the system is shutting down.
1660 */
1661 while (!list_empty(&devices_kset->list)) {
1662 dev = list_entry(devices_kset->list.prev, struct device,
1663 kobj.entry);
1664 get_device(dev);
1665 /*
1666 * Make sure the device is off the kset list, in the
1667 * event that dev->*->shutdown() doesn't remove it.
1668 */
1669 list_del_init(&dev->kobj.entry);
1670 spin_unlock(&devices_kset->list_lock);
1671
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001672 if (dev->bus && dev->bus->shutdown) {
1673 dev_dbg(dev, "shutdown\n");
1674 dev->bus->shutdown(dev);
1675 } else if (dev->driver && dev->driver->shutdown) {
1676 dev_dbg(dev, "shutdown\n");
1677 dev->driver->shutdown(dev);
1678 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001679 put_device(dev);
1680
1681 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001682 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001683 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07001684 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001685}
Joe Perches99bcf212010-06-27 01:02:34 +00001686
1687/*
1688 * Device logging functions
1689 */
1690
1691#ifdef CONFIG_PRINTK
1692
1693static int __dev_printk(const char *level, const struct device *dev,
1694 struct va_format *vaf)
1695{
1696 if (!dev)
1697 return printk("%s(NULL device *): %pV", level, vaf);
1698
1699 return printk("%s%s %s: %pV",
1700 level, dev_driver_string(dev), dev_name(dev), vaf);
1701}
1702
1703int dev_printk(const char *level, const struct device *dev,
1704 const char *fmt, ...)
1705{
1706 struct va_format vaf;
1707 va_list args;
1708 int r;
1709
1710 va_start(args, fmt);
1711
1712 vaf.fmt = fmt;
1713 vaf.va = &args;
1714
1715 r = __dev_printk(level, dev, &vaf);
1716 va_end(args);
1717
1718 return r;
1719}
1720EXPORT_SYMBOL(dev_printk);
1721
1722#define define_dev_printk_level(func, kern_level) \
1723int func(const struct device *dev, const char *fmt, ...) \
1724{ \
1725 struct va_format vaf; \
1726 va_list args; \
1727 int r; \
1728 \
1729 va_start(args, fmt); \
1730 \
1731 vaf.fmt = fmt; \
1732 vaf.va = &args; \
1733 \
1734 r = __dev_printk(kern_level, dev, &vaf); \
1735 va_end(args); \
1736 \
1737 return r; \
1738} \
1739EXPORT_SYMBOL(func);
1740
1741define_dev_printk_level(dev_emerg, KERN_EMERG);
1742define_dev_printk_level(dev_alert, KERN_ALERT);
1743define_dev_printk_level(dev_crit, KERN_CRIT);
1744define_dev_printk_level(dev_err, KERN_ERR);
1745define_dev_printk_level(dev_warn, KERN_WARNING);
1746define_dev_printk_level(dev_notice, KERN_NOTICE);
1747define_dev_printk_level(_dev_info, KERN_INFO);
1748
1749#endif