blob: 6cf9069f315003fadcc8b46d5c4a1dfb8dfdeec8 [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 Sievers7eff2e72007-08-14 15:15:12 +0200206 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100207 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200208 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200209 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800210 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100211 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
Kay Sievers7eff2e72007-08-14 15:15:12 +0200214 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700215 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200216 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200217 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800218 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100219 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800220 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200221 }
222
Kay Sievers7eff2e72007-08-14 15:15:12 +0200223 /* have the device type specific fuction add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200224 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200225 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200226 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800227 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100228 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800229 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700230 }
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return retval;
233}
234
Emese Revfy9cd43612009-12-31 14:52:51 +0100235static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100236 .filter = dev_uevent_filter,
237 .name = dev_uevent_name,
238 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239};
240
Kay Sievers16574dc2007-04-06 01:40:38 +0200241static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
242 char *buf)
243{
244 struct kobject *top_kobj;
245 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200246 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200247 int i;
248 size_t count = 0;
249 int retval;
250
251 /* search the kset, the device belongs to */
252 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200253 while (!top_kobj->kset && top_kobj->parent)
254 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200255 if (!top_kobj->kset)
256 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200257
Kay Sievers16574dc2007-04-06 01:40:38 +0200258 kset = top_kobj->kset;
259 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
260 goto out;
261
262 /* respect filter */
263 if (kset->uevent_ops && kset->uevent_ops->filter)
264 if (!kset->uevent_ops->filter(kset, &dev->kobj))
265 goto out;
266
Kay Sievers7eff2e72007-08-14 15:15:12 +0200267 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
268 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200269 return -ENOMEM;
270
Kay Sievers16574dc2007-04-06 01:40:38 +0200271 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200272 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200273 if (retval)
274 goto out;
275
276 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200277 for (i = 0; i < env->envp_idx; i++)
278 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200279out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200280 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200281 return count;
282}
283
Kay Sieversa7fd6702005-10-01 14:49:43 +0200284static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
285 const char *buf, size_t count)
286{
Kay Sievers60a96a52007-07-08 22:29:26 +0200287 enum kobject_action action;
288
Kay Sievers3f5468c2010-01-14 22:54:37 +0100289 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200290 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100291 else
292 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200293 return count;
294}
295
Tejun Heoad6a1e12007-06-14 03:45:17 +0900296static struct device_attribute uevent_attr =
297 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
298
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500299static int device_add_attributes(struct device *dev,
300 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700301{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700302 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500303 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700304
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500305 if (attrs) {
306 for (i = 0; attr_name(attrs[i]); i++) {
307 error = device_create_file(dev, &attrs[i]);
308 if (error)
309 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700310 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500311 if (error)
312 while (--i >= 0)
313 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700314 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700315 return error;
316}
317
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500318static void device_remove_attributes(struct device *dev,
319 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700320{
321 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500322
323 if (attrs)
324 for (i = 0; attr_name(attrs[i]); i++)
325 device_remove_file(dev, &attrs[i]);
326}
327
328static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700329 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500330{
331 int error = 0;
332 int i;
333
334 if (groups) {
335 for (i = 0; groups[i]; i++) {
336 error = sysfs_create_group(&dev->kobj, groups[i]);
337 if (error) {
338 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800339 sysfs_remove_group(&dev->kobj,
340 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500341 break;
342 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700343 }
344 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500345 return error;
346}
347
348static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700349 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500350{
351 int i;
352
353 if (groups)
354 for (i = 0; groups[i]; i++)
355 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700356}
357
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700358static int device_add_attrs(struct device *dev)
359{
360 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200361 struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500362 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700363
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500364 if (class) {
365 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200366 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500367 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700368 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200369
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500370 if (type) {
371 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200372 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500373 goto err_remove_class_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200374 }
375
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500376 error = device_add_groups(dev, dev->groups);
377 if (error)
378 goto err_remove_type_groups;
379
380 return 0;
381
382 err_remove_type_groups:
383 if (type)
384 device_remove_groups(dev, type->groups);
385 err_remove_class_attrs:
386 if (class)
387 device_remove_attributes(dev, class->dev_attrs);
388
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700389 return error;
390}
391
392static void device_remove_attrs(struct device *dev)
393{
394 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200395 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700396
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500397 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200398
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500399 if (type)
400 device_remove_groups(dev, type->groups);
401
402 if (class)
403 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700404}
405
406
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700407static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
408 char *buf)
409{
410 return print_dev_t(buf, dev->devt);
411}
412
Tejun Heoad6a1e12007-06-14 03:45:17 +0900413static struct device_attribute devt_attr =
414 __ATTR(dev, S_IRUGO, show_dev, NULL);
415
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600416/* kset to create /sys/devices/ */
417struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800420 * device_create_file - create sysfs attribute file for device.
421 * @dev: device.
422 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200424int device_create_file(struct device *dev,
425 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 int error = 0;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100428 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 error = sysfs_create_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return error;
431}
432
433/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800434 * device_remove_file - remove sysfs attribute file.
435 * @dev: device.
436 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200438void device_remove_file(struct device *dev,
439 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100441 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700445/**
446 * device_create_bin_file - create sysfs binary attribute file for device.
447 * @dev: device.
448 * @attr: device binary attribute descriptor.
449 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200450int device_create_bin_file(struct device *dev,
451 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700452{
453 int error = -EINVAL;
454 if (dev)
455 error = sysfs_create_bin_file(&dev->kobj, attr);
456 return error;
457}
458EXPORT_SYMBOL_GPL(device_create_bin_file);
459
460/**
461 * device_remove_bin_file - remove sysfs binary attribute file
462 * @dev: device.
463 * @attr: device binary attribute descriptor.
464 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200465void device_remove_bin_file(struct device *dev,
466 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700467{
468 if (dev)
469 sysfs_remove_bin_file(&dev->kobj, attr);
470}
471EXPORT_SYMBOL_GPL(device_remove_bin_file);
472
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400473/**
Alan Stern523ded72007-04-26 00:12:04 -0700474 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400475 * @dev: device.
476 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700477 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400478 *
479 * Attribute methods must not unregister themselves or their parent device
480 * (which would amount to the same thing). Attempts to do so will deadlock,
481 * since unregistration is mutually exclusive with driver callbacks.
482 *
483 * Instead methods can call this routine, which will attempt to allocate
484 * and schedule a workqueue request to call back @func with @dev as its
485 * argument in the workqueue's process context. @dev will be pinned until
486 * @func returns.
487 *
Alan Stern523ded72007-04-26 00:12:04 -0700488 * This routine is usually called via the inline device_schedule_callback(),
489 * which automatically sets @owner to THIS_MODULE.
490 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400491 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700492 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400493 *
494 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
495 * underlying sysfs routine (since it is intended for use by attribute
496 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
497 */
Alan Stern523ded72007-04-26 00:12:04 -0700498int device_schedule_callback_owner(struct device *dev,
499 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400500{
501 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700502 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400503}
Alan Stern523ded72007-04-26 00:12:04 -0700504EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400505
James Bottomley34bb61f2005-09-06 16:56:51 -0700506static void klist_children_get(struct klist_node *n)
507{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800508 struct device_private *p = to_device_private_parent(n);
509 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700510
511 get_device(dev);
512}
513
514static void klist_children_put(struct klist_node *n)
515{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800516 struct device_private *p = to_device_private_parent(n);
517 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700518
519 put_device(dev);
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800523 * device_initialize - init device structure.
524 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 *
Cornelia Huck57394112008-09-03 18:26:40 +0200526 * This prepares the device for use by other layers by initializing
527 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800528 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200529 * that function, though it can also be called separately, so one
530 * may use @dev's fields. In particular, get_device()/put_device()
531 * may be used for reference counting of @dev after calling this
532 * function.
533 *
534 * NOTE: Use put_device() to give up your reference instead of freeing
535 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537void device_initialize(struct device *dev)
538{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600539 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700540 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000542 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100543 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900544 spin_lock_init(&dev->devres_lock);
545 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400546 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800547 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Kay Sievers86406242007-03-14 03:25:56 +0100550static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700551{
Kay Sievers86406242007-03-14 03:25:56 +0100552 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700553
Kay Sievers86406242007-03-14 03:25:56 +0100554 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800555 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600556 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700557
Kay Sievers86406242007-03-14 03:25:56 +0100558 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700559}
560
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700561struct class_dir {
562 struct kobject kobj;
563 struct class *class;
564};
565
566#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
567
568static void class_dir_release(struct kobject *kobj)
569{
570 struct class_dir *dir = to_class_dir(kobj);
571 kfree(dir);
572}
573
574static const
575struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
576{
577 struct class_dir *dir = to_class_dir(kobj);
578 return dir->class->ns_type;
579}
580
581static struct kobj_type class_dir_ktype = {
582 .release = class_dir_release,
583 .sysfs_ops = &kobj_sysfs_ops,
584 .child_ns_type = class_dir_child_ns_type
585};
586
587static struct kobject *
588class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
589{
590 struct class_dir *dir;
591 int retval;
592
593 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
594 if (!dir)
595 return NULL;
596
597 dir->class = class;
598 kobject_init(&dir->kobj, &class_dir_ktype);
599
600 dir->kobj.kset = &class->p->class_dirs;
601
602 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
603 if (retval < 0) {
604 kobject_put(&dir->kobj);
605 return NULL;
606 }
607 return &dir->kobj;
608}
609
610
Kay Sieversda231fd2007-11-21 17:29:15 +0100611static struct kobject *get_device_parent(struct device *dev,
612 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100613{
Kay Sievers86406242007-03-14 03:25:56 +0100614 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900615 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100616 struct kobject *kobj = NULL;
617 struct kobject *parent_kobj;
618 struct kobject *k;
619
Kay Sievers39aba962010-09-04 22:33:14 -0700620#ifdef CONFIG_SYSFS_DEPRECATED
621 /* block disks show up in /sys/block */
622 if (dev->class == &block_class) {
623 if (parent && parent->class == &block_class)
624 return &parent->kobj;
625 return &block_class.p->class_subsys.kobj;
626 }
627#endif
Kay Sievers86406242007-03-14 03:25:56 +0100628 /*
629 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100630 * Class-devices with a non class-device as parent, live
631 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100632 */
633 if (parent == NULL)
634 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700635 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100636 return &parent->kobj;
637 else
638 parent_kobj = &parent->kobj;
639
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900640 mutex_lock(&gdp_mutex);
641
Kay Sievers86406242007-03-14 03:25:56 +0100642 /* find our class-directory at the parent and reference it */
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500643 spin_lock(&dev->class->p->class_dirs.list_lock);
644 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100645 if (k->parent == parent_kobj) {
646 kobj = kobject_get(k);
647 break;
648 }
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500649 spin_unlock(&dev->class->p->class_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900650 if (kobj) {
651 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100652 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900653 }
Kay Sievers86406242007-03-14 03:25:56 +0100654
655 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700656 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100657 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900658 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800659 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100660 }
661
662 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100663 return &parent->kobj;
664 return NULL;
665}
Kay Sieversda231fd2007-11-21 17:29:15 +0100666
Cornelia Huck63b69712008-01-21 16:09:44 +0100667static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100668{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100669 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100670 if (!glue_dir || !dev->class ||
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500671 glue_dir->kset != &dev->class->p->class_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100672 return;
673
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100674 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100675}
Cornelia Huck63b69712008-01-21 16:09:44 +0100676
677static void cleanup_device_parent(struct device *dev)
678{
679 cleanup_glue_dir(dev, dev->kobj.parent);
680}
Kay Sievers86406242007-03-14 03:25:56 +0100681
Cornelia Huck63b69712008-01-21 16:09:44 +0100682static void setup_parent(struct device *dev, struct device *parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100683{
684 struct kobject *kobj;
685 kobj = get_device_parent(dev, parent);
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100686 if (kobj)
687 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100688}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100689
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700690static int device_add_class_symlinks(struct device *dev)
691{
692 int error;
693
694 if (!dev->class)
695 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100696
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700697 error = sysfs_create_link(&dev->kobj,
698 &dev->class->p->class_subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700699 "subsystem");
700 if (error)
701 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100702
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800703 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700704 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
705 "device");
706 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700707 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700708 }
Kay Sievers39aba962010-09-04 22:33:14 -0700709
710#ifdef CONFIG_SYSFS_DEPRECATED
711 /* /sys/block has directories and does not need symlinks */
712 if (dev->class == &block_class)
713 return 0;
714#endif
715
716 /* link in the class directory pointing to the device */
717 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
718 &dev->kobj, dev_name(dev));
719 if (error)
720 goto out_device;
721
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700722 return 0;
723
Kay Sievers39aba962010-09-04 22:33:14 -0700724out_device:
725 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100726
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700727out_subsys:
728 sysfs_remove_link(&dev->kobj, "subsystem");
729out:
730 return error;
731}
732
733static void device_remove_class_symlinks(struct device *dev)
734{
735 if (!dev->class)
736 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100737
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800738 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100739 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700740 sysfs_remove_link(&dev->kobj, "subsystem");
Kay Sievers39aba962010-09-04 22:33:14 -0700741#ifdef CONFIG_SYSFS_DEPRECATED
742 if (dev->class == &block_class)
743 return;
744#endif
745 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000749 * dev_set_name - set a device name
750 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700751 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000752 */
753int dev_set_name(struct device *dev, const char *fmt, ...)
754{
755 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100756 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000757
758 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100759 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000760 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100761 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000762}
763EXPORT_SYMBOL_GPL(dev_set_name);
764
765/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700766 * device_to_dev_kobj - select a /sys/dev/ directory for the device
767 * @dev: device
768 *
769 * By default we select char/ for new entries. Setting class->dev_obj
770 * to NULL prevents an entry from being created. class->dev_kobj must
771 * be set (or cleared) before any devices are registered to the class
772 * otherwise device_create_sys_dev_entry() and
773 * device_remove_sys_dev_entry() will disagree about the the presence
774 * of the link.
775 */
776static struct kobject *device_to_dev_kobj(struct device *dev)
777{
778 struct kobject *kobj;
779
780 if (dev->class)
781 kobj = dev->class->dev_kobj;
782 else
783 kobj = sysfs_dev_char_kobj;
784
785 return kobj;
786}
787
788static int device_create_sys_dev_entry(struct device *dev)
789{
790 struct kobject *kobj = device_to_dev_kobj(dev);
791 int error = 0;
792 char devt_str[15];
793
794 if (kobj) {
795 format_dev_t(devt_str, dev->devt);
796 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
797 }
798
799 return error;
800}
801
802static void device_remove_sys_dev_entry(struct device *dev)
803{
804 struct kobject *kobj = device_to_dev_kobj(dev);
805 char devt_str[15];
806
807 if (kobj) {
808 format_dev_t(devt_str, dev->devt);
809 sysfs_remove_link(kobj, devt_str);
810 }
811}
812
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700813int device_private_init(struct device *dev)
814{
815 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
816 if (!dev->p)
817 return -ENOMEM;
818 dev->p->device = dev;
819 klist_init(&dev->p->klist_children, klist_children_get,
820 klist_children_put);
821 return 0;
822}
823
Dan Williamse105b8b2008-04-21 10:51:07 -0700824/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800825 * device_add - add device to device hierarchy.
826 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800828 * This is part 2 of device_register(), though may be called
829 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *
Cornelia Huck57394112008-09-03 18:26:40 +0200831 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800832 * to the global and sibling lists for the device, then
833 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200834 *
835 * NOTE: _Never_ directly free @dev after calling this function, even
836 * if it returned an error! Always use put_device() to give up your
837 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
839int device_add(struct device *dev)
840{
841 struct device *parent = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200842 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700843 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700846 if (!dev)
847 goto done;
848
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800849 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700850 error = device_private_init(dev);
851 if (error)
852 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800853 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800854
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100855 /*
856 * for statically allocated devices, which should all be converted
857 * some day, we need to initialize the name. We prevent reading back
858 * the name, and force the use of dev_name()
859 */
860 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700861 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100862 dev->init_name = NULL;
863 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700864
Thomas Gleixnere6309e72009-12-10 19:32:49 +0000865 if (!dev_name(dev)) {
866 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -0700867 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +0000868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100870 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 parent = get_device(dev->parent);
Cornelia Huck63b69712008-01-21 16:09:44 +0100873 setup_parent(dev, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Yinghai Lu0d358f22008-02-19 03:20:41 -0800875 /* use parent numa_node */
876 if (parent)
877 set_dev_node(dev, dev_to_node(parent));
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -0700880 /* we require the name to be set before, and pass NULL */
881 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100882 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200884
Brian Walsh37022642006-08-14 22:43:19 -0700885 /* notify platform of device entry */
886 if (platform_notify)
887 platform_notify(dev);
888
Tejun Heoad6a1e12007-06-14 03:45:17 +0900889 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200890 if (error)
891 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200892
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700893 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900894 error = device_create_file(dev, &devt_attr);
895 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +0200896 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -0700897
898 error = device_create_sys_dev_entry(dev);
899 if (error)
900 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +0200901
902 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700903 }
904
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700905 error = device_add_class_symlinks(dev);
906 if (error)
907 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700908 error = device_add_attrs(dev);
909 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700910 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700911 error = bus_add_device(dev);
912 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -0400914 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100915 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -0400916 goto DPMError;
917 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -0500918
919 /* Notify clients of device addition. This call must come
920 * after dpm_sysf_add() and before kobject_uevent().
921 */
922 if (dev->bus)
923 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
924 BUS_NOTIFY_ADD_DEVICE, dev);
925
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200926 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -0400927 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800929 klist_add_tail(&dev->p->knode_parent,
930 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700932 if (dev->class) {
Dave Youngf75b1c62008-05-28 09:28:39 -0700933 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200934 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +0200935 klist_add_tail(&dev->knode_class,
936 &dev->class->p->class_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200937
938 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -0700939 list_for_each_entry(class_intf,
940 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200941 if (class_intf->add_dev)
942 class_intf->add_dev(dev, class_intf);
Dave Youngf75b1c62008-05-28 09:28:39 -0700943 mutex_unlock(&dev->class->p->class_mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700944 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700945done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 put_device(dev);
947 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -0400948 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100949 bus_remove_device(dev);
950 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +0000951 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700952 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700953 device_remove_class_symlinks(dev);
954 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900955 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +0100956 devtmpfs_delete_node(dev);
957 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -0700958 device_remove_sys_dev_entry(dev);
959 devtattrError:
960 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +0900961 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200962 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900963 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700964 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100965 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 kobject_del(&dev->kobj);
967 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +0100968 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (parent)
970 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -0700971name_error:
972 kfree(dev->p);
973 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700974 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800978 * device_register - register a device with the system.
979 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800981 * This happens in two clean steps - initialize the device
982 * and add it to the system. The two steps can be called
983 * separately, but this is the easiest and most common.
984 * I.e. you should only call the two helpers separately if
985 * have a clearly defined need to use and refcount the device
986 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +0200987 *
988 * NOTE: _Never_ directly free @dev after calling this function, even
989 * if it returned an error! Always use put_device() to give up the
990 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992int device_register(struct device *dev)
993{
994 device_initialize(dev);
995 return device_add(dev);
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800999 * get_device - increment reference count for device.
1000 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001002 * This simply forwards the call to kobject_get(), though
1003 * we do take care to provide for the case that we get a NULL
1004 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001006struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001012 * put_device - decrement reference count.
1013 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001015void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001017 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (dev)
1019 kobject_put(&dev->kobj);
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001023 * device_del - delete device from system.
1024 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001026 * This is the first part of the device unregistration
1027 * sequence. This removes the device from the lists we control
1028 * from here, has it removed from the other driver model
1029 * subsystems it was added to in device_add(), and removes it
1030 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001032 * NOTE: this should be called manually _iff_ device_add() was
1033 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001035void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001037 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001038 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Alan Sternec0676ee2008-12-05 14:10:31 -05001040 /* Notify clients of device removal. This call must come
1041 * before dpm_sysfs_remove().
1042 */
1043 if (dev->bus)
1044 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1045 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001046 device_pm_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001047 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001049 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001050 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001051 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001052 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001053 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001054 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001055 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001056 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001057
Dave Youngf75b1c62008-05-28 09:28:39 -07001058 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001059 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001060 list_for_each_entry(class_intf,
1061 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001062 if (class_intf->remove_dev)
1063 class_intf->remove_dev(dev, class_intf);
1064 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001065 klist_del(&dev->knode_class);
Dave Youngf75b1c62008-05-28 09:28:39 -07001066 mutex_unlock(&dev->class->p->class_mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001067 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001068 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001069 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001070 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Tejun Heo2f8d16a2007-03-09 19:34:19 +09001072 /*
1073 * Some platform devices are driven without driver attached
1074 * and managed resources may have been acquired. Make sure
1075 * all resources are released.
1076 */
1077 devres_release_all(dev);
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* Notify the platform of the removal, in case they
1080 * need to do anything...
1081 */
1082 if (platform_notify_remove)
1083 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001084 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001085 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001087 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001091 * device_unregister - unregister device from system.
1092 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001094 * We do this in two parts, like we do device_register(). First,
1095 * we remove it from all the subsystems with device_del(), then
1096 * we decrement the reference count via put_device(). If that
1097 * is the final reference count, the device will be cleaned up
1098 * via device_release() above. Otherwise, the structure will
1099 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001101void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001103 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 device_del(dev);
1105 put_device(dev);
1106}
1107
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001108static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001109{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001110 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001111 struct device *dev = NULL;
1112 struct device_private *p;
1113
1114 if (n) {
1115 p = to_device_private_parent(n);
1116 dev = p->device;
1117 }
1118 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001122 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001123 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001124 * @mode: returned file access mode
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001125 * @tmp: possibly allocated string
1126 *
1127 * Return the relative path of a possible device node.
1128 * Non-default names may need to allocate a memory to compose
1129 * a name. This memory is returned in tmp and needs to be
1130 * freed by the caller.
1131 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001132const char *device_get_devnode(struct device *dev,
1133 mode_t *mode, const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001134{
1135 char *s;
1136
1137 *tmp = NULL;
1138
1139 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001140 if (dev->type && dev->type->devnode)
1141 *tmp = dev->type->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001142 if (*tmp)
1143 return *tmp;
1144
1145 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001146 if (dev->class && dev->class->devnode)
1147 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001148 if (*tmp)
1149 return *tmp;
1150
1151 /* return name without allocation, tmp == NULL */
1152 if (strchr(dev_name(dev), '!') == NULL)
1153 return dev_name(dev);
1154
1155 /* replace '!' in the name with '/' */
1156 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1157 if (!*tmp)
1158 return NULL;
1159 while ((s = strchr(*tmp, '!')))
1160 s[0] = '/';
1161 return *tmp;
1162}
1163
1164/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001165 * device_for_each_child - device child iterator.
1166 * @parent: parent struct device.
1167 * @data: data for the callback.
1168 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001170 * Iterate over @parent's child devices, and call @fn for each,
1171 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001173 * We check the return of @fn each time. If it returns anything
1174 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001176int device_for_each_child(struct device *parent, void *data,
1177 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001179 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001180 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int error = 0;
1182
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001183 if (!parent->p)
1184 return 0;
1185
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001186 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001187 while ((child = next_device(&i)) && !error)
1188 error = fn(child, data);
1189 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return error;
1191}
1192
Cornelia Huck5ab69982006-11-16 15:42:07 +01001193/**
1194 * device_find_child - device iterator for locating a particular device.
1195 * @parent: parent struct device
1196 * @data: Data to pass to match function
1197 * @match: Callback function to check device
1198 *
1199 * This is similar to the device_for_each_child() function above, but it
1200 * returns a reference to a device that is 'found' for later use, as
1201 * determined by the @match callback.
1202 *
1203 * The callback should return 0 if the device doesn't match and non-zero
1204 * if it does. If the callback returns non-zero and a reference to the
1205 * current device can be obtained, this function will return to the caller
1206 * and not iterate over any more devices.
1207 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001208struct device *device_find_child(struct device *parent, void *data,
1209 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001210{
1211 struct klist_iter i;
1212 struct device *child;
1213
1214 if (!parent)
1215 return NULL;
1216
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001217 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001218 while ((child = next_device(&i)))
1219 if (match(child, data) && get_device(child))
1220 break;
1221 klist_iter_exit(&i);
1222 return child;
1223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225int __init devices_init(void)
1226{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001227 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1228 if (!devices_kset)
1229 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001230 dev_kobj = kobject_create_and_add("dev", NULL);
1231 if (!dev_kobj)
1232 goto dev_kobj_err;
1233 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1234 if (!sysfs_dev_block_kobj)
1235 goto block_kobj_err;
1236 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1237 if (!sysfs_dev_char_kobj)
1238 goto char_kobj_err;
1239
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001240 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001241
1242 char_kobj_err:
1243 kobject_put(sysfs_dev_block_kobj);
1244 block_kobj_err:
1245 kobject_put(dev_kobj);
1246 dev_kobj_err:
1247 kset_unregister(devices_kset);
1248 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001252EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254EXPORT_SYMBOL_GPL(device_initialize);
1255EXPORT_SYMBOL_GPL(device_add);
1256EXPORT_SYMBOL_GPL(device_register);
1257
1258EXPORT_SYMBOL_GPL(device_del);
1259EXPORT_SYMBOL_GPL(device_unregister);
1260EXPORT_SYMBOL_GPL(get_device);
1261EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263EXPORT_SYMBOL_GPL(device_create_file);
1264EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001265
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001266struct root_device
1267{
1268 struct device dev;
1269 struct module *owner;
1270};
1271
1272#define to_root_device(dev) container_of(dev, struct root_device, dev)
1273
1274static void root_device_release(struct device *dev)
1275{
1276 kfree(to_root_device(dev));
1277}
1278
1279/**
1280 * __root_device_register - allocate and register a root device
1281 * @name: root device name
1282 * @owner: owner module of the root device, usually THIS_MODULE
1283 *
1284 * This function allocates a root device and registers it
1285 * using device_register(). In order to free the returned
1286 * device, use root_device_unregister().
1287 *
1288 * Root devices are dummy devices which allow other devices
1289 * to be grouped under /sys/devices. Use this function to
1290 * allocate a root device and then use it as the parent of
1291 * any device which should appear under /sys/devices/{name}
1292 *
1293 * The /sys/devices/{name} directory will also contain a
1294 * 'module' symlink which points to the @owner directory
1295 * in sysfs.
1296 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001297 * Returns &struct device pointer on success, or ERR_PTR() on error.
1298 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001299 * Note: You probably want to use root_device_register().
1300 */
1301struct device *__root_device_register(const char *name, struct module *owner)
1302{
1303 struct root_device *root;
1304 int err = -ENOMEM;
1305
1306 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1307 if (!root)
1308 return ERR_PTR(err);
1309
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001310 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001311 if (err) {
1312 kfree(root);
1313 return ERR_PTR(err);
1314 }
1315
1316 root->dev.release = root_device_release;
1317
1318 err = device_register(&root->dev);
1319 if (err) {
1320 put_device(&root->dev);
1321 return ERR_PTR(err);
1322 }
1323
Christoph Egger1d9e8822010-05-17 16:57:58 +02001324#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001325 if (owner) {
1326 struct module_kobject *mk = &owner->mkobj;
1327
1328 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1329 if (err) {
1330 device_unregister(&root->dev);
1331 return ERR_PTR(err);
1332 }
1333 root->owner = owner;
1334 }
1335#endif
1336
1337 return &root->dev;
1338}
1339EXPORT_SYMBOL_GPL(__root_device_register);
1340
1341/**
1342 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001343 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001344 *
1345 * This function unregisters and cleans up a device that was created by
1346 * root_device_register().
1347 */
1348void root_device_unregister(struct device *dev)
1349{
1350 struct root_device *root = to_root_device(dev);
1351
1352 if (root->owner)
1353 sysfs_remove_link(&root->dev.kobj, "module");
1354
1355 device_unregister(dev);
1356}
1357EXPORT_SYMBOL_GPL(root_device_unregister);
1358
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001359
1360static void device_create_release(struct device *dev)
1361{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001362 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001363 kfree(dev);
1364}
1365
1366/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001367 * device_create_vargs - creates a device and registers it with sysfs
1368 * @class: pointer to the struct class that this device should be registered to
1369 * @parent: pointer to the parent struct device of this new device, if any
1370 * @devt: the dev_t for the char device to be added
1371 * @drvdata: the data to be added to the device for callbacks
1372 * @fmt: string for the device's name
1373 * @args: va_list for the device's name
1374 *
1375 * This function can be used by char device classes. A struct device
1376 * will be created in sysfs, registered to the specified class.
1377 *
1378 * A "dev" file will be created, showing the dev_t for the device, if
1379 * the dev_t is not 0,0.
1380 * If a pointer to a parent struct device is passed in, the newly created
1381 * struct device will be a child of that device in sysfs.
1382 * The pointer to the struct device will be returned from the call.
1383 * Any further sysfs files that might be required can be created using this
1384 * pointer.
1385 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001386 * Returns &struct device pointer on success, or ERR_PTR() on error.
1387 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001388 * Note: the struct class passed to this function must have previously
1389 * been created with a call to class_create().
1390 */
1391struct device *device_create_vargs(struct class *class, struct device *parent,
1392 dev_t devt, void *drvdata, const char *fmt,
1393 va_list args)
1394{
1395 struct device *dev = NULL;
1396 int retval = -ENODEV;
1397
1398 if (class == NULL || IS_ERR(class))
1399 goto error;
1400
1401 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1402 if (!dev) {
1403 retval = -ENOMEM;
1404 goto error;
1405 }
1406
1407 dev->devt = devt;
1408 dev->class = class;
1409 dev->parent = parent;
1410 dev->release = device_create_release;
1411 dev_set_drvdata(dev, drvdata);
1412
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001413 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1414 if (retval)
1415 goto error;
1416
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001417 retval = device_register(dev);
1418 if (retval)
1419 goto error;
1420
1421 return dev;
1422
1423error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001424 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001425 return ERR_PTR(retval);
1426}
1427EXPORT_SYMBOL_GPL(device_create_vargs);
1428
1429/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001430 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001431 * @class: pointer to the struct class that this device should be registered to
1432 * @parent: pointer to the parent struct device of this new device, if any
1433 * @devt: the dev_t for the char device to be added
1434 * @drvdata: the data to be added to the device for callbacks
1435 * @fmt: string for the device's name
1436 *
1437 * This function can be used by char device classes. A struct device
1438 * will be created in sysfs, registered to the specified class.
1439 *
1440 * A "dev" file will be created, showing the dev_t for the device, if
1441 * the dev_t is not 0,0.
1442 * If a pointer to a parent struct device is passed in, the newly created
1443 * struct device will be a child of that device in sysfs.
1444 * The pointer to the struct device will be returned from the call.
1445 * Any further sysfs files that might be required can be created using this
1446 * pointer.
1447 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001448 * Returns &struct device pointer on success, or ERR_PTR() on error.
1449 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001450 * Note: the struct class passed to this function must have previously
1451 * been created with a call to class_create().
1452 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001453struct device *device_create(struct class *class, struct device *parent,
1454 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001455{
1456 va_list vargs;
1457 struct device *dev;
1458
1459 va_start(vargs, fmt);
1460 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1461 va_end(vargs);
1462 return dev;
1463}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001464EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001465
Dave Youngcd354492008-01-28 16:56:11 +08001466static int __match_devt(struct device *dev, void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001467{
Dave Youngcd354492008-01-28 16:56:11 +08001468 dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001469
Dave Youngcd354492008-01-28 16:56:11 +08001470 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001471}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001472
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001473/**
1474 * device_destroy - removes a device that was created with device_create()
1475 * @class: pointer to the struct class that this device was registered with
1476 * @devt: the dev_t of the device that was previously registered
1477 *
1478 * This call unregisters and cleans up a device that was created with a
1479 * call to device_create().
1480 */
1481void device_destroy(struct class *class, dev_t devt)
1482{
1483 struct device *dev;
1484
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001485 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001486 if (dev) {
1487 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001488 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001489 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001490}
1491EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001492
1493/**
1494 * device_rename - renames a device
1495 * @dev: the pointer to the struct device to be renamed
1496 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001497 *
1498 * It is the responsibility of the caller to provide mutual
1499 * exclusion between two different calls of device_rename
1500 * on the same device to ensure that new_name is valid and
1501 * won't conflict with other devices.
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001502 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001503int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001504{
1505 char *old_class_name = NULL;
1506 char *new_class_name = NULL;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001507 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001508 int error;
1509
1510 dev = get_device(dev);
1511 if (!dev)
1512 return -EINVAL;
1513
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001514 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001515 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001516
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001517 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001518 if (!old_device_name) {
1519 error = -ENOMEM;
1520 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001521 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001522
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001523 if (dev->class) {
1524 error = sysfs_rename_link(&dev->class->p->class_subsys.kobj,
1525 &dev->kobj, old_device_name, new_name);
1526 if (error)
1527 goto out;
1528 }
Kay Sievers39aba962010-09-04 22:33:14 -07001529
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001530 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001531 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001532 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001533
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001534out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001535 put_device(dev);
1536
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001537 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001538 kfree(old_class_name);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001539 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001540
1541 return error;
1542}
Johannes Berga2807db2007-02-28 12:38:31 +01001543EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001544
1545static int device_move_class_links(struct device *dev,
1546 struct device *old_parent,
1547 struct device *new_parent)
1548{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001549 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001550
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001551 if (old_parent)
1552 sysfs_remove_link(&dev->kobj, "device");
1553 if (new_parent)
1554 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1555 "device");
1556 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001557}
1558
1559/**
1560 * device_move - moves a device to a new parent
1561 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001562 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001563 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001564 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001565int device_move(struct device *dev, struct device *new_parent,
1566 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001567{
1568 int error;
1569 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001570 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001571
1572 dev = get_device(dev);
1573 if (!dev)
1574 return -EINVAL;
1575
Cornelia Huckffa6a702009-03-04 12:44:00 +01001576 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001577 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001578 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001579
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001580 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1581 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001582 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001583 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001584 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001585 put_device(new_parent);
1586 goto out;
1587 }
1588 old_parent = dev->parent;
1589 dev->parent = new_parent;
1590 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001591 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001592 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001593 klist_add_tail(&dev->p->knode_parent,
1594 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001595 set_dev_node(dev, dev_to_node(new_parent));
1596 }
1597
Cornelia Huck8a824722006-11-20 17:07:51 +01001598 if (!dev->class)
1599 goto out_put;
1600 error = device_move_class_links(dev, old_parent, new_parent);
1601 if (error) {
1602 /* We ignore errors on cleanup since we're hosed anyway... */
1603 device_move_class_links(dev, new_parent, old_parent);
1604 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001605 if (new_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001606 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001607 dev->parent = old_parent;
1608 if (old_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001609 klist_add_tail(&dev->p->knode_parent,
1610 &old_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001611 set_dev_node(dev, dev_to_node(old_parent));
1612 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001613 }
Cornelia Huck63b69712008-01-21 16:09:44 +01001614 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001615 put_device(new_parent);
1616 goto out;
1617 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001618 switch (dpm_order) {
1619 case DPM_ORDER_NONE:
1620 break;
1621 case DPM_ORDER_DEV_AFTER_PARENT:
1622 device_pm_move_after(dev, new_parent);
1623 break;
1624 case DPM_ORDER_PARENT_BEFORE_DEV:
1625 device_pm_move_before(new_parent, dev);
1626 break;
1627 case DPM_ORDER_DEV_LAST:
1628 device_pm_move_last(dev);
1629 break;
1630 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001631out_put:
1632 put_device(old_parent);
1633out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001634 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001635 put_device(dev);
1636 return error;
1637}
Cornelia Huck8a824722006-11-20 17:07:51 +01001638EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001639
1640/**
1641 * device_shutdown - call ->shutdown() on each device to shutdown.
1642 */
1643void device_shutdown(void)
1644{
Hugh Daschbach62458382010-03-22 10:36:37 -07001645 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001646
Hugh Daschbach62458382010-03-22 10:36:37 -07001647 spin_lock(&devices_kset->list_lock);
1648 /*
1649 * Walk the devices list backward, shutting down each in turn.
1650 * Beware that device unplug events may also start pulling
1651 * devices offline, even as the system is shutting down.
1652 */
1653 while (!list_empty(&devices_kset->list)) {
1654 dev = list_entry(devices_kset->list.prev, struct device,
1655 kobj.entry);
1656 get_device(dev);
1657 /*
1658 * Make sure the device is off the kset list, in the
1659 * event that dev->*->shutdown() doesn't remove it.
1660 */
1661 list_del_init(&dev->kobj.entry);
1662 spin_unlock(&devices_kset->list_lock);
1663
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001664 if (dev->bus && dev->bus->shutdown) {
1665 dev_dbg(dev, "shutdown\n");
1666 dev->bus->shutdown(dev);
1667 } else if (dev->driver && dev->driver->shutdown) {
1668 dev_dbg(dev, "shutdown\n");
1669 dev->driver->shutdown(dev);
1670 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001671 put_device(dev);
1672
1673 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001674 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001675 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07001676 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001677}
Joe Perches99bcf212010-06-27 01:02:34 +00001678
1679/*
1680 * Device logging functions
1681 */
1682
1683#ifdef CONFIG_PRINTK
1684
1685static int __dev_printk(const char *level, const struct device *dev,
1686 struct va_format *vaf)
1687{
1688 if (!dev)
1689 return printk("%s(NULL device *): %pV", level, vaf);
1690
1691 return printk("%s%s %s: %pV",
1692 level, dev_driver_string(dev), dev_name(dev), vaf);
1693}
1694
1695int dev_printk(const char *level, const struct device *dev,
1696 const char *fmt, ...)
1697{
1698 struct va_format vaf;
1699 va_list args;
1700 int r;
1701
1702 va_start(args, fmt);
1703
1704 vaf.fmt = fmt;
1705 vaf.va = &args;
1706
1707 r = __dev_printk(level, dev, &vaf);
1708 va_end(args);
1709
1710 return r;
1711}
1712EXPORT_SYMBOL(dev_printk);
1713
1714#define define_dev_printk_level(func, kern_level) \
1715int func(const struct device *dev, const char *fmt, ...) \
1716{ \
1717 struct va_format vaf; \
1718 va_list args; \
1719 int r; \
1720 \
1721 va_start(args, fmt); \
1722 \
1723 vaf.fmt = fmt; \
1724 vaf.va = &args; \
1725 \
1726 r = __dev_printk(kern_level, dev, &vaf); \
1727 va_end(args); \
1728 \
1729 return r; \
1730} \
1731EXPORT_SYMBOL(func);
1732
1733define_dev_printk_level(dev_emerg, KERN_EMERG);
1734define_dev_printk_level(dev_alert, KERN_ALERT);
1735define_dev_printk_level(dev_crit, KERN_CRIT);
1736define_dev_printk_level(dev_err, KERN_ERR);
1737define_dev_printk_level(dev_warn, KERN_WARNING);
1738define_dev_printk_level(dev_notice, KERN_NOTICE);
1739define_dev_printk_level(_dev_info, KERN_INFO);
1740
1741#endif