blob: 276c7e3f754c8395fbf470e528742cd98d63c724 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/base/core.c - core driver model code (device registration, etc)
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07007 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020013#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070018#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100019#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070020#include <linux/of.h>
21#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010022#include <linux/genhd.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070023#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010024#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020025#include <linux/netdevice.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070027#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "base.h"
30#include "power/power.h"
31
Andi Kleene52eec12010-09-08 16:54:17 +020032#ifdef CONFIG_SYSFS_DEPRECATED
33#ifdef CONFIG_SYSFS_DEPRECATED_V2
34long sysfs_deprecated = 1;
35#else
36long sysfs_deprecated = 0;
37#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080038static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020039{
Jingoo Han34da5e62013-07-26 13:10:22 +090040 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020041}
42early_param("sysfs.deprecated", sysfs_deprecated_setup);
43#endif
44
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010045/* Device links support. */
46
47#ifdef CONFIG_SRCU
48static DEFINE_MUTEX(device_links_lock);
49DEFINE_STATIC_SRCU(device_links_srcu);
50
51static inline void device_links_write_lock(void)
52{
53 mutex_lock(&device_links_lock);
54}
55
56static inline void device_links_write_unlock(void)
57{
58 mutex_unlock(&device_links_lock);
59}
60
61int device_links_read_lock(void)
62{
63 return srcu_read_lock(&device_links_srcu);
64}
65
66void device_links_read_unlock(int idx)
67{
68 srcu_read_unlock(&device_links_srcu, idx);
69}
70#else /* !CONFIG_SRCU */
71static DECLARE_RWSEM(device_links_lock);
72
73static inline void device_links_write_lock(void)
74{
75 down_write(&device_links_lock);
76}
77
78static inline void device_links_write_unlock(void)
79{
80 up_write(&device_links_lock);
81}
82
83int device_links_read_lock(void)
84{
85 down_read(&device_links_lock);
86 return 0;
87}
88
89void device_links_read_unlock(int not_used)
90{
91 up_read(&device_links_lock);
92}
93#endif /* !CONFIG_SRCU */
94
95/**
96 * device_is_dependent - Check if one device depends on another one
97 * @dev: Device to check dependencies for.
98 * @target: Device to check against.
99 *
100 * Check if @target depends on @dev or any device dependent on it (its child or
101 * its consumer etc). Return 1 if that is the case or 0 otherwise.
102 */
103static int device_is_dependent(struct device *dev, void *target)
104{
105 struct device_link *link;
106 int ret;
107
108 if (WARN_ON(dev == target))
109 return 1;
110
111 ret = device_for_each_child(dev, target, device_is_dependent);
112 if (ret)
113 return ret;
114
115 list_for_each_entry(link, &dev->links.consumers, s_node) {
116 if (WARN_ON(link->consumer == target))
117 return 1;
118
119 ret = device_is_dependent(link->consumer, target);
120 if (ret)
121 break;
122 }
123 return ret;
124}
125
126static int device_reorder_to_tail(struct device *dev, void *not_used)
127{
128 struct device_link *link;
129
130 /*
131 * Devices that have not been registered yet will be put to the ends
132 * of the lists during the registration, so skip them here.
133 */
134 if (device_is_registered(dev))
135 devices_kset_move_last(dev);
136
137 if (device_pm_initialized(dev))
138 device_pm_move_last(dev);
139
140 device_for_each_child(dev, NULL, device_reorder_to_tail);
141 list_for_each_entry(link, &dev->links.consumers, s_node)
142 device_reorder_to_tail(link->consumer, NULL);
143
144 return 0;
145}
146
147/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700148 * device_pm_move_to_tail - Move set of devices to the end of device lists
149 * @dev: Device to move
150 *
151 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
152 *
153 * It moves the @dev along with all of its children and all of its consumers
154 * to the ends of the device_kset and dpm_list, recursively.
155 */
156void device_pm_move_to_tail(struct device *dev)
157{
158 int idx;
159
160 idx = device_links_read_lock();
161 device_pm_lock();
162 device_reorder_to_tail(dev, NULL);
163 device_pm_unlock();
164 device_links_read_unlock(idx);
165}
166
167/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100168 * device_link_add - Create a link between two devices.
169 * @consumer: Consumer end of the link.
170 * @supplier: Supplier end of the link.
171 * @flags: Link flags.
172 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100173 * The caller is responsible for the proper synchronization of the link creation
174 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
175 * runtime PM framework to take the link into account. Second, if the
176 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
177 * be forced into the active metastate and reference-counted upon the creation
178 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
179 * ignored.
180 *
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100181 * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically
182 * when the consumer device driver unbinds from it. The combination of both
183 * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL
184 * to be returned.
185 *
186 * A side effect of the link creation is re-ordering of dpm_list and the
187 * devices_kset list by moving the consumer device and all devices depending
188 * on it to the ends of these lists (that does not happen to devices that have
189 * not been registered when this function is called).
190 *
191 * The supplier device is required to be registered when this function is called
192 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100193 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100194 */
195struct device_link *device_link_add(struct device *consumer,
196 struct device *supplier, u32 flags)
197{
198 struct device_link *link;
199
200 if (!consumer || !supplier ||
201 ((flags & DL_FLAG_STATELESS) && (flags & DL_FLAG_AUTOREMOVE)))
202 return NULL;
203
204 device_links_write_lock();
205 device_pm_lock();
206
207 /*
208 * If the supplier has not been fully registered yet or there is a
209 * reverse dependency between the consumer and the supplier already in
210 * the graph, return NULL.
211 */
212 if (!device_pm_initialized(supplier)
213 || device_is_dependent(consumer, supplier)) {
214 link = NULL;
215 goto out;
216 }
217
218 list_for_each_entry(link, &supplier->links.consumers, s_node)
Lukas Wunneread18c22018-02-10 19:27:12 +0100219 if (link->consumer == consumer) {
220 kref_get(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100221 goto out;
Lukas Wunneread18c22018-02-10 19:27:12 +0100222 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100223
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100224 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100225 if (!link)
226 goto out;
227
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100228 if (flags & DL_FLAG_PM_RUNTIME) {
229 if (flags & DL_FLAG_RPM_ACTIVE) {
230 if (pm_runtime_get_sync(supplier) < 0) {
231 pm_runtime_put_noidle(supplier);
232 kfree(link);
233 link = NULL;
234 goto out;
235 }
236 link->rpm_active = true;
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100237 }
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100238 pm_runtime_new_link(consumer);
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200239 /*
240 * If the link is being added by the consumer driver at probe
241 * time, balance the decrementation of the supplier's runtime PM
242 * usage counter after consumer probe in driver_probe_device().
243 */
244 if (consumer->links.status == DL_DEV_PROBING)
245 pm_runtime_get_noresume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100246 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100247 get_device(supplier);
248 link->supplier = supplier;
249 INIT_LIST_HEAD(&link->s_node);
250 get_device(consumer);
251 link->consumer = consumer;
252 INIT_LIST_HEAD(&link->c_node);
253 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100254 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100255
Lukas Wunner64df1142016-12-04 13:10:04 +0100256 /* Determine the initial link state. */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100257 if (flags & DL_FLAG_STATELESS) {
258 link->status = DL_STATE_NONE;
259 } else {
260 switch (supplier->links.status) {
261 case DL_DEV_DRIVER_BOUND:
262 switch (consumer->links.status) {
263 case DL_DEV_PROBING:
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100264 /*
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200265 * Some callers expect the link creation during
266 * consumer driver probe to resume the supplier
267 * even without DL_FLAG_RPM_ACTIVE.
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100268 */
269 if (flags & DL_FLAG_PM_RUNTIME)
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200270 pm_runtime_resume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100271
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100272 link->status = DL_STATE_CONSUMER_PROBE;
273 break;
274 case DL_DEV_DRIVER_BOUND:
275 link->status = DL_STATE_ACTIVE;
276 break;
277 default:
278 link->status = DL_STATE_AVAILABLE;
279 break;
280 }
281 break;
282 case DL_DEV_UNBINDING:
283 link->status = DL_STATE_SUPPLIER_UNBIND;
284 break;
285 default:
286 link->status = DL_STATE_DORMANT;
287 break;
288 }
289 }
290
291 /*
292 * Move the consumer and all of the devices depending on it to the end
293 * of dpm_list and the devices_kset list.
294 *
295 * It is necessary to hold dpm_list locked throughout all that or else
296 * we may end up suspending with a wrong ordering of it.
297 */
298 device_reorder_to_tail(consumer, NULL);
299
300 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
301 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
302
303 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
304
305 out:
306 device_pm_unlock();
307 device_links_write_unlock();
308 return link;
309}
310EXPORT_SYMBOL_GPL(device_link_add);
311
312static void device_link_free(struct device_link *link)
313{
314 put_device(link->consumer);
315 put_device(link->supplier);
316 kfree(link);
317}
318
319#ifdef CONFIG_SRCU
320static void __device_link_free_srcu(struct rcu_head *rhead)
321{
322 device_link_free(container_of(rhead, struct device_link, rcu_head));
323}
324
Lukas Wunneread18c22018-02-10 19:27:12 +0100325static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100326{
Lukas Wunneread18c22018-02-10 19:27:12 +0100327 struct device_link *link = container_of(kref, struct device_link, kref);
328
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100329 dev_info(link->consumer, "Dropping the link to %s\n",
330 dev_name(link->supplier));
331
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100332 if (link->flags & DL_FLAG_PM_RUNTIME)
333 pm_runtime_drop_link(link->consumer);
334
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100335 list_del_rcu(&link->s_node);
336 list_del_rcu(&link->c_node);
337 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
338}
339#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100340static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100341{
Lukas Wunneread18c22018-02-10 19:27:12 +0100342 struct device_link *link = container_of(kref, struct device_link, kref);
343
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100344 dev_info(link->consumer, "Dropping the link to %s\n",
345 dev_name(link->supplier));
346
Lukas Wunner433986c2018-02-10 19:13:58 +0100347 if (link->flags & DL_FLAG_PM_RUNTIME)
348 pm_runtime_drop_link(link->consumer);
349
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100350 list_del(&link->s_node);
351 list_del(&link->c_node);
352 device_link_free(link);
353}
354#endif /* !CONFIG_SRCU */
355
356/**
357 * device_link_del - Delete a link between two devices.
358 * @link: Device link to delete.
359 *
360 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100361 * PM. If the link was added multiple times, it needs to be deleted as often.
362 * Care is required for hotplugged devices: Their links are purged on removal
363 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100364 */
365void device_link_del(struct device_link *link)
366{
367 device_links_write_lock();
368 device_pm_lock();
Lukas Wunneread18c22018-02-10 19:27:12 +0100369 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100370 device_pm_unlock();
371 device_links_write_unlock();
372}
373EXPORT_SYMBOL_GPL(device_link_del);
374
375static void device_links_missing_supplier(struct device *dev)
376{
377 struct device_link *link;
378
379 list_for_each_entry(link, &dev->links.suppliers, c_node)
380 if (link->status == DL_STATE_CONSUMER_PROBE)
381 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
382}
383
384/**
385 * device_links_check_suppliers - Check presence of supplier drivers.
386 * @dev: Consumer device.
387 *
388 * Check links from this device to any suppliers. Walk the list of the device's
389 * links to suppliers and see if all of them are available. If not, simply
390 * return -EPROBE_DEFER.
391 *
392 * We need to guarantee that the supplier will not go away after the check has
393 * been positive here. It only can go away in __device_release_driver() and
394 * that function checks the device's links to consumers. This means we need to
395 * mark the link as "consumer probe in progress" to make the supplier removal
396 * wait for us to complete (or bad things may happen).
397 *
398 * Links with the DL_FLAG_STATELESS flag set are ignored.
399 */
400int device_links_check_suppliers(struct device *dev)
401{
402 struct device_link *link;
403 int ret = 0;
404
405 device_links_write_lock();
406
407 list_for_each_entry(link, &dev->links.suppliers, c_node) {
408 if (link->flags & DL_FLAG_STATELESS)
409 continue;
410
411 if (link->status != DL_STATE_AVAILABLE) {
412 device_links_missing_supplier(dev);
413 ret = -EPROBE_DEFER;
414 break;
415 }
416 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
417 }
418 dev->links.status = DL_DEV_PROBING;
419
420 device_links_write_unlock();
421 return ret;
422}
423
424/**
425 * device_links_driver_bound - Update device links after probing its driver.
426 * @dev: Device to update the links for.
427 *
428 * The probe has been successful, so update links from this device to any
429 * consumers by changing their status to "available".
430 *
431 * Also change the status of @dev's links to suppliers to "active".
432 *
433 * Links with the DL_FLAG_STATELESS flag set are ignored.
434 */
435void device_links_driver_bound(struct device *dev)
436{
437 struct device_link *link;
438
439 device_links_write_lock();
440
441 list_for_each_entry(link, &dev->links.consumers, s_node) {
442 if (link->flags & DL_FLAG_STATELESS)
443 continue;
444
445 WARN_ON(link->status != DL_STATE_DORMANT);
446 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
447 }
448
449 list_for_each_entry(link, &dev->links.suppliers, c_node) {
450 if (link->flags & DL_FLAG_STATELESS)
451 continue;
452
453 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
454 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
455 }
456
457 dev->links.status = DL_DEV_DRIVER_BOUND;
458
459 device_links_write_unlock();
460}
461
462/**
463 * __device_links_no_driver - Update links of a device without a driver.
464 * @dev: Device without a drvier.
465 *
466 * Delete all non-persistent links from this device to any suppliers.
467 *
468 * Persistent links stay around, but their status is changed to "available",
469 * unless they already are in the "supplier unbind in progress" state in which
470 * case they need not be updated.
471 *
472 * Links with the DL_FLAG_STATELESS flag set are ignored.
473 */
474static void __device_links_no_driver(struct device *dev)
475{
476 struct device_link *link, *ln;
477
478 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
479 if (link->flags & DL_FLAG_STATELESS)
480 continue;
481
482 if (link->flags & DL_FLAG_AUTOREMOVE)
Lukas Wunneread18c22018-02-10 19:27:12 +0100483 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100484 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
485 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
486 }
487
488 dev->links.status = DL_DEV_NO_DRIVER;
489}
490
491void device_links_no_driver(struct device *dev)
492{
493 device_links_write_lock();
494 __device_links_no_driver(dev);
495 device_links_write_unlock();
496}
497
498/**
499 * device_links_driver_cleanup - Update links after driver removal.
500 * @dev: Device whose driver has just gone away.
501 *
502 * Update links to consumers for @dev by changing their status to "dormant" and
503 * invoke %__device_links_no_driver() to update links to suppliers for it as
504 * appropriate.
505 *
506 * Links with the DL_FLAG_STATELESS flag set are ignored.
507 */
508void device_links_driver_cleanup(struct device *dev)
509{
510 struct device_link *link;
511
512 device_links_write_lock();
513
514 list_for_each_entry(link, &dev->links.consumers, s_node) {
515 if (link->flags & DL_FLAG_STATELESS)
516 continue;
517
518 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
519 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
520 WRITE_ONCE(link->status, DL_STATE_DORMANT);
521 }
522
523 __device_links_no_driver(dev);
524
525 device_links_write_unlock();
526}
527
528/**
529 * device_links_busy - Check if there are any busy links to consumers.
530 * @dev: Device to check.
531 *
532 * Check each consumer of the device and return 'true' if its link's status
533 * is one of "consumer probe" or "active" (meaning that the given consumer is
534 * probing right now or its driver is present). Otherwise, change the link
535 * state to "supplier unbind" to prevent the consumer from being probed
536 * successfully going forward.
537 *
538 * Return 'false' if there are no probing or active consumers.
539 *
540 * Links with the DL_FLAG_STATELESS flag set are ignored.
541 */
542bool device_links_busy(struct device *dev)
543{
544 struct device_link *link;
545 bool ret = false;
546
547 device_links_write_lock();
548
549 list_for_each_entry(link, &dev->links.consumers, s_node) {
550 if (link->flags & DL_FLAG_STATELESS)
551 continue;
552
553 if (link->status == DL_STATE_CONSUMER_PROBE
554 || link->status == DL_STATE_ACTIVE) {
555 ret = true;
556 break;
557 }
558 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
559 }
560
561 dev->links.status = DL_DEV_UNBINDING;
562
563 device_links_write_unlock();
564 return ret;
565}
566
567/**
568 * device_links_unbind_consumers - Force unbind consumers of the given device.
569 * @dev: Device to unbind the consumers of.
570 *
571 * Walk the list of links to consumers for @dev and if any of them is in the
572 * "consumer probe" state, wait for all device probes in progress to complete
573 * and start over.
574 *
575 * If that's not the case, change the status of the link to "supplier unbind"
576 * and check if the link was in the "active" state. If so, force the consumer
577 * driver to unbind and start over (the consumer will not re-probe as we have
578 * changed the state of the link already).
579 *
580 * Links with the DL_FLAG_STATELESS flag set are ignored.
581 */
582void device_links_unbind_consumers(struct device *dev)
583{
584 struct device_link *link;
585
586 start:
587 device_links_write_lock();
588
589 list_for_each_entry(link, &dev->links.consumers, s_node) {
590 enum device_link_state status;
591
592 if (link->flags & DL_FLAG_STATELESS)
593 continue;
594
595 status = link->status;
596 if (status == DL_STATE_CONSUMER_PROBE) {
597 device_links_write_unlock();
598
599 wait_for_device_probe();
600 goto start;
601 }
602 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
603 if (status == DL_STATE_ACTIVE) {
604 struct device *consumer = link->consumer;
605
606 get_device(consumer);
607
608 device_links_write_unlock();
609
610 device_release_driver_internal(consumer, NULL,
611 consumer->parent);
612 put_device(consumer);
613 goto start;
614 }
615 }
616
617 device_links_write_unlock();
618}
619
620/**
621 * device_links_purge - Delete existing links to other devices.
622 * @dev: Target device.
623 */
624static void device_links_purge(struct device *dev)
625{
626 struct device_link *link, *ln;
627
628 /*
629 * Delete all of the remaining links from this device to any other
630 * devices (either consumers or suppliers).
631 */
632 device_links_write_lock();
633
634 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
635 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100636 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100637 }
638
639 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
640 WARN_ON(link->status != DL_STATE_DORMANT &&
641 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100642 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100643 }
644
645 device_links_write_unlock();
646}
647
648/* Device links support end. */
649
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800650int (*platform_notify)(struct device *dev) = NULL;
651int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700652static struct kobject *dev_kobj;
653struct kobject *sysfs_dev_char_kobj;
654struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200656static DEFINE_MUTEX(device_hotplug_lock);
657
658void lock_device_hotplug(void)
659{
660 mutex_lock(&device_hotplug_lock);
661}
662
663void unlock_device_hotplug(void)
664{
665 mutex_unlock(&device_hotplug_lock);
666}
667
668int lock_device_hotplug_sysfs(void)
669{
670 if (mutex_trylock(&device_hotplug_lock))
671 return 0;
672
673 /* Avoid busy looping (5 ms of sleep should do). */
674 msleep(5);
675 return restart_syscall();
676}
677
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800678#ifdef CONFIG_BLOCK
679static inline int device_is_not_partition(struct device *dev)
680{
681 return !(dev->type == &part_type);
682}
683#else
684static inline int device_is_not_partition(struct device *dev)
685{
686 return 1;
687}
688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Alan Stern3e956372006-06-16 17:10:48 -0400690/**
691 * dev_driver_string - Return a device's driver name, if at all possible
692 * @dev: struct device to get the name of
693 *
694 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800695 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400696 * it is attached to. If it is not attached to a bus either, an empty
697 * string will be returned.
698 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700699const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400700{
Alan Stern35899722009-12-04 11:06:57 -0500701 struct device_driver *drv;
702
703 /* dev->driver can change to NULL underneath us because of unbinding,
704 * so be careful about accessing it. dev->bus and dev->class should
705 * never change once they are set, so they don't need special care.
706 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700707 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500708 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100709 (dev->bus ? dev->bus->name :
710 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400711}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600712EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
715
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800716static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
717 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800719 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200720 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500721 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400724 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800725 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900726 printk("dev_attr_show: %pS returned bad count\n",
727 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return ret;
730}
731
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800732static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
733 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800735 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200736 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500737 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400740 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return ret;
742}
743
Emese Revfy52cf25d2010-01-19 02:58:23 +0100744static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 .show = dev_attr_show,
746 .store = dev_attr_store,
747};
748
Kay Sieversca22e562011-12-14 14:29:38 -0800749#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
750
751ssize_t device_store_ulong(struct device *dev,
752 struct device_attribute *attr,
753 const char *buf, size_t size)
754{
755 struct dev_ext_attribute *ea = to_ext_attr(attr);
756 char *end;
757 unsigned long new = simple_strtoul(buf, &end, 0);
758 if (end == buf)
759 return -EINVAL;
760 *(unsigned long *)(ea->var) = new;
761 /* Always return full write size even if we didn't consume all */
762 return size;
763}
764EXPORT_SYMBOL_GPL(device_store_ulong);
765
766ssize_t device_show_ulong(struct device *dev,
767 struct device_attribute *attr,
768 char *buf)
769{
770 struct dev_ext_attribute *ea = to_ext_attr(attr);
771 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
772}
773EXPORT_SYMBOL_GPL(device_show_ulong);
774
775ssize_t device_store_int(struct device *dev,
776 struct device_attribute *attr,
777 const char *buf, size_t size)
778{
779 struct dev_ext_attribute *ea = to_ext_attr(attr);
780 char *end;
781 long new = simple_strtol(buf, &end, 0);
782 if (end == buf || new > INT_MAX || new < INT_MIN)
783 return -EINVAL;
784 *(int *)(ea->var) = new;
785 /* Always return full write size even if we didn't consume all */
786 return size;
787}
788EXPORT_SYMBOL_GPL(device_store_int);
789
790ssize_t device_show_int(struct device *dev,
791 struct device_attribute *attr,
792 char *buf)
793{
794 struct dev_ext_attribute *ea = to_ext_attr(attr);
795
796 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
797}
798EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Borislav Petkov91872392012-10-09 19:52:05 +0200800ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
801 const char *buf, size_t size)
802{
803 struct dev_ext_attribute *ea = to_ext_attr(attr);
804
805 if (strtobool(buf, ea->var) < 0)
806 return -EINVAL;
807
808 return size;
809}
810EXPORT_SYMBOL_GPL(device_store_bool);
811
812ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
813 char *buf)
814{
815 struct dev_ext_attribute *ea = to_ext_attr(attr);
816
817 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
818}
819EXPORT_SYMBOL_GPL(device_show_bool);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400822 * device_release - free device structure.
823 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400825 * This is called once the reference count for the object
826 * reaches 0. We forward the call to the device's release
827 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800829static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200831 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800832 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Ming Leia525a3d2012-07-25 01:42:29 +0800834 /*
835 * Some platform devices are driven without driver attached
836 * and managed resources may have been acquired. Make sure
837 * all resources are released.
838 *
839 * Drivers still can add resources into device after device
840 * is deleted but alive, so release devres here to avoid
841 * possible memory leak.
842 */
843 devres_release_all(dev);
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (dev->release)
846 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200847 else if (dev->type && dev->type->release)
848 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700849 else if (dev->class && dev->class->dev_release)
850 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700851 else
852 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800853 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100854 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800855 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700858static const void *device_namespace(struct kobject *kobj)
859{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200860 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700861 const void *ns = NULL;
862
863 if (dev->class && dev->class->ns_type)
864 ns = dev->class->namespace(dev);
865
866 return ns;
867}
868
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000869static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
870{
871 struct device *dev = kobj_to_dev(kobj);
872
873 if (dev->class && dev->class->get_ownership)
874 dev->class->get_ownership(dev, uid, gid);
875}
876
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600877static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 .release = device_release,
879 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700880 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000881 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882};
883
884
Kay Sievers312c0042005-11-16 09:00:00 +0100885static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct kobj_type *ktype = get_ktype(kobj);
888
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600889 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200890 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (dev->bus)
892 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700893 if (dev->class)
894 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 return 0;
897}
898
Kay Sievers312c0042005-11-16 09:00:00 +0100899static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200901 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700903 if (dev->bus)
904 return dev->bus->name;
905 if (dev->class)
906 return dev->class->name;
907 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Kay Sievers7eff2e72007-08-14 15:15:12 +0200910static int dev_uevent(struct kset *kset, struct kobject *kobj,
911 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200913 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int retval = 0;
915
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200916 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700917 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200918 const char *tmp;
919 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400920 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700921 kuid_t uid = GLOBAL_ROOT_UID;
922 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200923
Kay Sievers7eff2e72007-08-14 15:15:12 +0200924 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
925 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700926 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200927 if (name) {
928 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200929 if (mode)
930 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700931 if (!uid_eq(uid, GLOBAL_ROOT_UID))
932 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
933 if (!gid_eq(gid, GLOBAL_ROOT_GID))
934 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700935 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200936 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700937 }
938
Kay Sievers414264f2007-03-12 21:08:57 +0100939 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200940 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100941
Kay Sievers239378f2006-10-07 21:54:55 +0200942 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200943 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200944
Grant Likely07d57a32012-02-01 11:22:22 -0700945 /* Add common DT information about the device */
946 of_device_uevent(dev, env);
947
Kay Sievers7eff2e72007-08-14 15:15:12 +0200948 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100949 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200950 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200951 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800952 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100953 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
Kay Sievers7eff2e72007-08-14 15:15:12 +0200956 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700957 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200958 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200959 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800960 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100961 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800962 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200963 }
964
Stefan Weileef35c22010-08-06 21:11:15 +0200965 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200966 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200967 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200968 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800969 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100970 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800971 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700972 }
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return retval;
975}
976
Emese Revfy9cd43612009-12-31 14:52:51 +0100977static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100978 .filter = dev_uevent_filter,
979 .name = dev_uevent_name,
980 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981};
982
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700983static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +0200984 char *buf)
985{
986 struct kobject *top_kobj;
987 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200988 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200989 int i;
990 size_t count = 0;
991 int retval;
992
993 /* search the kset, the device belongs to */
994 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200995 while (!top_kobj->kset && top_kobj->parent)
996 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200997 if (!top_kobj->kset)
998 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200999
Kay Sievers16574dc2007-04-06 01:40:38 +02001000 kset = top_kobj->kset;
1001 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1002 goto out;
1003
1004 /* respect filter */
1005 if (kset->uevent_ops && kset->uevent_ops->filter)
1006 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1007 goto out;
1008
Kay Sievers7eff2e72007-08-14 15:15:12 +02001009 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1010 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001011 return -ENOMEM;
1012
Kay Sievers16574dc2007-04-06 01:40:38 +02001013 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001014 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001015 if (retval)
1016 goto out;
1017
1018 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001019 for (i = 0; i < env->envp_idx; i++)
1020 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001021out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001022 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001023 return count;
1024}
1025
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001026static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001027 const char *buf, size_t count)
1028{
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001029 if (kobject_synth_uevent(&dev->kobj, buf, count))
1030 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Kay Sievers60a96a52007-07-08 22:29:26 +02001031
Kay Sieversa7fd6702005-10-01 14:49:43 +02001032 return count;
1033}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001034static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001035
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001036static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001037 char *buf)
1038{
1039 bool val;
1040
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001041 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001042 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001043 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001044 return sprintf(buf, "%u\n", val);
1045}
1046
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001047static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001048 const char *buf, size_t count)
1049{
1050 bool val;
1051 int ret;
1052
1053 ret = strtobool(buf, &val);
1054 if (ret < 0)
1055 return ret;
1056
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001057 ret = lock_device_hotplug_sysfs();
1058 if (ret)
1059 return ret;
1060
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001061 ret = val ? device_online(dev) : device_offline(dev);
1062 unlock_device_hotplug();
1063 return ret < 0 ? ret : count;
1064}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001065static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001066
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001067int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001068{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001069 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001070}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001071EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001072
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001073void device_remove_groups(struct device *dev,
1074 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001075{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001076 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001077}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001078EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001079
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001080union device_attr_group_devres {
1081 const struct attribute_group *group;
1082 const struct attribute_group **groups;
1083};
1084
1085static int devm_attr_group_match(struct device *dev, void *res, void *data)
1086{
1087 return ((union device_attr_group_devres *)res)->group == data;
1088}
1089
1090static void devm_attr_group_remove(struct device *dev, void *res)
1091{
1092 union device_attr_group_devres *devres = res;
1093 const struct attribute_group *group = devres->group;
1094
1095 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1096 sysfs_remove_group(&dev->kobj, group);
1097}
1098
1099static void devm_attr_groups_remove(struct device *dev, void *res)
1100{
1101 union device_attr_group_devres *devres = res;
1102 const struct attribute_group **groups = devres->groups;
1103
1104 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1105 sysfs_remove_groups(&dev->kobj, groups);
1106}
1107
1108/**
1109 * devm_device_add_group - given a device, create a managed attribute group
1110 * @dev: The device to create the group for
1111 * @grp: The attribute group to create
1112 *
1113 * This function creates a group for the first time. It will explicitly
1114 * warn and error if any of the attribute files being created already exist.
1115 *
1116 * Returns 0 on success or error code on failure.
1117 */
1118int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1119{
1120 union device_attr_group_devres *devres;
1121 int error;
1122
1123 devres = devres_alloc(devm_attr_group_remove,
1124 sizeof(*devres), GFP_KERNEL);
1125 if (!devres)
1126 return -ENOMEM;
1127
1128 error = sysfs_create_group(&dev->kobj, grp);
1129 if (error) {
1130 devres_free(devres);
1131 return error;
1132 }
1133
1134 devres->group = grp;
1135 devres_add(dev, devres);
1136 return 0;
1137}
1138EXPORT_SYMBOL_GPL(devm_device_add_group);
1139
1140/**
1141 * devm_device_remove_group: remove a managed group from a device
1142 * @dev: device to remove the group from
1143 * @grp: group to remove
1144 *
1145 * This function removes a group of attributes from a device. The attributes
1146 * previously have to have been created for this group, otherwise it will fail.
1147 */
1148void devm_device_remove_group(struct device *dev,
1149 const struct attribute_group *grp)
1150{
1151 WARN_ON(devres_release(dev, devm_attr_group_remove,
1152 devm_attr_group_match,
1153 /* cast away const */ (void *)grp));
1154}
1155EXPORT_SYMBOL_GPL(devm_device_remove_group);
1156
1157/**
1158 * devm_device_add_groups - create a bunch of managed attribute groups
1159 * @dev: The device to create the group for
1160 * @groups: The attribute groups to create, NULL terminated
1161 *
1162 * This function creates a bunch of managed attribute groups. If an error
1163 * occurs when creating a group, all previously created groups will be
1164 * removed, unwinding everything back to the original state when this
1165 * function was called. It will explicitly warn and error if any of the
1166 * attribute files being created already exist.
1167 *
1168 * Returns 0 on success or error code from sysfs_create_group on failure.
1169 */
1170int devm_device_add_groups(struct device *dev,
1171 const struct attribute_group **groups)
1172{
1173 union device_attr_group_devres *devres;
1174 int error;
1175
1176 devres = devres_alloc(devm_attr_groups_remove,
1177 sizeof(*devres), GFP_KERNEL);
1178 if (!devres)
1179 return -ENOMEM;
1180
1181 error = sysfs_create_groups(&dev->kobj, groups);
1182 if (error) {
1183 devres_free(devres);
1184 return error;
1185 }
1186
1187 devres->groups = groups;
1188 devres_add(dev, devres);
1189 return 0;
1190}
1191EXPORT_SYMBOL_GPL(devm_device_add_groups);
1192
1193/**
1194 * devm_device_remove_groups - remove a list of managed groups
1195 *
1196 * @dev: The device for the groups to be removed from
1197 * @groups: NULL terminated list of groups to be removed
1198 *
1199 * If groups is not NULL, remove the specified groups from the device.
1200 */
1201void devm_device_remove_groups(struct device *dev,
1202 const struct attribute_group **groups)
1203{
1204 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1205 devm_attr_group_match,
1206 /* cast away const */ (void *)groups));
1207}
1208EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001210static int device_add_attrs(struct device *dev)
1211{
1212 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001213 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001214 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001215
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001216 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001217 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001218 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001219 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001220 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001221
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001222 if (type) {
1223 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001224 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001225 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001226 }
1227
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001228 error = device_add_groups(dev, dev->groups);
1229 if (error)
1230 goto err_remove_type_groups;
1231
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001232 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001233 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001234 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001235 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001236 }
1237
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001238 return 0;
1239
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001240 err_remove_dev_groups:
1241 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001242 err_remove_type_groups:
1243 if (type)
1244 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001245 err_remove_class_groups:
1246 if (class)
1247 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001248
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001249 return error;
1250}
1251
1252static void device_remove_attrs(struct device *dev)
1253{
1254 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001255 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001256
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001257 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001258 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001259
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001260 if (type)
1261 device_remove_groups(dev, type->groups);
1262
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001263 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001264 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001265}
1266
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001267static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001268 char *buf)
1269{
1270 return print_dev_t(buf, dev->devt);
1271}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001272static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001273
Kay Sieversca22e562011-12-14 14:29:38 -08001274/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001275struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001278 * devices_kset_move_before - Move device in the devices_kset's list.
1279 * @deva: Device to move.
1280 * @devb: Device @deva should come before.
1281 */
1282static void devices_kset_move_before(struct device *deva, struct device *devb)
1283{
1284 if (!devices_kset)
1285 return;
1286 pr_debug("devices_kset: Moving %s before %s\n",
1287 dev_name(deva), dev_name(devb));
1288 spin_lock(&devices_kset->list_lock);
1289 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1290 spin_unlock(&devices_kset->list_lock);
1291}
1292
1293/**
1294 * devices_kset_move_after - Move device in the devices_kset's list.
1295 * @deva: Device to move
1296 * @devb: Device @deva should come after.
1297 */
1298static void devices_kset_move_after(struct device *deva, struct device *devb)
1299{
1300 if (!devices_kset)
1301 return;
1302 pr_debug("devices_kset: Moving %s after %s\n",
1303 dev_name(deva), dev_name(devb));
1304 spin_lock(&devices_kset->list_lock);
1305 list_move(&deva->kobj.entry, &devb->kobj.entry);
1306 spin_unlock(&devices_kset->list_lock);
1307}
1308
1309/**
1310 * devices_kset_move_last - move the device to the end of devices_kset's list.
1311 * @dev: device to move
1312 */
1313void devices_kset_move_last(struct device *dev)
1314{
1315 if (!devices_kset)
1316 return;
1317 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1318 spin_lock(&devices_kset->list_lock);
1319 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1320 spin_unlock(&devices_kset->list_lock);
1321}
1322
1323/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001324 * device_create_file - create sysfs attribute file for device.
1325 * @dev: device.
1326 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001328int device_create_file(struct device *dev,
1329 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001332
1333 if (dev) {
1334 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001335 "Attribute %s: write permission without 'store'\n",
1336 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001337 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001338 "Attribute %s: read permission without 'show'\n",
1339 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001341 }
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return error;
1344}
David Graham White86df2682013-07-21 20:41:14 -04001345EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001348 * device_remove_file - remove sysfs attribute file.
1349 * @dev: device.
1350 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001352void device_remove_file(struct device *dev,
1353 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001355 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
David Graham White86df2682013-07-21 20:41:14 -04001358EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001360/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001361 * device_remove_file_self - remove sysfs attribute file from its own method.
1362 * @dev: device.
1363 * @attr: device attribute descriptor.
1364 *
1365 * See kernfs_remove_self() for details.
1366 */
1367bool device_remove_file_self(struct device *dev,
1368 const struct device_attribute *attr)
1369{
1370 if (dev)
1371 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1372 else
1373 return false;
1374}
1375EXPORT_SYMBOL_GPL(device_remove_file_self);
1376
1377/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001378 * device_create_bin_file - create sysfs binary attribute file for device.
1379 * @dev: device.
1380 * @attr: device binary attribute descriptor.
1381 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001382int device_create_bin_file(struct device *dev,
1383 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001384{
1385 int error = -EINVAL;
1386 if (dev)
1387 error = sysfs_create_bin_file(&dev->kobj, attr);
1388 return error;
1389}
1390EXPORT_SYMBOL_GPL(device_create_bin_file);
1391
1392/**
1393 * device_remove_bin_file - remove sysfs binary attribute file
1394 * @dev: device.
1395 * @attr: device binary attribute descriptor.
1396 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001397void device_remove_bin_file(struct device *dev,
1398 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001399{
1400 if (dev)
1401 sysfs_remove_bin_file(&dev->kobj, attr);
1402}
1403EXPORT_SYMBOL_GPL(device_remove_bin_file);
1404
James Bottomley34bb61f2005-09-06 16:56:51 -07001405static void klist_children_get(struct klist_node *n)
1406{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001407 struct device_private *p = to_device_private_parent(n);
1408 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001409
1410 get_device(dev);
1411}
1412
1413static void klist_children_put(struct klist_node *n)
1414{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001415 struct device_private *p = to_device_private_parent(n);
1416 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001417
1418 put_device(dev);
1419}
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001422 * device_initialize - init device structure.
1423 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 *
Cornelia Huck57394112008-09-03 18:26:40 +02001425 * This prepares the device for use by other layers by initializing
1426 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001427 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001428 * that function, though it can also be called separately, so one
1429 * may use @dev's fields. In particular, get_device()/put_device()
1430 * may be used for reference counting of @dev after calling this
1431 * function.
1432 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001433 * All fields in @dev must be initialized by the caller to 0, except
1434 * for those explicitly set to some other value. The simplest
1435 * approach is to use kzalloc() to allocate the structure containing
1436 * @dev.
1437 *
Cornelia Huck57394112008-09-03 18:26:40 +02001438 * NOTE: Use put_device() to give up your reference instead of freeing
1439 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441void device_initialize(struct device *dev)
1442{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001443 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001444 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001446 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001447 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001448 spin_lock_init(&dev->devres_lock);
1449 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001450 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001451 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001452#ifdef CONFIG_GENERIC_MSI_IRQ
1453 INIT_LIST_HEAD(&dev->msi_list);
1454#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001455 INIT_LIST_HEAD(&dev->links.consumers);
1456 INIT_LIST_HEAD(&dev->links.suppliers);
1457 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
David Graham White86df2682013-07-21 20:41:14 -04001459EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Tejun Heod73ce002013-03-12 11:30:05 -07001461struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001462{
Kay Sievers86406242007-03-14 03:25:56 +01001463 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001464
Kay Sievers86406242007-03-14 03:25:56 +01001465 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001466 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001467 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001468
Kay Sievers86406242007-03-14 03:25:56 +01001469 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001470}
1471
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001472struct class_dir {
1473 struct kobject kobj;
1474 struct class *class;
1475};
1476
1477#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1478
1479static void class_dir_release(struct kobject *kobj)
1480{
1481 struct class_dir *dir = to_class_dir(kobj);
1482 kfree(dir);
1483}
1484
1485static const
1486struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1487{
1488 struct class_dir *dir = to_class_dir(kobj);
1489 return dir->class->ns_type;
1490}
1491
1492static struct kobj_type class_dir_ktype = {
1493 .release = class_dir_release,
1494 .sysfs_ops = &kobj_sysfs_ops,
1495 .child_ns_type = class_dir_child_ns_type
1496};
1497
1498static struct kobject *
1499class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1500{
1501 struct class_dir *dir;
1502 int retval;
1503
1504 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1505 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001506 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001507
1508 dir->class = class;
1509 kobject_init(&dir->kobj, &class_dir_ktype);
1510
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001511 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001512
1513 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1514 if (retval < 0) {
1515 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001516 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001517 }
1518 return &dir->kobj;
1519}
1520
Yijing Wange4a60d12014-11-07 12:05:49 +08001521static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001522
Kay Sieversda231fd2007-11-21 17:29:15 +01001523static struct kobject *get_device_parent(struct device *dev,
1524 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001525{
Kay Sievers86406242007-03-14 03:25:56 +01001526 if (dev->class) {
1527 struct kobject *kobj = NULL;
1528 struct kobject *parent_kobj;
1529 struct kobject *k;
1530
Randy Dunlapead454f2010-09-24 14:36:49 -07001531#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001532 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001533 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001534 if (parent && parent->class == &block_class)
1535 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001536 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001537 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001538#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001539
Kay Sievers86406242007-03-14 03:25:56 +01001540 /*
1541 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001542 * Class-devices with a non class-device as parent, live
1543 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001544 */
1545 if (parent == NULL)
1546 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001547 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001548 return &parent->kobj;
1549 else
1550 parent_kobj = &parent->kobj;
1551
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001552 mutex_lock(&gdp_mutex);
1553
Kay Sievers86406242007-03-14 03:25:56 +01001554 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001555 spin_lock(&dev->class->p->glue_dirs.list_lock);
1556 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001557 if (k->parent == parent_kobj) {
1558 kobj = kobject_get(k);
1559 break;
1560 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001561 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001562 if (kobj) {
1563 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001564 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001565 }
Kay Sievers86406242007-03-14 03:25:56 +01001566
1567 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001568 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001569 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001570 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001571 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001572 }
1573
Kay Sieversca22e562011-12-14 14:29:38 -08001574 /* subsystems can specify a default root directory for their devices */
1575 if (!parent && dev->bus && dev->bus->dev_root)
1576 return &dev->bus->dev_root->kobj;
1577
Kay Sievers86406242007-03-14 03:25:56 +01001578 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001579 return &parent->kobj;
1580 return NULL;
1581}
Kay Sieversda231fd2007-11-21 17:29:15 +01001582
Ming Leicebf8fd2016-07-10 19:27:36 +08001583static inline bool live_in_glue_dir(struct kobject *kobj,
1584 struct device *dev)
1585{
1586 if (!kobj || !dev->class ||
1587 kobj->kset != &dev->class->p->glue_dirs)
1588 return false;
1589 return true;
1590}
1591
1592static inline struct kobject *get_glue_dir(struct device *dev)
1593{
1594 return dev->kobj.parent;
1595}
1596
1597/*
1598 * make sure cleaning up dir as the last step, we need to make
1599 * sure .release handler of kobject is run with holding the
1600 * global lock
1601 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001602static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001603{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001604 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001605 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001606 return;
1607
Yijing Wange4a60d12014-11-07 12:05:49 +08001608 mutex_lock(&gdp_mutex);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001609 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001610 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001611}
Cornelia Huck63b69712008-01-21 16:09:44 +01001612
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001613static int device_add_class_symlinks(struct device *dev)
1614{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001615 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001616 int error;
1617
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001618 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001619 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001620 if (error)
1621 dev_warn(dev, "Error %d creating of_node link\n",error);
1622 /* An error here doesn't warrant bringing down the device */
1623 }
1624
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001625 if (!dev->class)
1626 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001627
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001628 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001629 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001630 "subsystem");
1631 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001632 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001633
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001634 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001635 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1636 "device");
1637 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001638 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001639 }
Kay Sievers39aba962010-09-04 22:33:14 -07001640
Randy Dunlapead454f2010-09-24 14:36:49 -07001641#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001642 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001643 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001644 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001645#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001646
1647 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001648 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001649 &dev->kobj, dev_name(dev));
1650 if (error)
1651 goto out_device;
1652
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001653 return 0;
1654
Kay Sievers39aba962010-09-04 22:33:14 -07001655out_device:
1656 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001657
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001658out_subsys:
1659 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001660out_devnode:
1661 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001662 return error;
1663}
1664
1665static void device_remove_class_symlinks(struct device *dev)
1666{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001667 if (dev_of_node(dev))
1668 sysfs_remove_link(&dev->kobj, "of_node");
1669
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001670 if (!dev->class)
1671 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001672
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001673 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001674 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001675 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001676#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001677 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001678 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001679#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001680 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001684 * dev_set_name - set a device name
1685 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001686 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001687 */
1688int dev_set_name(struct device *dev, const char *fmt, ...)
1689{
1690 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001691 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001692
1693 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001694 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001695 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001696 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001697}
1698EXPORT_SYMBOL_GPL(dev_set_name);
1699
1700/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001701 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1702 * @dev: device
1703 *
1704 * By default we select char/ for new entries. Setting class->dev_obj
1705 * to NULL prevents an entry from being created. class->dev_kobj must
1706 * be set (or cleared) before any devices are registered to the class
1707 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001708 * device_remove_sys_dev_entry() will disagree about the presence of
1709 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001710 */
1711static struct kobject *device_to_dev_kobj(struct device *dev)
1712{
1713 struct kobject *kobj;
1714
1715 if (dev->class)
1716 kobj = dev->class->dev_kobj;
1717 else
1718 kobj = sysfs_dev_char_kobj;
1719
1720 return kobj;
1721}
1722
1723static int device_create_sys_dev_entry(struct device *dev)
1724{
1725 struct kobject *kobj = device_to_dev_kobj(dev);
1726 int error = 0;
1727 char devt_str[15];
1728
1729 if (kobj) {
1730 format_dev_t(devt_str, dev->devt);
1731 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1732 }
1733
1734 return error;
1735}
1736
1737static void device_remove_sys_dev_entry(struct device *dev)
1738{
1739 struct kobject *kobj = device_to_dev_kobj(dev);
1740 char devt_str[15];
1741
1742 if (kobj) {
1743 format_dev_t(devt_str, dev->devt);
1744 sysfs_remove_link(kobj, devt_str);
1745 }
1746}
1747
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001748int device_private_init(struct device *dev)
1749{
1750 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1751 if (!dev->p)
1752 return -ENOMEM;
1753 dev->p->device = dev;
1754 klist_init(&dev->p->klist_children, klist_children_get,
1755 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001756 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001757 return 0;
1758}
1759
Dan Williamse105b8b2008-04-21 10:51:07 -07001760/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001761 * device_add - add device to device hierarchy.
1762 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001764 * This is part 2 of device_register(), though may be called
1765 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 *
Cornelia Huck57394112008-09-03 18:26:40 +02001767 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001768 * to the global and sibling lists for the device, then
1769 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001770 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001771 * Do not call this routine or device_register() more than once for
1772 * any device structure. The driver model core is not designed to work
1773 * with devices that get unregistered and then spring back to life.
1774 * (Among other things, it's very hard to guarantee that all references
1775 * to the previous incarnation of @dev have been dropped.) Allocate
1776 * and register a fresh new struct device instead.
1777 *
Cornelia Huck57394112008-09-03 18:26:40 +02001778 * NOTE: _Never_ directly free @dev after calling this function, even
1779 * if it returned an error! Always use put_device() to give up your
1780 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
1782int device_add(struct device *dev)
1783{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301784 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001785 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001786 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001787 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001788 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001791 if (!dev)
1792 goto done;
1793
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001794 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001795 error = device_private_init(dev);
1796 if (error)
1797 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001798 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001799
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001800 /*
1801 * for statically allocated devices, which should all be converted
1802 * some day, we need to initialize the name. We prevent reading back
1803 * the name, and force the use of dev_name()
1804 */
1805 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001806 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001807 dev->init_name = NULL;
1808 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001809
Kay Sieversca22e562011-12-14 14:29:38 -08001810 /* subsystems can specify simple device enumeration */
1811 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1812 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1813
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001814 if (!dev_name(dev)) {
1815 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001816 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001819 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001822 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001823 if (IS_ERR(kobj)) {
1824 error = PTR_ERR(kobj);
1825 goto parent_error;
1826 }
Kay Sieversca22e562011-12-14 14:29:38 -08001827 if (kobj)
1828 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Yinghai Lu0d358f22008-02-19 03:20:41 -08001830 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001831 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001832 set_dev_node(dev, dev_to_node(parent));
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001835 /* we require the name to be set before, and pass NULL */
1836 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001837 if (error) {
1838 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001840 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001841
Brian Walsh37022642006-08-14 22:43:19 -07001842 /* notify platform of device entry */
1843 if (platform_notify)
1844 platform_notify(dev);
1845
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001846 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001847 if (error)
1848 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001849
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001850 error = device_add_class_symlinks(dev);
1851 if (error)
1852 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001853 error = device_add_attrs(dev);
1854 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001855 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001856 error = bus_add_device(dev);
1857 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001859 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001860 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001861 goto DPMError;
1862 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001863
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001864 if (MAJOR(dev->devt)) {
1865 error = device_create_file(dev, &dev_attr_dev);
1866 if (error)
1867 goto DevAttrError;
1868
1869 error = device_create_sys_dev_entry(dev);
1870 if (error)
1871 goto SysEntryError;
1872
1873 devtmpfs_create_node(dev);
1874 }
1875
Alan Sternec0676ee2008-12-05 14:10:31 -05001876 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001877 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001878 */
1879 if (dev->bus)
1880 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1881 BUS_NOTIFY_ADD_DEVICE, dev);
1882
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001883 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001884 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001886 klist_add_tail(&dev->p->knode_parent,
1887 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001889 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001890 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001891 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001892 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001893 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001894
1895 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001896 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001897 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001898 if (class_intf->add_dev)
1899 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001900 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001901 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001902done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 put_device(dev);
1904 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001905 SysEntryError:
1906 if (MAJOR(dev->devt))
1907 device_remove_file(dev, &dev_attr_dev);
1908 DevAttrError:
1909 device_pm_remove(dev);
1910 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001911 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001912 bus_remove_device(dev);
1913 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001914 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001915 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001916 device_remove_class_symlinks(dev);
1917 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001918 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001919 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001920 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001921 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 kobject_del(&dev->kobj);
1923 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001924 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001925parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01001926 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001927name_error:
1928 kfree(dev->p);
1929 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001930 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
David Graham White86df2682013-07-21 20:41:14 -04001932EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001935 * device_register - register a device with the system.
1936 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001938 * This happens in two clean steps - initialize the device
1939 * and add it to the system. The two steps can be called
1940 * separately, but this is the easiest and most common.
1941 * I.e. you should only call the two helpers separately if
1942 * have a clearly defined need to use and refcount the device
1943 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001944 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001945 * For more information, see the kerneldoc for device_initialize()
1946 * and device_add().
1947 *
Cornelia Huck57394112008-09-03 18:26:40 +02001948 * NOTE: _Never_ directly free @dev after calling this function, even
1949 * if it returned an error! Always use put_device() to give up the
1950 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952int device_register(struct device *dev)
1953{
1954 device_initialize(dev);
1955 return device_add(dev);
1956}
David Graham White86df2682013-07-21 20:41:14 -04001957EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001960 * get_device - increment reference count for device.
1961 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001963 * This simply forwards the call to kobject_get(), though
1964 * we do take care to provide for the case that we get a NULL
1965 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001967struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001969 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
David Graham White86df2682013-07-21 20:41:14 -04001971EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001974 * put_device - decrement reference count.
1975 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001977void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001979 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (dev)
1981 kobject_put(&dev->kobj);
1982}
David Graham White86df2682013-07-21 20:41:14 -04001983EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001986 * device_del - delete device from system.
1987 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001989 * This is the first part of the device unregistration
1990 * sequence. This removes the device from the lists we control
1991 * from here, has it removed from the other driver model
1992 * subsystems it was added to in device_add(), and removes it
1993 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001995 * NOTE: this should be called manually _iff_ device_add() was
1996 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001998void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002000 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002001 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002002 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Alan Sternec0676ee2008-12-05 14:10:31 -05002004 /* Notify clients of device removal. This call must come
2005 * before dpm_sysfs_remove().
2006 */
2007 if (dev->bus)
2008 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2009 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002010
Alan Stern3b98aea2008-08-07 13:06:12 -04002011 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002013 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002014 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002015 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002016 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002017 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002018 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002019 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002020 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002021
Kay Sieversca22e562011-12-14 14:29:38 -08002022 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002023 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002024 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002025 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002026 if (class_intf->remove_dev)
2027 class_intf->remove_dev(dev, class_intf);
2028 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02002029 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002030 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002031 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002032 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002033 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002034 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002035 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002036 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02002037 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002038 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 /* Notify the platform of the removal, in case they
2041 * need to do anything...
2042 */
2043 if (platform_notify_remove)
2044 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02002045 if (dev->bus)
2046 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2047 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002048 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002049 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002051 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002052 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
David Graham White86df2682013-07-21 20:41:14 -04002054EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002057 * device_unregister - unregister device from system.
2058 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002060 * We do this in two parts, like we do device_register(). First,
2061 * we remove it from all the subsystems with device_del(), then
2062 * we decrement the reference count via put_device(). If that
2063 * is the final reference count, the device will be cleaned up
2064 * via device_release() above. Otherwise, the structure will
2065 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002067void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002069 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 device_del(dev);
2071 put_device(dev);
2072}
David Graham White86df2682013-07-21 20:41:14 -04002073EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002075static struct device *prev_device(struct klist_iter *i)
2076{
2077 struct klist_node *n = klist_prev(i);
2078 struct device *dev = NULL;
2079 struct device_private *p;
2080
2081 if (n) {
2082 p = to_device_private_parent(n);
2083 dev = p->device;
2084 }
2085 return dev;
2086}
2087
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002088static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002089{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002090 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002091 struct device *dev = NULL;
2092 struct device_private *p;
2093
2094 if (n) {
2095 p = to_device_private_parent(n);
2096 dev = p->device;
2097 }
2098 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002099}
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002102 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002103 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002104 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002105 * @uid: returned file owner
2106 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002107 * @tmp: possibly allocated string
2108 *
2109 * Return the relative path of a possible device node.
2110 * Non-default names may need to allocate a memory to compose
2111 * a name. This memory is returned in tmp and needs to be
2112 * freed by the caller.
2113 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002114const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002115 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002116 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002117{
2118 char *s;
2119
2120 *tmp = NULL;
2121
2122 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002123 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002124 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002125 if (*tmp)
2126 return *tmp;
2127
2128 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002129 if (dev->class && dev->class->devnode)
2130 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002131 if (*tmp)
2132 return *tmp;
2133
2134 /* return name without allocation, tmp == NULL */
2135 if (strchr(dev_name(dev), '!') == NULL)
2136 return dev_name(dev);
2137
2138 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002139 s = kstrdup(dev_name(dev), GFP_KERNEL);
2140 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002141 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002142 strreplace(s, '!', '/');
2143 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002144}
2145
2146/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002147 * device_for_each_child - device child iterator.
2148 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002149 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002150 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002152 * Iterate over @parent's child devices, and call @fn for each,
2153 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002155 * We check the return of @fn each time. If it returns anything
2156 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002158int device_for_each_child(struct device *parent, void *data,
2159 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002161 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002162 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 int error = 0;
2164
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002165 if (!parent->p)
2166 return 0;
2167
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002168 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002169 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002170 error = fn(child, data);
2171 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return error;
2173}
David Graham White86df2682013-07-21 20:41:14 -04002174EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Cornelia Huck5ab69982006-11-16 15:42:07 +01002176/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002177 * device_for_each_child_reverse - device child iterator in reversed order.
2178 * @parent: parent struct device.
2179 * @fn: function to be called for each device.
2180 * @data: data for the callback.
2181 *
2182 * Iterate over @parent's child devices, and call @fn for each,
2183 * passing it @data.
2184 *
2185 * We check the return of @fn each time. If it returns anything
2186 * other than 0, we break out and return that value.
2187 */
2188int device_for_each_child_reverse(struct device *parent, void *data,
2189 int (*fn)(struct device *dev, void *data))
2190{
2191 struct klist_iter i;
2192 struct device *child;
2193 int error = 0;
2194
2195 if (!parent->p)
2196 return 0;
2197
2198 klist_iter_init(&parent->p->klist_children, &i);
2199 while ((child = prev_device(&i)) && !error)
2200 error = fn(child, data);
2201 klist_iter_exit(&i);
2202 return error;
2203}
2204EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2205
2206/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002207 * device_find_child - device iterator for locating a particular device.
2208 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002209 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002210 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002211 *
2212 * This is similar to the device_for_each_child() function above, but it
2213 * returns a reference to a device that is 'found' for later use, as
2214 * determined by the @match callback.
2215 *
2216 * The callback should return 0 if the device doesn't match and non-zero
2217 * if it does. If the callback returns non-zero and a reference to the
2218 * current device can be obtained, this function will return to the caller
2219 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002220 *
2221 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002222 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002223struct device *device_find_child(struct device *parent, void *data,
2224 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002225{
2226 struct klist_iter i;
2227 struct device *child;
2228
2229 if (!parent)
2230 return NULL;
2231
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002232 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002233 while ((child = next_device(&i)))
2234 if (match(child, data) && get_device(child))
2235 break;
2236 klist_iter_exit(&i);
2237 return child;
2238}
David Graham White86df2682013-07-21 20:41:14 -04002239EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241int __init devices_init(void)
2242{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002243 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2244 if (!devices_kset)
2245 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002246 dev_kobj = kobject_create_and_add("dev", NULL);
2247 if (!dev_kobj)
2248 goto dev_kobj_err;
2249 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2250 if (!sysfs_dev_block_kobj)
2251 goto block_kobj_err;
2252 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2253 if (!sysfs_dev_char_kobj)
2254 goto char_kobj_err;
2255
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002256 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002257
2258 char_kobj_err:
2259 kobject_put(sysfs_dev_block_kobj);
2260 block_kobj_err:
2261 kobject_put(dev_kobj);
2262 dev_kobj_err:
2263 kset_unregister(devices_kset);
2264 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002267static int device_check_offline(struct device *dev, void *not_used)
2268{
2269 int ret;
2270
2271 ret = device_for_each_child(dev, NULL, device_check_offline);
2272 if (ret)
2273 return ret;
2274
2275 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2276}
2277
2278/**
2279 * device_offline - Prepare the device for hot-removal.
2280 * @dev: Device to be put offline.
2281 *
2282 * Execute the device bus type's .offline() callback, if present, to prepare
2283 * the device for a subsequent hot-removal. If that succeeds, the device must
2284 * not be used until either it is removed or its bus type's .online() callback
2285 * is executed.
2286 *
2287 * Call under device_hotplug_lock.
2288 */
2289int device_offline(struct device *dev)
2290{
2291 int ret;
2292
2293 if (dev->offline_disabled)
2294 return -EPERM;
2295
2296 ret = device_for_each_child(dev, NULL, device_check_offline);
2297 if (ret)
2298 return ret;
2299
2300 device_lock(dev);
2301 if (device_supports_offline(dev)) {
2302 if (dev->offline) {
2303 ret = 1;
2304 } else {
2305 ret = dev->bus->offline(dev);
2306 if (!ret) {
2307 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2308 dev->offline = true;
2309 }
2310 }
2311 }
2312 device_unlock(dev);
2313
2314 return ret;
2315}
2316
2317/**
2318 * device_online - Put the device back online after successful device_offline().
2319 * @dev: Device to be put back online.
2320 *
2321 * If device_offline() has been successfully executed for @dev, but the device
2322 * has not been removed subsequently, execute its bus type's .online() callback
2323 * to indicate that the device can be used again.
2324 *
2325 * Call under device_hotplug_lock.
2326 */
2327int device_online(struct device *dev)
2328{
2329 int ret = 0;
2330
2331 device_lock(dev);
2332 if (device_supports_offline(dev)) {
2333 if (dev->offline) {
2334 ret = dev->bus->online(dev);
2335 if (!ret) {
2336 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2337 dev->offline = false;
2338 }
2339 } else {
2340 ret = 1;
2341 }
2342 }
2343 device_unlock(dev);
2344
2345 return ret;
2346}
2347
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002348struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002349 struct device dev;
2350 struct module *owner;
2351};
2352
Josh Triplett93058422012-11-18 21:27:55 -08002353static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002354{
2355 return container_of(d, struct root_device, dev);
2356}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002357
2358static void root_device_release(struct device *dev)
2359{
2360 kfree(to_root_device(dev));
2361}
2362
2363/**
2364 * __root_device_register - allocate and register a root device
2365 * @name: root device name
2366 * @owner: owner module of the root device, usually THIS_MODULE
2367 *
2368 * This function allocates a root device and registers it
2369 * using device_register(). In order to free the returned
2370 * device, use root_device_unregister().
2371 *
2372 * Root devices are dummy devices which allow other devices
2373 * to be grouped under /sys/devices. Use this function to
2374 * allocate a root device and then use it as the parent of
2375 * any device which should appear under /sys/devices/{name}
2376 *
2377 * The /sys/devices/{name} directory will also contain a
2378 * 'module' symlink which points to the @owner directory
2379 * in sysfs.
2380 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002381 * Returns &struct device pointer on success, or ERR_PTR() on error.
2382 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002383 * Note: You probably want to use root_device_register().
2384 */
2385struct device *__root_device_register(const char *name, struct module *owner)
2386{
2387 struct root_device *root;
2388 int err = -ENOMEM;
2389
2390 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2391 if (!root)
2392 return ERR_PTR(err);
2393
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002394 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002395 if (err) {
2396 kfree(root);
2397 return ERR_PTR(err);
2398 }
2399
2400 root->dev.release = root_device_release;
2401
2402 err = device_register(&root->dev);
2403 if (err) {
2404 put_device(&root->dev);
2405 return ERR_PTR(err);
2406 }
2407
Christoph Egger1d9e8822010-05-17 16:57:58 +02002408#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002409 if (owner) {
2410 struct module_kobject *mk = &owner->mkobj;
2411
2412 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2413 if (err) {
2414 device_unregister(&root->dev);
2415 return ERR_PTR(err);
2416 }
2417 root->owner = owner;
2418 }
2419#endif
2420
2421 return &root->dev;
2422}
2423EXPORT_SYMBOL_GPL(__root_device_register);
2424
2425/**
2426 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002427 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002428 *
2429 * This function unregisters and cleans up a device that was created by
2430 * root_device_register().
2431 */
2432void root_device_unregister(struct device *dev)
2433{
2434 struct root_device *root = to_root_device(dev);
2435
2436 if (root->owner)
2437 sysfs_remove_link(&root->dev.kobj, "module");
2438
2439 device_unregister(dev);
2440}
2441EXPORT_SYMBOL_GPL(root_device_unregister);
2442
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002443
2444static void device_create_release(struct device *dev)
2445{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002446 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002447 kfree(dev);
2448}
2449
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002450static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002451device_create_groups_vargs(struct class *class, struct device *parent,
2452 dev_t devt, void *drvdata,
2453 const struct attribute_group **groups,
2454 const char *fmt, va_list args)
2455{
2456 struct device *dev = NULL;
2457 int retval = -ENODEV;
2458
2459 if (class == NULL || IS_ERR(class))
2460 goto error;
2461
2462 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2463 if (!dev) {
2464 retval = -ENOMEM;
2465 goto error;
2466 }
2467
David Herrmannbbc780f2013-11-21 20:15:48 +01002468 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002469 dev->devt = devt;
2470 dev->class = class;
2471 dev->parent = parent;
2472 dev->groups = groups;
2473 dev->release = device_create_release;
2474 dev_set_drvdata(dev, drvdata);
2475
2476 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2477 if (retval)
2478 goto error;
2479
David Herrmannbbc780f2013-11-21 20:15:48 +01002480 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002481 if (retval)
2482 goto error;
2483
2484 return dev;
2485
2486error:
2487 put_device(dev);
2488 return ERR_PTR(retval);
2489}
2490
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002491/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002492 * device_create_vargs - creates a device and registers it with sysfs
2493 * @class: pointer to the struct class that this device should be registered to
2494 * @parent: pointer to the parent struct device of this new device, if any
2495 * @devt: the dev_t for the char device to be added
2496 * @drvdata: the data to be added to the device for callbacks
2497 * @fmt: string for the device's name
2498 * @args: va_list for the device's name
2499 *
2500 * This function can be used by char device classes. A struct device
2501 * will be created in sysfs, registered to the specified class.
2502 *
2503 * A "dev" file will be created, showing the dev_t for the device, if
2504 * the dev_t is not 0,0.
2505 * If a pointer to a parent struct device is passed in, the newly created
2506 * struct device will be a child of that device in sysfs.
2507 * The pointer to the struct device will be returned from the call.
2508 * Any further sysfs files that might be required can be created using this
2509 * pointer.
2510 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002511 * Returns &struct device pointer on success, or ERR_PTR() on error.
2512 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002513 * Note: the struct class passed to this function must have previously
2514 * been created with a call to class_create().
2515 */
2516struct device *device_create_vargs(struct class *class, struct device *parent,
2517 dev_t devt, void *drvdata, const char *fmt,
2518 va_list args)
2519{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002520 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2521 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002522}
2523EXPORT_SYMBOL_GPL(device_create_vargs);
2524
2525/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002526 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002527 * @class: pointer to the struct class that this device should be registered to
2528 * @parent: pointer to the parent struct device of this new device, if any
2529 * @devt: the dev_t for the char device to be added
2530 * @drvdata: the data to be added to the device for callbacks
2531 * @fmt: string for the device's name
2532 *
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 *
2536 * A "dev" file will be created, showing the dev_t for the device, if
2537 * the dev_t is not 0,0.
2538 * If a pointer to a parent struct device is passed in, the newly created
2539 * struct device will be a child of that device in sysfs.
2540 * The pointer to the struct device will be returned from the call.
2541 * Any further sysfs files that might be required can be created using this
2542 * pointer.
2543 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002544 * Returns &struct device pointer on success, or ERR_PTR() on error.
2545 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002546 * Note: the struct class passed to this function must have previously
2547 * been created with a call to class_create().
2548 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002549struct device *device_create(struct class *class, struct device *parent,
2550 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002551{
2552 va_list vargs;
2553 struct device *dev;
2554
2555 va_start(vargs, fmt);
2556 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2557 va_end(vargs);
2558 return dev;
2559}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002560EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002561
Guenter Roeck39ef3112013-07-14 16:05:57 -07002562/**
2563 * device_create_with_groups - creates a device and registers it with sysfs
2564 * @class: pointer to the struct class that this device should be registered to
2565 * @parent: pointer to the parent struct device of this new device, if any
2566 * @devt: the dev_t for the char device to be added
2567 * @drvdata: the data to be added to the device for callbacks
2568 * @groups: NULL-terminated list of attribute groups to be created
2569 * @fmt: string for the device's name
2570 *
2571 * This function can be used by char device classes. A struct device
2572 * will be created in sysfs, registered to the specified class.
2573 * Additional attributes specified in the groups parameter will also
2574 * be created automatically.
2575 *
2576 * A "dev" file will be created, showing the dev_t for the device, if
2577 * the dev_t is not 0,0.
2578 * If a pointer to a parent struct device is passed in, the newly created
2579 * struct device will be a child of that device in sysfs.
2580 * The pointer to the struct device will be returned from the call.
2581 * Any further sysfs files that might be required can be created using this
2582 * pointer.
2583 *
2584 * Returns &struct device pointer on success, or ERR_PTR() on error.
2585 *
2586 * Note: the struct class passed to this function must have previously
2587 * been created with a call to class_create().
2588 */
2589struct device *device_create_with_groups(struct class *class,
2590 struct device *parent, dev_t devt,
2591 void *drvdata,
2592 const struct attribute_group **groups,
2593 const char *fmt, ...)
2594{
2595 va_list vargs;
2596 struct device *dev;
2597
2598 va_start(vargs, fmt);
2599 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2600 fmt, vargs);
2601 va_end(vargs);
2602 return dev;
2603}
2604EXPORT_SYMBOL_GPL(device_create_with_groups);
2605
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002606static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002607{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002608 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002609
Dave Youngcd354492008-01-28 16:56:11 +08002610 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002611}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002612
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002613/**
2614 * device_destroy - removes a device that was created with device_create()
2615 * @class: pointer to the struct class that this device was registered with
2616 * @devt: the dev_t of the device that was previously registered
2617 *
2618 * This call unregisters and cleans up a device that was created with a
2619 * call to device_create().
2620 */
2621void device_destroy(struct class *class, dev_t devt)
2622{
2623 struct device *dev;
2624
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002625 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002626 if (dev) {
2627 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002628 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002629 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002630}
2631EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002632
2633/**
2634 * device_rename - renames a device
2635 * @dev: the pointer to the struct device to be renamed
2636 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002637 *
2638 * It is the responsibility of the caller to provide mutual
2639 * exclusion between two different calls of device_rename
2640 * on the same device to ensure that new_name is valid and
2641 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002642 *
Timur Tabia5462512010-12-13 14:08:52 -06002643 * Note: Don't call this function. Currently, the networking layer calls this
2644 * function, but that will change. The following text from Kay Sievers offers
2645 * some insight:
2646 *
2647 * Renaming devices is racy at many levels, symlinks and other stuff are not
2648 * replaced atomically, and you get a "move" uevent, but it's not easy to
2649 * connect the event to the old and new device. Device nodes are not renamed at
2650 * all, there isn't even support for that in the kernel now.
2651 *
2652 * In the meantime, during renaming, your target name might be taken by another
2653 * driver, creating conflicts. Or the old name is taken directly after you
2654 * renamed it -- then you get events for the same DEVPATH, before you even see
2655 * the "move" event. It's just a mess, and nothing new should ever rely on
2656 * kernel device renaming. Besides that, it's not even implemented now for
2657 * other things than (driver-core wise very simple) network devices.
2658 *
2659 * We are currently about to change network renaming in udev to completely
2660 * disallow renaming of devices in the same namespace as the kernel uses,
2661 * because we can't solve the problems properly, that arise with swapping names
2662 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2663 * be allowed to some other name than eth[0-9]*, for the aforementioned
2664 * reasons.
2665 *
2666 * Make up a "real" name in the driver before you register anything, or add
2667 * some other attributes for userspace to find the device, or use udev to add
2668 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2669 * don't even want to get into that and try to implement the missing pieces in
2670 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002671 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002672int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002673{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002674 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002675 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002676 int error;
2677
2678 dev = get_device(dev);
2679 if (!dev)
2680 return -EINVAL;
2681
ethan.zhao69df7532013-10-13 22:12:35 +08002682 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002683
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002684 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002685 if (!old_device_name) {
2686 error = -ENOMEM;
2687 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002688 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002689
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002690 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002691 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2692 kobj, old_device_name,
2693 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002694 if (error)
2695 goto out;
2696 }
Kay Sievers39aba962010-09-04 22:33:14 -07002697
Tejun Heo4b30ee52013-09-11 22:29:06 -04002698 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002699 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002700 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002701
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002702out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002703 put_device(dev);
2704
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002705 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002706
2707 return error;
2708}
Johannes Berga2807db2007-02-28 12:38:31 +01002709EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002710
2711static int device_move_class_links(struct device *dev,
2712 struct device *old_parent,
2713 struct device *new_parent)
2714{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002715 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002716
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002717 if (old_parent)
2718 sysfs_remove_link(&dev->kobj, "device");
2719 if (new_parent)
2720 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2721 "device");
2722 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002723}
2724
2725/**
2726 * device_move - moves a device to a new parent
2727 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02002728 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002729 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002730 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002731int device_move(struct device *dev, struct device *new_parent,
2732 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002733{
2734 int error;
2735 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002736 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002737
2738 dev = get_device(dev);
2739 if (!dev)
2740 return -EINVAL;
2741
Cornelia Huckffa6a702009-03-04 12:44:00 +01002742 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002743 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002744 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002745 if (IS_ERR(new_parent_kobj)) {
2746 error = PTR_ERR(new_parent_kobj);
2747 put_device(new_parent);
2748 goto out;
2749 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002750
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002751 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2752 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002753 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002754 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002755 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002756 put_device(new_parent);
2757 goto out;
2758 }
2759 old_parent = dev->parent;
2760 dev->parent = new_parent;
2761 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002762 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002763 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002764 klist_add_tail(&dev->p->knode_parent,
2765 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002766 set_dev_node(dev, dev_to_node(new_parent));
2767 }
2768
Rabin Vincentbdd40342012-04-23 09:16:36 +02002769 if (dev->class) {
2770 error = device_move_class_links(dev, old_parent, new_parent);
2771 if (error) {
2772 /* We ignore errors on cleanup since we're hosed anyway... */
2773 device_move_class_links(dev, new_parent, old_parent);
2774 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2775 if (new_parent)
2776 klist_remove(&dev->p->knode_parent);
2777 dev->parent = old_parent;
2778 if (old_parent) {
2779 klist_add_tail(&dev->p->knode_parent,
2780 &old_parent->p->klist_children);
2781 set_dev_node(dev, dev_to_node(old_parent));
2782 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002783 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002784 cleanup_glue_dir(dev, new_parent_kobj);
2785 put_device(new_parent);
2786 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002787 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002788 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002789 switch (dpm_order) {
2790 case DPM_ORDER_NONE:
2791 break;
2792 case DPM_ORDER_DEV_AFTER_PARENT:
2793 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002794 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002795 break;
2796 case DPM_ORDER_PARENT_BEFORE_DEV:
2797 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002798 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002799 break;
2800 case DPM_ORDER_DEV_LAST:
2801 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002802 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002803 break;
2804 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002805
Cornelia Huck8a824722006-11-20 17:07:51 +01002806 put_device(old_parent);
2807out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002808 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002809 put_device(dev);
2810 return error;
2811}
Cornelia Huck8a824722006-11-20 17:07:51 +01002812EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002813
2814/**
2815 * device_shutdown - call ->shutdown() on each device to shutdown.
2816 */
2817void device_shutdown(void)
2818{
Benson Leungf123db82013-09-24 20:05:11 -07002819 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002820
Hugh Daschbach62458382010-03-22 10:36:37 -07002821 spin_lock(&devices_kset->list_lock);
2822 /*
2823 * Walk the devices list backward, shutting down each in turn.
2824 * Beware that device unplug events may also start pulling
2825 * devices offline, even as the system is shutting down.
2826 */
2827 while (!list_empty(&devices_kset->list)) {
2828 dev = list_entry(devices_kset->list.prev, struct device,
2829 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002830
2831 /*
2832 * hold reference count of device's parent to
2833 * prevent it from being freed because parent's
2834 * lock is to be held
2835 */
Benson Leungf123db82013-09-24 20:05:11 -07002836 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002837 get_device(dev);
2838 /*
2839 * Make sure the device is off the kset list, in the
2840 * event that dev->*->shutdown() doesn't remove it.
2841 */
2842 list_del_init(&dev->kobj.entry);
2843 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002844
Ming Leid1c6c032012-06-22 18:01:40 +08002845 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002846 if (parent)
2847 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002848 device_lock(dev);
2849
Alan Sternfe6b91f2011-12-06 23:24:52 +01002850 /* Don't allow any more runtime suspends */
2851 pm_runtime_get_noresume(dev);
2852 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002853
Michal Suchanek75216212017-08-11 15:44:43 +02002854 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07002855 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02002856 dev_info(dev, "shutdown_pre\n");
2857 dev->class->shutdown_pre(dev);
2858 }
2859 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002860 if (initcall_debug)
2861 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002862 dev->bus->shutdown(dev);
2863 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002864 if (initcall_debug)
2865 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002866 dev->driver->shutdown(dev);
2867 }
Ming Leid1c6c032012-06-22 18:01:40 +08002868
2869 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002870 if (parent)
2871 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002872
Hugh Daschbach62458382010-03-22 10:36:37 -07002873 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002874 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002875
2876 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002877 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002878 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002879}
Joe Perches99bcf212010-06-27 01:02:34 +00002880
2881/*
2882 * Device logging functions
2883 */
2884
2885#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002886static int
2887create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002888{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002889 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002890 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002891
Kay Sieversc4e00da2012-05-03 02:29:59 +02002892 if (dev->class)
2893 subsys = dev->class->name;
2894 else if (dev->bus)
2895 subsys = dev->bus->name;
2896 else
Joe Perches798efc62012-09-12 20:11:29 -07002897 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002898
Joe Perches798efc62012-09-12 20:11:29 -07002899 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002900 if (pos >= hdrlen)
2901 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002902
2903 /*
2904 * Add device identifier DEVICE=:
2905 * b12:8 block dev_t
2906 * c127:3 char dev_t
2907 * n8 netdev ifindex
2908 * +sound:card0 subsystem:devname
2909 */
2910 if (MAJOR(dev->devt)) {
2911 char c;
2912
2913 if (strcmp(subsys, "block") == 0)
2914 c = 'b';
2915 else
2916 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002917 pos++;
2918 pos += snprintf(hdr + pos, hdrlen - pos,
2919 "DEVICE=%c%u:%u",
2920 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002921 } else if (strcmp(subsys, "net") == 0) {
2922 struct net_device *net = to_net_dev(dev);
2923
Joe Perches798efc62012-09-12 20:11:29 -07002924 pos++;
2925 pos += snprintf(hdr + pos, hdrlen - pos,
2926 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002927 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002928 pos++;
2929 pos += snprintf(hdr + pos, hdrlen - pos,
2930 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002931 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002932
Ben Hutchings655e5b72014-08-26 00:34:44 -07002933 if (pos >= hdrlen)
2934 goto overflow;
2935
Joe Perches798efc62012-09-12 20:11:29 -07002936 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002937
2938overflow:
2939 dev_WARN(dev, "device/subsystem name too long");
2940 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002941}
Joe Perches798efc62012-09-12 20:11:29 -07002942
Joe Perches05e4e5b2012-09-12 20:13:37 -07002943int dev_vprintk_emit(int level, const struct device *dev,
2944 const char *fmt, va_list args)
2945{
2946 char hdr[128];
2947 size_t hdrlen;
2948
2949 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2950
2951 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2952}
2953EXPORT_SYMBOL(dev_vprintk_emit);
2954
2955int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2956{
2957 va_list args;
2958 int r;
2959
2960 va_start(args, fmt);
2961
2962 r = dev_vprintk_emit(level, dev, fmt, args);
2963
2964 va_end(args);
2965
2966 return r;
2967}
2968EXPORT_SYMBOL(dev_printk_emit);
2969
Joe Perchesd1f10522014-12-25 15:07:04 -08002970static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07002971 struct va_format *vaf)
2972{
Joe Perchesd1f10522014-12-25 15:07:04 -08002973 if (dev)
2974 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2975 dev_driver_string(dev), dev_name(dev), vaf);
2976 else
2977 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002978}
Joe Perches99bcf212010-06-27 01:02:34 +00002979
Joe Perchesd1f10522014-12-25 15:07:04 -08002980void dev_printk(const char *level, const struct device *dev,
2981 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00002982{
2983 struct va_format vaf;
2984 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00002985
2986 va_start(args, fmt);
2987
2988 vaf.fmt = fmt;
2989 vaf.va = &args;
2990
Joe Perchesd1f10522014-12-25 15:07:04 -08002991 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002992
Joe Perches99bcf212010-06-27 01:02:34 +00002993 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00002994}
2995EXPORT_SYMBOL(dev_printk);
2996
2997#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08002998void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00002999{ \
3000 struct va_format vaf; \
3001 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003002 \
3003 va_start(args, fmt); \
3004 \
3005 vaf.fmt = fmt; \
3006 vaf.va = &args; \
3007 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003008 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003009 \
Joe Perches99bcf212010-06-27 01:02:34 +00003010 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003011} \
3012EXPORT_SYMBOL(func);
3013
3014define_dev_printk_level(dev_emerg, KERN_EMERG);
3015define_dev_printk_level(dev_alert, KERN_ALERT);
3016define_dev_printk_level(dev_crit, KERN_CRIT);
3017define_dev_printk_level(dev_err, KERN_ERR);
3018define_dev_printk_level(dev_warn, KERN_WARNING);
3019define_dev_printk_level(dev_notice, KERN_NOTICE);
3020define_dev_printk_level(_dev_info, KERN_INFO);
3021
3022#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003023
3024static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3025{
3026 return fwnode && !IS_ERR(fwnode->secondary);
3027}
3028
3029/**
3030 * set_primary_fwnode - Change the primary firmware node of a given device.
3031 * @dev: Device to handle.
3032 * @fwnode: New primary firmware node of the device.
3033 *
3034 * Set the device's firmware node pointer to @fwnode, but if a secondary
3035 * firmware node of the device is present, preserve it.
3036 */
3037void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3038{
3039 if (fwnode) {
3040 struct fwnode_handle *fn = dev->fwnode;
3041
3042 if (fwnode_is_primary(fn))
3043 fn = fn->secondary;
3044
Mika Westerberg55f89a82015-11-30 17:11:39 +02003045 if (fn) {
3046 WARN_ON(fwnode->secondary);
3047 fwnode->secondary = fn;
3048 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003049 dev->fwnode = fwnode;
3050 } else {
3051 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3052 dev->fwnode->secondary : NULL;
3053 }
3054}
3055EXPORT_SYMBOL_GPL(set_primary_fwnode);
3056
3057/**
3058 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3059 * @dev: Device to handle.
3060 * @fwnode: New secondary firmware node of the device.
3061 *
3062 * If a primary firmware node of the device is present, set its secondary
3063 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3064 * @fwnode.
3065 */
3066void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3067{
3068 if (fwnode)
3069 fwnode->secondary = ERR_PTR(-ENODEV);
3070
3071 if (fwnode_is_primary(dev->fwnode))
3072 dev->fwnode->secondary = fwnode;
3073 else
3074 dev->fwnode = fwnode;
3075}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003076
3077/**
3078 * device_set_of_node_from_dev - reuse device-tree node of another device
3079 * @dev: device whose device-tree node is being set
3080 * @dev2: device whose device-tree node is being reused
3081 *
3082 * Takes another reference to the new device-tree node after first dropping
3083 * any reference held to the old node.
3084 */
3085void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3086{
3087 of_node_put(dev->of_node);
3088 dev->of_node = of_node_get(dev2->of_node);
3089 dev->of_node_reused = true;
3090}
3091EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);