blob: a4d6f9b93088dc18d92bc04cfff4cf6618f8c5d1 [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
Heikki Krogerus7847a142018-11-09 17:21:35 +030011#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/device.h>
13#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020014#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010023#include <linux/genhd.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070024#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010025#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020026#include <linux/netdevice.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010027#include <linux/sched/signal.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070028#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "base.h"
31#include "power/power.h"
32
Andi Kleene52eec12010-09-08 16:54:17 +020033#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080039static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020040{
Jingoo Han34da5e62013-07-26 13:10:22 +090041 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020042}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010046/* Device links support. */
47
48#ifdef CONFIG_SRCU
49static DEFINE_MUTEX(device_links_lock);
50DEFINE_STATIC_SRCU(device_links_srcu);
51
52static inline void device_links_write_lock(void)
53{
54 mutex_lock(&device_links_lock);
55}
56
57static inline void device_links_write_unlock(void)
58{
59 mutex_unlock(&device_links_lock);
60}
61
62int device_links_read_lock(void)
63{
64 return srcu_read_lock(&device_links_srcu);
65}
66
67void device_links_read_unlock(int idx)
68{
69 srcu_read_unlock(&device_links_srcu, idx);
70}
71#else /* !CONFIG_SRCU */
72static DECLARE_RWSEM(device_links_lock);
73
74static inline void device_links_write_lock(void)
75{
76 down_write(&device_links_lock);
77}
78
79static inline void device_links_write_unlock(void)
80{
81 up_write(&device_links_lock);
82}
83
84int device_links_read_lock(void)
85{
86 down_read(&device_links_lock);
87 return 0;
88}
89
90void device_links_read_unlock(int not_used)
91{
92 up_read(&device_links_lock);
93}
94#endif /* !CONFIG_SRCU */
95
96/**
97 * device_is_dependent - Check if one device depends on another one
98 * @dev: Device to check dependencies for.
99 * @target: Device to check against.
100 *
101 * Check if @target depends on @dev or any device dependent on it (its child or
102 * its consumer etc). Return 1 if that is the case or 0 otherwise.
103 */
104static int device_is_dependent(struct device *dev, void *target)
105{
106 struct device_link *link;
107 int ret;
108
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200109 if (dev == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100110 return 1;
111
112 ret = device_for_each_child(dev, target, device_is_dependent);
113 if (ret)
114 return ret;
115
116 list_for_each_entry(link, &dev->links.consumers, s_node) {
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200117 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100118 return 1;
119
120 ret = device_is_dependent(link->consumer, target);
121 if (ret)
122 break;
123 }
124 return ret;
125}
126
127static int device_reorder_to_tail(struct device *dev, void *not_used)
128{
129 struct device_link *link;
130
131 /*
132 * Devices that have not been registered yet will be put to the ends
133 * of the lists during the registration, so skip them here.
134 */
135 if (device_is_registered(dev))
136 devices_kset_move_last(dev);
137
138 if (device_pm_initialized(dev))
139 device_pm_move_last(dev);
140
141 device_for_each_child(dev, NULL, device_reorder_to_tail);
142 list_for_each_entry(link, &dev->links.consumers, s_node)
143 device_reorder_to_tail(link->consumer, NULL);
144
145 return 0;
146}
147
148/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700149 * device_pm_move_to_tail - Move set of devices to the end of device lists
150 * @dev: Device to move
151 *
152 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
153 *
154 * It moves the @dev along with all of its children and all of its consumers
155 * to the ends of the device_kset and dpm_list, recursively.
156 */
157void device_pm_move_to_tail(struct device *dev)
158{
159 int idx;
160
161 idx = device_links_read_lock();
162 device_pm_lock();
163 device_reorder_to_tail(dev, NULL);
164 device_pm_unlock();
165 device_links_read_unlock(idx);
166}
167
168/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100169 * device_link_add - Create a link between two devices.
170 * @consumer: Consumer end of the link.
171 * @supplier: Supplier end of the link.
172 * @flags: Link flags.
173 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100174 * The caller is responsible for the proper synchronization of the link creation
175 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
176 * runtime PM framework to take the link into account. Second, if the
177 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
178 * be forced into the active metastate and reference-counted upon the creation
179 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
180 * ignored.
181 *
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100182 * If the DL_FLAG_AUTOREMOVE_CONSUMER flag is set, the link will be removed
183 * automatically when the consumer device driver unbinds from it. Analogously,
184 * if DL_FLAG_AUTOREMOVE_SUPPLIER is set in @flags, the link will be removed
185 * automatically when the supplier device driver unbinds from it.
186 *
187 * The combination of DL_FLAG_STATELESS and either DL_FLAG_AUTOREMOVE_CONSUMER
188 * or DL_FLAG_AUTOREMOVE_SUPPLIER set in @flags at the same time is invalid and
189 * will cause NULL to be returned upfront.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100190 *
191 * A side effect of the link creation is re-ordering of dpm_list and the
192 * devices_kset list by moving the consumer device and all devices depending
193 * on it to the ends of these lists (that does not happen to devices that have
194 * not been registered when this function is called).
195 *
196 * The supplier device is required to be registered when this function is called
197 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100198 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100199 */
200struct device_link *device_link_add(struct device *consumer,
201 struct device *supplier, u32 flags)
202{
203 struct device_link *link;
204
205 if (!consumer || !supplier ||
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100206 (flags & DL_FLAG_STATELESS &&
207 flags & (DL_FLAG_AUTOREMOVE_CONSUMER | DL_FLAG_AUTOREMOVE_SUPPLIER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100208 return NULL;
209
210 device_links_write_lock();
211 device_pm_lock();
212
213 /*
214 * If the supplier has not been fully registered yet or there is a
215 * reverse dependency between the consumer and the supplier already in
216 * the graph, return NULL.
217 */
218 if (!device_pm_initialized(supplier)
219 || device_is_dependent(consumer, supplier)) {
220 link = NULL;
221 goto out;
222 }
223
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100224 list_for_each_entry(link, &supplier->links.consumers, s_node) {
225 if (link->consumer != consumer)
226 continue;
227
228 /*
229 * Don't return a stateless link if the caller wants a stateful
230 * one and vice versa.
231 */
232 if (WARN_ON((flags & DL_FLAG_STATELESS) != (link->flags & DL_FLAG_STATELESS))) {
233 link = NULL;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100234 goto out;
Lukas Wunneread18c22018-02-10 19:27:12 +0100235 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100236
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100237 if (flags & DL_FLAG_AUTOREMOVE_CONSUMER)
238 link->flags |= DL_FLAG_AUTOREMOVE_CONSUMER;
239
240 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
241 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
242
243 kref_get(&link->kref);
244 goto out;
245 }
246
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100247 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100248 if (!link)
249 goto out;
250
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100251 if (flags & DL_FLAG_PM_RUNTIME) {
252 if (flags & DL_FLAG_RPM_ACTIVE) {
253 if (pm_runtime_get_sync(supplier) < 0) {
254 pm_runtime_put_noidle(supplier);
255 kfree(link);
256 link = NULL;
257 goto out;
258 }
259 link->rpm_active = true;
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100260 }
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100261 pm_runtime_new_link(consumer);
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200262 /*
263 * If the link is being added by the consumer driver at probe
264 * time, balance the decrementation of the supplier's runtime PM
265 * usage counter after consumer probe in driver_probe_device().
266 */
267 if (consumer->links.status == DL_DEV_PROBING)
268 pm_runtime_get_noresume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100269 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100270 get_device(supplier);
271 link->supplier = supplier;
272 INIT_LIST_HEAD(&link->s_node);
273 get_device(consumer);
274 link->consumer = consumer;
275 INIT_LIST_HEAD(&link->c_node);
276 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100277 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100278
Lukas Wunner64df1142016-12-04 13:10:04 +0100279 /* Determine the initial link state. */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100280 if (flags & DL_FLAG_STATELESS) {
281 link->status = DL_STATE_NONE;
282 } else {
283 switch (supplier->links.status) {
284 case DL_DEV_DRIVER_BOUND:
285 switch (consumer->links.status) {
286 case DL_DEV_PROBING:
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100287 /*
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200288 * Some callers expect the link creation during
289 * consumer driver probe to resume the supplier
290 * even without DL_FLAG_RPM_ACTIVE.
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100291 */
292 if (flags & DL_FLAG_PM_RUNTIME)
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200293 pm_runtime_resume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100294
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100295 link->status = DL_STATE_CONSUMER_PROBE;
296 break;
297 case DL_DEV_DRIVER_BOUND:
298 link->status = DL_STATE_ACTIVE;
299 break;
300 default:
301 link->status = DL_STATE_AVAILABLE;
302 break;
303 }
304 break;
305 case DL_DEV_UNBINDING:
306 link->status = DL_STATE_SUPPLIER_UNBIND;
307 break;
308 default:
309 link->status = DL_STATE_DORMANT;
310 break;
311 }
312 }
313
314 /*
315 * Move the consumer and all of the devices depending on it to the end
316 * of dpm_list and the devices_kset list.
317 *
318 * It is necessary to hold dpm_list locked throughout all that or else
319 * we may end up suspending with a wrong ordering of it.
320 */
321 device_reorder_to_tail(consumer, NULL);
322
323 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
324 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
325
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100326 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100327
328 out:
329 device_pm_unlock();
330 device_links_write_unlock();
331 return link;
332}
333EXPORT_SYMBOL_GPL(device_link_add);
334
335static void device_link_free(struct device_link *link)
336{
337 put_device(link->consumer);
338 put_device(link->supplier);
339 kfree(link);
340}
341
342#ifdef CONFIG_SRCU
343static void __device_link_free_srcu(struct rcu_head *rhead)
344{
345 device_link_free(container_of(rhead, struct device_link, rcu_head));
346}
347
Lukas Wunneread18c22018-02-10 19:27:12 +0100348static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100349{
Lukas Wunneread18c22018-02-10 19:27:12 +0100350 struct device_link *link = container_of(kref, struct device_link, kref);
351
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100352 dev_dbg(link->consumer, "Dropping the link to %s\n",
353 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100354
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100355 if (link->flags & DL_FLAG_PM_RUNTIME)
356 pm_runtime_drop_link(link->consumer);
357
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100358 list_del_rcu(&link->s_node);
359 list_del_rcu(&link->c_node);
360 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
361}
362#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100363static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100364{
Lukas Wunneread18c22018-02-10 19:27:12 +0100365 struct device_link *link = container_of(kref, struct device_link, kref);
366
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100367 dev_info(link->consumer, "Dropping the link to %s\n",
368 dev_name(link->supplier));
369
Lukas Wunner433986c2018-02-10 19:13:58 +0100370 if (link->flags & DL_FLAG_PM_RUNTIME)
371 pm_runtime_drop_link(link->consumer);
372
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100373 list_del(&link->s_node);
374 list_del(&link->c_node);
375 device_link_free(link);
376}
377#endif /* !CONFIG_SRCU */
378
379/**
380 * device_link_del - Delete a link between two devices.
381 * @link: Device link to delete.
382 *
383 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100384 * PM. If the link was added multiple times, it needs to be deleted as often.
385 * Care is required for hotplugged devices: Their links are purged on removal
386 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100387 */
388void device_link_del(struct device_link *link)
389{
390 device_links_write_lock();
391 device_pm_lock();
Lukas Wunneread18c22018-02-10 19:27:12 +0100392 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100393 device_pm_unlock();
394 device_links_write_unlock();
395}
396EXPORT_SYMBOL_GPL(device_link_del);
397
pascal pailletd8842212018-07-05 14:25:56 +0000398/**
399 * device_link_remove - remove a link between two devices.
400 * @consumer: Consumer end of the link.
401 * @supplier: Supplier end of the link.
402 *
403 * The caller must ensure proper synchronization of this function with runtime
404 * PM.
405 */
406void device_link_remove(void *consumer, struct device *supplier)
407{
408 struct device_link *link;
409
410 if (WARN_ON(consumer == supplier))
411 return;
412
413 device_links_write_lock();
414 device_pm_lock();
415
416 list_for_each_entry(link, &supplier->links.consumers, s_node) {
417 if (link->consumer == consumer) {
418 kref_put(&link->kref, __device_link_del);
419 break;
420 }
421 }
422
423 device_pm_unlock();
424 device_links_write_unlock();
425}
426EXPORT_SYMBOL_GPL(device_link_remove);
427
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100428static void device_links_missing_supplier(struct device *dev)
429{
430 struct device_link *link;
431
432 list_for_each_entry(link, &dev->links.suppliers, c_node)
433 if (link->status == DL_STATE_CONSUMER_PROBE)
434 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
435}
436
437/**
438 * device_links_check_suppliers - Check presence of supplier drivers.
439 * @dev: Consumer device.
440 *
441 * Check links from this device to any suppliers. Walk the list of the device's
442 * links to suppliers and see if all of them are available. If not, simply
443 * return -EPROBE_DEFER.
444 *
445 * We need to guarantee that the supplier will not go away after the check has
446 * been positive here. It only can go away in __device_release_driver() and
447 * that function checks the device's links to consumers. This means we need to
448 * mark the link as "consumer probe in progress" to make the supplier removal
449 * wait for us to complete (or bad things may happen).
450 *
451 * Links with the DL_FLAG_STATELESS flag set are ignored.
452 */
453int device_links_check_suppliers(struct device *dev)
454{
455 struct device_link *link;
456 int ret = 0;
457
458 device_links_write_lock();
459
460 list_for_each_entry(link, &dev->links.suppliers, c_node) {
461 if (link->flags & DL_FLAG_STATELESS)
462 continue;
463
464 if (link->status != DL_STATE_AVAILABLE) {
465 device_links_missing_supplier(dev);
466 ret = -EPROBE_DEFER;
467 break;
468 }
469 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
470 }
471 dev->links.status = DL_DEV_PROBING;
472
473 device_links_write_unlock();
474 return ret;
475}
476
477/**
478 * device_links_driver_bound - Update device links after probing its driver.
479 * @dev: Device to update the links for.
480 *
481 * The probe has been successful, so update links from this device to any
482 * consumers by changing their status to "available".
483 *
484 * Also change the status of @dev's links to suppliers to "active".
485 *
486 * Links with the DL_FLAG_STATELESS flag set are ignored.
487 */
488void device_links_driver_bound(struct device *dev)
489{
490 struct device_link *link;
491
492 device_links_write_lock();
493
494 list_for_each_entry(link, &dev->links.consumers, s_node) {
495 if (link->flags & DL_FLAG_STATELESS)
496 continue;
497
498 WARN_ON(link->status != DL_STATE_DORMANT);
499 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
500 }
501
502 list_for_each_entry(link, &dev->links.suppliers, c_node) {
503 if (link->flags & DL_FLAG_STATELESS)
504 continue;
505
506 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
507 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
508 }
509
510 dev->links.status = DL_DEV_DRIVER_BOUND;
511
512 device_links_write_unlock();
513}
514
515/**
516 * __device_links_no_driver - Update links of a device without a driver.
517 * @dev: Device without a drvier.
518 *
519 * Delete all non-persistent links from this device to any suppliers.
520 *
521 * Persistent links stay around, but their status is changed to "available",
522 * unless they already are in the "supplier unbind in progress" state in which
523 * case they need not be updated.
524 *
525 * Links with the DL_FLAG_STATELESS flag set are ignored.
526 */
527static void __device_links_no_driver(struct device *dev)
528{
529 struct device_link *link, *ln;
530
531 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
532 if (link->flags & DL_FLAG_STATELESS)
533 continue;
534
Vivek Gautame88728f2018-06-27 18:20:55 +0530535 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Yong Wu0fe6f782019-01-01 12:51:05 +0800536 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100537 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
538 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
539 }
540
541 dev->links.status = DL_DEV_NO_DRIVER;
542}
543
544void device_links_no_driver(struct device *dev)
545{
546 device_links_write_lock();
547 __device_links_no_driver(dev);
548 device_links_write_unlock();
549}
550
551/**
552 * device_links_driver_cleanup - Update links after driver removal.
553 * @dev: Device whose driver has just gone away.
554 *
555 * Update links to consumers for @dev by changing their status to "dormant" and
556 * invoke %__device_links_no_driver() to update links to suppliers for it as
557 * appropriate.
558 *
559 * Links with the DL_FLAG_STATELESS flag set are ignored.
560 */
561void device_links_driver_cleanup(struct device *dev)
562{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100563 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100564
565 device_links_write_lock();
566
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100567 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100568 if (link->flags & DL_FLAG_STATELESS)
569 continue;
570
Vivek Gautame88728f2018-06-27 18:20:55 +0530571 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100572 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530573
574 /*
575 * autoremove the links between this @dev and its consumer
576 * devices that are not active, i.e. where the link state
577 * has moved to DL_STATE_SUPPLIER_UNBIND.
578 */
579 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
580 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Yong Wu0fe6f782019-01-01 12:51:05 +0800581 __device_link_del(&link->kref);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530582
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100583 WRITE_ONCE(link->status, DL_STATE_DORMANT);
584 }
585
586 __device_links_no_driver(dev);
587
588 device_links_write_unlock();
589}
590
591/**
592 * device_links_busy - Check if there are any busy links to consumers.
593 * @dev: Device to check.
594 *
595 * Check each consumer of the device and return 'true' if its link's status
596 * is one of "consumer probe" or "active" (meaning that the given consumer is
597 * probing right now or its driver is present). Otherwise, change the link
598 * state to "supplier unbind" to prevent the consumer from being probed
599 * successfully going forward.
600 *
601 * Return 'false' if there are no probing or active consumers.
602 *
603 * Links with the DL_FLAG_STATELESS flag set are ignored.
604 */
605bool device_links_busy(struct device *dev)
606{
607 struct device_link *link;
608 bool ret = false;
609
610 device_links_write_lock();
611
612 list_for_each_entry(link, &dev->links.consumers, s_node) {
613 if (link->flags & DL_FLAG_STATELESS)
614 continue;
615
616 if (link->status == DL_STATE_CONSUMER_PROBE
617 || link->status == DL_STATE_ACTIVE) {
618 ret = true;
619 break;
620 }
621 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
622 }
623
624 dev->links.status = DL_DEV_UNBINDING;
625
626 device_links_write_unlock();
627 return ret;
628}
629
630/**
631 * device_links_unbind_consumers - Force unbind consumers of the given device.
632 * @dev: Device to unbind the consumers of.
633 *
634 * Walk the list of links to consumers for @dev and if any of them is in the
635 * "consumer probe" state, wait for all device probes in progress to complete
636 * and start over.
637 *
638 * If that's not the case, change the status of the link to "supplier unbind"
639 * and check if the link was in the "active" state. If so, force the consumer
640 * driver to unbind and start over (the consumer will not re-probe as we have
641 * changed the state of the link already).
642 *
643 * Links with the DL_FLAG_STATELESS flag set are ignored.
644 */
645void device_links_unbind_consumers(struct device *dev)
646{
647 struct device_link *link;
648
649 start:
650 device_links_write_lock();
651
652 list_for_each_entry(link, &dev->links.consumers, s_node) {
653 enum device_link_state status;
654
655 if (link->flags & DL_FLAG_STATELESS)
656 continue;
657
658 status = link->status;
659 if (status == DL_STATE_CONSUMER_PROBE) {
660 device_links_write_unlock();
661
662 wait_for_device_probe();
663 goto start;
664 }
665 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
666 if (status == DL_STATE_ACTIVE) {
667 struct device *consumer = link->consumer;
668
669 get_device(consumer);
670
671 device_links_write_unlock();
672
673 device_release_driver_internal(consumer, NULL,
674 consumer->parent);
675 put_device(consumer);
676 goto start;
677 }
678 }
679
680 device_links_write_unlock();
681}
682
683/**
684 * device_links_purge - Delete existing links to other devices.
685 * @dev: Target device.
686 */
687static void device_links_purge(struct device *dev)
688{
689 struct device_link *link, *ln;
690
691 /*
692 * Delete all of the remaining links from this device to any other
693 * devices (either consumers or suppliers).
694 */
695 device_links_write_lock();
696
697 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
698 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100699 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100700 }
701
702 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
703 WARN_ON(link->status != DL_STATE_DORMANT &&
704 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100705 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100706 }
707
708 device_links_write_unlock();
709}
710
711/* Device links support end. */
712
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800713int (*platform_notify)(struct device *dev) = NULL;
714int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700715static struct kobject *dev_kobj;
716struct kobject *sysfs_dev_char_kobj;
717struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200719static DEFINE_MUTEX(device_hotplug_lock);
720
721void lock_device_hotplug(void)
722{
723 mutex_lock(&device_hotplug_lock);
724}
725
726void unlock_device_hotplug(void)
727{
728 mutex_unlock(&device_hotplug_lock);
729}
730
731int lock_device_hotplug_sysfs(void)
732{
733 if (mutex_trylock(&device_hotplug_lock))
734 return 0;
735
736 /* Avoid busy looping (5 ms of sleep should do). */
737 msleep(5);
738 return restart_syscall();
739}
740
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800741#ifdef CONFIG_BLOCK
742static inline int device_is_not_partition(struct device *dev)
743{
744 return !(dev->type == &part_type);
745}
746#else
747static inline int device_is_not_partition(struct device *dev)
748{
749 return 1;
750}
751#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Heikki Krogerus07de0e82018-11-09 17:21:34 +0300753static int
754device_platform_notify(struct device *dev, enum kobject_action action)
755{
Heikki Krogerus7847a142018-11-09 17:21:35 +0300756 int ret;
757
758 ret = acpi_platform_notify(dev, action);
759 if (ret)
760 return ret;
761
Heikki Krogerus59abd832018-11-09 17:21:36 +0300762 ret = software_node_notify(dev, action);
763 if (ret)
764 return ret;
765
Heikki Krogerus07de0e82018-11-09 17:21:34 +0300766 if (platform_notify && action == KOBJ_ADD)
767 platform_notify(dev);
768 else if (platform_notify_remove && action == KOBJ_REMOVE)
769 platform_notify_remove(dev);
770 return 0;
771}
772
Alan Stern3e956372006-06-16 17:10:48 -0400773/**
774 * dev_driver_string - Return a device's driver name, if at all possible
775 * @dev: struct device to get the name of
776 *
777 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800778 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400779 * it is attached to. If it is not attached to a bus either, an empty
780 * string will be returned.
781 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700782const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400783{
Alan Stern35899722009-12-04 11:06:57 -0500784 struct device_driver *drv;
785
786 /* dev->driver can change to NULL underneath us because of unbinding,
787 * so be careful about accessing it. dev->bus and dev->class should
788 * never change once they are set, so they don't need special care.
789 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700790 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500791 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100792 (dev->bus ? dev->bus->name :
793 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400794}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600795EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
798
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800799static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
800 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800802 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200803 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500804 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400807 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800808 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900809 printk("dev_attr_show: %pS returned bad count\n",
810 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return ret;
813}
814
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800815static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
816 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800818 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200819 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500820 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400823 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return ret;
825}
826
Emese Revfy52cf25d2010-01-19 02:58:23 +0100827static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 .show = dev_attr_show,
829 .store = dev_attr_store,
830};
831
Kay Sieversca22e562011-12-14 14:29:38 -0800832#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
833
834ssize_t device_store_ulong(struct device *dev,
835 struct device_attribute *attr,
836 const char *buf, size_t size)
837{
838 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -0800839 int ret;
840 unsigned long new;
841
842 ret = kstrtoul(buf, 0, &new);
843 if (ret)
844 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -0800845 *(unsigned long *)(ea->var) = new;
846 /* Always return full write size even if we didn't consume all */
847 return size;
848}
849EXPORT_SYMBOL_GPL(device_store_ulong);
850
851ssize_t device_show_ulong(struct device *dev,
852 struct device_attribute *attr,
853 char *buf)
854{
855 struct dev_ext_attribute *ea = to_ext_attr(attr);
856 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
857}
858EXPORT_SYMBOL_GPL(device_show_ulong);
859
860ssize_t device_store_int(struct device *dev,
861 struct device_attribute *attr,
862 const char *buf, size_t size)
863{
864 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -0800865 int ret;
866 long new;
867
868 ret = kstrtol(buf, 0, &new);
869 if (ret)
870 return ret;
871
872 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -0800873 return -EINVAL;
874 *(int *)(ea->var) = new;
875 /* Always return full write size even if we didn't consume all */
876 return size;
877}
878EXPORT_SYMBOL_GPL(device_store_int);
879
880ssize_t device_show_int(struct device *dev,
881 struct device_attribute *attr,
882 char *buf)
883{
884 struct dev_ext_attribute *ea = to_ext_attr(attr);
885
886 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
887}
888EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Borislav Petkov91872392012-10-09 19:52:05 +0200890ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
891 const char *buf, size_t size)
892{
893 struct dev_ext_attribute *ea = to_ext_attr(attr);
894
895 if (strtobool(buf, ea->var) < 0)
896 return -EINVAL;
897
898 return size;
899}
900EXPORT_SYMBOL_GPL(device_store_bool);
901
902ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
903 char *buf)
904{
905 struct dev_ext_attribute *ea = to_ext_attr(attr);
906
907 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
908}
909EXPORT_SYMBOL_GPL(device_show_bool);
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400912 * device_release - free device structure.
913 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400915 * This is called once the reference count for the object
916 * reaches 0. We forward the call to the device's release
917 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800919static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200921 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800922 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Ming Leia525a3d2012-07-25 01:42:29 +0800924 /*
925 * Some platform devices are driven without driver attached
926 * and managed resources may have been acquired. Make sure
927 * all resources are released.
928 *
929 * Drivers still can add resources into device after device
930 * is deleted but alive, so release devres here to avoid
931 * possible memory leak.
932 */
933 devres_release_all(dev);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (dev->release)
936 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200937 else if (dev->type && dev->type->release)
938 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700939 else if (dev->class && dev->class->dev_release)
940 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700941 else
Ezequiel Garcia186bddb2018-12-03 13:44:35 -0300942 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100943 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800944 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700947static const void *device_namespace(struct kobject *kobj)
948{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200949 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700950 const void *ns = NULL;
951
952 if (dev->class && dev->class->ns_type)
953 ns = dev->class->namespace(dev);
954
955 return ns;
956}
957
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000958static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
959{
960 struct device *dev = kobj_to_dev(kobj);
961
962 if (dev->class && dev->class->get_ownership)
963 dev->class->get_ownership(dev, uid, gid);
964}
965
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600966static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 .release = device_release,
968 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700969 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000970 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971};
972
973
Kay Sievers312c0042005-11-16 09:00:00 +0100974static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct kobj_type *ktype = get_ktype(kobj);
977
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600978 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200979 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (dev->bus)
981 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700982 if (dev->class)
983 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 return 0;
986}
987
Kay Sievers312c0042005-11-16 09:00:00 +0100988static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200990 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700992 if (dev->bus)
993 return dev->bus->name;
994 if (dev->class)
995 return dev->class->name;
996 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Kay Sievers7eff2e72007-08-14 15:15:12 +0200999static int dev_uevent(struct kset *kset, struct kobject *kobj,
1000 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001002 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int retval = 0;
1004
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001005 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001006 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001007 const char *tmp;
1008 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04001009 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001010 kuid_t uid = GLOBAL_ROOT_UID;
1011 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001012
Kay Sievers7eff2e72007-08-14 15:15:12 +02001013 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1014 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001015 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001016 if (name) {
1017 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02001018 if (mode)
1019 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001020 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1021 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1022 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1023 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001024 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001025 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001026 }
1027
Kay Sievers414264f2007-03-12 21:08:57 +01001028 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001029 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01001030
Kay Sievers239378f2006-10-07 21:54:55 +02001031 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001032 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02001033
Grant Likely07d57a32012-02-01 11:22:22 -07001034 /* Add common DT information about the device */
1035 of_device_uevent(dev, env);
1036
Kay Sievers7eff2e72007-08-14 15:15:12 +02001037 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01001038 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001039 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001040 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001041 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001042 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Kay Sievers7eff2e72007-08-14 15:15:12 +02001045 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001046 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001047 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001048 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001049 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001050 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001051 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001052 }
1053
Stefan Weileef35c22010-08-06 21:11:15 +02001054 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02001055 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001056 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001057 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001058 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001059 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001060 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001061 }
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return retval;
1064}
1065
Emese Revfy9cd43612009-12-31 14:52:51 +01001066static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01001067 .filter = dev_uevent_filter,
1068 .name = dev_uevent_name,
1069 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070};
1071
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001072static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02001073 char *buf)
1074{
1075 struct kobject *top_kobj;
1076 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001077 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02001078 int i;
1079 size_t count = 0;
1080 int retval;
1081
1082 /* search the kset, the device belongs to */
1083 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001084 while (!top_kobj->kset && top_kobj->parent)
1085 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02001086 if (!top_kobj->kset)
1087 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001088
Kay Sievers16574dc2007-04-06 01:40:38 +02001089 kset = top_kobj->kset;
1090 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1091 goto out;
1092
1093 /* respect filter */
1094 if (kset->uevent_ops && kset->uevent_ops->filter)
1095 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1096 goto out;
1097
Kay Sievers7eff2e72007-08-14 15:15:12 +02001098 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1099 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001100 return -ENOMEM;
1101
Kay Sievers16574dc2007-04-06 01:40:38 +02001102 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001103 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001104 if (retval)
1105 goto out;
1106
1107 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001108 for (i = 0; i < env->envp_idx; i++)
1109 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001110out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001111 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001112 return count;
1113}
1114
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001115static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001116 const char *buf, size_t count)
1117{
Peter Rajnohadf44b472018-12-05 12:27:44 +01001118 int rc;
1119
1120 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1121
1122 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001123 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01001124 return rc;
1125 }
Kay Sievers60a96a52007-07-08 22:29:26 +02001126
Kay Sieversa7fd6702005-10-01 14:49:43 +02001127 return count;
1128}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001129static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001130
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001131static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001132 char *buf)
1133{
1134 bool val;
1135
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001136 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001137 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001138 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001139 return sprintf(buf, "%u\n", val);
1140}
1141
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001142static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001143 const char *buf, size_t count)
1144{
1145 bool val;
1146 int ret;
1147
1148 ret = strtobool(buf, &val);
1149 if (ret < 0)
1150 return ret;
1151
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001152 ret = lock_device_hotplug_sysfs();
1153 if (ret)
1154 return ret;
1155
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001156 ret = val ? device_online(dev) : device_offline(dev);
1157 unlock_device_hotplug();
1158 return ret < 0 ? ret : count;
1159}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001160static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001161
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001162int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001163{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001164 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001165}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001166EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001167
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001168void device_remove_groups(struct device *dev,
1169 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001170{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001171 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001172}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001173EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001174
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001175union device_attr_group_devres {
1176 const struct attribute_group *group;
1177 const struct attribute_group **groups;
1178};
1179
1180static int devm_attr_group_match(struct device *dev, void *res, void *data)
1181{
1182 return ((union device_attr_group_devres *)res)->group == data;
1183}
1184
1185static void devm_attr_group_remove(struct device *dev, void *res)
1186{
1187 union device_attr_group_devres *devres = res;
1188 const struct attribute_group *group = devres->group;
1189
1190 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1191 sysfs_remove_group(&dev->kobj, group);
1192}
1193
1194static void devm_attr_groups_remove(struct device *dev, void *res)
1195{
1196 union device_attr_group_devres *devres = res;
1197 const struct attribute_group **groups = devres->groups;
1198
1199 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1200 sysfs_remove_groups(&dev->kobj, groups);
1201}
1202
1203/**
1204 * devm_device_add_group - given a device, create a managed attribute group
1205 * @dev: The device to create the group for
1206 * @grp: The attribute group to create
1207 *
1208 * This function creates a group for the first time. It will explicitly
1209 * warn and error if any of the attribute files being created already exist.
1210 *
1211 * Returns 0 on success or error code on failure.
1212 */
1213int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1214{
1215 union device_attr_group_devres *devres;
1216 int error;
1217
1218 devres = devres_alloc(devm_attr_group_remove,
1219 sizeof(*devres), GFP_KERNEL);
1220 if (!devres)
1221 return -ENOMEM;
1222
1223 error = sysfs_create_group(&dev->kobj, grp);
1224 if (error) {
1225 devres_free(devres);
1226 return error;
1227 }
1228
1229 devres->group = grp;
1230 devres_add(dev, devres);
1231 return 0;
1232}
1233EXPORT_SYMBOL_GPL(devm_device_add_group);
1234
1235/**
1236 * devm_device_remove_group: remove a managed group from a device
1237 * @dev: device to remove the group from
1238 * @grp: group to remove
1239 *
1240 * This function removes a group of attributes from a device. The attributes
1241 * previously have to have been created for this group, otherwise it will fail.
1242 */
1243void devm_device_remove_group(struct device *dev,
1244 const struct attribute_group *grp)
1245{
1246 WARN_ON(devres_release(dev, devm_attr_group_remove,
1247 devm_attr_group_match,
1248 /* cast away const */ (void *)grp));
1249}
1250EXPORT_SYMBOL_GPL(devm_device_remove_group);
1251
1252/**
1253 * devm_device_add_groups - create a bunch of managed attribute groups
1254 * @dev: The device to create the group for
1255 * @groups: The attribute groups to create, NULL terminated
1256 *
1257 * This function creates a bunch of managed attribute groups. If an error
1258 * occurs when creating a group, all previously created groups will be
1259 * removed, unwinding everything back to the original state when this
1260 * function was called. It will explicitly warn and error if any of the
1261 * attribute files being created already exist.
1262 *
1263 * Returns 0 on success or error code from sysfs_create_group on failure.
1264 */
1265int devm_device_add_groups(struct device *dev,
1266 const struct attribute_group **groups)
1267{
1268 union device_attr_group_devres *devres;
1269 int error;
1270
1271 devres = devres_alloc(devm_attr_groups_remove,
1272 sizeof(*devres), GFP_KERNEL);
1273 if (!devres)
1274 return -ENOMEM;
1275
1276 error = sysfs_create_groups(&dev->kobj, groups);
1277 if (error) {
1278 devres_free(devres);
1279 return error;
1280 }
1281
1282 devres->groups = groups;
1283 devres_add(dev, devres);
1284 return 0;
1285}
1286EXPORT_SYMBOL_GPL(devm_device_add_groups);
1287
1288/**
1289 * devm_device_remove_groups - remove a list of managed groups
1290 *
1291 * @dev: The device for the groups to be removed from
1292 * @groups: NULL terminated list of groups to be removed
1293 *
1294 * If groups is not NULL, remove the specified groups from the device.
1295 */
1296void devm_device_remove_groups(struct device *dev,
1297 const struct attribute_group **groups)
1298{
1299 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1300 devm_attr_group_match,
1301 /* cast away const */ (void *)groups));
1302}
1303EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001305static int device_add_attrs(struct device *dev)
1306{
1307 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001308 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001309 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001310
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001311 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001312 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001313 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001314 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001315 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001316
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001317 if (type) {
1318 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001319 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001320 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001321 }
1322
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001323 error = device_add_groups(dev, dev->groups);
1324 if (error)
1325 goto err_remove_type_groups;
1326
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001327 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001328 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001329 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001330 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001331 }
1332
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001333 return 0;
1334
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001335 err_remove_dev_groups:
1336 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001337 err_remove_type_groups:
1338 if (type)
1339 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001340 err_remove_class_groups:
1341 if (class)
1342 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001343
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001344 return error;
1345}
1346
1347static void device_remove_attrs(struct device *dev)
1348{
1349 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001350 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001351
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001352 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001353 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001354
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001355 if (type)
1356 device_remove_groups(dev, type->groups);
1357
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001358 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001359 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001360}
1361
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001362static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001363 char *buf)
1364{
1365 return print_dev_t(buf, dev->devt);
1366}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001367static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001368
Kay Sieversca22e562011-12-14 14:29:38 -08001369/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001370struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001373 * devices_kset_move_before - Move device in the devices_kset's list.
1374 * @deva: Device to move.
1375 * @devb: Device @deva should come before.
1376 */
1377static void devices_kset_move_before(struct device *deva, struct device *devb)
1378{
1379 if (!devices_kset)
1380 return;
1381 pr_debug("devices_kset: Moving %s before %s\n",
1382 dev_name(deva), dev_name(devb));
1383 spin_lock(&devices_kset->list_lock);
1384 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1385 spin_unlock(&devices_kset->list_lock);
1386}
1387
1388/**
1389 * devices_kset_move_after - Move device in the devices_kset's list.
1390 * @deva: Device to move
1391 * @devb: Device @deva should come after.
1392 */
1393static void devices_kset_move_after(struct device *deva, struct device *devb)
1394{
1395 if (!devices_kset)
1396 return;
1397 pr_debug("devices_kset: Moving %s after %s\n",
1398 dev_name(deva), dev_name(devb));
1399 spin_lock(&devices_kset->list_lock);
1400 list_move(&deva->kobj.entry, &devb->kobj.entry);
1401 spin_unlock(&devices_kset->list_lock);
1402}
1403
1404/**
1405 * devices_kset_move_last - move the device to the end of devices_kset's list.
1406 * @dev: device to move
1407 */
1408void devices_kset_move_last(struct device *dev)
1409{
1410 if (!devices_kset)
1411 return;
1412 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1413 spin_lock(&devices_kset->list_lock);
1414 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1415 spin_unlock(&devices_kset->list_lock);
1416}
1417
1418/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001419 * device_create_file - create sysfs attribute file for device.
1420 * @dev: device.
1421 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001423int device_create_file(struct device *dev,
1424 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001427
1428 if (dev) {
1429 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001430 "Attribute %s: write permission without 'store'\n",
1431 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001432 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001433 "Attribute %s: read permission without 'show'\n",
1434 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001436 }
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return error;
1439}
David Graham White86df2682013-07-21 20:41:14 -04001440EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001443 * device_remove_file - remove sysfs attribute file.
1444 * @dev: device.
1445 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001447void device_remove_file(struct device *dev,
1448 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001450 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
David Graham White86df2682013-07-21 20:41:14 -04001453EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001455/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001456 * device_remove_file_self - remove sysfs attribute file from its own method.
1457 * @dev: device.
1458 * @attr: device attribute descriptor.
1459 *
1460 * See kernfs_remove_self() for details.
1461 */
1462bool device_remove_file_self(struct device *dev,
1463 const struct device_attribute *attr)
1464{
1465 if (dev)
1466 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1467 else
1468 return false;
1469}
1470EXPORT_SYMBOL_GPL(device_remove_file_self);
1471
1472/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001473 * device_create_bin_file - create sysfs binary attribute file for device.
1474 * @dev: device.
1475 * @attr: device binary attribute descriptor.
1476 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001477int device_create_bin_file(struct device *dev,
1478 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001479{
1480 int error = -EINVAL;
1481 if (dev)
1482 error = sysfs_create_bin_file(&dev->kobj, attr);
1483 return error;
1484}
1485EXPORT_SYMBOL_GPL(device_create_bin_file);
1486
1487/**
1488 * device_remove_bin_file - remove sysfs binary attribute file
1489 * @dev: device.
1490 * @attr: device binary attribute descriptor.
1491 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001492void device_remove_bin_file(struct device *dev,
1493 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001494{
1495 if (dev)
1496 sysfs_remove_bin_file(&dev->kobj, attr);
1497}
1498EXPORT_SYMBOL_GPL(device_remove_bin_file);
1499
James Bottomley34bb61f2005-09-06 16:56:51 -07001500static void klist_children_get(struct klist_node *n)
1501{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001502 struct device_private *p = to_device_private_parent(n);
1503 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001504
1505 get_device(dev);
1506}
1507
1508static void klist_children_put(struct klist_node *n)
1509{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001510 struct device_private *p = to_device_private_parent(n);
1511 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001512
1513 put_device(dev);
1514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001517 * device_initialize - init device structure.
1518 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 *
Cornelia Huck57394112008-09-03 18:26:40 +02001520 * This prepares the device for use by other layers by initializing
1521 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001522 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001523 * that function, though it can also be called separately, so one
1524 * may use @dev's fields. In particular, get_device()/put_device()
1525 * may be used for reference counting of @dev after calling this
1526 * function.
1527 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001528 * All fields in @dev must be initialized by the caller to 0, except
1529 * for those explicitly set to some other value. The simplest
1530 * approach is to use kzalloc() to allocate the structure containing
1531 * @dev.
1532 *
Cornelia Huck57394112008-09-03 18:26:40 +02001533 * NOTE: Use put_device() to give up your reference instead of freeing
1534 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536void device_initialize(struct device *dev)
1537{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001538 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001539 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001541 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001542 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001543 spin_lock_init(&dev->devres_lock);
1544 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001545 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001546 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001547#ifdef CONFIG_GENERIC_MSI_IRQ
1548 INIT_LIST_HEAD(&dev->msi_list);
1549#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001550 INIT_LIST_HEAD(&dev->links.consumers);
1551 INIT_LIST_HEAD(&dev->links.suppliers);
1552 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
David Graham White86df2682013-07-21 20:41:14 -04001554EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Tejun Heod73ce002013-03-12 11:30:05 -07001556struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001557{
Kay Sievers86406242007-03-14 03:25:56 +01001558 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001559
Kay Sievers86406242007-03-14 03:25:56 +01001560 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001561 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001562 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001563
Kay Sievers86406242007-03-14 03:25:56 +01001564 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001565}
1566
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001567struct class_dir {
1568 struct kobject kobj;
1569 struct class *class;
1570};
1571
1572#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1573
1574static void class_dir_release(struct kobject *kobj)
1575{
1576 struct class_dir *dir = to_class_dir(kobj);
1577 kfree(dir);
1578}
1579
1580static const
1581struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1582{
1583 struct class_dir *dir = to_class_dir(kobj);
1584 return dir->class->ns_type;
1585}
1586
1587static struct kobj_type class_dir_ktype = {
1588 .release = class_dir_release,
1589 .sysfs_ops = &kobj_sysfs_ops,
1590 .child_ns_type = class_dir_child_ns_type
1591};
1592
1593static struct kobject *
1594class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1595{
1596 struct class_dir *dir;
1597 int retval;
1598
1599 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1600 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001601 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001602
1603 dir->class = class;
1604 kobject_init(&dir->kobj, &class_dir_ktype);
1605
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001606 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001607
1608 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1609 if (retval < 0) {
1610 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001611 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001612 }
1613 return &dir->kobj;
1614}
1615
Yijing Wange4a60d12014-11-07 12:05:49 +08001616static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001617
Kay Sieversda231fd2007-11-21 17:29:15 +01001618static struct kobject *get_device_parent(struct device *dev,
1619 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001620{
Kay Sievers86406242007-03-14 03:25:56 +01001621 if (dev->class) {
1622 struct kobject *kobj = NULL;
1623 struct kobject *parent_kobj;
1624 struct kobject *k;
1625
Randy Dunlapead454f2010-09-24 14:36:49 -07001626#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001627 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001628 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001629 if (parent && parent->class == &block_class)
1630 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001631 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001632 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001633#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001634
Kay Sievers86406242007-03-14 03:25:56 +01001635 /*
1636 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001637 * Class-devices with a non class-device as parent, live
1638 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001639 */
1640 if (parent == NULL)
1641 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001642 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001643 return &parent->kobj;
1644 else
1645 parent_kobj = &parent->kobj;
1646
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001647 mutex_lock(&gdp_mutex);
1648
Kay Sievers86406242007-03-14 03:25:56 +01001649 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001650 spin_lock(&dev->class->p->glue_dirs.list_lock);
1651 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001652 if (k->parent == parent_kobj) {
1653 kobj = kobject_get(k);
1654 break;
1655 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001656 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001657 if (kobj) {
1658 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001659 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001660 }
Kay Sievers86406242007-03-14 03:25:56 +01001661
1662 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001663 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001664 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001665 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001666 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001667 }
1668
Kay Sieversca22e562011-12-14 14:29:38 -08001669 /* subsystems can specify a default root directory for their devices */
1670 if (!parent && dev->bus && dev->bus->dev_root)
1671 return &dev->bus->dev_root->kobj;
1672
Kay Sievers86406242007-03-14 03:25:56 +01001673 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001674 return &parent->kobj;
1675 return NULL;
1676}
Kay Sieversda231fd2007-11-21 17:29:15 +01001677
Ming Leicebf8fd2016-07-10 19:27:36 +08001678static inline bool live_in_glue_dir(struct kobject *kobj,
1679 struct device *dev)
1680{
1681 if (!kobj || !dev->class ||
1682 kobj->kset != &dev->class->p->glue_dirs)
1683 return false;
1684 return true;
1685}
1686
1687static inline struct kobject *get_glue_dir(struct device *dev)
1688{
1689 return dev->kobj.parent;
1690}
1691
1692/*
1693 * make sure cleaning up dir as the last step, we need to make
1694 * sure .release handler of kobject is run with holding the
1695 * global lock
1696 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001697static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001698{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001699 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001700 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001701 return;
1702
Yijing Wange4a60d12014-11-07 12:05:49 +08001703 mutex_lock(&gdp_mutex);
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10001704 if (!kobject_has_children(glue_dir))
1705 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001706 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001707 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001708}
Cornelia Huck63b69712008-01-21 16:09:44 +01001709
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001710static int device_add_class_symlinks(struct device *dev)
1711{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001712 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001713 int error;
1714
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001715 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001716 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001717 if (error)
1718 dev_warn(dev, "Error %d creating of_node link\n",error);
1719 /* An error here doesn't warrant bringing down the device */
1720 }
1721
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001722 if (!dev->class)
1723 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001724
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001725 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001726 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001727 "subsystem");
1728 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001729 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001730
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001731 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001732 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1733 "device");
1734 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001735 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001736 }
Kay Sievers39aba962010-09-04 22:33:14 -07001737
Randy Dunlapead454f2010-09-24 14:36:49 -07001738#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001739 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001740 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001741 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001742#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001743
1744 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001745 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001746 &dev->kobj, dev_name(dev));
1747 if (error)
1748 goto out_device;
1749
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001750 return 0;
1751
Kay Sievers39aba962010-09-04 22:33:14 -07001752out_device:
1753 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001754
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001755out_subsys:
1756 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001757out_devnode:
1758 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001759 return error;
1760}
1761
1762static void device_remove_class_symlinks(struct device *dev)
1763{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001764 if (dev_of_node(dev))
1765 sysfs_remove_link(&dev->kobj, "of_node");
1766
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001767 if (!dev->class)
1768 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001769
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001770 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001771 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001772 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001773#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001774 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001775 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001776#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001777 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001778}
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001781 * dev_set_name - set a device name
1782 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001783 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001784 */
1785int dev_set_name(struct device *dev, const char *fmt, ...)
1786{
1787 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001788 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001789
1790 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001791 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001792 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001793 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001794}
1795EXPORT_SYMBOL_GPL(dev_set_name);
1796
1797/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001798 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1799 * @dev: device
1800 *
1801 * By default we select char/ for new entries. Setting class->dev_obj
1802 * to NULL prevents an entry from being created. class->dev_kobj must
1803 * be set (or cleared) before any devices are registered to the class
1804 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001805 * device_remove_sys_dev_entry() will disagree about the presence of
1806 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001807 */
1808static struct kobject *device_to_dev_kobj(struct device *dev)
1809{
1810 struct kobject *kobj;
1811
1812 if (dev->class)
1813 kobj = dev->class->dev_kobj;
1814 else
1815 kobj = sysfs_dev_char_kobj;
1816
1817 return kobj;
1818}
1819
1820static int device_create_sys_dev_entry(struct device *dev)
1821{
1822 struct kobject *kobj = device_to_dev_kobj(dev);
1823 int error = 0;
1824 char devt_str[15];
1825
1826 if (kobj) {
1827 format_dev_t(devt_str, dev->devt);
1828 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1829 }
1830
1831 return error;
1832}
1833
1834static void device_remove_sys_dev_entry(struct device *dev)
1835{
1836 struct kobject *kobj = device_to_dev_kobj(dev);
1837 char devt_str[15];
1838
1839 if (kobj) {
1840 format_dev_t(devt_str, dev->devt);
1841 sysfs_remove_link(kobj, devt_str);
1842 }
1843}
1844
Shaokun Zhang46d3a032018-07-15 18:08:56 +08001845static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001846{
1847 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1848 if (!dev->p)
1849 return -ENOMEM;
1850 dev->p->device = dev;
1851 klist_init(&dev->p->klist_children, klist_children_get,
1852 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001853 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001854 return 0;
1855}
1856
Dan Williamse105b8b2008-04-21 10:51:07 -07001857/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001858 * device_add - add device to device hierarchy.
1859 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001861 * This is part 2 of device_register(), though may be called
1862 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 *
Cornelia Huck57394112008-09-03 18:26:40 +02001864 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001865 * to the global and sibling lists for the device, then
1866 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001867 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001868 * Do not call this routine or device_register() more than once for
1869 * any device structure. The driver model core is not designed to work
1870 * with devices that get unregistered and then spring back to life.
1871 * (Among other things, it's very hard to guarantee that all references
1872 * to the previous incarnation of @dev have been dropped.) Allocate
1873 * and register a fresh new struct device instead.
1874 *
Cornelia Huck57394112008-09-03 18:26:40 +02001875 * NOTE: _Never_ directly free @dev after calling this function, even
1876 * if it returned an error! Always use put_device() to give up your
1877 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
1879int device_add(struct device *dev)
1880{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301881 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001882 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001883 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001884 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001885 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001888 if (!dev)
1889 goto done;
1890
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001891 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001892 error = device_private_init(dev);
1893 if (error)
1894 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001895 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001896
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001897 /*
1898 * for statically allocated devices, which should all be converted
1899 * some day, we need to initialize the name. We prevent reading back
1900 * the name, and force the use of dev_name()
1901 */
1902 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001903 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001904 dev->init_name = NULL;
1905 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001906
Kay Sieversca22e562011-12-14 14:29:38 -08001907 /* subsystems can specify simple device enumeration */
1908 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1909 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1910
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001911 if (!dev_name(dev)) {
1912 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001913 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001916 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001919 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001920 if (IS_ERR(kobj)) {
1921 error = PTR_ERR(kobj);
1922 goto parent_error;
1923 }
Kay Sieversca22e562011-12-14 14:29:38 -08001924 if (kobj)
1925 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Yinghai Lu0d358f22008-02-19 03:20:41 -08001927 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001928 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001929 set_dev_node(dev, dev_to_node(parent));
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001932 /* we require the name to be set before, and pass NULL */
1933 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001934 if (error) {
1935 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001937 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001938
Brian Walsh37022642006-08-14 22:43:19 -07001939 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001940 error = device_platform_notify(dev, KOBJ_ADD);
1941 if (error)
1942 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07001943
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001944 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001945 if (error)
1946 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001947
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001948 error = device_add_class_symlinks(dev);
1949 if (error)
1950 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001951 error = device_add_attrs(dev);
1952 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001953 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001954 error = bus_add_device(dev);
1955 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001957 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001958 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001959 goto DPMError;
1960 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001961
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001962 if (MAJOR(dev->devt)) {
1963 error = device_create_file(dev, &dev_attr_dev);
1964 if (error)
1965 goto DevAttrError;
1966
1967 error = device_create_sys_dev_entry(dev);
1968 if (error)
1969 goto SysEntryError;
1970
1971 devtmpfs_create_node(dev);
1972 }
1973
Alan Sternec0676ee2008-12-05 14:10:31 -05001974 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001975 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001976 */
1977 if (dev->bus)
1978 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1979 BUS_NOTIFY_ADD_DEVICE, dev);
1980
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001981 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001982 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001984 klist_add_tail(&dev->p->knode_parent,
1985 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001987 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001988 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001989 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08001990 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001991 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001992
1993 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001994 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001995 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001996 if (class_intf->add_dev)
1997 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001998 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001999 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002000done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 put_device(dev);
2002 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04002003 SysEntryError:
2004 if (MAJOR(dev->devt))
2005 device_remove_file(dev, &dev_attr_dev);
2006 DevAttrError:
2007 device_pm_remove(dev);
2008 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04002009 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01002010 bus_remove_device(dev);
2011 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00002012 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002013 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002014 device_remove_class_symlinks(dev);
2015 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002016 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002017 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002018 device_platform_notify(dev, KOBJ_REMOVE);
2019platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01002020 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002021 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 kobject_del(&dev->kobj);
2023 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08002024 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002025parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01002026 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07002027name_error:
2028 kfree(dev->p);
2029 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002030 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
David Graham White86df2682013-07-21 20:41:14 -04002032EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002035 * device_register - register a device with the system.
2036 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002038 * This happens in two clean steps - initialize the device
2039 * and add it to the system. The two steps can be called
2040 * separately, but this is the easiest and most common.
2041 * I.e. you should only call the two helpers separately if
2042 * have a clearly defined need to use and refcount the device
2043 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02002044 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002045 * For more information, see the kerneldoc for device_initialize()
2046 * and device_add().
2047 *
Cornelia Huck57394112008-09-03 18:26:40 +02002048 * NOTE: _Never_ directly free @dev after calling this function, even
2049 * if it returned an error! Always use put_device() to give up the
2050 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052int device_register(struct device *dev)
2053{
2054 device_initialize(dev);
2055 return device_add(dev);
2056}
David Graham White86df2682013-07-21 20:41:14 -04002057EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002060 * get_device - increment reference count for device.
2061 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002063 * This simply forwards the call to kobject_get(), though
2064 * we do take care to provide for the case that we get a NULL
2065 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002067struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002069 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
David Graham White86df2682013-07-21 20:41:14 -04002071EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002074 * put_device - decrement reference count.
2075 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002077void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02002079 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (dev)
2081 kobject_put(&dev->kobj);
2082}
David Graham White86df2682013-07-21 20:41:14 -04002083EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002086 * device_del - delete device from system.
2087 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002089 * This is the first part of the device unregistration
2090 * sequence. This removes the device from the lists we control
2091 * from here, has it removed from the other driver model
2092 * subsystems it was added to in device_add(), and removes it
2093 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002095 * NOTE: this should be called manually _iff_ device_add() was
2096 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002098void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002100 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002101 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002102 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Alexander Duyck3451a492019-01-22 10:39:10 -08002104 /*
2105 * Hold the device lock and set the "dead" flag to guarantee that
2106 * the update behavior is consistent with the other bitfields near
2107 * it and that we cannot have an asynchronous probe routine trying
2108 * to run while we are tearing out the bus/class/sysfs from
2109 * underneath the device.
2110 */
2111 device_lock(dev);
2112 dev->p->dead = true;
2113 device_unlock(dev);
2114
Alan Sternec0676ee2008-12-05 14:10:31 -05002115 /* Notify clients of device removal. This call must come
2116 * before dpm_sysfs_remove().
2117 */
2118 if (dev->bus)
2119 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2120 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002121
Alan Stern3b98aea2008-08-07 13:06:12 -04002122 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002124 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002125 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002126 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002127 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002128 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002129 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002130 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002131 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002132
Kay Sieversca22e562011-12-14 14:29:38 -08002133 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002134 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002135 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002136 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002137 if (class_intf->remove_dev)
2138 class_intf->remove_dev(dev, class_intf);
2139 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08002140 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002141 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002142 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002143 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002144 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002145 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002146 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002147 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002148 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02002149 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002150 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Joerg Roedel599bad32014-09-30 13:02:02 +02002152 if (dev->bus)
2153 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2154 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002155 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002156 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002158 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002159 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
David Graham White86df2682013-07-21 20:41:14 -04002161EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002164 * device_unregister - unregister device from system.
2165 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002167 * We do this in two parts, like we do device_register(). First,
2168 * we remove it from all the subsystems with device_del(), then
2169 * we decrement the reference count via put_device(). If that
2170 * is the final reference count, the device will be cleaned up
2171 * via device_release() above. Otherwise, the structure will
2172 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002174void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002176 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 device_del(dev);
2178 put_device(dev);
2179}
David Graham White86df2682013-07-21 20:41:14 -04002180EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002182static struct device *prev_device(struct klist_iter *i)
2183{
2184 struct klist_node *n = klist_prev(i);
2185 struct device *dev = NULL;
2186 struct device_private *p;
2187
2188 if (n) {
2189 p = to_device_private_parent(n);
2190 dev = p->device;
2191 }
2192 return dev;
2193}
2194
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002195static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002196{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002197 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002198 struct device *dev = NULL;
2199 struct device_private *p;
2200
2201 if (n) {
2202 p = to_device_private_parent(n);
2203 dev = p->device;
2204 }
2205 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002206}
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002209 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002210 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002211 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002212 * @uid: returned file owner
2213 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002214 * @tmp: possibly allocated string
2215 *
2216 * Return the relative path of a possible device node.
2217 * Non-default names may need to allocate a memory to compose
2218 * a name. This memory is returned in tmp and needs to be
2219 * freed by the caller.
2220 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002221const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002222 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002223 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002224{
2225 char *s;
2226
2227 *tmp = NULL;
2228
2229 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002230 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002231 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002232 if (*tmp)
2233 return *tmp;
2234
2235 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002236 if (dev->class && dev->class->devnode)
2237 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002238 if (*tmp)
2239 return *tmp;
2240
2241 /* return name without allocation, tmp == NULL */
2242 if (strchr(dev_name(dev), '!') == NULL)
2243 return dev_name(dev);
2244
2245 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002246 s = kstrdup(dev_name(dev), GFP_KERNEL);
2247 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002248 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002249 strreplace(s, '!', '/');
2250 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002251}
2252
2253/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002254 * device_for_each_child - device child iterator.
2255 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002256 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002257 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002259 * Iterate over @parent's child devices, and call @fn for each,
2260 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002262 * We check the return of @fn each time. If it returns anything
2263 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002265int device_for_each_child(struct device *parent, void *data,
2266 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002268 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002269 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 int error = 0;
2271
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002272 if (!parent->p)
2273 return 0;
2274
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002275 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002276 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002277 error = fn(child, data);
2278 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return error;
2280}
David Graham White86df2682013-07-21 20:41:14 -04002281EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Cornelia Huck5ab69982006-11-16 15:42:07 +01002283/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002284 * device_for_each_child_reverse - device child iterator in reversed order.
2285 * @parent: parent struct device.
2286 * @fn: function to be called for each device.
2287 * @data: data for the callback.
2288 *
2289 * Iterate over @parent's child devices, and call @fn for each,
2290 * passing it @data.
2291 *
2292 * We check the return of @fn each time. If it returns anything
2293 * other than 0, we break out and return that value.
2294 */
2295int device_for_each_child_reverse(struct device *parent, void *data,
2296 int (*fn)(struct device *dev, void *data))
2297{
2298 struct klist_iter i;
2299 struct device *child;
2300 int error = 0;
2301
2302 if (!parent->p)
2303 return 0;
2304
2305 klist_iter_init(&parent->p->klist_children, &i);
2306 while ((child = prev_device(&i)) && !error)
2307 error = fn(child, data);
2308 klist_iter_exit(&i);
2309 return error;
2310}
2311EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2312
2313/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002314 * device_find_child - device iterator for locating a particular device.
2315 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002316 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002317 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002318 *
2319 * This is similar to the device_for_each_child() function above, but it
2320 * returns a reference to a device that is 'found' for later use, as
2321 * determined by the @match callback.
2322 *
2323 * The callback should return 0 if the device doesn't match and non-zero
2324 * if it does. If the callback returns non-zero and a reference to the
2325 * current device can be obtained, this function will return to the caller
2326 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002327 *
2328 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002329 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002330struct device *device_find_child(struct device *parent, void *data,
2331 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002332{
2333 struct klist_iter i;
2334 struct device *child;
2335
2336 if (!parent)
2337 return NULL;
2338
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002339 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002340 while ((child = next_device(&i)))
2341 if (match(child, data) && get_device(child))
2342 break;
2343 klist_iter_exit(&i);
2344 return child;
2345}
David Graham White86df2682013-07-21 20:41:14 -04002346EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348int __init devices_init(void)
2349{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002350 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2351 if (!devices_kset)
2352 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002353 dev_kobj = kobject_create_and_add("dev", NULL);
2354 if (!dev_kobj)
2355 goto dev_kobj_err;
2356 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2357 if (!sysfs_dev_block_kobj)
2358 goto block_kobj_err;
2359 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2360 if (!sysfs_dev_char_kobj)
2361 goto char_kobj_err;
2362
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002363 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002364
2365 char_kobj_err:
2366 kobject_put(sysfs_dev_block_kobj);
2367 block_kobj_err:
2368 kobject_put(dev_kobj);
2369 dev_kobj_err:
2370 kset_unregister(devices_kset);
2371 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002374static int device_check_offline(struct device *dev, void *not_used)
2375{
2376 int ret;
2377
2378 ret = device_for_each_child(dev, NULL, device_check_offline);
2379 if (ret)
2380 return ret;
2381
2382 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2383}
2384
2385/**
2386 * device_offline - Prepare the device for hot-removal.
2387 * @dev: Device to be put offline.
2388 *
2389 * Execute the device bus type's .offline() callback, if present, to prepare
2390 * the device for a subsequent hot-removal. If that succeeds, the device must
2391 * not be used until either it is removed or its bus type's .online() callback
2392 * is executed.
2393 *
2394 * Call under device_hotplug_lock.
2395 */
2396int device_offline(struct device *dev)
2397{
2398 int ret;
2399
2400 if (dev->offline_disabled)
2401 return -EPERM;
2402
2403 ret = device_for_each_child(dev, NULL, device_check_offline);
2404 if (ret)
2405 return ret;
2406
2407 device_lock(dev);
2408 if (device_supports_offline(dev)) {
2409 if (dev->offline) {
2410 ret = 1;
2411 } else {
2412 ret = dev->bus->offline(dev);
2413 if (!ret) {
2414 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2415 dev->offline = true;
2416 }
2417 }
2418 }
2419 device_unlock(dev);
2420
2421 return ret;
2422}
2423
2424/**
2425 * device_online - Put the device back online after successful device_offline().
2426 * @dev: Device to be put back online.
2427 *
2428 * If device_offline() has been successfully executed for @dev, but the device
2429 * has not been removed subsequently, execute its bus type's .online() callback
2430 * to indicate that the device can be used again.
2431 *
2432 * Call under device_hotplug_lock.
2433 */
2434int device_online(struct device *dev)
2435{
2436 int ret = 0;
2437
2438 device_lock(dev);
2439 if (device_supports_offline(dev)) {
2440 if (dev->offline) {
2441 ret = dev->bus->online(dev);
2442 if (!ret) {
2443 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2444 dev->offline = false;
2445 }
2446 } else {
2447 ret = 1;
2448 }
2449 }
2450 device_unlock(dev);
2451
2452 return ret;
2453}
2454
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002455struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002456 struct device dev;
2457 struct module *owner;
2458};
2459
Josh Triplett93058422012-11-18 21:27:55 -08002460static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002461{
2462 return container_of(d, struct root_device, dev);
2463}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002464
2465static void root_device_release(struct device *dev)
2466{
2467 kfree(to_root_device(dev));
2468}
2469
2470/**
2471 * __root_device_register - allocate and register a root device
2472 * @name: root device name
2473 * @owner: owner module of the root device, usually THIS_MODULE
2474 *
2475 * This function allocates a root device and registers it
2476 * using device_register(). In order to free the returned
2477 * device, use root_device_unregister().
2478 *
2479 * Root devices are dummy devices which allow other devices
2480 * to be grouped under /sys/devices. Use this function to
2481 * allocate a root device and then use it as the parent of
2482 * any device which should appear under /sys/devices/{name}
2483 *
2484 * The /sys/devices/{name} directory will also contain a
2485 * 'module' symlink which points to the @owner directory
2486 * in sysfs.
2487 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002488 * Returns &struct device pointer on success, or ERR_PTR() on error.
2489 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002490 * Note: You probably want to use root_device_register().
2491 */
2492struct device *__root_device_register(const char *name, struct module *owner)
2493{
2494 struct root_device *root;
2495 int err = -ENOMEM;
2496
2497 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2498 if (!root)
2499 return ERR_PTR(err);
2500
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002501 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002502 if (err) {
2503 kfree(root);
2504 return ERR_PTR(err);
2505 }
2506
2507 root->dev.release = root_device_release;
2508
2509 err = device_register(&root->dev);
2510 if (err) {
2511 put_device(&root->dev);
2512 return ERR_PTR(err);
2513 }
2514
Christoph Egger1d9e8822010-05-17 16:57:58 +02002515#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002516 if (owner) {
2517 struct module_kobject *mk = &owner->mkobj;
2518
2519 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2520 if (err) {
2521 device_unregister(&root->dev);
2522 return ERR_PTR(err);
2523 }
2524 root->owner = owner;
2525 }
2526#endif
2527
2528 return &root->dev;
2529}
2530EXPORT_SYMBOL_GPL(__root_device_register);
2531
2532/**
2533 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002534 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002535 *
2536 * This function unregisters and cleans up a device that was created by
2537 * root_device_register().
2538 */
2539void root_device_unregister(struct device *dev)
2540{
2541 struct root_device *root = to_root_device(dev);
2542
2543 if (root->owner)
2544 sysfs_remove_link(&root->dev.kobj, "module");
2545
2546 device_unregister(dev);
2547}
2548EXPORT_SYMBOL_GPL(root_device_unregister);
2549
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002550
2551static void device_create_release(struct device *dev)
2552{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002553 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002554 kfree(dev);
2555}
2556
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002557static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002558device_create_groups_vargs(struct class *class, struct device *parent,
2559 dev_t devt, void *drvdata,
2560 const struct attribute_group **groups,
2561 const char *fmt, va_list args)
2562{
2563 struct device *dev = NULL;
2564 int retval = -ENODEV;
2565
2566 if (class == NULL || IS_ERR(class))
2567 goto error;
2568
2569 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2570 if (!dev) {
2571 retval = -ENOMEM;
2572 goto error;
2573 }
2574
David Herrmannbbc780f2013-11-21 20:15:48 +01002575 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002576 dev->devt = devt;
2577 dev->class = class;
2578 dev->parent = parent;
2579 dev->groups = groups;
2580 dev->release = device_create_release;
2581 dev_set_drvdata(dev, drvdata);
2582
2583 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2584 if (retval)
2585 goto error;
2586
David Herrmannbbc780f2013-11-21 20:15:48 +01002587 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002588 if (retval)
2589 goto error;
2590
2591 return dev;
2592
2593error:
2594 put_device(dev);
2595 return ERR_PTR(retval);
2596}
2597
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002598/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002599 * device_create_vargs - creates a device and registers it with sysfs
2600 * @class: pointer to the struct class that this device should be registered to
2601 * @parent: pointer to the parent struct device of this new device, if any
2602 * @devt: the dev_t for the char device to be added
2603 * @drvdata: the data to be added to the device for callbacks
2604 * @fmt: string for the device's name
2605 * @args: va_list for the device's name
2606 *
2607 * This function can be used by char device classes. A struct device
2608 * will be created in sysfs, registered to the specified class.
2609 *
2610 * A "dev" file will be created, showing the dev_t for the device, if
2611 * the dev_t is not 0,0.
2612 * If a pointer to a parent struct device is passed in, the newly created
2613 * struct device will be a child of that device in sysfs.
2614 * The pointer to the struct device will be returned from the call.
2615 * Any further sysfs files that might be required can be created using this
2616 * pointer.
2617 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002618 * Returns &struct device pointer on success, or ERR_PTR() on error.
2619 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002620 * Note: the struct class passed to this function must have previously
2621 * been created with a call to class_create().
2622 */
2623struct device *device_create_vargs(struct class *class, struct device *parent,
2624 dev_t devt, void *drvdata, const char *fmt,
2625 va_list args)
2626{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002627 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2628 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002629}
2630EXPORT_SYMBOL_GPL(device_create_vargs);
2631
2632/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002633 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002634 * @class: pointer to the struct class that this device should be registered to
2635 * @parent: pointer to the parent struct device of this new device, if any
2636 * @devt: the dev_t for the char device to be added
2637 * @drvdata: the data to be added to the device for callbacks
2638 * @fmt: string for the device's name
2639 *
2640 * This function can be used by char device classes. A struct device
2641 * will be created in sysfs, registered to the specified class.
2642 *
2643 * A "dev" file will be created, showing the dev_t for the device, if
2644 * the dev_t is not 0,0.
2645 * If a pointer to a parent struct device is passed in, the newly created
2646 * struct device will be a child of that device in sysfs.
2647 * The pointer to the struct device will be returned from the call.
2648 * Any further sysfs files that might be required can be created using this
2649 * pointer.
2650 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002651 * Returns &struct device pointer on success, or ERR_PTR() on error.
2652 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002653 * Note: the struct class passed to this function must have previously
2654 * been created with a call to class_create().
2655 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002656struct device *device_create(struct class *class, struct device *parent,
2657 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002658{
2659 va_list vargs;
2660 struct device *dev;
2661
2662 va_start(vargs, fmt);
2663 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2664 va_end(vargs);
2665 return dev;
2666}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002667EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002668
Guenter Roeck39ef3112013-07-14 16:05:57 -07002669/**
2670 * device_create_with_groups - creates a device and registers it with sysfs
2671 * @class: pointer to the struct class that this device should be registered to
2672 * @parent: pointer to the parent struct device of this new device, if any
2673 * @devt: the dev_t for the char device to be added
2674 * @drvdata: the data to be added to the device for callbacks
2675 * @groups: NULL-terminated list of attribute groups to be created
2676 * @fmt: string for the device's name
2677 *
2678 * This function can be used by char device classes. A struct device
2679 * will be created in sysfs, registered to the specified class.
2680 * Additional attributes specified in the groups parameter will also
2681 * be created automatically.
2682 *
2683 * A "dev" file will be created, showing the dev_t for the device, if
2684 * the dev_t is not 0,0.
2685 * If a pointer to a parent struct device is passed in, the newly created
2686 * struct device will be a child of that device in sysfs.
2687 * The pointer to the struct device will be returned from the call.
2688 * Any further sysfs files that might be required can be created using this
2689 * pointer.
2690 *
2691 * Returns &struct device pointer on success, or ERR_PTR() on error.
2692 *
2693 * Note: the struct class passed to this function must have previously
2694 * been created with a call to class_create().
2695 */
2696struct device *device_create_with_groups(struct class *class,
2697 struct device *parent, dev_t devt,
2698 void *drvdata,
2699 const struct attribute_group **groups,
2700 const char *fmt, ...)
2701{
2702 va_list vargs;
2703 struct device *dev;
2704
2705 va_start(vargs, fmt);
2706 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2707 fmt, vargs);
2708 va_end(vargs);
2709 return dev;
2710}
2711EXPORT_SYMBOL_GPL(device_create_with_groups);
2712
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002713static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002714{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002715 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002716
Dave Youngcd354492008-01-28 16:56:11 +08002717 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002718}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002719
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002720/**
2721 * device_destroy - removes a device that was created with device_create()
2722 * @class: pointer to the struct class that this device was registered with
2723 * @devt: the dev_t of the device that was previously registered
2724 *
2725 * This call unregisters and cleans up a device that was created with a
2726 * call to device_create().
2727 */
2728void device_destroy(struct class *class, dev_t devt)
2729{
2730 struct device *dev;
2731
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002732 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002733 if (dev) {
2734 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002735 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002736 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002737}
2738EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002739
2740/**
2741 * device_rename - renames a device
2742 * @dev: the pointer to the struct device to be renamed
2743 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002744 *
2745 * It is the responsibility of the caller to provide mutual
2746 * exclusion between two different calls of device_rename
2747 * on the same device to ensure that new_name is valid and
2748 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002749 *
Timur Tabia5462512010-12-13 14:08:52 -06002750 * Note: Don't call this function. Currently, the networking layer calls this
2751 * function, but that will change. The following text from Kay Sievers offers
2752 * some insight:
2753 *
2754 * Renaming devices is racy at many levels, symlinks and other stuff are not
2755 * replaced atomically, and you get a "move" uevent, but it's not easy to
2756 * connect the event to the old and new device. Device nodes are not renamed at
2757 * all, there isn't even support for that in the kernel now.
2758 *
2759 * In the meantime, during renaming, your target name might be taken by another
2760 * driver, creating conflicts. Or the old name is taken directly after you
2761 * renamed it -- then you get events for the same DEVPATH, before you even see
2762 * the "move" event. It's just a mess, and nothing new should ever rely on
2763 * kernel device renaming. Besides that, it's not even implemented now for
2764 * other things than (driver-core wise very simple) network devices.
2765 *
2766 * We are currently about to change network renaming in udev to completely
2767 * disallow renaming of devices in the same namespace as the kernel uses,
2768 * because we can't solve the problems properly, that arise with swapping names
2769 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2770 * be allowed to some other name than eth[0-9]*, for the aforementioned
2771 * reasons.
2772 *
2773 * Make up a "real" name in the driver before you register anything, or add
2774 * some other attributes for userspace to find the device, or use udev to add
2775 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2776 * don't even want to get into that and try to implement the missing pieces in
2777 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002778 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002779int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002780{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002781 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002782 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002783 int error;
2784
2785 dev = get_device(dev);
2786 if (!dev)
2787 return -EINVAL;
2788
ethan.zhao69df7532013-10-13 22:12:35 +08002789 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002790
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002791 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002792 if (!old_device_name) {
2793 error = -ENOMEM;
2794 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002795 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002796
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002797 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002798 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2799 kobj, old_device_name,
2800 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002801 if (error)
2802 goto out;
2803 }
Kay Sievers39aba962010-09-04 22:33:14 -07002804
Tejun Heo4b30ee52013-09-11 22:29:06 -04002805 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002806 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002807 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002808
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002809out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002810 put_device(dev);
2811
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002812 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002813
2814 return error;
2815}
Johannes Berga2807db2007-02-28 12:38:31 +01002816EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002817
2818static int device_move_class_links(struct device *dev,
2819 struct device *old_parent,
2820 struct device *new_parent)
2821{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002822 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002823
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002824 if (old_parent)
2825 sysfs_remove_link(&dev->kobj, "device");
2826 if (new_parent)
2827 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2828 "device");
2829 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002830}
2831
2832/**
2833 * device_move - moves a device to a new parent
2834 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02002835 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002836 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002837 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002838int device_move(struct device *dev, struct device *new_parent,
2839 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002840{
2841 int error;
2842 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002843 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002844
2845 dev = get_device(dev);
2846 if (!dev)
2847 return -EINVAL;
2848
Cornelia Huckffa6a702009-03-04 12:44:00 +01002849 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002850 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002851 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002852 if (IS_ERR(new_parent_kobj)) {
2853 error = PTR_ERR(new_parent_kobj);
2854 put_device(new_parent);
2855 goto out;
2856 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002857
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002858 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2859 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002860 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002861 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002862 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002863 put_device(new_parent);
2864 goto out;
2865 }
2866 old_parent = dev->parent;
2867 dev->parent = new_parent;
2868 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002869 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002870 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002871 klist_add_tail(&dev->p->knode_parent,
2872 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002873 set_dev_node(dev, dev_to_node(new_parent));
2874 }
2875
Rabin Vincentbdd40342012-04-23 09:16:36 +02002876 if (dev->class) {
2877 error = device_move_class_links(dev, old_parent, new_parent);
2878 if (error) {
2879 /* We ignore errors on cleanup since we're hosed anyway... */
2880 device_move_class_links(dev, new_parent, old_parent);
2881 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2882 if (new_parent)
2883 klist_remove(&dev->p->knode_parent);
2884 dev->parent = old_parent;
2885 if (old_parent) {
2886 klist_add_tail(&dev->p->knode_parent,
2887 &old_parent->p->klist_children);
2888 set_dev_node(dev, dev_to_node(old_parent));
2889 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002890 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002891 cleanup_glue_dir(dev, new_parent_kobj);
2892 put_device(new_parent);
2893 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002894 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002895 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002896 switch (dpm_order) {
2897 case DPM_ORDER_NONE:
2898 break;
2899 case DPM_ORDER_DEV_AFTER_PARENT:
2900 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002901 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002902 break;
2903 case DPM_ORDER_PARENT_BEFORE_DEV:
2904 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002905 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002906 break;
2907 case DPM_ORDER_DEV_LAST:
2908 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002909 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002910 break;
2911 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002912
Cornelia Huck8a824722006-11-20 17:07:51 +01002913 put_device(old_parent);
2914out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002915 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002916 put_device(dev);
2917 return error;
2918}
Cornelia Huck8a824722006-11-20 17:07:51 +01002919EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002920
2921/**
2922 * device_shutdown - call ->shutdown() on each device to shutdown.
2923 */
2924void device_shutdown(void)
2925{
Benson Leungf123db82013-09-24 20:05:11 -07002926 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002927
Pingfan Liu3297c8f2018-07-19 13:14:58 +08002928 wait_for_device_probe();
2929 device_block_probing();
2930
Hugh Daschbach62458382010-03-22 10:36:37 -07002931 spin_lock(&devices_kset->list_lock);
2932 /*
2933 * Walk the devices list backward, shutting down each in turn.
2934 * Beware that device unplug events may also start pulling
2935 * devices offline, even as the system is shutting down.
2936 */
2937 while (!list_empty(&devices_kset->list)) {
2938 dev = list_entry(devices_kset->list.prev, struct device,
2939 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002940
2941 /*
2942 * hold reference count of device's parent to
2943 * prevent it from being freed because parent's
2944 * lock is to be held
2945 */
Benson Leungf123db82013-09-24 20:05:11 -07002946 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002947 get_device(dev);
2948 /*
2949 * Make sure the device is off the kset list, in the
2950 * event that dev->*->shutdown() doesn't remove it.
2951 */
2952 list_del_init(&dev->kobj.entry);
2953 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002954
Ming Leid1c6c032012-06-22 18:01:40 +08002955 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002956 if (parent)
2957 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002958 device_lock(dev);
2959
Alan Sternfe6b91f2011-12-06 23:24:52 +01002960 /* Don't allow any more runtime suspends */
2961 pm_runtime_get_noresume(dev);
2962 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002963
Michal Suchanek75216212017-08-11 15:44:43 +02002964 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07002965 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02002966 dev_info(dev, "shutdown_pre\n");
2967 dev->class->shutdown_pre(dev);
2968 }
2969 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002970 if (initcall_debug)
2971 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002972 dev->bus->shutdown(dev);
2973 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002974 if (initcall_debug)
2975 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002976 dev->driver->shutdown(dev);
2977 }
Ming Leid1c6c032012-06-22 18:01:40 +08002978
2979 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002980 if (parent)
2981 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002982
Hugh Daschbach62458382010-03-22 10:36:37 -07002983 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002984 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002985
2986 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002987 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002988 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002989}
Joe Perches99bcf212010-06-27 01:02:34 +00002990
2991/*
2992 * Device logging functions
2993 */
2994
2995#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002996static int
2997create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002998{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002999 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07003000 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003001
Kay Sieversc4e00da2012-05-03 02:29:59 +02003002 if (dev->class)
3003 subsys = dev->class->name;
3004 else if (dev->bus)
3005 subsys = dev->bus->name;
3006 else
Joe Perches798efc62012-09-12 20:11:29 -07003007 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003008
Joe Perches798efc62012-09-12 20:11:29 -07003009 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07003010 if (pos >= hdrlen)
3011 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003012
3013 /*
3014 * Add device identifier DEVICE=:
3015 * b12:8 block dev_t
3016 * c127:3 char dev_t
3017 * n8 netdev ifindex
3018 * +sound:card0 subsystem:devname
3019 */
3020 if (MAJOR(dev->devt)) {
3021 char c;
3022
3023 if (strcmp(subsys, "block") == 0)
3024 c = 'b';
3025 else
3026 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07003027 pos++;
3028 pos += snprintf(hdr + pos, hdrlen - pos,
3029 "DEVICE=%c%u:%u",
3030 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003031 } else if (strcmp(subsys, "net") == 0) {
3032 struct net_device *net = to_net_dev(dev);
3033
Joe Perches798efc62012-09-12 20:11:29 -07003034 pos++;
3035 pos += snprintf(hdr + pos, hdrlen - pos,
3036 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02003037 } else {
Joe Perches798efc62012-09-12 20:11:29 -07003038 pos++;
3039 pos += snprintf(hdr + pos, hdrlen - pos,
3040 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003041 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06003042
Ben Hutchings655e5b72014-08-26 00:34:44 -07003043 if (pos >= hdrlen)
3044 goto overflow;
3045
Joe Perches798efc62012-09-12 20:11:29 -07003046 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07003047
3048overflow:
3049 dev_WARN(dev, "device/subsystem name too long");
3050 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003051}
Joe Perches798efc62012-09-12 20:11:29 -07003052
Joe Perches05e4e5b2012-09-12 20:13:37 -07003053int dev_vprintk_emit(int level, const struct device *dev,
3054 const char *fmt, va_list args)
3055{
3056 char hdr[128];
3057 size_t hdrlen;
3058
3059 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3060
3061 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3062}
3063EXPORT_SYMBOL(dev_vprintk_emit);
3064
3065int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3066{
3067 va_list args;
3068 int r;
3069
3070 va_start(args, fmt);
3071
3072 r = dev_vprintk_emit(level, dev, fmt, args);
3073
3074 va_end(args);
3075
3076 return r;
3077}
3078EXPORT_SYMBOL(dev_printk_emit);
3079
Joe Perchesd1f10522014-12-25 15:07:04 -08003080static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07003081 struct va_format *vaf)
3082{
Joe Perchesd1f10522014-12-25 15:07:04 -08003083 if (dev)
3084 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3085 dev_driver_string(dev), dev_name(dev), vaf);
3086 else
3087 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003088}
Joe Perches99bcf212010-06-27 01:02:34 +00003089
Joe Perchesd1f10522014-12-25 15:07:04 -08003090void dev_printk(const char *level, const struct device *dev,
3091 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00003092{
3093 struct va_format vaf;
3094 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00003095
3096 va_start(args, fmt);
3097
3098 vaf.fmt = fmt;
3099 vaf.va = &args;
3100
Joe Perchesd1f10522014-12-25 15:07:04 -08003101 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003102
Joe Perches99bcf212010-06-27 01:02:34 +00003103 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00003104}
3105EXPORT_SYMBOL(dev_printk);
3106
3107#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08003108void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00003109{ \
3110 struct va_format vaf; \
3111 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003112 \
3113 va_start(args, fmt); \
3114 \
3115 vaf.fmt = fmt; \
3116 vaf.va = &args; \
3117 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003118 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003119 \
Joe Perches99bcf212010-06-27 01:02:34 +00003120 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003121} \
3122EXPORT_SYMBOL(func);
3123
Joe Perches663336e2018-05-09 08:15:46 -07003124define_dev_printk_level(_dev_emerg, KERN_EMERG);
3125define_dev_printk_level(_dev_alert, KERN_ALERT);
3126define_dev_printk_level(_dev_crit, KERN_CRIT);
3127define_dev_printk_level(_dev_err, KERN_ERR);
3128define_dev_printk_level(_dev_warn, KERN_WARNING);
3129define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00003130define_dev_printk_level(_dev_info, KERN_INFO);
3131
3132#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003133
3134static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3135{
3136 return fwnode && !IS_ERR(fwnode->secondary);
3137}
3138
3139/**
3140 * set_primary_fwnode - Change the primary firmware node of a given device.
3141 * @dev: Device to handle.
3142 * @fwnode: New primary firmware node of the device.
3143 *
3144 * Set the device's firmware node pointer to @fwnode, but if a secondary
3145 * firmware node of the device is present, preserve it.
3146 */
3147void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3148{
3149 if (fwnode) {
3150 struct fwnode_handle *fn = dev->fwnode;
3151
3152 if (fwnode_is_primary(fn))
3153 fn = fn->secondary;
3154
Mika Westerberg55f89a82015-11-30 17:11:39 +02003155 if (fn) {
3156 WARN_ON(fwnode->secondary);
3157 fwnode->secondary = fn;
3158 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003159 dev->fwnode = fwnode;
3160 } else {
3161 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3162 dev->fwnode->secondary : NULL;
3163 }
3164}
3165EXPORT_SYMBOL_GPL(set_primary_fwnode);
3166
3167/**
3168 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3169 * @dev: Device to handle.
3170 * @fwnode: New secondary firmware node of the device.
3171 *
3172 * If a primary firmware node of the device is present, set its secondary
3173 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3174 * @fwnode.
3175 */
3176void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3177{
3178 if (fwnode)
3179 fwnode->secondary = ERR_PTR(-ENODEV);
3180
3181 if (fwnode_is_primary(dev->fwnode))
3182 dev->fwnode->secondary = fwnode;
3183 else
3184 dev->fwnode = fwnode;
3185}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003186
3187/**
3188 * device_set_of_node_from_dev - reuse device-tree node of another device
3189 * @dev: device whose device-tree node is being set
3190 * @dev2: device whose device-tree node is being reused
3191 *
3192 * Takes another reference to the new device-tree node after first dropping
3193 * any reference held to the old node.
3194 */
3195void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3196{
3197 of_node_put(dev->of_node);
3198 dev->of_node = of_node_get(dev2->of_node);
3199 dev->of_node_reused = true;
3200}
3201EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);