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