]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/base/core.c
PM / sleep: Make async suspend/resume of devices use device links
[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>
97badf87 15#include <linux/fwnode.h>
1da177e4
LT
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/string.h>
23681e47 20#include <linux/kdev_t.h>
116af378 21#include <linux/notifier.h>
07d57a32
GL
22#include <linux/of.h>
23#include <linux/of_device.h>
da231fd5 24#include <linux/genhd.h>
815d2d50 25#include <linux/kallsyms.h>
f75b1c60 26#include <linux/mutex.h>
af8db150 27#include <linux/pm_runtime.h>
c4e00daa 28#include <linux/netdevice.h>
63967685 29#include <linux/sysfs.h>
1da177e4
LT
30
31#include "base.h"
32#include "power/power.h"
33
e52eec13
AK
34#ifdef CONFIG_SYSFS_DEPRECATED
35#ifdef CONFIG_SYSFS_DEPRECATED_V2
36long sysfs_deprecated = 1;
37#else
38long sysfs_deprecated = 0;
39#endif
3454bf96 40static int __init sysfs_deprecated_setup(char *arg)
e52eec13 41{
34da5e67 42 return kstrtol(arg, 10, &sysfs_deprecated);
e52eec13
AK
43}
44early_param("sysfs.deprecated", sysfs_deprecated_setup);
45#endif
46
9ed98953
RW
47/* Device links support. */
48
49#ifdef CONFIG_SRCU
50static DEFINE_MUTEX(device_links_lock);
51DEFINE_STATIC_SRCU(device_links_srcu);
52
53static inline void device_links_write_lock(void)
54{
55 mutex_lock(&device_links_lock);
56}
57
58static inline void device_links_write_unlock(void)
59{
60 mutex_unlock(&device_links_lock);
61}
62
63int device_links_read_lock(void)
64{
65 return srcu_read_lock(&device_links_srcu);
66}
67
68void device_links_read_unlock(int idx)
69{
70 srcu_read_unlock(&device_links_srcu, idx);
71}
72#else /* !CONFIG_SRCU */
73static DECLARE_RWSEM(device_links_lock);
74
75static inline void device_links_write_lock(void)
76{
77 down_write(&device_links_lock);
78}
79
80static inline void device_links_write_unlock(void)
81{
82 up_write(&device_links_lock);
83}
84
85int device_links_read_lock(void)
86{
87 down_read(&device_links_lock);
88 return 0;
89}
90
91void device_links_read_unlock(int not_used)
92{
93 up_read(&device_links_lock);
94}
95#endif /* !CONFIG_SRCU */
96
97/**
98 * device_is_dependent - Check if one device depends on another one
99 * @dev: Device to check dependencies for.
100 * @target: Device to check against.
101 *
102 * Check if @target depends on @dev or any device dependent on it (its child or
103 * its consumer etc). Return 1 if that is the case or 0 otherwise.
104 */
105static int device_is_dependent(struct device *dev, void *target)
106{
107 struct device_link *link;
108 int ret;
109
110 if (WARN_ON(dev == target))
111 return 1;
112
113 ret = device_for_each_child(dev, target, device_is_dependent);
114 if (ret)
115 return ret;
116
117 list_for_each_entry(link, &dev->links.consumers, s_node) {
118 if (WARN_ON(link->consumer == target))
119 return 1;
120
121 ret = device_is_dependent(link->consumer, target);
122 if (ret)
123 break;
124 }
125 return ret;
126}
127
128static int device_reorder_to_tail(struct device *dev, void *not_used)
129{
130 struct device_link *link;
131
132 /*
133 * Devices that have not been registered yet will be put to the ends
134 * of the lists during the registration, so skip them here.
135 */
136 if (device_is_registered(dev))
137 devices_kset_move_last(dev);
138
139 if (device_pm_initialized(dev))
140 device_pm_move_last(dev);
141
142 device_for_each_child(dev, NULL, device_reorder_to_tail);
143 list_for_each_entry(link, &dev->links.consumers, s_node)
144 device_reorder_to_tail(link->consumer, NULL);
145
146 return 0;
147}
148
149/**
150 * device_link_add - Create a link between two devices.
151 * @consumer: Consumer end of the link.
152 * @supplier: Supplier end of the link.
153 * @flags: Link flags.
154 *
155 * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically
156 * when the consumer device driver unbinds from it. The combination of both
157 * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL
158 * to be returned.
159 *
160 * A side effect of the link creation is re-ordering of dpm_list and the
161 * devices_kset list by moving the consumer device and all devices depending
162 * on it to the ends of these lists (that does not happen to devices that have
163 * not been registered when this function is called).
164 *
165 * The supplier device is required to be registered when this function is called
166 * and NULL will be returned if that is not the case. The consumer device need
167 * not be registerd, however.
168 */
169struct device_link *device_link_add(struct device *consumer,
170 struct device *supplier, u32 flags)
171{
172 struct device_link *link;
173
174 if (!consumer || !supplier ||
175 ((flags & DL_FLAG_STATELESS) && (flags & DL_FLAG_AUTOREMOVE)))
176 return NULL;
177
178 device_links_write_lock();
179 device_pm_lock();
180
181 /*
182 * If the supplier has not been fully registered yet or there is a
183 * reverse dependency between the consumer and the supplier already in
184 * the graph, return NULL.
185 */
186 if (!device_pm_initialized(supplier)
187 || device_is_dependent(consumer, supplier)) {
188 link = NULL;
189 goto out;
190 }
191
192 list_for_each_entry(link, &supplier->links.consumers, s_node)
193 if (link->consumer == consumer)
194 goto out;
195
196 link = kmalloc(sizeof(*link), GFP_KERNEL);
197 if (!link)
198 goto out;
199
200 get_device(supplier);
201 link->supplier = supplier;
202 INIT_LIST_HEAD(&link->s_node);
203 get_device(consumer);
204 link->consumer = consumer;
205 INIT_LIST_HEAD(&link->c_node);
206 link->flags = flags;
207
208 /* Deterine the initial link state. */
209 if (flags & DL_FLAG_STATELESS) {
210 link->status = DL_STATE_NONE;
211 } else {
212 switch (supplier->links.status) {
213 case DL_DEV_DRIVER_BOUND:
214 switch (consumer->links.status) {
215 case DL_DEV_PROBING:
216 link->status = DL_STATE_CONSUMER_PROBE;
217 break;
218 case DL_DEV_DRIVER_BOUND:
219 link->status = DL_STATE_ACTIVE;
220 break;
221 default:
222 link->status = DL_STATE_AVAILABLE;
223 break;
224 }
225 break;
226 case DL_DEV_UNBINDING:
227 link->status = DL_STATE_SUPPLIER_UNBIND;
228 break;
229 default:
230 link->status = DL_STATE_DORMANT;
231 break;
232 }
233 }
234
235 /*
236 * Move the consumer and all of the devices depending on it to the end
237 * of dpm_list and the devices_kset list.
238 *
239 * It is necessary to hold dpm_list locked throughout all that or else
240 * we may end up suspending with a wrong ordering of it.
241 */
242 device_reorder_to_tail(consumer, NULL);
243
244 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
245 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
246
247 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
248
249 out:
250 device_pm_unlock();
251 device_links_write_unlock();
252 return link;
253}
254EXPORT_SYMBOL_GPL(device_link_add);
255
256static void device_link_free(struct device_link *link)
257{
258 put_device(link->consumer);
259 put_device(link->supplier);
260 kfree(link);
261}
262
263#ifdef CONFIG_SRCU
264static void __device_link_free_srcu(struct rcu_head *rhead)
265{
266 device_link_free(container_of(rhead, struct device_link, rcu_head));
267}
268
269static void __device_link_del(struct device_link *link)
270{
271 dev_info(link->consumer, "Dropping the link to %s\n",
272 dev_name(link->supplier));
273
274 list_del_rcu(&link->s_node);
275 list_del_rcu(&link->c_node);
276 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
277}
278#else /* !CONFIG_SRCU */
279static void __device_link_del(struct device_link *link)
280{
281 dev_info(link->consumer, "Dropping the link to %s\n",
282 dev_name(link->supplier));
283
284 list_del(&link->s_node);
285 list_del(&link->c_node);
286 device_link_free(link);
287}
288#endif /* !CONFIG_SRCU */
289
290/**
291 * device_link_del - Delete a link between two devices.
292 * @link: Device link to delete.
293 *
294 * The caller must ensure proper synchronization of this function with runtime
295 * PM.
296 */
297void device_link_del(struct device_link *link)
298{
299 device_links_write_lock();
300 device_pm_lock();
301 __device_link_del(link);
302 device_pm_unlock();
303 device_links_write_unlock();
304}
305EXPORT_SYMBOL_GPL(device_link_del);
306
307static void device_links_missing_supplier(struct device *dev)
308{
309 struct device_link *link;
310
311 list_for_each_entry(link, &dev->links.suppliers, c_node)
312 if (link->status == DL_STATE_CONSUMER_PROBE)
313 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
314}
315
316/**
317 * device_links_check_suppliers - Check presence of supplier drivers.
318 * @dev: Consumer device.
319 *
320 * Check links from this device to any suppliers. Walk the list of the device's
321 * links to suppliers and see if all of them are available. If not, simply
322 * return -EPROBE_DEFER.
323 *
324 * We need to guarantee that the supplier will not go away after the check has
325 * been positive here. It only can go away in __device_release_driver() and
326 * that function checks the device's links to consumers. This means we need to
327 * mark the link as "consumer probe in progress" to make the supplier removal
328 * wait for us to complete (or bad things may happen).
329 *
330 * Links with the DL_FLAG_STATELESS flag set are ignored.
331 */
332int device_links_check_suppliers(struct device *dev)
333{
334 struct device_link *link;
335 int ret = 0;
336
337 device_links_write_lock();
338
339 list_for_each_entry(link, &dev->links.suppliers, c_node) {
340 if (link->flags & DL_FLAG_STATELESS)
341 continue;
342
343 if (link->status != DL_STATE_AVAILABLE) {
344 device_links_missing_supplier(dev);
345 ret = -EPROBE_DEFER;
346 break;
347 }
348 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
349 }
350 dev->links.status = DL_DEV_PROBING;
351
352 device_links_write_unlock();
353 return ret;
354}
355
356/**
357 * device_links_driver_bound - Update device links after probing its driver.
358 * @dev: Device to update the links for.
359 *
360 * The probe has been successful, so update links from this device to any
361 * consumers by changing their status to "available".
362 *
363 * Also change the status of @dev's links to suppliers to "active".
364 *
365 * Links with the DL_FLAG_STATELESS flag set are ignored.
366 */
367void device_links_driver_bound(struct device *dev)
368{
369 struct device_link *link;
370
371 device_links_write_lock();
372
373 list_for_each_entry(link, &dev->links.consumers, s_node) {
374 if (link->flags & DL_FLAG_STATELESS)
375 continue;
376
377 WARN_ON(link->status != DL_STATE_DORMANT);
378 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
379 }
380
381 list_for_each_entry(link, &dev->links.suppliers, c_node) {
382 if (link->flags & DL_FLAG_STATELESS)
383 continue;
384
385 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
386 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
387 }
388
389 dev->links.status = DL_DEV_DRIVER_BOUND;
390
391 device_links_write_unlock();
392}
393
394/**
395 * __device_links_no_driver - Update links of a device without a driver.
396 * @dev: Device without a drvier.
397 *
398 * Delete all non-persistent links from this device to any suppliers.
399 *
400 * Persistent links stay around, but their status is changed to "available",
401 * unless they already are in the "supplier unbind in progress" state in which
402 * case they need not be updated.
403 *
404 * Links with the DL_FLAG_STATELESS flag set are ignored.
405 */
406static void __device_links_no_driver(struct device *dev)
407{
408 struct device_link *link, *ln;
409
410 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
411 if (link->flags & DL_FLAG_STATELESS)
412 continue;
413
414 if (link->flags & DL_FLAG_AUTOREMOVE)
415 __device_link_del(link);
416 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
417 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
418 }
419
420 dev->links.status = DL_DEV_NO_DRIVER;
421}
422
423void device_links_no_driver(struct device *dev)
424{
425 device_links_write_lock();
426 __device_links_no_driver(dev);
427 device_links_write_unlock();
428}
429
430/**
431 * device_links_driver_cleanup - Update links after driver removal.
432 * @dev: Device whose driver has just gone away.
433 *
434 * Update links to consumers for @dev by changing their status to "dormant" and
435 * invoke %__device_links_no_driver() to update links to suppliers for it as
436 * appropriate.
437 *
438 * Links with the DL_FLAG_STATELESS flag set are ignored.
439 */
440void device_links_driver_cleanup(struct device *dev)
441{
442 struct device_link *link;
443
444 device_links_write_lock();
445
446 list_for_each_entry(link, &dev->links.consumers, s_node) {
447 if (link->flags & DL_FLAG_STATELESS)
448 continue;
449
450 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
451 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
452 WRITE_ONCE(link->status, DL_STATE_DORMANT);
453 }
454
455 __device_links_no_driver(dev);
456
457 device_links_write_unlock();
458}
459
460/**
461 * device_links_busy - Check if there are any busy links to consumers.
462 * @dev: Device to check.
463 *
464 * Check each consumer of the device and return 'true' if its link's status
465 * is one of "consumer probe" or "active" (meaning that the given consumer is
466 * probing right now or its driver is present). Otherwise, change the link
467 * state to "supplier unbind" to prevent the consumer from being probed
468 * successfully going forward.
469 *
470 * Return 'false' if there are no probing or active consumers.
471 *
472 * Links with the DL_FLAG_STATELESS flag set are ignored.
473 */
474bool device_links_busy(struct device *dev)
475{
476 struct device_link *link;
477 bool ret = false;
478
479 device_links_write_lock();
480
481 list_for_each_entry(link, &dev->links.consumers, s_node) {
482 if (link->flags & DL_FLAG_STATELESS)
483 continue;
484
485 if (link->status == DL_STATE_CONSUMER_PROBE
486 || link->status == DL_STATE_ACTIVE) {
487 ret = true;
488 break;
489 }
490 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
491 }
492
493 dev->links.status = DL_DEV_UNBINDING;
494
495 device_links_write_unlock();
496 return ret;
497}
498
499/**
500 * device_links_unbind_consumers - Force unbind consumers of the given device.
501 * @dev: Device to unbind the consumers of.
502 *
503 * Walk the list of links to consumers for @dev and if any of them is in the
504 * "consumer probe" state, wait for all device probes in progress to complete
505 * and start over.
506 *
507 * If that's not the case, change the status of the link to "supplier unbind"
508 * and check if the link was in the "active" state. If so, force the consumer
509 * driver to unbind and start over (the consumer will not re-probe as we have
510 * changed the state of the link already).
511 *
512 * Links with the DL_FLAG_STATELESS flag set are ignored.
513 */
514void device_links_unbind_consumers(struct device *dev)
515{
516 struct device_link *link;
517
518 start:
519 device_links_write_lock();
520
521 list_for_each_entry(link, &dev->links.consumers, s_node) {
522 enum device_link_state status;
523
524 if (link->flags & DL_FLAG_STATELESS)
525 continue;
526
527 status = link->status;
528 if (status == DL_STATE_CONSUMER_PROBE) {
529 device_links_write_unlock();
530
531 wait_for_device_probe();
532 goto start;
533 }
534 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
535 if (status == DL_STATE_ACTIVE) {
536 struct device *consumer = link->consumer;
537
538 get_device(consumer);
539
540 device_links_write_unlock();
541
542 device_release_driver_internal(consumer, NULL,
543 consumer->parent);
544 put_device(consumer);
545 goto start;
546 }
547 }
548
549 device_links_write_unlock();
550}
551
552/**
553 * device_links_purge - Delete existing links to other devices.
554 * @dev: Target device.
555 */
556static void device_links_purge(struct device *dev)
557{
558 struct device_link *link, *ln;
559
560 /*
561 * Delete all of the remaining links from this device to any other
562 * devices (either consumers or suppliers).
563 */
564 device_links_write_lock();
565
566 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
567 WARN_ON(link->status == DL_STATE_ACTIVE);
568 __device_link_del(link);
569 }
570
571 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
572 WARN_ON(link->status != DL_STATE_DORMANT &&
573 link->status != DL_STATE_NONE);
574 __device_link_del(link);
575 }
576
577 device_links_write_unlock();
578}
579
580/* Device links support end. */
581
4a3ad20c
GKH
582int (*platform_notify)(struct device *dev) = NULL;
583int (*platform_notify_remove)(struct device *dev) = NULL;
e105b8bf
DW
584static struct kobject *dev_kobj;
585struct kobject *sysfs_dev_char_kobj;
586struct kobject *sysfs_dev_block_kobj;
1da177e4 587
5e33bc41
RW
588static DEFINE_MUTEX(device_hotplug_lock);
589
590void lock_device_hotplug(void)
591{
592 mutex_lock(&device_hotplug_lock);
593}
594
595void unlock_device_hotplug(void)
596{
597 mutex_unlock(&device_hotplug_lock);
598}
599
600int lock_device_hotplug_sysfs(void)
601{
602 if (mutex_trylock(&device_hotplug_lock))
603 return 0;
604
605 /* Avoid busy looping (5 ms of sleep should do). */
606 msleep(5);
607 return restart_syscall();
608}
609
4e886c29
GKH
610#ifdef CONFIG_BLOCK
611static inline int device_is_not_partition(struct device *dev)
612{
613 return !(dev->type == &part_type);
614}
615#else
616static inline int device_is_not_partition(struct device *dev)
617{
618 return 1;
619}
620#endif
1da177e4 621
3e95637a
AS
622/**
623 * dev_driver_string - Return a device's driver name, if at all possible
624 * @dev: struct device to get the name of
625 *
626 * Will return the device's driver's name if it is bound to a device. If
9169c012 627 * the device is not bound to a driver, it will return the name of the bus
3e95637a
AS
628 * it is attached to. If it is not attached to a bus either, an empty
629 * string will be returned.
630 */
bf9ca69f 631const char *dev_driver_string(const struct device *dev)
3e95637a 632{
3589972e
AS
633 struct device_driver *drv;
634
635 /* dev->driver can change to NULL underneath us because of unbinding,
636 * so be careful about accessing it. dev->bus and dev->class should
637 * never change once they are set, so they don't need special care.
638 */
639 drv = ACCESS_ONCE(dev->driver);
640 return drv ? drv->name :
a456b702
JD
641 (dev->bus ? dev->bus->name :
642 (dev->class ? dev->class->name : ""));
3e95637a 643}
310a922d 644EXPORT_SYMBOL(dev_driver_string);
3e95637a 645
1da177e4
LT
646#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
647
4a3ad20c
GKH
648static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
649 char *buf)
1da177e4 650{
4a3ad20c 651 struct device_attribute *dev_attr = to_dev_attr(attr);
b0d1f807 652 struct device *dev = kobj_to_dev(kobj);
4a0c20bf 653 ssize_t ret = -EIO;
1da177e4
LT
654
655 if (dev_attr->show)
54b6f35c 656 ret = dev_attr->show(dev, dev_attr, buf);
815d2d50 657 if (ret >= (ssize_t)PAGE_SIZE) {
53a9c87e
GKH
658 print_symbol("dev_attr_show: %s returned bad count\n",
659 (unsigned long)dev_attr->show);
815d2d50 660 }
1da177e4
LT
661 return ret;
662}
663
4a3ad20c
GKH
664static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
665 const char *buf, size_t count)
1da177e4 666{
4a3ad20c 667 struct device_attribute *dev_attr = to_dev_attr(attr);
b0d1f807 668 struct device *dev = kobj_to_dev(kobj);
4a0c20bf 669 ssize_t ret = -EIO;
1da177e4
LT
670
671 if (dev_attr->store)
54b6f35c 672 ret = dev_attr->store(dev, dev_attr, buf, count);
1da177e4
LT
673 return ret;
674}
675
52cf25d0 676static const struct sysfs_ops dev_sysfs_ops = {
1da177e4
LT
677 .show = dev_attr_show,
678 .store = dev_attr_store,
679};
680
ca22e56d
KS
681#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
682
683ssize_t device_store_ulong(struct device *dev,
684 struct device_attribute *attr,
685 const char *buf, size_t size)
686{
687 struct dev_ext_attribute *ea = to_ext_attr(attr);
688 char *end;
689 unsigned long new = simple_strtoul(buf, &end, 0);
690 if (end == buf)
691 return -EINVAL;
692 *(unsigned long *)(ea->var) = new;
693 /* Always return full write size even if we didn't consume all */
694 return size;
695}
696EXPORT_SYMBOL_GPL(device_store_ulong);
697
698ssize_t device_show_ulong(struct device *dev,
699 struct device_attribute *attr,
700 char *buf)
701{
702 struct dev_ext_attribute *ea = to_ext_attr(attr);
703 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
704}
705EXPORT_SYMBOL_GPL(device_show_ulong);
706
707ssize_t device_store_int(struct device *dev,
708 struct device_attribute *attr,
709 const char *buf, size_t size)
710{
711 struct dev_ext_attribute *ea = to_ext_attr(attr);
712 char *end;
713 long new = simple_strtol(buf, &end, 0);
714 if (end == buf || new > INT_MAX || new < INT_MIN)
715 return -EINVAL;
716 *(int *)(ea->var) = new;
717 /* Always return full write size even if we didn't consume all */
718 return size;
719}
720EXPORT_SYMBOL_GPL(device_store_int);
721
722ssize_t device_show_int(struct device *dev,
723 struct device_attribute *attr,
724 char *buf)
725{
726 struct dev_ext_attribute *ea = to_ext_attr(attr);
727
728 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
729}
730EXPORT_SYMBOL_GPL(device_show_int);
1da177e4 731
91872392
BP
732ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
733 const char *buf, size_t size)
734{
735 struct dev_ext_attribute *ea = to_ext_attr(attr);
736
737 if (strtobool(buf, ea->var) < 0)
738 return -EINVAL;
739
740 return size;
741}
742EXPORT_SYMBOL_GPL(device_store_bool);
743
744ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
745 char *buf)
746{
747 struct dev_ext_attribute *ea = to_ext_attr(attr);
748
749 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
750}
751EXPORT_SYMBOL_GPL(device_show_bool);
752
1da177e4 753/**
f8878dcb
RD
754 * device_release - free device structure.
755 * @kobj: device's kobject.
1da177e4 756 *
f8878dcb
RD
757 * This is called once the reference count for the object
758 * reaches 0. We forward the call to the device's release
759 * method, which should handle actually freeing the structure.
1da177e4 760 */
4a3ad20c 761static void device_release(struct kobject *kobj)
1da177e4 762{
b0d1f807 763 struct device *dev = kobj_to_dev(kobj);
fb069a5d 764 struct device_private *p = dev->p;
1da177e4 765
a525a3dd
ML
766 /*
767 * Some platform devices are driven without driver attached
768 * and managed resources may have been acquired. Make sure
769 * all resources are released.
770 *
771 * Drivers still can add resources into device after device
772 * is deleted but alive, so release devres here to avoid
773 * possible memory leak.
774 */
775 devres_release_all(dev);
776
1da177e4
LT
777 if (dev->release)
778 dev->release(dev);
f9f852df
KS
779 else if (dev->type && dev->type->release)
780 dev->type->release(dev);
2620efef
GKH
781 else if (dev->class && dev->class->dev_release)
782 dev->class->dev_release(dev);
f810a5cf
AV
783 else
784 WARN(1, KERN_ERR "Device '%s' does not have a release() "
4a3ad20c 785 "function, it is broken and must be fixed.\n",
1e0b2cf9 786 dev_name(dev));
fb069a5d 787 kfree(p);
1da177e4
LT
788}
789
bc451f20
EB
790static const void *device_namespace(struct kobject *kobj)
791{
b0d1f807 792 struct device *dev = kobj_to_dev(kobj);
bc451f20
EB
793 const void *ns = NULL;
794
795 if (dev->class && dev->class->ns_type)
796 ns = dev->class->namespace(dev);
797
798 return ns;
799}
800
8f4afc41 801static struct kobj_type device_ktype = {
1da177e4
LT
802 .release = device_release,
803 .sysfs_ops = &dev_sysfs_ops,
bc451f20 804 .namespace = device_namespace,
1da177e4
LT
805};
806
807
312c004d 808static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1da177e4
LT
809{
810 struct kobj_type *ktype = get_ktype(kobj);
811
8f4afc41 812 if (ktype == &device_ktype) {
b0d1f807 813 struct device *dev = kobj_to_dev(kobj);
1da177e4
LT
814 if (dev->bus)
815 return 1;
23681e47
GKH
816 if (dev->class)
817 return 1;
1da177e4
LT
818 }
819 return 0;
820}
821
312c004d 822static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1da177e4 823{
b0d1f807 824 struct device *dev = kobj_to_dev(kobj);
1da177e4 825
23681e47
GKH
826 if (dev->bus)
827 return dev->bus->name;
828 if (dev->class)
829 return dev->class->name;
830 return NULL;
1da177e4
LT
831}
832
7eff2e7a
KS
833static int dev_uevent(struct kset *kset, struct kobject *kobj,
834 struct kobj_uevent_env *env)
1da177e4 835{
b0d1f807 836 struct device *dev = kobj_to_dev(kobj);
1da177e4
LT
837 int retval = 0;
838
6fcf53ac 839 /* add device node properties if present */
23681e47 840 if (MAJOR(dev->devt)) {
6fcf53ac
KS
841 const char *tmp;
842 const char *name;
2c9ede55 843 umode_t mode = 0;
4e4098a3
GKH
844 kuid_t uid = GLOBAL_ROOT_UID;
845 kgid_t gid = GLOBAL_ROOT_GID;
6fcf53ac 846
7eff2e7a
KS
847 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
848 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
3c2670e6 849 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
6fcf53ac
KS
850 if (name) {
851 add_uevent_var(env, "DEVNAME=%s", name);
e454cea2
KS
852 if (mode)
853 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
4e4098a3
GKH
854 if (!uid_eq(uid, GLOBAL_ROOT_UID))
855 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
856 if (!gid_eq(gid, GLOBAL_ROOT_GID))
857 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
3c2670e6 858 kfree(tmp);
6fcf53ac 859 }
23681e47
GKH
860 }
861
414264f9 862 if (dev->type && dev->type->name)
7eff2e7a 863 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
414264f9 864
239378f1 865 if (dev->driver)
7eff2e7a 866 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
239378f1 867
07d57a32
GL
868 /* Add common DT information about the device */
869 of_device_uevent(dev, env);
870
7eff2e7a 871 /* have the bus specific function add its stuff */
312c004d 872 if (dev->bus && dev->bus->uevent) {
7eff2e7a 873 retval = dev->bus->uevent(dev, env);
f9f852df 874 if (retval)
7dc72b28 875 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1e0b2cf9 876 dev_name(dev), __func__, retval);
1da177e4
LT
877 }
878
7eff2e7a 879 /* have the class specific function add its stuff */
2620efef 880 if (dev->class && dev->class->dev_uevent) {
7eff2e7a 881 retval = dev->class->dev_uevent(dev, env);
f9f852df 882 if (retval)
7dc72b28 883 pr_debug("device: '%s': %s: class uevent() "
1e0b2cf9 884 "returned %d\n", dev_name(dev),
2b3a302a 885 __func__, retval);
f9f852df
KS
886 }
887
eef35c2d 888 /* have the device type specific function add its stuff */
f9f852df 889 if (dev->type && dev->type->uevent) {
7eff2e7a 890 retval = dev->type->uevent(dev, env);
f9f852df 891 if (retval)
7dc72b28 892 pr_debug("device: '%s': %s: dev_type uevent() "
1e0b2cf9 893 "returned %d\n", dev_name(dev),
2b3a302a 894 __func__, retval);
2620efef
GKH
895 }
896
1da177e4
LT
897 return retval;
898}
899
9cd43611 900static const struct kset_uevent_ops device_uevent_ops = {
312c004d
KS
901 .filter = dev_uevent_filter,
902 .name = dev_uevent_name,
903 .uevent = dev_uevent,
1da177e4
LT
904};
905
c5e064a6 906static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
16574dcc
KS
907 char *buf)
908{
909 struct kobject *top_kobj;
910 struct kset *kset;
7eff2e7a 911 struct kobj_uevent_env *env = NULL;
16574dcc
KS
912 int i;
913 size_t count = 0;
914 int retval;
915
916 /* search the kset, the device belongs to */
917 top_kobj = &dev->kobj;
5c5daf65
KS
918 while (!top_kobj->kset && top_kobj->parent)
919 top_kobj = top_kobj->parent;
16574dcc
KS
920 if (!top_kobj->kset)
921 goto out;
5c5daf65 922
16574dcc
KS
923 kset = top_kobj->kset;
924 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
925 goto out;
926
927 /* respect filter */
928 if (kset->uevent_ops && kset->uevent_ops->filter)
929 if (!kset->uevent_ops->filter(kset, &dev->kobj))
930 goto out;
931
7eff2e7a
KS
932 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
933 if (!env)
c7308c81
GKH
934 return -ENOMEM;
935
16574dcc 936 /* let the kset specific function add its keys */
7eff2e7a 937 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
16574dcc
KS
938 if (retval)
939 goto out;
940
941 /* copy keys to file */
7eff2e7a
KS
942 for (i = 0; i < env->envp_idx; i++)
943 count += sprintf(&buf[count], "%s\n", env->envp[i]);
16574dcc 944out:
7eff2e7a 945 kfree(env);
16574dcc
KS
946 return count;
947}
948
c5e064a6 949static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
a7fd6706
KS
950 const char *buf, size_t count)
951{
60a96a59
KS
952 enum kobject_action action;
953
3f5468c9 954 if (kobject_action_type(buf, count, &action) == 0)
60a96a59 955 kobject_uevent(&dev->kobj, action);
3f5468c9
KS
956 else
957 dev_err(dev, "uevent: unknown action-string\n");
a7fd6706
KS
958 return count;
959}
c5e064a6 960static DEVICE_ATTR_RW(uevent);
a7fd6706 961
c5e064a6 962static ssize_t online_show(struct device *dev, struct device_attribute *attr,
4f3549d7
RW
963 char *buf)
964{
965 bool val;
966
5e33bc41 967 device_lock(dev);
4f3549d7 968 val = !dev->offline;
5e33bc41 969 device_unlock(dev);
4f3549d7
RW
970 return sprintf(buf, "%u\n", val);
971}
972
c5e064a6 973static ssize_t online_store(struct device *dev, struct device_attribute *attr,
4f3549d7
RW
974 const char *buf, size_t count)
975{
976 bool val;
977 int ret;
978
979 ret = strtobool(buf, &val);
980 if (ret < 0)
981 return ret;
982
5e33bc41
RW
983 ret = lock_device_hotplug_sysfs();
984 if (ret)
985 return ret;
986
4f3549d7
RW
987 ret = val ? device_online(dev) : device_offline(dev);
988 unlock_device_hotplug();
989 return ret < 0 ? ret : count;
990}
c5e064a6 991static DEVICE_ATTR_RW(online);
4f3549d7 992
fa6fdb33 993int device_add_groups(struct device *dev, const struct attribute_group **groups)
621a1672 994{
3e9b2bae 995 return sysfs_create_groups(&dev->kobj, groups);
de0ff00d
GKH
996}
997
fa6fdb33
GKH
998void device_remove_groups(struct device *dev,
999 const struct attribute_group **groups)
de0ff00d 1000{
3e9b2bae 1001 sysfs_remove_groups(&dev->kobj, groups);
de0ff00d
GKH
1002}
1003
2620efef
GKH
1004static int device_add_attrs(struct device *dev)
1005{
1006 struct class *class = dev->class;
aed65af1 1007 const struct device_type *type = dev->type;
621a1672 1008 int error;
2620efef 1009
621a1672 1010 if (class) {
d05a6f96 1011 error = device_add_groups(dev, class->dev_groups);
f9f852df 1012 if (error)
621a1672 1013 return error;
2620efef 1014 }
f9f852df 1015
621a1672
DT
1016 if (type) {
1017 error = device_add_groups(dev, type->groups);
f9f852df 1018 if (error)
a6b01ded 1019 goto err_remove_class_groups;
f9f852df
KS
1020 }
1021
621a1672
DT
1022 error = device_add_groups(dev, dev->groups);
1023 if (error)
1024 goto err_remove_type_groups;
1025
4f3549d7 1026 if (device_supports_offline(dev) && !dev->offline_disabled) {
c5e064a6 1027 error = device_create_file(dev, &dev_attr_online);
4f3549d7 1028 if (error)
ecfbf6fd 1029 goto err_remove_dev_groups;
4f3549d7
RW
1030 }
1031
621a1672
DT
1032 return 0;
1033
ecfbf6fd
RW
1034 err_remove_dev_groups:
1035 device_remove_groups(dev, dev->groups);
621a1672
DT
1036 err_remove_type_groups:
1037 if (type)
1038 device_remove_groups(dev, type->groups);
d05a6f96
GKH
1039 err_remove_class_groups:
1040 if (class)
1041 device_remove_groups(dev, class->dev_groups);
621a1672 1042
2620efef
GKH
1043 return error;
1044}
1045
1046static void device_remove_attrs(struct device *dev)
1047{
1048 struct class *class = dev->class;
aed65af1 1049 const struct device_type *type = dev->type;
2620efef 1050
c5e064a6 1051 device_remove_file(dev, &dev_attr_online);
621a1672 1052 device_remove_groups(dev, dev->groups);
f9f852df 1053
621a1672
DT
1054 if (type)
1055 device_remove_groups(dev, type->groups);
1056
a6b01ded 1057 if (class)
d05a6f96 1058 device_remove_groups(dev, class->dev_groups);
2620efef
GKH
1059}
1060
c5e064a6 1061static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
23681e47
GKH
1062 char *buf)
1063{
1064 return print_dev_t(buf, dev->devt);
1065}
c5e064a6 1066static DEVICE_ATTR_RO(dev);
ad6a1e1c 1067
ca22e56d 1068/* /sys/devices/ */
881c6cfd 1069struct kset *devices_kset;
1da177e4 1070
52cdbdd4
GS
1071/**
1072 * devices_kset_move_before - Move device in the devices_kset's list.
1073 * @deva: Device to move.
1074 * @devb: Device @deva should come before.
1075 */
1076static void devices_kset_move_before(struct device *deva, struct device *devb)
1077{
1078 if (!devices_kset)
1079 return;
1080 pr_debug("devices_kset: Moving %s before %s\n",
1081 dev_name(deva), dev_name(devb));
1082 spin_lock(&devices_kset->list_lock);
1083 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1084 spin_unlock(&devices_kset->list_lock);
1085}
1086
1087/**
1088 * devices_kset_move_after - Move device in the devices_kset's list.
1089 * @deva: Device to move
1090 * @devb: Device @deva should come after.
1091 */
1092static void devices_kset_move_after(struct device *deva, struct device *devb)
1093{
1094 if (!devices_kset)
1095 return;
1096 pr_debug("devices_kset: Moving %s after %s\n",
1097 dev_name(deva), dev_name(devb));
1098 spin_lock(&devices_kset->list_lock);
1099 list_move(&deva->kobj.entry, &devb->kobj.entry);
1100 spin_unlock(&devices_kset->list_lock);
1101}
1102
1103/**
1104 * devices_kset_move_last - move the device to the end of devices_kset's list.
1105 * @dev: device to move
1106 */
1107void devices_kset_move_last(struct device *dev)
1108{
1109 if (!devices_kset)
1110 return;
1111 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1112 spin_lock(&devices_kset->list_lock);
1113 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1114 spin_unlock(&devices_kset->list_lock);
1115}
1116
1da177e4 1117/**
4a3ad20c
GKH
1118 * device_create_file - create sysfs attribute file for device.
1119 * @dev: device.
1120 * @attr: device attribute descriptor.
1da177e4 1121 */
26579ab7
PC
1122int device_create_file(struct device *dev,
1123 const struct device_attribute *attr)
1da177e4
LT
1124{
1125 int error = 0;
8f46baaa
FB
1126
1127 if (dev) {
1128 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
97521978 1129 "Attribute %s: write permission without 'store'\n",
1130 attr->attr.name);
8f46baaa 1131 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
97521978 1132 "Attribute %s: read permission without 'show'\n",
1133 attr->attr.name);
1da177e4 1134 error = sysfs_create_file(&dev->kobj, &attr->attr);
8f46baaa
FB
1135 }
1136
1da177e4
LT
1137 return error;
1138}
86df2687 1139EXPORT_SYMBOL_GPL(device_create_file);
1da177e4
LT
1140
1141/**
4a3ad20c
GKH
1142 * device_remove_file - remove sysfs attribute file.
1143 * @dev: device.
1144 * @attr: device attribute descriptor.
1da177e4 1145 */
26579ab7
PC
1146void device_remove_file(struct device *dev,
1147 const struct device_attribute *attr)
1da177e4 1148{
0c98b19f 1149 if (dev)
1da177e4 1150 sysfs_remove_file(&dev->kobj, &attr->attr);
1da177e4 1151}
86df2687 1152EXPORT_SYMBOL_GPL(device_remove_file);
1da177e4 1153
6b0afc2a
TH
1154/**
1155 * device_remove_file_self - remove sysfs attribute file from its own method.
1156 * @dev: device.
1157 * @attr: device attribute descriptor.
1158 *
1159 * See kernfs_remove_self() for details.
1160 */
1161bool device_remove_file_self(struct device *dev,
1162 const struct device_attribute *attr)
1163{
1164 if (dev)
1165 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1166 else
1167 return false;
1168}
1169EXPORT_SYMBOL_GPL(device_remove_file_self);
1170
2589f188
GKH
1171/**
1172 * device_create_bin_file - create sysfs binary attribute file for device.
1173 * @dev: device.
1174 * @attr: device binary attribute descriptor.
1175 */
66ecb92b
PC
1176int device_create_bin_file(struct device *dev,
1177 const struct bin_attribute *attr)
2589f188
GKH
1178{
1179 int error = -EINVAL;
1180 if (dev)
1181 error = sysfs_create_bin_file(&dev->kobj, attr);
1182 return error;
1183}
1184EXPORT_SYMBOL_GPL(device_create_bin_file);
1185
1186/**
1187 * device_remove_bin_file - remove sysfs binary attribute file
1188 * @dev: device.
1189 * @attr: device binary attribute descriptor.
1190 */
66ecb92b
PC
1191void device_remove_bin_file(struct device *dev,
1192 const struct bin_attribute *attr)
2589f188
GKH
1193{
1194 if (dev)
1195 sysfs_remove_bin_file(&dev->kobj, attr);
1196}
1197EXPORT_SYMBOL_GPL(device_remove_bin_file);
1198
34bb61f9
JB
1199static void klist_children_get(struct klist_node *n)
1200{
f791b8c8
GKH
1201 struct device_private *p = to_device_private_parent(n);
1202 struct device *dev = p->device;
34bb61f9
JB
1203
1204 get_device(dev);
1205}
1206
1207static void klist_children_put(struct klist_node *n)
1208{
f791b8c8
GKH
1209 struct device_private *p = to_device_private_parent(n);
1210 struct device *dev = p->device;
34bb61f9
JB
1211
1212 put_device(dev);
1213}
1214
1da177e4 1215/**
4a3ad20c
GKH
1216 * device_initialize - init device structure.
1217 * @dev: device.
1da177e4 1218 *
5739411a
CH
1219 * This prepares the device for use by other layers by initializing
1220 * its fields.
4a3ad20c 1221 * It is the first half of device_register(), if called by
5739411a
CH
1222 * that function, though it can also be called separately, so one
1223 * may use @dev's fields. In particular, get_device()/put_device()
1224 * may be used for reference counting of @dev after calling this
1225 * function.
1226 *
b10d5efd
AS
1227 * All fields in @dev must be initialized by the caller to 0, except
1228 * for those explicitly set to some other value. The simplest
1229 * approach is to use kzalloc() to allocate the structure containing
1230 * @dev.
1231 *
5739411a
CH
1232 * NOTE: Use put_device() to give up your reference instead of freeing
1233 * @dev directly once you have called this function.
1da177e4 1234 */
1da177e4
LT
1235void device_initialize(struct device *dev)
1236{
881c6cfd 1237 dev->kobj.kset = devices_kset;
f9cb074b 1238 kobject_init(&dev->kobj, &device_ktype);
1da177e4 1239 INIT_LIST_HEAD(&dev->dma_pools);
3142788b 1240 mutex_init(&dev->mutex);
1704f47b 1241 lockdep_set_novalidate_class(&dev->mutex);
9ac7849e
TH
1242 spin_lock_init(&dev->devres_lock);
1243 INIT_LIST_HEAD(&dev->devres_head);
3b98aeaf 1244 device_pm_init(dev);
87348136 1245 set_dev_node(dev, -1);
4a7cc831
JL
1246#ifdef CONFIG_GENERIC_MSI_IRQ
1247 INIT_LIST_HEAD(&dev->msi_list);
1248#endif
9ed98953
RW
1249 INIT_LIST_HEAD(&dev->links.consumers);
1250 INIT_LIST_HEAD(&dev->links.suppliers);
1251 dev->links.status = DL_DEV_NO_DRIVER;
1da177e4 1252}
86df2687 1253EXPORT_SYMBOL_GPL(device_initialize);
1da177e4 1254
d73ce004 1255struct kobject *virtual_device_parent(struct device *dev)
f0ee61a6 1256{
86406245 1257 static struct kobject *virtual_dir = NULL;
f0ee61a6 1258
86406245 1259 if (!virtual_dir)
4ff6abff 1260 virtual_dir = kobject_create_and_add("virtual",
881c6cfd 1261 &devices_kset->kobj);
f0ee61a6 1262
86406245 1263 return virtual_dir;
f0ee61a6
GKH
1264}
1265
bc451f20
EB
1266struct class_dir {
1267 struct kobject kobj;
1268 struct class *class;
1269};
1270
1271#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1272
1273static void class_dir_release(struct kobject *kobj)
1274{
1275 struct class_dir *dir = to_class_dir(kobj);
1276 kfree(dir);
1277}
1278
1279static const
1280struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
40fa5422 1281{
bc451f20
EB
1282 struct class_dir *dir = to_class_dir(kobj);
1283 return dir->class->ns_type;
1284}
1285
1286static struct kobj_type class_dir_ktype = {
1287 .release = class_dir_release,
1288 .sysfs_ops = &kobj_sysfs_ops,
1289 .child_ns_type = class_dir_child_ns_type
1290};
1291
1292static struct kobject *
1293class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1294{
1295 struct class_dir *dir;
43968d2f
GKH
1296 int retval;
1297
bc451f20
EB
1298 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1299 if (!dir)
1300 return NULL;
1301
1302 dir->class = class;
1303 kobject_init(&dir->kobj, &class_dir_ktype);
1304
6b6e39a6 1305 dir->kobj.kset = &class->p->glue_dirs;
bc451f20
EB
1306
1307 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1308 if (retval < 0) {
1309 kobject_put(&dir->kobj);
1310 return NULL;
1311 }
1312 return &dir->kobj;
1313}
1314
e4a60d13 1315static DEFINE_MUTEX(gdp_mutex);
bc451f20
EB
1316
1317static struct kobject *get_device_parent(struct device *dev,
1318 struct device *parent)
1319{
86406245
KS
1320 if (dev->class) {
1321 struct kobject *kobj = NULL;
1322 struct kobject *parent_kobj;
1323 struct kobject *k;
1324
ead454fe 1325#ifdef CONFIG_BLOCK
39aba963 1326 /* block disks show up in /sys/block */
e52eec13 1327 if (sysfs_deprecated && dev->class == &block_class) {
39aba963
KS
1328 if (parent && parent->class == &block_class)
1329 return &parent->kobj;
6b6e39a6 1330 return &block_class.p->subsys.kobj;
39aba963 1331 }
ead454fe 1332#endif
e52eec13 1333
86406245
KS
1334 /*
1335 * If we have no parent, we live in "virtual".
0f4dafc0
KS
1336 * Class-devices with a non class-device as parent, live
1337 * in a "glue" directory to prevent namespace collisions.
86406245
KS
1338 */
1339 if (parent == NULL)
1340 parent_kobj = virtual_device_parent(dev);
24b1442d 1341 else if (parent->class && !dev->class->ns_type)
86406245
KS
1342 return &parent->kobj;
1343 else
1344 parent_kobj = &parent->kobj;
1345
77d3d7c1
TH
1346 mutex_lock(&gdp_mutex);
1347
86406245 1348 /* find our class-directory at the parent and reference it */
6b6e39a6
KS
1349 spin_lock(&dev->class->p->glue_dirs.list_lock);
1350 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
86406245
KS
1351 if (k->parent == parent_kobj) {
1352 kobj = kobject_get(k);
1353 break;
1354 }
6b6e39a6 1355 spin_unlock(&dev->class->p->glue_dirs.list_lock);
77d3d7c1
TH
1356 if (kobj) {
1357 mutex_unlock(&gdp_mutex);
86406245 1358 return kobj;
77d3d7c1 1359 }
86406245
KS
1360
1361 /* or create a new class-directory at the parent device */
bc451f20 1362 k = class_dir_create_and_add(dev->class, parent_kobj);
0f4dafc0 1363 /* do not emit an uevent for this simple "glue" directory */
77d3d7c1 1364 mutex_unlock(&gdp_mutex);
43968d2f 1365 return k;
86406245
KS
1366 }
1367
ca22e56d
KS
1368 /* subsystems can specify a default root directory for their devices */
1369 if (!parent && dev->bus && dev->bus->dev_root)
1370 return &dev->bus->dev_root->kobj;
1371
86406245 1372 if (parent)
c744aeae
CH
1373 return &parent->kobj;
1374 return NULL;
1375}
da231fd5 1376
cebf8fd1
ML
1377static inline bool live_in_glue_dir(struct kobject *kobj,
1378 struct device *dev)
1379{
1380 if (!kobj || !dev->class ||
1381 kobj->kset != &dev->class->p->glue_dirs)
1382 return false;
1383 return true;
1384}
1385
1386static inline struct kobject *get_glue_dir(struct device *dev)
1387{
1388 return dev->kobj.parent;
1389}
1390
1391/*
1392 * make sure cleaning up dir as the last step, we need to make
1393 * sure .release handler of kobject is run with holding the
1394 * global lock
1395 */
63b6971a 1396static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
da231fd5 1397{
0f4dafc0 1398 /* see if we live in a "glue" directory */
cebf8fd1 1399 if (!live_in_glue_dir(glue_dir, dev))
da231fd5
KS
1400 return;
1401
e4a60d13 1402 mutex_lock(&gdp_mutex);
0f4dafc0 1403 kobject_put(glue_dir);
e4a60d13 1404 mutex_unlock(&gdp_mutex);
da231fd5 1405}
63b6971a 1406
2ee97caf
CH
1407static int device_add_class_symlinks(struct device *dev)
1408{
5590f319 1409 struct device_node *of_node = dev_of_node(dev);
2ee97caf
CH
1410 int error;
1411
5590f319
BH
1412 if (of_node) {
1413 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
1414 if (error)
1415 dev_warn(dev, "Error %d creating of_node link\n",error);
1416 /* An error here doesn't warrant bringing down the device */
1417 }
1418
2ee97caf
CH
1419 if (!dev->class)
1420 return 0;
da231fd5 1421
1fbfee6c 1422 error = sysfs_create_link(&dev->kobj,
6b6e39a6 1423 &dev->class->p->subsys.kobj,
2ee97caf
CH
1424 "subsystem");
1425 if (error)
5590f319 1426 goto out_devnode;
da231fd5 1427
4e886c29 1428 if (dev->parent && device_is_not_partition(dev)) {
39aba963 1429 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
4f01a757
DT
1430 "device");
1431 if (error)
39aba963 1432 goto out_subsys;
2ee97caf 1433 }
2ee97caf 1434
ead454fe 1435#ifdef CONFIG_BLOCK
39aba963 1436 /* /sys/block has directories and does not need symlinks */
e52eec13 1437 if (sysfs_deprecated && dev->class == &block_class)
39aba963 1438 return 0;
ead454fe 1439#endif
39aba963 1440
da231fd5 1441 /* link in the class directory pointing to the device */
6b6e39a6 1442 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1e0b2cf9 1443 &dev->kobj, dev_name(dev));
da231fd5 1444 if (error)
39aba963 1445 goto out_device;
da231fd5 1446
da231fd5
KS
1447 return 0;
1448
39aba963
KS
1449out_device:
1450 sysfs_remove_link(&dev->kobj, "device");
da231fd5 1451
2ee97caf
CH
1452out_subsys:
1453 sysfs_remove_link(&dev->kobj, "subsystem");
5590f319
BH
1454out_devnode:
1455 sysfs_remove_link(&dev->kobj, "of_node");
2ee97caf
CH
1456 return error;
1457}
1458
1459static void device_remove_class_symlinks(struct device *dev)
1460{
5590f319
BH
1461 if (dev_of_node(dev))
1462 sysfs_remove_link(&dev->kobj, "of_node");
1463
2ee97caf
CH
1464 if (!dev->class)
1465 return;
da231fd5 1466
4e886c29 1467 if (dev->parent && device_is_not_partition(dev))
da231fd5 1468 sysfs_remove_link(&dev->kobj, "device");
2ee97caf 1469 sysfs_remove_link(&dev->kobj, "subsystem");
ead454fe 1470#ifdef CONFIG_BLOCK
e52eec13 1471 if (sysfs_deprecated && dev->class == &block_class)
39aba963 1472 return;
ead454fe 1473#endif
6b6e39a6 1474 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2ee97caf
CH
1475}
1476
413c239f
SR
1477/**
1478 * dev_set_name - set a device name
1479 * @dev: device
46232366 1480 * @fmt: format string for the device's name
413c239f
SR
1481 */
1482int dev_set_name(struct device *dev, const char *fmt, ...)
1483{
1484 va_list vargs;
1fa5ae85 1485 int err;
413c239f
SR
1486
1487 va_start(vargs, fmt);
1fa5ae85 1488 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
413c239f 1489 va_end(vargs);
1fa5ae85 1490 return err;
413c239f
SR
1491}
1492EXPORT_SYMBOL_GPL(dev_set_name);
1493
e105b8bf
DW
1494/**
1495 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1496 * @dev: device
1497 *
1498 * By default we select char/ for new entries. Setting class->dev_obj
1499 * to NULL prevents an entry from being created. class->dev_kobj must
1500 * be set (or cleared) before any devices are registered to the class
1501 * otherwise device_create_sys_dev_entry() and
0d4e293c
PK
1502 * device_remove_sys_dev_entry() will disagree about the presence of
1503 * the link.
e105b8bf
DW
1504 */
1505static struct kobject *device_to_dev_kobj(struct device *dev)
1506{
1507 struct kobject *kobj;
1508
1509 if (dev->class)
1510 kobj = dev->class->dev_kobj;
1511 else
1512 kobj = sysfs_dev_char_kobj;
1513
1514 return kobj;
1515}
1516
1517static int device_create_sys_dev_entry(struct device *dev)
1518{
1519 struct kobject *kobj = device_to_dev_kobj(dev);
1520 int error = 0;
1521 char devt_str[15];
1522
1523 if (kobj) {
1524 format_dev_t(devt_str, dev->devt);
1525 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1526 }
1527
1528 return error;
1529}
1530
1531static void device_remove_sys_dev_entry(struct device *dev)
1532{
1533 struct kobject *kobj = device_to_dev_kobj(dev);
1534 char devt_str[15];
1535
1536 if (kobj) {
1537 format_dev_t(devt_str, dev->devt);
1538 sysfs_remove_link(kobj, devt_str);
1539 }
1540}
1541
b4028437
GKH
1542int device_private_init(struct device *dev)
1543{
1544 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1545 if (!dev->p)
1546 return -ENOMEM;
1547 dev->p->device = dev;
1548 klist_init(&dev->p->klist_children, klist_children_get,
1549 klist_children_put);
ef8a3fd6 1550 INIT_LIST_HEAD(&dev->p->deferred_probe);
b4028437
GKH
1551 return 0;
1552}
1553
1da177e4 1554/**
4a3ad20c
GKH
1555 * device_add - add device to device hierarchy.
1556 * @dev: device.
1da177e4 1557 *
4a3ad20c
GKH
1558 * This is part 2 of device_register(), though may be called
1559 * separately _iff_ device_initialize() has been called separately.
1da177e4 1560 *
5739411a 1561 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
4a3ad20c
GKH
1562 * to the global and sibling lists for the device, then
1563 * adds it to the other relevant subsystems of the driver model.
5739411a 1564 *
b10d5efd
AS
1565 * Do not call this routine or device_register() more than once for
1566 * any device structure. The driver model core is not designed to work
1567 * with devices that get unregistered and then spring back to life.
1568 * (Among other things, it's very hard to guarantee that all references
1569 * to the previous incarnation of @dev have been dropped.) Allocate
1570 * and register a fresh new struct device instead.
1571 *
5739411a
CH
1572 * NOTE: _Never_ directly free @dev after calling this function, even
1573 * if it returned an error! Always use put_device() to give up your
1574 * reference instead.
1da177e4
LT
1575 */
1576int device_add(struct device *dev)
1577{
1578 struct device *parent = NULL;
ca22e56d 1579 struct kobject *kobj;
c47ed219 1580 struct class_interface *class_intf;
c906a48a 1581 int error = -EINVAL;
cebf8fd1 1582 struct kobject *glue_dir = NULL;
775b64d2 1583
1da177e4 1584 dev = get_device(dev);
c906a48a
GKH
1585 if (!dev)
1586 goto done;
1587
fb069a5d 1588 if (!dev->p) {
b4028437
GKH
1589 error = device_private_init(dev);
1590 if (error)
1591 goto done;
fb069a5d 1592 }
fb069a5d 1593
1fa5ae85
KS
1594 /*
1595 * for statically allocated devices, which should all be converted
1596 * some day, we need to initialize the name. We prevent reading back
1597 * the name, and force the use of dev_name()
1598 */
1599 if (dev->init_name) {
acc0e90f 1600 dev_set_name(dev, "%s", dev->init_name);
1fa5ae85
KS
1601 dev->init_name = NULL;
1602 }
c906a48a 1603
ca22e56d
KS
1604 /* subsystems can specify simple device enumeration */
1605 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1606 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1607
e6309e75
TG
1608 if (!dev_name(dev)) {
1609 error = -EINVAL;
5c8563d7 1610 goto name_error;
e6309e75 1611 }
1da177e4 1612
1e0b2cf9 1613 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
c205ef48 1614
1da177e4 1615 parent = get_device(dev->parent);
ca22e56d
KS
1616 kobj = get_device_parent(dev, parent);
1617 if (kobj)
1618 dev->kobj.parent = kobj;
1da177e4 1619
0d358f22 1620 /* use parent numa_node */
56f2de81 1621 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
0d358f22
YL
1622 set_dev_node(dev, dev_to_node(parent));
1623
1da177e4 1624 /* first, register with generic layer. */
8a577ffc
KS
1625 /* we require the name to be set before, and pass NULL */
1626 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
cebf8fd1
ML
1627 if (error) {
1628 glue_dir = get_glue_dir(dev);
1da177e4 1629 goto Error;
cebf8fd1 1630 }
a7fd6706 1631
37022644
BW
1632 /* notify platform of device entry */
1633 if (platform_notify)
1634 platform_notify(dev);
1635
c5e064a6 1636 error = device_create_file(dev, &dev_attr_uevent);
a306eea4
CH
1637 if (error)
1638 goto attrError;
a7fd6706 1639
2ee97caf
CH
1640 error = device_add_class_symlinks(dev);
1641 if (error)
1642 goto SymlinkError;
dc0afa83
CH
1643 error = device_add_attrs(dev);
1644 if (error)
2620efef 1645 goto AttrsError;
dc0afa83
CH
1646 error = bus_add_device(dev);
1647 if (error)
1da177e4 1648 goto BusError;
3b98aeaf 1649 error = dpm_sysfs_add(dev);
57eee3d2 1650 if (error)
3b98aeaf
AS
1651 goto DPMError;
1652 device_pm_add(dev);
ec0676ee 1653
0cd75047
SK
1654 if (MAJOR(dev->devt)) {
1655 error = device_create_file(dev, &dev_attr_dev);
1656 if (error)
1657 goto DevAttrError;
1658
1659 error = device_create_sys_dev_entry(dev);
1660 if (error)
1661 goto SysEntryError;
1662
1663 devtmpfs_create_node(dev);
1664 }
1665
ec0676ee 1666 /* Notify clients of device addition. This call must come
268863f4 1667 * after dpm_sysfs_add() and before kobject_uevent().
ec0676ee
AS
1668 */
1669 if (dev->bus)
1670 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1671 BUS_NOTIFY_ADD_DEVICE, dev);
1672
83b5fb4c 1673 kobject_uevent(&dev->kobj, KOBJ_ADD);
2023c610 1674 bus_probe_device(dev);
1da177e4 1675 if (parent)
f791b8c8
GKH
1676 klist_add_tail(&dev->p->knode_parent,
1677 &parent->p->klist_children);
1da177e4 1678
5d9fd169 1679 if (dev->class) {
ca22e56d 1680 mutex_lock(&dev->class->p->mutex);
c47ed219 1681 /* tie the class to the device */
5a3ceb86 1682 klist_add_tail(&dev->knode_class,
6b6e39a6 1683 &dev->class->p->klist_devices);
c47ed219
GKH
1684
1685 /* notify any interfaces that the device is here */
184f1f77 1686 list_for_each_entry(class_intf,
ca22e56d 1687 &dev->class->p->interfaces, node)
c47ed219
GKH
1688 if (class_intf->add_dev)
1689 class_intf->add_dev(dev, class_intf);
ca22e56d 1690 mutex_unlock(&dev->class->p->mutex);
5d9fd169 1691 }
c906a48a 1692done:
1da177e4
LT
1693 put_device(dev);
1694 return error;
0cd75047
SK
1695 SysEntryError:
1696 if (MAJOR(dev->devt))
1697 device_remove_file(dev, &dev_attr_dev);
1698 DevAttrError:
1699 device_pm_remove(dev);
1700 dpm_sysfs_remove(dev);
3b98aeaf 1701 DPMError:
57eee3d2
RW
1702 bus_remove_device(dev);
1703 BusError:
82f0cf9b 1704 device_remove_attrs(dev);
2620efef 1705 AttrsError:
2ee97caf
CH
1706 device_remove_class_symlinks(dev);
1707 SymlinkError:
c5e064a6 1708 device_remove_file(dev, &dev_attr_uevent);
23681e47 1709 attrError:
312c004d 1710 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
cebf8fd1 1711 glue_dir = get_glue_dir(dev);
1da177e4
LT
1712 kobject_del(&dev->kobj);
1713 Error:
cebf8fd1 1714 cleanup_glue_dir(dev, glue_dir);
5f0163a5 1715 put_device(parent);
5c8563d7
KS
1716name_error:
1717 kfree(dev->p);
1718 dev->p = NULL;
c906a48a 1719 goto done;
1da177e4 1720}
86df2687 1721EXPORT_SYMBOL_GPL(device_add);
1da177e4 1722
1da177e4 1723/**
4a3ad20c
GKH
1724 * device_register - register a device with the system.
1725 * @dev: pointer to the device structure
1da177e4 1726 *
4a3ad20c
GKH
1727 * This happens in two clean steps - initialize the device
1728 * and add it to the system. The two steps can be called
1729 * separately, but this is the easiest and most common.
1730 * I.e. you should only call the two helpers separately if
1731 * have a clearly defined need to use and refcount the device
1732 * before it is added to the hierarchy.
5739411a 1733 *
b10d5efd
AS
1734 * For more information, see the kerneldoc for device_initialize()
1735 * and device_add().
1736 *
5739411a
CH
1737 * NOTE: _Never_ directly free @dev after calling this function, even
1738 * if it returned an error! Always use put_device() to give up the
1739 * reference initialized in this function instead.
1da177e4 1740 */
1da177e4
LT
1741int device_register(struct device *dev)
1742{
1743 device_initialize(dev);
1744 return device_add(dev);
1745}
86df2687 1746EXPORT_SYMBOL_GPL(device_register);
1da177e4 1747
1da177e4 1748/**
4a3ad20c
GKH
1749 * get_device - increment reference count for device.
1750 * @dev: device.
1da177e4 1751 *
4a3ad20c
GKH
1752 * This simply forwards the call to kobject_get(), though
1753 * we do take care to provide for the case that we get a NULL
1754 * pointer passed in.
1da177e4 1755 */
4a3ad20c 1756struct device *get_device(struct device *dev)
1da177e4 1757{
b0d1f807 1758 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1da177e4 1759}
86df2687 1760EXPORT_SYMBOL_GPL(get_device);
1da177e4 1761
1da177e4 1762/**
4a3ad20c
GKH
1763 * put_device - decrement reference count.
1764 * @dev: device in question.
1da177e4 1765 */
4a3ad20c 1766void put_device(struct device *dev)
1da177e4 1767{
edfaa7c3 1768 /* might_sleep(); */
1da177e4
LT
1769 if (dev)
1770 kobject_put(&dev->kobj);
1771}
86df2687 1772EXPORT_SYMBOL_GPL(put_device);
1da177e4 1773
1da177e4 1774/**
4a3ad20c
GKH
1775 * device_del - delete device from system.
1776 * @dev: device.
1da177e4 1777 *
4a3ad20c
GKH
1778 * This is the first part of the device unregistration
1779 * sequence. This removes the device from the lists we control
1780 * from here, has it removed from the other driver model
1781 * subsystems it was added to in device_add(), and removes it
1782 * from the kobject hierarchy.
1da177e4 1783 *
4a3ad20c
GKH
1784 * NOTE: this should be called manually _iff_ device_add() was
1785 * also called manually.
1da177e4 1786 */
4a3ad20c 1787void device_del(struct device *dev)
1da177e4 1788{
4a3ad20c 1789 struct device *parent = dev->parent;
cebf8fd1 1790 struct kobject *glue_dir = NULL;
c47ed219 1791 struct class_interface *class_intf;
1da177e4 1792
ec0676ee
AS
1793 /* Notify clients of device removal. This call must come
1794 * before dpm_sysfs_remove().
1795 */
1796 if (dev->bus)
1797 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1798 BUS_NOTIFY_DEL_DEVICE, dev);
9ed98953
RW
1799
1800 device_links_purge(dev);
3b98aeaf 1801 dpm_sysfs_remove(dev);
1da177e4 1802 if (parent)
f791b8c8 1803 klist_del(&dev->p->knode_parent);
e105b8bf 1804 if (MAJOR(dev->devt)) {
2b2af54a 1805 devtmpfs_delete_node(dev);
e105b8bf 1806 device_remove_sys_dev_entry(dev);
c5e064a6 1807 device_remove_file(dev, &dev_attr_dev);
e105b8bf 1808 }
b9d9c82b 1809 if (dev->class) {
da231fd5 1810 device_remove_class_symlinks(dev);
99ef3ef8 1811
ca22e56d 1812 mutex_lock(&dev->class->p->mutex);
c47ed219 1813 /* notify any interfaces that the device is now gone */
184f1f77 1814 list_for_each_entry(class_intf,
ca22e56d 1815 &dev->class->p->interfaces, node)
c47ed219
GKH
1816 if (class_intf->remove_dev)
1817 class_intf->remove_dev(dev, class_intf);
1818 /* remove the device from the class list */
5a3ceb86 1819 klist_del(&dev->knode_class);
ca22e56d 1820 mutex_unlock(&dev->class->p->mutex);
b9d9c82b 1821 }
c5e064a6 1822 device_remove_file(dev, &dev_attr_uevent);
2620efef 1823 device_remove_attrs(dev);
28953533 1824 bus_remove_device(dev);
4b6d1f12 1825 device_pm_remove(dev);
d1c3414c 1826 driver_deferred_probe_del(dev);
478573c9 1827 device_remove_properties(dev);
1da177e4
LT
1828
1829 /* Notify the platform of the removal, in case they
1830 * need to do anything...
1831 */
1832 if (platform_notify_remove)
1833 platform_notify_remove(dev);
599bad38
JR
1834 if (dev->bus)
1835 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1836 BUS_NOTIFY_REMOVED_DEVICE, dev);
312c004d 1837 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
cebf8fd1 1838 glue_dir = get_glue_dir(dev);
1da177e4 1839 kobject_del(&dev->kobj);
cebf8fd1 1840 cleanup_glue_dir(dev, glue_dir);
da231fd5 1841 put_device(parent);
1da177e4 1842}
86df2687 1843EXPORT_SYMBOL_GPL(device_del);
1da177e4
LT
1844
1845/**
4a3ad20c
GKH
1846 * device_unregister - unregister device from system.
1847 * @dev: device going away.
1da177e4 1848 *
4a3ad20c
GKH
1849 * We do this in two parts, like we do device_register(). First,
1850 * we remove it from all the subsystems with device_del(), then
1851 * we decrement the reference count via put_device(). If that
1852 * is the final reference count, the device will be cleaned up
1853 * via device_release() above. Otherwise, the structure will
1854 * stick around until the final reference to the device is dropped.
1da177e4 1855 */
4a3ad20c 1856void device_unregister(struct device *dev)
1da177e4 1857{
1e0b2cf9 1858 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1da177e4
LT
1859 device_del(dev);
1860 put_device(dev);
1861}
86df2687 1862EXPORT_SYMBOL_GPL(device_unregister);
1da177e4 1863
3d060aeb
AS
1864static struct device *prev_device(struct klist_iter *i)
1865{
1866 struct klist_node *n = klist_prev(i);
1867 struct device *dev = NULL;
1868 struct device_private *p;
1869
1870 if (n) {
1871 p = to_device_private_parent(n);
1872 dev = p->device;
1873 }
1874 return dev;
1875}
1876
4a3ad20c 1877static struct device *next_device(struct klist_iter *i)
36239577 1878{
4a3ad20c 1879 struct klist_node *n = klist_next(i);
f791b8c8
GKH
1880 struct device *dev = NULL;
1881 struct device_private *p;
1882
1883 if (n) {
1884 p = to_device_private_parent(n);
1885 dev = p->device;
1886 }
1887 return dev;
36239577
PM
1888}
1889
6fcf53ac 1890/**
e454cea2 1891 * device_get_devnode - path of device node file
6fcf53ac 1892 * @dev: device
e454cea2 1893 * @mode: returned file access mode
3c2670e6
KS
1894 * @uid: returned file owner
1895 * @gid: returned file group
6fcf53ac
KS
1896 * @tmp: possibly allocated string
1897 *
1898 * Return the relative path of a possible device node.
1899 * Non-default names may need to allocate a memory to compose
1900 * a name. This memory is returned in tmp and needs to be
1901 * freed by the caller.
1902 */
e454cea2 1903const char *device_get_devnode(struct device *dev,
4e4098a3 1904 umode_t *mode, kuid_t *uid, kgid_t *gid,
3c2670e6 1905 const char **tmp)
6fcf53ac
KS
1906{
1907 char *s;
1908
1909 *tmp = NULL;
1910
1911 /* the device type may provide a specific name */
e454cea2 1912 if (dev->type && dev->type->devnode)
3c2670e6 1913 *tmp = dev->type->devnode(dev, mode, uid, gid);
6fcf53ac
KS
1914 if (*tmp)
1915 return *tmp;
1916
1917 /* the class may provide a specific name */
e454cea2
KS
1918 if (dev->class && dev->class->devnode)
1919 *tmp = dev->class->devnode(dev, mode);
6fcf53ac
KS
1920 if (*tmp)
1921 return *tmp;
1922
1923 /* return name without allocation, tmp == NULL */
1924 if (strchr(dev_name(dev), '!') == NULL)
1925 return dev_name(dev);
1926
1927 /* replace '!' in the name with '/' */
a29fd614
RV
1928 s = kstrdup(dev_name(dev), GFP_KERNEL);
1929 if (!s)
6fcf53ac 1930 return NULL;
a29fd614
RV
1931 strreplace(s, '!', '/');
1932 return *tmp = s;
6fcf53ac
KS
1933}
1934
1da177e4 1935/**
4a3ad20c
GKH
1936 * device_for_each_child - device child iterator.
1937 * @parent: parent struct device.
4a3ad20c 1938 * @fn: function to be called for each device.
f8878dcb 1939 * @data: data for the callback.
1da177e4 1940 *
4a3ad20c
GKH
1941 * Iterate over @parent's child devices, and call @fn for each,
1942 * passing it @data.
1da177e4 1943 *
4a3ad20c
GKH
1944 * We check the return of @fn each time. If it returns anything
1945 * other than 0, we break out and return that value.
1da177e4 1946 */
4a3ad20c
GKH
1947int device_for_each_child(struct device *parent, void *data,
1948 int (*fn)(struct device *dev, void *data))
1da177e4 1949{
36239577 1950 struct klist_iter i;
4a3ad20c 1951 struct device *child;
1da177e4
LT
1952 int error = 0;
1953
014c90db
GKH
1954 if (!parent->p)
1955 return 0;
1956
f791b8c8 1957 klist_iter_init(&parent->p->klist_children, &i);
36239577
PM
1958 while ((child = next_device(&i)) && !error)
1959 error = fn(child, data);
1960 klist_iter_exit(&i);
1da177e4
LT
1961 return error;
1962}
86df2687 1963EXPORT_SYMBOL_GPL(device_for_each_child);
1da177e4 1964
3d060aeb
AS
1965/**
1966 * device_for_each_child_reverse - device child iterator in reversed order.
1967 * @parent: parent struct device.
1968 * @fn: function to be called for each device.
1969 * @data: data for the callback.
1970 *
1971 * Iterate over @parent's child devices, and call @fn for each,
1972 * passing it @data.
1973 *
1974 * We check the return of @fn each time. If it returns anything
1975 * other than 0, we break out and return that value.
1976 */
1977int device_for_each_child_reverse(struct device *parent, void *data,
1978 int (*fn)(struct device *dev, void *data))
1979{
1980 struct klist_iter i;
1981 struct device *child;
1982 int error = 0;
1983
1984 if (!parent->p)
1985 return 0;
1986
1987 klist_iter_init(&parent->p->klist_children, &i);
1988 while ((child = prev_device(&i)) && !error)
1989 error = fn(child, data);
1990 klist_iter_exit(&i);
1991 return error;
1992}
1993EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
1994
5ab69981
CH
1995/**
1996 * device_find_child - device iterator for locating a particular device.
1997 * @parent: parent struct device
5ab69981 1998 * @match: Callback function to check device
f8878dcb 1999 * @data: Data to pass to match function
5ab69981
CH
2000 *
2001 * This is similar to the device_for_each_child() function above, but it
2002 * returns a reference to a device that is 'found' for later use, as
2003 * determined by the @match callback.
2004 *
2005 * The callback should return 0 if the device doesn't match and non-zero
2006 * if it does. If the callback returns non-zero and a reference to the
2007 * current device can be obtained, this function will return to the caller
2008 * and not iterate over any more devices.
a4e2400a
FV
2009 *
2010 * NOTE: you will need to drop the reference with put_device() after use.
5ab69981 2011 */
4a3ad20c
GKH
2012struct device *device_find_child(struct device *parent, void *data,
2013 int (*match)(struct device *dev, void *data))
5ab69981
CH
2014{
2015 struct klist_iter i;
2016 struct device *child;
2017
2018 if (!parent)
2019 return NULL;
2020
f791b8c8 2021 klist_iter_init(&parent->p->klist_children, &i);
5ab69981
CH
2022 while ((child = next_device(&i)))
2023 if (match(child, data) && get_device(child))
2024 break;
2025 klist_iter_exit(&i);
2026 return child;
2027}
86df2687 2028EXPORT_SYMBOL_GPL(device_find_child);
5ab69981 2029
1da177e4
LT
2030int __init devices_init(void)
2031{
881c6cfd
GKH
2032 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2033 if (!devices_kset)
2034 return -ENOMEM;
e105b8bf
DW
2035 dev_kobj = kobject_create_and_add("dev", NULL);
2036 if (!dev_kobj)
2037 goto dev_kobj_err;
2038 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2039 if (!sysfs_dev_block_kobj)
2040 goto block_kobj_err;
2041 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2042 if (!sysfs_dev_char_kobj)
2043 goto char_kobj_err;
2044
881c6cfd 2045 return 0;
e105b8bf
DW
2046
2047 char_kobj_err:
2048 kobject_put(sysfs_dev_block_kobj);
2049 block_kobj_err:
2050 kobject_put(dev_kobj);
2051 dev_kobj_err:
2052 kset_unregister(devices_kset);
2053 return -ENOMEM;
1da177e4
LT
2054}
2055
4f3549d7
RW
2056static int device_check_offline(struct device *dev, void *not_used)
2057{
2058 int ret;
2059
2060 ret = device_for_each_child(dev, NULL, device_check_offline);
2061 if (ret)
2062 return ret;
2063
2064 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2065}
2066
2067/**
2068 * device_offline - Prepare the device for hot-removal.
2069 * @dev: Device to be put offline.
2070 *
2071 * Execute the device bus type's .offline() callback, if present, to prepare
2072 * the device for a subsequent hot-removal. If that succeeds, the device must
2073 * not be used until either it is removed or its bus type's .online() callback
2074 * is executed.
2075 *
2076 * Call under device_hotplug_lock.
2077 */
2078int device_offline(struct device *dev)
2079{
2080 int ret;
2081
2082 if (dev->offline_disabled)
2083 return -EPERM;
2084
2085 ret = device_for_each_child(dev, NULL, device_check_offline);
2086 if (ret)
2087 return ret;
2088
2089 device_lock(dev);
2090 if (device_supports_offline(dev)) {
2091 if (dev->offline) {
2092 ret = 1;
2093 } else {
2094 ret = dev->bus->offline(dev);
2095 if (!ret) {
2096 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2097 dev->offline = true;
2098 }
2099 }
2100 }
2101 device_unlock(dev);
2102
2103 return ret;
2104}
2105
2106/**
2107 * device_online - Put the device back online after successful device_offline().
2108 * @dev: Device to be put back online.
2109 *
2110 * If device_offline() has been successfully executed for @dev, but the device
2111 * has not been removed subsequently, execute its bus type's .online() callback
2112 * to indicate that the device can be used again.
2113 *
2114 * Call under device_hotplug_lock.
2115 */
2116int device_online(struct device *dev)
2117{
2118 int ret = 0;
2119
2120 device_lock(dev);
2121 if (device_supports_offline(dev)) {
2122 if (dev->offline) {
2123 ret = dev->bus->online(dev);
2124 if (!ret) {
2125 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2126 dev->offline = false;
2127 }
2128 } else {
2129 ret = 1;
2130 }
2131 }
2132 device_unlock(dev);
2133
2134 return ret;
2135}
2136
7f100d15 2137struct root_device {
0aa0dc41
MM
2138 struct device dev;
2139 struct module *owner;
2140};
2141
93058424 2142static inline struct root_device *to_root_device(struct device *d)
481e2079
FW
2143{
2144 return container_of(d, struct root_device, dev);
2145}
0aa0dc41
MM
2146
2147static void root_device_release(struct device *dev)
2148{
2149 kfree(to_root_device(dev));
2150}
2151
2152/**
2153 * __root_device_register - allocate and register a root device
2154 * @name: root device name
2155 * @owner: owner module of the root device, usually THIS_MODULE
2156 *
2157 * This function allocates a root device and registers it
2158 * using device_register(). In order to free the returned
2159 * device, use root_device_unregister().
2160 *
2161 * Root devices are dummy devices which allow other devices
2162 * to be grouped under /sys/devices. Use this function to
2163 * allocate a root device and then use it as the parent of
2164 * any device which should appear under /sys/devices/{name}
2165 *
2166 * The /sys/devices/{name} directory will also contain a
2167 * 'module' symlink which points to the @owner directory
2168 * in sysfs.
2169 *
f0eae0ed
JN
2170 * Returns &struct device pointer on success, or ERR_PTR() on error.
2171 *
0aa0dc41
MM
2172 * Note: You probably want to use root_device_register().
2173 */
2174struct device *__root_device_register(const char *name, struct module *owner)
2175{
2176 struct root_device *root;
2177 int err = -ENOMEM;
2178
2179 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2180 if (!root)
2181 return ERR_PTR(err);
2182
acc0e90f 2183 err = dev_set_name(&root->dev, "%s", name);
0aa0dc41
MM
2184 if (err) {
2185 kfree(root);
2186 return ERR_PTR(err);
2187 }
2188
2189 root->dev.release = root_device_release;
2190
2191 err = device_register(&root->dev);
2192 if (err) {
2193 put_device(&root->dev);
2194 return ERR_PTR(err);
2195 }
2196
1d9e882b 2197#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
0aa0dc41
MM
2198 if (owner) {
2199 struct module_kobject *mk = &owner->mkobj;
2200
2201 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2202 if (err) {
2203 device_unregister(&root->dev);
2204 return ERR_PTR(err);
2205 }
2206 root->owner = owner;
2207 }
2208#endif
2209
2210 return &root->dev;
2211}
2212EXPORT_SYMBOL_GPL(__root_device_register);
2213
2214/**
2215 * root_device_unregister - unregister and free a root device
7cbcf225 2216 * @dev: device going away
0aa0dc41
MM
2217 *
2218 * This function unregisters and cleans up a device that was created by
2219 * root_device_register().
2220 */
2221void root_device_unregister(struct device *dev)
2222{
2223 struct root_device *root = to_root_device(dev);
2224
2225 if (root->owner)
2226 sysfs_remove_link(&root->dev.kobj, "module");
2227
2228 device_unregister(dev);
2229}
2230EXPORT_SYMBOL_GPL(root_device_unregister);
2231
23681e47
GKH
2232
2233static void device_create_release(struct device *dev)
2234{
1e0b2cf9 2235 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
23681e47
GKH
2236 kfree(dev);
2237}
2238
39ef3112
GR
2239static struct device *
2240device_create_groups_vargs(struct class *class, struct device *parent,
2241 dev_t devt, void *drvdata,
2242 const struct attribute_group **groups,
2243 const char *fmt, va_list args)
23681e47 2244{
23681e47
GKH
2245 struct device *dev = NULL;
2246 int retval = -ENODEV;
2247
2248 if (class == NULL || IS_ERR(class))
2249 goto error;
23681e47
GKH
2250
2251 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2252 if (!dev) {
2253 retval = -ENOMEM;
2254 goto error;
2255 }
2256
bbc780f8 2257 device_initialize(dev);
23681e47
GKH
2258 dev->devt = devt;
2259 dev->class = class;
2260 dev->parent = parent;
39ef3112 2261 dev->groups = groups;
23681e47 2262 dev->release = device_create_release;
8882b394 2263 dev_set_drvdata(dev, drvdata);
23681e47 2264
1fa5ae85
KS
2265 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2266 if (retval)
2267 goto error;
2268
bbc780f8 2269 retval = device_add(dev);
23681e47
GKH
2270 if (retval)
2271 goto error;
2272
23681e47
GKH
2273 return dev;
2274
2275error:
286661b3 2276 put_device(dev);
23681e47
GKH
2277 return ERR_PTR(retval);
2278}
39ef3112
GR
2279
2280/**
2281 * device_create_vargs - creates a device and registers it with sysfs
2282 * @class: pointer to the struct class that this device should be registered to
2283 * @parent: pointer to the parent struct device of this new device, if any
2284 * @devt: the dev_t for the char device to be added
2285 * @drvdata: the data to be added to the device for callbacks
2286 * @fmt: string for the device's name
2287 * @args: va_list for the device's name
2288 *
2289 * This function can be used by char device classes. A struct device
2290 * will be created in sysfs, registered to the specified class.
2291 *
2292 * A "dev" file will be created, showing the dev_t for the device, if
2293 * the dev_t is not 0,0.
2294 * If a pointer to a parent struct device is passed in, the newly created
2295 * struct device will be a child of that device in sysfs.
2296 * The pointer to the struct device will be returned from the call.
2297 * Any further sysfs files that might be required can be created using this
2298 * pointer.
2299 *
2300 * Returns &struct device pointer on success, or ERR_PTR() on error.
2301 *
2302 * Note: the struct class passed to this function must have previously
2303 * been created with a call to class_create().
2304 */
2305struct device *device_create_vargs(struct class *class, struct device *parent,
2306 dev_t devt, void *drvdata, const char *fmt,
2307 va_list args)
2308{
2309 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2310 fmt, args);
2311}
8882b394
GKH
2312EXPORT_SYMBOL_GPL(device_create_vargs);
2313
2314/**
4e106739 2315 * device_create - creates a device and registers it with sysfs
8882b394
GKH
2316 * @class: pointer to the struct class that this device should be registered to
2317 * @parent: pointer to the parent struct device of this new device, if any
2318 * @devt: the dev_t for the char device to be added
2319 * @drvdata: the data to be added to the device for callbacks
2320 * @fmt: string for the device's name
2321 *
2322 * This function can be used by char device classes. A struct device
2323 * will be created in sysfs, registered to the specified class.
2324 *
2325 * A "dev" file will be created, showing the dev_t for the device, if
2326 * the dev_t is not 0,0.
2327 * If a pointer to a parent struct device is passed in, the newly created
2328 * struct device will be a child of that device in sysfs.
2329 * The pointer to the struct device will be returned from the call.
2330 * Any further sysfs files that might be required can be created using this
2331 * pointer.
2332 *
f0eae0ed
JN
2333 * Returns &struct device pointer on success, or ERR_PTR() on error.
2334 *
8882b394
GKH
2335 * Note: the struct class passed to this function must have previously
2336 * been created with a call to class_create().
2337 */
4e106739
GKH
2338struct device *device_create(struct class *class, struct device *parent,
2339 dev_t devt, void *drvdata, const char *fmt, ...)
8882b394
GKH
2340{
2341 va_list vargs;
2342 struct device *dev;
2343
2344 va_start(vargs, fmt);
2345 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2346 va_end(vargs);
2347 return dev;
2348}
4e106739 2349EXPORT_SYMBOL_GPL(device_create);
8882b394 2350
39ef3112
GR
2351/**
2352 * device_create_with_groups - creates a device and registers it with sysfs
2353 * @class: pointer to the struct class that this device should be registered to
2354 * @parent: pointer to the parent struct device of this new device, if any
2355 * @devt: the dev_t for the char device to be added
2356 * @drvdata: the data to be added to the device for callbacks
2357 * @groups: NULL-terminated list of attribute groups to be created
2358 * @fmt: string for the device's name
2359 *
2360 * This function can be used by char device classes. A struct device
2361 * will be created in sysfs, registered to the specified class.
2362 * Additional attributes specified in the groups parameter will also
2363 * be created automatically.
2364 *
2365 * A "dev" file will be created, showing the dev_t for the device, if
2366 * the dev_t is not 0,0.
2367 * If a pointer to a parent struct device is passed in, the newly created
2368 * struct device will be a child of that device in sysfs.
2369 * The pointer to the struct device will be returned from the call.
2370 * Any further sysfs files that might be required can be created using this
2371 * pointer.
2372 *
2373 * Returns &struct device pointer on success, or ERR_PTR() on error.
2374 *
2375 * Note: the struct class passed to this function must have previously
2376 * been created with a call to class_create().
2377 */
2378struct device *device_create_with_groups(struct class *class,
2379 struct device *parent, dev_t devt,
2380 void *drvdata,
2381 const struct attribute_group **groups,
2382 const char *fmt, ...)
2383{
2384 va_list vargs;
2385 struct device *dev;
2386
2387 va_start(vargs, fmt);
2388 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2389 fmt, vargs);
2390 va_end(vargs);
2391 return dev;
2392}
2393EXPORT_SYMBOL_GPL(device_create_with_groups);
2394
9f3b795a 2395static int __match_devt(struct device *dev, const void *data)
23681e47 2396{
9f3b795a 2397 const dev_t *devt = data;
23681e47 2398
cd35449b 2399 return dev->devt == *devt;
775b64d2
RW
2400}
2401
2402/**
2403 * device_destroy - removes a device that was created with device_create()
2404 * @class: pointer to the struct class that this device was registered with
2405 * @devt: the dev_t of the device that was previously registered
2406 *
2407 * This call unregisters and cleans up a device that was created with a
2408 * call to device_create().
2409 */
2410void device_destroy(struct class *class, dev_t devt)
2411{
2412 struct device *dev;
23681e47 2413
695794ae 2414 dev = class_find_device(class, NULL, &devt, __match_devt);
cd35449b
DY
2415 if (dev) {
2416 put_device(dev);
23681e47 2417 device_unregister(dev);
cd35449b 2418 }
23681e47
GKH
2419}
2420EXPORT_SYMBOL_GPL(device_destroy);
a2de48ca
GKH
2421
2422/**
2423 * device_rename - renames a device
2424 * @dev: the pointer to the struct device to be renamed
2425 * @new_name: the new name of the device
030c1d2b
EB
2426 *
2427 * It is the responsibility of the caller to provide mutual
2428 * exclusion between two different calls of device_rename
2429 * on the same device to ensure that new_name is valid and
2430 * won't conflict with other devices.
c6c0ac66 2431 *
a5462516
TT
2432 * Note: Don't call this function. Currently, the networking layer calls this
2433 * function, but that will change. The following text from Kay Sievers offers
2434 * some insight:
2435 *
2436 * Renaming devices is racy at many levels, symlinks and other stuff are not
2437 * replaced atomically, and you get a "move" uevent, but it's not easy to
2438 * connect the event to the old and new device. Device nodes are not renamed at
2439 * all, there isn't even support for that in the kernel now.
2440 *
2441 * In the meantime, during renaming, your target name might be taken by another
2442 * driver, creating conflicts. Or the old name is taken directly after you
2443 * renamed it -- then you get events for the same DEVPATH, before you even see
2444 * the "move" event. It's just a mess, and nothing new should ever rely on
2445 * kernel device renaming. Besides that, it's not even implemented now for
2446 * other things than (driver-core wise very simple) network devices.
2447 *
2448 * We are currently about to change network renaming in udev to completely
2449 * disallow renaming of devices in the same namespace as the kernel uses,
2450 * because we can't solve the problems properly, that arise with swapping names
2451 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2452 * be allowed to some other name than eth[0-9]*, for the aforementioned
2453 * reasons.
2454 *
2455 * Make up a "real" name in the driver before you register anything, or add
2456 * some other attributes for userspace to find the device, or use udev to add
2457 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2458 * don't even want to get into that and try to implement the missing pieces in
2459 * the core. We really have other pieces to fix in the driver core mess. :)
a2de48ca 2460 */
6937e8f8 2461int device_rename(struct device *dev, const char *new_name)
a2de48ca 2462{
4b30ee58 2463 struct kobject *kobj = &dev->kobj;
2ee97caf 2464 char *old_device_name = NULL;
a2de48ca
GKH
2465 int error;
2466
2467 dev = get_device(dev);
2468 if (!dev)
2469 return -EINVAL;
2470
69df7533 2471 dev_dbg(dev, "renaming to %s\n", new_name);
a2de48ca 2472
1fa5ae85 2473 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2ee97caf
CH
2474 if (!old_device_name) {
2475 error = -ENOMEM;
2476 goto out;
a2de48ca 2477 }
a2de48ca 2478
f349cf34 2479 if (dev->class) {
4b30ee58
TH
2480 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2481 kobj, old_device_name,
2482 new_name, kobject_namespace(kobj));
f349cf34
EB
2483 if (error)
2484 goto out;
2485 }
39aba963 2486
4b30ee58 2487 error = kobject_rename(kobj, new_name);
1fa5ae85 2488 if (error)
2ee97caf 2489 goto out;
a2de48ca 2490
2ee97caf 2491out:
a2de48ca
GKH
2492 put_device(dev);
2493
2ee97caf 2494 kfree(old_device_name);
a2de48ca
GKH
2495
2496 return error;
2497}
a2807dbc 2498EXPORT_SYMBOL_GPL(device_rename);
8a82472f
CH
2499
2500static int device_move_class_links(struct device *dev,
2501 struct device *old_parent,
2502 struct device *new_parent)
2503{
f7f3461d 2504 int error = 0;
8a82472f 2505
f7f3461d
GKH
2506 if (old_parent)
2507 sysfs_remove_link(&dev->kobj, "device");
2508 if (new_parent)
2509 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2510 "device");
2511 return error;
8a82472f
CH
2512}
2513
2514/**
2515 * device_move - moves a device to a new parent
2516 * @dev: the pointer to the struct device to be moved
c744aeae 2517 * @new_parent: the new parent of the device (can by NULL)
ffa6a705 2518 * @dpm_order: how to reorder the dpm_list
8a82472f 2519 */
ffa6a705
CH
2520int device_move(struct device *dev, struct device *new_parent,
2521 enum dpm_order dpm_order)
8a82472f
CH
2522{
2523 int error;
2524 struct device *old_parent;
c744aeae 2525 struct kobject *new_parent_kobj;
8a82472f
CH
2526
2527 dev = get_device(dev);
2528 if (!dev)
2529 return -EINVAL;
2530
ffa6a705 2531 device_pm_lock();
8a82472f 2532 new_parent = get_device(new_parent);
4a3ad20c 2533 new_parent_kobj = get_device_parent(dev, new_parent);
63b6971a 2534
1e0b2cf9
KS
2535 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2536 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
c744aeae 2537 error = kobject_move(&dev->kobj, new_parent_kobj);
8a82472f 2538 if (error) {
63b6971a 2539 cleanup_glue_dir(dev, new_parent_kobj);
8a82472f
CH
2540 put_device(new_parent);
2541 goto out;
2542 }
2543 old_parent = dev->parent;
2544 dev->parent = new_parent;
2545 if (old_parent)
f791b8c8 2546 klist_remove(&dev->p->knode_parent);
0d358f22 2547 if (new_parent) {
f791b8c8
GKH
2548 klist_add_tail(&dev->p->knode_parent,
2549 &new_parent->p->klist_children);
0d358f22
YL
2550 set_dev_node(dev, dev_to_node(new_parent));
2551 }
2552
bdd4034d
RV
2553 if (dev->class) {
2554 error = device_move_class_links(dev, old_parent, new_parent);
2555 if (error) {
2556 /* We ignore errors on cleanup since we're hosed anyway... */
2557 device_move_class_links(dev, new_parent, old_parent);
2558 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2559 if (new_parent)
2560 klist_remove(&dev->p->knode_parent);
2561 dev->parent = old_parent;
2562 if (old_parent) {
2563 klist_add_tail(&dev->p->knode_parent,
2564 &old_parent->p->klist_children);
2565 set_dev_node(dev, dev_to_node(old_parent));
2566 }
0d358f22 2567 }
bdd4034d
RV
2568 cleanup_glue_dir(dev, new_parent_kobj);
2569 put_device(new_parent);
2570 goto out;
8a82472f 2571 }
8a82472f 2572 }
ffa6a705
CH
2573 switch (dpm_order) {
2574 case DPM_ORDER_NONE:
2575 break;
2576 case DPM_ORDER_DEV_AFTER_PARENT:
2577 device_pm_move_after(dev, new_parent);
52cdbdd4 2578 devices_kset_move_after(dev, new_parent);
ffa6a705
CH
2579 break;
2580 case DPM_ORDER_PARENT_BEFORE_DEV:
2581 device_pm_move_before(new_parent, dev);
52cdbdd4 2582 devices_kset_move_before(new_parent, dev);
ffa6a705
CH
2583 break;
2584 case DPM_ORDER_DEV_LAST:
2585 device_pm_move_last(dev);
52cdbdd4 2586 devices_kset_move_last(dev);
ffa6a705
CH
2587 break;
2588 }
bdd4034d 2589
8a82472f
CH
2590 put_device(old_parent);
2591out:
ffa6a705 2592 device_pm_unlock();
8a82472f
CH
2593 put_device(dev);
2594 return error;
2595}
8a82472f 2596EXPORT_SYMBOL_GPL(device_move);
37b0c020
GKH
2597
2598/**
2599 * device_shutdown - call ->shutdown() on each device to shutdown.
2600 */
2601void device_shutdown(void)
2602{
f123db8e 2603 struct device *dev, *parent;
6245838f
HD
2604
2605 spin_lock(&devices_kset->list_lock);
2606 /*
2607 * Walk the devices list backward, shutting down each in turn.
2608 * Beware that device unplug events may also start pulling
2609 * devices offline, even as the system is shutting down.
2610 */
2611 while (!list_empty(&devices_kset->list)) {
2612 dev = list_entry(devices_kset->list.prev, struct device,
2613 kobj.entry);
d1c6c030
ML
2614
2615 /*
2616 * hold reference count of device's parent to
2617 * prevent it from being freed because parent's
2618 * lock is to be held
2619 */
f123db8e 2620 parent = get_device(dev->parent);
6245838f
HD
2621 get_device(dev);
2622 /*
2623 * Make sure the device is off the kset list, in the
2624 * event that dev->*->shutdown() doesn't remove it.
2625 */
2626 list_del_init(&dev->kobj.entry);
2627 spin_unlock(&devices_kset->list_lock);
fe6b91f4 2628
d1c6c030 2629 /* hold lock to avoid race with probe/release */
f123db8e
BL
2630 if (parent)
2631 device_lock(parent);
d1c6c030
ML
2632 device_lock(dev);
2633
fe6b91f4
AS
2634 /* Don't allow any more runtime suspends */
2635 pm_runtime_get_noresume(dev);
2636 pm_runtime_barrier(dev);
37b0c020 2637
37b0c020 2638 if (dev->bus && dev->bus->shutdown) {
0246c4fa
SL
2639 if (initcall_debug)
2640 dev_info(dev, "shutdown\n");
37b0c020
GKH
2641 dev->bus->shutdown(dev);
2642 } else if (dev->driver && dev->driver->shutdown) {
0246c4fa
SL
2643 if (initcall_debug)
2644 dev_info(dev, "shutdown\n");
37b0c020
GKH
2645 dev->driver->shutdown(dev);
2646 }
d1c6c030
ML
2647
2648 device_unlock(dev);
f123db8e
BL
2649 if (parent)
2650 device_unlock(parent);
d1c6c030 2651
6245838f 2652 put_device(dev);
f123db8e 2653 put_device(parent);
6245838f
HD
2654
2655 spin_lock(&devices_kset->list_lock);
37b0c020 2656 }
6245838f 2657 spin_unlock(&devices_kset->list_lock);
37b0c020 2658}
99bcf217
JP
2659
2660/*
2661 * Device logging functions
2662 */
2663
2664#ifdef CONFIG_PRINTK
666f355f
JP
2665static int
2666create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
99bcf217 2667{
c4e00daa 2668 const char *subsys;
798efc60 2669 size_t pos = 0;
99bcf217 2670
c4e00daa
KS
2671 if (dev->class)
2672 subsys = dev->class->name;
2673 else if (dev->bus)
2674 subsys = dev->bus->name;
2675 else
798efc60 2676 return 0;
c4e00daa 2677
798efc60 2678 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
655e5b7c
BH
2679 if (pos >= hdrlen)
2680 goto overflow;
c4e00daa
KS
2681
2682 /*
2683 * Add device identifier DEVICE=:
2684 * b12:8 block dev_t
2685 * c127:3 char dev_t
2686 * n8 netdev ifindex
2687 * +sound:card0 subsystem:devname
2688 */
2689 if (MAJOR(dev->devt)) {
2690 char c;
2691
2692 if (strcmp(subsys, "block") == 0)
2693 c = 'b';
2694 else
2695 c = 'c';
798efc60
JP
2696 pos++;
2697 pos += snprintf(hdr + pos, hdrlen - pos,
2698 "DEVICE=%c%u:%u",
2699 c, MAJOR(dev->devt), MINOR(dev->devt));
c4e00daa
KS
2700 } else if (strcmp(subsys, "net") == 0) {
2701 struct net_device *net = to_net_dev(dev);
2702
798efc60
JP
2703 pos++;
2704 pos += snprintf(hdr + pos, hdrlen - pos,
2705 "DEVICE=n%u", net->ifindex);
c4e00daa 2706 } else {
798efc60
JP
2707 pos++;
2708 pos += snprintf(hdr + pos, hdrlen - pos,
2709 "DEVICE=+%s:%s", subsys, dev_name(dev));
c4e00daa 2710 }
af7f2158 2711
655e5b7c
BH
2712 if (pos >= hdrlen)
2713 goto overflow;
2714
798efc60 2715 return pos;
655e5b7c
BH
2716
2717overflow:
2718 dev_WARN(dev, "device/subsystem name too long");
2719 return 0;
798efc60 2720}
798efc60 2721
05e4e5b8
JP
2722int dev_vprintk_emit(int level, const struct device *dev,
2723 const char *fmt, va_list args)
2724{
2725 char hdr[128];
2726 size_t hdrlen;
2727
2728 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2729
2730 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2731}
2732EXPORT_SYMBOL(dev_vprintk_emit);
2733
2734int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2735{
2736 va_list args;
2737 int r;
2738
2739 va_start(args, fmt);
2740
2741 r = dev_vprintk_emit(level, dev, fmt, args);
2742
2743 va_end(args);
2744
2745 return r;
2746}
2747EXPORT_SYMBOL(dev_printk_emit);
2748
d1f1052c 2749static void __dev_printk(const char *level, const struct device *dev,
798efc60
JP
2750 struct va_format *vaf)
2751{
d1f1052c
JP
2752 if (dev)
2753 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2754 dev_driver_string(dev), dev_name(dev), vaf);
2755 else
2756 printk("%s(NULL device *): %pV", level, vaf);
99bcf217
JP
2757}
2758
d1f1052c
JP
2759void dev_printk(const char *level, const struct device *dev,
2760 const char *fmt, ...)
99bcf217
JP
2761{
2762 struct va_format vaf;
2763 va_list args;
99bcf217
JP
2764
2765 va_start(args, fmt);
2766
2767 vaf.fmt = fmt;
2768 vaf.va = &args;
2769
d1f1052c 2770 __dev_printk(level, dev, &vaf);
798efc60 2771
99bcf217 2772 va_end(args);
99bcf217
JP
2773}
2774EXPORT_SYMBOL(dev_printk);
2775
2776#define define_dev_printk_level(func, kern_level) \
d1f1052c 2777void func(const struct device *dev, const char *fmt, ...) \
99bcf217
JP
2778{ \
2779 struct va_format vaf; \
2780 va_list args; \
99bcf217
JP
2781 \
2782 va_start(args, fmt); \
2783 \
2784 vaf.fmt = fmt; \
2785 vaf.va = &args; \
2786 \
d1f1052c 2787 __dev_printk(kern_level, dev, &vaf); \
798efc60 2788 \
99bcf217 2789 va_end(args); \
99bcf217
JP
2790} \
2791EXPORT_SYMBOL(func);
2792
2793define_dev_printk_level(dev_emerg, KERN_EMERG);
2794define_dev_printk_level(dev_alert, KERN_ALERT);
2795define_dev_printk_level(dev_crit, KERN_CRIT);
2796define_dev_printk_level(dev_err, KERN_ERR);
2797define_dev_printk_level(dev_warn, KERN_WARNING);
2798define_dev_printk_level(dev_notice, KERN_NOTICE);
2799define_dev_printk_level(_dev_info, KERN_INFO);
2800
2801#endif
97badf87
RW
2802
2803static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2804{
2805 return fwnode && !IS_ERR(fwnode->secondary);
2806}
2807
2808/**
2809 * set_primary_fwnode - Change the primary firmware node of a given device.
2810 * @dev: Device to handle.
2811 * @fwnode: New primary firmware node of the device.
2812 *
2813 * Set the device's firmware node pointer to @fwnode, but if a secondary
2814 * firmware node of the device is present, preserve it.
2815 */
2816void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2817{
2818 if (fwnode) {
2819 struct fwnode_handle *fn = dev->fwnode;
2820
2821 if (fwnode_is_primary(fn))
2822 fn = fn->secondary;
2823
55f89a8a
MW
2824 if (fn) {
2825 WARN_ON(fwnode->secondary);
2826 fwnode->secondary = fn;
2827 }
97badf87
RW
2828 dev->fwnode = fwnode;
2829 } else {
2830 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
2831 dev->fwnode->secondary : NULL;
2832 }
2833}
2834EXPORT_SYMBOL_GPL(set_primary_fwnode);
2835
2836/**
2837 * set_secondary_fwnode - Change the secondary firmware node of a given device.
2838 * @dev: Device to handle.
2839 * @fwnode: New secondary firmware node of the device.
2840 *
2841 * If a primary firmware node of the device is present, set its secondary
2842 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
2843 * @fwnode.
2844 */
2845void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2846{
2847 if (fwnode)
2848 fwnode->secondary = ERR_PTR(-ENODEV);
2849
2850 if (fwnode_is_primary(dev->fwnode))
2851 dev->fwnode->secondary = fwnode;
2852 else
2853 dev->fwnode = fwnode;
2854}