blob: 14c1e3151e08d65cb09412e04dc66d0531253c0c [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 *
Vivek Gautame88728f2018-06-27 18:20:55 +0530181 * If the DL_FLAG_AUTOREMOVE_CONSUMER is set, the link will be removed
182 * automatically when the consumer device driver unbinds from it.
183 * The combination of both DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_STATELESS
184 * set is invalid and will cause NULL to be returned.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100185 *
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 ||
Vivek Gautame88728f2018-06-27 18:20:55 +0530201 ((flags & DL_FLAG_STATELESS) &&
202 (flags & DL_FLAG_AUTOREMOVE_CONSUMER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100203 return NULL;
204
205 device_links_write_lock();
206 device_pm_lock();
207
208 /*
209 * If the supplier has not been fully registered yet or there is a
210 * reverse dependency between the consumer and the supplier already in
211 * the graph, return NULL.
212 */
213 if (!device_pm_initialized(supplier)
214 || device_is_dependent(consumer, supplier)) {
215 link = NULL;
216 goto out;
217 }
218
219 list_for_each_entry(link, &supplier->links.consumers, s_node)
Lukas Wunneread18c22018-02-10 19:27:12 +0100220 if (link->consumer == consumer) {
221 kref_get(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100222 goto out;
Lukas Wunneread18c22018-02-10 19:27:12 +0100223 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100224
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100225 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100226 if (!link)
227 goto out;
228
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100229 if (flags & DL_FLAG_PM_RUNTIME) {
230 if (flags & DL_FLAG_RPM_ACTIVE) {
231 if (pm_runtime_get_sync(supplier) < 0) {
232 pm_runtime_put_noidle(supplier);
233 kfree(link);
234 link = NULL;
235 goto out;
236 }
237 link->rpm_active = true;
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100238 }
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100239 pm_runtime_new_link(consumer);
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200240 /*
241 * If the link is being added by the consumer driver at probe
242 * time, balance the decrementation of the supplier's runtime PM
243 * usage counter after consumer probe in driver_probe_device().
244 */
245 if (consumer->links.status == DL_DEV_PROBING)
246 pm_runtime_get_noresume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100247 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100248 get_device(supplier);
249 link->supplier = supplier;
250 INIT_LIST_HEAD(&link->s_node);
251 get_device(consumer);
252 link->consumer = consumer;
253 INIT_LIST_HEAD(&link->c_node);
254 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100255 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100256
Lukas Wunner64df1142016-12-04 13:10:04 +0100257 /* Determine the initial link state. */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100258 if (flags & DL_FLAG_STATELESS) {
259 link->status = DL_STATE_NONE;
260 } else {
261 switch (supplier->links.status) {
262 case DL_DEV_DRIVER_BOUND:
263 switch (consumer->links.status) {
264 case DL_DEV_PROBING:
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100265 /*
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200266 * Some callers expect the link creation during
267 * consumer driver probe to resume the supplier
268 * even without DL_FLAG_RPM_ACTIVE.
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100269 */
270 if (flags & DL_FLAG_PM_RUNTIME)
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200271 pm_runtime_resume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100272
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100273 link->status = DL_STATE_CONSUMER_PROBE;
274 break;
275 case DL_DEV_DRIVER_BOUND:
276 link->status = DL_STATE_ACTIVE;
277 break;
278 default:
279 link->status = DL_STATE_AVAILABLE;
280 break;
281 }
282 break;
283 case DL_DEV_UNBINDING:
284 link->status = DL_STATE_SUPPLIER_UNBIND;
285 break;
286 default:
287 link->status = DL_STATE_DORMANT;
288 break;
289 }
290 }
291
292 /*
293 * Move the consumer and all of the devices depending on it to the end
294 * of dpm_list and the devices_kset list.
295 *
296 * It is necessary to hold dpm_list locked throughout all that or else
297 * we may end up suspending with a wrong ordering of it.
298 */
299 device_reorder_to_tail(consumer, NULL);
300
301 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
302 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
303
304 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
305
306 out:
307 device_pm_unlock();
308 device_links_write_unlock();
309 return link;
310}
311EXPORT_SYMBOL_GPL(device_link_add);
312
313static void device_link_free(struct device_link *link)
314{
315 put_device(link->consumer);
316 put_device(link->supplier);
317 kfree(link);
318}
319
320#ifdef CONFIG_SRCU
321static void __device_link_free_srcu(struct rcu_head *rhead)
322{
323 device_link_free(container_of(rhead, struct device_link, rcu_head));
324}
325
Lukas Wunneread18c22018-02-10 19:27:12 +0100326static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100327{
Lukas Wunneread18c22018-02-10 19:27:12 +0100328 struct device_link *link = container_of(kref, struct device_link, kref);
329
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100330 dev_info(link->consumer, "Dropping the link to %s\n",
331 dev_name(link->supplier));
332
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100333 if (link->flags & DL_FLAG_PM_RUNTIME)
334 pm_runtime_drop_link(link->consumer);
335
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100336 list_del_rcu(&link->s_node);
337 list_del_rcu(&link->c_node);
338 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
339}
340#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100341static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100342{
Lukas Wunneread18c22018-02-10 19:27:12 +0100343 struct device_link *link = container_of(kref, struct device_link, kref);
344
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100345 dev_info(link->consumer, "Dropping the link to %s\n",
346 dev_name(link->supplier));
347
Lukas Wunner433986c2018-02-10 19:13:58 +0100348 if (link->flags & DL_FLAG_PM_RUNTIME)
349 pm_runtime_drop_link(link->consumer);
350
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100351 list_del(&link->s_node);
352 list_del(&link->c_node);
353 device_link_free(link);
354}
355#endif /* !CONFIG_SRCU */
356
357/**
358 * device_link_del - Delete a link between two devices.
359 * @link: Device link to delete.
360 *
361 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100362 * PM. If the link was added multiple times, it needs to be deleted as often.
363 * Care is required for hotplugged devices: Their links are purged on removal
364 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100365 */
366void device_link_del(struct device_link *link)
367{
368 device_links_write_lock();
369 device_pm_lock();
Lukas Wunneread18c22018-02-10 19:27:12 +0100370 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100371 device_pm_unlock();
372 device_links_write_unlock();
373}
374EXPORT_SYMBOL_GPL(device_link_del);
375
376static void device_links_missing_supplier(struct device *dev)
377{
378 struct device_link *link;
379
380 list_for_each_entry(link, &dev->links.suppliers, c_node)
381 if (link->status == DL_STATE_CONSUMER_PROBE)
382 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
383}
384
385/**
386 * device_links_check_suppliers - Check presence of supplier drivers.
387 * @dev: Consumer device.
388 *
389 * Check links from this device to any suppliers. Walk the list of the device's
390 * links to suppliers and see if all of them are available. If not, simply
391 * return -EPROBE_DEFER.
392 *
393 * We need to guarantee that the supplier will not go away after the check has
394 * been positive here. It only can go away in __device_release_driver() and
395 * that function checks the device's links to consumers. This means we need to
396 * mark the link as "consumer probe in progress" to make the supplier removal
397 * wait for us to complete (or bad things may happen).
398 *
399 * Links with the DL_FLAG_STATELESS flag set are ignored.
400 */
401int device_links_check_suppliers(struct device *dev)
402{
403 struct device_link *link;
404 int ret = 0;
405
406 device_links_write_lock();
407
408 list_for_each_entry(link, &dev->links.suppliers, c_node) {
409 if (link->flags & DL_FLAG_STATELESS)
410 continue;
411
412 if (link->status != DL_STATE_AVAILABLE) {
413 device_links_missing_supplier(dev);
414 ret = -EPROBE_DEFER;
415 break;
416 }
417 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
418 }
419 dev->links.status = DL_DEV_PROBING;
420
421 device_links_write_unlock();
422 return ret;
423}
424
425/**
426 * device_links_driver_bound - Update device links after probing its driver.
427 * @dev: Device to update the links for.
428 *
429 * The probe has been successful, so update links from this device to any
430 * consumers by changing their status to "available".
431 *
432 * Also change the status of @dev's links to suppliers to "active".
433 *
434 * Links with the DL_FLAG_STATELESS flag set are ignored.
435 */
436void device_links_driver_bound(struct device *dev)
437{
438 struct device_link *link;
439
440 device_links_write_lock();
441
442 list_for_each_entry(link, &dev->links.consumers, s_node) {
443 if (link->flags & DL_FLAG_STATELESS)
444 continue;
445
446 WARN_ON(link->status != DL_STATE_DORMANT);
447 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
448 }
449
450 list_for_each_entry(link, &dev->links.suppliers, c_node) {
451 if (link->flags & DL_FLAG_STATELESS)
452 continue;
453
454 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
455 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
456 }
457
458 dev->links.status = DL_DEV_DRIVER_BOUND;
459
460 device_links_write_unlock();
461}
462
463/**
464 * __device_links_no_driver - Update links of a device without a driver.
465 * @dev: Device without a drvier.
466 *
467 * Delete all non-persistent links from this device to any suppliers.
468 *
469 * Persistent links stay around, but their status is changed to "available",
470 * unless they already are in the "supplier unbind in progress" state in which
471 * case they need not be updated.
472 *
473 * Links with the DL_FLAG_STATELESS flag set are ignored.
474 */
475static void __device_links_no_driver(struct device *dev)
476{
477 struct device_link *link, *ln;
478
479 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
480 if (link->flags & DL_FLAG_STATELESS)
481 continue;
482
Vivek Gautame88728f2018-06-27 18:20:55 +0530483 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Lukas Wunneread18c22018-02-10 19:27:12 +0100484 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100485 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
486 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
487 }
488
489 dev->links.status = DL_DEV_NO_DRIVER;
490}
491
492void device_links_no_driver(struct device *dev)
493{
494 device_links_write_lock();
495 __device_links_no_driver(dev);
496 device_links_write_unlock();
497}
498
499/**
500 * device_links_driver_cleanup - Update links after driver removal.
501 * @dev: Device whose driver has just gone away.
502 *
503 * Update links to consumers for @dev by changing their status to "dormant" and
504 * invoke %__device_links_no_driver() to update links to suppliers for it as
505 * appropriate.
506 *
507 * Links with the DL_FLAG_STATELESS flag set are ignored.
508 */
509void device_links_driver_cleanup(struct device *dev)
510{
511 struct device_link *link;
512
513 device_links_write_lock();
514
515 list_for_each_entry(link, &dev->links.consumers, s_node) {
516 if (link->flags & DL_FLAG_STATELESS)
517 continue;
518
Vivek Gautame88728f2018-06-27 18:20:55 +0530519 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100520 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
521 WRITE_ONCE(link->status, DL_STATE_DORMANT);
522 }
523
524 __device_links_no_driver(dev);
525
526 device_links_write_unlock();
527}
528
529/**
530 * device_links_busy - Check if there are any busy links to consumers.
531 * @dev: Device to check.
532 *
533 * Check each consumer of the device and return 'true' if its link's status
534 * is one of "consumer probe" or "active" (meaning that the given consumer is
535 * probing right now or its driver is present). Otherwise, change the link
536 * state to "supplier unbind" to prevent the consumer from being probed
537 * successfully going forward.
538 *
539 * Return 'false' if there are no probing or active consumers.
540 *
541 * Links with the DL_FLAG_STATELESS flag set are ignored.
542 */
543bool device_links_busy(struct device *dev)
544{
545 struct device_link *link;
546 bool ret = false;
547
548 device_links_write_lock();
549
550 list_for_each_entry(link, &dev->links.consumers, s_node) {
551 if (link->flags & DL_FLAG_STATELESS)
552 continue;
553
554 if (link->status == DL_STATE_CONSUMER_PROBE
555 || link->status == DL_STATE_ACTIVE) {
556 ret = true;
557 break;
558 }
559 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
560 }
561
562 dev->links.status = DL_DEV_UNBINDING;
563
564 device_links_write_unlock();
565 return ret;
566}
567
568/**
569 * device_links_unbind_consumers - Force unbind consumers of the given device.
570 * @dev: Device to unbind the consumers of.
571 *
572 * Walk the list of links to consumers for @dev and if any of them is in the
573 * "consumer probe" state, wait for all device probes in progress to complete
574 * and start over.
575 *
576 * If that's not the case, change the status of the link to "supplier unbind"
577 * and check if the link was in the "active" state. If so, force the consumer
578 * driver to unbind and start over (the consumer will not re-probe as we have
579 * changed the state of the link already).
580 *
581 * Links with the DL_FLAG_STATELESS flag set are ignored.
582 */
583void device_links_unbind_consumers(struct device *dev)
584{
585 struct device_link *link;
586
587 start:
588 device_links_write_lock();
589
590 list_for_each_entry(link, &dev->links.consumers, s_node) {
591 enum device_link_state status;
592
593 if (link->flags & DL_FLAG_STATELESS)
594 continue;
595
596 status = link->status;
597 if (status == DL_STATE_CONSUMER_PROBE) {
598 device_links_write_unlock();
599
600 wait_for_device_probe();
601 goto start;
602 }
603 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
604 if (status == DL_STATE_ACTIVE) {
605 struct device *consumer = link->consumer;
606
607 get_device(consumer);
608
609 device_links_write_unlock();
610
611 device_release_driver_internal(consumer, NULL,
612 consumer->parent);
613 put_device(consumer);
614 goto start;
615 }
616 }
617
618 device_links_write_unlock();
619}
620
621/**
622 * device_links_purge - Delete existing links to other devices.
623 * @dev: Target device.
624 */
625static void device_links_purge(struct device *dev)
626{
627 struct device_link *link, *ln;
628
629 /*
630 * Delete all of the remaining links from this device to any other
631 * devices (either consumers or suppliers).
632 */
633 device_links_write_lock();
634
635 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
636 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100637 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100638 }
639
640 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
641 WARN_ON(link->status != DL_STATE_DORMANT &&
642 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100643 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100644 }
645
646 device_links_write_unlock();
647}
648
649/* Device links support end. */
650
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800651int (*platform_notify)(struct device *dev) = NULL;
652int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700653static struct kobject *dev_kobj;
654struct kobject *sysfs_dev_char_kobj;
655struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200657static DEFINE_MUTEX(device_hotplug_lock);
658
659void lock_device_hotplug(void)
660{
661 mutex_lock(&device_hotplug_lock);
662}
663
664void unlock_device_hotplug(void)
665{
666 mutex_unlock(&device_hotplug_lock);
667}
668
669int lock_device_hotplug_sysfs(void)
670{
671 if (mutex_trylock(&device_hotplug_lock))
672 return 0;
673
674 /* Avoid busy looping (5 ms of sleep should do). */
675 msleep(5);
676 return restart_syscall();
677}
678
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800679#ifdef CONFIG_BLOCK
680static inline int device_is_not_partition(struct device *dev)
681{
682 return !(dev->type == &part_type);
683}
684#else
685static inline int device_is_not_partition(struct device *dev)
686{
687 return 1;
688}
689#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Alan Stern3e956372006-06-16 17:10:48 -0400691/**
692 * dev_driver_string - Return a device's driver name, if at all possible
693 * @dev: struct device to get the name of
694 *
695 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800696 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400697 * it is attached to. If it is not attached to a bus either, an empty
698 * string will be returned.
699 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700700const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400701{
Alan Stern35899722009-12-04 11:06:57 -0500702 struct device_driver *drv;
703
704 /* dev->driver can change to NULL underneath us because of unbinding,
705 * so be careful about accessing it. dev->bus and dev->class should
706 * never change once they are set, so they don't need special care.
707 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700708 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500709 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100710 (dev->bus ? dev->bus->name :
711 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400712}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600713EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
716
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800717static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
718 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800720 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200721 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500722 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400725 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800726 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900727 printk("dev_attr_show: %pS returned bad count\n",
728 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return ret;
731}
732
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800733static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
734 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800736 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200737 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500738 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400741 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return ret;
743}
744
Emese Revfy52cf25d2010-01-19 02:58:23 +0100745static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 .show = dev_attr_show,
747 .store = dev_attr_store,
748};
749
Kay Sieversca22e562011-12-14 14:29:38 -0800750#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
751
752ssize_t device_store_ulong(struct device *dev,
753 struct device_attribute *attr,
754 const char *buf, size_t size)
755{
756 struct dev_ext_attribute *ea = to_ext_attr(attr);
757 char *end;
758 unsigned long new = simple_strtoul(buf, &end, 0);
759 if (end == buf)
760 return -EINVAL;
761 *(unsigned long *)(ea->var) = new;
762 /* Always return full write size even if we didn't consume all */
763 return size;
764}
765EXPORT_SYMBOL_GPL(device_store_ulong);
766
767ssize_t device_show_ulong(struct device *dev,
768 struct device_attribute *attr,
769 char *buf)
770{
771 struct dev_ext_attribute *ea = to_ext_attr(attr);
772 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
773}
774EXPORT_SYMBOL_GPL(device_show_ulong);
775
776ssize_t device_store_int(struct device *dev,
777 struct device_attribute *attr,
778 const char *buf, size_t size)
779{
780 struct dev_ext_attribute *ea = to_ext_attr(attr);
781 char *end;
782 long new = simple_strtol(buf, &end, 0);
783 if (end == buf || new > INT_MAX || new < INT_MIN)
784 return -EINVAL;
785 *(int *)(ea->var) = new;
786 /* Always return full write size even if we didn't consume all */
787 return size;
788}
789EXPORT_SYMBOL_GPL(device_store_int);
790
791ssize_t device_show_int(struct device *dev,
792 struct device_attribute *attr,
793 char *buf)
794{
795 struct dev_ext_attribute *ea = to_ext_attr(attr);
796
797 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
798}
799EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Borislav Petkov91872392012-10-09 19:52:05 +0200801ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
802 const char *buf, size_t size)
803{
804 struct dev_ext_attribute *ea = to_ext_attr(attr);
805
806 if (strtobool(buf, ea->var) < 0)
807 return -EINVAL;
808
809 return size;
810}
811EXPORT_SYMBOL_GPL(device_store_bool);
812
813ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
814 char *buf)
815{
816 struct dev_ext_attribute *ea = to_ext_attr(attr);
817
818 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
819}
820EXPORT_SYMBOL_GPL(device_show_bool);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400823 * device_release - free device structure.
824 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400826 * This is called once the reference count for the object
827 * reaches 0. We forward the call to the device's release
828 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800830static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200832 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800833 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Ming Leia525a3d2012-07-25 01:42:29 +0800835 /*
836 * Some platform devices are driven without driver attached
837 * and managed resources may have been acquired. Make sure
838 * all resources are released.
839 *
840 * Drivers still can add resources into device after device
841 * is deleted but alive, so release devres here to avoid
842 * possible memory leak.
843 */
844 devres_release_all(dev);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (dev->release)
847 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200848 else if (dev->type && dev->type->release)
849 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700850 else if (dev->class && dev->class->dev_release)
851 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700852 else
853 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800854 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100855 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800856 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700859static const void *device_namespace(struct kobject *kobj)
860{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200861 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700862 const void *ns = NULL;
863
864 if (dev->class && dev->class->ns_type)
865 ns = dev->class->namespace(dev);
866
867 return ns;
868}
869
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600870static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 .release = device_release,
872 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700873 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874};
875
876
Kay Sievers312c0042005-11-16 09:00:00 +0100877static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 struct kobj_type *ktype = get_ktype(kobj);
880
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600881 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200882 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (dev->bus)
884 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700885 if (dev->class)
886 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 return 0;
889}
890
Kay Sievers312c0042005-11-16 09:00:00 +0100891static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200893 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700895 if (dev->bus)
896 return dev->bus->name;
897 if (dev->class)
898 return dev->class->name;
899 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Kay Sievers7eff2e72007-08-14 15:15:12 +0200902static int dev_uevent(struct kset *kset, struct kobject *kobj,
903 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200905 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int retval = 0;
907
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200908 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700909 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200910 const char *tmp;
911 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400912 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700913 kuid_t uid = GLOBAL_ROOT_UID;
914 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200915
Kay Sievers7eff2e72007-08-14 15:15:12 +0200916 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
917 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700918 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200919 if (name) {
920 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200921 if (mode)
922 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700923 if (!uid_eq(uid, GLOBAL_ROOT_UID))
924 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
925 if (!gid_eq(gid, GLOBAL_ROOT_GID))
926 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700927 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200928 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700929 }
930
Kay Sievers414264f2007-03-12 21:08:57 +0100931 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200932 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100933
Kay Sievers239378f2006-10-07 21:54:55 +0200934 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200935 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200936
Grant Likely07d57a32012-02-01 11:22:22 -0700937 /* Add common DT information about the device */
938 of_device_uevent(dev, env);
939
Kay Sievers7eff2e72007-08-14 15:15:12 +0200940 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100941 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200942 retval = dev->bus->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: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100945 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947
Kay Sievers7eff2e72007-08-14 15:15:12 +0200948 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700949 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200950 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200951 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800952 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100953 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800954 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200955 }
956
Stefan Weileef35c22010-08-06 21:11:15 +0200957 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200958 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200959 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200960 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800961 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100962 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800963 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700964 }
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return retval;
967}
968
Emese Revfy9cd43612009-12-31 14:52:51 +0100969static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100970 .filter = dev_uevent_filter,
971 .name = dev_uevent_name,
972 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973};
974
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700975static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +0200976 char *buf)
977{
978 struct kobject *top_kobj;
979 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200980 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200981 int i;
982 size_t count = 0;
983 int retval;
984
985 /* search the kset, the device belongs to */
986 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200987 while (!top_kobj->kset && top_kobj->parent)
988 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200989 if (!top_kobj->kset)
990 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200991
Kay Sievers16574dc2007-04-06 01:40:38 +0200992 kset = top_kobj->kset;
993 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
994 goto out;
995
996 /* respect filter */
997 if (kset->uevent_ops && kset->uevent_ops->filter)
998 if (!kset->uevent_ops->filter(kset, &dev->kobj))
999 goto out;
1000
Kay Sievers7eff2e72007-08-14 15:15:12 +02001001 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1002 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001003 return -ENOMEM;
1004
Kay Sievers16574dc2007-04-06 01:40:38 +02001005 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001006 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001007 if (retval)
1008 goto out;
1009
1010 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001011 for (i = 0; i < env->envp_idx; i++)
1012 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001013out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001014 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001015 return count;
1016}
1017
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001018static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001019 const char *buf, size_t count)
1020{
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001021 if (kobject_synth_uevent(&dev->kobj, buf, count))
1022 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Kay Sievers60a96a52007-07-08 22:29:26 +02001023
Kay Sieversa7fd6702005-10-01 14:49:43 +02001024 return count;
1025}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001026static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001027
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001028static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001029 char *buf)
1030{
1031 bool val;
1032
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001033 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001034 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001035 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001036 return sprintf(buf, "%u\n", val);
1037}
1038
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001039static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001040 const char *buf, size_t count)
1041{
1042 bool val;
1043 int ret;
1044
1045 ret = strtobool(buf, &val);
1046 if (ret < 0)
1047 return ret;
1048
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001049 ret = lock_device_hotplug_sysfs();
1050 if (ret)
1051 return ret;
1052
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001053 ret = val ? device_online(dev) : device_offline(dev);
1054 unlock_device_hotplug();
1055 return ret < 0 ? ret : count;
1056}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001057static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001058
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001059int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001060{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001061 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001062}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001063EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001064
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001065void device_remove_groups(struct device *dev,
1066 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001067{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001068 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001069}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001070EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001071
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001072union device_attr_group_devres {
1073 const struct attribute_group *group;
1074 const struct attribute_group **groups;
1075};
1076
1077static int devm_attr_group_match(struct device *dev, void *res, void *data)
1078{
1079 return ((union device_attr_group_devres *)res)->group == data;
1080}
1081
1082static void devm_attr_group_remove(struct device *dev, void *res)
1083{
1084 union device_attr_group_devres *devres = res;
1085 const struct attribute_group *group = devres->group;
1086
1087 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1088 sysfs_remove_group(&dev->kobj, group);
1089}
1090
1091static void devm_attr_groups_remove(struct device *dev, void *res)
1092{
1093 union device_attr_group_devres *devres = res;
1094 const struct attribute_group **groups = devres->groups;
1095
1096 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1097 sysfs_remove_groups(&dev->kobj, groups);
1098}
1099
1100/**
1101 * devm_device_add_group - given a device, create a managed attribute group
1102 * @dev: The device to create the group for
1103 * @grp: The attribute group to create
1104 *
1105 * This function creates a group for the first time. It will explicitly
1106 * warn and error if any of the attribute files being created already exist.
1107 *
1108 * Returns 0 on success or error code on failure.
1109 */
1110int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1111{
1112 union device_attr_group_devres *devres;
1113 int error;
1114
1115 devres = devres_alloc(devm_attr_group_remove,
1116 sizeof(*devres), GFP_KERNEL);
1117 if (!devres)
1118 return -ENOMEM;
1119
1120 error = sysfs_create_group(&dev->kobj, grp);
1121 if (error) {
1122 devres_free(devres);
1123 return error;
1124 }
1125
1126 devres->group = grp;
1127 devres_add(dev, devres);
1128 return 0;
1129}
1130EXPORT_SYMBOL_GPL(devm_device_add_group);
1131
1132/**
1133 * devm_device_remove_group: remove a managed group from a device
1134 * @dev: device to remove the group from
1135 * @grp: group to remove
1136 *
1137 * This function removes a group of attributes from a device. The attributes
1138 * previously have to have been created for this group, otherwise it will fail.
1139 */
1140void devm_device_remove_group(struct device *dev,
1141 const struct attribute_group *grp)
1142{
1143 WARN_ON(devres_release(dev, devm_attr_group_remove,
1144 devm_attr_group_match,
1145 /* cast away const */ (void *)grp));
1146}
1147EXPORT_SYMBOL_GPL(devm_device_remove_group);
1148
1149/**
1150 * devm_device_add_groups - create a bunch of managed attribute groups
1151 * @dev: The device to create the group for
1152 * @groups: The attribute groups to create, NULL terminated
1153 *
1154 * This function creates a bunch of managed attribute groups. If an error
1155 * occurs when creating a group, all previously created groups will be
1156 * removed, unwinding everything back to the original state when this
1157 * function was called. It will explicitly warn and error if any of the
1158 * attribute files being created already exist.
1159 *
1160 * Returns 0 on success or error code from sysfs_create_group on failure.
1161 */
1162int devm_device_add_groups(struct device *dev,
1163 const struct attribute_group **groups)
1164{
1165 union device_attr_group_devres *devres;
1166 int error;
1167
1168 devres = devres_alloc(devm_attr_groups_remove,
1169 sizeof(*devres), GFP_KERNEL);
1170 if (!devres)
1171 return -ENOMEM;
1172
1173 error = sysfs_create_groups(&dev->kobj, groups);
1174 if (error) {
1175 devres_free(devres);
1176 return error;
1177 }
1178
1179 devres->groups = groups;
1180 devres_add(dev, devres);
1181 return 0;
1182}
1183EXPORT_SYMBOL_GPL(devm_device_add_groups);
1184
1185/**
1186 * devm_device_remove_groups - remove a list of managed groups
1187 *
1188 * @dev: The device for the groups to be removed from
1189 * @groups: NULL terminated list of groups to be removed
1190 *
1191 * If groups is not NULL, remove the specified groups from the device.
1192 */
1193void devm_device_remove_groups(struct device *dev,
1194 const struct attribute_group **groups)
1195{
1196 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1197 devm_attr_group_match,
1198 /* cast away const */ (void *)groups));
1199}
1200EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001202static int device_add_attrs(struct device *dev)
1203{
1204 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001205 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001206 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001207
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001208 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001209 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001210 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001211 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001212 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001213
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001214 if (type) {
1215 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001216 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001217 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001218 }
1219
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001220 error = device_add_groups(dev, dev->groups);
1221 if (error)
1222 goto err_remove_type_groups;
1223
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001224 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001225 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001226 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001227 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001228 }
1229
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001230 return 0;
1231
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001232 err_remove_dev_groups:
1233 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001234 err_remove_type_groups:
1235 if (type)
1236 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001237 err_remove_class_groups:
1238 if (class)
1239 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001240
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001241 return error;
1242}
1243
1244static void device_remove_attrs(struct device *dev)
1245{
1246 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001247 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001248
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001249 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001250 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001251
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001252 if (type)
1253 device_remove_groups(dev, type->groups);
1254
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001255 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001256 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001257}
1258
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001259static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001260 char *buf)
1261{
1262 return print_dev_t(buf, dev->devt);
1263}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001264static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001265
Kay Sieversca22e562011-12-14 14:29:38 -08001266/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001267struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001270 * devices_kset_move_before - Move device in the devices_kset's list.
1271 * @deva: Device to move.
1272 * @devb: Device @deva should come before.
1273 */
1274static void devices_kset_move_before(struct device *deva, struct device *devb)
1275{
1276 if (!devices_kset)
1277 return;
1278 pr_debug("devices_kset: Moving %s before %s\n",
1279 dev_name(deva), dev_name(devb));
1280 spin_lock(&devices_kset->list_lock);
1281 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1282 spin_unlock(&devices_kset->list_lock);
1283}
1284
1285/**
1286 * devices_kset_move_after - Move device in the devices_kset's list.
1287 * @deva: Device to move
1288 * @devb: Device @deva should come after.
1289 */
1290static void devices_kset_move_after(struct device *deva, struct device *devb)
1291{
1292 if (!devices_kset)
1293 return;
1294 pr_debug("devices_kset: Moving %s after %s\n",
1295 dev_name(deva), dev_name(devb));
1296 spin_lock(&devices_kset->list_lock);
1297 list_move(&deva->kobj.entry, &devb->kobj.entry);
1298 spin_unlock(&devices_kset->list_lock);
1299}
1300
1301/**
1302 * devices_kset_move_last - move the device to the end of devices_kset's list.
1303 * @dev: device to move
1304 */
1305void devices_kset_move_last(struct device *dev)
1306{
1307 if (!devices_kset)
1308 return;
1309 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1310 spin_lock(&devices_kset->list_lock);
1311 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1312 spin_unlock(&devices_kset->list_lock);
1313}
1314
1315/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001316 * device_create_file - create sysfs attribute file for device.
1317 * @dev: device.
1318 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001320int device_create_file(struct device *dev,
1321 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001324
1325 if (dev) {
1326 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001327 "Attribute %s: write permission without 'store'\n",
1328 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001329 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001330 "Attribute %s: read permission without 'show'\n",
1331 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001333 }
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return error;
1336}
David Graham White86df2682013-07-21 20:41:14 -04001337EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001340 * device_remove_file - remove sysfs attribute file.
1341 * @dev: device.
1342 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001344void device_remove_file(struct device *dev,
1345 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001347 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
David Graham White86df2682013-07-21 20:41:14 -04001350EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001352/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001353 * device_remove_file_self - remove sysfs attribute file from its own method.
1354 * @dev: device.
1355 * @attr: device attribute descriptor.
1356 *
1357 * See kernfs_remove_self() for details.
1358 */
1359bool device_remove_file_self(struct device *dev,
1360 const struct device_attribute *attr)
1361{
1362 if (dev)
1363 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1364 else
1365 return false;
1366}
1367EXPORT_SYMBOL_GPL(device_remove_file_self);
1368
1369/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001370 * device_create_bin_file - create sysfs binary attribute file for device.
1371 * @dev: device.
1372 * @attr: device binary attribute descriptor.
1373 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001374int device_create_bin_file(struct device *dev,
1375 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001376{
1377 int error = -EINVAL;
1378 if (dev)
1379 error = sysfs_create_bin_file(&dev->kobj, attr);
1380 return error;
1381}
1382EXPORT_SYMBOL_GPL(device_create_bin_file);
1383
1384/**
1385 * device_remove_bin_file - remove sysfs binary attribute file
1386 * @dev: device.
1387 * @attr: device binary attribute descriptor.
1388 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001389void device_remove_bin_file(struct device *dev,
1390 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001391{
1392 if (dev)
1393 sysfs_remove_bin_file(&dev->kobj, attr);
1394}
1395EXPORT_SYMBOL_GPL(device_remove_bin_file);
1396
James Bottomley34bb61f2005-09-06 16:56:51 -07001397static void klist_children_get(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 get_device(dev);
1403}
1404
1405static void klist_children_put(struct klist_node *n)
1406{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001407 struct device_private *p = to_device_private_parent(n);
1408 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001409
1410 put_device(dev);
1411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001414 * device_initialize - init device structure.
1415 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
Cornelia Huck57394112008-09-03 18:26:40 +02001417 * This prepares the device for use by other layers by initializing
1418 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001419 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001420 * that function, though it can also be called separately, so one
1421 * may use @dev's fields. In particular, get_device()/put_device()
1422 * may be used for reference counting of @dev after calling this
1423 * function.
1424 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001425 * All fields in @dev must be initialized by the caller to 0, except
1426 * for those explicitly set to some other value. The simplest
1427 * approach is to use kzalloc() to allocate the structure containing
1428 * @dev.
1429 *
Cornelia Huck57394112008-09-03 18:26:40 +02001430 * NOTE: Use put_device() to give up your reference instead of freeing
1431 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433void device_initialize(struct device *dev)
1434{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001435 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001436 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001438 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001439 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001440 spin_lock_init(&dev->devres_lock);
1441 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001442 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001443 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001444#ifdef CONFIG_GENERIC_MSI_IRQ
1445 INIT_LIST_HEAD(&dev->msi_list);
1446#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001447 INIT_LIST_HEAD(&dev->links.consumers);
1448 INIT_LIST_HEAD(&dev->links.suppliers);
1449 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
David Graham White86df2682013-07-21 20:41:14 -04001451EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Tejun Heod73ce002013-03-12 11:30:05 -07001453struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001454{
Kay Sievers86406242007-03-14 03:25:56 +01001455 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001456
Kay Sievers86406242007-03-14 03:25:56 +01001457 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001458 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001459 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001460
Kay Sievers86406242007-03-14 03:25:56 +01001461 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001462}
1463
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001464struct class_dir {
1465 struct kobject kobj;
1466 struct class *class;
1467};
1468
1469#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1470
1471static void class_dir_release(struct kobject *kobj)
1472{
1473 struct class_dir *dir = to_class_dir(kobj);
1474 kfree(dir);
1475}
1476
1477static const
1478struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1479{
1480 struct class_dir *dir = to_class_dir(kobj);
1481 return dir->class->ns_type;
1482}
1483
1484static struct kobj_type class_dir_ktype = {
1485 .release = class_dir_release,
1486 .sysfs_ops = &kobj_sysfs_ops,
1487 .child_ns_type = class_dir_child_ns_type
1488};
1489
1490static struct kobject *
1491class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1492{
1493 struct class_dir *dir;
1494 int retval;
1495
1496 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1497 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001498 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001499
1500 dir->class = class;
1501 kobject_init(&dir->kobj, &class_dir_ktype);
1502
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001503 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001504
1505 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1506 if (retval < 0) {
1507 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001508 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001509 }
1510 return &dir->kobj;
1511}
1512
Yijing Wange4a60d12014-11-07 12:05:49 +08001513static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001514
Kay Sieversda231fd2007-11-21 17:29:15 +01001515static struct kobject *get_device_parent(struct device *dev,
1516 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001517{
Kay Sievers86406242007-03-14 03:25:56 +01001518 if (dev->class) {
1519 struct kobject *kobj = NULL;
1520 struct kobject *parent_kobj;
1521 struct kobject *k;
1522
Randy Dunlapead454f2010-09-24 14:36:49 -07001523#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001524 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001525 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001526 if (parent && parent->class == &block_class)
1527 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001528 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001529 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001530#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001531
Kay Sievers86406242007-03-14 03:25:56 +01001532 /*
1533 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001534 * Class-devices with a non class-device as parent, live
1535 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001536 */
1537 if (parent == NULL)
1538 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001539 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001540 return &parent->kobj;
1541 else
1542 parent_kobj = &parent->kobj;
1543
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001544 mutex_lock(&gdp_mutex);
1545
Kay Sievers86406242007-03-14 03:25:56 +01001546 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001547 spin_lock(&dev->class->p->glue_dirs.list_lock);
1548 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001549 if (k->parent == parent_kobj) {
1550 kobj = kobject_get(k);
1551 break;
1552 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001553 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001554 if (kobj) {
1555 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001556 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001557 }
Kay Sievers86406242007-03-14 03:25:56 +01001558
1559 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001560 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001561 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001562 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001563 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001564 }
1565
Kay Sieversca22e562011-12-14 14:29:38 -08001566 /* subsystems can specify a default root directory for their devices */
1567 if (!parent && dev->bus && dev->bus->dev_root)
1568 return &dev->bus->dev_root->kobj;
1569
Kay Sievers86406242007-03-14 03:25:56 +01001570 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001571 return &parent->kobj;
1572 return NULL;
1573}
Kay Sieversda231fd2007-11-21 17:29:15 +01001574
Ming Leicebf8fd2016-07-10 19:27:36 +08001575static inline bool live_in_glue_dir(struct kobject *kobj,
1576 struct device *dev)
1577{
1578 if (!kobj || !dev->class ||
1579 kobj->kset != &dev->class->p->glue_dirs)
1580 return false;
1581 return true;
1582}
1583
1584static inline struct kobject *get_glue_dir(struct device *dev)
1585{
1586 return dev->kobj.parent;
1587}
1588
1589/*
1590 * make sure cleaning up dir as the last step, we need to make
1591 * sure .release handler of kobject is run with holding the
1592 * global lock
1593 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001594static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001595{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001596 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001597 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001598 return;
1599
Yijing Wange4a60d12014-11-07 12:05:49 +08001600 mutex_lock(&gdp_mutex);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001601 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001602 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001603}
Cornelia Huck63b69712008-01-21 16:09:44 +01001604
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001605static int device_add_class_symlinks(struct device *dev)
1606{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001607 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001608 int error;
1609
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001610 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001611 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001612 if (error)
1613 dev_warn(dev, "Error %d creating of_node link\n",error);
1614 /* An error here doesn't warrant bringing down the device */
1615 }
1616
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001617 if (!dev->class)
1618 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001619
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001620 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001621 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001622 "subsystem");
1623 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001624 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001625
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001626 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001627 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1628 "device");
1629 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001630 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001631 }
Kay Sievers39aba962010-09-04 22:33:14 -07001632
Randy Dunlapead454f2010-09-24 14:36:49 -07001633#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001634 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001635 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001636 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001637#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001638
1639 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001640 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001641 &dev->kobj, dev_name(dev));
1642 if (error)
1643 goto out_device;
1644
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001645 return 0;
1646
Kay Sievers39aba962010-09-04 22:33:14 -07001647out_device:
1648 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001649
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001650out_subsys:
1651 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001652out_devnode:
1653 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001654 return error;
1655}
1656
1657static void device_remove_class_symlinks(struct device *dev)
1658{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001659 if (dev_of_node(dev))
1660 sysfs_remove_link(&dev->kobj, "of_node");
1661
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001662 if (!dev->class)
1663 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001664
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001665 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001666 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001667 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001668#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001669 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001670 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001671#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001672 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001673}
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001676 * dev_set_name - set a device name
1677 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001678 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001679 */
1680int dev_set_name(struct device *dev, const char *fmt, ...)
1681{
1682 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001683 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001684
1685 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001686 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001687 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001688 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001689}
1690EXPORT_SYMBOL_GPL(dev_set_name);
1691
1692/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001693 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1694 * @dev: device
1695 *
1696 * By default we select char/ for new entries. Setting class->dev_obj
1697 * to NULL prevents an entry from being created. class->dev_kobj must
1698 * be set (or cleared) before any devices are registered to the class
1699 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001700 * device_remove_sys_dev_entry() will disagree about the presence of
1701 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001702 */
1703static struct kobject *device_to_dev_kobj(struct device *dev)
1704{
1705 struct kobject *kobj;
1706
1707 if (dev->class)
1708 kobj = dev->class->dev_kobj;
1709 else
1710 kobj = sysfs_dev_char_kobj;
1711
1712 return kobj;
1713}
1714
1715static int device_create_sys_dev_entry(struct device *dev)
1716{
1717 struct kobject *kobj = device_to_dev_kobj(dev);
1718 int error = 0;
1719 char devt_str[15];
1720
1721 if (kobj) {
1722 format_dev_t(devt_str, dev->devt);
1723 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1724 }
1725
1726 return error;
1727}
1728
1729static void device_remove_sys_dev_entry(struct device *dev)
1730{
1731 struct kobject *kobj = device_to_dev_kobj(dev);
1732 char devt_str[15];
1733
1734 if (kobj) {
1735 format_dev_t(devt_str, dev->devt);
1736 sysfs_remove_link(kobj, devt_str);
1737 }
1738}
1739
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001740int device_private_init(struct device *dev)
1741{
1742 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1743 if (!dev->p)
1744 return -ENOMEM;
1745 dev->p->device = dev;
1746 klist_init(&dev->p->klist_children, klist_children_get,
1747 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001748 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001749 return 0;
1750}
1751
Dan Williamse105b8b2008-04-21 10:51:07 -07001752/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001753 * device_add - add device to device hierarchy.
1754 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001756 * This is part 2 of device_register(), though may be called
1757 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 *
Cornelia Huck57394112008-09-03 18:26:40 +02001759 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001760 * to the global and sibling lists for the device, then
1761 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001762 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001763 * Do not call this routine or device_register() more than once for
1764 * any device structure. The driver model core is not designed to work
1765 * with devices that get unregistered and then spring back to life.
1766 * (Among other things, it's very hard to guarantee that all references
1767 * to the previous incarnation of @dev have been dropped.) Allocate
1768 * and register a fresh new struct device instead.
1769 *
Cornelia Huck57394112008-09-03 18:26:40 +02001770 * NOTE: _Never_ directly free @dev after calling this function, even
1771 * if it returned an error! Always use put_device() to give up your
1772 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
1774int device_add(struct device *dev)
1775{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301776 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001777 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001778 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001779 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001780 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001783 if (!dev)
1784 goto done;
1785
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001786 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001787 error = device_private_init(dev);
1788 if (error)
1789 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001790 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001791
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001792 /*
1793 * for statically allocated devices, which should all be converted
1794 * some day, we need to initialize the name. We prevent reading back
1795 * the name, and force the use of dev_name()
1796 */
1797 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001798 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001799 dev->init_name = NULL;
1800 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001801
Kay Sieversca22e562011-12-14 14:29:38 -08001802 /* subsystems can specify simple device enumeration */
1803 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1804 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1805
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001806 if (!dev_name(dev)) {
1807 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001808 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001811 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001814 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001815 if (IS_ERR(kobj)) {
1816 error = PTR_ERR(kobj);
1817 goto parent_error;
1818 }
Kay Sieversca22e562011-12-14 14:29:38 -08001819 if (kobj)
1820 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Yinghai Lu0d358f22008-02-19 03:20:41 -08001822 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001823 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001824 set_dev_node(dev, dev_to_node(parent));
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001827 /* we require the name to be set before, and pass NULL */
1828 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001829 if (error) {
1830 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001832 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001833
Brian Walsh37022642006-08-14 22:43:19 -07001834 /* notify platform of device entry */
1835 if (platform_notify)
1836 platform_notify(dev);
1837
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001838 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001839 if (error)
1840 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001841
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001842 error = device_add_class_symlinks(dev);
1843 if (error)
1844 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001845 error = device_add_attrs(dev);
1846 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001847 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001848 error = bus_add_device(dev);
1849 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001851 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001852 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001853 goto DPMError;
1854 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001855
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001856 if (MAJOR(dev->devt)) {
1857 error = device_create_file(dev, &dev_attr_dev);
1858 if (error)
1859 goto DevAttrError;
1860
1861 error = device_create_sys_dev_entry(dev);
1862 if (error)
1863 goto SysEntryError;
1864
1865 devtmpfs_create_node(dev);
1866 }
1867
Alan Sternec0676ee2008-12-05 14:10:31 -05001868 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001869 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001870 */
1871 if (dev->bus)
1872 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1873 BUS_NOTIFY_ADD_DEVICE, dev);
1874
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001875 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001876 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001878 klist_add_tail(&dev->p->knode_parent,
1879 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001881 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001882 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001883 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001884 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001885 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001886
1887 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001888 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001889 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001890 if (class_intf->add_dev)
1891 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001892 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001893 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001894done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 put_device(dev);
1896 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001897 SysEntryError:
1898 if (MAJOR(dev->devt))
1899 device_remove_file(dev, &dev_attr_dev);
1900 DevAttrError:
1901 device_pm_remove(dev);
1902 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001903 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001904 bus_remove_device(dev);
1905 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001906 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001907 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001908 device_remove_class_symlinks(dev);
1909 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001910 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001911 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001912 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001913 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 kobject_del(&dev->kobj);
1915 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001916 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001917parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01001918 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001919name_error:
1920 kfree(dev->p);
1921 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001922 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
David Graham White86df2682013-07-21 20:41:14 -04001924EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001927 * device_register - register a device with the system.
1928 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001930 * This happens in two clean steps - initialize the device
1931 * and add it to the system. The two steps can be called
1932 * separately, but this is the easiest and most common.
1933 * I.e. you should only call the two helpers separately if
1934 * have a clearly defined need to use and refcount the device
1935 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001936 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001937 * For more information, see the kerneldoc for device_initialize()
1938 * and device_add().
1939 *
Cornelia Huck57394112008-09-03 18:26:40 +02001940 * NOTE: _Never_ directly free @dev after calling this function, even
1941 * if it returned an error! Always use put_device() to give up the
1942 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944int device_register(struct device *dev)
1945{
1946 device_initialize(dev);
1947 return device_add(dev);
1948}
David Graham White86df2682013-07-21 20:41:14 -04001949EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001952 * get_device - increment reference count for device.
1953 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001955 * This simply forwards the call to kobject_get(), though
1956 * we do take care to provide for the case that we get a NULL
1957 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001959struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001961 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
David Graham White86df2682013-07-21 20:41:14 -04001963EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001966 * put_device - decrement reference count.
1967 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001969void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001971 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (dev)
1973 kobject_put(&dev->kobj);
1974}
David Graham White86df2682013-07-21 20:41:14 -04001975EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001978 * device_del - delete device from system.
1979 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001981 * This is the first part of the device unregistration
1982 * sequence. This removes the device from the lists we control
1983 * from here, has it removed from the other driver model
1984 * subsystems it was added to in device_add(), and removes it
1985 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001987 * NOTE: this should be called manually _iff_ device_add() was
1988 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001990void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001992 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08001993 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001994 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Alan Sternec0676ee2008-12-05 14:10:31 -05001996 /* Notify clients of device removal. This call must come
1997 * before dpm_sysfs_remove().
1998 */
1999 if (dev->bus)
2000 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2001 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002002
Alan Stern3b98aea2008-08-07 13:06:12 -04002003 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002005 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002006 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002007 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002008 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002009 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002010 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002011 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002012 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002013
Kay Sieversca22e562011-12-14 14:29:38 -08002014 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002015 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002016 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002017 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002018 if (class_intf->remove_dev)
2019 class_intf->remove_dev(dev, class_intf);
2020 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02002021 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002022 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002023 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002024 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002025 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002026 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002027 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002028 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02002029 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002030 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 /* Notify the platform of the removal, in case they
2033 * need to do anything...
2034 */
2035 if (platform_notify_remove)
2036 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02002037 if (dev->bus)
2038 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2039 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002040 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002041 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002043 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002044 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
David Graham White86df2682013-07-21 20:41:14 -04002046EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002049 * device_unregister - unregister device from system.
2050 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002052 * We do this in two parts, like we do device_register(). First,
2053 * we remove it from all the subsystems with device_del(), then
2054 * we decrement the reference count via put_device(). If that
2055 * is the final reference count, the device will be cleaned up
2056 * via device_release() above. Otherwise, the structure will
2057 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002059void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002061 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 device_del(dev);
2063 put_device(dev);
2064}
David Graham White86df2682013-07-21 20:41:14 -04002065EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002067static struct device *prev_device(struct klist_iter *i)
2068{
2069 struct klist_node *n = klist_prev(i);
2070 struct device *dev = NULL;
2071 struct device_private *p;
2072
2073 if (n) {
2074 p = to_device_private_parent(n);
2075 dev = p->device;
2076 }
2077 return dev;
2078}
2079
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002080static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002081{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002082 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002083 struct device *dev = NULL;
2084 struct device_private *p;
2085
2086 if (n) {
2087 p = to_device_private_parent(n);
2088 dev = p->device;
2089 }
2090 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002091}
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002094 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002095 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002096 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002097 * @uid: returned file owner
2098 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002099 * @tmp: possibly allocated string
2100 *
2101 * Return the relative path of a possible device node.
2102 * Non-default names may need to allocate a memory to compose
2103 * a name. This memory is returned in tmp and needs to be
2104 * freed by the caller.
2105 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002106const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002107 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002108 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002109{
2110 char *s;
2111
2112 *tmp = NULL;
2113
2114 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002115 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002116 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002117 if (*tmp)
2118 return *tmp;
2119
2120 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002121 if (dev->class && dev->class->devnode)
2122 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002123 if (*tmp)
2124 return *tmp;
2125
2126 /* return name without allocation, tmp == NULL */
2127 if (strchr(dev_name(dev), '!') == NULL)
2128 return dev_name(dev);
2129
2130 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002131 s = kstrdup(dev_name(dev), GFP_KERNEL);
2132 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002133 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002134 strreplace(s, '!', '/');
2135 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002136}
2137
2138/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002139 * device_for_each_child - device child iterator.
2140 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002141 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002142 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002144 * Iterate over @parent's child devices, and call @fn for each,
2145 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002147 * We check the return of @fn each time. If it returns anything
2148 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002150int device_for_each_child(struct device *parent, void *data,
2151 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002153 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002154 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int error = 0;
2156
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002157 if (!parent->p)
2158 return 0;
2159
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002160 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002161 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002162 error = fn(child, data);
2163 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return error;
2165}
David Graham White86df2682013-07-21 20:41:14 -04002166EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Cornelia Huck5ab69982006-11-16 15:42:07 +01002168/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002169 * device_for_each_child_reverse - device child iterator in reversed order.
2170 * @parent: parent struct device.
2171 * @fn: function to be called for each device.
2172 * @data: data for the callback.
2173 *
2174 * Iterate over @parent's child devices, and call @fn for each,
2175 * passing it @data.
2176 *
2177 * We check the return of @fn each time. If it returns anything
2178 * other than 0, we break out and return that value.
2179 */
2180int device_for_each_child_reverse(struct device *parent, void *data,
2181 int (*fn)(struct device *dev, void *data))
2182{
2183 struct klist_iter i;
2184 struct device *child;
2185 int error = 0;
2186
2187 if (!parent->p)
2188 return 0;
2189
2190 klist_iter_init(&parent->p->klist_children, &i);
2191 while ((child = prev_device(&i)) && !error)
2192 error = fn(child, data);
2193 klist_iter_exit(&i);
2194 return error;
2195}
2196EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2197
2198/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002199 * device_find_child - device iterator for locating a particular device.
2200 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002201 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002202 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002203 *
2204 * This is similar to the device_for_each_child() function above, but it
2205 * returns a reference to a device that is 'found' for later use, as
2206 * determined by the @match callback.
2207 *
2208 * The callback should return 0 if the device doesn't match and non-zero
2209 * if it does. If the callback returns non-zero and a reference to the
2210 * current device can be obtained, this function will return to the caller
2211 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002212 *
2213 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002214 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002215struct device *device_find_child(struct device *parent, void *data,
2216 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002217{
2218 struct klist_iter i;
2219 struct device *child;
2220
2221 if (!parent)
2222 return NULL;
2223
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002224 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002225 while ((child = next_device(&i)))
2226 if (match(child, data) && get_device(child))
2227 break;
2228 klist_iter_exit(&i);
2229 return child;
2230}
David Graham White86df2682013-07-21 20:41:14 -04002231EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233int __init devices_init(void)
2234{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002235 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2236 if (!devices_kset)
2237 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002238 dev_kobj = kobject_create_and_add("dev", NULL);
2239 if (!dev_kobj)
2240 goto dev_kobj_err;
2241 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2242 if (!sysfs_dev_block_kobj)
2243 goto block_kobj_err;
2244 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2245 if (!sysfs_dev_char_kobj)
2246 goto char_kobj_err;
2247
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002248 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002249
2250 char_kobj_err:
2251 kobject_put(sysfs_dev_block_kobj);
2252 block_kobj_err:
2253 kobject_put(dev_kobj);
2254 dev_kobj_err:
2255 kset_unregister(devices_kset);
2256 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002259static int device_check_offline(struct device *dev, void *not_used)
2260{
2261 int ret;
2262
2263 ret = device_for_each_child(dev, NULL, device_check_offline);
2264 if (ret)
2265 return ret;
2266
2267 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2268}
2269
2270/**
2271 * device_offline - Prepare the device for hot-removal.
2272 * @dev: Device to be put offline.
2273 *
2274 * Execute the device bus type's .offline() callback, if present, to prepare
2275 * the device for a subsequent hot-removal. If that succeeds, the device must
2276 * not be used until either it is removed or its bus type's .online() callback
2277 * is executed.
2278 *
2279 * Call under device_hotplug_lock.
2280 */
2281int device_offline(struct device *dev)
2282{
2283 int ret;
2284
2285 if (dev->offline_disabled)
2286 return -EPERM;
2287
2288 ret = device_for_each_child(dev, NULL, device_check_offline);
2289 if (ret)
2290 return ret;
2291
2292 device_lock(dev);
2293 if (device_supports_offline(dev)) {
2294 if (dev->offline) {
2295 ret = 1;
2296 } else {
2297 ret = dev->bus->offline(dev);
2298 if (!ret) {
2299 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2300 dev->offline = true;
2301 }
2302 }
2303 }
2304 device_unlock(dev);
2305
2306 return ret;
2307}
2308
2309/**
2310 * device_online - Put the device back online after successful device_offline().
2311 * @dev: Device to be put back online.
2312 *
2313 * If device_offline() has been successfully executed for @dev, but the device
2314 * has not been removed subsequently, execute its bus type's .online() callback
2315 * to indicate that the device can be used again.
2316 *
2317 * Call under device_hotplug_lock.
2318 */
2319int device_online(struct device *dev)
2320{
2321 int ret = 0;
2322
2323 device_lock(dev);
2324 if (device_supports_offline(dev)) {
2325 if (dev->offline) {
2326 ret = dev->bus->online(dev);
2327 if (!ret) {
2328 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2329 dev->offline = false;
2330 }
2331 } else {
2332 ret = 1;
2333 }
2334 }
2335 device_unlock(dev);
2336
2337 return ret;
2338}
2339
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002340struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002341 struct device dev;
2342 struct module *owner;
2343};
2344
Josh Triplett93058422012-11-18 21:27:55 -08002345static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002346{
2347 return container_of(d, struct root_device, dev);
2348}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002349
2350static void root_device_release(struct device *dev)
2351{
2352 kfree(to_root_device(dev));
2353}
2354
2355/**
2356 * __root_device_register - allocate and register a root device
2357 * @name: root device name
2358 * @owner: owner module of the root device, usually THIS_MODULE
2359 *
2360 * This function allocates a root device and registers it
2361 * using device_register(). In order to free the returned
2362 * device, use root_device_unregister().
2363 *
2364 * Root devices are dummy devices which allow other devices
2365 * to be grouped under /sys/devices. Use this function to
2366 * allocate a root device and then use it as the parent of
2367 * any device which should appear under /sys/devices/{name}
2368 *
2369 * The /sys/devices/{name} directory will also contain a
2370 * 'module' symlink which points to the @owner directory
2371 * in sysfs.
2372 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002373 * Returns &struct device pointer on success, or ERR_PTR() on error.
2374 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002375 * Note: You probably want to use root_device_register().
2376 */
2377struct device *__root_device_register(const char *name, struct module *owner)
2378{
2379 struct root_device *root;
2380 int err = -ENOMEM;
2381
2382 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2383 if (!root)
2384 return ERR_PTR(err);
2385
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002386 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002387 if (err) {
2388 kfree(root);
2389 return ERR_PTR(err);
2390 }
2391
2392 root->dev.release = root_device_release;
2393
2394 err = device_register(&root->dev);
2395 if (err) {
2396 put_device(&root->dev);
2397 return ERR_PTR(err);
2398 }
2399
Christoph Egger1d9e8822010-05-17 16:57:58 +02002400#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002401 if (owner) {
2402 struct module_kobject *mk = &owner->mkobj;
2403
2404 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2405 if (err) {
2406 device_unregister(&root->dev);
2407 return ERR_PTR(err);
2408 }
2409 root->owner = owner;
2410 }
2411#endif
2412
2413 return &root->dev;
2414}
2415EXPORT_SYMBOL_GPL(__root_device_register);
2416
2417/**
2418 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002419 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002420 *
2421 * This function unregisters and cleans up a device that was created by
2422 * root_device_register().
2423 */
2424void root_device_unregister(struct device *dev)
2425{
2426 struct root_device *root = to_root_device(dev);
2427
2428 if (root->owner)
2429 sysfs_remove_link(&root->dev.kobj, "module");
2430
2431 device_unregister(dev);
2432}
2433EXPORT_SYMBOL_GPL(root_device_unregister);
2434
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002435
2436static void device_create_release(struct device *dev)
2437{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002438 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002439 kfree(dev);
2440}
2441
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002442static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002443device_create_groups_vargs(struct class *class, struct device *parent,
2444 dev_t devt, void *drvdata,
2445 const struct attribute_group **groups,
2446 const char *fmt, va_list args)
2447{
2448 struct device *dev = NULL;
2449 int retval = -ENODEV;
2450
2451 if (class == NULL || IS_ERR(class))
2452 goto error;
2453
2454 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2455 if (!dev) {
2456 retval = -ENOMEM;
2457 goto error;
2458 }
2459
David Herrmannbbc780f2013-11-21 20:15:48 +01002460 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002461 dev->devt = devt;
2462 dev->class = class;
2463 dev->parent = parent;
2464 dev->groups = groups;
2465 dev->release = device_create_release;
2466 dev_set_drvdata(dev, drvdata);
2467
2468 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2469 if (retval)
2470 goto error;
2471
David Herrmannbbc780f2013-11-21 20:15:48 +01002472 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002473 if (retval)
2474 goto error;
2475
2476 return dev;
2477
2478error:
2479 put_device(dev);
2480 return ERR_PTR(retval);
2481}
2482
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002483/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002484 * device_create_vargs - creates a device and registers it with sysfs
2485 * @class: pointer to the struct class that this device should be registered to
2486 * @parent: pointer to the parent struct device of this new device, if any
2487 * @devt: the dev_t for the char device to be added
2488 * @drvdata: the data to be added to the device for callbacks
2489 * @fmt: string for the device's name
2490 * @args: va_list for the device's name
2491 *
2492 * This function can be used by char device classes. A struct device
2493 * will be created in sysfs, registered to the specified class.
2494 *
2495 * A "dev" file will be created, showing the dev_t for the device, if
2496 * the dev_t is not 0,0.
2497 * If a pointer to a parent struct device is passed in, the newly created
2498 * struct device will be a child of that device in sysfs.
2499 * The pointer to the struct device will be returned from the call.
2500 * Any further sysfs files that might be required can be created using this
2501 * pointer.
2502 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002503 * Returns &struct device pointer on success, or ERR_PTR() on error.
2504 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002505 * Note: the struct class passed to this function must have previously
2506 * been created with a call to class_create().
2507 */
2508struct device *device_create_vargs(struct class *class, struct device *parent,
2509 dev_t devt, void *drvdata, const char *fmt,
2510 va_list args)
2511{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002512 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2513 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002514}
2515EXPORT_SYMBOL_GPL(device_create_vargs);
2516
2517/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002518 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002519 * @class: pointer to the struct class that this device should be registered to
2520 * @parent: pointer to the parent struct device of this new device, if any
2521 * @devt: the dev_t for the char device to be added
2522 * @drvdata: the data to be added to the device for callbacks
2523 * @fmt: string for the device's name
2524 *
2525 * This function can be used by char device classes. A struct device
2526 * will be created in sysfs, registered to the specified class.
2527 *
2528 * A "dev" file will be created, showing the dev_t for the device, if
2529 * the dev_t is not 0,0.
2530 * If a pointer to a parent struct device is passed in, the newly created
2531 * struct device will be a child of that device in sysfs.
2532 * The pointer to the struct device will be returned from the call.
2533 * Any further sysfs files that might be required can be created using this
2534 * pointer.
2535 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002536 * Returns &struct device pointer on success, or ERR_PTR() on error.
2537 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002538 * Note: the struct class passed to this function must have previously
2539 * been created with a call to class_create().
2540 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002541struct device *device_create(struct class *class, struct device *parent,
2542 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002543{
2544 va_list vargs;
2545 struct device *dev;
2546
2547 va_start(vargs, fmt);
2548 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2549 va_end(vargs);
2550 return dev;
2551}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002552EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002553
Guenter Roeck39ef3112013-07-14 16:05:57 -07002554/**
2555 * device_create_with_groups - creates a device and registers it with sysfs
2556 * @class: pointer to the struct class that this device should be registered to
2557 * @parent: pointer to the parent struct device of this new device, if any
2558 * @devt: the dev_t for the char device to be added
2559 * @drvdata: the data to be added to the device for callbacks
2560 * @groups: NULL-terminated list of attribute groups to be created
2561 * @fmt: string for the device's name
2562 *
2563 * This function can be used by char device classes. A struct device
2564 * will be created in sysfs, registered to the specified class.
2565 * Additional attributes specified in the groups parameter will also
2566 * be created automatically.
2567 *
2568 * A "dev" file will be created, showing the dev_t for the device, if
2569 * the dev_t is not 0,0.
2570 * If a pointer to a parent struct device is passed in, the newly created
2571 * struct device will be a child of that device in sysfs.
2572 * The pointer to the struct device will be returned from the call.
2573 * Any further sysfs files that might be required can be created using this
2574 * pointer.
2575 *
2576 * Returns &struct device pointer on success, or ERR_PTR() on error.
2577 *
2578 * Note: the struct class passed to this function must have previously
2579 * been created with a call to class_create().
2580 */
2581struct device *device_create_with_groups(struct class *class,
2582 struct device *parent, dev_t devt,
2583 void *drvdata,
2584 const struct attribute_group **groups,
2585 const char *fmt, ...)
2586{
2587 va_list vargs;
2588 struct device *dev;
2589
2590 va_start(vargs, fmt);
2591 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2592 fmt, vargs);
2593 va_end(vargs);
2594 return dev;
2595}
2596EXPORT_SYMBOL_GPL(device_create_with_groups);
2597
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002598static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002599{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002600 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002601
Dave Youngcd354492008-01-28 16:56:11 +08002602 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002603}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002604
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002605/**
2606 * device_destroy - removes a device that was created with device_create()
2607 * @class: pointer to the struct class that this device was registered with
2608 * @devt: the dev_t of the device that was previously registered
2609 *
2610 * This call unregisters and cleans up a device that was created with a
2611 * call to device_create().
2612 */
2613void device_destroy(struct class *class, dev_t devt)
2614{
2615 struct device *dev;
2616
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002617 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002618 if (dev) {
2619 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002620 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002621 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002622}
2623EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002624
2625/**
2626 * device_rename - renames a device
2627 * @dev: the pointer to the struct device to be renamed
2628 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002629 *
2630 * It is the responsibility of the caller to provide mutual
2631 * exclusion between two different calls of device_rename
2632 * on the same device to ensure that new_name is valid and
2633 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002634 *
Timur Tabia5462512010-12-13 14:08:52 -06002635 * Note: Don't call this function. Currently, the networking layer calls this
2636 * function, but that will change. The following text from Kay Sievers offers
2637 * some insight:
2638 *
2639 * Renaming devices is racy at many levels, symlinks and other stuff are not
2640 * replaced atomically, and you get a "move" uevent, but it's not easy to
2641 * connect the event to the old and new device. Device nodes are not renamed at
2642 * all, there isn't even support for that in the kernel now.
2643 *
2644 * In the meantime, during renaming, your target name might be taken by another
2645 * driver, creating conflicts. Or the old name is taken directly after you
2646 * renamed it -- then you get events for the same DEVPATH, before you even see
2647 * the "move" event. It's just a mess, and nothing new should ever rely on
2648 * kernel device renaming. Besides that, it's not even implemented now for
2649 * other things than (driver-core wise very simple) network devices.
2650 *
2651 * We are currently about to change network renaming in udev to completely
2652 * disallow renaming of devices in the same namespace as the kernel uses,
2653 * because we can't solve the problems properly, that arise with swapping names
2654 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2655 * be allowed to some other name than eth[0-9]*, for the aforementioned
2656 * reasons.
2657 *
2658 * Make up a "real" name in the driver before you register anything, or add
2659 * some other attributes for userspace to find the device, or use udev to add
2660 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2661 * don't even want to get into that and try to implement the missing pieces in
2662 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002663 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002664int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002665{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002666 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002667 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002668 int error;
2669
2670 dev = get_device(dev);
2671 if (!dev)
2672 return -EINVAL;
2673
ethan.zhao69df7532013-10-13 22:12:35 +08002674 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002675
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002676 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002677 if (!old_device_name) {
2678 error = -ENOMEM;
2679 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002680 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002681
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002682 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002683 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2684 kobj, old_device_name,
2685 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002686 if (error)
2687 goto out;
2688 }
Kay Sievers39aba962010-09-04 22:33:14 -07002689
Tejun Heo4b30ee52013-09-11 22:29:06 -04002690 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002691 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002692 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002693
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002694out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002695 put_device(dev);
2696
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002697 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002698
2699 return error;
2700}
Johannes Berga2807db2007-02-28 12:38:31 +01002701EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002702
2703static int device_move_class_links(struct device *dev,
2704 struct device *old_parent,
2705 struct device *new_parent)
2706{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002707 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002708
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002709 if (old_parent)
2710 sysfs_remove_link(&dev->kobj, "device");
2711 if (new_parent)
2712 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2713 "device");
2714 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002715}
2716
2717/**
2718 * device_move - moves a device to a new parent
2719 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02002720 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002721 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002722 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002723int device_move(struct device *dev, struct device *new_parent,
2724 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002725{
2726 int error;
2727 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002728 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002729
2730 dev = get_device(dev);
2731 if (!dev)
2732 return -EINVAL;
2733
Cornelia Huckffa6a702009-03-04 12:44:00 +01002734 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002735 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002736 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002737 if (IS_ERR(new_parent_kobj)) {
2738 error = PTR_ERR(new_parent_kobj);
2739 put_device(new_parent);
2740 goto out;
2741 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002742
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002743 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2744 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002745 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002746 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002747 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002748 put_device(new_parent);
2749 goto out;
2750 }
2751 old_parent = dev->parent;
2752 dev->parent = new_parent;
2753 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002754 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002755 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002756 klist_add_tail(&dev->p->knode_parent,
2757 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002758 set_dev_node(dev, dev_to_node(new_parent));
2759 }
2760
Rabin Vincentbdd40342012-04-23 09:16:36 +02002761 if (dev->class) {
2762 error = device_move_class_links(dev, old_parent, new_parent);
2763 if (error) {
2764 /* We ignore errors on cleanup since we're hosed anyway... */
2765 device_move_class_links(dev, new_parent, old_parent);
2766 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2767 if (new_parent)
2768 klist_remove(&dev->p->knode_parent);
2769 dev->parent = old_parent;
2770 if (old_parent) {
2771 klist_add_tail(&dev->p->knode_parent,
2772 &old_parent->p->klist_children);
2773 set_dev_node(dev, dev_to_node(old_parent));
2774 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002775 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002776 cleanup_glue_dir(dev, new_parent_kobj);
2777 put_device(new_parent);
2778 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002779 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002780 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002781 switch (dpm_order) {
2782 case DPM_ORDER_NONE:
2783 break;
2784 case DPM_ORDER_DEV_AFTER_PARENT:
2785 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002786 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002787 break;
2788 case DPM_ORDER_PARENT_BEFORE_DEV:
2789 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002790 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002791 break;
2792 case DPM_ORDER_DEV_LAST:
2793 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002794 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002795 break;
2796 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002797
Cornelia Huck8a824722006-11-20 17:07:51 +01002798 put_device(old_parent);
2799out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002800 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002801 put_device(dev);
2802 return error;
2803}
Cornelia Huck8a824722006-11-20 17:07:51 +01002804EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002805
2806/**
2807 * device_shutdown - call ->shutdown() on each device to shutdown.
2808 */
2809void device_shutdown(void)
2810{
Benson Leungf123db82013-09-24 20:05:11 -07002811 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002812
Hugh Daschbach62458382010-03-22 10:36:37 -07002813 spin_lock(&devices_kset->list_lock);
2814 /*
2815 * Walk the devices list backward, shutting down each in turn.
2816 * Beware that device unplug events may also start pulling
2817 * devices offline, even as the system is shutting down.
2818 */
2819 while (!list_empty(&devices_kset->list)) {
2820 dev = list_entry(devices_kset->list.prev, struct device,
2821 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002822
2823 /*
2824 * hold reference count of device's parent to
2825 * prevent it from being freed because parent's
2826 * lock is to be held
2827 */
Benson Leungf123db82013-09-24 20:05:11 -07002828 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002829 get_device(dev);
2830 /*
2831 * Make sure the device is off the kset list, in the
2832 * event that dev->*->shutdown() doesn't remove it.
2833 */
2834 list_del_init(&dev->kobj.entry);
2835 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002836
Ming Leid1c6c032012-06-22 18:01:40 +08002837 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002838 if (parent)
2839 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002840 device_lock(dev);
2841
Alan Sternfe6b91f2011-12-06 23:24:52 +01002842 /* Don't allow any more runtime suspends */
2843 pm_runtime_get_noresume(dev);
2844 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002845
Michal Suchanek75216212017-08-11 15:44:43 +02002846 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07002847 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02002848 dev_info(dev, "shutdown_pre\n");
2849 dev->class->shutdown_pre(dev);
2850 }
2851 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002852 if (initcall_debug)
2853 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002854 dev->bus->shutdown(dev);
2855 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002856 if (initcall_debug)
2857 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002858 dev->driver->shutdown(dev);
2859 }
Ming Leid1c6c032012-06-22 18:01:40 +08002860
2861 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002862 if (parent)
2863 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002864
Hugh Daschbach62458382010-03-22 10:36:37 -07002865 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002866 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002867
2868 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002869 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002870 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002871}
Joe Perches99bcf212010-06-27 01:02:34 +00002872
2873/*
2874 * Device logging functions
2875 */
2876
2877#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002878static int
2879create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002880{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002881 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002882 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002883
Kay Sieversc4e00da2012-05-03 02:29:59 +02002884 if (dev->class)
2885 subsys = dev->class->name;
2886 else if (dev->bus)
2887 subsys = dev->bus->name;
2888 else
Joe Perches798efc62012-09-12 20:11:29 -07002889 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002890
Joe Perches798efc62012-09-12 20:11:29 -07002891 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002892 if (pos >= hdrlen)
2893 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002894
2895 /*
2896 * Add device identifier DEVICE=:
2897 * b12:8 block dev_t
2898 * c127:3 char dev_t
2899 * n8 netdev ifindex
2900 * +sound:card0 subsystem:devname
2901 */
2902 if (MAJOR(dev->devt)) {
2903 char c;
2904
2905 if (strcmp(subsys, "block") == 0)
2906 c = 'b';
2907 else
2908 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002909 pos++;
2910 pos += snprintf(hdr + pos, hdrlen - pos,
2911 "DEVICE=%c%u:%u",
2912 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002913 } else if (strcmp(subsys, "net") == 0) {
2914 struct net_device *net = to_net_dev(dev);
2915
Joe Perches798efc62012-09-12 20:11:29 -07002916 pos++;
2917 pos += snprintf(hdr + pos, hdrlen - pos,
2918 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002919 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002920 pos++;
2921 pos += snprintf(hdr + pos, hdrlen - pos,
2922 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002923 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002924
Ben Hutchings655e5b72014-08-26 00:34:44 -07002925 if (pos >= hdrlen)
2926 goto overflow;
2927
Joe Perches798efc62012-09-12 20:11:29 -07002928 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002929
2930overflow:
2931 dev_WARN(dev, "device/subsystem name too long");
2932 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002933}
Joe Perches798efc62012-09-12 20:11:29 -07002934
Joe Perches05e4e5b2012-09-12 20:13:37 -07002935int dev_vprintk_emit(int level, const struct device *dev,
2936 const char *fmt, va_list args)
2937{
2938 char hdr[128];
2939 size_t hdrlen;
2940
2941 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2942
2943 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2944}
2945EXPORT_SYMBOL(dev_vprintk_emit);
2946
2947int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2948{
2949 va_list args;
2950 int r;
2951
2952 va_start(args, fmt);
2953
2954 r = dev_vprintk_emit(level, dev, fmt, args);
2955
2956 va_end(args);
2957
2958 return r;
2959}
2960EXPORT_SYMBOL(dev_printk_emit);
2961
Joe Perchesd1f10522014-12-25 15:07:04 -08002962static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07002963 struct va_format *vaf)
2964{
Joe Perchesd1f10522014-12-25 15:07:04 -08002965 if (dev)
2966 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2967 dev_driver_string(dev), dev_name(dev), vaf);
2968 else
2969 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002970}
Joe Perches99bcf212010-06-27 01:02:34 +00002971
Joe Perchesd1f10522014-12-25 15:07:04 -08002972void dev_printk(const char *level, const struct device *dev,
2973 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00002974{
2975 struct va_format vaf;
2976 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00002977
2978 va_start(args, fmt);
2979
2980 vaf.fmt = fmt;
2981 vaf.va = &args;
2982
Joe Perchesd1f10522014-12-25 15:07:04 -08002983 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002984
Joe Perches99bcf212010-06-27 01:02:34 +00002985 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00002986}
2987EXPORT_SYMBOL(dev_printk);
2988
2989#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08002990void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00002991{ \
2992 struct va_format vaf; \
2993 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00002994 \
2995 va_start(args, fmt); \
2996 \
2997 vaf.fmt = fmt; \
2998 vaf.va = &args; \
2999 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003000 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003001 \
Joe Perches99bcf212010-06-27 01:02:34 +00003002 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003003} \
3004EXPORT_SYMBOL(func);
3005
3006define_dev_printk_level(dev_emerg, KERN_EMERG);
3007define_dev_printk_level(dev_alert, KERN_ALERT);
3008define_dev_printk_level(dev_crit, KERN_CRIT);
3009define_dev_printk_level(dev_err, KERN_ERR);
3010define_dev_printk_level(dev_warn, KERN_WARNING);
3011define_dev_printk_level(dev_notice, KERN_NOTICE);
3012define_dev_printk_level(_dev_info, KERN_INFO);
3013
3014#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003015
3016static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3017{
3018 return fwnode && !IS_ERR(fwnode->secondary);
3019}
3020
3021/**
3022 * set_primary_fwnode - Change the primary firmware node of a given device.
3023 * @dev: Device to handle.
3024 * @fwnode: New primary firmware node of the device.
3025 *
3026 * Set the device's firmware node pointer to @fwnode, but if a secondary
3027 * firmware node of the device is present, preserve it.
3028 */
3029void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3030{
3031 if (fwnode) {
3032 struct fwnode_handle *fn = dev->fwnode;
3033
3034 if (fwnode_is_primary(fn))
3035 fn = fn->secondary;
3036
Mika Westerberg55f89a82015-11-30 17:11:39 +02003037 if (fn) {
3038 WARN_ON(fwnode->secondary);
3039 fwnode->secondary = fn;
3040 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003041 dev->fwnode = fwnode;
3042 } else {
3043 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3044 dev->fwnode->secondary : NULL;
3045 }
3046}
3047EXPORT_SYMBOL_GPL(set_primary_fwnode);
3048
3049/**
3050 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3051 * @dev: Device to handle.
3052 * @fwnode: New secondary firmware node of the device.
3053 *
3054 * If a primary firmware node of the device is present, set its secondary
3055 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3056 * @fwnode.
3057 */
3058void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3059{
3060 if (fwnode)
3061 fwnode->secondary = ERR_PTR(-ENODEV);
3062
3063 if (fwnode_is_primary(dev->fwnode))
3064 dev->fwnode->secondary = fwnode;
3065 else
3066 dev->fwnode = fwnode;
3067}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003068
3069/**
3070 * device_set_of_node_from_dev - reuse device-tree node of another device
3071 * @dev: device whose device-tree node is being set
3072 * @dev2: device whose device-tree node is being reused
3073 *
3074 * Takes another reference to the new device-tree node after first dropping
3075 * any reference held to the old node.
3076 */
3077void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3078{
3079 of_node_put(dev->of_node);
3080 dev->of_node = of_node_get(dev2->of_node);
3081 dev->of_node_reused = true;
3082}
3083EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);