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