blob: 0073b09bb99f9d0650abd11589fcb2285bc0b69e [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 *
Vivek Gautame88728f2018-06-27 18:20:55 +0530182 * If the DL_FLAG_AUTOREMOVE_CONSUMER is set, the link will be removed
183 * automatically when the consumer device driver unbinds from it.
184 * The combination of both DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_STATELESS
185 * set is invalid and will cause NULL to be returned.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100186 *
187 * A side effect of the link creation is re-ordering of dpm_list and the
188 * devices_kset list by moving the consumer device and all devices depending
189 * on it to the ends of these lists (that does not happen to devices that have
190 * not been registered when this function is called).
191 *
192 * The supplier device is required to be registered when this function is called
193 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100194 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100195 */
196struct device_link *device_link_add(struct device *consumer,
197 struct device *supplier, u32 flags)
198{
199 struct device_link *link;
200
201 if (!consumer || !supplier ||
Vivek Gautame88728f2018-06-27 18:20:55 +0530202 ((flags & DL_FLAG_STATELESS) &&
203 (flags & DL_FLAG_AUTOREMOVE_CONSUMER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100204 return NULL;
205
206 device_links_write_lock();
207 device_pm_lock();
208
209 /*
210 * If the supplier has not been fully registered yet or there is a
211 * reverse dependency between the consumer and the supplier already in
212 * the graph, return NULL.
213 */
214 if (!device_pm_initialized(supplier)
215 || device_is_dependent(consumer, supplier)) {
216 link = NULL;
217 goto out;
218 }
219
220 list_for_each_entry(link, &supplier->links.consumers, s_node)
Lukas Wunneread18c22018-02-10 19:27:12 +0100221 if (link->consumer == consumer) {
222 kref_get(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100223 goto out;
Lukas Wunneread18c22018-02-10 19:27:12 +0100224 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100225
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100226 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100227 if (!link)
228 goto out;
229
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100230 if (flags & DL_FLAG_PM_RUNTIME) {
231 if (flags & DL_FLAG_RPM_ACTIVE) {
232 if (pm_runtime_get_sync(supplier) < 0) {
233 pm_runtime_put_noidle(supplier);
234 kfree(link);
235 link = NULL;
236 goto out;
237 }
238 link->rpm_active = true;
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100239 }
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100240 pm_runtime_new_link(consumer);
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200241 /*
242 * If the link is being added by the consumer driver at probe
243 * time, balance the decrementation of the supplier's runtime PM
244 * usage counter after consumer probe in driver_probe_device().
245 */
246 if (consumer->links.status == DL_DEV_PROBING)
247 pm_runtime_get_noresume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100248 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100249 get_device(supplier);
250 link->supplier = supplier;
251 INIT_LIST_HEAD(&link->s_node);
252 get_device(consumer);
253 link->consumer = consumer;
254 INIT_LIST_HEAD(&link->c_node);
255 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100256 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100257
Lukas Wunner64df1142016-12-04 13:10:04 +0100258 /* Determine the initial link state. */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100259 if (flags & DL_FLAG_STATELESS) {
260 link->status = DL_STATE_NONE;
261 } else {
262 switch (supplier->links.status) {
263 case DL_DEV_DRIVER_BOUND:
264 switch (consumer->links.status) {
265 case DL_DEV_PROBING:
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100266 /*
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200267 * Some callers expect the link creation during
268 * consumer driver probe to resume the supplier
269 * even without DL_FLAG_RPM_ACTIVE.
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100270 */
271 if (flags & DL_FLAG_PM_RUNTIME)
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200272 pm_runtime_resume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100273
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100274 link->status = DL_STATE_CONSUMER_PROBE;
275 break;
276 case DL_DEV_DRIVER_BOUND:
277 link->status = DL_STATE_ACTIVE;
278 break;
279 default:
280 link->status = DL_STATE_AVAILABLE;
281 break;
282 }
283 break;
284 case DL_DEV_UNBINDING:
285 link->status = DL_STATE_SUPPLIER_UNBIND;
286 break;
287 default:
288 link->status = DL_STATE_DORMANT;
289 break;
290 }
291 }
292
293 /*
294 * Move the consumer and all of the devices depending on it to the end
295 * of dpm_list and the devices_kset list.
296 *
297 * It is necessary to hold dpm_list locked throughout all that or else
298 * we may end up suspending with a wrong ordering of it.
299 */
300 device_reorder_to_tail(consumer, NULL);
301
302 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
303 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
304
305 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
306
307 out:
308 device_pm_unlock();
309 device_links_write_unlock();
310 return link;
311}
312EXPORT_SYMBOL_GPL(device_link_add);
313
314static void device_link_free(struct device_link *link)
315{
316 put_device(link->consumer);
317 put_device(link->supplier);
318 kfree(link);
319}
320
321#ifdef CONFIG_SRCU
322static void __device_link_free_srcu(struct rcu_head *rhead)
323{
324 device_link_free(container_of(rhead, struct device_link, rcu_head));
325}
326
Lukas Wunneread18c22018-02-10 19:27:12 +0100327static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100328{
Lukas Wunneread18c22018-02-10 19:27:12 +0100329 struct device_link *link = container_of(kref, struct device_link, kref);
330
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100331 dev_info(link->consumer, "Dropping the link to %s\n",
332 dev_name(link->supplier));
333
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100334 if (link->flags & DL_FLAG_PM_RUNTIME)
335 pm_runtime_drop_link(link->consumer);
336
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100337 list_del_rcu(&link->s_node);
338 list_del_rcu(&link->c_node);
339 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
340}
341#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100342static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100343{
Lukas Wunneread18c22018-02-10 19:27:12 +0100344 struct device_link *link = container_of(kref, struct device_link, kref);
345
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100346 dev_info(link->consumer, "Dropping the link to %s\n",
347 dev_name(link->supplier));
348
Lukas Wunner433986c2018-02-10 19:13:58 +0100349 if (link->flags & DL_FLAG_PM_RUNTIME)
350 pm_runtime_drop_link(link->consumer);
351
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100352 list_del(&link->s_node);
353 list_del(&link->c_node);
354 device_link_free(link);
355}
356#endif /* !CONFIG_SRCU */
357
358/**
359 * device_link_del - Delete a link between two devices.
360 * @link: Device link to delete.
361 *
362 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100363 * PM. If the link was added multiple times, it needs to be deleted as often.
364 * Care is required for hotplugged devices: Their links are purged on removal
365 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100366 */
367void device_link_del(struct device_link *link)
368{
369 device_links_write_lock();
370 device_pm_lock();
Lukas Wunneread18c22018-02-10 19:27:12 +0100371 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100372 device_pm_unlock();
373 device_links_write_unlock();
374}
375EXPORT_SYMBOL_GPL(device_link_del);
376
pascal pailletd8842212018-07-05 14:25:56 +0000377/**
378 * device_link_remove - remove a link between two devices.
379 * @consumer: Consumer end of the link.
380 * @supplier: Supplier end of the link.
381 *
382 * The caller must ensure proper synchronization of this function with runtime
383 * PM.
384 */
385void device_link_remove(void *consumer, struct device *supplier)
386{
387 struct device_link *link;
388
389 if (WARN_ON(consumer == supplier))
390 return;
391
392 device_links_write_lock();
393 device_pm_lock();
394
395 list_for_each_entry(link, &supplier->links.consumers, s_node) {
396 if (link->consumer == consumer) {
397 kref_put(&link->kref, __device_link_del);
398 break;
399 }
400 }
401
402 device_pm_unlock();
403 device_links_write_unlock();
404}
405EXPORT_SYMBOL_GPL(device_link_remove);
406
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100407static void device_links_missing_supplier(struct device *dev)
408{
409 struct device_link *link;
410
411 list_for_each_entry(link, &dev->links.suppliers, c_node)
412 if (link->status == DL_STATE_CONSUMER_PROBE)
413 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
414}
415
416/**
417 * device_links_check_suppliers - Check presence of supplier drivers.
418 * @dev: Consumer device.
419 *
420 * Check links from this device to any suppliers. Walk the list of the device's
421 * links to suppliers and see if all of them are available. If not, simply
422 * return -EPROBE_DEFER.
423 *
424 * We need to guarantee that the supplier will not go away after the check has
425 * been positive here. It only can go away in __device_release_driver() and
426 * that function checks the device's links to consumers. This means we need to
427 * mark the link as "consumer probe in progress" to make the supplier removal
428 * wait for us to complete (or bad things may happen).
429 *
430 * Links with the DL_FLAG_STATELESS flag set are ignored.
431 */
432int device_links_check_suppliers(struct device *dev)
433{
434 struct device_link *link;
435 int ret = 0;
436
437 device_links_write_lock();
438
439 list_for_each_entry(link, &dev->links.suppliers, c_node) {
440 if (link->flags & DL_FLAG_STATELESS)
441 continue;
442
443 if (link->status != DL_STATE_AVAILABLE) {
444 device_links_missing_supplier(dev);
445 ret = -EPROBE_DEFER;
446 break;
447 }
448 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
449 }
450 dev->links.status = DL_DEV_PROBING;
451
452 device_links_write_unlock();
453 return ret;
454}
455
456/**
457 * device_links_driver_bound - Update device links after probing its driver.
458 * @dev: Device to update the links for.
459 *
460 * The probe has been successful, so update links from this device to any
461 * consumers by changing their status to "available".
462 *
463 * Also change the status of @dev's links to suppliers to "active".
464 *
465 * Links with the DL_FLAG_STATELESS flag set are ignored.
466 */
467void device_links_driver_bound(struct device *dev)
468{
469 struct device_link *link;
470
471 device_links_write_lock();
472
473 list_for_each_entry(link, &dev->links.consumers, s_node) {
474 if (link->flags & DL_FLAG_STATELESS)
475 continue;
476
477 WARN_ON(link->status != DL_STATE_DORMANT);
478 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
479 }
480
481 list_for_each_entry(link, &dev->links.suppliers, c_node) {
482 if (link->flags & DL_FLAG_STATELESS)
483 continue;
484
485 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
486 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
487 }
488
489 dev->links.status = DL_DEV_DRIVER_BOUND;
490
491 device_links_write_unlock();
492}
493
494/**
495 * __device_links_no_driver - Update links of a device without a driver.
496 * @dev: Device without a drvier.
497 *
498 * Delete all non-persistent links from this device to any suppliers.
499 *
500 * Persistent links stay around, but their status is changed to "available",
501 * unless they already are in the "supplier unbind in progress" state in which
502 * case they need not be updated.
503 *
504 * Links with the DL_FLAG_STATELESS flag set are ignored.
505 */
506static void __device_links_no_driver(struct device *dev)
507{
508 struct device_link *link, *ln;
509
510 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
511 if (link->flags & DL_FLAG_STATELESS)
512 continue;
513
Vivek Gautame88728f2018-06-27 18:20:55 +0530514 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Lukas Wunneread18c22018-02-10 19:27:12 +0100515 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100516 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
517 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
518 }
519
520 dev->links.status = DL_DEV_NO_DRIVER;
521}
522
523void device_links_no_driver(struct device *dev)
524{
525 device_links_write_lock();
526 __device_links_no_driver(dev);
527 device_links_write_unlock();
528}
529
530/**
531 * device_links_driver_cleanup - Update links after driver removal.
532 * @dev: Device whose driver has just gone away.
533 *
534 * Update links to consumers for @dev by changing their status to "dormant" and
535 * invoke %__device_links_no_driver() to update links to suppliers for it as
536 * appropriate.
537 *
538 * Links with the DL_FLAG_STATELESS flag set are ignored.
539 */
540void device_links_driver_cleanup(struct device *dev)
541{
542 struct device_link *link;
543
544 device_links_write_lock();
545
546 list_for_each_entry(link, &dev->links.consumers, s_node) {
547 if (link->flags & DL_FLAG_STATELESS)
548 continue;
549
Vivek Gautame88728f2018-06-27 18:20:55 +0530550 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100551 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530552
553 /*
554 * autoremove the links between this @dev and its consumer
555 * devices that are not active, i.e. where the link state
556 * has moved to DL_STATE_SUPPLIER_UNBIND.
557 */
558 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
559 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
560 kref_put(&link->kref, __device_link_del);
561
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100562 WRITE_ONCE(link->status, DL_STATE_DORMANT);
563 }
564
565 __device_links_no_driver(dev);
566
567 device_links_write_unlock();
568}
569
570/**
571 * device_links_busy - Check if there are any busy links to consumers.
572 * @dev: Device to check.
573 *
574 * Check each consumer of the device and return 'true' if its link's status
575 * is one of "consumer probe" or "active" (meaning that the given consumer is
576 * probing right now or its driver is present). Otherwise, change the link
577 * state to "supplier unbind" to prevent the consumer from being probed
578 * successfully going forward.
579 *
580 * Return 'false' if there are no probing or active consumers.
581 *
582 * Links with the DL_FLAG_STATELESS flag set are ignored.
583 */
584bool device_links_busy(struct device *dev)
585{
586 struct device_link *link;
587 bool ret = false;
588
589 device_links_write_lock();
590
591 list_for_each_entry(link, &dev->links.consumers, s_node) {
592 if (link->flags & DL_FLAG_STATELESS)
593 continue;
594
595 if (link->status == DL_STATE_CONSUMER_PROBE
596 || link->status == DL_STATE_ACTIVE) {
597 ret = true;
598 break;
599 }
600 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
601 }
602
603 dev->links.status = DL_DEV_UNBINDING;
604
605 device_links_write_unlock();
606 return ret;
607}
608
609/**
610 * device_links_unbind_consumers - Force unbind consumers of the given device.
611 * @dev: Device to unbind the consumers of.
612 *
613 * Walk the list of links to consumers for @dev and if any of them is in the
614 * "consumer probe" state, wait for all device probes in progress to complete
615 * and start over.
616 *
617 * If that's not the case, change the status of the link to "supplier unbind"
618 * and check if the link was in the "active" state. If so, force the consumer
619 * driver to unbind and start over (the consumer will not re-probe as we have
620 * changed the state of the link already).
621 *
622 * Links with the DL_FLAG_STATELESS flag set are ignored.
623 */
624void device_links_unbind_consumers(struct device *dev)
625{
626 struct device_link *link;
627
628 start:
629 device_links_write_lock();
630
631 list_for_each_entry(link, &dev->links.consumers, s_node) {
632 enum device_link_state status;
633
634 if (link->flags & DL_FLAG_STATELESS)
635 continue;
636
637 status = link->status;
638 if (status == DL_STATE_CONSUMER_PROBE) {
639 device_links_write_unlock();
640
641 wait_for_device_probe();
642 goto start;
643 }
644 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
645 if (status == DL_STATE_ACTIVE) {
646 struct device *consumer = link->consumer;
647
648 get_device(consumer);
649
650 device_links_write_unlock();
651
652 device_release_driver_internal(consumer, NULL,
653 consumer->parent);
654 put_device(consumer);
655 goto start;
656 }
657 }
658
659 device_links_write_unlock();
660}
661
662/**
663 * device_links_purge - Delete existing links to other devices.
664 * @dev: Target device.
665 */
666static void device_links_purge(struct device *dev)
667{
668 struct device_link *link, *ln;
669
670 /*
671 * Delete all of the remaining links from this device to any other
672 * devices (either consumers or suppliers).
673 */
674 device_links_write_lock();
675
676 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
677 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100678 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100679 }
680
681 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
682 WARN_ON(link->status != DL_STATE_DORMANT &&
683 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100684 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100685 }
686
687 device_links_write_unlock();
688}
689
690/* Device links support end. */
691
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800692int (*platform_notify)(struct device *dev) = NULL;
693int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700694static struct kobject *dev_kobj;
695struct kobject *sysfs_dev_char_kobj;
696struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200698static DEFINE_MUTEX(device_hotplug_lock);
699
700void lock_device_hotplug(void)
701{
702 mutex_lock(&device_hotplug_lock);
703}
704
705void unlock_device_hotplug(void)
706{
707 mutex_unlock(&device_hotplug_lock);
708}
709
710int lock_device_hotplug_sysfs(void)
711{
712 if (mutex_trylock(&device_hotplug_lock))
713 return 0;
714
715 /* Avoid busy looping (5 ms of sleep should do). */
716 msleep(5);
717 return restart_syscall();
718}
719
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800720#ifdef CONFIG_BLOCK
721static inline int device_is_not_partition(struct device *dev)
722{
723 return !(dev->type == &part_type);
724}
725#else
726static inline int device_is_not_partition(struct device *dev)
727{
728 return 1;
729}
730#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Heikki Krogerus07de0e82018-11-09 17:21:34 +0300732static int
733device_platform_notify(struct device *dev, enum kobject_action action)
734{
Heikki Krogerus7847a142018-11-09 17:21:35 +0300735 int ret;
736
737 ret = acpi_platform_notify(dev, action);
738 if (ret)
739 return ret;
740
Heikki Krogerus59abd832018-11-09 17:21:36 +0300741 ret = software_node_notify(dev, action);
742 if (ret)
743 return ret;
744
Heikki Krogerus07de0e82018-11-09 17:21:34 +0300745 if (platform_notify && action == KOBJ_ADD)
746 platform_notify(dev);
747 else if (platform_notify_remove && action == KOBJ_REMOVE)
748 platform_notify_remove(dev);
749 return 0;
750}
751
Alan Stern3e956372006-06-16 17:10:48 -0400752/**
753 * dev_driver_string - Return a device's driver name, if at all possible
754 * @dev: struct device to get the name of
755 *
756 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800757 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400758 * it is attached to. If it is not attached to a bus either, an empty
759 * string will be returned.
760 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700761const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400762{
Alan Stern35899722009-12-04 11:06:57 -0500763 struct device_driver *drv;
764
765 /* dev->driver can change to NULL underneath us because of unbinding,
766 * so be careful about accessing it. dev->bus and dev->class should
767 * never change once they are set, so they don't need special care.
768 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700769 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500770 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100771 (dev->bus ? dev->bus->name :
772 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400773}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600774EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
777
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800778static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
779 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800781 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200782 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500783 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400786 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800787 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900788 printk("dev_attr_show: %pS returned bad count\n",
789 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return ret;
792}
793
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800794static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
795 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800797 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200798 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500799 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400802 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return ret;
804}
805
Emese Revfy52cf25d2010-01-19 02:58:23 +0100806static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .show = dev_attr_show,
808 .store = dev_attr_store,
809};
810
Kay Sieversca22e562011-12-14 14:29:38 -0800811#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
812
813ssize_t device_store_ulong(struct device *dev,
814 struct device_attribute *attr,
815 const char *buf, size_t size)
816{
817 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -0800818 int ret;
819 unsigned long new;
820
821 ret = kstrtoul(buf, 0, &new);
822 if (ret)
823 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -0800824 *(unsigned long *)(ea->var) = new;
825 /* Always return full write size even if we didn't consume all */
826 return size;
827}
828EXPORT_SYMBOL_GPL(device_store_ulong);
829
830ssize_t device_show_ulong(struct device *dev,
831 struct device_attribute *attr,
832 char *buf)
833{
834 struct dev_ext_attribute *ea = to_ext_attr(attr);
835 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
836}
837EXPORT_SYMBOL_GPL(device_show_ulong);
838
839ssize_t device_store_int(struct device *dev,
840 struct device_attribute *attr,
841 const char *buf, size_t size)
842{
843 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -0800844 int ret;
845 long new;
846
847 ret = kstrtol(buf, 0, &new);
848 if (ret)
849 return ret;
850
851 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -0800852 return -EINVAL;
853 *(int *)(ea->var) = new;
854 /* Always return full write size even if we didn't consume all */
855 return size;
856}
857EXPORT_SYMBOL_GPL(device_store_int);
858
859ssize_t device_show_int(struct device *dev,
860 struct device_attribute *attr,
861 char *buf)
862{
863 struct dev_ext_attribute *ea = to_ext_attr(attr);
864
865 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
866}
867EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Borislav Petkov91872392012-10-09 19:52:05 +0200869ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
870 const char *buf, size_t size)
871{
872 struct dev_ext_attribute *ea = to_ext_attr(attr);
873
874 if (strtobool(buf, ea->var) < 0)
875 return -EINVAL;
876
877 return size;
878}
879EXPORT_SYMBOL_GPL(device_store_bool);
880
881ssize_t device_show_bool(struct device *dev, 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", *(bool *)(ea->var));
887}
888EXPORT_SYMBOL_GPL(device_show_bool);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400891 * device_release - free device structure.
892 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400894 * This is called once the reference count for the object
895 * reaches 0. We forward the call to the device's release
896 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800898static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200900 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800901 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Ming Leia525a3d2012-07-25 01:42:29 +0800903 /*
904 * Some platform devices are driven without driver attached
905 * and managed resources may have been acquired. Make sure
906 * all resources are released.
907 *
908 * Drivers still can add resources into device after device
909 * is deleted but alive, so release devres here to avoid
910 * possible memory leak.
911 */
912 devres_release_all(dev);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (dev->release)
915 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200916 else if (dev->type && dev->type->release)
917 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700918 else if (dev->class && dev->class->dev_release)
919 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700920 else
Ezequiel Garcia186bddb2018-12-03 13:44:35 -0300921 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 +0100922 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800923 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700926static const void *device_namespace(struct kobject *kobj)
927{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200928 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700929 const void *ns = NULL;
930
931 if (dev->class && dev->class->ns_type)
932 ns = dev->class->namespace(dev);
933
934 return ns;
935}
936
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000937static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
938{
939 struct device *dev = kobj_to_dev(kobj);
940
941 if (dev->class && dev->class->get_ownership)
942 dev->class->get_ownership(dev, uid, gid);
943}
944
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600945static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 .release = device_release,
947 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700948 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000949 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950};
951
952
Kay Sievers312c0042005-11-16 09:00:00 +0100953static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct kobj_type *ktype = get_ktype(kobj);
956
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600957 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200958 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (dev->bus)
960 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700961 if (dev->class)
962 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 return 0;
965}
966
Kay Sievers312c0042005-11-16 09:00:00 +0100967static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200969 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700971 if (dev->bus)
972 return dev->bus->name;
973 if (dev->class)
974 return dev->class->name;
975 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Kay Sievers7eff2e72007-08-14 15:15:12 +0200978static int dev_uevent(struct kset *kset, struct kobject *kobj,
979 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200981 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int retval = 0;
983
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200984 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700985 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200986 const char *tmp;
987 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400988 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700989 kuid_t uid = GLOBAL_ROOT_UID;
990 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200991
Kay Sievers7eff2e72007-08-14 15:15:12 +0200992 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
993 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700994 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200995 if (name) {
996 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200997 if (mode)
998 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700999 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1000 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1001 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1002 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001003 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001004 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001005 }
1006
Kay Sievers414264f2007-03-12 21:08:57 +01001007 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001008 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01001009
Kay Sievers239378f2006-10-07 21:54:55 +02001010 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001011 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02001012
Grant Likely07d57a32012-02-01 11:22:22 -07001013 /* Add common DT information about the device */
1014 of_device_uevent(dev, env);
1015
Kay Sievers7eff2e72007-08-14 15:15:12 +02001016 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01001017 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001018 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001019 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001020 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001021 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023
Kay Sievers7eff2e72007-08-14 15:15:12 +02001024 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001025 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001026 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001027 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001028 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001029 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001030 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001031 }
1032
Stefan Weileef35c22010-08-06 21:11:15 +02001033 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02001034 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001035 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001036 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001037 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001038 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001039 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001040 }
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return retval;
1043}
1044
Emese Revfy9cd43612009-12-31 14:52:51 +01001045static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01001046 .filter = dev_uevent_filter,
1047 .name = dev_uevent_name,
1048 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049};
1050
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001051static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02001052 char *buf)
1053{
1054 struct kobject *top_kobj;
1055 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001056 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02001057 int i;
1058 size_t count = 0;
1059 int retval;
1060
1061 /* search the kset, the device belongs to */
1062 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001063 while (!top_kobj->kset && top_kobj->parent)
1064 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02001065 if (!top_kobj->kset)
1066 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001067
Kay Sievers16574dc2007-04-06 01:40:38 +02001068 kset = top_kobj->kset;
1069 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1070 goto out;
1071
1072 /* respect filter */
1073 if (kset->uevent_ops && kset->uevent_ops->filter)
1074 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1075 goto out;
1076
Kay Sievers7eff2e72007-08-14 15:15:12 +02001077 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1078 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001079 return -ENOMEM;
1080
Kay Sievers16574dc2007-04-06 01:40:38 +02001081 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001082 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001083 if (retval)
1084 goto out;
1085
1086 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001087 for (i = 0; i < env->envp_idx; i++)
1088 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001089out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001090 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001091 return count;
1092}
1093
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001094static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001095 const char *buf, size_t count)
1096{
Peter Rajnohadf44b472018-12-05 12:27:44 +01001097 int rc;
1098
1099 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1100
1101 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001102 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01001103 return rc;
1104 }
Kay Sievers60a96a52007-07-08 22:29:26 +02001105
Kay Sieversa7fd6702005-10-01 14:49:43 +02001106 return count;
1107}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001108static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001109
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001110static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001111 char *buf)
1112{
1113 bool val;
1114
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001115 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001116 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001117 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001118 return sprintf(buf, "%u\n", val);
1119}
1120
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001121static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001122 const char *buf, size_t count)
1123{
1124 bool val;
1125 int ret;
1126
1127 ret = strtobool(buf, &val);
1128 if (ret < 0)
1129 return ret;
1130
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001131 ret = lock_device_hotplug_sysfs();
1132 if (ret)
1133 return ret;
1134
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001135 ret = val ? device_online(dev) : device_offline(dev);
1136 unlock_device_hotplug();
1137 return ret < 0 ? ret : count;
1138}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001139static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001140
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001141int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001142{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001143 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001144}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001145EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001146
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001147void device_remove_groups(struct device *dev,
1148 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001149{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001150 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001151}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001152EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001153
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001154union device_attr_group_devres {
1155 const struct attribute_group *group;
1156 const struct attribute_group **groups;
1157};
1158
1159static int devm_attr_group_match(struct device *dev, void *res, void *data)
1160{
1161 return ((union device_attr_group_devres *)res)->group == data;
1162}
1163
1164static void devm_attr_group_remove(struct device *dev, void *res)
1165{
1166 union device_attr_group_devres *devres = res;
1167 const struct attribute_group *group = devres->group;
1168
1169 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1170 sysfs_remove_group(&dev->kobj, group);
1171}
1172
1173static void devm_attr_groups_remove(struct device *dev, void *res)
1174{
1175 union device_attr_group_devres *devres = res;
1176 const struct attribute_group **groups = devres->groups;
1177
1178 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1179 sysfs_remove_groups(&dev->kobj, groups);
1180}
1181
1182/**
1183 * devm_device_add_group - given a device, create a managed attribute group
1184 * @dev: The device to create the group for
1185 * @grp: The attribute group to create
1186 *
1187 * This function creates a group for the first time. It will explicitly
1188 * warn and error if any of the attribute files being created already exist.
1189 *
1190 * Returns 0 on success or error code on failure.
1191 */
1192int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1193{
1194 union device_attr_group_devres *devres;
1195 int error;
1196
1197 devres = devres_alloc(devm_attr_group_remove,
1198 sizeof(*devres), GFP_KERNEL);
1199 if (!devres)
1200 return -ENOMEM;
1201
1202 error = sysfs_create_group(&dev->kobj, grp);
1203 if (error) {
1204 devres_free(devres);
1205 return error;
1206 }
1207
1208 devres->group = grp;
1209 devres_add(dev, devres);
1210 return 0;
1211}
1212EXPORT_SYMBOL_GPL(devm_device_add_group);
1213
1214/**
1215 * devm_device_remove_group: remove a managed group from a device
1216 * @dev: device to remove the group from
1217 * @grp: group to remove
1218 *
1219 * This function removes a group of attributes from a device. The attributes
1220 * previously have to have been created for this group, otherwise it will fail.
1221 */
1222void devm_device_remove_group(struct device *dev,
1223 const struct attribute_group *grp)
1224{
1225 WARN_ON(devres_release(dev, devm_attr_group_remove,
1226 devm_attr_group_match,
1227 /* cast away const */ (void *)grp));
1228}
1229EXPORT_SYMBOL_GPL(devm_device_remove_group);
1230
1231/**
1232 * devm_device_add_groups - create a bunch of managed attribute groups
1233 * @dev: The device to create the group for
1234 * @groups: The attribute groups to create, NULL terminated
1235 *
1236 * This function creates a bunch of managed attribute groups. If an error
1237 * occurs when creating a group, all previously created groups will be
1238 * removed, unwinding everything back to the original state when this
1239 * function was called. It will explicitly warn and error if any of the
1240 * attribute files being created already exist.
1241 *
1242 * Returns 0 on success or error code from sysfs_create_group on failure.
1243 */
1244int devm_device_add_groups(struct device *dev,
1245 const struct attribute_group **groups)
1246{
1247 union device_attr_group_devres *devres;
1248 int error;
1249
1250 devres = devres_alloc(devm_attr_groups_remove,
1251 sizeof(*devres), GFP_KERNEL);
1252 if (!devres)
1253 return -ENOMEM;
1254
1255 error = sysfs_create_groups(&dev->kobj, groups);
1256 if (error) {
1257 devres_free(devres);
1258 return error;
1259 }
1260
1261 devres->groups = groups;
1262 devres_add(dev, devres);
1263 return 0;
1264}
1265EXPORT_SYMBOL_GPL(devm_device_add_groups);
1266
1267/**
1268 * devm_device_remove_groups - remove a list of managed groups
1269 *
1270 * @dev: The device for the groups to be removed from
1271 * @groups: NULL terminated list of groups to be removed
1272 *
1273 * If groups is not NULL, remove the specified groups from the device.
1274 */
1275void devm_device_remove_groups(struct device *dev,
1276 const struct attribute_group **groups)
1277{
1278 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1279 devm_attr_group_match,
1280 /* cast away const */ (void *)groups));
1281}
1282EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001284static int device_add_attrs(struct device *dev)
1285{
1286 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001287 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001288 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001289
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001290 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001291 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001292 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001293 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001294 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001295
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001296 if (type) {
1297 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001298 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001299 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001300 }
1301
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001302 error = device_add_groups(dev, dev->groups);
1303 if (error)
1304 goto err_remove_type_groups;
1305
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001306 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001307 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001308 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001309 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001310 }
1311
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001312 return 0;
1313
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001314 err_remove_dev_groups:
1315 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001316 err_remove_type_groups:
1317 if (type)
1318 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001319 err_remove_class_groups:
1320 if (class)
1321 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001322
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001323 return error;
1324}
1325
1326static void device_remove_attrs(struct device *dev)
1327{
1328 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001329 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001330
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001331 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001332 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001333
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001334 if (type)
1335 device_remove_groups(dev, type->groups);
1336
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001337 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001338 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001339}
1340
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001341static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001342 char *buf)
1343{
1344 return print_dev_t(buf, dev->devt);
1345}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001346static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001347
Kay Sieversca22e562011-12-14 14:29:38 -08001348/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001349struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001352 * devices_kset_move_before - Move device in the devices_kset's list.
1353 * @deva: Device to move.
1354 * @devb: Device @deva should come before.
1355 */
1356static void devices_kset_move_before(struct device *deva, struct device *devb)
1357{
1358 if (!devices_kset)
1359 return;
1360 pr_debug("devices_kset: Moving %s before %s\n",
1361 dev_name(deva), dev_name(devb));
1362 spin_lock(&devices_kset->list_lock);
1363 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1364 spin_unlock(&devices_kset->list_lock);
1365}
1366
1367/**
1368 * devices_kset_move_after - Move device in the devices_kset's list.
1369 * @deva: Device to move
1370 * @devb: Device @deva should come after.
1371 */
1372static void devices_kset_move_after(struct device *deva, struct device *devb)
1373{
1374 if (!devices_kset)
1375 return;
1376 pr_debug("devices_kset: Moving %s after %s\n",
1377 dev_name(deva), dev_name(devb));
1378 spin_lock(&devices_kset->list_lock);
1379 list_move(&deva->kobj.entry, &devb->kobj.entry);
1380 spin_unlock(&devices_kset->list_lock);
1381}
1382
1383/**
1384 * devices_kset_move_last - move the device to the end of devices_kset's list.
1385 * @dev: device to move
1386 */
1387void devices_kset_move_last(struct device *dev)
1388{
1389 if (!devices_kset)
1390 return;
1391 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1392 spin_lock(&devices_kset->list_lock);
1393 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1394 spin_unlock(&devices_kset->list_lock);
1395}
1396
1397/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001398 * device_create_file - create sysfs attribute file for device.
1399 * @dev: device.
1400 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001402int device_create_file(struct device *dev,
1403 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001406
1407 if (dev) {
1408 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001409 "Attribute %s: write permission without 'store'\n",
1410 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001411 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001412 "Attribute %s: read permission without 'show'\n",
1413 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001415 }
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return error;
1418}
David Graham White86df2682013-07-21 20:41:14 -04001419EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001422 * device_remove_file - remove sysfs attribute file.
1423 * @dev: device.
1424 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001426void device_remove_file(struct device *dev,
1427 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001429 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
David Graham White86df2682013-07-21 20:41:14 -04001432EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001434/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001435 * device_remove_file_self - remove sysfs attribute file from its own method.
1436 * @dev: device.
1437 * @attr: device attribute descriptor.
1438 *
1439 * See kernfs_remove_self() for details.
1440 */
1441bool device_remove_file_self(struct device *dev,
1442 const struct device_attribute *attr)
1443{
1444 if (dev)
1445 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1446 else
1447 return false;
1448}
1449EXPORT_SYMBOL_GPL(device_remove_file_self);
1450
1451/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001452 * device_create_bin_file - create sysfs binary attribute file for device.
1453 * @dev: device.
1454 * @attr: device binary attribute descriptor.
1455 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001456int device_create_bin_file(struct device *dev,
1457 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001458{
1459 int error = -EINVAL;
1460 if (dev)
1461 error = sysfs_create_bin_file(&dev->kobj, attr);
1462 return error;
1463}
1464EXPORT_SYMBOL_GPL(device_create_bin_file);
1465
1466/**
1467 * device_remove_bin_file - remove sysfs binary attribute file
1468 * @dev: device.
1469 * @attr: device binary attribute descriptor.
1470 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001471void device_remove_bin_file(struct device *dev,
1472 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001473{
1474 if (dev)
1475 sysfs_remove_bin_file(&dev->kobj, attr);
1476}
1477EXPORT_SYMBOL_GPL(device_remove_bin_file);
1478
James Bottomley34bb61f2005-09-06 16:56:51 -07001479static void klist_children_get(struct klist_node *n)
1480{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001481 struct device_private *p = to_device_private_parent(n);
1482 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001483
1484 get_device(dev);
1485}
1486
1487static void klist_children_put(struct klist_node *n)
1488{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001489 struct device_private *p = to_device_private_parent(n);
1490 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001491
1492 put_device(dev);
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001496 * device_initialize - init device structure.
1497 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *
Cornelia Huck57394112008-09-03 18:26:40 +02001499 * This prepares the device for use by other layers by initializing
1500 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001501 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001502 * that function, though it can also be called separately, so one
1503 * may use @dev's fields. In particular, get_device()/put_device()
1504 * may be used for reference counting of @dev after calling this
1505 * function.
1506 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001507 * All fields in @dev must be initialized by the caller to 0, except
1508 * for those explicitly set to some other value. The simplest
1509 * approach is to use kzalloc() to allocate the structure containing
1510 * @dev.
1511 *
Cornelia Huck57394112008-09-03 18:26:40 +02001512 * NOTE: Use put_device() to give up your reference instead of freeing
1513 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515void device_initialize(struct device *dev)
1516{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001517 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001518 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001520 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001521 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001522 spin_lock_init(&dev->devres_lock);
1523 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001524 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001525 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001526#ifdef CONFIG_GENERIC_MSI_IRQ
1527 INIT_LIST_HEAD(&dev->msi_list);
1528#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001529 INIT_LIST_HEAD(&dev->links.consumers);
1530 INIT_LIST_HEAD(&dev->links.suppliers);
1531 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
David Graham White86df2682013-07-21 20:41:14 -04001533EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Tejun Heod73ce002013-03-12 11:30:05 -07001535struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001536{
Kay Sievers86406242007-03-14 03:25:56 +01001537 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001538
Kay Sievers86406242007-03-14 03:25:56 +01001539 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001540 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001541 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001542
Kay Sievers86406242007-03-14 03:25:56 +01001543 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001544}
1545
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001546struct class_dir {
1547 struct kobject kobj;
1548 struct class *class;
1549};
1550
1551#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1552
1553static void class_dir_release(struct kobject *kobj)
1554{
1555 struct class_dir *dir = to_class_dir(kobj);
1556 kfree(dir);
1557}
1558
1559static const
1560struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1561{
1562 struct class_dir *dir = to_class_dir(kobj);
1563 return dir->class->ns_type;
1564}
1565
1566static struct kobj_type class_dir_ktype = {
1567 .release = class_dir_release,
1568 .sysfs_ops = &kobj_sysfs_ops,
1569 .child_ns_type = class_dir_child_ns_type
1570};
1571
1572static struct kobject *
1573class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1574{
1575 struct class_dir *dir;
1576 int retval;
1577
1578 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1579 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001580 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001581
1582 dir->class = class;
1583 kobject_init(&dir->kobj, &class_dir_ktype);
1584
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001585 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001586
1587 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1588 if (retval < 0) {
1589 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001590 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001591 }
1592 return &dir->kobj;
1593}
1594
Yijing Wange4a60d12014-11-07 12:05:49 +08001595static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001596
Kay Sieversda231fd2007-11-21 17:29:15 +01001597static struct kobject *get_device_parent(struct device *dev,
1598 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001599{
Kay Sievers86406242007-03-14 03:25:56 +01001600 if (dev->class) {
1601 struct kobject *kobj = NULL;
1602 struct kobject *parent_kobj;
1603 struct kobject *k;
1604
Randy Dunlapead454f2010-09-24 14:36:49 -07001605#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001606 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001607 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001608 if (parent && parent->class == &block_class)
1609 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001610 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001611 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001612#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001613
Kay Sievers86406242007-03-14 03:25:56 +01001614 /*
1615 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001616 * Class-devices with a non class-device as parent, live
1617 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001618 */
1619 if (parent == NULL)
1620 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001621 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001622 return &parent->kobj;
1623 else
1624 parent_kobj = &parent->kobj;
1625
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001626 mutex_lock(&gdp_mutex);
1627
Kay Sievers86406242007-03-14 03:25:56 +01001628 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001629 spin_lock(&dev->class->p->glue_dirs.list_lock);
1630 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001631 if (k->parent == parent_kobj) {
1632 kobj = kobject_get(k);
1633 break;
1634 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001635 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001636 if (kobj) {
1637 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001638 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001639 }
Kay Sievers86406242007-03-14 03:25:56 +01001640
1641 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001642 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001643 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001644 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001645 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001646 }
1647
Kay Sieversca22e562011-12-14 14:29:38 -08001648 /* subsystems can specify a default root directory for their devices */
1649 if (!parent && dev->bus && dev->bus->dev_root)
1650 return &dev->bus->dev_root->kobj;
1651
Kay Sievers86406242007-03-14 03:25:56 +01001652 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001653 return &parent->kobj;
1654 return NULL;
1655}
Kay Sieversda231fd2007-11-21 17:29:15 +01001656
Ming Leicebf8fd2016-07-10 19:27:36 +08001657static inline bool live_in_glue_dir(struct kobject *kobj,
1658 struct device *dev)
1659{
1660 if (!kobj || !dev->class ||
1661 kobj->kset != &dev->class->p->glue_dirs)
1662 return false;
1663 return true;
1664}
1665
1666static inline struct kobject *get_glue_dir(struct device *dev)
1667{
1668 return dev->kobj.parent;
1669}
1670
1671/*
1672 * make sure cleaning up dir as the last step, we need to make
1673 * sure .release handler of kobject is run with holding the
1674 * global lock
1675 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001676static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001677{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001678 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001679 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001680 return;
1681
Yijing Wange4a60d12014-11-07 12:05:49 +08001682 mutex_lock(&gdp_mutex);
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10001683 if (!kobject_has_children(glue_dir))
1684 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001685 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001686 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001687}
Cornelia Huck63b69712008-01-21 16:09:44 +01001688
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001689static int device_add_class_symlinks(struct device *dev)
1690{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001691 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001692 int error;
1693
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001694 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001695 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001696 if (error)
1697 dev_warn(dev, "Error %d creating of_node link\n",error);
1698 /* An error here doesn't warrant bringing down the device */
1699 }
1700
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001701 if (!dev->class)
1702 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001703
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001704 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001705 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001706 "subsystem");
1707 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001708 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001709
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001710 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001711 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1712 "device");
1713 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001714 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001715 }
Kay Sievers39aba962010-09-04 22:33:14 -07001716
Randy Dunlapead454f2010-09-24 14:36:49 -07001717#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001718 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001719 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001720 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001721#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001722
1723 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001724 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001725 &dev->kobj, dev_name(dev));
1726 if (error)
1727 goto out_device;
1728
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001729 return 0;
1730
Kay Sievers39aba962010-09-04 22:33:14 -07001731out_device:
1732 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001733
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001734out_subsys:
1735 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001736out_devnode:
1737 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001738 return error;
1739}
1740
1741static void device_remove_class_symlinks(struct device *dev)
1742{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001743 if (dev_of_node(dev))
1744 sysfs_remove_link(&dev->kobj, "of_node");
1745
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001746 if (!dev->class)
1747 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001748
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001749 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001750 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001751 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001752#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001753 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001754 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001755#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001756 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001757}
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001760 * dev_set_name - set a device name
1761 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001762 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001763 */
1764int dev_set_name(struct device *dev, const char *fmt, ...)
1765{
1766 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001767 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001768
1769 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001770 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001771 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001772 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001773}
1774EXPORT_SYMBOL_GPL(dev_set_name);
1775
1776/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001777 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1778 * @dev: device
1779 *
1780 * By default we select char/ for new entries. Setting class->dev_obj
1781 * to NULL prevents an entry from being created. class->dev_kobj must
1782 * be set (or cleared) before any devices are registered to the class
1783 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001784 * device_remove_sys_dev_entry() will disagree about the presence of
1785 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001786 */
1787static struct kobject *device_to_dev_kobj(struct device *dev)
1788{
1789 struct kobject *kobj;
1790
1791 if (dev->class)
1792 kobj = dev->class->dev_kobj;
1793 else
1794 kobj = sysfs_dev_char_kobj;
1795
1796 return kobj;
1797}
1798
1799static int device_create_sys_dev_entry(struct device *dev)
1800{
1801 struct kobject *kobj = device_to_dev_kobj(dev);
1802 int error = 0;
1803 char devt_str[15];
1804
1805 if (kobj) {
1806 format_dev_t(devt_str, dev->devt);
1807 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1808 }
1809
1810 return error;
1811}
1812
1813static void device_remove_sys_dev_entry(struct device *dev)
1814{
1815 struct kobject *kobj = device_to_dev_kobj(dev);
1816 char devt_str[15];
1817
1818 if (kobj) {
1819 format_dev_t(devt_str, dev->devt);
1820 sysfs_remove_link(kobj, devt_str);
1821 }
1822}
1823
Shaokun Zhang46d3a032018-07-15 18:08:56 +08001824static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001825{
1826 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1827 if (!dev->p)
1828 return -ENOMEM;
1829 dev->p->device = dev;
1830 klist_init(&dev->p->klist_children, klist_children_get,
1831 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001832 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001833 return 0;
1834}
1835
Dan Williamse105b8b2008-04-21 10:51:07 -07001836/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001837 * device_add - add device to device hierarchy.
1838 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001840 * This is part 2 of device_register(), though may be called
1841 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 *
Cornelia Huck57394112008-09-03 18:26:40 +02001843 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001844 * to the global and sibling lists for the device, then
1845 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001846 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001847 * Do not call this routine or device_register() more than once for
1848 * any device structure. The driver model core is not designed to work
1849 * with devices that get unregistered and then spring back to life.
1850 * (Among other things, it's very hard to guarantee that all references
1851 * to the previous incarnation of @dev have been dropped.) Allocate
1852 * and register a fresh new struct device instead.
1853 *
Cornelia Huck57394112008-09-03 18:26:40 +02001854 * NOTE: _Never_ directly free @dev after calling this function, even
1855 * if it returned an error! Always use put_device() to give up your
1856 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 */
1858int device_add(struct device *dev)
1859{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301860 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001861 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001862 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001863 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001864 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001867 if (!dev)
1868 goto done;
1869
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001870 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001871 error = device_private_init(dev);
1872 if (error)
1873 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001874 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001875
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001876 /*
1877 * for statically allocated devices, which should all be converted
1878 * some day, we need to initialize the name. We prevent reading back
1879 * the name, and force the use of dev_name()
1880 */
1881 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001882 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001883 dev->init_name = NULL;
1884 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001885
Kay Sieversca22e562011-12-14 14:29:38 -08001886 /* subsystems can specify simple device enumeration */
1887 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1888 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1889
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001890 if (!dev_name(dev)) {
1891 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001892 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001895 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001898 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001899 if (IS_ERR(kobj)) {
1900 error = PTR_ERR(kobj);
1901 goto parent_error;
1902 }
Kay Sieversca22e562011-12-14 14:29:38 -08001903 if (kobj)
1904 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Yinghai Lu0d358f22008-02-19 03:20:41 -08001906 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001907 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001908 set_dev_node(dev, dev_to_node(parent));
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001911 /* we require the name to be set before, and pass NULL */
1912 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001913 if (error) {
1914 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001916 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001917
Brian Walsh37022642006-08-14 22:43:19 -07001918 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001919 error = device_platform_notify(dev, KOBJ_ADD);
1920 if (error)
1921 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07001922
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001923 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001924 if (error)
1925 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001926
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001927 error = device_add_class_symlinks(dev);
1928 if (error)
1929 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001930 error = device_add_attrs(dev);
1931 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001932 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001933 error = bus_add_device(dev);
1934 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001936 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001937 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001938 goto DPMError;
1939 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001940
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001941 if (MAJOR(dev->devt)) {
1942 error = device_create_file(dev, &dev_attr_dev);
1943 if (error)
1944 goto DevAttrError;
1945
1946 error = device_create_sys_dev_entry(dev);
1947 if (error)
1948 goto SysEntryError;
1949
1950 devtmpfs_create_node(dev);
1951 }
1952
Alan Sternec0676ee2008-12-05 14:10:31 -05001953 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001954 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001955 */
1956 if (dev->bus)
1957 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1958 BUS_NOTIFY_ADD_DEVICE, dev);
1959
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001960 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001961 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001963 klist_add_tail(&dev->p->knode_parent,
1964 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001966 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001967 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001968 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001969 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001970 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001971
1972 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001973 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001974 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001975 if (class_intf->add_dev)
1976 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001977 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001978 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001979done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 put_device(dev);
1981 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001982 SysEntryError:
1983 if (MAJOR(dev->devt))
1984 device_remove_file(dev, &dev_attr_dev);
1985 DevAttrError:
1986 device_pm_remove(dev);
1987 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001988 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001989 bus_remove_device(dev);
1990 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001991 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001992 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001993 device_remove_class_symlinks(dev);
1994 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001995 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001996 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001997 device_platform_notify(dev, KOBJ_REMOVE);
1998platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01001999 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002000 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 kobject_del(&dev->kobj);
2002 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08002003 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002004parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01002005 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07002006name_error:
2007 kfree(dev->p);
2008 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002009 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
David Graham White86df2682013-07-21 20:41:14 -04002011EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002014 * device_register - register a device with the system.
2015 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002017 * This happens in two clean steps - initialize the device
2018 * and add it to the system. The two steps can be called
2019 * separately, but this is the easiest and most common.
2020 * I.e. you should only call the two helpers separately if
2021 * have a clearly defined need to use and refcount the device
2022 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02002023 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002024 * For more information, see the kerneldoc for device_initialize()
2025 * and device_add().
2026 *
Cornelia Huck57394112008-09-03 18:26:40 +02002027 * NOTE: _Never_ directly free @dev after calling this function, even
2028 * if it returned an error! Always use put_device() to give up the
2029 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031int device_register(struct device *dev)
2032{
2033 device_initialize(dev);
2034 return device_add(dev);
2035}
David Graham White86df2682013-07-21 20:41:14 -04002036EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002039 * get_device - increment reference count for device.
2040 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002042 * This simply forwards the call to kobject_get(), though
2043 * we do take care to provide for the case that we get a NULL
2044 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002046struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002048 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
David Graham White86df2682013-07-21 20:41:14 -04002050EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002053 * put_device - decrement reference count.
2054 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002056void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02002058 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (dev)
2060 kobject_put(&dev->kobj);
2061}
David Graham White86df2682013-07-21 20:41:14 -04002062EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002065 * device_del - delete device from system.
2066 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002068 * This is the first part of the device unregistration
2069 * sequence. This removes the device from the lists we control
2070 * from here, has it removed from the other driver model
2071 * subsystems it was added to in device_add(), and removes it
2072 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002074 * NOTE: this should be called manually _iff_ device_add() was
2075 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002077void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002079 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002080 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002081 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Alan Sternec0676ee2008-12-05 14:10:31 -05002083 /* Notify clients of device removal. This call must come
2084 * before dpm_sysfs_remove().
2085 */
2086 if (dev->bus)
2087 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2088 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002089
Alan Stern3b98aea2008-08-07 13:06:12 -04002090 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002092 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002093 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002094 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002095 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002096 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002097 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002098 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002099 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002100
Kay Sieversca22e562011-12-14 14:29:38 -08002101 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002102 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002103 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002104 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002105 if (class_intf->remove_dev)
2106 class_intf->remove_dev(dev, class_intf);
2107 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02002108 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002109 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002110 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002111 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002112 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002113 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002114 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002115 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002116 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02002117 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002118 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Joerg Roedel599bad32014-09-30 13:02:02 +02002120 if (dev->bus)
2121 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2122 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002123 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002124 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002126 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002127 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
David Graham White86df2682013-07-21 20:41:14 -04002129EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002132 * device_unregister - unregister device from system.
2133 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002135 * We do this in two parts, like we do device_register(). First,
2136 * we remove it from all the subsystems with device_del(), then
2137 * we decrement the reference count via put_device(). If that
2138 * is the final reference count, the device will be cleaned up
2139 * via device_release() above. Otherwise, the structure will
2140 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002142void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002144 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 device_del(dev);
2146 put_device(dev);
2147}
David Graham White86df2682013-07-21 20:41:14 -04002148EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002150static struct device *prev_device(struct klist_iter *i)
2151{
2152 struct klist_node *n = klist_prev(i);
2153 struct device *dev = NULL;
2154 struct device_private *p;
2155
2156 if (n) {
2157 p = to_device_private_parent(n);
2158 dev = p->device;
2159 }
2160 return dev;
2161}
2162
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002163static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002164{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002165 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002166 struct device *dev = NULL;
2167 struct device_private *p;
2168
2169 if (n) {
2170 p = to_device_private_parent(n);
2171 dev = p->device;
2172 }
2173 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002177 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002178 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002179 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002180 * @uid: returned file owner
2181 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002182 * @tmp: possibly allocated string
2183 *
2184 * Return the relative path of a possible device node.
2185 * Non-default names may need to allocate a memory to compose
2186 * a name. This memory is returned in tmp and needs to be
2187 * freed by the caller.
2188 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002189const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002190 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002191 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002192{
2193 char *s;
2194
2195 *tmp = NULL;
2196
2197 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002198 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002199 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002200 if (*tmp)
2201 return *tmp;
2202
2203 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002204 if (dev->class && dev->class->devnode)
2205 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002206 if (*tmp)
2207 return *tmp;
2208
2209 /* return name without allocation, tmp == NULL */
2210 if (strchr(dev_name(dev), '!') == NULL)
2211 return dev_name(dev);
2212
2213 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002214 s = kstrdup(dev_name(dev), GFP_KERNEL);
2215 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002216 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002217 strreplace(s, '!', '/');
2218 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002219}
2220
2221/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002222 * device_for_each_child - device child iterator.
2223 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002224 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002225 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002227 * Iterate over @parent's child devices, and call @fn for each,
2228 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002230 * We check the return of @fn each time. If it returns anything
2231 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002233int device_for_each_child(struct device *parent, void *data,
2234 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002236 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002237 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int error = 0;
2239
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002240 if (!parent->p)
2241 return 0;
2242
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002243 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002244 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002245 error = fn(child, data);
2246 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return error;
2248}
David Graham White86df2682013-07-21 20:41:14 -04002249EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Cornelia Huck5ab69982006-11-16 15:42:07 +01002251/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002252 * device_for_each_child_reverse - device child iterator in reversed order.
2253 * @parent: parent struct device.
2254 * @fn: function to be called for each device.
2255 * @data: data for the callback.
2256 *
2257 * Iterate over @parent's child devices, and call @fn for each,
2258 * passing it @data.
2259 *
2260 * We check the return of @fn each time. If it returns anything
2261 * other than 0, we break out and return that value.
2262 */
2263int device_for_each_child_reverse(struct device *parent, void *data,
2264 int (*fn)(struct device *dev, void *data))
2265{
2266 struct klist_iter i;
2267 struct device *child;
2268 int error = 0;
2269
2270 if (!parent->p)
2271 return 0;
2272
2273 klist_iter_init(&parent->p->klist_children, &i);
2274 while ((child = prev_device(&i)) && !error)
2275 error = fn(child, data);
2276 klist_iter_exit(&i);
2277 return error;
2278}
2279EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2280
2281/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002282 * device_find_child - device iterator for locating a particular device.
2283 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002284 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002285 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002286 *
2287 * This is similar to the device_for_each_child() function above, but it
2288 * returns a reference to a device that is 'found' for later use, as
2289 * determined by the @match callback.
2290 *
2291 * The callback should return 0 if the device doesn't match and non-zero
2292 * if it does. If the callback returns non-zero and a reference to the
2293 * current device can be obtained, this function will return to the caller
2294 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002295 *
2296 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002297 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002298struct device *device_find_child(struct device *parent, void *data,
2299 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002300{
2301 struct klist_iter i;
2302 struct device *child;
2303
2304 if (!parent)
2305 return NULL;
2306
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002307 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002308 while ((child = next_device(&i)))
2309 if (match(child, data) && get_device(child))
2310 break;
2311 klist_iter_exit(&i);
2312 return child;
2313}
David Graham White86df2682013-07-21 20:41:14 -04002314EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316int __init devices_init(void)
2317{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002318 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2319 if (!devices_kset)
2320 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002321 dev_kobj = kobject_create_and_add("dev", NULL);
2322 if (!dev_kobj)
2323 goto dev_kobj_err;
2324 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2325 if (!sysfs_dev_block_kobj)
2326 goto block_kobj_err;
2327 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2328 if (!sysfs_dev_char_kobj)
2329 goto char_kobj_err;
2330
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002331 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002332
2333 char_kobj_err:
2334 kobject_put(sysfs_dev_block_kobj);
2335 block_kobj_err:
2336 kobject_put(dev_kobj);
2337 dev_kobj_err:
2338 kset_unregister(devices_kset);
2339 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002342static int device_check_offline(struct device *dev, void *not_used)
2343{
2344 int ret;
2345
2346 ret = device_for_each_child(dev, NULL, device_check_offline);
2347 if (ret)
2348 return ret;
2349
2350 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2351}
2352
2353/**
2354 * device_offline - Prepare the device for hot-removal.
2355 * @dev: Device to be put offline.
2356 *
2357 * Execute the device bus type's .offline() callback, if present, to prepare
2358 * the device for a subsequent hot-removal. If that succeeds, the device must
2359 * not be used until either it is removed or its bus type's .online() callback
2360 * is executed.
2361 *
2362 * Call under device_hotplug_lock.
2363 */
2364int device_offline(struct device *dev)
2365{
2366 int ret;
2367
2368 if (dev->offline_disabled)
2369 return -EPERM;
2370
2371 ret = device_for_each_child(dev, NULL, device_check_offline);
2372 if (ret)
2373 return ret;
2374
2375 device_lock(dev);
2376 if (device_supports_offline(dev)) {
2377 if (dev->offline) {
2378 ret = 1;
2379 } else {
2380 ret = dev->bus->offline(dev);
2381 if (!ret) {
2382 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2383 dev->offline = true;
2384 }
2385 }
2386 }
2387 device_unlock(dev);
2388
2389 return ret;
2390}
2391
2392/**
2393 * device_online - Put the device back online after successful device_offline().
2394 * @dev: Device to be put back online.
2395 *
2396 * If device_offline() has been successfully executed for @dev, but the device
2397 * has not been removed subsequently, execute its bus type's .online() callback
2398 * to indicate that the device can be used again.
2399 *
2400 * Call under device_hotplug_lock.
2401 */
2402int device_online(struct device *dev)
2403{
2404 int ret = 0;
2405
2406 device_lock(dev);
2407 if (device_supports_offline(dev)) {
2408 if (dev->offline) {
2409 ret = dev->bus->online(dev);
2410 if (!ret) {
2411 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2412 dev->offline = false;
2413 }
2414 } else {
2415 ret = 1;
2416 }
2417 }
2418 device_unlock(dev);
2419
2420 return ret;
2421}
2422
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002423struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002424 struct device dev;
2425 struct module *owner;
2426};
2427
Josh Triplett93058422012-11-18 21:27:55 -08002428static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002429{
2430 return container_of(d, struct root_device, dev);
2431}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002432
2433static void root_device_release(struct device *dev)
2434{
2435 kfree(to_root_device(dev));
2436}
2437
2438/**
2439 * __root_device_register - allocate and register a root device
2440 * @name: root device name
2441 * @owner: owner module of the root device, usually THIS_MODULE
2442 *
2443 * This function allocates a root device and registers it
2444 * using device_register(). In order to free the returned
2445 * device, use root_device_unregister().
2446 *
2447 * Root devices are dummy devices which allow other devices
2448 * to be grouped under /sys/devices. Use this function to
2449 * allocate a root device and then use it as the parent of
2450 * any device which should appear under /sys/devices/{name}
2451 *
2452 * The /sys/devices/{name} directory will also contain a
2453 * 'module' symlink which points to the @owner directory
2454 * in sysfs.
2455 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002456 * Returns &struct device pointer on success, or ERR_PTR() on error.
2457 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002458 * Note: You probably want to use root_device_register().
2459 */
2460struct device *__root_device_register(const char *name, struct module *owner)
2461{
2462 struct root_device *root;
2463 int err = -ENOMEM;
2464
2465 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2466 if (!root)
2467 return ERR_PTR(err);
2468
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002469 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002470 if (err) {
2471 kfree(root);
2472 return ERR_PTR(err);
2473 }
2474
2475 root->dev.release = root_device_release;
2476
2477 err = device_register(&root->dev);
2478 if (err) {
2479 put_device(&root->dev);
2480 return ERR_PTR(err);
2481 }
2482
Christoph Egger1d9e8822010-05-17 16:57:58 +02002483#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002484 if (owner) {
2485 struct module_kobject *mk = &owner->mkobj;
2486
2487 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2488 if (err) {
2489 device_unregister(&root->dev);
2490 return ERR_PTR(err);
2491 }
2492 root->owner = owner;
2493 }
2494#endif
2495
2496 return &root->dev;
2497}
2498EXPORT_SYMBOL_GPL(__root_device_register);
2499
2500/**
2501 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002502 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002503 *
2504 * This function unregisters and cleans up a device that was created by
2505 * root_device_register().
2506 */
2507void root_device_unregister(struct device *dev)
2508{
2509 struct root_device *root = to_root_device(dev);
2510
2511 if (root->owner)
2512 sysfs_remove_link(&root->dev.kobj, "module");
2513
2514 device_unregister(dev);
2515}
2516EXPORT_SYMBOL_GPL(root_device_unregister);
2517
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002518
2519static void device_create_release(struct device *dev)
2520{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002521 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002522 kfree(dev);
2523}
2524
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002525static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002526device_create_groups_vargs(struct class *class, struct device *parent,
2527 dev_t devt, void *drvdata,
2528 const struct attribute_group **groups,
2529 const char *fmt, va_list args)
2530{
2531 struct device *dev = NULL;
2532 int retval = -ENODEV;
2533
2534 if (class == NULL || IS_ERR(class))
2535 goto error;
2536
2537 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2538 if (!dev) {
2539 retval = -ENOMEM;
2540 goto error;
2541 }
2542
David Herrmannbbc780f2013-11-21 20:15:48 +01002543 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002544 dev->devt = devt;
2545 dev->class = class;
2546 dev->parent = parent;
2547 dev->groups = groups;
2548 dev->release = device_create_release;
2549 dev_set_drvdata(dev, drvdata);
2550
2551 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2552 if (retval)
2553 goto error;
2554
David Herrmannbbc780f2013-11-21 20:15:48 +01002555 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002556 if (retval)
2557 goto error;
2558
2559 return dev;
2560
2561error:
2562 put_device(dev);
2563 return ERR_PTR(retval);
2564}
2565
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002566/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002567 * device_create_vargs - creates a device and registers it with sysfs
2568 * @class: pointer to the struct class that this device should be registered to
2569 * @parent: pointer to the parent struct device of this new device, if any
2570 * @devt: the dev_t for the char device to be added
2571 * @drvdata: the data to be added to the device for callbacks
2572 * @fmt: string for the device's name
2573 * @args: va_list for the device's name
2574 *
2575 * This function can be used by char device classes. A struct device
2576 * will be created in sysfs, registered to the specified class.
2577 *
2578 * A "dev" file will be created, showing the dev_t for the device, if
2579 * the dev_t is not 0,0.
2580 * If a pointer to a parent struct device is passed in, the newly created
2581 * struct device will be a child of that device in sysfs.
2582 * The pointer to the struct device will be returned from the call.
2583 * Any further sysfs files that might be required can be created using this
2584 * pointer.
2585 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002586 * Returns &struct device pointer on success, or ERR_PTR() on error.
2587 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002588 * Note: the struct class passed to this function must have previously
2589 * been created with a call to class_create().
2590 */
2591struct device *device_create_vargs(struct class *class, struct device *parent,
2592 dev_t devt, void *drvdata, const char *fmt,
2593 va_list args)
2594{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002595 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2596 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002597}
2598EXPORT_SYMBOL_GPL(device_create_vargs);
2599
2600/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002601 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002602 * @class: pointer to the struct class that this device should be registered to
2603 * @parent: pointer to the parent struct device of this new device, if any
2604 * @devt: the dev_t for the char device to be added
2605 * @drvdata: the data to be added to the device for callbacks
2606 * @fmt: string for the device's name
2607 *
2608 * This function can be used by char device classes. A struct device
2609 * will be created in sysfs, registered to the specified class.
2610 *
2611 * A "dev" file will be created, showing the dev_t for the device, if
2612 * the dev_t is not 0,0.
2613 * If a pointer to a parent struct device is passed in, the newly created
2614 * struct device will be a child of that device in sysfs.
2615 * The pointer to the struct device will be returned from the call.
2616 * Any further sysfs files that might be required can be created using this
2617 * pointer.
2618 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002619 * Returns &struct device pointer on success, or ERR_PTR() on error.
2620 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002621 * Note: the struct class passed to this function must have previously
2622 * been created with a call to class_create().
2623 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002624struct device *device_create(struct class *class, struct device *parent,
2625 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002626{
2627 va_list vargs;
2628 struct device *dev;
2629
2630 va_start(vargs, fmt);
2631 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2632 va_end(vargs);
2633 return dev;
2634}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002635EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002636
Guenter Roeck39ef3112013-07-14 16:05:57 -07002637/**
2638 * device_create_with_groups - creates a device and registers it with sysfs
2639 * @class: pointer to the struct class that this device should be registered to
2640 * @parent: pointer to the parent struct device of this new device, if any
2641 * @devt: the dev_t for the char device to be added
2642 * @drvdata: the data to be added to the device for callbacks
2643 * @groups: NULL-terminated list of attribute groups to be created
2644 * @fmt: string for the device's name
2645 *
2646 * This function can be used by char device classes. A struct device
2647 * will be created in sysfs, registered to the specified class.
2648 * Additional attributes specified in the groups parameter will also
2649 * be created automatically.
2650 *
2651 * A "dev" file will be created, showing the dev_t for the device, if
2652 * the dev_t is not 0,0.
2653 * If a pointer to a parent struct device is passed in, the newly created
2654 * struct device will be a child of that device in sysfs.
2655 * The pointer to the struct device will be returned from the call.
2656 * Any further sysfs files that might be required can be created using this
2657 * pointer.
2658 *
2659 * Returns &struct device pointer on success, or ERR_PTR() on error.
2660 *
2661 * Note: the struct class passed to this function must have previously
2662 * been created with a call to class_create().
2663 */
2664struct device *device_create_with_groups(struct class *class,
2665 struct device *parent, dev_t devt,
2666 void *drvdata,
2667 const struct attribute_group **groups,
2668 const char *fmt, ...)
2669{
2670 va_list vargs;
2671 struct device *dev;
2672
2673 va_start(vargs, fmt);
2674 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2675 fmt, vargs);
2676 va_end(vargs);
2677 return dev;
2678}
2679EXPORT_SYMBOL_GPL(device_create_with_groups);
2680
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002681static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002682{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002683 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002684
Dave Youngcd354492008-01-28 16:56:11 +08002685 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002686}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002687
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002688/**
2689 * device_destroy - removes a device that was created with device_create()
2690 * @class: pointer to the struct class that this device was registered with
2691 * @devt: the dev_t of the device that was previously registered
2692 *
2693 * This call unregisters and cleans up a device that was created with a
2694 * call to device_create().
2695 */
2696void device_destroy(struct class *class, dev_t devt)
2697{
2698 struct device *dev;
2699
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002700 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002701 if (dev) {
2702 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002703 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002704 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002705}
2706EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002707
2708/**
2709 * device_rename - renames a device
2710 * @dev: the pointer to the struct device to be renamed
2711 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002712 *
2713 * It is the responsibility of the caller to provide mutual
2714 * exclusion between two different calls of device_rename
2715 * on the same device to ensure that new_name is valid and
2716 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002717 *
Timur Tabia5462512010-12-13 14:08:52 -06002718 * Note: Don't call this function. Currently, the networking layer calls this
2719 * function, but that will change. The following text from Kay Sievers offers
2720 * some insight:
2721 *
2722 * Renaming devices is racy at many levels, symlinks and other stuff are not
2723 * replaced atomically, and you get a "move" uevent, but it's not easy to
2724 * connect the event to the old and new device. Device nodes are not renamed at
2725 * all, there isn't even support for that in the kernel now.
2726 *
2727 * In the meantime, during renaming, your target name might be taken by another
2728 * driver, creating conflicts. Or the old name is taken directly after you
2729 * renamed it -- then you get events for the same DEVPATH, before you even see
2730 * the "move" event. It's just a mess, and nothing new should ever rely on
2731 * kernel device renaming. Besides that, it's not even implemented now for
2732 * other things than (driver-core wise very simple) network devices.
2733 *
2734 * We are currently about to change network renaming in udev to completely
2735 * disallow renaming of devices in the same namespace as the kernel uses,
2736 * because we can't solve the problems properly, that arise with swapping names
2737 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2738 * be allowed to some other name than eth[0-9]*, for the aforementioned
2739 * reasons.
2740 *
2741 * Make up a "real" name in the driver before you register anything, or add
2742 * some other attributes for userspace to find the device, or use udev to add
2743 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2744 * don't even want to get into that and try to implement the missing pieces in
2745 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002746 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002747int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002748{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002749 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002750 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002751 int error;
2752
2753 dev = get_device(dev);
2754 if (!dev)
2755 return -EINVAL;
2756
ethan.zhao69df7532013-10-13 22:12:35 +08002757 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002758
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002759 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002760 if (!old_device_name) {
2761 error = -ENOMEM;
2762 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002763 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002764
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002765 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002766 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2767 kobj, old_device_name,
2768 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002769 if (error)
2770 goto out;
2771 }
Kay Sievers39aba962010-09-04 22:33:14 -07002772
Tejun Heo4b30ee52013-09-11 22:29:06 -04002773 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002774 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002775 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002776
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002777out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002778 put_device(dev);
2779
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002780 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002781
2782 return error;
2783}
Johannes Berga2807db2007-02-28 12:38:31 +01002784EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002785
2786static int device_move_class_links(struct device *dev,
2787 struct device *old_parent,
2788 struct device *new_parent)
2789{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002790 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002791
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002792 if (old_parent)
2793 sysfs_remove_link(&dev->kobj, "device");
2794 if (new_parent)
2795 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2796 "device");
2797 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002798}
2799
2800/**
2801 * device_move - moves a device to a new parent
2802 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02002803 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002804 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002805 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002806int device_move(struct device *dev, struct device *new_parent,
2807 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002808{
2809 int error;
2810 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002811 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002812
2813 dev = get_device(dev);
2814 if (!dev)
2815 return -EINVAL;
2816
Cornelia Huckffa6a702009-03-04 12:44:00 +01002817 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002818 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002819 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002820 if (IS_ERR(new_parent_kobj)) {
2821 error = PTR_ERR(new_parent_kobj);
2822 put_device(new_parent);
2823 goto out;
2824 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002825
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002826 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2827 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002828 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002829 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002830 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002831 put_device(new_parent);
2832 goto out;
2833 }
2834 old_parent = dev->parent;
2835 dev->parent = new_parent;
2836 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002837 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002838 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002839 klist_add_tail(&dev->p->knode_parent,
2840 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002841 set_dev_node(dev, dev_to_node(new_parent));
2842 }
2843
Rabin Vincentbdd40342012-04-23 09:16:36 +02002844 if (dev->class) {
2845 error = device_move_class_links(dev, old_parent, new_parent);
2846 if (error) {
2847 /* We ignore errors on cleanup since we're hosed anyway... */
2848 device_move_class_links(dev, new_parent, old_parent);
2849 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2850 if (new_parent)
2851 klist_remove(&dev->p->knode_parent);
2852 dev->parent = old_parent;
2853 if (old_parent) {
2854 klist_add_tail(&dev->p->knode_parent,
2855 &old_parent->p->klist_children);
2856 set_dev_node(dev, dev_to_node(old_parent));
2857 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002858 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002859 cleanup_glue_dir(dev, new_parent_kobj);
2860 put_device(new_parent);
2861 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002862 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002863 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002864 switch (dpm_order) {
2865 case DPM_ORDER_NONE:
2866 break;
2867 case DPM_ORDER_DEV_AFTER_PARENT:
2868 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002869 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002870 break;
2871 case DPM_ORDER_PARENT_BEFORE_DEV:
2872 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002873 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002874 break;
2875 case DPM_ORDER_DEV_LAST:
2876 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002877 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002878 break;
2879 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002880
Cornelia Huck8a824722006-11-20 17:07:51 +01002881 put_device(old_parent);
2882out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002883 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002884 put_device(dev);
2885 return error;
2886}
Cornelia Huck8a824722006-11-20 17:07:51 +01002887EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002888
2889/**
2890 * device_shutdown - call ->shutdown() on each device to shutdown.
2891 */
2892void device_shutdown(void)
2893{
Benson Leungf123db82013-09-24 20:05:11 -07002894 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002895
Pingfan Liu3297c8f2018-07-19 13:14:58 +08002896 wait_for_device_probe();
2897 device_block_probing();
2898
Hugh Daschbach62458382010-03-22 10:36:37 -07002899 spin_lock(&devices_kset->list_lock);
2900 /*
2901 * Walk the devices list backward, shutting down each in turn.
2902 * Beware that device unplug events may also start pulling
2903 * devices offline, even as the system is shutting down.
2904 */
2905 while (!list_empty(&devices_kset->list)) {
2906 dev = list_entry(devices_kset->list.prev, struct device,
2907 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002908
2909 /*
2910 * hold reference count of device's parent to
2911 * prevent it from being freed because parent's
2912 * lock is to be held
2913 */
Benson Leungf123db82013-09-24 20:05:11 -07002914 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002915 get_device(dev);
2916 /*
2917 * Make sure the device is off the kset list, in the
2918 * event that dev->*->shutdown() doesn't remove it.
2919 */
2920 list_del_init(&dev->kobj.entry);
2921 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002922
Ming Leid1c6c032012-06-22 18:01:40 +08002923 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002924 if (parent)
2925 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002926 device_lock(dev);
2927
Alan Sternfe6b91f2011-12-06 23:24:52 +01002928 /* Don't allow any more runtime suspends */
2929 pm_runtime_get_noresume(dev);
2930 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002931
Michal Suchanek75216212017-08-11 15:44:43 +02002932 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07002933 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02002934 dev_info(dev, "shutdown_pre\n");
2935 dev->class->shutdown_pre(dev);
2936 }
2937 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002938 if (initcall_debug)
2939 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002940 dev->bus->shutdown(dev);
2941 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002942 if (initcall_debug)
2943 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002944 dev->driver->shutdown(dev);
2945 }
Ming Leid1c6c032012-06-22 18:01:40 +08002946
2947 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002948 if (parent)
2949 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002950
Hugh Daschbach62458382010-03-22 10:36:37 -07002951 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002952 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002953
2954 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002955 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002956 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002957}
Joe Perches99bcf212010-06-27 01:02:34 +00002958
2959/*
2960 * Device logging functions
2961 */
2962
2963#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002964static int
2965create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002966{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002967 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002968 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002969
Kay Sieversc4e00da2012-05-03 02:29:59 +02002970 if (dev->class)
2971 subsys = dev->class->name;
2972 else if (dev->bus)
2973 subsys = dev->bus->name;
2974 else
Joe Perches798efc62012-09-12 20:11:29 -07002975 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002976
Joe Perches798efc62012-09-12 20:11:29 -07002977 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002978 if (pos >= hdrlen)
2979 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002980
2981 /*
2982 * Add device identifier DEVICE=:
2983 * b12:8 block dev_t
2984 * c127:3 char dev_t
2985 * n8 netdev ifindex
2986 * +sound:card0 subsystem:devname
2987 */
2988 if (MAJOR(dev->devt)) {
2989 char c;
2990
2991 if (strcmp(subsys, "block") == 0)
2992 c = 'b';
2993 else
2994 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002995 pos++;
2996 pos += snprintf(hdr + pos, hdrlen - pos,
2997 "DEVICE=%c%u:%u",
2998 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002999 } else if (strcmp(subsys, "net") == 0) {
3000 struct net_device *net = to_net_dev(dev);
3001
Joe Perches798efc62012-09-12 20:11:29 -07003002 pos++;
3003 pos += snprintf(hdr + pos, hdrlen - pos,
3004 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02003005 } else {
Joe Perches798efc62012-09-12 20:11:29 -07003006 pos++;
3007 pos += snprintf(hdr + pos, hdrlen - pos,
3008 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003009 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06003010
Ben Hutchings655e5b72014-08-26 00:34:44 -07003011 if (pos >= hdrlen)
3012 goto overflow;
3013
Joe Perches798efc62012-09-12 20:11:29 -07003014 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07003015
3016overflow:
3017 dev_WARN(dev, "device/subsystem name too long");
3018 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003019}
Joe Perches798efc62012-09-12 20:11:29 -07003020
Joe Perches05e4e5b2012-09-12 20:13:37 -07003021int dev_vprintk_emit(int level, const struct device *dev,
3022 const char *fmt, va_list args)
3023{
3024 char hdr[128];
3025 size_t hdrlen;
3026
3027 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3028
3029 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3030}
3031EXPORT_SYMBOL(dev_vprintk_emit);
3032
3033int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3034{
3035 va_list args;
3036 int r;
3037
3038 va_start(args, fmt);
3039
3040 r = dev_vprintk_emit(level, dev, fmt, args);
3041
3042 va_end(args);
3043
3044 return r;
3045}
3046EXPORT_SYMBOL(dev_printk_emit);
3047
Joe Perchesd1f10522014-12-25 15:07:04 -08003048static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07003049 struct va_format *vaf)
3050{
Joe Perchesd1f10522014-12-25 15:07:04 -08003051 if (dev)
3052 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3053 dev_driver_string(dev), dev_name(dev), vaf);
3054 else
3055 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003056}
Joe Perches99bcf212010-06-27 01:02:34 +00003057
Joe Perchesd1f10522014-12-25 15:07:04 -08003058void dev_printk(const char *level, const struct device *dev,
3059 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00003060{
3061 struct va_format vaf;
3062 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00003063
3064 va_start(args, fmt);
3065
3066 vaf.fmt = fmt;
3067 vaf.va = &args;
3068
Joe Perchesd1f10522014-12-25 15:07:04 -08003069 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003070
Joe Perches99bcf212010-06-27 01:02:34 +00003071 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00003072}
3073EXPORT_SYMBOL(dev_printk);
3074
3075#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08003076void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00003077{ \
3078 struct va_format vaf; \
3079 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003080 \
3081 va_start(args, fmt); \
3082 \
3083 vaf.fmt = fmt; \
3084 vaf.va = &args; \
3085 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003086 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003087 \
Joe Perches99bcf212010-06-27 01:02:34 +00003088 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003089} \
3090EXPORT_SYMBOL(func);
3091
Joe Perches663336e2018-05-09 08:15:46 -07003092define_dev_printk_level(_dev_emerg, KERN_EMERG);
3093define_dev_printk_level(_dev_alert, KERN_ALERT);
3094define_dev_printk_level(_dev_crit, KERN_CRIT);
3095define_dev_printk_level(_dev_err, KERN_ERR);
3096define_dev_printk_level(_dev_warn, KERN_WARNING);
3097define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00003098define_dev_printk_level(_dev_info, KERN_INFO);
3099
3100#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003101
3102static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3103{
3104 return fwnode && !IS_ERR(fwnode->secondary);
3105}
3106
3107/**
3108 * set_primary_fwnode - Change the primary firmware node of a given device.
3109 * @dev: Device to handle.
3110 * @fwnode: New primary firmware node of the device.
3111 *
3112 * Set the device's firmware node pointer to @fwnode, but if a secondary
3113 * firmware node of the device is present, preserve it.
3114 */
3115void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3116{
3117 if (fwnode) {
3118 struct fwnode_handle *fn = dev->fwnode;
3119
3120 if (fwnode_is_primary(fn))
3121 fn = fn->secondary;
3122
Mika Westerberg55f89a82015-11-30 17:11:39 +02003123 if (fn) {
3124 WARN_ON(fwnode->secondary);
3125 fwnode->secondary = fn;
3126 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003127 dev->fwnode = fwnode;
3128 } else {
3129 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3130 dev->fwnode->secondary : NULL;
3131 }
3132}
3133EXPORT_SYMBOL_GPL(set_primary_fwnode);
3134
3135/**
3136 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3137 * @dev: Device to handle.
3138 * @fwnode: New secondary firmware node of the device.
3139 *
3140 * If a primary firmware node of the device is present, set its secondary
3141 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3142 * @fwnode.
3143 */
3144void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3145{
3146 if (fwnode)
3147 fwnode->secondary = ERR_PTR(-ENODEV);
3148
3149 if (fwnode_is_primary(dev->fwnode))
3150 dev->fwnode->secondary = fwnode;
3151 else
3152 dev->fwnode = fwnode;
3153}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003154
3155/**
3156 * device_set_of_node_from_dev - reuse device-tree node of another device
3157 * @dev: device whose device-tree node is being set
3158 * @dev2: device whose device-tree node is being reused
3159 *
3160 * Takes another reference to the new device-tree node after first dropping
3161 * any reference held to the old node.
3162 */
3163void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3164{
3165 of_node_put(dev->of_node);
3166 dev->of_node = of_node_get(dev2->of_node);
3167 dev->of_node_reused = true;
3168}
3169EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);