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