blob: 36622b52e419db9573c5cfb31f03bf740324e961 [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. Wysocki21d5c572016-10-30 17:32:31 +0100239 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100240 get_device(supplier);
241 link->supplier = supplier;
242 INIT_LIST_HEAD(&link->s_node);
243 get_device(consumer);
244 link->consumer = consumer;
245 INIT_LIST_HEAD(&link->c_node);
246 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100247 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100248
Lukas Wunner64df1142016-12-04 13:10:04 +0100249 /* Determine the initial link state. */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100250 if (flags & DL_FLAG_STATELESS) {
251 link->status = DL_STATE_NONE;
252 } else {
253 switch (supplier->links.status) {
254 case DL_DEV_DRIVER_BOUND:
255 switch (consumer->links.status) {
256 case DL_DEV_PROBING:
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100257 /*
258 * Balance the decrementation of the supplier's
259 * runtime PM usage counter after consumer probe
260 * in driver_probe_device().
261 */
262 if (flags & DL_FLAG_PM_RUNTIME)
263 pm_runtime_get_sync(supplier);
264
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100265 link->status = DL_STATE_CONSUMER_PROBE;
266 break;
267 case DL_DEV_DRIVER_BOUND:
268 link->status = DL_STATE_ACTIVE;
269 break;
270 default:
271 link->status = DL_STATE_AVAILABLE;
272 break;
273 }
274 break;
275 case DL_DEV_UNBINDING:
276 link->status = DL_STATE_SUPPLIER_UNBIND;
277 break;
278 default:
279 link->status = DL_STATE_DORMANT;
280 break;
281 }
282 }
283
284 /*
285 * Move the consumer and all of the devices depending on it to the end
286 * of dpm_list and the devices_kset list.
287 *
288 * It is necessary to hold dpm_list locked throughout all that or else
289 * we may end up suspending with a wrong ordering of it.
290 */
291 device_reorder_to_tail(consumer, NULL);
292
293 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
294 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
295
296 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
297
298 out:
299 device_pm_unlock();
300 device_links_write_unlock();
301 return link;
302}
303EXPORT_SYMBOL_GPL(device_link_add);
304
305static void device_link_free(struct device_link *link)
306{
307 put_device(link->consumer);
308 put_device(link->supplier);
309 kfree(link);
310}
311
312#ifdef CONFIG_SRCU
313static void __device_link_free_srcu(struct rcu_head *rhead)
314{
315 device_link_free(container_of(rhead, struct device_link, rcu_head));
316}
317
Lukas Wunneread18c22018-02-10 19:27:12 +0100318static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100319{
Lukas Wunneread18c22018-02-10 19:27:12 +0100320 struct device_link *link = container_of(kref, struct device_link, kref);
321
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100322 dev_info(link->consumer, "Dropping the link to %s\n",
323 dev_name(link->supplier));
324
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100325 if (link->flags & DL_FLAG_PM_RUNTIME)
326 pm_runtime_drop_link(link->consumer);
327
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100328 list_del_rcu(&link->s_node);
329 list_del_rcu(&link->c_node);
330 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
331}
332#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100333static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100334{
Lukas Wunneread18c22018-02-10 19:27:12 +0100335 struct device_link *link = container_of(kref, struct device_link, kref);
336
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100337 dev_info(link->consumer, "Dropping the link to %s\n",
338 dev_name(link->supplier));
339
Lukas Wunner433986c2018-02-10 19:13:58 +0100340 if (link->flags & DL_FLAG_PM_RUNTIME)
341 pm_runtime_drop_link(link->consumer);
342
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100343 list_del(&link->s_node);
344 list_del(&link->c_node);
345 device_link_free(link);
346}
347#endif /* !CONFIG_SRCU */
348
349/**
350 * device_link_del - Delete a link between two devices.
351 * @link: Device link to delete.
352 *
353 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100354 * PM. If the link was added multiple times, it needs to be deleted as often.
355 * Care is required for hotplugged devices: Their links are purged on removal
356 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100357 */
358void device_link_del(struct device_link *link)
359{
360 device_links_write_lock();
361 device_pm_lock();
Lukas Wunneread18c22018-02-10 19:27:12 +0100362 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100363 device_pm_unlock();
364 device_links_write_unlock();
365}
366EXPORT_SYMBOL_GPL(device_link_del);
367
368static void device_links_missing_supplier(struct device *dev)
369{
370 struct device_link *link;
371
372 list_for_each_entry(link, &dev->links.suppliers, c_node)
373 if (link->status == DL_STATE_CONSUMER_PROBE)
374 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
375}
376
377/**
378 * device_links_check_suppliers - Check presence of supplier drivers.
379 * @dev: Consumer device.
380 *
381 * Check links from this device to any suppliers. Walk the list of the device's
382 * links to suppliers and see if all of them are available. If not, simply
383 * return -EPROBE_DEFER.
384 *
385 * We need to guarantee that the supplier will not go away after the check has
386 * been positive here. It only can go away in __device_release_driver() and
387 * that function checks the device's links to consumers. This means we need to
388 * mark the link as "consumer probe in progress" to make the supplier removal
389 * wait for us to complete (or bad things may happen).
390 *
391 * Links with the DL_FLAG_STATELESS flag set are ignored.
392 */
393int device_links_check_suppliers(struct device *dev)
394{
395 struct device_link *link;
396 int ret = 0;
397
398 device_links_write_lock();
399
400 list_for_each_entry(link, &dev->links.suppliers, c_node) {
401 if (link->flags & DL_FLAG_STATELESS)
402 continue;
403
404 if (link->status != DL_STATE_AVAILABLE) {
405 device_links_missing_supplier(dev);
406 ret = -EPROBE_DEFER;
407 break;
408 }
409 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
410 }
411 dev->links.status = DL_DEV_PROBING;
412
413 device_links_write_unlock();
414 return ret;
415}
416
417/**
418 * device_links_driver_bound - Update device links after probing its driver.
419 * @dev: Device to update the links for.
420 *
421 * The probe has been successful, so update links from this device to any
422 * consumers by changing their status to "available".
423 *
424 * Also change the status of @dev's links to suppliers to "active".
425 *
426 * Links with the DL_FLAG_STATELESS flag set are ignored.
427 */
428void device_links_driver_bound(struct device *dev)
429{
430 struct device_link *link;
431
432 device_links_write_lock();
433
434 list_for_each_entry(link, &dev->links.consumers, s_node) {
435 if (link->flags & DL_FLAG_STATELESS)
436 continue;
437
438 WARN_ON(link->status != DL_STATE_DORMANT);
439 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
440 }
441
442 list_for_each_entry(link, &dev->links.suppliers, c_node) {
443 if (link->flags & DL_FLAG_STATELESS)
444 continue;
445
446 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
447 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
448 }
449
450 dev->links.status = DL_DEV_DRIVER_BOUND;
451
452 device_links_write_unlock();
453}
454
455/**
456 * __device_links_no_driver - Update links of a device without a driver.
457 * @dev: Device without a drvier.
458 *
459 * Delete all non-persistent links from this device to any suppliers.
460 *
461 * Persistent links stay around, but their status is changed to "available",
462 * unless they already are in the "supplier unbind in progress" state in which
463 * case they need not be updated.
464 *
465 * Links with the DL_FLAG_STATELESS flag set are ignored.
466 */
467static void __device_links_no_driver(struct device *dev)
468{
469 struct device_link *link, *ln;
470
471 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
472 if (link->flags & DL_FLAG_STATELESS)
473 continue;
474
475 if (link->flags & DL_FLAG_AUTOREMOVE)
Lukas Wunneread18c22018-02-10 19:27:12 +0100476 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100477 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
478 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
479 }
480
481 dev->links.status = DL_DEV_NO_DRIVER;
482}
483
484void device_links_no_driver(struct device *dev)
485{
486 device_links_write_lock();
487 __device_links_no_driver(dev);
488 device_links_write_unlock();
489}
490
491/**
492 * device_links_driver_cleanup - Update links after driver removal.
493 * @dev: Device whose driver has just gone away.
494 *
495 * Update links to consumers for @dev by changing their status to "dormant" and
496 * invoke %__device_links_no_driver() to update links to suppliers for it as
497 * appropriate.
498 *
499 * Links with the DL_FLAG_STATELESS flag set are ignored.
500 */
501void device_links_driver_cleanup(struct device *dev)
502{
503 struct device_link *link;
504
505 device_links_write_lock();
506
507 list_for_each_entry(link, &dev->links.consumers, s_node) {
508 if (link->flags & DL_FLAG_STATELESS)
509 continue;
510
511 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
512 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
513 WRITE_ONCE(link->status, DL_STATE_DORMANT);
514 }
515
516 __device_links_no_driver(dev);
517
518 device_links_write_unlock();
519}
520
521/**
522 * device_links_busy - Check if there are any busy links to consumers.
523 * @dev: Device to check.
524 *
525 * Check each consumer of the device and return 'true' if its link's status
526 * is one of "consumer probe" or "active" (meaning that the given consumer is
527 * probing right now or its driver is present). Otherwise, change the link
528 * state to "supplier unbind" to prevent the consumer from being probed
529 * successfully going forward.
530 *
531 * Return 'false' if there are no probing or active consumers.
532 *
533 * Links with the DL_FLAG_STATELESS flag set are ignored.
534 */
535bool device_links_busy(struct device *dev)
536{
537 struct device_link *link;
538 bool ret = false;
539
540 device_links_write_lock();
541
542 list_for_each_entry(link, &dev->links.consumers, s_node) {
543 if (link->flags & DL_FLAG_STATELESS)
544 continue;
545
546 if (link->status == DL_STATE_CONSUMER_PROBE
547 || link->status == DL_STATE_ACTIVE) {
548 ret = true;
549 break;
550 }
551 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
552 }
553
554 dev->links.status = DL_DEV_UNBINDING;
555
556 device_links_write_unlock();
557 return ret;
558}
559
560/**
561 * device_links_unbind_consumers - Force unbind consumers of the given device.
562 * @dev: Device to unbind the consumers of.
563 *
564 * Walk the list of links to consumers for @dev and if any of them is in the
565 * "consumer probe" state, wait for all device probes in progress to complete
566 * and start over.
567 *
568 * If that's not the case, change the status of the link to "supplier unbind"
569 * and check if the link was in the "active" state. If so, force the consumer
570 * driver to unbind and start over (the consumer will not re-probe as we have
571 * changed the state of the link already).
572 *
573 * Links with the DL_FLAG_STATELESS flag set are ignored.
574 */
575void device_links_unbind_consumers(struct device *dev)
576{
577 struct device_link *link;
578
579 start:
580 device_links_write_lock();
581
582 list_for_each_entry(link, &dev->links.consumers, s_node) {
583 enum device_link_state status;
584
585 if (link->flags & DL_FLAG_STATELESS)
586 continue;
587
588 status = link->status;
589 if (status == DL_STATE_CONSUMER_PROBE) {
590 device_links_write_unlock();
591
592 wait_for_device_probe();
593 goto start;
594 }
595 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
596 if (status == DL_STATE_ACTIVE) {
597 struct device *consumer = link->consumer;
598
599 get_device(consumer);
600
601 device_links_write_unlock();
602
603 device_release_driver_internal(consumer, NULL,
604 consumer->parent);
605 put_device(consumer);
606 goto start;
607 }
608 }
609
610 device_links_write_unlock();
611}
612
613/**
614 * device_links_purge - Delete existing links to other devices.
615 * @dev: Target device.
616 */
617static void device_links_purge(struct device *dev)
618{
619 struct device_link *link, *ln;
620
621 /*
622 * Delete all of the remaining links from this device to any other
623 * devices (either consumers or suppliers).
624 */
625 device_links_write_lock();
626
627 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
628 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100629 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100630 }
631
632 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
633 WARN_ON(link->status != DL_STATE_DORMANT &&
634 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100635 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100636 }
637
638 device_links_write_unlock();
639}
640
641/* Device links support end. */
642
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800643int (*platform_notify)(struct device *dev) = NULL;
644int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700645static struct kobject *dev_kobj;
646struct kobject *sysfs_dev_char_kobj;
647struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200649static DEFINE_MUTEX(device_hotplug_lock);
650
651void lock_device_hotplug(void)
652{
653 mutex_lock(&device_hotplug_lock);
654}
655
656void unlock_device_hotplug(void)
657{
658 mutex_unlock(&device_hotplug_lock);
659}
660
661int lock_device_hotplug_sysfs(void)
662{
663 if (mutex_trylock(&device_hotplug_lock))
664 return 0;
665
666 /* Avoid busy looping (5 ms of sleep should do). */
667 msleep(5);
668 return restart_syscall();
669}
670
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800671#ifdef CONFIG_BLOCK
672static inline int device_is_not_partition(struct device *dev)
673{
674 return !(dev->type == &part_type);
675}
676#else
677static inline int device_is_not_partition(struct device *dev)
678{
679 return 1;
680}
681#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Alan Stern3e956372006-06-16 17:10:48 -0400683/**
684 * dev_driver_string - Return a device's driver name, if at all possible
685 * @dev: struct device to get the name of
686 *
687 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800688 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400689 * it is attached to. If it is not attached to a bus either, an empty
690 * string will be returned.
691 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700692const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400693{
Alan Stern35899722009-12-04 11:06:57 -0500694 struct device_driver *drv;
695
696 /* dev->driver can change to NULL underneath us because of unbinding,
697 * so be careful about accessing it. dev->bus and dev->class should
698 * never change once they are set, so they don't need special care.
699 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700700 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500701 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100702 (dev->bus ? dev->bus->name :
703 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400704}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600705EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
708
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800709static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
710 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800712 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200713 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500714 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400717 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800718 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900719 printk("dev_attr_show: %pS returned bad count\n",
720 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return ret;
723}
724
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800725static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
726 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800728 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200729 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500730 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400733 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return ret;
735}
736
Emese Revfy52cf25d2010-01-19 02:58:23 +0100737static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 .show = dev_attr_show,
739 .store = dev_attr_store,
740};
741
Kay Sieversca22e562011-12-14 14:29:38 -0800742#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
743
744ssize_t device_store_ulong(struct device *dev,
745 struct device_attribute *attr,
746 const char *buf, size_t size)
747{
748 struct dev_ext_attribute *ea = to_ext_attr(attr);
749 char *end;
750 unsigned long new = simple_strtoul(buf, &end, 0);
751 if (end == buf)
752 return -EINVAL;
753 *(unsigned long *)(ea->var) = new;
754 /* Always return full write size even if we didn't consume all */
755 return size;
756}
757EXPORT_SYMBOL_GPL(device_store_ulong);
758
759ssize_t device_show_ulong(struct device *dev,
760 struct device_attribute *attr,
761 char *buf)
762{
763 struct dev_ext_attribute *ea = to_ext_attr(attr);
764 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
765}
766EXPORT_SYMBOL_GPL(device_show_ulong);
767
768ssize_t device_store_int(struct device *dev,
769 struct device_attribute *attr,
770 const char *buf, size_t size)
771{
772 struct dev_ext_attribute *ea = to_ext_attr(attr);
773 char *end;
774 long new = simple_strtol(buf, &end, 0);
775 if (end == buf || new > INT_MAX || new < INT_MIN)
776 return -EINVAL;
777 *(int *)(ea->var) = new;
778 /* Always return full write size even if we didn't consume all */
779 return size;
780}
781EXPORT_SYMBOL_GPL(device_store_int);
782
783ssize_t device_show_int(struct device *dev,
784 struct device_attribute *attr,
785 char *buf)
786{
787 struct dev_ext_attribute *ea = to_ext_attr(attr);
788
789 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
790}
791EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Borislav Petkov91872392012-10-09 19:52:05 +0200793ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t size)
795{
796 struct dev_ext_attribute *ea = to_ext_attr(attr);
797
798 if (strtobool(buf, ea->var) < 0)
799 return -EINVAL;
800
801 return size;
802}
803EXPORT_SYMBOL_GPL(device_store_bool);
804
805ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
806 char *buf)
807{
808 struct dev_ext_attribute *ea = to_ext_attr(attr);
809
810 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
811}
812EXPORT_SYMBOL_GPL(device_show_bool);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400815 * device_release - free device structure.
816 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400818 * This is called once the reference count for the object
819 * reaches 0. We forward the call to the device's release
820 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800822static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200824 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800825 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Ming Leia525a3d2012-07-25 01:42:29 +0800827 /*
828 * Some platform devices are driven without driver attached
829 * and managed resources may have been acquired. Make sure
830 * all resources are released.
831 *
832 * Drivers still can add resources into device after device
833 * is deleted but alive, so release devres here to avoid
834 * possible memory leak.
835 */
836 devres_release_all(dev);
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (dev->release)
839 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200840 else if (dev->type && dev->type->release)
841 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700842 else if (dev->class && dev->class->dev_release)
843 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700844 else
845 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800846 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100847 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800848 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700851static const void *device_namespace(struct kobject *kobj)
852{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200853 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700854 const void *ns = NULL;
855
856 if (dev->class && dev->class->ns_type)
857 ns = dev->class->namespace(dev);
858
859 return ns;
860}
861
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600862static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 .release = device_release,
864 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700865 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866};
867
868
Kay Sievers312c0042005-11-16 09:00:00 +0100869static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 struct kobj_type *ktype = get_ktype(kobj);
872
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600873 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200874 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (dev->bus)
876 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700877 if (dev->class)
878 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880 return 0;
881}
882
Kay Sievers312c0042005-11-16 09:00:00 +0100883static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200885 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700887 if (dev->bus)
888 return dev->bus->name;
889 if (dev->class)
890 return dev->class->name;
891 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Kay Sievers7eff2e72007-08-14 15:15:12 +0200894static int dev_uevent(struct kset *kset, struct kobject *kobj,
895 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200897 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int retval = 0;
899
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200900 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700901 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200902 const char *tmp;
903 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400904 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700905 kuid_t uid = GLOBAL_ROOT_UID;
906 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200907
Kay Sievers7eff2e72007-08-14 15:15:12 +0200908 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
909 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700910 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200911 if (name) {
912 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200913 if (mode)
914 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700915 if (!uid_eq(uid, GLOBAL_ROOT_UID))
916 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
917 if (!gid_eq(gid, GLOBAL_ROOT_GID))
918 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700919 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200920 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700921 }
922
Kay Sievers414264f2007-03-12 21:08:57 +0100923 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200924 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100925
Kay Sievers239378f2006-10-07 21:54:55 +0200926 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200927 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200928
Grant Likely07d57a32012-02-01 11:22:22 -0700929 /* Add common DT information about the device */
930 of_device_uevent(dev, env);
931
Kay Sievers7eff2e72007-08-14 15:15:12 +0200932 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100933 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200934 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200935 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800936 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100937 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
Kay Sievers7eff2e72007-08-14 15:15:12 +0200940 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700941 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200942 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200943 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800944 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100945 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800946 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200947 }
948
Stefan Weileef35c22010-08-06 21:11:15 +0200949 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200950 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200951 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200952 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800953 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100954 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800955 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700956 }
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return retval;
959}
960
Emese Revfy9cd43612009-12-31 14:52:51 +0100961static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100962 .filter = dev_uevent_filter,
963 .name = dev_uevent_name,
964 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965};
966
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700967static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +0200968 char *buf)
969{
970 struct kobject *top_kobj;
971 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200972 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200973 int i;
974 size_t count = 0;
975 int retval;
976
977 /* search the kset, the device belongs to */
978 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200979 while (!top_kobj->kset && top_kobj->parent)
980 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200981 if (!top_kobj->kset)
982 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200983
Kay Sievers16574dc2007-04-06 01:40:38 +0200984 kset = top_kobj->kset;
985 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
986 goto out;
987
988 /* respect filter */
989 if (kset->uevent_ops && kset->uevent_ops->filter)
990 if (!kset->uevent_ops->filter(kset, &dev->kobj))
991 goto out;
992
Kay Sievers7eff2e72007-08-14 15:15:12 +0200993 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
994 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200995 return -ENOMEM;
996
Kay Sievers16574dc2007-04-06 01:40:38 +0200997 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200998 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200999 if (retval)
1000 goto out;
1001
1002 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001003 for (i = 0; i < env->envp_idx; i++)
1004 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001005out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001006 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001007 return count;
1008}
1009
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001010static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001011 const char *buf, size_t count)
1012{
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001013 if (kobject_synth_uevent(&dev->kobj, buf, count))
1014 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Kay Sievers60a96a52007-07-08 22:29:26 +02001015
Kay Sieversa7fd6702005-10-01 14:49:43 +02001016 return count;
1017}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001018static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001019
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001020static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001021 char *buf)
1022{
1023 bool val;
1024
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001025 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001026 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001027 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001028 return sprintf(buf, "%u\n", val);
1029}
1030
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001031static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001032 const char *buf, size_t count)
1033{
1034 bool val;
1035 int ret;
1036
1037 ret = strtobool(buf, &val);
1038 if (ret < 0)
1039 return ret;
1040
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001041 ret = lock_device_hotplug_sysfs();
1042 if (ret)
1043 return ret;
1044
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001045 ret = val ? device_online(dev) : device_offline(dev);
1046 unlock_device_hotplug();
1047 return ret < 0 ? ret : count;
1048}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001049static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001050
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001051int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001052{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001053 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001054}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001055EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001056
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001057void device_remove_groups(struct device *dev,
1058 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001059{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001060 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001061}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001062EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001063
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001064union device_attr_group_devres {
1065 const struct attribute_group *group;
1066 const struct attribute_group **groups;
1067};
1068
1069static int devm_attr_group_match(struct device *dev, void *res, void *data)
1070{
1071 return ((union device_attr_group_devres *)res)->group == data;
1072}
1073
1074static void devm_attr_group_remove(struct device *dev, void *res)
1075{
1076 union device_attr_group_devres *devres = res;
1077 const struct attribute_group *group = devres->group;
1078
1079 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1080 sysfs_remove_group(&dev->kobj, group);
1081}
1082
1083static void devm_attr_groups_remove(struct device *dev, void *res)
1084{
1085 union device_attr_group_devres *devres = res;
1086 const struct attribute_group **groups = devres->groups;
1087
1088 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1089 sysfs_remove_groups(&dev->kobj, groups);
1090}
1091
1092/**
1093 * devm_device_add_group - given a device, create a managed attribute group
1094 * @dev: The device to create the group for
1095 * @grp: The attribute group to create
1096 *
1097 * This function creates a group for the first time. It will explicitly
1098 * warn and error if any of the attribute files being created already exist.
1099 *
1100 * Returns 0 on success or error code on failure.
1101 */
1102int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1103{
1104 union device_attr_group_devres *devres;
1105 int error;
1106
1107 devres = devres_alloc(devm_attr_group_remove,
1108 sizeof(*devres), GFP_KERNEL);
1109 if (!devres)
1110 return -ENOMEM;
1111
1112 error = sysfs_create_group(&dev->kobj, grp);
1113 if (error) {
1114 devres_free(devres);
1115 return error;
1116 }
1117
1118 devres->group = grp;
1119 devres_add(dev, devres);
1120 return 0;
1121}
1122EXPORT_SYMBOL_GPL(devm_device_add_group);
1123
1124/**
1125 * devm_device_remove_group: remove a managed group from a device
1126 * @dev: device to remove the group from
1127 * @grp: group to remove
1128 *
1129 * This function removes a group of attributes from a device. The attributes
1130 * previously have to have been created for this group, otherwise it will fail.
1131 */
1132void devm_device_remove_group(struct device *dev,
1133 const struct attribute_group *grp)
1134{
1135 WARN_ON(devres_release(dev, devm_attr_group_remove,
1136 devm_attr_group_match,
1137 /* cast away const */ (void *)grp));
1138}
1139EXPORT_SYMBOL_GPL(devm_device_remove_group);
1140
1141/**
1142 * devm_device_add_groups - create a bunch of managed attribute groups
1143 * @dev: The device to create the group for
1144 * @groups: The attribute groups to create, NULL terminated
1145 *
1146 * This function creates a bunch of managed attribute groups. If an error
1147 * occurs when creating a group, all previously created groups will be
1148 * removed, unwinding everything back to the original state when this
1149 * function was called. It will explicitly warn and error if any of the
1150 * attribute files being created already exist.
1151 *
1152 * Returns 0 on success or error code from sysfs_create_group on failure.
1153 */
1154int devm_device_add_groups(struct device *dev,
1155 const struct attribute_group **groups)
1156{
1157 union device_attr_group_devres *devres;
1158 int error;
1159
1160 devres = devres_alloc(devm_attr_groups_remove,
1161 sizeof(*devres), GFP_KERNEL);
1162 if (!devres)
1163 return -ENOMEM;
1164
1165 error = sysfs_create_groups(&dev->kobj, groups);
1166 if (error) {
1167 devres_free(devres);
1168 return error;
1169 }
1170
1171 devres->groups = groups;
1172 devres_add(dev, devres);
1173 return 0;
1174}
1175EXPORT_SYMBOL_GPL(devm_device_add_groups);
1176
1177/**
1178 * devm_device_remove_groups - remove a list of managed groups
1179 *
1180 * @dev: The device for the groups to be removed from
1181 * @groups: NULL terminated list of groups to be removed
1182 *
1183 * If groups is not NULL, remove the specified groups from the device.
1184 */
1185void devm_device_remove_groups(struct device *dev,
1186 const struct attribute_group **groups)
1187{
1188 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1189 devm_attr_group_match,
1190 /* cast away const */ (void *)groups));
1191}
1192EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001194static int device_add_attrs(struct device *dev)
1195{
1196 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001197 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001198 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001199
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001200 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001201 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001202 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001203 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001204 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001205
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001206 if (type) {
1207 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001208 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001209 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001210 }
1211
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001212 error = device_add_groups(dev, dev->groups);
1213 if (error)
1214 goto err_remove_type_groups;
1215
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001216 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001217 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001218 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001219 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001220 }
1221
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001222 return 0;
1223
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001224 err_remove_dev_groups:
1225 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001226 err_remove_type_groups:
1227 if (type)
1228 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001229 err_remove_class_groups:
1230 if (class)
1231 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001232
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001233 return error;
1234}
1235
1236static void device_remove_attrs(struct device *dev)
1237{
1238 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001239 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001240
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001241 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001242 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001243
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001244 if (type)
1245 device_remove_groups(dev, type->groups);
1246
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001247 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001248 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001249}
1250
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001251static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001252 char *buf)
1253{
1254 return print_dev_t(buf, dev->devt);
1255}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001256static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001257
Kay Sieversca22e562011-12-14 14:29:38 -08001258/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001259struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001262 * devices_kset_move_before - Move device in the devices_kset's list.
1263 * @deva: Device to move.
1264 * @devb: Device @deva should come before.
1265 */
1266static void devices_kset_move_before(struct device *deva, struct device *devb)
1267{
1268 if (!devices_kset)
1269 return;
1270 pr_debug("devices_kset: Moving %s before %s\n",
1271 dev_name(deva), dev_name(devb));
1272 spin_lock(&devices_kset->list_lock);
1273 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1274 spin_unlock(&devices_kset->list_lock);
1275}
1276
1277/**
1278 * devices_kset_move_after - Move device in the devices_kset's list.
1279 * @deva: Device to move
1280 * @devb: Device @deva should come after.
1281 */
1282static void devices_kset_move_after(struct device *deva, struct device *devb)
1283{
1284 if (!devices_kset)
1285 return;
1286 pr_debug("devices_kset: Moving %s after %s\n",
1287 dev_name(deva), dev_name(devb));
1288 spin_lock(&devices_kset->list_lock);
1289 list_move(&deva->kobj.entry, &devb->kobj.entry);
1290 spin_unlock(&devices_kset->list_lock);
1291}
1292
1293/**
1294 * devices_kset_move_last - move the device to the end of devices_kset's list.
1295 * @dev: device to move
1296 */
1297void devices_kset_move_last(struct device *dev)
1298{
1299 if (!devices_kset)
1300 return;
1301 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1302 spin_lock(&devices_kset->list_lock);
1303 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1304 spin_unlock(&devices_kset->list_lock);
1305}
1306
1307/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001308 * device_create_file - create sysfs attribute file for device.
1309 * @dev: device.
1310 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001312int device_create_file(struct device *dev,
1313 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001316
1317 if (dev) {
1318 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001319 "Attribute %s: write permission without 'store'\n",
1320 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001321 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001322 "Attribute %s: read permission without 'show'\n",
1323 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001325 }
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return error;
1328}
David Graham White86df2682013-07-21 20:41:14 -04001329EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001332 * device_remove_file - remove sysfs attribute file.
1333 * @dev: device.
1334 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001336void device_remove_file(struct device *dev,
1337 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001339 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
David Graham White86df2682013-07-21 20:41:14 -04001342EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001344/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001345 * device_remove_file_self - remove sysfs attribute file from its own method.
1346 * @dev: device.
1347 * @attr: device attribute descriptor.
1348 *
1349 * See kernfs_remove_self() for details.
1350 */
1351bool device_remove_file_self(struct device *dev,
1352 const struct device_attribute *attr)
1353{
1354 if (dev)
1355 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1356 else
1357 return false;
1358}
1359EXPORT_SYMBOL_GPL(device_remove_file_self);
1360
1361/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001362 * device_create_bin_file - create sysfs binary attribute file for device.
1363 * @dev: device.
1364 * @attr: device binary attribute descriptor.
1365 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001366int device_create_bin_file(struct device *dev,
1367 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001368{
1369 int error = -EINVAL;
1370 if (dev)
1371 error = sysfs_create_bin_file(&dev->kobj, attr);
1372 return error;
1373}
1374EXPORT_SYMBOL_GPL(device_create_bin_file);
1375
1376/**
1377 * device_remove_bin_file - remove sysfs binary attribute file
1378 * @dev: device.
1379 * @attr: device binary attribute descriptor.
1380 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001381void device_remove_bin_file(struct device *dev,
1382 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001383{
1384 if (dev)
1385 sysfs_remove_bin_file(&dev->kobj, attr);
1386}
1387EXPORT_SYMBOL_GPL(device_remove_bin_file);
1388
James Bottomley34bb61f2005-09-06 16:56:51 -07001389static void klist_children_get(struct klist_node *n)
1390{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001391 struct device_private *p = to_device_private_parent(n);
1392 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001393
1394 get_device(dev);
1395}
1396
1397static void klist_children_put(struct klist_node *n)
1398{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001399 struct device_private *p = to_device_private_parent(n);
1400 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001401
1402 put_device(dev);
1403}
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001406 * device_initialize - init device structure.
1407 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 *
Cornelia Huck57394112008-09-03 18:26:40 +02001409 * This prepares the device for use by other layers by initializing
1410 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001411 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001412 * that function, though it can also be called separately, so one
1413 * may use @dev's fields. In particular, get_device()/put_device()
1414 * may be used for reference counting of @dev after calling this
1415 * function.
1416 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001417 * All fields in @dev must be initialized by the caller to 0, except
1418 * for those explicitly set to some other value. The simplest
1419 * approach is to use kzalloc() to allocate the structure containing
1420 * @dev.
1421 *
Cornelia Huck57394112008-09-03 18:26:40 +02001422 * NOTE: Use put_device() to give up your reference instead of freeing
1423 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425void device_initialize(struct device *dev)
1426{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001427 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001428 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001430 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001431 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001432 spin_lock_init(&dev->devres_lock);
1433 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001434 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001435 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001436#ifdef CONFIG_GENERIC_MSI_IRQ
1437 INIT_LIST_HEAD(&dev->msi_list);
1438#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001439 INIT_LIST_HEAD(&dev->links.consumers);
1440 INIT_LIST_HEAD(&dev->links.suppliers);
1441 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
David Graham White86df2682013-07-21 20:41:14 -04001443EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Tejun Heod73ce002013-03-12 11:30:05 -07001445struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001446{
Kay Sievers86406242007-03-14 03:25:56 +01001447 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001448
Kay Sievers86406242007-03-14 03:25:56 +01001449 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001450 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001451 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001452
Kay Sievers86406242007-03-14 03:25:56 +01001453 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001454}
1455
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001456struct class_dir {
1457 struct kobject kobj;
1458 struct class *class;
1459};
1460
1461#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1462
1463static void class_dir_release(struct kobject *kobj)
1464{
1465 struct class_dir *dir = to_class_dir(kobj);
1466 kfree(dir);
1467}
1468
1469static const
1470struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1471{
1472 struct class_dir *dir = to_class_dir(kobj);
1473 return dir->class->ns_type;
1474}
1475
1476static struct kobj_type class_dir_ktype = {
1477 .release = class_dir_release,
1478 .sysfs_ops = &kobj_sysfs_ops,
1479 .child_ns_type = class_dir_child_ns_type
1480};
1481
1482static struct kobject *
1483class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1484{
1485 struct class_dir *dir;
1486 int retval;
1487
1488 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1489 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001490 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001491
1492 dir->class = class;
1493 kobject_init(&dir->kobj, &class_dir_ktype);
1494
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001495 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001496
1497 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1498 if (retval < 0) {
1499 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001500 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001501 }
1502 return &dir->kobj;
1503}
1504
Yijing Wange4a60d12014-11-07 12:05:49 +08001505static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001506
Kay Sieversda231fd2007-11-21 17:29:15 +01001507static struct kobject *get_device_parent(struct device *dev,
1508 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001509{
Kay Sievers86406242007-03-14 03:25:56 +01001510 if (dev->class) {
1511 struct kobject *kobj = NULL;
1512 struct kobject *parent_kobj;
1513 struct kobject *k;
1514
Randy Dunlapead454f2010-09-24 14:36:49 -07001515#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001516 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001517 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001518 if (parent && parent->class == &block_class)
1519 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001520 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001521 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001522#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001523
Kay Sievers86406242007-03-14 03:25:56 +01001524 /*
1525 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001526 * Class-devices with a non class-device as parent, live
1527 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001528 */
1529 if (parent == NULL)
1530 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001531 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001532 return &parent->kobj;
1533 else
1534 parent_kobj = &parent->kobj;
1535
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001536 mutex_lock(&gdp_mutex);
1537
Kay Sievers86406242007-03-14 03:25:56 +01001538 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001539 spin_lock(&dev->class->p->glue_dirs.list_lock);
1540 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001541 if (k->parent == parent_kobj) {
1542 kobj = kobject_get(k);
1543 break;
1544 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001545 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001546 if (kobj) {
1547 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001548 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001549 }
Kay Sievers86406242007-03-14 03:25:56 +01001550
1551 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001552 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001553 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001554 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001555 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001556 }
1557
Kay Sieversca22e562011-12-14 14:29:38 -08001558 /* subsystems can specify a default root directory for their devices */
1559 if (!parent && dev->bus && dev->bus->dev_root)
1560 return &dev->bus->dev_root->kobj;
1561
Kay Sievers86406242007-03-14 03:25:56 +01001562 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001563 return &parent->kobj;
1564 return NULL;
1565}
Kay Sieversda231fd2007-11-21 17:29:15 +01001566
Ming Leicebf8fd2016-07-10 19:27:36 +08001567static inline bool live_in_glue_dir(struct kobject *kobj,
1568 struct device *dev)
1569{
1570 if (!kobj || !dev->class ||
1571 kobj->kset != &dev->class->p->glue_dirs)
1572 return false;
1573 return true;
1574}
1575
1576static inline struct kobject *get_glue_dir(struct device *dev)
1577{
1578 return dev->kobj.parent;
1579}
1580
1581/*
1582 * make sure cleaning up dir as the last step, we need to make
1583 * sure .release handler of kobject is run with holding the
1584 * global lock
1585 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001586static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001587{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001588 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001589 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001590 return;
1591
Yijing Wange4a60d12014-11-07 12:05:49 +08001592 mutex_lock(&gdp_mutex);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001593 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001594 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001595}
Cornelia Huck63b69712008-01-21 16:09:44 +01001596
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001597static int device_add_class_symlinks(struct device *dev)
1598{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001599 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001600 int error;
1601
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001602 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001603 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001604 if (error)
1605 dev_warn(dev, "Error %d creating of_node link\n",error);
1606 /* An error here doesn't warrant bringing down the device */
1607 }
1608
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001609 if (!dev->class)
1610 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001611
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001612 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001613 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001614 "subsystem");
1615 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001616 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001617
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001618 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001619 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1620 "device");
1621 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001622 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001623 }
Kay Sievers39aba962010-09-04 22:33:14 -07001624
Randy Dunlapead454f2010-09-24 14:36:49 -07001625#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001626 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001627 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001628 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001629#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001630
1631 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001632 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001633 &dev->kobj, dev_name(dev));
1634 if (error)
1635 goto out_device;
1636
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001637 return 0;
1638
Kay Sievers39aba962010-09-04 22:33:14 -07001639out_device:
1640 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001641
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001642out_subsys:
1643 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001644out_devnode:
1645 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001646 return error;
1647}
1648
1649static void device_remove_class_symlinks(struct device *dev)
1650{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001651 if (dev_of_node(dev))
1652 sysfs_remove_link(&dev->kobj, "of_node");
1653
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001654 if (!dev->class)
1655 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001656
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001657 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001658 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001659 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001660#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001661 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001662 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001663#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001664 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001668 * dev_set_name - set a device name
1669 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001670 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001671 */
1672int dev_set_name(struct device *dev, const char *fmt, ...)
1673{
1674 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001675 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001676
1677 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001678 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001679 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001680 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001681}
1682EXPORT_SYMBOL_GPL(dev_set_name);
1683
1684/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001685 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1686 * @dev: device
1687 *
1688 * By default we select char/ for new entries. Setting class->dev_obj
1689 * to NULL prevents an entry from being created. class->dev_kobj must
1690 * be set (or cleared) before any devices are registered to the class
1691 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001692 * device_remove_sys_dev_entry() will disagree about the presence of
1693 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001694 */
1695static struct kobject *device_to_dev_kobj(struct device *dev)
1696{
1697 struct kobject *kobj;
1698
1699 if (dev->class)
1700 kobj = dev->class->dev_kobj;
1701 else
1702 kobj = sysfs_dev_char_kobj;
1703
1704 return kobj;
1705}
1706
1707static int device_create_sys_dev_entry(struct device *dev)
1708{
1709 struct kobject *kobj = device_to_dev_kobj(dev);
1710 int error = 0;
1711 char devt_str[15];
1712
1713 if (kobj) {
1714 format_dev_t(devt_str, dev->devt);
1715 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1716 }
1717
1718 return error;
1719}
1720
1721static void device_remove_sys_dev_entry(struct device *dev)
1722{
1723 struct kobject *kobj = device_to_dev_kobj(dev);
1724 char devt_str[15];
1725
1726 if (kobj) {
1727 format_dev_t(devt_str, dev->devt);
1728 sysfs_remove_link(kobj, devt_str);
1729 }
1730}
1731
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001732int device_private_init(struct device *dev)
1733{
1734 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1735 if (!dev->p)
1736 return -ENOMEM;
1737 dev->p->device = dev;
1738 klist_init(&dev->p->klist_children, klist_children_get,
1739 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001740 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001741 return 0;
1742}
1743
Dan Williamse105b8b2008-04-21 10:51:07 -07001744/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001745 * device_add - add device to device hierarchy.
1746 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001748 * This is part 2 of device_register(), though may be called
1749 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 *
Cornelia Huck57394112008-09-03 18:26:40 +02001751 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001752 * to the global and sibling lists for the device, then
1753 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001754 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001755 * Do not call this routine or device_register() more than once for
1756 * any device structure. The driver model core is not designed to work
1757 * with devices that get unregistered and then spring back to life.
1758 * (Among other things, it's very hard to guarantee that all references
1759 * to the previous incarnation of @dev have been dropped.) Allocate
1760 * and register a fresh new struct device instead.
1761 *
Cornelia Huck57394112008-09-03 18:26:40 +02001762 * NOTE: _Never_ directly free @dev after calling this function, even
1763 * if it returned an error! Always use put_device() to give up your
1764 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 */
1766int device_add(struct device *dev)
1767{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301768 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001769 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001770 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001771 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001772 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001775 if (!dev)
1776 goto done;
1777
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001778 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001779 error = device_private_init(dev);
1780 if (error)
1781 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001782 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001783
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001784 /*
1785 * for statically allocated devices, which should all be converted
1786 * some day, we need to initialize the name. We prevent reading back
1787 * the name, and force the use of dev_name()
1788 */
1789 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001790 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001791 dev->init_name = NULL;
1792 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001793
Kay Sieversca22e562011-12-14 14:29:38 -08001794 /* subsystems can specify simple device enumeration */
1795 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1796 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1797
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001798 if (!dev_name(dev)) {
1799 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001800 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001803 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001806 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001807 if (IS_ERR(kobj)) {
1808 error = PTR_ERR(kobj);
1809 goto parent_error;
1810 }
Kay Sieversca22e562011-12-14 14:29:38 -08001811 if (kobj)
1812 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Yinghai Lu0d358f22008-02-19 03:20:41 -08001814 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001815 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001816 set_dev_node(dev, dev_to_node(parent));
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001819 /* we require the name to be set before, and pass NULL */
1820 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001821 if (error) {
1822 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001824 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001825
Brian Walsh37022642006-08-14 22:43:19 -07001826 /* notify platform of device entry */
1827 if (platform_notify)
1828 platform_notify(dev);
1829
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001830 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001831 if (error)
1832 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001833
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001834 error = device_add_class_symlinks(dev);
1835 if (error)
1836 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001837 error = device_add_attrs(dev);
1838 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001839 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001840 error = bus_add_device(dev);
1841 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001843 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001844 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001845 goto DPMError;
1846 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001847
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001848 if (MAJOR(dev->devt)) {
1849 error = device_create_file(dev, &dev_attr_dev);
1850 if (error)
1851 goto DevAttrError;
1852
1853 error = device_create_sys_dev_entry(dev);
1854 if (error)
1855 goto SysEntryError;
1856
1857 devtmpfs_create_node(dev);
1858 }
1859
Alan Sternec0676ee2008-12-05 14:10:31 -05001860 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001861 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001862 */
1863 if (dev->bus)
1864 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1865 BUS_NOTIFY_ADD_DEVICE, dev);
1866
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001867 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001868 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001870 klist_add_tail(&dev->p->knode_parent,
1871 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001873 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001874 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001875 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001876 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001877 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001878
1879 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001880 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001881 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001882 if (class_intf->add_dev)
1883 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001884 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001885 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001886done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 put_device(dev);
1888 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001889 SysEntryError:
1890 if (MAJOR(dev->devt))
1891 device_remove_file(dev, &dev_attr_dev);
1892 DevAttrError:
1893 device_pm_remove(dev);
1894 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001895 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001896 bus_remove_device(dev);
1897 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001898 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001899 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001900 device_remove_class_symlinks(dev);
1901 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001902 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001903 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001904 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001905 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 kobject_del(&dev->kobj);
1907 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001908 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001909parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01001910 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001911name_error:
1912 kfree(dev->p);
1913 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001914 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
David Graham White86df2682013-07-21 20:41:14 -04001916EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001919 * device_register - register a device with the system.
1920 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001922 * This happens in two clean steps - initialize the device
1923 * and add it to the system. The two steps can be called
1924 * separately, but this is the easiest and most common.
1925 * I.e. you should only call the two helpers separately if
1926 * have a clearly defined need to use and refcount the device
1927 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001928 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001929 * For more information, see the kerneldoc for device_initialize()
1930 * and device_add().
1931 *
Cornelia Huck57394112008-09-03 18:26:40 +02001932 * NOTE: _Never_ directly free @dev after calling this function, even
1933 * if it returned an error! Always use put_device() to give up the
1934 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936int device_register(struct device *dev)
1937{
1938 device_initialize(dev);
1939 return device_add(dev);
1940}
David Graham White86df2682013-07-21 20:41:14 -04001941EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001944 * get_device - increment reference count for device.
1945 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001947 * This simply forwards the call to kobject_get(), though
1948 * we do take care to provide for the case that we get a NULL
1949 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001951struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001953 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
David Graham White86df2682013-07-21 20:41:14 -04001955EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001958 * put_device - decrement reference count.
1959 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001961void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001963 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (dev)
1965 kobject_put(&dev->kobj);
1966}
David Graham White86df2682013-07-21 20:41:14 -04001967EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001970 * device_del - delete device from system.
1971 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001973 * This is the first part of the device unregistration
1974 * sequence. This removes the device from the lists we control
1975 * from here, has it removed from the other driver model
1976 * subsystems it was added to in device_add(), and removes it
1977 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001979 * NOTE: this should be called manually _iff_ device_add() was
1980 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001982void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001984 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08001985 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001986 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Alan Sternec0676ee2008-12-05 14:10:31 -05001988 /* Notify clients of device removal. This call must come
1989 * before dpm_sysfs_remove().
1990 */
1991 if (dev->bus)
1992 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1993 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001994
Alan Stern3b98aea2008-08-07 13:06:12 -04001995 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001997 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001998 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001999 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002000 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002001 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002002 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002003 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002004 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002005
Kay Sieversca22e562011-12-14 14:29:38 -08002006 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002007 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002008 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002009 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002010 if (class_intf->remove_dev)
2011 class_intf->remove_dev(dev, class_intf);
2012 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02002013 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002014 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002015 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002016 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002017 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002018 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002019 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002020 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02002021 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002022 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 /* Notify the platform of the removal, in case they
2025 * need to do anything...
2026 */
2027 if (platform_notify_remove)
2028 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02002029 if (dev->bus)
2030 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2031 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002032 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002033 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002035 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002036 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
David Graham White86df2682013-07-21 20:41:14 -04002038EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002041 * device_unregister - unregister device from system.
2042 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002044 * We do this in two parts, like we do device_register(). First,
2045 * we remove it from all the subsystems with device_del(), then
2046 * we decrement the reference count via put_device(). If that
2047 * is the final reference count, the device will be cleaned up
2048 * via device_release() above. Otherwise, the structure will
2049 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002051void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002053 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 device_del(dev);
2055 put_device(dev);
2056}
David Graham White86df2682013-07-21 20:41:14 -04002057EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002059static struct device *prev_device(struct klist_iter *i)
2060{
2061 struct klist_node *n = klist_prev(i);
2062 struct device *dev = NULL;
2063 struct device_private *p;
2064
2065 if (n) {
2066 p = to_device_private_parent(n);
2067 dev = p->device;
2068 }
2069 return dev;
2070}
2071
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002072static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002073{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002074 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002075 struct device *dev = NULL;
2076 struct device_private *p;
2077
2078 if (n) {
2079 p = to_device_private_parent(n);
2080 dev = p->device;
2081 }
2082 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002083}
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002086 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002087 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002088 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002089 * @uid: returned file owner
2090 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002091 * @tmp: possibly allocated string
2092 *
2093 * Return the relative path of a possible device node.
2094 * Non-default names may need to allocate a memory to compose
2095 * a name. This memory is returned in tmp and needs to be
2096 * freed by the caller.
2097 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002098const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002099 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002100 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002101{
2102 char *s;
2103
2104 *tmp = NULL;
2105
2106 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002107 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002108 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002109 if (*tmp)
2110 return *tmp;
2111
2112 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002113 if (dev->class && dev->class->devnode)
2114 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002115 if (*tmp)
2116 return *tmp;
2117
2118 /* return name without allocation, tmp == NULL */
2119 if (strchr(dev_name(dev), '!') == NULL)
2120 return dev_name(dev);
2121
2122 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002123 s = kstrdup(dev_name(dev), GFP_KERNEL);
2124 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002125 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002126 strreplace(s, '!', '/');
2127 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002128}
2129
2130/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002131 * device_for_each_child - device child iterator.
2132 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002133 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002134 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002136 * Iterate over @parent's child devices, and call @fn for each,
2137 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002139 * We check the return of @fn each time. If it returns anything
2140 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002142int device_for_each_child(struct device *parent, void *data,
2143 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002145 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002146 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int error = 0;
2148
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002149 if (!parent->p)
2150 return 0;
2151
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002152 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002153 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002154 error = fn(child, data);
2155 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return error;
2157}
David Graham White86df2682013-07-21 20:41:14 -04002158EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Cornelia Huck5ab69982006-11-16 15:42:07 +01002160/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002161 * device_for_each_child_reverse - device child iterator in reversed order.
2162 * @parent: parent struct device.
2163 * @fn: function to be called for each device.
2164 * @data: data for the callback.
2165 *
2166 * Iterate over @parent's child devices, and call @fn for each,
2167 * passing it @data.
2168 *
2169 * We check the return of @fn each time. If it returns anything
2170 * other than 0, we break out and return that value.
2171 */
2172int device_for_each_child_reverse(struct device *parent, void *data,
2173 int (*fn)(struct device *dev, void *data))
2174{
2175 struct klist_iter i;
2176 struct device *child;
2177 int error = 0;
2178
2179 if (!parent->p)
2180 return 0;
2181
2182 klist_iter_init(&parent->p->klist_children, &i);
2183 while ((child = prev_device(&i)) && !error)
2184 error = fn(child, data);
2185 klist_iter_exit(&i);
2186 return error;
2187}
2188EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2189
2190/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002191 * device_find_child - device iterator for locating a particular device.
2192 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002193 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002194 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002195 *
2196 * This is similar to the device_for_each_child() function above, but it
2197 * returns a reference to a device that is 'found' for later use, as
2198 * determined by the @match callback.
2199 *
2200 * The callback should return 0 if the device doesn't match and non-zero
2201 * if it does. If the callback returns non-zero and a reference to the
2202 * current device can be obtained, this function will return to the caller
2203 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002204 *
2205 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002206 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002207struct device *device_find_child(struct device *parent, void *data,
2208 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002209{
2210 struct klist_iter i;
2211 struct device *child;
2212
2213 if (!parent)
2214 return NULL;
2215
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002216 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002217 while ((child = next_device(&i)))
2218 if (match(child, data) && get_device(child))
2219 break;
2220 klist_iter_exit(&i);
2221 return child;
2222}
David Graham White86df2682013-07-21 20:41:14 -04002223EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225int __init devices_init(void)
2226{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002227 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2228 if (!devices_kset)
2229 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002230 dev_kobj = kobject_create_and_add("dev", NULL);
2231 if (!dev_kobj)
2232 goto dev_kobj_err;
2233 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2234 if (!sysfs_dev_block_kobj)
2235 goto block_kobj_err;
2236 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2237 if (!sysfs_dev_char_kobj)
2238 goto char_kobj_err;
2239
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002240 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002241
2242 char_kobj_err:
2243 kobject_put(sysfs_dev_block_kobj);
2244 block_kobj_err:
2245 kobject_put(dev_kobj);
2246 dev_kobj_err:
2247 kset_unregister(devices_kset);
2248 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002251static int device_check_offline(struct device *dev, void *not_used)
2252{
2253 int ret;
2254
2255 ret = device_for_each_child(dev, NULL, device_check_offline);
2256 if (ret)
2257 return ret;
2258
2259 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2260}
2261
2262/**
2263 * device_offline - Prepare the device for hot-removal.
2264 * @dev: Device to be put offline.
2265 *
2266 * Execute the device bus type's .offline() callback, if present, to prepare
2267 * the device for a subsequent hot-removal. If that succeeds, the device must
2268 * not be used until either it is removed or its bus type's .online() callback
2269 * is executed.
2270 *
2271 * Call under device_hotplug_lock.
2272 */
2273int device_offline(struct device *dev)
2274{
2275 int ret;
2276
2277 if (dev->offline_disabled)
2278 return -EPERM;
2279
2280 ret = device_for_each_child(dev, NULL, device_check_offline);
2281 if (ret)
2282 return ret;
2283
2284 device_lock(dev);
2285 if (device_supports_offline(dev)) {
2286 if (dev->offline) {
2287 ret = 1;
2288 } else {
2289 ret = dev->bus->offline(dev);
2290 if (!ret) {
2291 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2292 dev->offline = true;
2293 }
2294 }
2295 }
2296 device_unlock(dev);
2297
2298 return ret;
2299}
2300
2301/**
2302 * device_online - Put the device back online after successful device_offline().
2303 * @dev: Device to be put back online.
2304 *
2305 * If device_offline() has been successfully executed for @dev, but the device
2306 * has not been removed subsequently, execute its bus type's .online() callback
2307 * to indicate that the device can be used again.
2308 *
2309 * Call under device_hotplug_lock.
2310 */
2311int device_online(struct device *dev)
2312{
2313 int ret = 0;
2314
2315 device_lock(dev);
2316 if (device_supports_offline(dev)) {
2317 if (dev->offline) {
2318 ret = dev->bus->online(dev);
2319 if (!ret) {
2320 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2321 dev->offline = false;
2322 }
2323 } else {
2324 ret = 1;
2325 }
2326 }
2327 device_unlock(dev);
2328
2329 return ret;
2330}
2331
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002332struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002333 struct device dev;
2334 struct module *owner;
2335};
2336
Josh Triplett93058422012-11-18 21:27:55 -08002337static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002338{
2339 return container_of(d, struct root_device, dev);
2340}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002341
2342static void root_device_release(struct device *dev)
2343{
2344 kfree(to_root_device(dev));
2345}
2346
2347/**
2348 * __root_device_register - allocate and register a root device
2349 * @name: root device name
2350 * @owner: owner module of the root device, usually THIS_MODULE
2351 *
2352 * This function allocates a root device and registers it
2353 * using device_register(). In order to free the returned
2354 * device, use root_device_unregister().
2355 *
2356 * Root devices are dummy devices which allow other devices
2357 * to be grouped under /sys/devices. Use this function to
2358 * allocate a root device and then use it as the parent of
2359 * any device which should appear under /sys/devices/{name}
2360 *
2361 * The /sys/devices/{name} directory will also contain a
2362 * 'module' symlink which points to the @owner directory
2363 * in sysfs.
2364 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002365 * Returns &struct device pointer on success, or ERR_PTR() on error.
2366 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002367 * Note: You probably want to use root_device_register().
2368 */
2369struct device *__root_device_register(const char *name, struct module *owner)
2370{
2371 struct root_device *root;
2372 int err = -ENOMEM;
2373
2374 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2375 if (!root)
2376 return ERR_PTR(err);
2377
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002378 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002379 if (err) {
2380 kfree(root);
2381 return ERR_PTR(err);
2382 }
2383
2384 root->dev.release = root_device_release;
2385
2386 err = device_register(&root->dev);
2387 if (err) {
2388 put_device(&root->dev);
2389 return ERR_PTR(err);
2390 }
2391
Christoph Egger1d9e8822010-05-17 16:57:58 +02002392#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002393 if (owner) {
2394 struct module_kobject *mk = &owner->mkobj;
2395
2396 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2397 if (err) {
2398 device_unregister(&root->dev);
2399 return ERR_PTR(err);
2400 }
2401 root->owner = owner;
2402 }
2403#endif
2404
2405 return &root->dev;
2406}
2407EXPORT_SYMBOL_GPL(__root_device_register);
2408
2409/**
2410 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002411 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002412 *
2413 * This function unregisters and cleans up a device that was created by
2414 * root_device_register().
2415 */
2416void root_device_unregister(struct device *dev)
2417{
2418 struct root_device *root = to_root_device(dev);
2419
2420 if (root->owner)
2421 sysfs_remove_link(&root->dev.kobj, "module");
2422
2423 device_unregister(dev);
2424}
2425EXPORT_SYMBOL_GPL(root_device_unregister);
2426
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002427
2428static void device_create_release(struct device *dev)
2429{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002430 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002431 kfree(dev);
2432}
2433
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002434static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002435device_create_groups_vargs(struct class *class, struct device *parent,
2436 dev_t devt, void *drvdata,
2437 const struct attribute_group **groups,
2438 const char *fmt, va_list args)
2439{
2440 struct device *dev = NULL;
2441 int retval = -ENODEV;
2442
2443 if (class == NULL || IS_ERR(class))
2444 goto error;
2445
2446 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2447 if (!dev) {
2448 retval = -ENOMEM;
2449 goto error;
2450 }
2451
David Herrmannbbc780f2013-11-21 20:15:48 +01002452 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002453 dev->devt = devt;
2454 dev->class = class;
2455 dev->parent = parent;
2456 dev->groups = groups;
2457 dev->release = device_create_release;
2458 dev_set_drvdata(dev, drvdata);
2459
2460 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2461 if (retval)
2462 goto error;
2463
David Herrmannbbc780f2013-11-21 20:15:48 +01002464 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002465 if (retval)
2466 goto error;
2467
2468 return dev;
2469
2470error:
2471 put_device(dev);
2472 return ERR_PTR(retval);
2473}
2474
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002475/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002476 * device_create_vargs - creates a device and registers it with sysfs
2477 * @class: pointer to the struct class that this device should be registered to
2478 * @parent: pointer to the parent struct device of this new device, if any
2479 * @devt: the dev_t for the char device to be added
2480 * @drvdata: the data to be added to the device for callbacks
2481 * @fmt: string for the device's name
2482 * @args: va_list for the device's name
2483 *
2484 * This function can be used by char device classes. A struct device
2485 * will be created in sysfs, registered to the specified class.
2486 *
2487 * A "dev" file will be created, showing the dev_t for the device, if
2488 * the dev_t is not 0,0.
2489 * If a pointer to a parent struct device is passed in, the newly created
2490 * struct device will be a child of that device in sysfs.
2491 * The pointer to the struct device will be returned from the call.
2492 * Any further sysfs files that might be required can be created using this
2493 * pointer.
2494 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002495 * Returns &struct device pointer on success, or ERR_PTR() on error.
2496 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002497 * Note: the struct class passed to this function must have previously
2498 * been created with a call to class_create().
2499 */
2500struct device *device_create_vargs(struct class *class, struct device *parent,
2501 dev_t devt, void *drvdata, const char *fmt,
2502 va_list args)
2503{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002504 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2505 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002506}
2507EXPORT_SYMBOL_GPL(device_create_vargs);
2508
2509/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002510 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002511 * @class: pointer to the struct class that this device should be registered to
2512 * @parent: pointer to the parent struct device of this new device, if any
2513 * @devt: the dev_t for the char device to be added
2514 * @drvdata: the data to be added to the device for callbacks
2515 * @fmt: string for the device's name
2516 *
2517 * This function can be used by char device classes. A struct device
2518 * will be created in sysfs, registered to the specified class.
2519 *
2520 * A "dev" file will be created, showing the dev_t for the device, if
2521 * the dev_t is not 0,0.
2522 * If a pointer to a parent struct device is passed in, the newly created
2523 * struct device will be a child of that device in sysfs.
2524 * The pointer to the struct device will be returned from the call.
2525 * Any further sysfs files that might be required can be created using this
2526 * pointer.
2527 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002528 * Returns &struct device pointer on success, or ERR_PTR() on error.
2529 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002530 * Note: the struct class passed to this function must have previously
2531 * been created with a call to class_create().
2532 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002533struct device *device_create(struct class *class, struct device *parent,
2534 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002535{
2536 va_list vargs;
2537 struct device *dev;
2538
2539 va_start(vargs, fmt);
2540 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2541 va_end(vargs);
2542 return dev;
2543}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002544EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002545
Guenter Roeck39ef3112013-07-14 16:05:57 -07002546/**
2547 * device_create_with_groups - creates a device and registers it with sysfs
2548 * @class: pointer to the struct class that this device should be registered to
2549 * @parent: pointer to the parent struct device of this new device, if any
2550 * @devt: the dev_t for the char device to be added
2551 * @drvdata: the data to be added to the device for callbacks
2552 * @groups: NULL-terminated list of attribute groups to be created
2553 * @fmt: string for the device's name
2554 *
2555 * This function can be used by char device classes. A struct device
2556 * will be created in sysfs, registered to the specified class.
2557 * Additional attributes specified in the groups parameter will also
2558 * be created automatically.
2559 *
2560 * A "dev" file will be created, showing the dev_t for the device, if
2561 * the dev_t is not 0,0.
2562 * If a pointer to a parent struct device is passed in, the newly created
2563 * struct device will be a child of that device in sysfs.
2564 * The pointer to the struct device will be returned from the call.
2565 * Any further sysfs files that might be required can be created using this
2566 * pointer.
2567 *
2568 * Returns &struct device pointer on success, or ERR_PTR() on error.
2569 *
2570 * Note: the struct class passed to this function must have previously
2571 * been created with a call to class_create().
2572 */
2573struct device *device_create_with_groups(struct class *class,
2574 struct device *parent, dev_t devt,
2575 void *drvdata,
2576 const struct attribute_group **groups,
2577 const char *fmt, ...)
2578{
2579 va_list vargs;
2580 struct device *dev;
2581
2582 va_start(vargs, fmt);
2583 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2584 fmt, vargs);
2585 va_end(vargs);
2586 return dev;
2587}
2588EXPORT_SYMBOL_GPL(device_create_with_groups);
2589
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002590static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002591{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002592 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002593
Dave Youngcd354492008-01-28 16:56:11 +08002594 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002595}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002596
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002597/**
2598 * device_destroy - removes a device that was created with device_create()
2599 * @class: pointer to the struct class that this device was registered with
2600 * @devt: the dev_t of the device that was previously registered
2601 *
2602 * This call unregisters and cleans up a device that was created with a
2603 * call to device_create().
2604 */
2605void device_destroy(struct class *class, dev_t devt)
2606{
2607 struct device *dev;
2608
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002609 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002610 if (dev) {
2611 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002612 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002613 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002614}
2615EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002616
2617/**
2618 * device_rename - renames a device
2619 * @dev: the pointer to the struct device to be renamed
2620 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002621 *
2622 * It is the responsibility of the caller to provide mutual
2623 * exclusion between two different calls of device_rename
2624 * on the same device to ensure that new_name is valid and
2625 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002626 *
Timur Tabia5462512010-12-13 14:08:52 -06002627 * Note: Don't call this function. Currently, the networking layer calls this
2628 * function, but that will change. The following text from Kay Sievers offers
2629 * some insight:
2630 *
2631 * Renaming devices is racy at many levels, symlinks and other stuff are not
2632 * replaced atomically, and you get a "move" uevent, but it's not easy to
2633 * connect the event to the old and new device. Device nodes are not renamed at
2634 * all, there isn't even support for that in the kernel now.
2635 *
2636 * In the meantime, during renaming, your target name might be taken by another
2637 * driver, creating conflicts. Or the old name is taken directly after you
2638 * renamed it -- then you get events for the same DEVPATH, before you even see
2639 * the "move" event. It's just a mess, and nothing new should ever rely on
2640 * kernel device renaming. Besides that, it's not even implemented now for
2641 * other things than (driver-core wise very simple) network devices.
2642 *
2643 * We are currently about to change network renaming in udev to completely
2644 * disallow renaming of devices in the same namespace as the kernel uses,
2645 * because we can't solve the problems properly, that arise with swapping names
2646 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2647 * be allowed to some other name than eth[0-9]*, for the aforementioned
2648 * reasons.
2649 *
2650 * Make up a "real" name in the driver before you register anything, or add
2651 * some other attributes for userspace to find the device, or use udev to add
2652 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2653 * don't even want to get into that and try to implement the missing pieces in
2654 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002655 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002656int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002657{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002658 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002659 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002660 int error;
2661
2662 dev = get_device(dev);
2663 if (!dev)
2664 return -EINVAL;
2665
ethan.zhao69df7532013-10-13 22:12:35 +08002666 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002667
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002668 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002669 if (!old_device_name) {
2670 error = -ENOMEM;
2671 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002672 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002673
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002674 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002675 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2676 kobj, old_device_name,
2677 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002678 if (error)
2679 goto out;
2680 }
Kay Sievers39aba962010-09-04 22:33:14 -07002681
Tejun Heo4b30ee52013-09-11 22:29:06 -04002682 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002683 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002684 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002685
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002686out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002687 put_device(dev);
2688
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002689 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002690
2691 return error;
2692}
Johannes Berga2807db2007-02-28 12:38:31 +01002693EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002694
2695static int device_move_class_links(struct device *dev,
2696 struct device *old_parent,
2697 struct device *new_parent)
2698{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002699 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002700
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002701 if (old_parent)
2702 sysfs_remove_link(&dev->kobj, "device");
2703 if (new_parent)
2704 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2705 "device");
2706 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002707}
2708
2709/**
2710 * device_move - moves a device to a new parent
2711 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02002712 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002713 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002714 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002715int device_move(struct device *dev, struct device *new_parent,
2716 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002717{
2718 int error;
2719 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002720 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002721
2722 dev = get_device(dev);
2723 if (!dev)
2724 return -EINVAL;
2725
Cornelia Huckffa6a702009-03-04 12:44:00 +01002726 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002727 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002728 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002729 if (IS_ERR(new_parent_kobj)) {
2730 error = PTR_ERR(new_parent_kobj);
2731 put_device(new_parent);
2732 goto out;
2733 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002734
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002735 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2736 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002737 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002738 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002739 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002740 put_device(new_parent);
2741 goto out;
2742 }
2743 old_parent = dev->parent;
2744 dev->parent = new_parent;
2745 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002746 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002747 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002748 klist_add_tail(&dev->p->knode_parent,
2749 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002750 set_dev_node(dev, dev_to_node(new_parent));
2751 }
2752
Rabin Vincentbdd40342012-04-23 09:16:36 +02002753 if (dev->class) {
2754 error = device_move_class_links(dev, old_parent, new_parent);
2755 if (error) {
2756 /* We ignore errors on cleanup since we're hosed anyway... */
2757 device_move_class_links(dev, new_parent, old_parent);
2758 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2759 if (new_parent)
2760 klist_remove(&dev->p->knode_parent);
2761 dev->parent = old_parent;
2762 if (old_parent) {
2763 klist_add_tail(&dev->p->knode_parent,
2764 &old_parent->p->klist_children);
2765 set_dev_node(dev, dev_to_node(old_parent));
2766 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002767 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002768 cleanup_glue_dir(dev, new_parent_kobj);
2769 put_device(new_parent);
2770 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002771 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002772 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002773 switch (dpm_order) {
2774 case DPM_ORDER_NONE:
2775 break;
2776 case DPM_ORDER_DEV_AFTER_PARENT:
2777 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002778 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002779 break;
2780 case DPM_ORDER_PARENT_BEFORE_DEV:
2781 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002782 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002783 break;
2784 case DPM_ORDER_DEV_LAST:
2785 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002786 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002787 break;
2788 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002789
Cornelia Huck8a824722006-11-20 17:07:51 +01002790 put_device(old_parent);
2791out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002792 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002793 put_device(dev);
2794 return error;
2795}
Cornelia Huck8a824722006-11-20 17:07:51 +01002796EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002797
2798/**
2799 * device_shutdown - call ->shutdown() on each device to shutdown.
2800 */
2801void device_shutdown(void)
2802{
Benson Leungf123db82013-09-24 20:05:11 -07002803 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002804
Hugh Daschbach62458382010-03-22 10:36:37 -07002805 spin_lock(&devices_kset->list_lock);
2806 /*
2807 * Walk the devices list backward, shutting down each in turn.
2808 * Beware that device unplug events may also start pulling
2809 * devices offline, even as the system is shutting down.
2810 */
2811 while (!list_empty(&devices_kset->list)) {
2812 dev = list_entry(devices_kset->list.prev, struct device,
2813 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002814
2815 /*
2816 * hold reference count of device's parent to
2817 * prevent it from being freed because parent's
2818 * lock is to be held
2819 */
Benson Leungf123db82013-09-24 20:05:11 -07002820 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002821 get_device(dev);
2822 /*
2823 * Make sure the device is off the kset list, in the
2824 * event that dev->*->shutdown() doesn't remove it.
2825 */
2826 list_del_init(&dev->kobj.entry);
2827 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002828
Ming Leid1c6c032012-06-22 18:01:40 +08002829 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002830 if (parent)
2831 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002832 device_lock(dev);
2833
Alan Sternfe6b91f2011-12-06 23:24:52 +01002834 /* Don't allow any more runtime suspends */
2835 pm_runtime_get_noresume(dev);
2836 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002837
Michal Suchanek75216212017-08-11 15:44:43 +02002838 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07002839 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02002840 dev_info(dev, "shutdown_pre\n");
2841 dev->class->shutdown_pre(dev);
2842 }
2843 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002844 if (initcall_debug)
2845 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002846 dev->bus->shutdown(dev);
2847 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002848 if (initcall_debug)
2849 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002850 dev->driver->shutdown(dev);
2851 }
Ming Leid1c6c032012-06-22 18:01:40 +08002852
2853 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002854 if (parent)
2855 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002856
Hugh Daschbach62458382010-03-22 10:36:37 -07002857 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002858 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002859
2860 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002861 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002862 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002863}
Joe Perches99bcf212010-06-27 01:02:34 +00002864
2865/*
2866 * Device logging functions
2867 */
2868
2869#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002870static int
2871create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002872{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002873 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002874 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002875
Kay Sieversc4e00da2012-05-03 02:29:59 +02002876 if (dev->class)
2877 subsys = dev->class->name;
2878 else if (dev->bus)
2879 subsys = dev->bus->name;
2880 else
Joe Perches798efc62012-09-12 20:11:29 -07002881 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002882
Joe Perches798efc62012-09-12 20:11:29 -07002883 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002884 if (pos >= hdrlen)
2885 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002886
2887 /*
2888 * Add device identifier DEVICE=:
2889 * b12:8 block dev_t
2890 * c127:3 char dev_t
2891 * n8 netdev ifindex
2892 * +sound:card0 subsystem:devname
2893 */
2894 if (MAJOR(dev->devt)) {
2895 char c;
2896
2897 if (strcmp(subsys, "block") == 0)
2898 c = 'b';
2899 else
2900 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002901 pos++;
2902 pos += snprintf(hdr + pos, hdrlen - pos,
2903 "DEVICE=%c%u:%u",
2904 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002905 } else if (strcmp(subsys, "net") == 0) {
2906 struct net_device *net = to_net_dev(dev);
2907
Joe Perches798efc62012-09-12 20:11:29 -07002908 pos++;
2909 pos += snprintf(hdr + pos, hdrlen - pos,
2910 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002911 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002912 pos++;
2913 pos += snprintf(hdr + pos, hdrlen - pos,
2914 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002915 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002916
Ben Hutchings655e5b72014-08-26 00:34:44 -07002917 if (pos >= hdrlen)
2918 goto overflow;
2919
Joe Perches798efc62012-09-12 20:11:29 -07002920 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002921
2922overflow:
2923 dev_WARN(dev, "device/subsystem name too long");
2924 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002925}
Joe Perches798efc62012-09-12 20:11:29 -07002926
Joe Perches05e4e5b2012-09-12 20:13:37 -07002927int dev_vprintk_emit(int level, const struct device *dev,
2928 const char *fmt, va_list args)
2929{
2930 char hdr[128];
2931 size_t hdrlen;
2932
2933 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2934
2935 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2936}
2937EXPORT_SYMBOL(dev_vprintk_emit);
2938
2939int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2940{
2941 va_list args;
2942 int r;
2943
2944 va_start(args, fmt);
2945
2946 r = dev_vprintk_emit(level, dev, fmt, args);
2947
2948 va_end(args);
2949
2950 return r;
2951}
2952EXPORT_SYMBOL(dev_printk_emit);
2953
Joe Perchesd1f10522014-12-25 15:07:04 -08002954static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07002955 struct va_format *vaf)
2956{
Joe Perchesd1f10522014-12-25 15:07:04 -08002957 if (dev)
2958 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2959 dev_driver_string(dev), dev_name(dev), vaf);
2960 else
2961 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002962}
Joe Perches99bcf212010-06-27 01:02:34 +00002963
Joe Perchesd1f10522014-12-25 15:07:04 -08002964void dev_printk(const char *level, const struct device *dev,
2965 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00002966{
2967 struct va_format vaf;
2968 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00002969
2970 va_start(args, fmt);
2971
2972 vaf.fmt = fmt;
2973 vaf.va = &args;
2974
Joe Perchesd1f10522014-12-25 15:07:04 -08002975 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002976
Joe Perches99bcf212010-06-27 01:02:34 +00002977 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00002978}
2979EXPORT_SYMBOL(dev_printk);
2980
2981#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08002982void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00002983{ \
2984 struct va_format vaf; \
2985 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00002986 \
2987 va_start(args, fmt); \
2988 \
2989 vaf.fmt = fmt; \
2990 vaf.va = &args; \
2991 \
Joe Perchesd1f10522014-12-25 15:07:04 -08002992 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002993 \
Joe Perches99bcf212010-06-27 01:02:34 +00002994 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00002995} \
2996EXPORT_SYMBOL(func);
2997
2998define_dev_printk_level(dev_emerg, KERN_EMERG);
2999define_dev_printk_level(dev_alert, KERN_ALERT);
3000define_dev_printk_level(dev_crit, KERN_CRIT);
3001define_dev_printk_level(dev_err, KERN_ERR);
3002define_dev_printk_level(dev_warn, KERN_WARNING);
3003define_dev_printk_level(dev_notice, KERN_NOTICE);
3004define_dev_printk_level(_dev_info, KERN_INFO);
3005
3006#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003007
3008static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3009{
3010 return fwnode && !IS_ERR(fwnode->secondary);
3011}
3012
3013/**
3014 * set_primary_fwnode - Change the primary firmware node of a given device.
3015 * @dev: Device to handle.
3016 * @fwnode: New primary firmware node of the device.
3017 *
3018 * Set the device's firmware node pointer to @fwnode, but if a secondary
3019 * firmware node of the device is present, preserve it.
3020 */
3021void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3022{
3023 if (fwnode) {
3024 struct fwnode_handle *fn = dev->fwnode;
3025
3026 if (fwnode_is_primary(fn))
3027 fn = fn->secondary;
3028
Mika Westerberg55f89a82015-11-30 17:11:39 +02003029 if (fn) {
3030 WARN_ON(fwnode->secondary);
3031 fwnode->secondary = fn;
3032 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003033 dev->fwnode = fwnode;
3034 } else {
3035 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3036 dev->fwnode->secondary : NULL;
3037 }
3038}
3039EXPORT_SYMBOL_GPL(set_primary_fwnode);
3040
3041/**
3042 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3043 * @dev: Device to handle.
3044 * @fwnode: New secondary firmware node of the device.
3045 *
3046 * If a primary firmware node of the device is present, set its secondary
3047 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3048 * @fwnode.
3049 */
3050void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3051{
3052 if (fwnode)
3053 fwnode->secondary = ERR_PTR(-ENODEV);
3054
3055 if (fwnode_is_primary(dev->fwnode))
3056 dev->fwnode->secondary = fwnode;
3057 else
3058 dev->fwnode = fwnode;
3059}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003060
3061/**
3062 * device_set_of_node_from_dev - reuse device-tree node of another device
3063 * @dev: device whose device-tree node is being set
3064 * @dev2: device whose device-tree node is being reused
3065 *
3066 * Takes another reference to the new device-tree node after first dropping
3067 * any reference held to the old node.
3068 */
3069void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3070{
3071 of_node_put(dev->of_node);
3072 dev->of_node = of_node_get(dev2->of_node);
3073 dev->of_node_reused = true;
3074}
3075EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);