]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/base/core.c
driver core: use initcall_debug to control shutdown info
[mirror_ubuntu-bionic-kernel.git] / drivers / base / core.c
CommitLineData
1da177e4
LT
1/*
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
64bb5d2c
GKH
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
1da177e4
LT
8 *
9 * This file is released under the GPLv2
10 *
11 */
12
1da177e4
LT
13#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>
23681e47 19#include <linux/kdev_t.h>
116af378 20#include <linux/notifier.h>
07d57a32
GL
21#include <linux/of.h>
22#include <linux/of_device.h>
da231fd5 23#include <linux/genhd.h>
815d2d50 24#include <linux/kallsyms.h>
f75b1c60 25#include <linux/mutex.h>
401097ea 26#include <linux/async.h>
af8db150 27#include <linux/pm_runtime.h>
c4e00daa 28#include <linux/netdevice.h>
1da177e4
LT
29
30#include "base.h"
31#include "power/power.h"
32
e52eec13
AK
33#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
39static __init int sysfs_deprecated_setup(char *arg)
40{
41 return strict_strtol(arg, 10, &sysfs_deprecated);
42}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
4a3ad20c
GKH
46int (*platform_notify)(struct device *dev) = NULL;
47int (*platform_notify_remove)(struct device *dev) = NULL;
e105b8bf
DW
48static struct kobject *dev_kobj;
49struct kobject *sysfs_dev_char_kobj;
50struct kobject *sysfs_dev_block_kobj;
1da177e4 51
4e886c29
GKH
52#ifdef CONFIG_BLOCK
53static inline int device_is_not_partition(struct device *dev)
54{
55 return !(dev->type == &part_type);
56}
57#else
58static inline int device_is_not_partition(struct device *dev)
59{
60 return 1;
61}
62#endif
1da177e4 63
3e95637a
AS
64/**
65 * dev_driver_string - Return a device's driver name, if at all possible
66 * @dev: struct device to get the name of
67 *
68 * Will return the device's driver's name if it is bound to a device. If
9169c012 69 * the device is not bound to a driver, it will return the name of the bus
3e95637a
AS
70 * it is attached to. If it is not attached to a bus either, an empty
71 * string will be returned.
72 */
bf9ca69f 73const char *dev_driver_string(const struct device *dev)
3e95637a 74{
3589972e
AS
75 struct device_driver *drv;
76
77 /* dev->driver can change to NULL underneath us because of unbinding,
78 * so be careful about accessing it. dev->bus and dev->class should
79 * never change once they are set, so they don't need special care.
80 */
81 drv = ACCESS_ONCE(dev->driver);
82 return drv ? drv->name :
a456b702
JD
83 (dev->bus ? dev->bus->name :
84 (dev->class ? dev->class->name : ""));
3e95637a 85}
310a922d 86EXPORT_SYMBOL(dev_driver_string);
3e95637a 87
1da177e4
LT
88#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
89
4a3ad20c
GKH
90static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
91 char *buf)
1da177e4 92{
4a3ad20c 93 struct device_attribute *dev_attr = to_dev_attr(attr);
b0d1f807 94 struct device *dev = kobj_to_dev(kobj);
4a0c20bf 95 ssize_t ret = -EIO;
1da177e4
LT
96
97 if (dev_attr->show)
54b6f35c 98 ret = dev_attr->show(dev, dev_attr, buf);
815d2d50
AM
99 if (ret >= (ssize_t)PAGE_SIZE) {
100 print_symbol("dev_attr_show: %s returned bad count\n",
101 (unsigned long)dev_attr->show);
102 }
1da177e4
LT
103 return ret;
104}
105
4a3ad20c
GKH
106static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
107 const char *buf, size_t count)
1da177e4 108{
4a3ad20c 109 struct device_attribute *dev_attr = to_dev_attr(attr);
b0d1f807 110 struct device *dev = kobj_to_dev(kobj);
4a0c20bf 111 ssize_t ret = -EIO;
1da177e4
LT
112
113 if (dev_attr->store)
54b6f35c 114 ret = dev_attr->store(dev, dev_attr, buf, count);
1da177e4
LT
115 return ret;
116}
117
52cf25d0 118static const struct sysfs_ops dev_sysfs_ops = {
1da177e4
LT
119 .show = dev_attr_show,
120 .store = dev_attr_store,
121};
122
ca22e56d
KS
123#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
124
125ssize_t device_store_ulong(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t size)
128{
129 struct dev_ext_attribute *ea = to_ext_attr(attr);
130 char *end;
131 unsigned long new = simple_strtoul(buf, &end, 0);
132 if (end == buf)
133 return -EINVAL;
134 *(unsigned long *)(ea->var) = new;
135 /* Always return full write size even if we didn't consume all */
136 return size;
137}
138EXPORT_SYMBOL_GPL(device_store_ulong);
139
140ssize_t device_show_ulong(struct device *dev,
141 struct device_attribute *attr,
142 char *buf)
143{
144 struct dev_ext_attribute *ea = to_ext_attr(attr);
145 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
146}
147EXPORT_SYMBOL_GPL(device_show_ulong);
148
149ssize_t device_store_int(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t size)
152{
153 struct dev_ext_attribute *ea = to_ext_attr(attr);
154 char *end;
155 long new = simple_strtol(buf, &end, 0);
156 if (end == buf || new > INT_MAX || new < INT_MIN)
157 return -EINVAL;
158 *(int *)(ea->var) = new;
159 /* Always return full write size even if we didn't consume all */
160 return size;
161}
162EXPORT_SYMBOL_GPL(device_store_int);
163
164ssize_t device_show_int(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
168 struct dev_ext_attribute *ea = to_ext_attr(attr);
169
170 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
171}
172EXPORT_SYMBOL_GPL(device_show_int);
1da177e4
LT
173
174/**
175 * device_release - free device structure.
176 * @kobj: device's kobject.
177 *
178 * This is called once the reference count for the object
179 * reaches 0. We forward the call to the device's release
180 * method, which should handle actually freeing the structure.
181 */
4a3ad20c 182static void device_release(struct kobject *kobj)
1da177e4 183{
b0d1f807 184 struct device *dev = kobj_to_dev(kobj);
fb069a5d 185 struct device_private *p = dev->p;
1da177e4 186
a525a3dd
ML
187 /*
188 * Some platform devices are driven without driver attached
189 * and managed resources may have been acquired. Make sure
190 * all resources are released.
191 *
192 * Drivers still can add resources into device after device
193 * is deleted but alive, so release devres here to avoid
194 * possible memory leak.
195 */
196 devres_release_all(dev);
197
1da177e4
LT
198 if (dev->release)
199 dev->release(dev);
f9f852df
KS
200 else if (dev->type && dev->type->release)
201 dev->type->release(dev);
2620efef
GKH
202 else if (dev->class && dev->class->dev_release)
203 dev->class->dev_release(dev);
f810a5cf
AV
204 else
205 WARN(1, KERN_ERR "Device '%s' does not have a release() "
4a3ad20c 206 "function, it is broken and must be fixed.\n",
1e0b2cf9 207 dev_name(dev));
fb069a5d 208 kfree(p);
1da177e4
LT
209}
210
bc451f20
EB
211static const void *device_namespace(struct kobject *kobj)
212{
b0d1f807 213 struct device *dev = kobj_to_dev(kobj);
bc451f20
EB
214 const void *ns = NULL;
215
216 if (dev->class && dev->class->ns_type)
217 ns = dev->class->namespace(dev);
218
219 return ns;
220}
221
8f4afc41 222static struct kobj_type device_ktype = {
1da177e4
LT
223 .release = device_release,
224 .sysfs_ops = &dev_sysfs_ops,
bc451f20 225 .namespace = device_namespace,
1da177e4
LT
226};
227
228
312c004d 229static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1da177e4
LT
230{
231 struct kobj_type *ktype = get_ktype(kobj);
232
8f4afc41 233 if (ktype == &device_ktype) {
b0d1f807 234 struct device *dev = kobj_to_dev(kobj);
1da177e4
LT
235 if (dev->bus)
236 return 1;
23681e47
GKH
237 if (dev->class)
238 return 1;
1da177e4
LT
239 }
240 return 0;
241}
242
312c004d 243static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1da177e4 244{
b0d1f807 245 struct device *dev = kobj_to_dev(kobj);
1da177e4 246
23681e47
GKH
247 if (dev->bus)
248 return dev->bus->name;
249 if (dev->class)
250 return dev->class->name;
251 return NULL;
1da177e4
LT
252}
253
7eff2e7a
KS
254static int dev_uevent(struct kset *kset, struct kobject *kobj,
255 struct kobj_uevent_env *env)
1da177e4 256{
b0d1f807 257 struct device *dev = kobj_to_dev(kobj);
1da177e4
LT
258 int retval = 0;
259
6fcf53ac 260 /* add device node properties if present */
23681e47 261 if (MAJOR(dev->devt)) {
6fcf53ac
KS
262 const char *tmp;
263 const char *name;
2c9ede55 264 umode_t mode = 0;
6fcf53ac 265
7eff2e7a
KS
266 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
267 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
e454cea2 268 name = device_get_devnode(dev, &mode, &tmp);
6fcf53ac
KS
269 if (name) {
270 add_uevent_var(env, "DEVNAME=%s", name);
271 kfree(tmp);
e454cea2
KS
272 if (mode)
273 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
6fcf53ac 274 }
23681e47
GKH
275 }
276
414264f9 277 if (dev->type && dev->type->name)
7eff2e7a 278 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
414264f9 279
239378f1 280 if (dev->driver)
7eff2e7a 281 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
239378f1 282
07d57a32
GL
283 /* Add common DT information about the device */
284 of_device_uevent(dev, env);
285
7eff2e7a 286 /* have the bus specific function add its stuff */
312c004d 287 if (dev->bus && dev->bus->uevent) {
7eff2e7a 288 retval = dev->bus->uevent(dev, env);
f9f852df 289 if (retval)
7dc72b28 290 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1e0b2cf9 291 dev_name(dev), __func__, retval);
1da177e4
LT
292 }
293
7eff2e7a 294 /* have the class specific function add its stuff */
2620efef 295 if (dev->class && dev->class->dev_uevent) {
7eff2e7a 296 retval = dev->class->dev_uevent(dev, env);
f9f852df 297 if (retval)
7dc72b28 298 pr_debug("device: '%s': %s: class uevent() "
1e0b2cf9 299 "returned %d\n", dev_name(dev),
2b3a302a 300 __func__, retval);
f9f852df
KS
301 }
302
eef35c2d 303 /* have the device type specific function add its stuff */
f9f852df 304 if (dev->type && dev->type->uevent) {
7eff2e7a 305 retval = dev->type->uevent(dev, env);
f9f852df 306 if (retval)
7dc72b28 307 pr_debug("device: '%s': %s: dev_type uevent() "
1e0b2cf9 308 "returned %d\n", dev_name(dev),
2b3a302a 309 __func__, retval);
2620efef
GKH
310 }
311
1da177e4
LT
312 return retval;
313}
314
9cd43611 315static const struct kset_uevent_ops device_uevent_ops = {
312c004d
KS
316 .filter = dev_uevent_filter,
317 .name = dev_uevent_name,
318 .uevent = dev_uevent,
1da177e4
LT
319};
320
16574dcc
KS
321static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
322 char *buf)
323{
324 struct kobject *top_kobj;
325 struct kset *kset;
7eff2e7a 326 struct kobj_uevent_env *env = NULL;
16574dcc
KS
327 int i;
328 size_t count = 0;
329 int retval;
330
331 /* search the kset, the device belongs to */
332 top_kobj = &dev->kobj;
5c5daf65
KS
333 while (!top_kobj->kset && top_kobj->parent)
334 top_kobj = top_kobj->parent;
16574dcc
KS
335 if (!top_kobj->kset)
336 goto out;
5c5daf65 337
16574dcc
KS
338 kset = top_kobj->kset;
339 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
340 goto out;
341
342 /* respect filter */
343 if (kset->uevent_ops && kset->uevent_ops->filter)
344 if (!kset->uevent_ops->filter(kset, &dev->kobj))
345 goto out;
346
7eff2e7a
KS
347 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
348 if (!env)
c7308c81
GKH
349 return -ENOMEM;
350
16574dcc 351 /* let the kset specific function add its keys */
7eff2e7a 352 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
16574dcc
KS
353 if (retval)
354 goto out;
355
356 /* copy keys to file */
7eff2e7a
KS
357 for (i = 0; i < env->envp_idx; i++)
358 count += sprintf(&buf[count], "%s\n", env->envp[i]);
16574dcc 359out:
7eff2e7a 360 kfree(env);
16574dcc
KS
361 return count;
362}
363
a7fd6706
KS
364static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
365 const char *buf, size_t count)
366{
60a96a59
KS
367 enum kobject_action action;
368
3f5468c9 369 if (kobject_action_type(buf, count, &action) == 0)
60a96a59 370 kobject_uevent(&dev->kobj, action);
3f5468c9
KS
371 else
372 dev_err(dev, "uevent: unknown action-string\n");
a7fd6706
KS
373 return count;
374}
375
ad6a1e1c
TH
376static struct device_attribute uevent_attr =
377 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
378
621a1672
DT
379static int device_add_attributes(struct device *dev,
380 struct device_attribute *attrs)
de0ff00d 381{
621a1672 382 int error = 0;
de0ff00d 383 int i;
621a1672
DT
384
385 if (attrs) {
386 for (i = 0; attr_name(attrs[i]); i++) {
387 error = device_create_file(dev, &attrs[i]);
388 if (error)
389 break;
390 }
391 if (error)
392 while (--i >= 0)
393 device_remove_file(dev, &attrs[i]);
394 }
395 return error;
396}
397
398static void device_remove_attributes(struct device *dev,
399 struct device_attribute *attrs)
400{
401 int i;
402
403 if (attrs)
404 for (i = 0; attr_name(attrs[i]); i++)
405 device_remove_file(dev, &attrs[i]);
406}
407
c97415a7
SA
408static int device_add_bin_attributes(struct device *dev,
409 struct bin_attribute *attrs)
410{
411 int error = 0;
412 int i;
413
414 if (attrs) {
415 for (i = 0; attr_name(attrs[i]); i++) {
416 error = device_create_bin_file(dev, &attrs[i]);
417 if (error)
418 break;
419 }
420 if (error)
421 while (--i >= 0)
422 device_remove_bin_file(dev, &attrs[i]);
423 }
424 return error;
425}
426
427static void device_remove_bin_attributes(struct device *dev,
428 struct bin_attribute *attrs)
429{
430 int i;
431
432 if (attrs)
433 for (i = 0; attr_name(attrs[i]); i++)
434 device_remove_bin_file(dev, &attrs[i]);
435}
436
621a1672 437static int device_add_groups(struct device *dev,
a4dbd674 438 const struct attribute_group **groups)
621a1672 439{
de0ff00d 440 int error = 0;
621a1672 441 int i;
de0ff00d 442
621a1672
DT
443 if (groups) {
444 for (i = 0; groups[i]; i++) {
445 error = sysfs_create_group(&dev->kobj, groups[i]);
de0ff00d
GKH
446 if (error) {
447 while (--i >= 0)
4a3ad20c
GKH
448 sysfs_remove_group(&dev->kobj,
449 groups[i]);
621a1672 450 break;
de0ff00d
GKH
451 }
452 }
453 }
de0ff00d
GKH
454 return error;
455}
456
621a1672 457static void device_remove_groups(struct device *dev,
a4dbd674 458 const struct attribute_group **groups)
de0ff00d
GKH
459{
460 int i;
621a1672
DT
461
462 if (groups)
463 for (i = 0; groups[i]; i++)
464 sysfs_remove_group(&dev->kobj, groups[i]);
de0ff00d
GKH
465}
466
2620efef
GKH
467static int device_add_attrs(struct device *dev)
468{
469 struct class *class = dev->class;
aed65af1 470 const struct device_type *type = dev->type;
621a1672 471 int error;
2620efef 472
621a1672
DT
473 if (class) {
474 error = device_add_attributes(dev, class->dev_attrs);
f9f852df 475 if (error)
621a1672 476 return error;
c97415a7
SA
477 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
478 if (error)
479 goto err_remove_class_attrs;
2620efef 480 }
f9f852df 481
621a1672
DT
482 if (type) {
483 error = device_add_groups(dev, type->groups);
f9f852df 484 if (error)
c97415a7 485 goto err_remove_class_bin_attrs;
f9f852df
KS
486 }
487
621a1672
DT
488 error = device_add_groups(dev, dev->groups);
489 if (error)
490 goto err_remove_type_groups;
491
492 return 0;
493
494 err_remove_type_groups:
495 if (type)
496 device_remove_groups(dev, type->groups);
c97415a7
SA
497 err_remove_class_bin_attrs:
498 if (class)
499 device_remove_bin_attributes(dev, class->dev_bin_attrs);
621a1672
DT
500 err_remove_class_attrs:
501 if (class)
502 device_remove_attributes(dev, class->dev_attrs);
503
2620efef
GKH
504 return error;
505}
506
507static void device_remove_attrs(struct device *dev)
508{
509 struct class *class = dev->class;
aed65af1 510 const struct device_type *type = dev->type;
2620efef 511
621a1672 512 device_remove_groups(dev, dev->groups);
f9f852df 513
621a1672
DT
514 if (type)
515 device_remove_groups(dev, type->groups);
516
c97415a7 517 if (class) {
621a1672 518 device_remove_attributes(dev, class->dev_attrs);
c97415a7
SA
519 device_remove_bin_attributes(dev, class->dev_bin_attrs);
520 }
2620efef
GKH
521}
522
523
23681e47
GKH
524static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
525 char *buf)
526{
527 return print_dev_t(buf, dev->devt);
528}
529
ad6a1e1c
TH
530static struct device_attribute devt_attr =
531 __ATTR(dev, S_IRUGO, show_dev, NULL);
532
ca22e56d 533/* /sys/devices/ */
881c6cfd 534struct kset *devices_kset;
1da177e4 535
1da177e4 536/**
4a3ad20c
GKH
537 * device_create_file - create sysfs attribute file for device.
538 * @dev: device.
539 * @attr: device attribute descriptor.
1da177e4 540 */
26579ab7
PC
541int device_create_file(struct device *dev,
542 const struct device_attribute *attr)
1da177e4
LT
543{
544 int error = 0;
0c98b19f 545 if (dev)
1da177e4 546 error = sysfs_create_file(&dev->kobj, &attr->attr);
1da177e4
LT
547 return error;
548}
549
550/**
4a3ad20c
GKH
551 * device_remove_file - remove sysfs attribute file.
552 * @dev: device.
553 * @attr: device attribute descriptor.
1da177e4 554 */
26579ab7
PC
555void device_remove_file(struct device *dev,
556 const struct device_attribute *attr)
1da177e4 557{
0c98b19f 558 if (dev)
1da177e4 559 sysfs_remove_file(&dev->kobj, &attr->attr);
1da177e4
LT
560}
561
2589f188
GKH
562/**
563 * device_create_bin_file - create sysfs binary attribute file for device.
564 * @dev: device.
565 * @attr: device binary attribute descriptor.
566 */
66ecb92b
PC
567int device_create_bin_file(struct device *dev,
568 const struct bin_attribute *attr)
2589f188
GKH
569{
570 int error = -EINVAL;
571 if (dev)
572 error = sysfs_create_bin_file(&dev->kobj, attr);
573 return error;
574}
575EXPORT_SYMBOL_GPL(device_create_bin_file);
576
577/**
578 * device_remove_bin_file - remove sysfs binary attribute file
579 * @dev: device.
580 * @attr: device binary attribute descriptor.
581 */
66ecb92b
PC
582void device_remove_bin_file(struct device *dev,
583 const struct bin_attribute *attr)
2589f188
GKH
584{
585 if (dev)
586 sysfs_remove_bin_file(&dev->kobj, attr);
587}
588EXPORT_SYMBOL_GPL(device_remove_bin_file);
589
d9a9cdfb 590/**
523ded71 591 * device_schedule_callback_owner - helper to schedule a callback for a device
d9a9cdfb
AS
592 * @dev: device.
593 * @func: callback function to invoke later.
523ded71 594 * @owner: module owning the callback routine
d9a9cdfb
AS
595 *
596 * Attribute methods must not unregister themselves or their parent device
597 * (which would amount to the same thing). Attempts to do so will deadlock,
598 * since unregistration is mutually exclusive with driver callbacks.
599 *
600 * Instead methods can call this routine, which will attempt to allocate
601 * and schedule a workqueue request to call back @func with @dev as its
602 * argument in the workqueue's process context. @dev will be pinned until
603 * @func returns.
604 *
523ded71
AS
605 * This routine is usually called via the inline device_schedule_callback(),
606 * which automatically sets @owner to THIS_MODULE.
607 *
d9a9cdfb 608 * Returns 0 if the request was submitted, -ENOMEM if storage could not
523ded71 609 * be allocated, -ENODEV if a reference to @owner isn't available.
d9a9cdfb
AS
610 *
611 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
612 * underlying sysfs routine (since it is intended for use by attribute
613 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
614 */
523ded71
AS
615int device_schedule_callback_owner(struct device *dev,
616 void (*func)(struct device *), struct module *owner)
d9a9cdfb
AS
617{
618 return sysfs_schedule_callback(&dev->kobj,
523ded71 619 (void (*)(void *)) func, dev, owner);
d9a9cdfb 620}
523ded71 621EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
d9a9cdfb 622
34bb61f9
JB
623static void klist_children_get(struct klist_node *n)
624{
f791b8c8
GKH
625 struct device_private *p = to_device_private_parent(n);
626 struct device *dev = p->device;
34bb61f9
JB
627
628 get_device(dev);
629}
630
631static void klist_children_put(struct klist_node *n)
632{
f791b8c8
GKH
633 struct device_private *p = to_device_private_parent(n);
634 struct device *dev = p->device;
34bb61f9
JB
635
636 put_device(dev);
637}
638
1da177e4 639/**
4a3ad20c
GKH
640 * device_initialize - init device structure.
641 * @dev: device.
1da177e4 642 *
5739411a
CH
643 * This prepares the device for use by other layers by initializing
644 * its fields.
4a3ad20c 645 * It is the first half of device_register(), if called by
5739411a
CH
646 * that function, though it can also be called separately, so one
647 * may use @dev's fields. In particular, get_device()/put_device()
648 * may be used for reference counting of @dev after calling this
649 * function.
650 *
b10d5efd
AS
651 * All fields in @dev must be initialized by the caller to 0, except
652 * for those explicitly set to some other value. The simplest
653 * approach is to use kzalloc() to allocate the structure containing
654 * @dev.
655 *
5739411a
CH
656 * NOTE: Use put_device() to give up your reference instead of freeing
657 * @dev directly once you have called this function.
1da177e4 658 */
1da177e4
LT
659void device_initialize(struct device *dev)
660{
881c6cfd 661 dev->kobj.kset = devices_kset;
f9cb074b 662 kobject_init(&dev->kobj, &device_ktype);
1da177e4 663 INIT_LIST_HEAD(&dev->dma_pools);
3142788b 664 mutex_init(&dev->mutex);
1704f47b 665 lockdep_set_novalidate_class(&dev->mutex);
9ac7849e
TH
666 spin_lock_init(&dev->devres_lock);
667 INIT_LIST_HEAD(&dev->devres_head);
3b98aeaf 668 device_pm_init(dev);
87348136 669 set_dev_node(dev, -1);
1da177e4
LT
670}
671
86406245 672static struct kobject *virtual_device_parent(struct device *dev)
f0ee61a6 673{
86406245 674 static struct kobject *virtual_dir = NULL;
f0ee61a6 675
86406245 676 if (!virtual_dir)
4ff6abff 677 virtual_dir = kobject_create_and_add("virtual",
881c6cfd 678 &devices_kset->kobj);
f0ee61a6 679
86406245 680 return virtual_dir;
f0ee61a6
GKH
681}
682
bc451f20
EB
683struct class_dir {
684 struct kobject kobj;
685 struct class *class;
686};
687
688#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
689
690static void class_dir_release(struct kobject *kobj)
691{
692 struct class_dir *dir = to_class_dir(kobj);
693 kfree(dir);
694}
695
696static const
697struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
40fa5422 698{
bc451f20
EB
699 struct class_dir *dir = to_class_dir(kobj);
700 return dir->class->ns_type;
701}
702
703static struct kobj_type class_dir_ktype = {
704 .release = class_dir_release,
705 .sysfs_ops = &kobj_sysfs_ops,
706 .child_ns_type = class_dir_child_ns_type
707};
708
709static struct kobject *
710class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
711{
712 struct class_dir *dir;
43968d2f
GKH
713 int retval;
714
bc451f20
EB
715 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
716 if (!dir)
717 return NULL;
718
719 dir->class = class;
720 kobject_init(&dir->kobj, &class_dir_ktype);
721
6b6e39a6 722 dir->kobj.kset = &class->p->glue_dirs;
bc451f20
EB
723
724 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
725 if (retval < 0) {
726 kobject_put(&dir->kobj);
727 return NULL;
728 }
729 return &dir->kobj;
730}
731
732
733static struct kobject *get_device_parent(struct device *dev,
734 struct device *parent)
735{
86406245 736 if (dev->class) {
77d3d7c1 737 static DEFINE_MUTEX(gdp_mutex);
86406245
KS
738 struct kobject *kobj = NULL;
739 struct kobject *parent_kobj;
740 struct kobject *k;
741
ead454fe 742#ifdef CONFIG_BLOCK
39aba963 743 /* block disks show up in /sys/block */
e52eec13 744 if (sysfs_deprecated && dev->class == &block_class) {
39aba963
KS
745 if (parent && parent->class == &block_class)
746 return &parent->kobj;
6b6e39a6 747 return &block_class.p->subsys.kobj;
39aba963 748 }
ead454fe 749#endif
e52eec13 750
86406245
KS
751 /*
752 * If we have no parent, we live in "virtual".
0f4dafc0
KS
753 * Class-devices with a non class-device as parent, live
754 * in a "glue" directory to prevent namespace collisions.
86406245
KS
755 */
756 if (parent == NULL)
757 parent_kobj = virtual_device_parent(dev);
24b1442d 758 else if (parent->class && !dev->class->ns_type)
86406245
KS
759 return &parent->kobj;
760 else
761 parent_kobj = &parent->kobj;
762
77d3d7c1
TH
763 mutex_lock(&gdp_mutex);
764
86406245 765 /* find our class-directory at the parent and reference it */
6b6e39a6
KS
766 spin_lock(&dev->class->p->glue_dirs.list_lock);
767 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
86406245
KS
768 if (k->parent == parent_kobj) {
769 kobj = kobject_get(k);
770 break;
771 }
6b6e39a6 772 spin_unlock(&dev->class->p->glue_dirs.list_lock);
77d3d7c1
TH
773 if (kobj) {
774 mutex_unlock(&gdp_mutex);
86406245 775 return kobj;
77d3d7c1 776 }
86406245
KS
777
778 /* or create a new class-directory at the parent device */
bc451f20 779 k = class_dir_create_and_add(dev->class, parent_kobj);
0f4dafc0 780 /* do not emit an uevent for this simple "glue" directory */
77d3d7c1 781 mutex_unlock(&gdp_mutex);
43968d2f 782 return k;
86406245
KS
783 }
784
ca22e56d
KS
785 /* subsystems can specify a default root directory for their devices */
786 if (!parent && dev->bus && dev->bus->dev_root)
787 return &dev->bus->dev_root->kobj;
788
86406245 789 if (parent)
c744aeae
CH
790 return &parent->kobj;
791 return NULL;
792}
da231fd5 793
63b6971a 794static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
da231fd5 795{
0f4dafc0 796 /* see if we live in a "glue" directory */
c1fe539a 797 if (!glue_dir || !dev->class ||
6b6e39a6 798 glue_dir->kset != &dev->class->p->glue_dirs)
da231fd5
KS
799 return;
800
0f4dafc0 801 kobject_put(glue_dir);
da231fd5 802}
63b6971a
CH
803
804static void cleanup_device_parent(struct device *dev)
805{
806 cleanup_glue_dir(dev, dev->kobj.parent);
807}
86406245 808
2ee97caf
CH
809static int device_add_class_symlinks(struct device *dev)
810{
811 int error;
812
813 if (!dev->class)
814 return 0;
da231fd5 815
1fbfee6c 816 error = sysfs_create_link(&dev->kobj,
6b6e39a6 817 &dev->class->p->subsys.kobj,
2ee97caf
CH
818 "subsystem");
819 if (error)
820 goto out;
da231fd5 821
4e886c29 822 if (dev->parent && device_is_not_partition(dev)) {
39aba963 823 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
4f01a757
DT
824 "device");
825 if (error)
39aba963 826 goto out_subsys;
2ee97caf 827 }
2ee97caf 828
ead454fe 829#ifdef CONFIG_BLOCK
39aba963 830 /* /sys/block has directories and does not need symlinks */
e52eec13 831 if (sysfs_deprecated && dev->class == &block_class)
39aba963 832 return 0;
ead454fe 833#endif
39aba963 834
da231fd5 835 /* link in the class directory pointing to the device */
6b6e39a6 836 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1e0b2cf9 837 &dev->kobj, dev_name(dev));
da231fd5 838 if (error)
39aba963 839 goto out_device;
da231fd5 840
da231fd5
KS
841 return 0;
842
39aba963
KS
843out_device:
844 sysfs_remove_link(&dev->kobj, "device");
da231fd5 845
2ee97caf
CH
846out_subsys:
847 sysfs_remove_link(&dev->kobj, "subsystem");
848out:
849 return error;
850}
851
852static void device_remove_class_symlinks(struct device *dev)
853{
854 if (!dev->class)
855 return;
da231fd5 856
4e886c29 857 if (dev->parent && device_is_not_partition(dev))
da231fd5 858 sysfs_remove_link(&dev->kobj, "device");
2ee97caf 859 sysfs_remove_link(&dev->kobj, "subsystem");
ead454fe 860#ifdef CONFIG_BLOCK
e52eec13 861 if (sysfs_deprecated && dev->class == &block_class)
39aba963 862 return;
ead454fe 863#endif
6b6e39a6 864 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2ee97caf
CH
865}
866
413c239f
SR
867/**
868 * dev_set_name - set a device name
869 * @dev: device
46232366 870 * @fmt: format string for the device's name
413c239f
SR
871 */
872int dev_set_name(struct device *dev, const char *fmt, ...)
873{
874 va_list vargs;
1fa5ae85 875 int err;
413c239f
SR
876
877 va_start(vargs, fmt);
1fa5ae85 878 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
413c239f 879 va_end(vargs);
1fa5ae85 880 return err;
413c239f
SR
881}
882EXPORT_SYMBOL_GPL(dev_set_name);
883
e105b8bf
DW
884/**
885 * device_to_dev_kobj - select a /sys/dev/ directory for the device
886 * @dev: device
887 *
888 * By default we select char/ for new entries. Setting class->dev_obj
889 * to NULL prevents an entry from being created. class->dev_kobj must
890 * be set (or cleared) before any devices are registered to the class
891 * otherwise device_create_sys_dev_entry() and
0d4e293c
PK
892 * device_remove_sys_dev_entry() will disagree about the presence of
893 * the link.
e105b8bf
DW
894 */
895static struct kobject *device_to_dev_kobj(struct device *dev)
896{
897 struct kobject *kobj;
898
899 if (dev->class)
900 kobj = dev->class->dev_kobj;
901 else
902 kobj = sysfs_dev_char_kobj;
903
904 return kobj;
905}
906
907static int device_create_sys_dev_entry(struct device *dev)
908{
909 struct kobject *kobj = device_to_dev_kobj(dev);
910 int error = 0;
911 char devt_str[15];
912
913 if (kobj) {
914 format_dev_t(devt_str, dev->devt);
915 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
916 }
917
918 return error;
919}
920
921static void device_remove_sys_dev_entry(struct device *dev)
922{
923 struct kobject *kobj = device_to_dev_kobj(dev);
924 char devt_str[15];
925
926 if (kobj) {
927 format_dev_t(devt_str, dev->devt);
928 sysfs_remove_link(kobj, devt_str);
929 }
930}
931
b4028437
GKH
932int device_private_init(struct device *dev)
933{
934 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
935 if (!dev->p)
936 return -ENOMEM;
937 dev->p->device = dev;
938 klist_init(&dev->p->klist_children, klist_children_get,
939 klist_children_put);
ef8a3fd6 940 INIT_LIST_HEAD(&dev->p->deferred_probe);
b4028437
GKH
941 return 0;
942}
943
1da177e4 944/**
4a3ad20c
GKH
945 * device_add - add device to device hierarchy.
946 * @dev: device.
1da177e4 947 *
4a3ad20c
GKH
948 * This is part 2 of device_register(), though may be called
949 * separately _iff_ device_initialize() has been called separately.
1da177e4 950 *
5739411a 951 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
4a3ad20c
GKH
952 * to the global and sibling lists for the device, then
953 * adds it to the other relevant subsystems of the driver model.
5739411a 954 *
b10d5efd
AS
955 * Do not call this routine or device_register() more than once for
956 * any device structure. The driver model core is not designed to work
957 * with devices that get unregistered and then spring back to life.
958 * (Among other things, it's very hard to guarantee that all references
959 * to the previous incarnation of @dev have been dropped.) Allocate
960 * and register a fresh new struct device instead.
961 *
5739411a
CH
962 * NOTE: _Never_ directly free @dev after calling this function, even
963 * if it returned an error! Always use put_device() to give up your
964 * reference instead.
1da177e4
LT
965 */
966int device_add(struct device *dev)
967{
968 struct device *parent = NULL;
ca22e56d 969 struct kobject *kobj;
c47ed219 970 struct class_interface *class_intf;
c906a48a 971 int error = -EINVAL;
775b64d2 972
1da177e4 973 dev = get_device(dev);
c906a48a
GKH
974 if (!dev)
975 goto done;
976
fb069a5d 977 if (!dev->p) {
b4028437
GKH
978 error = device_private_init(dev);
979 if (error)
980 goto done;
fb069a5d 981 }
fb069a5d 982
1fa5ae85
KS
983 /*
984 * for statically allocated devices, which should all be converted
985 * some day, we need to initialize the name. We prevent reading back
986 * the name, and force the use of dev_name()
987 */
988 if (dev->init_name) {
acc0e90f 989 dev_set_name(dev, "%s", dev->init_name);
1fa5ae85
KS
990 dev->init_name = NULL;
991 }
c906a48a 992
ca22e56d
KS
993 /* subsystems can specify simple device enumeration */
994 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
995 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
996
e6309e75
TG
997 if (!dev_name(dev)) {
998 error = -EINVAL;
5c8563d7 999 goto name_error;
e6309e75 1000 }
1da177e4 1001
1e0b2cf9 1002 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
c205ef48 1003
1da177e4 1004 parent = get_device(dev->parent);
ca22e56d
KS
1005 kobj = get_device_parent(dev, parent);
1006 if (kobj)
1007 dev->kobj.parent = kobj;
1da177e4 1008
0d358f22
YL
1009 /* use parent numa_node */
1010 if (parent)
1011 set_dev_node(dev, dev_to_node(parent));
1012
1da177e4 1013 /* first, register with generic layer. */
8a577ffc
KS
1014 /* we require the name to be set before, and pass NULL */
1015 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
40fa5422 1016 if (error)
1da177e4 1017 goto Error;
a7fd6706 1018
37022644
BW
1019 /* notify platform of device entry */
1020 if (platform_notify)
1021 platform_notify(dev);
1022
ad6a1e1c 1023 error = device_create_file(dev, &uevent_attr);
a306eea4
CH
1024 if (error)
1025 goto attrError;
a7fd6706 1026
23681e47 1027 if (MAJOR(dev->devt)) {
ad6a1e1c
TH
1028 error = device_create_file(dev, &devt_attr);
1029 if (error)
a306eea4 1030 goto ueventattrError;
e105b8bf
DW
1031
1032 error = device_create_sys_dev_entry(dev);
1033 if (error)
1034 goto devtattrError;
2b2af54a
KS
1035
1036 devtmpfs_create_node(dev);
23681e47
GKH
1037 }
1038
2ee97caf
CH
1039 error = device_add_class_symlinks(dev);
1040 if (error)
1041 goto SymlinkError;
dc0afa83
CH
1042 error = device_add_attrs(dev);
1043 if (error)
2620efef 1044 goto AttrsError;
dc0afa83
CH
1045 error = bus_add_device(dev);
1046 if (error)
1da177e4 1047 goto BusError;
3b98aeaf 1048 error = dpm_sysfs_add(dev);
57eee3d2 1049 if (error)
3b98aeaf
AS
1050 goto DPMError;
1051 device_pm_add(dev);
ec0676ee
AS
1052
1053 /* Notify clients of device addition. This call must come
268863f4 1054 * after dpm_sysfs_add() and before kobject_uevent().
ec0676ee
AS
1055 */
1056 if (dev->bus)
1057 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1058 BUS_NOTIFY_ADD_DEVICE, dev);
1059
83b5fb4c 1060 kobject_uevent(&dev->kobj, KOBJ_ADD);
2023c610 1061 bus_probe_device(dev);
1da177e4 1062 if (parent)
f791b8c8
GKH
1063 klist_add_tail(&dev->p->knode_parent,
1064 &parent->p->klist_children);
1da177e4 1065
5d9fd169 1066 if (dev->class) {
ca22e56d 1067 mutex_lock(&dev->class->p->mutex);
c47ed219 1068 /* tie the class to the device */
5a3ceb86 1069 klist_add_tail(&dev->knode_class,
6b6e39a6 1070 &dev->class->p->klist_devices);
c47ed219
GKH
1071
1072 /* notify any interfaces that the device is here */
184f1f77 1073 list_for_each_entry(class_intf,
ca22e56d 1074 &dev->class->p->interfaces, node)
c47ed219
GKH
1075 if (class_intf->add_dev)
1076 class_intf->add_dev(dev, class_intf);
ca22e56d 1077 mutex_unlock(&dev->class->p->mutex);
5d9fd169 1078 }
c906a48a 1079done:
1da177e4
LT
1080 put_device(dev);
1081 return error;
3b98aeaf 1082 DPMError:
57eee3d2
RW
1083 bus_remove_device(dev);
1084 BusError:
82f0cf9b 1085 device_remove_attrs(dev);
2620efef 1086 AttrsError:
2ee97caf
CH
1087 device_remove_class_symlinks(dev);
1088 SymlinkError:
ad72956d
KS
1089 if (MAJOR(dev->devt))
1090 devtmpfs_delete_node(dev);
e105b8bf
DW
1091 if (MAJOR(dev->devt))
1092 device_remove_sys_dev_entry(dev);
1093 devtattrError:
ad6a1e1c
TH
1094 if (MAJOR(dev->devt))
1095 device_remove_file(dev, &devt_attr);
a306eea4 1096 ueventattrError:
ad6a1e1c 1097 device_remove_file(dev, &uevent_attr);
23681e47 1098 attrError:
312c004d 1099 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1da177e4
LT
1100 kobject_del(&dev->kobj);
1101 Error:
63b6971a 1102 cleanup_device_parent(dev);
1da177e4
LT
1103 if (parent)
1104 put_device(parent);
5c8563d7
KS
1105name_error:
1106 kfree(dev->p);
1107 dev->p = NULL;
c906a48a 1108 goto done;
1da177e4
LT
1109}
1110
1da177e4 1111/**
4a3ad20c
GKH
1112 * device_register - register a device with the system.
1113 * @dev: pointer to the device structure
1da177e4 1114 *
4a3ad20c
GKH
1115 * This happens in two clean steps - initialize the device
1116 * and add it to the system. The two steps can be called
1117 * separately, but this is the easiest and most common.
1118 * I.e. you should only call the two helpers separately if
1119 * have a clearly defined need to use and refcount the device
1120 * before it is added to the hierarchy.
5739411a 1121 *
b10d5efd
AS
1122 * For more information, see the kerneldoc for device_initialize()
1123 * and device_add().
1124 *
5739411a
CH
1125 * NOTE: _Never_ directly free @dev after calling this function, even
1126 * if it returned an error! Always use put_device() to give up the
1127 * reference initialized in this function instead.
1da177e4 1128 */
1da177e4
LT
1129int device_register(struct device *dev)
1130{
1131 device_initialize(dev);
1132 return device_add(dev);
1133}
1134
1da177e4 1135/**
4a3ad20c
GKH
1136 * get_device - increment reference count for device.
1137 * @dev: device.
1da177e4 1138 *
4a3ad20c
GKH
1139 * This simply forwards the call to kobject_get(), though
1140 * we do take care to provide for the case that we get a NULL
1141 * pointer passed in.
1da177e4 1142 */
4a3ad20c 1143struct device *get_device(struct device *dev)
1da177e4 1144{
b0d1f807 1145 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1da177e4
LT
1146}
1147
1da177e4 1148/**
4a3ad20c
GKH
1149 * put_device - decrement reference count.
1150 * @dev: device in question.
1da177e4 1151 */
4a3ad20c 1152void put_device(struct device *dev)
1da177e4 1153{
edfaa7c3 1154 /* might_sleep(); */
1da177e4
LT
1155 if (dev)
1156 kobject_put(&dev->kobj);
1157}
1158
1da177e4 1159/**
4a3ad20c
GKH
1160 * device_del - delete device from system.
1161 * @dev: device.
1da177e4 1162 *
4a3ad20c
GKH
1163 * This is the first part of the device unregistration
1164 * sequence. This removes the device from the lists we control
1165 * from here, has it removed from the other driver model
1166 * subsystems it was added to in device_add(), and removes it
1167 * from the kobject hierarchy.
1da177e4 1168 *
4a3ad20c
GKH
1169 * NOTE: this should be called manually _iff_ device_add() was
1170 * also called manually.
1da177e4 1171 */
4a3ad20c 1172void device_del(struct device *dev)
1da177e4 1173{
4a3ad20c 1174 struct device *parent = dev->parent;
c47ed219 1175 struct class_interface *class_intf;
1da177e4 1176
ec0676ee
AS
1177 /* Notify clients of device removal. This call must come
1178 * before dpm_sysfs_remove().
1179 */
1180 if (dev->bus)
1181 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1182 BUS_NOTIFY_DEL_DEVICE, dev);
775b64d2 1183 device_pm_remove(dev);
3b98aeaf 1184 dpm_sysfs_remove(dev);
1da177e4 1185 if (parent)
f791b8c8 1186 klist_del(&dev->p->knode_parent);
e105b8bf 1187 if (MAJOR(dev->devt)) {
2b2af54a 1188 devtmpfs_delete_node(dev);
e105b8bf 1189 device_remove_sys_dev_entry(dev);
ad6a1e1c 1190 device_remove_file(dev, &devt_attr);
e105b8bf 1191 }
b9d9c82b 1192 if (dev->class) {
da231fd5 1193 device_remove_class_symlinks(dev);
99ef3ef8 1194
ca22e56d 1195 mutex_lock(&dev->class->p->mutex);
c47ed219 1196 /* notify any interfaces that the device is now gone */
184f1f77 1197 list_for_each_entry(class_intf,
ca22e56d 1198 &dev->class->p->interfaces, node)
c47ed219
GKH
1199 if (class_intf->remove_dev)
1200 class_intf->remove_dev(dev, class_intf);
1201 /* remove the device from the class list */
5a3ceb86 1202 klist_del(&dev->knode_class);
ca22e56d 1203 mutex_unlock(&dev->class->p->mutex);
b9d9c82b 1204 }
ad6a1e1c 1205 device_remove_file(dev, &uevent_attr);
2620efef 1206 device_remove_attrs(dev);
28953533 1207 bus_remove_device(dev);
d1c3414c 1208 driver_deferred_probe_del(dev);
1da177e4
LT
1209
1210 /* Notify the platform of the removal, in case they
1211 * need to do anything...
1212 */
1213 if (platform_notify_remove)
1214 platform_notify_remove(dev);
312c004d 1215 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
da231fd5 1216 cleanup_device_parent(dev);
1da177e4 1217 kobject_del(&dev->kobj);
da231fd5 1218 put_device(parent);
1da177e4
LT
1219}
1220
1221/**
4a3ad20c
GKH
1222 * device_unregister - unregister device from system.
1223 * @dev: device going away.
1da177e4 1224 *
4a3ad20c
GKH
1225 * We do this in two parts, like we do device_register(). First,
1226 * we remove it from all the subsystems with device_del(), then
1227 * we decrement the reference count via put_device(). If that
1228 * is the final reference count, the device will be cleaned up
1229 * via device_release() above. Otherwise, the structure will
1230 * stick around until the final reference to the device is dropped.
1da177e4 1231 */
4a3ad20c 1232void device_unregister(struct device *dev)
1da177e4 1233{
1e0b2cf9 1234 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1da177e4
LT
1235 device_del(dev);
1236 put_device(dev);
1237}
1238
4a3ad20c 1239static struct device *next_device(struct klist_iter *i)
36239577 1240{
4a3ad20c 1241 struct klist_node *n = klist_next(i);
f791b8c8
GKH
1242 struct device *dev = NULL;
1243 struct device_private *p;
1244
1245 if (n) {
1246 p = to_device_private_parent(n);
1247 dev = p->device;
1248 }
1249 return dev;
36239577
PM
1250}
1251
6fcf53ac 1252/**
e454cea2 1253 * device_get_devnode - path of device node file
6fcf53ac 1254 * @dev: device
e454cea2 1255 * @mode: returned file access mode
6fcf53ac
KS
1256 * @tmp: possibly allocated string
1257 *
1258 * Return the relative path of a possible device node.
1259 * Non-default names may need to allocate a memory to compose
1260 * a name. This memory is returned in tmp and needs to be
1261 * freed by the caller.
1262 */
e454cea2 1263const char *device_get_devnode(struct device *dev,
2c9ede55 1264 umode_t *mode, const char **tmp)
6fcf53ac
KS
1265{
1266 char *s;
1267
1268 *tmp = NULL;
1269
1270 /* the device type may provide a specific name */
e454cea2
KS
1271 if (dev->type && dev->type->devnode)
1272 *tmp = dev->type->devnode(dev, mode);
6fcf53ac
KS
1273 if (*tmp)
1274 return *tmp;
1275
1276 /* the class may provide a specific name */
e454cea2
KS
1277 if (dev->class && dev->class->devnode)
1278 *tmp = dev->class->devnode(dev, mode);
6fcf53ac
KS
1279 if (*tmp)
1280 return *tmp;
1281
1282 /* return name without allocation, tmp == NULL */
1283 if (strchr(dev_name(dev), '!') == NULL)
1284 return dev_name(dev);
1285
1286 /* replace '!' in the name with '/' */
1287 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1288 if (!*tmp)
1289 return NULL;
1290 while ((s = strchr(*tmp, '!')))
1291 s[0] = '/';
1292 return *tmp;
1293}
1294
1da177e4 1295/**
4a3ad20c
GKH
1296 * device_for_each_child - device child iterator.
1297 * @parent: parent struct device.
1298 * @data: data for the callback.
1299 * @fn: function to be called for each device.
1da177e4 1300 *
4a3ad20c
GKH
1301 * Iterate over @parent's child devices, and call @fn for each,
1302 * passing it @data.
1da177e4 1303 *
4a3ad20c
GKH
1304 * We check the return of @fn each time. If it returns anything
1305 * other than 0, we break out and return that value.
1da177e4 1306 */
4a3ad20c
GKH
1307int device_for_each_child(struct device *parent, void *data,
1308 int (*fn)(struct device *dev, void *data))
1da177e4 1309{
36239577 1310 struct klist_iter i;
4a3ad20c 1311 struct device *child;
1da177e4
LT
1312 int error = 0;
1313
014c90db
GKH
1314 if (!parent->p)
1315 return 0;
1316
f791b8c8 1317 klist_iter_init(&parent->p->klist_children, &i);
36239577
PM
1318 while ((child = next_device(&i)) && !error)
1319 error = fn(child, data);
1320 klist_iter_exit(&i);
1da177e4
LT
1321 return error;
1322}
1323
5ab69981
CH
1324/**
1325 * device_find_child - device iterator for locating a particular device.
1326 * @parent: parent struct device
1327 * @data: Data to pass to match function
1328 * @match: Callback function to check device
1329 *
1330 * This is similar to the device_for_each_child() function above, but it
1331 * returns a reference to a device that is 'found' for later use, as
1332 * determined by the @match callback.
1333 *
1334 * The callback should return 0 if the device doesn't match and non-zero
1335 * if it does. If the callback returns non-zero and a reference to the
1336 * current device can be obtained, this function will return to the caller
1337 * and not iterate over any more devices.
1338 */
4a3ad20c
GKH
1339struct device *device_find_child(struct device *parent, void *data,
1340 int (*match)(struct device *dev, void *data))
5ab69981
CH
1341{
1342 struct klist_iter i;
1343 struct device *child;
1344
1345 if (!parent)
1346 return NULL;
1347
f791b8c8 1348 klist_iter_init(&parent->p->klist_children, &i);
5ab69981
CH
1349 while ((child = next_device(&i)))
1350 if (match(child, data) && get_device(child))
1351 break;
1352 klist_iter_exit(&i);
1353 return child;
1354}
1355
1da177e4
LT
1356int __init devices_init(void)
1357{
881c6cfd
GKH
1358 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1359 if (!devices_kset)
1360 return -ENOMEM;
e105b8bf
DW
1361 dev_kobj = kobject_create_and_add("dev", NULL);
1362 if (!dev_kobj)
1363 goto dev_kobj_err;
1364 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1365 if (!sysfs_dev_block_kobj)
1366 goto block_kobj_err;
1367 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1368 if (!sysfs_dev_char_kobj)
1369 goto char_kobj_err;
1370
881c6cfd 1371 return 0;
e105b8bf
DW
1372
1373 char_kobj_err:
1374 kobject_put(sysfs_dev_block_kobj);
1375 block_kobj_err:
1376 kobject_put(dev_kobj);
1377 dev_kobj_err:
1378 kset_unregister(devices_kset);
1379 return -ENOMEM;
1da177e4
LT
1380}
1381
1382EXPORT_SYMBOL_GPL(device_for_each_child);
5ab69981 1383EXPORT_SYMBOL_GPL(device_find_child);
1da177e4
LT
1384
1385EXPORT_SYMBOL_GPL(device_initialize);
1386EXPORT_SYMBOL_GPL(device_add);
1387EXPORT_SYMBOL_GPL(device_register);
1388
1389EXPORT_SYMBOL_GPL(device_del);
1390EXPORT_SYMBOL_GPL(device_unregister);
1391EXPORT_SYMBOL_GPL(get_device);
1392EXPORT_SYMBOL_GPL(put_device);
1da177e4
LT
1393
1394EXPORT_SYMBOL_GPL(device_create_file);
1395EXPORT_SYMBOL_GPL(device_remove_file);
23681e47 1396
7f100d15 1397struct root_device {
0aa0dc41
MM
1398 struct device dev;
1399 struct module *owner;
1400};
1401
481e2079
FW
1402inline struct root_device *to_root_device(struct device *d)
1403{
1404 return container_of(d, struct root_device, dev);
1405}
0aa0dc41
MM
1406
1407static void root_device_release(struct device *dev)
1408{
1409 kfree(to_root_device(dev));
1410}
1411
1412/**
1413 * __root_device_register - allocate and register a root device
1414 * @name: root device name
1415 * @owner: owner module of the root device, usually THIS_MODULE
1416 *
1417 * This function allocates a root device and registers it
1418 * using device_register(). In order to free the returned
1419 * device, use root_device_unregister().
1420 *
1421 * Root devices are dummy devices which allow other devices
1422 * to be grouped under /sys/devices. Use this function to
1423 * allocate a root device and then use it as the parent of
1424 * any device which should appear under /sys/devices/{name}
1425 *
1426 * The /sys/devices/{name} directory will also contain a
1427 * 'module' symlink which points to the @owner directory
1428 * in sysfs.
1429 *
f0eae0ed
JN
1430 * Returns &struct device pointer on success, or ERR_PTR() on error.
1431 *
0aa0dc41
MM
1432 * Note: You probably want to use root_device_register().
1433 */
1434struct device *__root_device_register(const char *name, struct module *owner)
1435{
1436 struct root_device *root;
1437 int err = -ENOMEM;
1438
1439 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1440 if (!root)
1441 return ERR_PTR(err);
1442
acc0e90f 1443 err = dev_set_name(&root->dev, "%s", name);
0aa0dc41
MM
1444 if (err) {
1445 kfree(root);
1446 return ERR_PTR(err);
1447 }
1448
1449 root->dev.release = root_device_release;
1450
1451 err = device_register(&root->dev);
1452 if (err) {
1453 put_device(&root->dev);
1454 return ERR_PTR(err);
1455 }
1456
1d9e882b 1457#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
0aa0dc41
MM
1458 if (owner) {
1459 struct module_kobject *mk = &owner->mkobj;
1460
1461 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1462 if (err) {
1463 device_unregister(&root->dev);
1464 return ERR_PTR(err);
1465 }
1466 root->owner = owner;
1467 }
1468#endif
1469
1470 return &root->dev;
1471}
1472EXPORT_SYMBOL_GPL(__root_device_register);
1473
1474/**
1475 * root_device_unregister - unregister and free a root device
7cbcf225 1476 * @dev: device going away
0aa0dc41
MM
1477 *
1478 * This function unregisters and cleans up a device that was created by
1479 * root_device_register().
1480 */
1481void root_device_unregister(struct device *dev)
1482{
1483 struct root_device *root = to_root_device(dev);
1484
1485 if (root->owner)
1486 sysfs_remove_link(&root->dev.kobj, "module");
1487
1488 device_unregister(dev);
1489}
1490EXPORT_SYMBOL_GPL(root_device_unregister);
1491
23681e47
GKH
1492
1493static void device_create_release(struct device *dev)
1494{
1e0b2cf9 1495 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
23681e47
GKH
1496 kfree(dev);
1497}
1498
1499/**
8882b394 1500 * device_create_vargs - creates a device and registers it with sysfs
42734daf
HK
1501 * @class: pointer to the struct class that this device should be registered to
1502 * @parent: pointer to the parent struct device of this new device, if any
1503 * @devt: the dev_t for the char device to be added
8882b394 1504 * @drvdata: the data to be added to the device for callbacks
42734daf 1505 * @fmt: string for the device's name
8882b394 1506 * @args: va_list for the device's name
42734daf
HK
1507 *
1508 * This function can be used by char device classes. A struct device
1509 * will be created in sysfs, registered to the specified class.
23681e47 1510 *
23681e47
GKH
1511 * A "dev" file will be created, showing the dev_t for the device, if
1512 * the dev_t is not 0,0.
42734daf
HK
1513 * If a pointer to a parent struct device is passed in, the newly created
1514 * struct device will be a child of that device in sysfs.
1515 * The pointer to the struct device will be returned from the call.
1516 * Any further sysfs files that might be required can be created using this
23681e47
GKH
1517 * pointer.
1518 *
f0eae0ed
JN
1519 * Returns &struct device pointer on success, or ERR_PTR() on error.
1520 *
23681e47
GKH
1521 * Note: the struct class passed to this function must have previously
1522 * been created with a call to class_create().
1523 */
8882b394
GKH
1524struct device *device_create_vargs(struct class *class, struct device *parent,
1525 dev_t devt, void *drvdata, const char *fmt,
1526 va_list args)
23681e47 1527{
23681e47
GKH
1528 struct device *dev = NULL;
1529 int retval = -ENODEV;
1530
1531 if (class == NULL || IS_ERR(class))
1532 goto error;
23681e47
GKH
1533
1534 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1535 if (!dev) {
1536 retval = -ENOMEM;
1537 goto error;
1538 }
1539
1540 dev->devt = devt;
1541 dev->class = class;
1542 dev->parent = parent;
1543 dev->release = device_create_release;
8882b394 1544 dev_set_drvdata(dev, drvdata);
23681e47 1545
1fa5ae85
KS
1546 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1547 if (retval)
1548 goto error;
1549
23681e47
GKH
1550 retval = device_register(dev);
1551 if (retval)
1552 goto error;
1553
23681e47
GKH
1554 return dev;
1555
1556error:
286661b3 1557 put_device(dev);
23681e47
GKH
1558 return ERR_PTR(retval);
1559}
8882b394
GKH
1560EXPORT_SYMBOL_GPL(device_create_vargs);
1561
1562/**
4e106739 1563 * device_create - creates a device and registers it with sysfs
8882b394
GKH
1564 * @class: pointer to the struct class that this device should be registered to
1565 * @parent: pointer to the parent struct device of this new device, if any
1566 * @devt: the dev_t for the char device to be added
1567 * @drvdata: the data to be added to the device for callbacks
1568 * @fmt: string for the device's name
1569 *
1570 * This function can be used by char device classes. A struct device
1571 * will be created in sysfs, registered to the specified class.
1572 *
1573 * A "dev" file will be created, showing the dev_t for the device, if
1574 * the dev_t is not 0,0.
1575 * If a pointer to a parent struct device is passed in, the newly created
1576 * struct device will be a child of that device in sysfs.
1577 * The pointer to the struct device will be returned from the call.
1578 * Any further sysfs files that might be required can be created using this
1579 * pointer.
1580 *
f0eae0ed
JN
1581 * Returns &struct device pointer on success, or ERR_PTR() on error.
1582 *
8882b394
GKH
1583 * Note: the struct class passed to this function must have previously
1584 * been created with a call to class_create().
1585 */
4e106739
GKH
1586struct device *device_create(struct class *class, struct device *parent,
1587 dev_t devt, void *drvdata, const char *fmt, ...)
8882b394
GKH
1588{
1589 va_list vargs;
1590 struct device *dev;
1591
1592 va_start(vargs, fmt);
1593 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1594 va_end(vargs);
1595 return dev;
1596}
4e106739 1597EXPORT_SYMBOL_GPL(device_create);
8882b394 1598
cd35449b 1599static int __match_devt(struct device *dev, void *data)
23681e47 1600{
cd35449b 1601 dev_t *devt = data;
23681e47 1602
cd35449b 1603 return dev->devt == *devt;
775b64d2
RW
1604}
1605
1606/**
1607 * device_destroy - removes a device that was created with device_create()
1608 * @class: pointer to the struct class that this device was registered with
1609 * @devt: the dev_t of the device that was previously registered
1610 *
1611 * This call unregisters and cleans up a device that was created with a
1612 * call to device_create().
1613 */
1614void device_destroy(struct class *class, dev_t devt)
1615{
1616 struct device *dev;
23681e47 1617
695794ae 1618 dev = class_find_device(class, NULL, &devt, __match_devt);
cd35449b
DY
1619 if (dev) {
1620 put_device(dev);
23681e47 1621 device_unregister(dev);
cd35449b 1622 }
23681e47
GKH
1623}
1624EXPORT_SYMBOL_GPL(device_destroy);
a2de48ca
GKH
1625
1626/**
1627 * device_rename - renames a device
1628 * @dev: the pointer to the struct device to be renamed
1629 * @new_name: the new name of the device
030c1d2b
EB
1630 *
1631 * It is the responsibility of the caller to provide mutual
1632 * exclusion between two different calls of device_rename
1633 * on the same device to ensure that new_name is valid and
1634 * won't conflict with other devices.
c6c0ac66 1635 *
a5462516
TT
1636 * Note: Don't call this function. Currently, the networking layer calls this
1637 * function, but that will change. The following text from Kay Sievers offers
1638 * some insight:
1639 *
1640 * Renaming devices is racy at many levels, symlinks and other stuff are not
1641 * replaced atomically, and you get a "move" uevent, but it's not easy to
1642 * connect the event to the old and new device. Device nodes are not renamed at
1643 * all, there isn't even support for that in the kernel now.
1644 *
1645 * In the meantime, during renaming, your target name might be taken by another
1646 * driver, creating conflicts. Or the old name is taken directly after you
1647 * renamed it -- then you get events for the same DEVPATH, before you even see
1648 * the "move" event. It's just a mess, and nothing new should ever rely on
1649 * kernel device renaming. Besides that, it's not even implemented now for
1650 * other things than (driver-core wise very simple) network devices.
1651 *
1652 * We are currently about to change network renaming in udev to completely
1653 * disallow renaming of devices in the same namespace as the kernel uses,
1654 * because we can't solve the problems properly, that arise with swapping names
1655 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1656 * be allowed to some other name than eth[0-9]*, for the aforementioned
1657 * reasons.
1658 *
1659 * Make up a "real" name in the driver before you register anything, or add
1660 * some other attributes for userspace to find the device, or use udev to add
1661 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1662 * don't even want to get into that and try to implement the missing pieces in
1663 * the core. We really have other pieces to fix in the driver core mess. :)
a2de48ca 1664 */
6937e8f8 1665int device_rename(struct device *dev, const char *new_name)
a2de48ca
GKH
1666{
1667 char *old_class_name = NULL;
1668 char *new_class_name = NULL;
2ee97caf 1669 char *old_device_name = NULL;
a2de48ca
GKH
1670 int error;
1671
1672 dev = get_device(dev);
1673 if (!dev)
1674 return -EINVAL;
1675
1e0b2cf9 1676 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
2b3a302a 1677 __func__, new_name);
a2de48ca 1678
1fa5ae85 1679 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2ee97caf
CH
1680 if (!old_device_name) {
1681 error = -ENOMEM;
1682 goto out;
a2de48ca 1683 }
a2de48ca 1684
f349cf34 1685 if (dev->class) {
6b6e39a6 1686 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
f349cf34
EB
1687 &dev->kobj, old_device_name, new_name);
1688 if (error)
1689 goto out;
1690 }
39aba963 1691
a2de48ca 1692 error = kobject_rename(&dev->kobj, new_name);
1fa5ae85 1693 if (error)
2ee97caf 1694 goto out;
a2de48ca 1695
2ee97caf 1696out:
a2de48ca
GKH
1697 put_device(dev);
1698
a2de48ca 1699 kfree(new_class_name);
952ab431 1700 kfree(old_class_name);
2ee97caf 1701 kfree(old_device_name);
a2de48ca
GKH
1702
1703 return error;
1704}
a2807dbc 1705EXPORT_SYMBOL_GPL(device_rename);
8a82472f
CH
1706
1707static int device_move_class_links(struct device *dev,
1708 struct device *old_parent,
1709 struct device *new_parent)
1710{
f7f3461d 1711 int error = 0;
8a82472f 1712
f7f3461d
GKH
1713 if (old_parent)
1714 sysfs_remove_link(&dev->kobj, "device");
1715 if (new_parent)
1716 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1717 "device");
1718 return error;
8a82472f
CH
1719}
1720
1721/**
1722 * device_move - moves a device to a new parent
1723 * @dev: the pointer to the struct device to be moved
c744aeae 1724 * @new_parent: the new parent of the device (can by NULL)
ffa6a705 1725 * @dpm_order: how to reorder the dpm_list
8a82472f 1726 */
ffa6a705
CH
1727int device_move(struct device *dev, struct device *new_parent,
1728 enum dpm_order dpm_order)
8a82472f
CH
1729{
1730 int error;
1731 struct device *old_parent;
c744aeae 1732 struct kobject *new_parent_kobj;
8a82472f
CH
1733
1734 dev = get_device(dev);
1735 if (!dev)
1736 return -EINVAL;
1737
ffa6a705 1738 device_pm_lock();
8a82472f 1739 new_parent = get_device(new_parent);
4a3ad20c 1740 new_parent_kobj = get_device_parent(dev, new_parent);
63b6971a 1741
1e0b2cf9
KS
1742 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1743 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
c744aeae 1744 error = kobject_move(&dev->kobj, new_parent_kobj);
8a82472f 1745 if (error) {
63b6971a 1746 cleanup_glue_dir(dev, new_parent_kobj);
8a82472f
CH
1747 put_device(new_parent);
1748 goto out;
1749 }
1750 old_parent = dev->parent;
1751 dev->parent = new_parent;
1752 if (old_parent)
f791b8c8 1753 klist_remove(&dev->p->knode_parent);
0d358f22 1754 if (new_parent) {
f791b8c8
GKH
1755 klist_add_tail(&dev->p->knode_parent,
1756 &new_parent->p->klist_children);
0d358f22
YL
1757 set_dev_node(dev, dev_to_node(new_parent));
1758 }
1759
bdd4034d
RV
1760 if (dev->class) {
1761 error = device_move_class_links(dev, old_parent, new_parent);
1762 if (error) {
1763 /* We ignore errors on cleanup since we're hosed anyway... */
1764 device_move_class_links(dev, new_parent, old_parent);
1765 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1766 if (new_parent)
1767 klist_remove(&dev->p->knode_parent);
1768 dev->parent = old_parent;
1769 if (old_parent) {
1770 klist_add_tail(&dev->p->knode_parent,
1771 &old_parent->p->klist_children);
1772 set_dev_node(dev, dev_to_node(old_parent));
1773 }
0d358f22 1774 }
bdd4034d
RV
1775 cleanup_glue_dir(dev, new_parent_kobj);
1776 put_device(new_parent);
1777 goto out;
8a82472f 1778 }
8a82472f 1779 }
ffa6a705
CH
1780 switch (dpm_order) {
1781 case DPM_ORDER_NONE:
1782 break;
1783 case DPM_ORDER_DEV_AFTER_PARENT:
1784 device_pm_move_after(dev, new_parent);
1785 break;
1786 case DPM_ORDER_PARENT_BEFORE_DEV:
1787 device_pm_move_before(new_parent, dev);
1788 break;
1789 case DPM_ORDER_DEV_LAST:
1790 device_pm_move_last(dev);
1791 break;
1792 }
bdd4034d 1793
8a82472f
CH
1794 put_device(old_parent);
1795out:
ffa6a705 1796 device_pm_unlock();
8a82472f
CH
1797 put_device(dev);
1798 return error;
1799}
8a82472f 1800EXPORT_SYMBOL_GPL(device_move);
37b0c020
GKH
1801
1802/**
1803 * device_shutdown - call ->shutdown() on each device to shutdown.
1804 */
1805void device_shutdown(void)
1806{
6245838f
HD
1807 struct device *dev;
1808
1809 spin_lock(&devices_kset->list_lock);
1810 /*
1811 * Walk the devices list backward, shutting down each in turn.
1812 * Beware that device unplug events may also start pulling
1813 * devices offline, even as the system is shutting down.
1814 */
1815 while (!list_empty(&devices_kset->list)) {
1816 dev = list_entry(devices_kset->list.prev, struct device,
1817 kobj.entry);
d1c6c030
ML
1818
1819 /*
1820 * hold reference count of device's parent to
1821 * prevent it from being freed because parent's
1822 * lock is to be held
1823 */
1824 get_device(dev->parent);
6245838f
HD
1825 get_device(dev);
1826 /*
1827 * Make sure the device is off the kset list, in the
1828 * event that dev->*->shutdown() doesn't remove it.
1829 */
1830 list_del_init(&dev->kobj.entry);
1831 spin_unlock(&devices_kset->list_lock);
fe6b91f4 1832
d1c6c030
ML
1833 /* hold lock to avoid race with probe/release */
1834 if (dev->parent)
1835 device_lock(dev->parent);
1836 device_lock(dev);
1837
fe6b91f4
AS
1838 /* Don't allow any more runtime suspends */
1839 pm_runtime_get_noresume(dev);
1840 pm_runtime_barrier(dev);
37b0c020 1841
37b0c020 1842 if (dev->bus && dev->bus->shutdown) {
0246c4fa
SL
1843 if (initcall_debug)
1844 dev_info(dev, "shutdown\n");
37b0c020
GKH
1845 dev->bus->shutdown(dev);
1846 } else if (dev->driver && dev->driver->shutdown) {
0246c4fa
SL
1847 if (initcall_debug)
1848 dev_info(dev, "shutdown\n");
37b0c020
GKH
1849 dev->driver->shutdown(dev);
1850 }
d1c6c030
ML
1851
1852 device_unlock(dev);
1853 if (dev->parent)
1854 device_unlock(dev->parent);
1855
6245838f 1856 put_device(dev);
d1c6c030 1857 put_device(dev->parent);
6245838f
HD
1858
1859 spin_lock(&devices_kset->list_lock);
37b0c020 1860 }
6245838f 1861 spin_unlock(&devices_kset->list_lock);
401097ea 1862 async_synchronize_full();
37b0c020 1863}
99bcf217
JP
1864
1865/*
1866 * Device logging functions
1867 */
1868
1869#ifdef CONFIG_PRINTK
666f355f
JP
1870static int
1871create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
99bcf217 1872{
c4e00daa 1873 const char *subsys;
798efc60 1874 size_t pos = 0;
99bcf217 1875
c4e00daa
KS
1876 if (dev->class)
1877 subsys = dev->class->name;
1878 else if (dev->bus)
1879 subsys = dev->bus->name;
1880 else
798efc60 1881 return 0;
c4e00daa 1882
798efc60 1883 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
c4e00daa
KS
1884
1885 /*
1886 * Add device identifier DEVICE=:
1887 * b12:8 block dev_t
1888 * c127:3 char dev_t
1889 * n8 netdev ifindex
1890 * +sound:card0 subsystem:devname
1891 */
1892 if (MAJOR(dev->devt)) {
1893 char c;
1894
1895 if (strcmp(subsys, "block") == 0)
1896 c = 'b';
1897 else
1898 c = 'c';
798efc60
JP
1899 pos++;
1900 pos += snprintf(hdr + pos, hdrlen - pos,
1901 "DEVICE=%c%u:%u",
1902 c, MAJOR(dev->devt), MINOR(dev->devt));
c4e00daa
KS
1903 } else if (strcmp(subsys, "net") == 0) {
1904 struct net_device *net = to_net_dev(dev);
1905
798efc60
JP
1906 pos++;
1907 pos += snprintf(hdr + pos, hdrlen - pos,
1908 "DEVICE=n%u", net->ifindex);
c4e00daa 1909 } else {
798efc60
JP
1910 pos++;
1911 pos += snprintf(hdr + pos, hdrlen - pos,
1912 "DEVICE=+%s:%s", subsys, dev_name(dev));
c4e00daa 1913 }
af7f2158 1914
798efc60
JP
1915 return pos;
1916}
1917EXPORT_SYMBOL(create_syslog_header);
1918
05e4e5b8
JP
1919int dev_vprintk_emit(int level, const struct device *dev,
1920 const char *fmt, va_list args)
1921{
1922 char hdr[128];
1923 size_t hdrlen;
1924
1925 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
1926
1927 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
1928}
1929EXPORT_SYMBOL(dev_vprintk_emit);
1930
1931int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1932{
1933 va_list args;
1934 int r;
1935
1936 va_start(args, fmt);
1937
1938 r = dev_vprintk_emit(level, dev, fmt, args);
1939
1940 va_end(args);
1941
1942 return r;
1943}
1944EXPORT_SYMBOL(dev_printk_emit);
1945
798efc60
JP
1946static int __dev_printk(const char *level, const struct device *dev,
1947 struct va_format *vaf)
1948{
798efc60
JP
1949 if (!dev)
1950 return printk("%s(NULL device *): %pV", level, vaf);
1951
666f355f
JP
1952 return dev_printk_emit(level[1] - '0', dev,
1953 "%s %s: %pV",
1954 dev_driver_string(dev), dev_name(dev), vaf);
99bcf217
JP
1955}
1956
1957int dev_printk(const char *level, const struct device *dev,
1958 const char *fmt, ...)
1959{
1960 struct va_format vaf;
1961 va_list args;
1962 int r;
1963
1964 va_start(args, fmt);
1965
1966 vaf.fmt = fmt;
1967 vaf.va = &args;
1968
1969 r = __dev_printk(level, dev, &vaf);
798efc60 1970
99bcf217
JP
1971 va_end(args);
1972
1973 return r;
1974}
1975EXPORT_SYMBOL(dev_printk);
1976
1977#define define_dev_printk_level(func, kern_level) \
1978int func(const struct device *dev, const char *fmt, ...) \
1979{ \
1980 struct va_format vaf; \
1981 va_list args; \
1982 int r; \
1983 \
1984 va_start(args, fmt); \
1985 \
1986 vaf.fmt = fmt; \
1987 vaf.va = &args; \
1988 \
1989 r = __dev_printk(kern_level, dev, &vaf); \
798efc60 1990 \
99bcf217
JP
1991 va_end(args); \
1992 \
1993 return r; \
1994} \
1995EXPORT_SYMBOL(func);
1996
1997define_dev_printk_level(dev_emerg, KERN_EMERG);
1998define_dev_printk_level(dev_alert, KERN_ALERT);
1999define_dev_printk_level(dev_crit, KERN_CRIT);
2000define_dev_printk_level(dev_err, KERN_ERR);
2001define_dev_printk_level(dev_warn, KERN_WARNING);
2002define_dev_printk_level(dev_notice, KERN_NOTICE);
2003define_dev_printk_level(_dev_info, KERN_INFO);
2004
2005#endif