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