2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
9 * This file is released under the GPLv2
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/kallsyms.h>
26 #include <linux/mutex.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
29 #include <linux/sched/signal.h>
30 #include <linux/sysfs.h>
33 #include "power/power.h"
35 #ifdef CONFIG_SYSFS_DEPRECATED
36 #ifdef CONFIG_SYSFS_DEPRECATED_V2
37 long sysfs_deprecated
= 1;
39 long sysfs_deprecated
= 0;
41 static int __init
sysfs_deprecated_setup(char *arg
)
43 return kstrtol(arg
, 10, &sysfs_deprecated
);
45 early_param("sysfs.deprecated", sysfs_deprecated_setup
);
48 /* Device links support. */
51 static DEFINE_MUTEX(device_links_lock
);
52 DEFINE_STATIC_SRCU(device_links_srcu
);
54 static inline void device_links_write_lock(void)
56 mutex_lock(&device_links_lock
);
59 static inline void device_links_write_unlock(void)
61 mutex_unlock(&device_links_lock
);
64 int device_links_read_lock(void)
66 return srcu_read_lock(&device_links_srcu
);
69 void device_links_read_unlock(int idx
)
71 srcu_read_unlock(&device_links_srcu
, idx
);
73 #else /* !CONFIG_SRCU */
74 static DECLARE_RWSEM(device_links_lock
);
76 static inline void device_links_write_lock(void)
78 down_write(&device_links_lock
);
81 static inline void device_links_write_unlock(void)
83 up_write(&device_links_lock
);
86 int device_links_read_lock(void)
88 down_read(&device_links_lock
);
92 void device_links_read_unlock(int not_used
)
94 up_read(&device_links_lock
);
96 #endif /* !CONFIG_SRCU */
99 * device_is_dependent - Check if one device depends on another one
100 * @dev: Device to check dependencies for.
101 * @target: Device to check against.
103 * Check if @target depends on @dev or any device dependent on it (its child or
104 * its consumer etc). Return 1 if that is the case or 0 otherwise.
106 static int device_is_dependent(struct device
*dev
, void *target
)
108 struct device_link
*link
;
111 if (WARN_ON(dev
== target
))
114 ret
= device_for_each_child(dev
, target
, device_is_dependent
);
118 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
119 if (WARN_ON(link
->consumer
== target
))
122 ret
= device_is_dependent(link
->consumer
, target
);
129 static int device_reorder_to_tail(struct device
*dev
, void *not_used
)
131 struct device_link
*link
;
134 * Devices that have not been registered yet will be put to the ends
135 * of the lists during the registration, so skip them here.
137 if (device_is_registered(dev
))
138 devices_kset_move_last(dev
);
140 if (device_pm_initialized(dev
))
141 device_pm_move_last(dev
);
143 device_for_each_child(dev
, NULL
, device_reorder_to_tail
);
144 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
)
145 device_reorder_to_tail(link
->consumer
, NULL
);
151 * device_link_add - Create a link between two devices.
152 * @consumer: Consumer end of the link.
153 * @supplier: Supplier end of the link.
154 * @flags: Link flags.
156 * The caller is responsible for the proper synchronization of the link creation
157 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
158 * runtime PM framework to take the link into account. Second, if the
159 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
160 * be forced into the active metastate and reference-counted upon the creation
161 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
164 * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically
165 * when the consumer device driver unbinds from it. The combination of both
166 * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL
169 * A side effect of the link creation is re-ordering of dpm_list and the
170 * devices_kset list by moving the consumer device and all devices depending
171 * on it to the ends of these lists (that does not happen to devices that have
172 * not been registered when this function is called).
174 * The supplier device is required to be registered when this function is called
175 * and NULL will be returned if that is not the case. The consumer device need
176 * not be registered, however.
178 struct device_link
*device_link_add(struct device
*consumer
,
179 struct device
*supplier
, u32 flags
)
181 struct device_link
*link
;
183 if (!consumer
|| !supplier
||
184 ((flags
& DL_FLAG_STATELESS
) && (flags
& DL_FLAG_AUTOREMOVE
)))
187 device_links_write_lock();
191 * If the supplier has not been fully registered yet or there is a
192 * reverse dependency between the consumer and the supplier already in
193 * the graph, return NULL.
195 if (!device_pm_initialized(supplier
)
196 || device_is_dependent(consumer
, supplier
)) {
201 list_for_each_entry(link
, &supplier
->links
.consumers
, s_node
)
202 if (link
->consumer
== consumer
)
205 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
209 if (flags
& DL_FLAG_PM_RUNTIME
) {
210 if (flags
& DL_FLAG_RPM_ACTIVE
) {
211 if (pm_runtime_get_sync(supplier
) < 0) {
212 pm_runtime_put_noidle(supplier
);
217 link
->rpm_active
= true;
219 pm_runtime_new_link(consumer
);
221 get_device(supplier
);
222 link
->supplier
= supplier
;
223 INIT_LIST_HEAD(&link
->s_node
);
224 get_device(consumer
);
225 link
->consumer
= consumer
;
226 INIT_LIST_HEAD(&link
->c_node
);
229 /* Determine the initial link state. */
230 if (flags
& DL_FLAG_STATELESS
) {
231 link
->status
= DL_STATE_NONE
;
233 switch (supplier
->links
.status
) {
234 case DL_DEV_DRIVER_BOUND
:
235 switch (consumer
->links
.status
) {
238 * Balance the decrementation of the supplier's
239 * runtime PM usage counter after consumer probe
240 * in driver_probe_device().
242 if (flags
& DL_FLAG_PM_RUNTIME
)
243 pm_runtime_get_sync(supplier
);
245 link
->status
= DL_STATE_CONSUMER_PROBE
;
247 case DL_DEV_DRIVER_BOUND
:
248 link
->status
= DL_STATE_ACTIVE
;
251 link
->status
= DL_STATE_AVAILABLE
;
255 case DL_DEV_UNBINDING
:
256 link
->status
= DL_STATE_SUPPLIER_UNBIND
;
259 link
->status
= DL_STATE_DORMANT
;
265 * Move the consumer and all of the devices depending on it to the end
266 * of dpm_list and the devices_kset list.
268 * It is necessary to hold dpm_list locked throughout all that or else
269 * we may end up suspending with a wrong ordering of it.
271 device_reorder_to_tail(consumer
, NULL
);
273 list_add_tail_rcu(&link
->s_node
, &supplier
->links
.consumers
);
274 list_add_tail_rcu(&link
->c_node
, &consumer
->links
.suppliers
);
276 dev_info(consumer
, "Linked as a consumer to %s\n", dev_name(supplier
));
280 device_links_write_unlock();
283 EXPORT_SYMBOL_GPL(device_link_add
);
285 static void device_link_free(struct device_link
*link
)
287 put_device(link
->consumer
);
288 put_device(link
->supplier
);
293 static void __device_link_free_srcu(struct rcu_head
*rhead
)
295 device_link_free(container_of(rhead
, struct device_link
, rcu_head
));
298 static void __device_link_del(struct device_link
*link
)
300 dev_info(link
->consumer
, "Dropping the link to %s\n",
301 dev_name(link
->supplier
));
303 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
304 pm_runtime_drop_link(link
->consumer
);
306 list_del_rcu(&link
->s_node
);
307 list_del_rcu(&link
->c_node
);
308 call_srcu(&device_links_srcu
, &link
->rcu_head
, __device_link_free_srcu
);
310 #else /* !CONFIG_SRCU */
311 static void __device_link_del(struct device_link
*link
)
313 dev_info(link
->consumer
, "Dropping the link to %s\n",
314 dev_name(link
->supplier
));
316 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
317 pm_runtime_drop_link(link
->consumer
);
319 list_del(&link
->s_node
);
320 list_del(&link
->c_node
);
321 device_link_free(link
);
323 #endif /* !CONFIG_SRCU */
326 * device_link_del - Delete a link between two devices.
327 * @link: Device link to delete.
329 * The caller must ensure proper synchronization of this function with runtime
332 void device_link_del(struct device_link
*link
)
334 device_links_write_lock();
336 __device_link_del(link
);
338 device_links_write_unlock();
340 EXPORT_SYMBOL_GPL(device_link_del
);
342 static void device_links_missing_supplier(struct device
*dev
)
344 struct device_link
*link
;
346 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
)
347 if (link
->status
== DL_STATE_CONSUMER_PROBE
)
348 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
352 * device_links_check_suppliers - Check presence of supplier drivers.
353 * @dev: Consumer device.
355 * Check links from this device to any suppliers. Walk the list of the device's
356 * links to suppliers and see if all of them are available. If not, simply
357 * return -EPROBE_DEFER.
359 * We need to guarantee that the supplier will not go away after the check has
360 * been positive here. It only can go away in __device_release_driver() and
361 * that function checks the device's links to consumers. This means we need to
362 * mark the link as "consumer probe in progress" to make the supplier removal
363 * wait for us to complete (or bad things may happen).
365 * Links with the DL_FLAG_STATELESS flag set are ignored.
367 int device_links_check_suppliers(struct device
*dev
)
369 struct device_link
*link
;
372 device_links_write_lock();
374 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
375 if (link
->flags
& DL_FLAG_STATELESS
)
378 if (link
->status
!= DL_STATE_AVAILABLE
) {
379 device_links_missing_supplier(dev
);
383 WRITE_ONCE(link
->status
, DL_STATE_CONSUMER_PROBE
);
385 dev
->links
.status
= DL_DEV_PROBING
;
387 device_links_write_unlock();
392 * device_links_driver_bound - Update device links after probing its driver.
393 * @dev: Device to update the links for.
395 * The probe has been successful, so update links from this device to any
396 * consumers by changing their status to "available".
398 * Also change the status of @dev's links to suppliers to "active".
400 * Links with the DL_FLAG_STATELESS flag set are ignored.
402 void device_links_driver_bound(struct device
*dev
)
404 struct device_link
*link
;
406 device_links_write_lock();
408 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
409 if (link
->flags
& DL_FLAG_STATELESS
)
412 WARN_ON(link
->status
!= DL_STATE_DORMANT
);
413 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
416 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
417 if (link
->flags
& DL_FLAG_STATELESS
)
420 WARN_ON(link
->status
!= DL_STATE_CONSUMER_PROBE
);
421 WRITE_ONCE(link
->status
, DL_STATE_ACTIVE
);
424 dev
->links
.status
= DL_DEV_DRIVER_BOUND
;
426 device_links_write_unlock();
430 * __device_links_no_driver - Update links of a device without a driver.
431 * @dev: Device without a drvier.
433 * Delete all non-persistent links from this device to any suppliers.
435 * Persistent links stay around, but their status is changed to "available",
436 * unless they already are in the "supplier unbind in progress" state in which
437 * case they need not be updated.
439 * Links with the DL_FLAG_STATELESS flag set are ignored.
441 static void __device_links_no_driver(struct device
*dev
)
443 struct device_link
*link
, *ln
;
445 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
446 if (link
->flags
& DL_FLAG_STATELESS
)
449 if (link
->flags
& DL_FLAG_AUTOREMOVE
)
450 __device_link_del(link
);
451 else if (link
->status
!= DL_STATE_SUPPLIER_UNBIND
)
452 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
455 dev
->links
.status
= DL_DEV_NO_DRIVER
;
458 void device_links_no_driver(struct device
*dev
)
460 device_links_write_lock();
461 __device_links_no_driver(dev
);
462 device_links_write_unlock();
466 * device_links_driver_cleanup - Update links after driver removal.
467 * @dev: Device whose driver has just gone away.
469 * Update links to consumers for @dev by changing their status to "dormant" and
470 * invoke %__device_links_no_driver() to update links to suppliers for it as
473 * Links with the DL_FLAG_STATELESS flag set are ignored.
475 void device_links_driver_cleanup(struct device
*dev
)
477 struct device_link
*link
;
479 device_links_write_lock();
481 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
482 if (link
->flags
& DL_FLAG_STATELESS
)
485 WARN_ON(link
->flags
& DL_FLAG_AUTOREMOVE
);
486 WARN_ON(link
->status
!= DL_STATE_SUPPLIER_UNBIND
);
487 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
490 __device_links_no_driver(dev
);
492 device_links_write_unlock();
496 * device_links_busy - Check if there are any busy links to consumers.
497 * @dev: Device to check.
499 * Check each consumer of the device and return 'true' if its link's status
500 * is one of "consumer probe" or "active" (meaning that the given consumer is
501 * probing right now or its driver is present). Otherwise, change the link
502 * state to "supplier unbind" to prevent the consumer from being probed
503 * successfully going forward.
505 * Return 'false' if there are no probing or active consumers.
507 * Links with the DL_FLAG_STATELESS flag set are ignored.
509 bool device_links_busy(struct device
*dev
)
511 struct device_link
*link
;
514 device_links_write_lock();
516 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
517 if (link
->flags
& DL_FLAG_STATELESS
)
520 if (link
->status
== DL_STATE_CONSUMER_PROBE
521 || link
->status
== DL_STATE_ACTIVE
) {
525 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
528 dev
->links
.status
= DL_DEV_UNBINDING
;
530 device_links_write_unlock();
535 * device_links_unbind_consumers - Force unbind consumers of the given device.
536 * @dev: Device to unbind the consumers of.
538 * Walk the list of links to consumers for @dev and if any of them is in the
539 * "consumer probe" state, wait for all device probes in progress to complete
542 * If that's not the case, change the status of the link to "supplier unbind"
543 * and check if the link was in the "active" state. If so, force the consumer
544 * driver to unbind and start over (the consumer will not re-probe as we have
545 * changed the state of the link already).
547 * Links with the DL_FLAG_STATELESS flag set are ignored.
549 void device_links_unbind_consumers(struct device
*dev
)
551 struct device_link
*link
;
554 device_links_write_lock();
556 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
557 enum device_link_state status
;
559 if (link
->flags
& DL_FLAG_STATELESS
)
562 status
= link
->status
;
563 if (status
== DL_STATE_CONSUMER_PROBE
) {
564 device_links_write_unlock();
566 wait_for_device_probe();
569 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
570 if (status
== DL_STATE_ACTIVE
) {
571 struct device
*consumer
= link
->consumer
;
573 get_device(consumer
);
575 device_links_write_unlock();
577 device_release_driver_internal(consumer
, NULL
,
579 put_device(consumer
);
584 device_links_write_unlock();
588 * device_links_purge - Delete existing links to other devices.
589 * @dev: Target device.
591 static void device_links_purge(struct device
*dev
)
593 struct device_link
*link
, *ln
;
596 * Delete all of the remaining links from this device to any other
597 * devices (either consumers or suppliers).
599 device_links_write_lock();
601 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
602 WARN_ON(link
->status
== DL_STATE_ACTIVE
);
603 __device_link_del(link
);
606 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.consumers
, s_node
) {
607 WARN_ON(link
->status
!= DL_STATE_DORMANT
&&
608 link
->status
!= DL_STATE_NONE
);
609 __device_link_del(link
);
612 device_links_write_unlock();
615 /* Device links support end. */
617 int (*platform_notify
)(struct device
*dev
) = NULL
;
618 int (*platform_notify_remove
)(struct device
*dev
) = NULL
;
619 static struct kobject
*dev_kobj
;
620 struct kobject
*sysfs_dev_char_kobj
;
621 struct kobject
*sysfs_dev_block_kobj
;
623 static DEFINE_MUTEX(device_hotplug_lock
);
625 void lock_device_hotplug(void)
627 mutex_lock(&device_hotplug_lock
);
630 void unlock_device_hotplug(void)
632 mutex_unlock(&device_hotplug_lock
);
635 int lock_device_hotplug_sysfs(void)
637 if (mutex_trylock(&device_hotplug_lock
))
640 /* Avoid busy looping (5 ms of sleep should do). */
642 return restart_syscall();
646 static inline int device_is_not_partition(struct device
*dev
)
648 return !(dev
->type
== &part_type
);
651 static inline int device_is_not_partition(struct device
*dev
)
658 * dev_driver_string - Return a device's driver name, if at all possible
659 * @dev: struct device to get the name of
661 * Will return the device's driver's name if it is bound to a device. If
662 * the device is not bound to a driver, it will return the name of the bus
663 * it is attached to. If it is not attached to a bus either, an empty
664 * string will be returned.
666 const char *dev_driver_string(const struct device
*dev
)
668 struct device_driver
*drv
;
670 /* dev->driver can change to NULL underneath us because of unbinding,
671 * so be careful about accessing it. dev->bus and dev->class should
672 * never change once they are set, so they don't need special care.
674 drv
= READ_ONCE(dev
->driver
);
675 return drv
? drv
->name
:
676 (dev
->bus
? dev
->bus
->name
:
677 (dev
->class ? dev
->class->name
: ""));
679 EXPORT_SYMBOL(dev_driver_string
);
681 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
683 static ssize_t
dev_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
686 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
687 struct device
*dev
= kobj_to_dev(kobj
);
691 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
692 if (ret
>= (ssize_t
)PAGE_SIZE
) {
693 print_symbol("dev_attr_show: %s returned bad count\n",
694 (unsigned long)dev_attr
->show
);
699 static ssize_t
dev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
700 const char *buf
, size_t count
)
702 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
703 struct device
*dev
= kobj_to_dev(kobj
);
707 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
711 static const struct sysfs_ops dev_sysfs_ops
= {
712 .show
= dev_attr_show
,
713 .store
= dev_attr_store
,
716 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
718 ssize_t
device_store_ulong(struct device
*dev
,
719 struct device_attribute
*attr
,
720 const char *buf
, size_t size
)
722 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
724 unsigned long new = simple_strtoul(buf
, &end
, 0);
727 *(unsigned long *)(ea
->var
) = new;
728 /* Always return full write size even if we didn't consume all */
731 EXPORT_SYMBOL_GPL(device_store_ulong
);
733 ssize_t
device_show_ulong(struct device
*dev
,
734 struct device_attribute
*attr
,
737 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
738 return snprintf(buf
, PAGE_SIZE
, "%lx\n", *(unsigned long *)(ea
->var
));
740 EXPORT_SYMBOL_GPL(device_show_ulong
);
742 ssize_t
device_store_int(struct device
*dev
,
743 struct device_attribute
*attr
,
744 const char *buf
, size_t size
)
746 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
748 long new = simple_strtol(buf
, &end
, 0);
749 if (end
== buf
|| new > INT_MAX
|| new < INT_MIN
)
751 *(int *)(ea
->var
) = new;
752 /* Always return full write size even if we didn't consume all */
755 EXPORT_SYMBOL_GPL(device_store_int
);
757 ssize_t
device_show_int(struct device
*dev
,
758 struct device_attribute
*attr
,
761 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
763 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(int *)(ea
->var
));
765 EXPORT_SYMBOL_GPL(device_show_int
);
767 ssize_t
device_store_bool(struct device
*dev
, struct device_attribute
*attr
,
768 const char *buf
, size_t size
)
770 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
772 if (strtobool(buf
, ea
->var
) < 0)
777 EXPORT_SYMBOL_GPL(device_store_bool
);
779 ssize_t
device_show_bool(struct device
*dev
, struct device_attribute
*attr
,
782 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
784 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(bool *)(ea
->var
));
786 EXPORT_SYMBOL_GPL(device_show_bool
);
789 * device_release - free device structure.
790 * @kobj: device's kobject.
792 * This is called once the reference count for the object
793 * reaches 0. We forward the call to the device's release
794 * method, which should handle actually freeing the structure.
796 static void device_release(struct kobject
*kobj
)
798 struct device
*dev
= kobj_to_dev(kobj
);
799 struct device_private
*p
= dev
->p
;
802 * Some platform devices are driven without driver attached
803 * and managed resources may have been acquired. Make sure
804 * all resources are released.
806 * Drivers still can add resources into device after device
807 * is deleted but alive, so release devres here to avoid
808 * possible memory leak.
810 devres_release_all(dev
);
814 else if (dev
->type
&& dev
->type
->release
)
815 dev
->type
->release(dev
);
816 else if (dev
->class && dev
->class->dev_release
)
817 dev
->class->dev_release(dev
);
819 WARN(1, KERN_ERR
"Device '%s' does not have a release() "
820 "function, it is broken and must be fixed.\n",
825 static const void *device_namespace(struct kobject
*kobj
)
827 struct device
*dev
= kobj_to_dev(kobj
);
828 const void *ns
= NULL
;
830 if (dev
->class && dev
->class->ns_type
)
831 ns
= dev
->class->namespace(dev
);
836 static void device_get_ownership(struct kobject
*kobj
, kuid_t
*uid
, kgid_t
*gid
)
838 struct device
*dev
= kobj_to_dev(kobj
);
840 if (dev
->class && dev
->class->get_ownership
)
841 dev
->class->get_ownership(dev
, uid
, gid
);
844 static struct kobj_type device_ktype
= {
845 .release
= device_release
,
846 .sysfs_ops
= &dev_sysfs_ops
,
847 .namespace = device_namespace
,
848 .get_ownership
= device_get_ownership
,
852 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
854 struct kobj_type
*ktype
= get_ktype(kobj
);
856 if (ktype
== &device_ktype
) {
857 struct device
*dev
= kobj_to_dev(kobj
);
866 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
868 struct device
*dev
= kobj_to_dev(kobj
);
871 return dev
->bus
->name
;
873 return dev
->class->name
;
877 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
878 struct kobj_uevent_env
*env
)
880 struct device
*dev
= kobj_to_dev(kobj
);
883 /* add device node properties if present */
884 if (MAJOR(dev
->devt
)) {
888 kuid_t uid
= GLOBAL_ROOT_UID
;
889 kgid_t gid
= GLOBAL_ROOT_GID
;
891 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
892 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
893 name
= device_get_devnode(dev
, &mode
, &uid
, &gid
, &tmp
);
895 add_uevent_var(env
, "DEVNAME=%s", name
);
897 add_uevent_var(env
, "DEVMODE=%#o", mode
& 0777);
898 if (!uid_eq(uid
, GLOBAL_ROOT_UID
))
899 add_uevent_var(env
, "DEVUID=%u", from_kuid(&init_user_ns
, uid
));
900 if (!gid_eq(gid
, GLOBAL_ROOT_GID
))
901 add_uevent_var(env
, "DEVGID=%u", from_kgid(&init_user_ns
, gid
));
906 if (dev
->type
&& dev
->type
->name
)
907 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
910 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
912 /* Add common DT information about the device */
913 of_device_uevent(dev
, env
);
915 /* have the bus specific function add its stuff */
916 if (dev
->bus
&& dev
->bus
->uevent
) {
917 retval
= dev
->bus
->uevent(dev
, env
);
919 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
920 dev_name(dev
), __func__
, retval
);
923 /* have the class specific function add its stuff */
924 if (dev
->class && dev
->class->dev_uevent
) {
925 retval
= dev
->class->dev_uevent(dev
, env
);
927 pr_debug("device: '%s': %s: class uevent() "
928 "returned %d\n", dev_name(dev
),
932 /* have the device type specific function add its stuff */
933 if (dev
->type
&& dev
->type
->uevent
) {
934 retval
= dev
->type
->uevent(dev
, env
);
936 pr_debug("device: '%s': %s: dev_type uevent() "
937 "returned %d\n", dev_name(dev
),
944 static const struct kset_uevent_ops device_uevent_ops
= {
945 .filter
= dev_uevent_filter
,
946 .name
= dev_uevent_name
,
947 .uevent
= dev_uevent
,
950 static ssize_t
uevent_show(struct device
*dev
, struct device_attribute
*attr
,
953 struct kobject
*top_kobj
;
955 struct kobj_uevent_env
*env
= NULL
;
960 /* search the kset, the device belongs to */
961 top_kobj
= &dev
->kobj
;
962 while (!top_kobj
->kset
&& top_kobj
->parent
)
963 top_kobj
= top_kobj
->parent
;
967 kset
= top_kobj
->kset
;
968 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
972 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
973 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
976 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
980 /* let the kset specific function add its keys */
981 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
985 /* copy keys to file */
986 for (i
= 0; i
< env
->envp_idx
; i
++)
987 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
993 static ssize_t
uevent_store(struct device
*dev
, struct device_attribute
*attr
,
994 const char *buf
, size_t count
)
996 if (kobject_synth_uevent(&dev
->kobj
, buf
, count
))
997 dev_err(dev
, "uevent: failed to send synthetic uevent\n");
1001 static DEVICE_ATTR_RW(uevent
);
1003 static ssize_t
online_show(struct device
*dev
, struct device_attribute
*attr
,
1009 val
= !dev
->offline
;
1011 return sprintf(buf
, "%u\n", val
);
1014 static ssize_t
online_store(struct device
*dev
, struct device_attribute
*attr
,
1015 const char *buf
, size_t count
)
1020 ret
= strtobool(buf
, &val
);
1024 ret
= lock_device_hotplug_sysfs();
1028 ret
= val
? device_online(dev
) : device_offline(dev
);
1029 unlock_device_hotplug();
1030 return ret
< 0 ? ret
: count
;
1032 static DEVICE_ATTR_RW(online
);
1034 int device_add_groups(struct device
*dev
, const struct attribute_group
**groups
)
1036 return sysfs_create_groups(&dev
->kobj
, groups
);
1038 EXPORT_SYMBOL_GPL(device_add_groups
);
1040 void device_remove_groups(struct device
*dev
,
1041 const struct attribute_group
**groups
)
1043 sysfs_remove_groups(&dev
->kobj
, groups
);
1045 EXPORT_SYMBOL_GPL(device_remove_groups
);
1047 union device_attr_group_devres
{
1048 const struct attribute_group
*group
;
1049 const struct attribute_group
**groups
;
1052 static int devm_attr_group_match(struct device
*dev
, void *res
, void *data
)
1054 return ((union device_attr_group_devres
*)res
)->group
== data
;
1057 static void devm_attr_group_remove(struct device
*dev
, void *res
)
1059 union device_attr_group_devres
*devres
= res
;
1060 const struct attribute_group
*group
= devres
->group
;
1062 dev_dbg(dev
, "%s: removing group %p\n", __func__
, group
);
1063 sysfs_remove_group(&dev
->kobj
, group
);
1066 static void devm_attr_groups_remove(struct device
*dev
, void *res
)
1068 union device_attr_group_devres
*devres
= res
;
1069 const struct attribute_group
**groups
= devres
->groups
;
1071 dev_dbg(dev
, "%s: removing groups %p\n", __func__
, groups
);
1072 sysfs_remove_groups(&dev
->kobj
, groups
);
1076 * devm_device_add_group - given a device, create a managed attribute group
1077 * @dev: The device to create the group for
1078 * @grp: The attribute group to create
1080 * This function creates a group for the first time. It will explicitly
1081 * warn and error if any of the attribute files being created already exist.
1083 * Returns 0 on success or error code on failure.
1085 int devm_device_add_group(struct device
*dev
, const struct attribute_group
*grp
)
1087 union device_attr_group_devres
*devres
;
1090 devres
= devres_alloc(devm_attr_group_remove
,
1091 sizeof(*devres
), GFP_KERNEL
);
1095 error
= sysfs_create_group(&dev
->kobj
, grp
);
1097 devres_free(devres
);
1101 devres
->group
= grp
;
1102 devres_add(dev
, devres
);
1105 EXPORT_SYMBOL_GPL(devm_device_add_group
);
1108 * devm_device_remove_group: remove a managed group from a device
1109 * @dev: device to remove the group from
1110 * @grp: group to remove
1112 * This function removes a group of attributes from a device. The attributes
1113 * previously have to have been created for this group, otherwise it will fail.
1115 void devm_device_remove_group(struct device
*dev
,
1116 const struct attribute_group
*grp
)
1118 WARN_ON(devres_release(dev
, devm_attr_group_remove
,
1119 devm_attr_group_match
,
1120 /* cast away const */ (void *)grp
));
1122 EXPORT_SYMBOL_GPL(devm_device_remove_group
);
1125 * devm_device_add_groups - create a bunch of managed attribute groups
1126 * @dev: The device to create the group for
1127 * @groups: The attribute groups to create, NULL terminated
1129 * This function creates a bunch of managed attribute groups. If an error
1130 * occurs when creating a group, all previously created groups will be
1131 * removed, unwinding everything back to the original state when this
1132 * function was called. It will explicitly warn and error if any of the
1133 * attribute files being created already exist.
1135 * Returns 0 on success or error code from sysfs_create_group on failure.
1137 int devm_device_add_groups(struct device
*dev
,
1138 const struct attribute_group
**groups
)
1140 union device_attr_group_devres
*devres
;
1143 devres
= devres_alloc(devm_attr_groups_remove
,
1144 sizeof(*devres
), GFP_KERNEL
);
1148 error
= sysfs_create_groups(&dev
->kobj
, groups
);
1150 devres_free(devres
);
1154 devres
->groups
= groups
;
1155 devres_add(dev
, devres
);
1158 EXPORT_SYMBOL_GPL(devm_device_add_groups
);
1161 * devm_device_remove_groups - remove a list of managed groups
1163 * @dev: The device for the groups to be removed from
1164 * @groups: NULL terminated list of groups to be removed
1166 * If groups is not NULL, remove the specified groups from the device.
1168 void devm_device_remove_groups(struct device
*dev
,
1169 const struct attribute_group
**groups
)
1171 WARN_ON(devres_release(dev
, devm_attr_groups_remove
,
1172 devm_attr_group_match
,
1173 /* cast away const */ (void *)groups
));
1175 EXPORT_SYMBOL_GPL(devm_device_remove_groups
);
1177 static int device_add_attrs(struct device
*dev
)
1179 struct class *class = dev
->class;
1180 const struct device_type
*type
= dev
->type
;
1184 error
= device_add_groups(dev
, class->dev_groups
);
1190 error
= device_add_groups(dev
, type
->groups
);
1192 goto err_remove_class_groups
;
1195 error
= device_add_groups(dev
, dev
->groups
);
1197 goto err_remove_type_groups
;
1199 if (device_supports_offline(dev
) && !dev
->offline_disabled
) {
1200 error
= device_create_file(dev
, &dev_attr_online
);
1202 goto err_remove_dev_groups
;
1207 err_remove_dev_groups
:
1208 device_remove_groups(dev
, dev
->groups
);
1209 err_remove_type_groups
:
1211 device_remove_groups(dev
, type
->groups
);
1212 err_remove_class_groups
:
1214 device_remove_groups(dev
, class->dev_groups
);
1219 static void device_remove_attrs(struct device
*dev
)
1221 struct class *class = dev
->class;
1222 const struct device_type
*type
= dev
->type
;
1224 device_remove_file(dev
, &dev_attr_online
);
1225 device_remove_groups(dev
, dev
->groups
);
1228 device_remove_groups(dev
, type
->groups
);
1231 device_remove_groups(dev
, class->dev_groups
);
1234 static ssize_t
dev_show(struct device
*dev
, struct device_attribute
*attr
,
1237 return print_dev_t(buf
, dev
->devt
);
1239 static DEVICE_ATTR_RO(dev
);
1242 struct kset
*devices_kset
;
1245 * devices_kset_move_before - Move device in the devices_kset's list.
1246 * @deva: Device to move.
1247 * @devb: Device @deva should come before.
1249 static void devices_kset_move_before(struct device
*deva
, struct device
*devb
)
1253 pr_debug("devices_kset: Moving %s before %s\n",
1254 dev_name(deva
), dev_name(devb
));
1255 spin_lock(&devices_kset
->list_lock
);
1256 list_move_tail(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1257 spin_unlock(&devices_kset
->list_lock
);
1261 * devices_kset_move_after - Move device in the devices_kset's list.
1262 * @deva: Device to move
1263 * @devb: Device @deva should come after.
1265 static void devices_kset_move_after(struct device
*deva
, struct device
*devb
)
1269 pr_debug("devices_kset: Moving %s after %s\n",
1270 dev_name(deva
), dev_name(devb
));
1271 spin_lock(&devices_kset
->list_lock
);
1272 list_move(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1273 spin_unlock(&devices_kset
->list_lock
);
1277 * devices_kset_move_last - move the device to the end of devices_kset's list.
1278 * @dev: device to move
1280 void devices_kset_move_last(struct device
*dev
)
1284 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev
));
1285 spin_lock(&devices_kset
->list_lock
);
1286 list_move_tail(&dev
->kobj
.entry
, &devices_kset
->list
);
1287 spin_unlock(&devices_kset
->list_lock
);
1291 * device_create_file - create sysfs attribute file for device.
1293 * @attr: device attribute descriptor.
1295 int device_create_file(struct device
*dev
,
1296 const struct device_attribute
*attr
)
1301 WARN(((attr
->attr
.mode
& S_IWUGO
) && !attr
->store
),
1302 "Attribute %s: write permission without 'store'\n",
1304 WARN(((attr
->attr
.mode
& S_IRUGO
) && !attr
->show
),
1305 "Attribute %s: read permission without 'show'\n",
1307 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
1312 EXPORT_SYMBOL_GPL(device_create_file
);
1315 * device_remove_file - remove sysfs attribute file.
1317 * @attr: device attribute descriptor.
1319 void device_remove_file(struct device
*dev
,
1320 const struct device_attribute
*attr
)
1323 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
1325 EXPORT_SYMBOL_GPL(device_remove_file
);
1328 * device_remove_file_self - remove sysfs attribute file from its own method.
1330 * @attr: device attribute descriptor.
1332 * See kernfs_remove_self() for details.
1334 bool device_remove_file_self(struct device
*dev
,
1335 const struct device_attribute
*attr
)
1338 return sysfs_remove_file_self(&dev
->kobj
, &attr
->attr
);
1342 EXPORT_SYMBOL_GPL(device_remove_file_self
);
1345 * device_create_bin_file - create sysfs binary attribute file for device.
1347 * @attr: device binary attribute descriptor.
1349 int device_create_bin_file(struct device
*dev
,
1350 const struct bin_attribute
*attr
)
1352 int error
= -EINVAL
;
1354 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
1357 EXPORT_SYMBOL_GPL(device_create_bin_file
);
1360 * device_remove_bin_file - remove sysfs binary attribute file
1362 * @attr: device binary attribute descriptor.
1364 void device_remove_bin_file(struct device
*dev
,
1365 const struct bin_attribute
*attr
)
1368 sysfs_remove_bin_file(&dev
->kobj
, attr
);
1370 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
1372 static void klist_children_get(struct klist_node
*n
)
1374 struct device_private
*p
= to_device_private_parent(n
);
1375 struct device
*dev
= p
->device
;
1380 static void klist_children_put(struct klist_node
*n
)
1382 struct device_private
*p
= to_device_private_parent(n
);
1383 struct device
*dev
= p
->device
;
1389 * device_initialize - init device structure.
1392 * This prepares the device for use by other layers by initializing
1394 * It is the first half of device_register(), if called by
1395 * that function, though it can also be called separately, so one
1396 * may use @dev's fields. In particular, get_device()/put_device()
1397 * may be used for reference counting of @dev after calling this
1400 * All fields in @dev must be initialized by the caller to 0, except
1401 * for those explicitly set to some other value. The simplest
1402 * approach is to use kzalloc() to allocate the structure containing
1405 * NOTE: Use put_device() to give up your reference instead of freeing
1406 * @dev directly once you have called this function.
1408 void device_initialize(struct device
*dev
)
1410 dev
->kobj
.kset
= devices_kset
;
1411 kobject_init(&dev
->kobj
, &device_ktype
);
1412 INIT_LIST_HEAD(&dev
->dma_pools
);
1413 mutex_init(&dev
->mutex
);
1414 lockdep_set_novalidate_class(&dev
->mutex
);
1415 spin_lock_init(&dev
->devres_lock
);
1416 INIT_LIST_HEAD(&dev
->devres_head
);
1417 device_pm_init(dev
);
1418 set_dev_node(dev
, -1);
1419 #ifdef CONFIG_GENERIC_MSI_IRQ
1420 INIT_LIST_HEAD(&dev
->msi_list
);
1422 INIT_LIST_HEAD(&dev
->links
.consumers
);
1423 INIT_LIST_HEAD(&dev
->links
.suppliers
);
1424 dev
->links
.status
= DL_DEV_NO_DRIVER
;
1426 EXPORT_SYMBOL_GPL(device_initialize
);
1428 struct kobject
*virtual_device_parent(struct device
*dev
)
1430 static struct kobject
*virtual_dir
= NULL
;
1433 virtual_dir
= kobject_create_and_add("virtual",
1434 &devices_kset
->kobj
);
1440 struct kobject kobj
;
1441 struct class *class;
1444 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1446 static void class_dir_release(struct kobject
*kobj
)
1448 struct class_dir
*dir
= to_class_dir(kobj
);
1453 struct kobj_ns_type_operations
*class_dir_child_ns_type(struct kobject
*kobj
)
1455 struct class_dir
*dir
= to_class_dir(kobj
);
1456 return dir
->class->ns_type
;
1459 static struct kobj_type class_dir_ktype
= {
1460 .release
= class_dir_release
,
1461 .sysfs_ops
= &kobj_sysfs_ops
,
1462 .child_ns_type
= class_dir_child_ns_type
1465 static struct kobject
*
1466 class_dir_create_and_add(struct class *class, struct kobject
*parent_kobj
)
1468 struct class_dir
*dir
;
1471 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1476 kobject_init(&dir
->kobj
, &class_dir_ktype
);
1478 dir
->kobj
.kset
= &class->p
->glue_dirs
;
1480 retval
= kobject_add(&dir
->kobj
, parent_kobj
, "%s", class->name
);
1482 kobject_put(&dir
->kobj
);
1488 static DEFINE_MUTEX(gdp_mutex
);
1490 static struct kobject
*get_device_parent(struct device
*dev
,
1491 struct device
*parent
)
1494 struct kobject
*kobj
= NULL
;
1495 struct kobject
*parent_kobj
;
1499 /* block disks show up in /sys/block */
1500 if (sysfs_deprecated
&& dev
->class == &block_class
) {
1501 if (parent
&& parent
->class == &block_class
)
1502 return &parent
->kobj
;
1503 return &block_class
.p
->subsys
.kobj
;
1508 * If we have no parent, we live in "virtual".
1509 * Class-devices with a non class-device as parent, live
1510 * in a "glue" directory to prevent namespace collisions.
1513 parent_kobj
= virtual_device_parent(dev
);
1514 else if (parent
->class && !dev
->class->ns_type
)
1515 return &parent
->kobj
;
1517 parent_kobj
= &parent
->kobj
;
1519 mutex_lock(&gdp_mutex
);
1521 /* find our class-directory at the parent and reference it */
1522 spin_lock(&dev
->class->p
->glue_dirs
.list_lock
);
1523 list_for_each_entry(k
, &dev
->class->p
->glue_dirs
.list
, entry
)
1524 if (k
->parent
== parent_kobj
) {
1525 kobj
= kobject_get(k
);
1528 spin_unlock(&dev
->class->p
->glue_dirs
.list_lock
);
1530 mutex_unlock(&gdp_mutex
);
1534 /* or create a new class-directory at the parent device */
1535 k
= class_dir_create_and_add(dev
->class, parent_kobj
);
1536 /* do not emit an uevent for this simple "glue" directory */
1537 mutex_unlock(&gdp_mutex
);
1541 /* subsystems can specify a default root directory for their devices */
1542 if (!parent
&& dev
->bus
&& dev
->bus
->dev_root
)
1543 return &dev
->bus
->dev_root
->kobj
;
1546 return &parent
->kobj
;
1550 static inline bool live_in_glue_dir(struct kobject
*kobj
,
1553 if (!kobj
|| !dev
->class ||
1554 kobj
->kset
!= &dev
->class->p
->glue_dirs
)
1559 static inline struct kobject
*get_glue_dir(struct device
*dev
)
1561 return dev
->kobj
.parent
;
1565 * make sure cleaning up dir as the last step, we need to make
1566 * sure .release handler of kobject is run with holding the
1569 static void cleanup_glue_dir(struct device
*dev
, struct kobject
*glue_dir
)
1571 /* see if we live in a "glue" directory */
1572 if (!live_in_glue_dir(glue_dir
, dev
))
1575 mutex_lock(&gdp_mutex
);
1576 kobject_put(glue_dir
);
1577 mutex_unlock(&gdp_mutex
);
1580 static int device_add_class_symlinks(struct device
*dev
)
1582 struct device_node
*of_node
= dev_of_node(dev
);
1586 error
= sysfs_create_link(&dev
->kobj
, of_node_kobj(of_node
), "of_node");
1588 dev_warn(dev
, "Error %d creating of_node link\n",error
);
1589 /* An error here doesn't warrant bringing down the device */
1595 error
= sysfs_create_link(&dev
->kobj
,
1596 &dev
->class->p
->subsys
.kobj
,
1601 if (dev
->parent
&& device_is_not_partition(dev
)) {
1602 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
1609 /* /sys/block has directories and does not need symlinks */
1610 if (sysfs_deprecated
&& dev
->class == &block_class
)
1614 /* link in the class directory pointing to the device */
1615 error
= sysfs_create_link(&dev
->class->p
->subsys
.kobj
,
1616 &dev
->kobj
, dev_name(dev
));
1623 sysfs_remove_link(&dev
->kobj
, "device");
1626 sysfs_remove_link(&dev
->kobj
, "subsystem");
1628 sysfs_remove_link(&dev
->kobj
, "of_node");
1632 static void device_remove_class_symlinks(struct device
*dev
)
1634 if (dev_of_node(dev
))
1635 sysfs_remove_link(&dev
->kobj
, "of_node");
1640 if (dev
->parent
&& device_is_not_partition(dev
))
1641 sysfs_remove_link(&dev
->kobj
, "device");
1642 sysfs_remove_link(&dev
->kobj
, "subsystem");
1644 if (sysfs_deprecated
&& dev
->class == &block_class
)
1647 sysfs_delete_link(&dev
->class->p
->subsys
.kobj
, &dev
->kobj
, dev_name(dev
));
1651 * dev_set_name - set a device name
1653 * @fmt: format string for the device's name
1655 int dev_set_name(struct device
*dev
, const char *fmt
, ...)
1660 va_start(vargs
, fmt
);
1661 err
= kobject_set_name_vargs(&dev
->kobj
, fmt
, vargs
);
1665 EXPORT_SYMBOL_GPL(dev_set_name
);
1668 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1671 * By default we select char/ for new entries. Setting class->dev_obj
1672 * to NULL prevents an entry from being created. class->dev_kobj must
1673 * be set (or cleared) before any devices are registered to the class
1674 * otherwise device_create_sys_dev_entry() and
1675 * device_remove_sys_dev_entry() will disagree about the presence of
1678 static struct kobject
*device_to_dev_kobj(struct device
*dev
)
1680 struct kobject
*kobj
;
1683 kobj
= dev
->class->dev_kobj
;
1685 kobj
= sysfs_dev_char_kobj
;
1690 static int device_create_sys_dev_entry(struct device
*dev
)
1692 struct kobject
*kobj
= device_to_dev_kobj(dev
);
1697 format_dev_t(devt_str
, dev
->devt
);
1698 error
= sysfs_create_link(kobj
, &dev
->kobj
, devt_str
);
1704 static void device_remove_sys_dev_entry(struct device
*dev
)
1706 struct kobject
*kobj
= device_to_dev_kobj(dev
);
1710 format_dev_t(devt_str
, dev
->devt
);
1711 sysfs_remove_link(kobj
, devt_str
);
1715 int device_private_init(struct device
*dev
)
1717 dev
->p
= kzalloc(sizeof(*dev
->p
), GFP_KERNEL
);
1720 dev
->p
->device
= dev
;
1721 klist_init(&dev
->p
->klist_children
, klist_children_get
,
1722 klist_children_put
);
1723 INIT_LIST_HEAD(&dev
->p
->deferred_probe
);
1728 * device_add - add device to device hierarchy.
1731 * This is part 2 of device_register(), though may be called
1732 * separately _iff_ device_initialize() has been called separately.
1734 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1735 * to the global and sibling lists for the device, then
1736 * adds it to the other relevant subsystems of the driver model.
1738 * Do not call this routine or device_register() more than once for
1739 * any device structure. The driver model core is not designed to work
1740 * with devices that get unregistered and then spring back to life.
1741 * (Among other things, it's very hard to guarantee that all references
1742 * to the previous incarnation of @dev have been dropped.) Allocate
1743 * and register a fresh new struct device instead.
1745 * NOTE: _Never_ directly free @dev after calling this function, even
1746 * if it returned an error! Always use put_device() to give up your
1747 * reference instead.
1749 int device_add(struct device
*dev
)
1751 struct device
*parent
;
1752 struct kobject
*kobj
;
1753 struct class_interface
*class_intf
;
1754 int error
= -EINVAL
;
1755 struct kobject
*glue_dir
= NULL
;
1757 dev
= get_device(dev
);
1762 error
= device_private_init(dev
);
1768 * for statically allocated devices, which should all be converted
1769 * some day, we need to initialize the name. We prevent reading back
1770 * the name, and force the use of dev_name()
1772 if (dev
->init_name
) {
1773 dev_set_name(dev
, "%s", dev
->init_name
);
1774 dev
->init_name
= NULL
;
1777 /* subsystems can specify simple device enumeration */
1778 if (!dev_name(dev
) && dev
->bus
&& dev
->bus
->dev_name
)
1779 dev_set_name(dev
, "%s%u", dev
->bus
->dev_name
, dev
->id
);
1781 if (!dev_name(dev
)) {
1786 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1788 parent
= get_device(dev
->parent
);
1789 kobj
= get_device_parent(dev
, parent
);
1791 dev
->kobj
.parent
= kobj
;
1793 /* use parent numa_node */
1794 if (parent
&& (dev_to_node(dev
) == NUMA_NO_NODE
))
1795 set_dev_node(dev
, dev_to_node(parent
));
1797 /* first, register with generic layer. */
1798 /* we require the name to be set before, and pass NULL */
1799 error
= kobject_add(&dev
->kobj
, dev
->kobj
.parent
, NULL
);
1801 glue_dir
= get_glue_dir(dev
);
1805 /* notify platform of device entry */
1806 if (platform_notify
)
1807 platform_notify(dev
);
1809 error
= device_create_file(dev
, &dev_attr_uevent
);
1813 error
= device_add_class_symlinks(dev
);
1816 error
= device_add_attrs(dev
);
1819 error
= bus_add_device(dev
);
1822 error
= dpm_sysfs_add(dev
);
1827 if (MAJOR(dev
->devt
)) {
1828 error
= device_create_file(dev
, &dev_attr_dev
);
1832 error
= device_create_sys_dev_entry(dev
);
1836 devtmpfs_create_node(dev
);
1839 /* Notify clients of device addition. This call must come
1840 * after dpm_sysfs_add() and before kobject_uevent().
1843 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1844 BUS_NOTIFY_ADD_DEVICE
, dev
);
1846 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
1847 bus_probe_device(dev
);
1849 klist_add_tail(&dev
->p
->knode_parent
,
1850 &parent
->p
->klist_children
);
1853 mutex_lock(&dev
->class->p
->mutex
);
1854 /* tie the class to the device */
1855 klist_add_tail(&dev
->knode_class
,
1856 &dev
->class->p
->klist_devices
);
1858 /* notify any interfaces that the device is here */
1859 list_for_each_entry(class_intf
,
1860 &dev
->class->p
->interfaces
, node
)
1861 if (class_intf
->add_dev
)
1862 class_intf
->add_dev(dev
, class_intf
);
1863 mutex_unlock(&dev
->class->p
->mutex
);
1869 if (MAJOR(dev
->devt
))
1870 device_remove_file(dev
, &dev_attr_dev
);
1872 device_pm_remove(dev
);
1873 dpm_sysfs_remove(dev
);
1875 bus_remove_device(dev
);
1877 device_remove_attrs(dev
);
1879 device_remove_class_symlinks(dev
);
1881 device_remove_file(dev
, &dev_attr_uevent
);
1883 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1884 glue_dir
= get_glue_dir(dev
);
1885 kobject_del(&dev
->kobj
);
1887 cleanup_glue_dir(dev
, glue_dir
);
1894 EXPORT_SYMBOL_GPL(device_add
);
1897 * device_register - register a device with the system.
1898 * @dev: pointer to the device structure
1900 * This happens in two clean steps - initialize the device
1901 * and add it to the system. The two steps can be called
1902 * separately, but this is the easiest and most common.
1903 * I.e. you should only call the two helpers separately if
1904 * have a clearly defined need to use and refcount the device
1905 * before it is added to the hierarchy.
1907 * For more information, see the kerneldoc for device_initialize()
1910 * NOTE: _Never_ directly free @dev after calling this function, even
1911 * if it returned an error! Always use put_device() to give up the
1912 * reference initialized in this function instead.
1914 int device_register(struct device
*dev
)
1916 device_initialize(dev
);
1917 return device_add(dev
);
1919 EXPORT_SYMBOL_GPL(device_register
);
1922 * get_device - increment reference count for device.
1925 * This simply forwards the call to kobject_get(), though
1926 * we do take care to provide for the case that we get a NULL
1927 * pointer passed in.
1929 struct device
*get_device(struct device
*dev
)
1931 return dev
? kobj_to_dev(kobject_get(&dev
->kobj
)) : NULL
;
1933 EXPORT_SYMBOL_GPL(get_device
);
1936 * put_device - decrement reference count.
1937 * @dev: device in question.
1939 void put_device(struct device
*dev
)
1941 /* might_sleep(); */
1943 kobject_put(&dev
->kobj
);
1945 EXPORT_SYMBOL_GPL(put_device
);
1948 * device_del - delete device from system.
1951 * This is the first part of the device unregistration
1952 * sequence. This removes the device from the lists we control
1953 * from here, has it removed from the other driver model
1954 * subsystems it was added to in device_add(), and removes it
1955 * from the kobject hierarchy.
1957 * NOTE: this should be called manually _iff_ device_add() was
1958 * also called manually.
1960 void device_del(struct device
*dev
)
1962 struct device
*parent
= dev
->parent
;
1963 struct kobject
*glue_dir
= NULL
;
1964 struct class_interface
*class_intf
;
1966 /* Notify clients of device removal. This call must come
1967 * before dpm_sysfs_remove().
1970 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1971 BUS_NOTIFY_DEL_DEVICE
, dev
);
1973 dpm_sysfs_remove(dev
);
1975 klist_del(&dev
->p
->knode_parent
);
1976 if (MAJOR(dev
->devt
)) {
1977 devtmpfs_delete_node(dev
);
1978 device_remove_sys_dev_entry(dev
);
1979 device_remove_file(dev
, &dev_attr_dev
);
1982 device_remove_class_symlinks(dev
);
1984 mutex_lock(&dev
->class->p
->mutex
);
1985 /* notify any interfaces that the device is now gone */
1986 list_for_each_entry(class_intf
,
1987 &dev
->class->p
->interfaces
, node
)
1988 if (class_intf
->remove_dev
)
1989 class_intf
->remove_dev(dev
, class_intf
);
1990 /* remove the device from the class list */
1991 klist_del(&dev
->knode_class
);
1992 mutex_unlock(&dev
->class->p
->mutex
);
1994 device_remove_file(dev
, &dev_attr_uevent
);
1995 device_remove_attrs(dev
);
1996 bus_remove_device(dev
);
1997 device_pm_remove(dev
);
1998 driver_deferred_probe_del(dev
);
1999 device_remove_properties(dev
);
2000 device_links_purge(dev
);
2002 /* Notify the platform of the removal, in case they
2003 * need to do anything...
2005 if (platform_notify_remove
)
2006 platform_notify_remove(dev
);
2008 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2009 BUS_NOTIFY_REMOVED_DEVICE
, dev
);
2010 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
2011 glue_dir
= get_glue_dir(dev
);
2012 kobject_del(&dev
->kobj
);
2013 cleanup_glue_dir(dev
, glue_dir
);
2016 EXPORT_SYMBOL_GPL(device_del
);
2019 * device_unregister - unregister device from system.
2020 * @dev: device going away.
2022 * We do this in two parts, like we do device_register(). First,
2023 * we remove it from all the subsystems with device_del(), then
2024 * we decrement the reference count via put_device(). If that
2025 * is the final reference count, the device will be cleaned up
2026 * via device_release() above. Otherwise, the structure will
2027 * stick around until the final reference to the device is dropped.
2029 void device_unregister(struct device
*dev
)
2031 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2035 EXPORT_SYMBOL_GPL(device_unregister
);
2037 static struct device
*prev_device(struct klist_iter
*i
)
2039 struct klist_node
*n
= klist_prev(i
);
2040 struct device
*dev
= NULL
;
2041 struct device_private
*p
;
2044 p
= to_device_private_parent(n
);
2050 static struct device
*next_device(struct klist_iter
*i
)
2052 struct klist_node
*n
= klist_next(i
);
2053 struct device
*dev
= NULL
;
2054 struct device_private
*p
;
2057 p
= to_device_private_parent(n
);
2064 * device_get_devnode - path of device node file
2066 * @mode: returned file access mode
2067 * @uid: returned file owner
2068 * @gid: returned file group
2069 * @tmp: possibly allocated string
2071 * Return the relative path of a possible device node.
2072 * Non-default names may need to allocate a memory to compose
2073 * a name. This memory is returned in tmp and needs to be
2074 * freed by the caller.
2076 const char *device_get_devnode(struct device
*dev
,
2077 umode_t
*mode
, kuid_t
*uid
, kgid_t
*gid
,
2084 /* the device type may provide a specific name */
2085 if (dev
->type
&& dev
->type
->devnode
)
2086 *tmp
= dev
->type
->devnode(dev
, mode
, uid
, gid
);
2090 /* the class may provide a specific name */
2091 if (dev
->class && dev
->class->devnode
)
2092 *tmp
= dev
->class->devnode(dev
, mode
);
2096 /* return name without allocation, tmp == NULL */
2097 if (strchr(dev_name(dev
), '!') == NULL
)
2098 return dev_name(dev
);
2100 /* replace '!' in the name with '/' */
2101 s
= kstrdup(dev_name(dev
), GFP_KERNEL
);
2104 strreplace(s
, '!', '/');
2109 * device_for_each_child - device child iterator.
2110 * @parent: parent struct device.
2111 * @fn: function to be called for each device.
2112 * @data: data for the callback.
2114 * Iterate over @parent's child devices, and call @fn for each,
2117 * We check the return of @fn each time. If it returns anything
2118 * other than 0, we break out and return that value.
2120 int device_for_each_child(struct device
*parent
, void *data
,
2121 int (*fn
)(struct device
*dev
, void *data
))
2123 struct klist_iter i
;
2124 struct device
*child
;
2130 klist_iter_init(&parent
->p
->klist_children
, &i
);
2131 while ((child
= next_device(&i
)) && !error
)
2132 error
= fn(child
, data
);
2133 klist_iter_exit(&i
);
2136 EXPORT_SYMBOL_GPL(device_for_each_child
);
2139 * device_for_each_child_reverse - device child iterator in reversed order.
2140 * @parent: parent struct device.
2141 * @fn: function to be called for each device.
2142 * @data: data for the callback.
2144 * Iterate over @parent's child devices, and call @fn for each,
2147 * We check the return of @fn each time. If it returns anything
2148 * other than 0, we break out and return that value.
2150 int device_for_each_child_reverse(struct device
*parent
, void *data
,
2151 int (*fn
)(struct device
*dev
, void *data
))
2153 struct klist_iter i
;
2154 struct device
*child
;
2160 klist_iter_init(&parent
->p
->klist_children
, &i
);
2161 while ((child
= prev_device(&i
)) && !error
)
2162 error
= fn(child
, data
);
2163 klist_iter_exit(&i
);
2166 EXPORT_SYMBOL_GPL(device_for_each_child_reverse
);
2169 * device_find_child - device iterator for locating a particular device.
2170 * @parent: parent struct device
2171 * @match: Callback function to check device
2172 * @data: Data to pass to match function
2174 * This is similar to the device_for_each_child() function above, but it
2175 * returns a reference to a device that is 'found' for later use, as
2176 * determined by the @match callback.
2178 * The callback should return 0 if the device doesn't match and non-zero
2179 * if it does. If the callback returns non-zero and a reference to the
2180 * current device can be obtained, this function will return to the caller
2181 * and not iterate over any more devices.
2183 * NOTE: you will need to drop the reference with put_device() after use.
2185 struct device
*device_find_child(struct device
*parent
, void *data
,
2186 int (*match
)(struct device
*dev
, void *data
))
2188 struct klist_iter i
;
2189 struct device
*child
;
2194 klist_iter_init(&parent
->p
->klist_children
, &i
);
2195 while ((child
= next_device(&i
)))
2196 if (match(child
, data
) && get_device(child
))
2198 klist_iter_exit(&i
);
2201 EXPORT_SYMBOL_GPL(device_find_child
);
2203 int __init
devices_init(void)
2205 devices_kset
= kset_create_and_add("devices", &device_uevent_ops
, NULL
);
2208 dev_kobj
= kobject_create_and_add("dev", NULL
);
2211 sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj
);
2212 if (!sysfs_dev_block_kobj
)
2213 goto block_kobj_err
;
2214 sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj
);
2215 if (!sysfs_dev_char_kobj
)
2221 kobject_put(sysfs_dev_block_kobj
);
2223 kobject_put(dev_kobj
);
2225 kset_unregister(devices_kset
);
2229 static int device_check_offline(struct device
*dev
, void *not_used
)
2233 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
2237 return device_supports_offline(dev
) && !dev
->offline
? -EBUSY
: 0;
2241 * device_offline - Prepare the device for hot-removal.
2242 * @dev: Device to be put offline.
2244 * Execute the device bus type's .offline() callback, if present, to prepare
2245 * the device for a subsequent hot-removal. If that succeeds, the device must
2246 * not be used until either it is removed or its bus type's .online() callback
2249 * Call under device_hotplug_lock.
2251 int device_offline(struct device
*dev
)
2255 if (dev
->offline_disabled
)
2258 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
2263 if (device_supports_offline(dev
)) {
2267 ret
= dev
->bus
->offline(dev
);
2269 kobject_uevent(&dev
->kobj
, KOBJ_OFFLINE
);
2270 dev
->offline
= true;
2280 * device_online - Put the device back online after successful device_offline().
2281 * @dev: Device to be put back online.
2283 * If device_offline() has been successfully executed for @dev, but the device
2284 * has not been removed subsequently, execute its bus type's .online() callback
2285 * to indicate that the device can be used again.
2287 * Call under device_hotplug_lock.
2289 int device_online(struct device
*dev
)
2294 if (device_supports_offline(dev
)) {
2296 ret
= dev
->bus
->online(dev
);
2298 kobject_uevent(&dev
->kobj
, KOBJ_ONLINE
);
2299 dev
->offline
= false;
2310 struct root_device
{
2312 struct module
*owner
;
2315 static inline struct root_device
*to_root_device(struct device
*d
)
2317 return container_of(d
, struct root_device
, dev
);
2320 static void root_device_release(struct device
*dev
)
2322 kfree(to_root_device(dev
));
2326 * __root_device_register - allocate and register a root device
2327 * @name: root device name
2328 * @owner: owner module of the root device, usually THIS_MODULE
2330 * This function allocates a root device and registers it
2331 * using device_register(). In order to free the returned
2332 * device, use root_device_unregister().
2334 * Root devices are dummy devices which allow other devices
2335 * to be grouped under /sys/devices. Use this function to
2336 * allocate a root device and then use it as the parent of
2337 * any device which should appear under /sys/devices/{name}
2339 * The /sys/devices/{name} directory will also contain a
2340 * 'module' symlink which points to the @owner directory
2343 * Returns &struct device pointer on success, or ERR_PTR() on error.
2345 * Note: You probably want to use root_device_register().
2347 struct device
*__root_device_register(const char *name
, struct module
*owner
)
2349 struct root_device
*root
;
2352 root
= kzalloc(sizeof(struct root_device
), GFP_KERNEL
);
2354 return ERR_PTR(err
);
2356 err
= dev_set_name(&root
->dev
, "%s", name
);
2359 return ERR_PTR(err
);
2362 root
->dev
.release
= root_device_release
;
2364 err
= device_register(&root
->dev
);
2366 put_device(&root
->dev
);
2367 return ERR_PTR(err
);
2370 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2372 struct module_kobject
*mk
= &owner
->mkobj
;
2374 err
= sysfs_create_link(&root
->dev
.kobj
, &mk
->kobj
, "module");
2376 device_unregister(&root
->dev
);
2377 return ERR_PTR(err
);
2379 root
->owner
= owner
;
2385 EXPORT_SYMBOL_GPL(__root_device_register
);
2388 * root_device_unregister - unregister and free a root device
2389 * @dev: device going away
2391 * This function unregisters and cleans up a device that was created by
2392 * root_device_register().
2394 void root_device_unregister(struct device
*dev
)
2396 struct root_device
*root
= to_root_device(dev
);
2399 sysfs_remove_link(&root
->dev
.kobj
, "module");
2401 device_unregister(dev
);
2403 EXPORT_SYMBOL_GPL(root_device_unregister
);
2406 static void device_create_release(struct device
*dev
)
2408 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2412 static struct device
*
2413 device_create_groups_vargs(struct class *class, struct device
*parent
,
2414 dev_t devt
, void *drvdata
,
2415 const struct attribute_group
**groups
,
2416 const char *fmt
, va_list args
)
2418 struct device
*dev
= NULL
;
2419 int retval
= -ENODEV
;
2421 if (class == NULL
|| IS_ERR(class))
2424 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2430 device_initialize(dev
);
2433 dev
->parent
= parent
;
2434 dev
->groups
= groups
;
2435 dev
->release
= device_create_release
;
2436 dev_set_drvdata(dev
, drvdata
);
2438 retval
= kobject_set_name_vargs(&dev
->kobj
, fmt
, args
);
2442 retval
= device_add(dev
);
2450 return ERR_PTR(retval
);
2454 * device_create_vargs - creates a device and registers it with sysfs
2455 * @class: pointer to the struct class that this device should be registered to
2456 * @parent: pointer to the parent struct device of this new device, if any
2457 * @devt: the dev_t for the char device to be added
2458 * @drvdata: the data to be added to the device for callbacks
2459 * @fmt: string for the device's name
2460 * @args: va_list for the device's name
2462 * This function can be used by char device classes. A struct device
2463 * will be created in sysfs, registered to the specified class.
2465 * A "dev" file will be created, showing the dev_t for the device, if
2466 * the dev_t is not 0,0.
2467 * If a pointer to a parent struct device is passed in, the newly created
2468 * struct device will be a child of that device in sysfs.
2469 * The pointer to the struct device will be returned from the call.
2470 * Any further sysfs files that might be required can be created using this
2473 * Returns &struct device pointer on success, or ERR_PTR() on error.
2475 * Note: the struct class passed to this function must have previously
2476 * been created with a call to class_create().
2478 struct device
*device_create_vargs(struct class *class, struct device
*parent
,
2479 dev_t devt
, void *drvdata
, const char *fmt
,
2482 return device_create_groups_vargs(class, parent
, devt
, drvdata
, NULL
,
2485 EXPORT_SYMBOL_GPL(device_create_vargs
);
2488 * device_create - creates a device and registers it with sysfs
2489 * @class: pointer to the struct class that this device should be registered to
2490 * @parent: pointer to the parent struct device of this new device, if any
2491 * @devt: the dev_t for the char device to be added
2492 * @drvdata: the data to be added to the device for callbacks
2493 * @fmt: string for the device's name
2495 * This function can be used by char device classes. A struct device
2496 * will be created in sysfs, registered to the specified class.
2498 * A "dev" file will be created, showing the dev_t for the device, if
2499 * the dev_t is not 0,0.
2500 * If a pointer to a parent struct device is passed in, the newly created
2501 * struct device will be a child of that device in sysfs.
2502 * The pointer to the struct device will be returned from the call.
2503 * Any further sysfs files that might be required can be created using this
2506 * Returns &struct device pointer on success, or ERR_PTR() on error.
2508 * Note: the struct class passed to this function must have previously
2509 * been created with a call to class_create().
2511 struct device
*device_create(struct class *class, struct device
*parent
,
2512 dev_t devt
, void *drvdata
, const char *fmt
, ...)
2517 va_start(vargs
, fmt
);
2518 dev
= device_create_vargs(class, parent
, devt
, drvdata
, fmt
, vargs
);
2522 EXPORT_SYMBOL_GPL(device_create
);
2525 * device_create_with_groups - creates a device and registers it with sysfs
2526 * @class: pointer to the struct class that this device should be registered to
2527 * @parent: pointer to the parent struct device of this new device, if any
2528 * @devt: the dev_t for the char device to be added
2529 * @drvdata: the data to be added to the device for callbacks
2530 * @groups: NULL-terminated list of attribute groups to be created
2531 * @fmt: string for the device's name
2533 * This function can be used by char device classes. A struct device
2534 * will be created in sysfs, registered to the specified class.
2535 * Additional attributes specified in the groups parameter will also
2536 * be created automatically.
2538 * A "dev" file will be created, showing the dev_t for the device, if
2539 * the dev_t is not 0,0.
2540 * If a pointer to a parent struct device is passed in, the newly created
2541 * struct device will be a child of that device in sysfs.
2542 * The pointer to the struct device will be returned from the call.
2543 * Any further sysfs files that might be required can be created using this
2546 * Returns &struct device pointer on success, or ERR_PTR() on error.
2548 * Note: the struct class passed to this function must have previously
2549 * been created with a call to class_create().
2551 struct device
*device_create_with_groups(struct class *class,
2552 struct device
*parent
, dev_t devt
,
2554 const struct attribute_group
**groups
,
2555 const char *fmt
, ...)
2560 va_start(vargs
, fmt
);
2561 dev
= device_create_groups_vargs(class, parent
, devt
, drvdata
, groups
,
2566 EXPORT_SYMBOL_GPL(device_create_with_groups
);
2568 static int __match_devt(struct device
*dev
, const void *data
)
2570 const dev_t
*devt
= data
;
2572 return dev
->devt
== *devt
;
2576 * device_destroy - removes a device that was created with device_create()
2577 * @class: pointer to the struct class that this device was registered with
2578 * @devt: the dev_t of the device that was previously registered
2580 * This call unregisters and cleans up a device that was created with a
2581 * call to device_create().
2583 void device_destroy(struct class *class, dev_t devt
)
2587 dev
= class_find_device(class, NULL
, &devt
, __match_devt
);
2590 device_unregister(dev
);
2593 EXPORT_SYMBOL_GPL(device_destroy
);
2596 * device_rename - renames a device
2597 * @dev: the pointer to the struct device to be renamed
2598 * @new_name: the new name of the device
2600 * It is the responsibility of the caller to provide mutual
2601 * exclusion between two different calls of device_rename
2602 * on the same device to ensure that new_name is valid and
2603 * won't conflict with other devices.
2605 * Note: Don't call this function. Currently, the networking layer calls this
2606 * function, but that will change. The following text from Kay Sievers offers
2609 * Renaming devices is racy at many levels, symlinks and other stuff are not
2610 * replaced atomically, and you get a "move" uevent, but it's not easy to
2611 * connect the event to the old and new device. Device nodes are not renamed at
2612 * all, there isn't even support for that in the kernel now.
2614 * In the meantime, during renaming, your target name might be taken by another
2615 * driver, creating conflicts. Or the old name is taken directly after you
2616 * renamed it -- then you get events for the same DEVPATH, before you even see
2617 * the "move" event. It's just a mess, and nothing new should ever rely on
2618 * kernel device renaming. Besides that, it's not even implemented now for
2619 * other things than (driver-core wise very simple) network devices.
2621 * We are currently about to change network renaming in udev to completely
2622 * disallow renaming of devices in the same namespace as the kernel uses,
2623 * because we can't solve the problems properly, that arise with swapping names
2624 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2625 * be allowed to some other name than eth[0-9]*, for the aforementioned
2628 * Make up a "real" name in the driver before you register anything, or add
2629 * some other attributes for userspace to find the device, or use udev to add
2630 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2631 * don't even want to get into that and try to implement the missing pieces in
2632 * the core. We really have other pieces to fix in the driver core mess. :)
2634 int device_rename(struct device
*dev
, const char *new_name
)
2636 struct kobject
*kobj
= &dev
->kobj
;
2637 char *old_device_name
= NULL
;
2640 dev
= get_device(dev
);
2644 dev_dbg(dev
, "renaming to %s\n", new_name
);
2646 old_device_name
= kstrdup(dev_name(dev
), GFP_KERNEL
);
2647 if (!old_device_name
) {
2653 error
= sysfs_rename_link_ns(&dev
->class->p
->subsys
.kobj
,
2654 kobj
, old_device_name
,
2655 new_name
, kobject_namespace(kobj
));
2660 error
= kobject_rename(kobj
, new_name
);
2667 kfree(old_device_name
);
2671 EXPORT_SYMBOL_GPL(device_rename
);
2673 static int device_move_class_links(struct device
*dev
,
2674 struct device
*old_parent
,
2675 struct device
*new_parent
)
2680 sysfs_remove_link(&dev
->kobj
, "device");
2682 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
2688 * device_move - moves a device to a new parent
2689 * @dev: the pointer to the struct device to be moved
2690 * @new_parent: the new parent of the device (can by NULL)
2691 * @dpm_order: how to reorder the dpm_list
2693 int device_move(struct device
*dev
, struct device
*new_parent
,
2694 enum dpm_order dpm_order
)
2697 struct device
*old_parent
;
2698 struct kobject
*new_parent_kobj
;
2700 dev
= get_device(dev
);
2705 new_parent
= get_device(new_parent
);
2706 new_parent_kobj
= get_device_parent(dev
, new_parent
);
2708 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev
),
2709 __func__
, new_parent
? dev_name(new_parent
) : "<NULL>");
2710 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
2712 cleanup_glue_dir(dev
, new_parent_kobj
);
2713 put_device(new_parent
);
2716 old_parent
= dev
->parent
;
2717 dev
->parent
= new_parent
;
2719 klist_remove(&dev
->p
->knode_parent
);
2721 klist_add_tail(&dev
->p
->knode_parent
,
2722 &new_parent
->p
->klist_children
);
2723 set_dev_node(dev
, dev_to_node(new_parent
));
2727 error
= device_move_class_links(dev
, old_parent
, new_parent
);
2729 /* We ignore errors on cleanup since we're hosed anyway... */
2730 device_move_class_links(dev
, new_parent
, old_parent
);
2731 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
2733 klist_remove(&dev
->p
->knode_parent
);
2734 dev
->parent
= old_parent
;
2736 klist_add_tail(&dev
->p
->knode_parent
,
2737 &old_parent
->p
->klist_children
);
2738 set_dev_node(dev
, dev_to_node(old_parent
));
2741 cleanup_glue_dir(dev
, new_parent_kobj
);
2742 put_device(new_parent
);
2746 switch (dpm_order
) {
2747 case DPM_ORDER_NONE
:
2749 case DPM_ORDER_DEV_AFTER_PARENT
:
2750 device_pm_move_after(dev
, new_parent
);
2751 devices_kset_move_after(dev
, new_parent
);
2753 case DPM_ORDER_PARENT_BEFORE_DEV
:
2754 device_pm_move_before(new_parent
, dev
);
2755 devices_kset_move_before(new_parent
, dev
);
2757 case DPM_ORDER_DEV_LAST
:
2758 device_pm_move_last(dev
);
2759 devices_kset_move_last(dev
);
2763 put_device(old_parent
);
2769 EXPORT_SYMBOL_GPL(device_move
);
2772 * device_shutdown - call ->shutdown() on each device to shutdown.
2774 void device_shutdown(void)
2776 struct device
*dev
, *parent
;
2778 spin_lock(&devices_kset
->list_lock
);
2780 * Walk the devices list backward, shutting down each in turn.
2781 * Beware that device unplug events may also start pulling
2782 * devices offline, even as the system is shutting down.
2784 while (!list_empty(&devices_kset
->list
)) {
2785 dev
= list_entry(devices_kset
->list
.prev
, struct device
,
2789 * hold reference count of device's parent to
2790 * prevent it from being freed because parent's
2791 * lock is to be held
2793 parent
= get_device(dev
->parent
);
2796 * Make sure the device is off the kset list, in the
2797 * event that dev->*->shutdown() doesn't remove it.
2799 list_del_init(&dev
->kobj
.entry
);
2800 spin_unlock(&devices_kset
->list_lock
);
2802 /* hold lock to avoid race with probe/release */
2804 device_lock(parent
);
2807 /* Don't allow any more runtime suspends */
2808 pm_runtime_get_noresume(dev
);
2809 pm_runtime_barrier(dev
);
2811 if (dev
->class && dev
->class->shutdown_pre
) {
2813 dev_info(dev
, "shutdown_pre\n");
2814 dev
->class->shutdown_pre(dev
);
2816 if (dev
->bus
&& dev
->bus
->shutdown
) {
2818 dev_info(dev
, "shutdown\n");
2819 dev
->bus
->shutdown(dev
);
2820 } else if (dev
->driver
&& dev
->driver
->shutdown
) {
2822 dev_info(dev
, "shutdown\n");
2823 dev
->driver
->shutdown(dev
);
2828 device_unlock(parent
);
2833 spin_lock(&devices_kset
->list_lock
);
2835 spin_unlock(&devices_kset
->list_lock
);
2839 * Device logging functions
2842 #ifdef CONFIG_PRINTK
2844 create_syslog_header(const struct device
*dev
, char *hdr
, size_t hdrlen
)
2850 subsys
= dev
->class->name
;
2852 subsys
= dev
->bus
->name
;
2856 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
, "SUBSYSTEM=%s", subsys
);
2861 * Add device identifier DEVICE=:
2865 * +sound:card0 subsystem:devname
2867 if (MAJOR(dev
->devt
)) {
2870 if (strcmp(subsys
, "block") == 0)
2875 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
2877 c
, MAJOR(dev
->devt
), MINOR(dev
->devt
));
2878 } else if (strcmp(subsys
, "net") == 0) {
2879 struct net_device
*net
= to_net_dev(dev
);
2882 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
2883 "DEVICE=n%u", net
->ifindex
);
2886 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
2887 "DEVICE=+%s:%s", subsys
, dev_name(dev
));
2896 dev_WARN(dev
, "device/subsystem name too long");
2900 int dev_vprintk_emit(int level
, const struct device
*dev
,
2901 const char *fmt
, va_list args
)
2906 hdrlen
= create_syslog_header(dev
, hdr
, sizeof(hdr
));
2908 return vprintk_emit(0, level
, hdrlen
? hdr
: NULL
, hdrlen
, fmt
, args
);
2910 EXPORT_SYMBOL(dev_vprintk_emit
);
2912 int dev_printk_emit(int level
, const struct device
*dev
, const char *fmt
, ...)
2917 va_start(args
, fmt
);
2919 r
= dev_vprintk_emit(level
, dev
, fmt
, args
);
2925 EXPORT_SYMBOL(dev_printk_emit
);
2927 static void __dev_printk(const char *level
, const struct device
*dev
,
2928 struct va_format
*vaf
)
2931 dev_printk_emit(level
[1] - '0', dev
, "%s %s: %pV",
2932 dev_driver_string(dev
), dev_name(dev
), vaf
);
2934 printk("%s(NULL device *): %pV", level
, vaf
);
2937 void dev_printk(const char *level
, const struct device
*dev
,
2938 const char *fmt
, ...)
2940 struct va_format vaf
;
2943 va_start(args
, fmt
);
2948 __dev_printk(level
, dev
, &vaf
);
2952 EXPORT_SYMBOL(dev_printk
);
2954 #define define_dev_printk_level(func, kern_level) \
2955 void func(const struct device *dev, const char *fmt, ...) \
2957 struct va_format vaf; \
2960 va_start(args, fmt); \
2965 __dev_printk(kern_level, dev, &vaf); \
2969 EXPORT_SYMBOL(func);
2971 define_dev_printk_level(dev_emerg
, KERN_EMERG
);
2972 define_dev_printk_level(dev_alert
, KERN_ALERT
);
2973 define_dev_printk_level(dev_crit
, KERN_CRIT
);
2974 define_dev_printk_level(dev_err
, KERN_ERR
);
2975 define_dev_printk_level(dev_warn
, KERN_WARNING
);
2976 define_dev_printk_level(dev_notice
, KERN_NOTICE
);
2977 define_dev_printk_level(_dev_info
, KERN_INFO
);
2981 static inline bool fwnode_is_primary(struct fwnode_handle
*fwnode
)
2983 return fwnode
&& !IS_ERR(fwnode
->secondary
);
2987 * set_primary_fwnode - Change the primary firmware node of a given device.
2988 * @dev: Device to handle.
2989 * @fwnode: New primary firmware node of the device.
2991 * Set the device's firmware node pointer to @fwnode, but if a secondary
2992 * firmware node of the device is present, preserve it.
2994 void set_primary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
2997 struct fwnode_handle
*fn
= dev
->fwnode
;
2999 if (fwnode_is_primary(fn
))
3003 WARN_ON(fwnode
->secondary
);
3004 fwnode
->secondary
= fn
;
3006 dev
->fwnode
= fwnode
;
3008 dev
->fwnode
= fwnode_is_primary(dev
->fwnode
) ?
3009 dev
->fwnode
->secondary
: NULL
;
3012 EXPORT_SYMBOL_GPL(set_primary_fwnode
);
3015 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3016 * @dev: Device to handle.
3017 * @fwnode: New secondary firmware node of the device.
3019 * If a primary firmware node of the device is present, set its secondary
3020 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3023 void set_secondary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
3026 fwnode
->secondary
= ERR_PTR(-ENODEV
);
3028 if (fwnode_is_primary(dev
->fwnode
))
3029 dev
->fwnode
->secondary
= fwnode
;
3031 dev
->fwnode
= fwnode
;
3035 * device_set_of_node_from_dev - reuse device-tree node of another device
3036 * @dev: device whose device-tree node is being set
3037 * @dev2: device whose device-tree node is being reused
3039 * Takes another reference to the new device-tree node after first dropping
3040 * any reference held to the old node.
3042 void device_set_of_node_from_dev(struct device
*dev
, const struct device
*dev2
)
3044 of_node_put(dev
->of_node
);
3045 dev
->of_node
= of_node_get(dev2
->of_node
);
3046 dev
->of_node_reused
= true;
3048 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev
);