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