blob: ed145fbfeddf06dcee5b833c0558d35c7fb6325d [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/base/core.c - core driver model code (device registration, etc)
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07007 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020013#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070018#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100019#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070020#include <linux/of.h>
21#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010022#include <linux/genhd.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070023#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010024#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020025#include <linux/netdevice.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070027#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "base.h"
30#include "power/power.h"
31
Andi Kleene52eec12010-09-08 16:54:17 +020032#ifdef CONFIG_SYSFS_DEPRECATED
33#ifdef CONFIG_SYSFS_DEPRECATED_V2
34long sysfs_deprecated = 1;
35#else
36long sysfs_deprecated = 0;
37#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080038static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020039{
Jingoo Han34da5e62013-07-26 13:10:22 +090040 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020041}
42early_param("sysfs.deprecated", sysfs_deprecated_setup);
43#endif
44
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010045/* Device links support. */
46
47#ifdef CONFIG_SRCU
48static DEFINE_MUTEX(device_links_lock);
49DEFINE_STATIC_SRCU(device_links_srcu);
50
51static inline void device_links_write_lock(void)
52{
53 mutex_lock(&device_links_lock);
54}
55
56static inline void device_links_write_unlock(void)
57{
58 mutex_unlock(&device_links_lock);
59}
60
61int device_links_read_lock(void)
62{
63 return srcu_read_lock(&device_links_srcu);
64}
65
66void device_links_read_unlock(int idx)
67{
68 srcu_read_unlock(&device_links_srcu, idx);
69}
70#else /* !CONFIG_SRCU */
71static DECLARE_RWSEM(device_links_lock);
72
73static inline void device_links_write_lock(void)
74{
75 down_write(&device_links_lock);
76}
77
78static inline void device_links_write_unlock(void)
79{
80 up_write(&device_links_lock);
81}
82
83int device_links_read_lock(void)
84{
85 down_read(&device_links_lock);
86 return 0;
87}
88
89void device_links_read_unlock(int not_used)
90{
91 up_read(&device_links_lock);
92}
93#endif /* !CONFIG_SRCU */
94
95/**
96 * device_is_dependent - Check if one device depends on another one
97 * @dev: Device to check dependencies for.
98 * @target: Device to check against.
99 *
100 * Check if @target depends on @dev or any device dependent on it (its child or
101 * its consumer etc). Return 1 if that is the case or 0 otherwise.
102 */
103static int device_is_dependent(struct device *dev, void *target)
104{
105 struct device_link *link;
106 int ret;
107
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200108 if (dev == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100109 return 1;
110
111 ret = device_for_each_child(dev, target, device_is_dependent);
112 if (ret)
113 return ret;
114
115 list_for_each_entry(link, &dev->links.consumers, s_node) {
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200116 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100117 return 1;
118
119 ret = device_is_dependent(link->consumer, target);
120 if (ret)
121 break;
122 }
123 return ret;
124}
125
126static int device_reorder_to_tail(struct device *dev, void *not_used)
127{
128 struct device_link *link;
129
130 /*
131 * Devices that have not been registered yet will be put to the ends
132 * of the lists during the registration, so skip them here.
133 */
134 if (device_is_registered(dev))
135 devices_kset_move_last(dev);
136
137 if (device_pm_initialized(dev))
138 device_pm_move_last(dev);
139
140 device_for_each_child(dev, NULL, device_reorder_to_tail);
141 list_for_each_entry(link, &dev->links.consumers, s_node)
142 device_reorder_to_tail(link->consumer, NULL);
143
144 return 0;
145}
146
147/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700148 * device_pm_move_to_tail - Move set of devices to the end of device lists
149 * @dev: Device to move
150 *
151 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
152 *
153 * It moves the @dev along with all of its children and all of its consumers
154 * to the ends of the device_kset and dpm_list, recursively.
155 */
156void device_pm_move_to_tail(struct device *dev)
157{
158 int idx;
159
160 idx = device_links_read_lock();
161 device_pm_lock();
162 device_reorder_to_tail(dev, NULL);
163 device_pm_unlock();
164 device_links_read_unlock(idx);
165}
166
167/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100168 * device_link_add - Create a link between two devices.
169 * @consumer: Consumer end of the link.
170 * @supplier: Supplier end of the link.
171 * @flags: Link flags.
172 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100173 * The caller is responsible for the proper synchronization of the link creation
174 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
175 * runtime PM framework to take the link into account. Second, if the
176 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
177 * be forced into the active metastate and reference-counted upon the creation
178 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
179 * ignored.
180 *
Vivek Gautame88728f2018-06-27 18:20:55 +0530181 * If the DL_FLAG_AUTOREMOVE_CONSUMER is set, the link will be removed
182 * automatically when the consumer device driver unbinds from it.
183 * The combination of both DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_STATELESS
184 * set is invalid and will cause NULL to be returned.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100185 *
186 * A side effect of the link creation is re-ordering of dpm_list and the
187 * devices_kset list by moving the consumer device and all devices depending
188 * on it to the ends of these lists (that does not happen to devices that have
189 * not been registered when this function is called).
190 *
191 * The supplier device is required to be registered when this function is called
192 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100193 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100194 */
195struct device_link *device_link_add(struct device *consumer,
196 struct device *supplier, u32 flags)
197{
198 struct device_link *link;
199
200 if (!consumer || !supplier ||
Vivek Gautame88728f2018-06-27 18:20:55 +0530201 ((flags & DL_FLAG_STATELESS) &&
202 (flags & DL_FLAG_AUTOREMOVE_CONSUMER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100203 return NULL;
204
205 device_links_write_lock();
206 device_pm_lock();
207
208 /*
209 * If the supplier has not been fully registered yet or there is a
210 * reverse dependency between the consumer and the supplier already in
211 * the graph, return NULL.
212 */
213 if (!device_pm_initialized(supplier)
214 || device_is_dependent(consumer, supplier)) {
215 link = NULL;
216 goto out;
217 }
218
219 list_for_each_entry(link, &supplier->links.consumers, s_node)
Lukas Wunneread18c22018-02-10 19:27:12 +0100220 if (link->consumer == consumer) {
221 kref_get(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100222 goto out;
Lukas Wunneread18c22018-02-10 19:27:12 +0100223 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100224
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100225 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100226 if (!link)
227 goto out;
228
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100229 if (flags & DL_FLAG_PM_RUNTIME) {
230 if (flags & DL_FLAG_RPM_ACTIVE) {
231 if (pm_runtime_get_sync(supplier) < 0) {
232 pm_runtime_put_noidle(supplier);
233 kfree(link);
234 link = NULL;
235 goto out;
236 }
237 link->rpm_active = true;
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100238 }
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100239 pm_runtime_new_link(consumer);
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200240 /*
241 * If the link is being added by the consumer driver at probe
242 * time, balance the decrementation of the supplier's runtime PM
243 * usage counter after consumer probe in driver_probe_device().
244 */
245 if (consumer->links.status == DL_DEV_PROBING)
246 pm_runtime_get_noresume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100247 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100248 get_device(supplier);
249 link->supplier = supplier;
250 INIT_LIST_HEAD(&link->s_node);
251 get_device(consumer);
252 link->consumer = consumer;
253 INIT_LIST_HEAD(&link->c_node);
254 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100255 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100256
Lukas Wunner64df1142016-12-04 13:10:04 +0100257 /* Determine the initial link state. */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100258 if (flags & DL_FLAG_STATELESS) {
259 link->status = DL_STATE_NONE;
260 } else {
261 switch (supplier->links.status) {
262 case DL_DEV_DRIVER_BOUND:
263 switch (consumer->links.status) {
264 case DL_DEV_PROBING:
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100265 /*
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200266 * Some callers expect the link creation during
267 * consumer driver probe to resume the supplier
268 * even without DL_FLAG_RPM_ACTIVE.
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100269 */
270 if (flags & DL_FLAG_PM_RUNTIME)
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200271 pm_runtime_resume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100272
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100273 link->status = DL_STATE_CONSUMER_PROBE;
274 break;
275 case DL_DEV_DRIVER_BOUND:
276 link->status = DL_STATE_ACTIVE;
277 break;
278 default:
279 link->status = DL_STATE_AVAILABLE;
280 break;
281 }
282 break;
283 case DL_DEV_UNBINDING:
284 link->status = DL_STATE_SUPPLIER_UNBIND;
285 break;
286 default:
287 link->status = DL_STATE_DORMANT;
288 break;
289 }
290 }
291
292 /*
293 * Move the consumer and all of the devices depending on it to the end
294 * of dpm_list and the devices_kset list.
295 *
296 * It is necessary to hold dpm_list locked throughout all that or else
297 * we may end up suspending with a wrong ordering of it.
298 */
299 device_reorder_to_tail(consumer, NULL);
300
301 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
302 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
303
304 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
305
306 out:
307 device_pm_unlock();
308 device_links_write_unlock();
309 return link;
310}
311EXPORT_SYMBOL_GPL(device_link_add);
312
313static void device_link_free(struct device_link *link)
314{
315 put_device(link->consumer);
316 put_device(link->supplier);
317 kfree(link);
318}
319
320#ifdef CONFIG_SRCU
321static void __device_link_free_srcu(struct rcu_head *rhead)
322{
323 device_link_free(container_of(rhead, struct device_link, rcu_head));
324}
325
Lukas Wunneread18c22018-02-10 19:27:12 +0100326static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100327{
Lukas Wunneread18c22018-02-10 19:27:12 +0100328 struct device_link *link = container_of(kref, struct device_link, kref);
329
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100330 dev_info(link->consumer, "Dropping the link to %s\n",
331 dev_name(link->supplier));
332
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100333 if (link->flags & DL_FLAG_PM_RUNTIME)
334 pm_runtime_drop_link(link->consumer);
335
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100336 list_del_rcu(&link->s_node);
337 list_del_rcu(&link->c_node);
338 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
339}
340#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100341static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100342{
Lukas Wunneread18c22018-02-10 19:27:12 +0100343 struct device_link *link = container_of(kref, struct device_link, kref);
344
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100345 dev_info(link->consumer, "Dropping the link to %s\n",
346 dev_name(link->supplier));
347
Lukas Wunner433986c2018-02-10 19:13:58 +0100348 if (link->flags & DL_FLAG_PM_RUNTIME)
349 pm_runtime_drop_link(link->consumer);
350
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100351 list_del(&link->s_node);
352 list_del(&link->c_node);
353 device_link_free(link);
354}
355#endif /* !CONFIG_SRCU */
356
357/**
358 * device_link_del - Delete a link between two devices.
359 * @link: Device link to delete.
360 *
361 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100362 * PM. If the link was added multiple times, it needs to be deleted as often.
363 * Care is required for hotplugged devices: Their links are purged on removal
364 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100365 */
366void device_link_del(struct device_link *link)
367{
368 device_links_write_lock();
369 device_pm_lock();
Lukas Wunneread18c22018-02-10 19:27:12 +0100370 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100371 device_pm_unlock();
372 device_links_write_unlock();
373}
374EXPORT_SYMBOL_GPL(device_link_del);
375
pascal pailletd8842212018-07-05 14:25:56 +0000376/**
377 * device_link_remove - remove a link between two devices.
378 * @consumer: Consumer end of the link.
379 * @supplier: Supplier end of the link.
380 *
381 * The caller must ensure proper synchronization of this function with runtime
382 * PM.
383 */
384void device_link_remove(void *consumer, struct device *supplier)
385{
386 struct device_link *link;
387
388 if (WARN_ON(consumer == supplier))
389 return;
390
391 device_links_write_lock();
392 device_pm_lock();
393
394 list_for_each_entry(link, &supplier->links.consumers, s_node) {
395 if (link->consumer == consumer) {
396 kref_put(&link->kref, __device_link_del);
397 break;
398 }
399 }
400
401 device_pm_unlock();
402 device_links_write_unlock();
403}
404EXPORT_SYMBOL_GPL(device_link_remove);
405
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100406static void device_links_missing_supplier(struct device *dev)
407{
408 struct device_link *link;
409
410 list_for_each_entry(link, &dev->links.suppliers, c_node)
411 if (link->status == DL_STATE_CONSUMER_PROBE)
412 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
413}
414
415/**
416 * device_links_check_suppliers - Check presence of supplier drivers.
417 * @dev: Consumer device.
418 *
419 * Check links from this device to any suppliers. Walk the list of the device's
420 * links to suppliers and see if all of them are available. If not, simply
421 * return -EPROBE_DEFER.
422 *
423 * We need to guarantee that the supplier will not go away after the check has
424 * been positive here. It only can go away in __device_release_driver() and
425 * that function checks the device's links to consumers. This means we need to
426 * mark the link as "consumer probe in progress" to make the supplier removal
427 * wait for us to complete (or bad things may happen).
428 *
429 * Links with the DL_FLAG_STATELESS flag set are ignored.
430 */
431int device_links_check_suppliers(struct device *dev)
432{
433 struct device_link *link;
434 int ret = 0;
435
436 device_links_write_lock();
437
438 list_for_each_entry(link, &dev->links.suppliers, c_node) {
439 if (link->flags & DL_FLAG_STATELESS)
440 continue;
441
442 if (link->status != DL_STATE_AVAILABLE) {
443 device_links_missing_supplier(dev);
444 ret = -EPROBE_DEFER;
445 break;
446 }
447 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
448 }
449 dev->links.status = DL_DEV_PROBING;
450
451 device_links_write_unlock();
452 return ret;
453}
454
455/**
456 * device_links_driver_bound - Update device links after probing its driver.
457 * @dev: Device to update the links for.
458 *
459 * The probe has been successful, so update links from this device to any
460 * consumers by changing their status to "available".
461 *
462 * Also change the status of @dev's links to suppliers to "active".
463 *
464 * Links with the DL_FLAG_STATELESS flag set are ignored.
465 */
466void device_links_driver_bound(struct device *dev)
467{
468 struct device_link *link;
469
470 device_links_write_lock();
471
472 list_for_each_entry(link, &dev->links.consumers, s_node) {
473 if (link->flags & DL_FLAG_STATELESS)
474 continue;
475
476 WARN_ON(link->status != DL_STATE_DORMANT);
477 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
478 }
479
480 list_for_each_entry(link, &dev->links.suppliers, c_node) {
481 if (link->flags & DL_FLAG_STATELESS)
482 continue;
483
484 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
485 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
486 }
487
488 dev->links.status = DL_DEV_DRIVER_BOUND;
489
490 device_links_write_unlock();
491}
492
493/**
494 * __device_links_no_driver - Update links of a device without a driver.
495 * @dev: Device without a drvier.
496 *
497 * Delete all non-persistent links from this device to any suppliers.
498 *
499 * Persistent links stay around, but their status is changed to "available",
500 * unless they already are in the "supplier unbind in progress" state in which
501 * case they need not be updated.
502 *
503 * Links with the DL_FLAG_STATELESS flag set are ignored.
504 */
505static void __device_links_no_driver(struct device *dev)
506{
507 struct device_link *link, *ln;
508
509 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
510 if (link->flags & DL_FLAG_STATELESS)
511 continue;
512
Vivek Gautame88728f2018-06-27 18:20:55 +0530513 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Lukas Wunneread18c22018-02-10 19:27:12 +0100514 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100515 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
516 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
517 }
518
519 dev->links.status = DL_DEV_NO_DRIVER;
520}
521
522void device_links_no_driver(struct device *dev)
523{
524 device_links_write_lock();
525 __device_links_no_driver(dev);
526 device_links_write_unlock();
527}
528
529/**
530 * device_links_driver_cleanup - Update links after driver removal.
531 * @dev: Device whose driver has just gone away.
532 *
533 * Update links to consumers for @dev by changing their status to "dormant" and
534 * invoke %__device_links_no_driver() to update links to suppliers for it as
535 * appropriate.
536 *
537 * Links with the DL_FLAG_STATELESS flag set are ignored.
538 */
539void device_links_driver_cleanup(struct device *dev)
540{
541 struct device_link *link;
542
543 device_links_write_lock();
544
545 list_for_each_entry(link, &dev->links.consumers, s_node) {
546 if (link->flags & DL_FLAG_STATELESS)
547 continue;
548
Vivek Gautame88728f2018-06-27 18:20:55 +0530549 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100550 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530551
552 /*
553 * autoremove the links between this @dev and its consumer
554 * devices that are not active, i.e. where the link state
555 * has moved to DL_STATE_SUPPLIER_UNBIND.
556 */
557 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
558 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
559 kref_put(&link->kref, __device_link_del);
560
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100561 WRITE_ONCE(link->status, DL_STATE_DORMANT);
562 }
563
564 __device_links_no_driver(dev);
565
566 device_links_write_unlock();
567}
568
569/**
570 * device_links_busy - Check if there are any busy links to consumers.
571 * @dev: Device to check.
572 *
573 * Check each consumer of the device and return 'true' if its link's status
574 * is one of "consumer probe" or "active" (meaning that the given consumer is
575 * probing right now or its driver is present). Otherwise, change the link
576 * state to "supplier unbind" to prevent the consumer from being probed
577 * successfully going forward.
578 *
579 * Return 'false' if there are no probing or active consumers.
580 *
581 * Links with the DL_FLAG_STATELESS flag set are ignored.
582 */
583bool device_links_busy(struct device *dev)
584{
585 struct device_link *link;
586 bool ret = false;
587
588 device_links_write_lock();
589
590 list_for_each_entry(link, &dev->links.consumers, s_node) {
591 if (link->flags & DL_FLAG_STATELESS)
592 continue;
593
594 if (link->status == DL_STATE_CONSUMER_PROBE
595 || link->status == DL_STATE_ACTIVE) {
596 ret = true;
597 break;
598 }
599 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
600 }
601
602 dev->links.status = DL_DEV_UNBINDING;
603
604 device_links_write_unlock();
605 return ret;
606}
607
608/**
609 * device_links_unbind_consumers - Force unbind consumers of the given device.
610 * @dev: Device to unbind the consumers of.
611 *
612 * Walk the list of links to consumers for @dev and if any of them is in the
613 * "consumer probe" state, wait for all device probes in progress to complete
614 * and start over.
615 *
616 * If that's not the case, change the status of the link to "supplier unbind"
617 * and check if the link was in the "active" state. If so, force the consumer
618 * driver to unbind and start over (the consumer will not re-probe as we have
619 * changed the state of the link already).
620 *
621 * Links with the DL_FLAG_STATELESS flag set are ignored.
622 */
623void device_links_unbind_consumers(struct device *dev)
624{
625 struct device_link *link;
626
627 start:
628 device_links_write_lock();
629
630 list_for_each_entry(link, &dev->links.consumers, s_node) {
631 enum device_link_state status;
632
633 if (link->flags & DL_FLAG_STATELESS)
634 continue;
635
636 status = link->status;
637 if (status == DL_STATE_CONSUMER_PROBE) {
638 device_links_write_unlock();
639
640 wait_for_device_probe();
641 goto start;
642 }
643 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
644 if (status == DL_STATE_ACTIVE) {
645 struct device *consumer = link->consumer;
646
647 get_device(consumer);
648
649 device_links_write_unlock();
650
651 device_release_driver_internal(consumer, NULL,
652 consumer->parent);
653 put_device(consumer);
654 goto start;
655 }
656 }
657
658 device_links_write_unlock();
659}
660
661/**
662 * device_links_purge - Delete existing links to other devices.
663 * @dev: Target device.
664 */
665static void device_links_purge(struct device *dev)
666{
667 struct device_link *link, *ln;
668
669 /*
670 * Delete all of the remaining links from this device to any other
671 * devices (either consumers or suppliers).
672 */
673 device_links_write_lock();
674
675 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
676 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100677 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100678 }
679
680 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
681 WARN_ON(link->status != DL_STATE_DORMANT &&
682 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100683 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100684 }
685
686 device_links_write_unlock();
687}
688
689/* Device links support end. */
690
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800691int (*platform_notify)(struct device *dev) = NULL;
692int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700693static struct kobject *dev_kobj;
694struct kobject *sysfs_dev_char_kobj;
695struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200697static DEFINE_MUTEX(device_hotplug_lock);
698
699void lock_device_hotplug(void)
700{
701 mutex_lock(&device_hotplug_lock);
702}
703
704void unlock_device_hotplug(void)
705{
706 mutex_unlock(&device_hotplug_lock);
707}
708
709int lock_device_hotplug_sysfs(void)
710{
711 if (mutex_trylock(&device_hotplug_lock))
712 return 0;
713
714 /* Avoid busy looping (5 ms of sleep should do). */
715 msleep(5);
716 return restart_syscall();
717}
718
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800719#ifdef CONFIG_BLOCK
720static inline int device_is_not_partition(struct device *dev)
721{
722 return !(dev->type == &part_type);
723}
724#else
725static inline int device_is_not_partition(struct device *dev)
726{
727 return 1;
728}
729#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Alan Stern3e956372006-06-16 17:10:48 -0400731/**
732 * dev_driver_string - Return a device's driver name, if at all possible
733 * @dev: struct device to get the name of
734 *
735 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800736 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400737 * it is attached to. If it is not attached to a bus either, an empty
738 * string will be returned.
739 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700740const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400741{
Alan Stern35899722009-12-04 11:06:57 -0500742 struct device_driver *drv;
743
744 /* dev->driver can change to NULL underneath us because of unbinding,
745 * so be careful about accessing it. dev->bus and dev->class should
746 * never change once they are set, so they don't need special care.
747 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700748 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500749 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100750 (dev->bus ? dev->bus->name :
751 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400752}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600753EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
756
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800757static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
758 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800760 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200761 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500762 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400765 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800766 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900767 printk("dev_attr_show: %pS returned bad count\n",
768 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return ret;
771}
772
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800773static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
774 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800776 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200777 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500778 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400781 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return ret;
783}
784
Emese Revfy52cf25d2010-01-19 02:58:23 +0100785static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .show = dev_attr_show,
787 .store = dev_attr_store,
788};
789
Kay Sieversca22e562011-12-14 14:29:38 -0800790#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
791
792ssize_t device_store_ulong(struct device *dev,
793 struct device_attribute *attr,
794 const char *buf, size_t size)
795{
796 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -0800797 int ret;
798 unsigned long new;
799
800 ret = kstrtoul(buf, 0, &new);
801 if (ret)
802 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -0800803 *(unsigned long *)(ea->var) = new;
804 /* Always return full write size even if we didn't consume all */
805 return size;
806}
807EXPORT_SYMBOL_GPL(device_store_ulong);
808
809ssize_t device_show_ulong(struct device *dev,
810 struct device_attribute *attr,
811 char *buf)
812{
813 struct dev_ext_attribute *ea = to_ext_attr(attr);
814 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
815}
816EXPORT_SYMBOL_GPL(device_show_ulong);
817
818ssize_t device_store_int(struct device *dev,
819 struct device_attribute *attr,
820 const char *buf, size_t size)
821{
822 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -0800823 int ret;
824 long new;
825
826 ret = kstrtol(buf, 0, &new);
827 if (ret)
828 return ret;
829
830 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -0800831 return -EINVAL;
832 *(int *)(ea->var) = new;
833 /* Always return full write size even if we didn't consume all */
834 return size;
835}
836EXPORT_SYMBOL_GPL(device_store_int);
837
838ssize_t device_show_int(struct device *dev,
839 struct device_attribute *attr,
840 char *buf)
841{
842 struct dev_ext_attribute *ea = to_ext_attr(attr);
843
844 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
845}
846EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Borislav Petkov91872392012-10-09 19:52:05 +0200848ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
849 const char *buf, size_t size)
850{
851 struct dev_ext_attribute *ea = to_ext_attr(attr);
852
853 if (strtobool(buf, ea->var) < 0)
854 return -EINVAL;
855
856 return size;
857}
858EXPORT_SYMBOL_GPL(device_store_bool);
859
860ssize_t device_show_bool(struct device *dev, 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", *(bool *)(ea->var));
866}
867EXPORT_SYMBOL_GPL(device_show_bool);
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400870 * device_release - free device structure.
871 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400873 * This is called once the reference count for the object
874 * reaches 0. We forward the call to the device's release
875 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800877static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200879 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800880 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Ming Leia525a3d2012-07-25 01:42:29 +0800882 /*
883 * Some platform devices are driven without driver attached
884 * and managed resources may have been acquired. Make sure
885 * all resources are released.
886 *
887 * Drivers still can add resources into device after device
888 * is deleted but alive, so release devres here to avoid
889 * possible memory leak.
890 */
891 devres_release_all(dev);
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (dev->release)
894 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200895 else if (dev->type && dev->type->release)
896 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700897 else if (dev->class && dev->class->dev_release)
898 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700899 else
900 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800901 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100902 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800903 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700906static const void *device_namespace(struct kobject *kobj)
907{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200908 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700909 const void *ns = NULL;
910
911 if (dev->class && dev->class->ns_type)
912 ns = dev->class->namespace(dev);
913
914 return ns;
915}
916
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000917static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
918{
919 struct device *dev = kobj_to_dev(kobj);
920
921 if (dev->class && dev->class->get_ownership)
922 dev->class->get_ownership(dev, uid, gid);
923}
924
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600925static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 .release = device_release,
927 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700928 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000929 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930};
931
932
Kay Sievers312c0042005-11-16 09:00:00 +0100933static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct kobj_type *ktype = get_ktype(kobj);
936
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600937 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200938 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (dev->bus)
940 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700941 if (dev->class)
942 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 return 0;
945}
946
Kay Sievers312c0042005-11-16 09:00:00 +0100947static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200949 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700951 if (dev->bus)
952 return dev->bus->name;
953 if (dev->class)
954 return dev->class->name;
955 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Kay Sievers7eff2e72007-08-14 15:15:12 +0200958static int dev_uevent(struct kset *kset, struct kobject *kobj,
959 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200961 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int retval = 0;
963
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200964 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700965 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200966 const char *tmp;
967 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400968 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700969 kuid_t uid = GLOBAL_ROOT_UID;
970 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200971
Kay Sievers7eff2e72007-08-14 15:15:12 +0200972 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
973 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700974 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200975 if (name) {
976 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200977 if (mode)
978 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700979 if (!uid_eq(uid, GLOBAL_ROOT_UID))
980 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
981 if (!gid_eq(gid, GLOBAL_ROOT_GID))
982 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700983 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200984 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700985 }
986
Kay Sievers414264f2007-03-12 21:08:57 +0100987 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200988 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100989
Kay Sievers239378f2006-10-07 21:54:55 +0200990 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200991 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200992
Grant Likely07d57a32012-02-01 11:22:22 -0700993 /* Add common DT information about the device */
994 of_device_uevent(dev, env);
995
Kay Sievers7eff2e72007-08-14 15:15:12 +0200996 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100997 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200998 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200999 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001000 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001001 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
Kay Sievers7eff2e72007-08-14 15:15:12 +02001004 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001005 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001006 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001007 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001008 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001009 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001010 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001011 }
1012
Stefan Weileef35c22010-08-06 21:11:15 +02001013 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02001014 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001015 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001016 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001017 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001018 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001019 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001020 }
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return retval;
1023}
1024
Emese Revfy9cd43612009-12-31 14:52:51 +01001025static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01001026 .filter = dev_uevent_filter,
1027 .name = dev_uevent_name,
1028 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029};
1030
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001031static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02001032 char *buf)
1033{
1034 struct kobject *top_kobj;
1035 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001036 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02001037 int i;
1038 size_t count = 0;
1039 int retval;
1040
1041 /* search the kset, the device belongs to */
1042 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001043 while (!top_kobj->kset && top_kobj->parent)
1044 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02001045 if (!top_kobj->kset)
1046 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001047
Kay Sievers16574dc2007-04-06 01:40:38 +02001048 kset = top_kobj->kset;
1049 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1050 goto out;
1051
1052 /* respect filter */
1053 if (kset->uevent_ops && kset->uevent_ops->filter)
1054 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1055 goto out;
1056
Kay Sievers7eff2e72007-08-14 15:15:12 +02001057 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1058 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001059 return -ENOMEM;
1060
Kay Sievers16574dc2007-04-06 01:40:38 +02001061 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001062 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001063 if (retval)
1064 goto out;
1065
1066 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001067 for (i = 0; i < env->envp_idx; i++)
1068 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001069out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001070 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001071 return count;
1072}
1073
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001074static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001075 const char *buf, size_t count)
1076{
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001077 if (kobject_synth_uevent(&dev->kobj, buf, count))
1078 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Kay Sievers60a96a52007-07-08 22:29:26 +02001079
Kay Sieversa7fd6702005-10-01 14:49:43 +02001080 return count;
1081}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001082static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001083
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001084static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001085 char *buf)
1086{
1087 bool val;
1088
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001089 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001090 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001091 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001092 return sprintf(buf, "%u\n", val);
1093}
1094
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001095static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001096 const char *buf, size_t count)
1097{
1098 bool val;
1099 int ret;
1100
1101 ret = strtobool(buf, &val);
1102 if (ret < 0)
1103 return ret;
1104
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001105 ret = lock_device_hotplug_sysfs();
1106 if (ret)
1107 return ret;
1108
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001109 ret = val ? device_online(dev) : device_offline(dev);
1110 unlock_device_hotplug();
1111 return ret < 0 ? ret : count;
1112}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001113static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001114
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001115int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001116{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001117 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001118}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001119EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001120
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001121void device_remove_groups(struct device *dev,
1122 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001123{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001124 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001125}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001126EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001127
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001128union device_attr_group_devres {
1129 const struct attribute_group *group;
1130 const struct attribute_group **groups;
1131};
1132
1133static int devm_attr_group_match(struct device *dev, void *res, void *data)
1134{
1135 return ((union device_attr_group_devres *)res)->group == data;
1136}
1137
1138static void devm_attr_group_remove(struct device *dev, void *res)
1139{
1140 union device_attr_group_devres *devres = res;
1141 const struct attribute_group *group = devres->group;
1142
1143 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1144 sysfs_remove_group(&dev->kobj, group);
1145}
1146
1147static void devm_attr_groups_remove(struct device *dev, void *res)
1148{
1149 union device_attr_group_devres *devres = res;
1150 const struct attribute_group **groups = devres->groups;
1151
1152 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1153 sysfs_remove_groups(&dev->kobj, groups);
1154}
1155
1156/**
1157 * devm_device_add_group - given a device, create a managed attribute group
1158 * @dev: The device to create the group for
1159 * @grp: The attribute group to create
1160 *
1161 * This function creates a group for the first time. It will explicitly
1162 * warn and error if any of the attribute files being created already exist.
1163 *
1164 * Returns 0 on success or error code on failure.
1165 */
1166int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1167{
1168 union device_attr_group_devres *devres;
1169 int error;
1170
1171 devres = devres_alloc(devm_attr_group_remove,
1172 sizeof(*devres), GFP_KERNEL);
1173 if (!devres)
1174 return -ENOMEM;
1175
1176 error = sysfs_create_group(&dev->kobj, grp);
1177 if (error) {
1178 devres_free(devres);
1179 return error;
1180 }
1181
1182 devres->group = grp;
1183 devres_add(dev, devres);
1184 return 0;
1185}
1186EXPORT_SYMBOL_GPL(devm_device_add_group);
1187
1188/**
1189 * devm_device_remove_group: remove a managed group from a device
1190 * @dev: device to remove the group from
1191 * @grp: group to remove
1192 *
1193 * This function removes a group of attributes from a device. The attributes
1194 * previously have to have been created for this group, otherwise it will fail.
1195 */
1196void devm_device_remove_group(struct device *dev,
1197 const struct attribute_group *grp)
1198{
1199 WARN_ON(devres_release(dev, devm_attr_group_remove,
1200 devm_attr_group_match,
1201 /* cast away const */ (void *)grp));
1202}
1203EXPORT_SYMBOL_GPL(devm_device_remove_group);
1204
1205/**
1206 * devm_device_add_groups - create a bunch of managed attribute groups
1207 * @dev: The device to create the group for
1208 * @groups: The attribute groups to create, NULL terminated
1209 *
1210 * This function creates a bunch of managed attribute groups. If an error
1211 * occurs when creating a group, all previously created groups will be
1212 * removed, unwinding everything back to the original state when this
1213 * function was called. It will explicitly warn and error if any of the
1214 * attribute files being created already exist.
1215 *
1216 * Returns 0 on success or error code from sysfs_create_group on failure.
1217 */
1218int devm_device_add_groups(struct device *dev,
1219 const struct attribute_group **groups)
1220{
1221 union device_attr_group_devres *devres;
1222 int error;
1223
1224 devres = devres_alloc(devm_attr_groups_remove,
1225 sizeof(*devres), GFP_KERNEL);
1226 if (!devres)
1227 return -ENOMEM;
1228
1229 error = sysfs_create_groups(&dev->kobj, groups);
1230 if (error) {
1231 devres_free(devres);
1232 return error;
1233 }
1234
1235 devres->groups = groups;
1236 devres_add(dev, devres);
1237 return 0;
1238}
1239EXPORT_SYMBOL_GPL(devm_device_add_groups);
1240
1241/**
1242 * devm_device_remove_groups - remove a list of managed groups
1243 *
1244 * @dev: The device for the groups to be removed from
1245 * @groups: NULL terminated list of groups to be removed
1246 *
1247 * If groups is not NULL, remove the specified groups from the device.
1248 */
1249void devm_device_remove_groups(struct device *dev,
1250 const struct attribute_group **groups)
1251{
1252 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1253 devm_attr_group_match,
1254 /* cast away const */ (void *)groups));
1255}
1256EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001258static int device_add_attrs(struct device *dev)
1259{
1260 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001261 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001262 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001263
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001264 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001265 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001266 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001267 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001268 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001269
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001270 if (type) {
1271 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001272 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001273 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001274 }
1275
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001276 error = device_add_groups(dev, dev->groups);
1277 if (error)
1278 goto err_remove_type_groups;
1279
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001280 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001281 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001282 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001283 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001284 }
1285
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001286 return 0;
1287
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001288 err_remove_dev_groups:
1289 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001290 err_remove_type_groups:
1291 if (type)
1292 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001293 err_remove_class_groups:
1294 if (class)
1295 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001296
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001297 return error;
1298}
1299
1300static void device_remove_attrs(struct device *dev)
1301{
1302 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001303 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001304
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001305 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001306 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001307
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001308 if (type)
1309 device_remove_groups(dev, type->groups);
1310
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001311 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001312 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001313}
1314
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001315static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001316 char *buf)
1317{
1318 return print_dev_t(buf, dev->devt);
1319}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001320static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001321
Kay Sieversca22e562011-12-14 14:29:38 -08001322/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001323struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001326 * devices_kset_move_before - Move device in the devices_kset's list.
1327 * @deva: Device to move.
1328 * @devb: Device @deva should come before.
1329 */
1330static void devices_kset_move_before(struct device *deva, struct device *devb)
1331{
1332 if (!devices_kset)
1333 return;
1334 pr_debug("devices_kset: Moving %s before %s\n",
1335 dev_name(deva), dev_name(devb));
1336 spin_lock(&devices_kset->list_lock);
1337 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1338 spin_unlock(&devices_kset->list_lock);
1339}
1340
1341/**
1342 * devices_kset_move_after - Move device in the devices_kset's list.
1343 * @deva: Device to move
1344 * @devb: Device @deva should come after.
1345 */
1346static void devices_kset_move_after(struct device *deva, struct device *devb)
1347{
1348 if (!devices_kset)
1349 return;
1350 pr_debug("devices_kset: Moving %s after %s\n",
1351 dev_name(deva), dev_name(devb));
1352 spin_lock(&devices_kset->list_lock);
1353 list_move(&deva->kobj.entry, &devb->kobj.entry);
1354 spin_unlock(&devices_kset->list_lock);
1355}
1356
1357/**
1358 * devices_kset_move_last - move the device to the end of devices_kset's list.
1359 * @dev: device to move
1360 */
1361void devices_kset_move_last(struct device *dev)
1362{
1363 if (!devices_kset)
1364 return;
1365 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1366 spin_lock(&devices_kset->list_lock);
1367 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1368 spin_unlock(&devices_kset->list_lock);
1369}
1370
1371/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001372 * device_create_file - create sysfs attribute file for device.
1373 * @dev: device.
1374 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001376int device_create_file(struct device *dev,
1377 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001380
1381 if (dev) {
1382 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001383 "Attribute %s: write permission without 'store'\n",
1384 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001385 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001386 "Attribute %s: read permission without 'show'\n",
1387 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001389 }
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return error;
1392}
David Graham White86df2682013-07-21 20:41:14 -04001393EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001396 * device_remove_file - remove sysfs attribute file.
1397 * @dev: device.
1398 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001400void device_remove_file(struct device *dev,
1401 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001403 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
David Graham White86df2682013-07-21 20:41:14 -04001406EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001408/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001409 * device_remove_file_self - remove sysfs attribute file from its own method.
1410 * @dev: device.
1411 * @attr: device attribute descriptor.
1412 *
1413 * See kernfs_remove_self() for details.
1414 */
1415bool device_remove_file_self(struct device *dev,
1416 const struct device_attribute *attr)
1417{
1418 if (dev)
1419 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1420 else
1421 return false;
1422}
1423EXPORT_SYMBOL_GPL(device_remove_file_self);
1424
1425/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001426 * device_create_bin_file - create sysfs binary attribute file for device.
1427 * @dev: device.
1428 * @attr: device binary attribute descriptor.
1429 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001430int device_create_bin_file(struct device *dev,
1431 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001432{
1433 int error = -EINVAL;
1434 if (dev)
1435 error = sysfs_create_bin_file(&dev->kobj, attr);
1436 return error;
1437}
1438EXPORT_SYMBOL_GPL(device_create_bin_file);
1439
1440/**
1441 * device_remove_bin_file - remove sysfs binary attribute file
1442 * @dev: device.
1443 * @attr: device binary attribute descriptor.
1444 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001445void device_remove_bin_file(struct device *dev,
1446 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001447{
1448 if (dev)
1449 sysfs_remove_bin_file(&dev->kobj, attr);
1450}
1451EXPORT_SYMBOL_GPL(device_remove_bin_file);
1452
James Bottomley34bb61f2005-09-06 16:56:51 -07001453static void klist_children_get(struct klist_node *n)
1454{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001455 struct device_private *p = to_device_private_parent(n);
1456 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001457
1458 get_device(dev);
1459}
1460
1461static void klist_children_put(struct klist_node *n)
1462{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001463 struct device_private *p = to_device_private_parent(n);
1464 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001465
1466 put_device(dev);
1467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001470 * device_initialize - init device structure.
1471 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 *
Cornelia Huck57394112008-09-03 18:26:40 +02001473 * This prepares the device for use by other layers by initializing
1474 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001475 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001476 * that function, though it can also be called separately, so one
1477 * may use @dev's fields. In particular, get_device()/put_device()
1478 * may be used for reference counting of @dev after calling this
1479 * function.
1480 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001481 * All fields in @dev must be initialized by the caller to 0, except
1482 * for those explicitly set to some other value. The simplest
1483 * approach is to use kzalloc() to allocate the structure containing
1484 * @dev.
1485 *
Cornelia Huck57394112008-09-03 18:26:40 +02001486 * NOTE: Use put_device() to give up your reference instead of freeing
1487 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489void device_initialize(struct device *dev)
1490{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001491 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001492 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001494 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001495 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001496 spin_lock_init(&dev->devres_lock);
1497 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001498 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001499 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001500#ifdef CONFIG_GENERIC_MSI_IRQ
1501 INIT_LIST_HEAD(&dev->msi_list);
1502#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001503 INIT_LIST_HEAD(&dev->links.consumers);
1504 INIT_LIST_HEAD(&dev->links.suppliers);
1505 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
David Graham White86df2682013-07-21 20:41:14 -04001507EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Tejun Heod73ce002013-03-12 11:30:05 -07001509struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001510{
Kay Sievers86406242007-03-14 03:25:56 +01001511 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001512
Kay Sievers86406242007-03-14 03:25:56 +01001513 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001514 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001515 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001516
Kay Sievers86406242007-03-14 03:25:56 +01001517 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001518}
1519
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001520struct class_dir {
1521 struct kobject kobj;
1522 struct class *class;
1523};
1524
1525#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1526
1527static void class_dir_release(struct kobject *kobj)
1528{
1529 struct class_dir *dir = to_class_dir(kobj);
1530 kfree(dir);
1531}
1532
1533static const
1534struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1535{
1536 struct class_dir *dir = to_class_dir(kobj);
1537 return dir->class->ns_type;
1538}
1539
1540static struct kobj_type class_dir_ktype = {
1541 .release = class_dir_release,
1542 .sysfs_ops = &kobj_sysfs_ops,
1543 .child_ns_type = class_dir_child_ns_type
1544};
1545
1546static struct kobject *
1547class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1548{
1549 struct class_dir *dir;
1550 int retval;
1551
1552 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1553 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001554 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001555
1556 dir->class = class;
1557 kobject_init(&dir->kobj, &class_dir_ktype);
1558
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001559 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001560
1561 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1562 if (retval < 0) {
1563 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001564 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001565 }
1566 return &dir->kobj;
1567}
1568
Yijing Wange4a60d12014-11-07 12:05:49 +08001569static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001570
Kay Sieversda231fd2007-11-21 17:29:15 +01001571static struct kobject *get_device_parent(struct device *dev,
1572 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001573{
Kay Sievers86406242007-03-14 03:25:56 +01001574 if (dev->class) {
1575 struct kobject *kobj = NULL;
1576 struct kobject *parent_kobj;
1577 struct kobject *k;
1578
Randy Dunlapead454f2010-09-24 14:36:49 -07001579#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001580 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001581 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001582 if (parent && parent->class == &block_class)
1583 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001584 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001585 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001586#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001587
Kay Sievers86406242007-03-14 03:25:56 +01001588 /*
1589 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001590 * Class-devices with a non class-device as parent, live
1591 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001592 */
1593 if (parent == NULL)
1594 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001595 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001596 return &parent->kobj;
1597 else
1598 parent_kobj = &parent->kobj;
1599
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001600 mutex_lock(&gdp_mutex);
1601
Kay Sievers86406242007-03-14 03:25:56 +01001602 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001603 spin_lock(&dev->class->p->glue_dirs.list_lock);
1604 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001605 if (k->parent == parent_kobj) {
1606 kobj = kobject_get(k);
1607 break;
1608 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001609 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001610 if (kobj) {
1611 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001612 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001613 }
Kay Sievers86406242007-03-14 03:25:56 +01001614
1615 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001616 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001617 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001618 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001619 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001620 }
1621
Kay Sieversca22e562011-12-14 14:29:38 -08001622 /* subsystems can specify a default root directory for their devices */
1623 if (!parent && dev->bus && dev->bus->dev_root)
1624 return &dev->bus->dev_root->kobj;
1625
Kay Sievers86406242007-03-14 03:25:56 +01001626 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001627 return &parent->kobj;
1628 return NULL;
1629}
Kay Sieversda231fd2007-11-21 17:29:15 +01001630
Ming Leicebf8fd2016-07-10 19:27:36 +08001631static inline bool live_in_glue_dir(struct kobject *kobj,
1632 struct device *dev)
1633{
1634 if (!kobj || !dev->class ||
1635 kobj->kset != &dev->class->p->glue_dirs)
1636 return false;
1637 return true;
1638}
1639
1640static inline struct kobject *get_glue_dir(struct device *dev)
1641{
1642 return dev->kobj.parent;
1643}
1644
1645/*
1646 * make sure cleaning up dir as the last step, we need to make
1647 * sure .release handler of kobject is run with holding the
1648 * global lock
1649 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001650static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001651{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001652 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001653 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001654 return;
1655
Yijing Wange4a60d12014-11-07 12:05:49 +08001656 mutex_lock(&gdp_mutex);
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10001657 if (!kobject_has_children(glue_dir))
1658 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001659 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001660 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001661}
Cornelia Huck63b69712008-01-21 16:09:44 +01001662
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001663static int device_add_class_symlinks(struct device *dev)
1664{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001665 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001666 int error;
1667
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001668 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001669 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001670 if (error)
1671 dev_warn(dev, "Error %d creating of_node link\n",error);
1672 /* An error here doesn't warrant bringing down the device */
1673 }
1674
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001675 if (!dev->class)
1676 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001677
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001678 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001679 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001680 "subsystem");
1681 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001682 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001683
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001684 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001685 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1686 "device");
1687 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001688 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001689 }
Kay Sievers39aba962010-09-04 22:33:14 -07001690
Randy Dunlapead454f2010-09-24 14:36:49 -07001691#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001692 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001693 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001694 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001695#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001696
1697 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001698 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001699 &dev->kobj, dev_name(dev));
1700 if (error)
1701 goto out_device;
1702
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001703 return 0;
1704
Kay Sievers39aba962010-09-04 22:33:14 -07001705out_device:
1706 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001707
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001708out_subsys:
1709 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001710out_devnode:
1711 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001712 return error;
1713}
1714
1715static void device_remove_class_symlinks(struct device *dev)
1716{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001717 if (dev_of_node(dev))
1718 sysfs_remove_link(&dev->kobj, "of_node");
1719
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001720 if (!dev->class)
1721 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001722
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001723 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001724 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001725 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001726#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001727 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001728 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001729#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001730 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001734 * dev_set_name - set a device name
1735 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001736 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001737 */
1738int dev_set_name(struct device *dev, const char *fmt, ...)
1739{
1740 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001741 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001742
1743 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001744 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001745 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001746 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001747}
1748EXPORT_SYMBOL_GPL(dev_set_name);
1749
1750/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001751 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1752 * @dev: device
1753 *
1754 * By default we select char/ for new entries. Setting class->dev_obj
1755 * to NULL prevents an entry from being created. class->dev_kobj must
1756 * be set (or cleared) before any devices are registered to the class
1757 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001758 * device_remove_sys_dev_entry() will disagree about the presence of
1759 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001760 */
1761static struct kobject *device_to_dev_kobj(struct device *dev)
1762{
1763 struct kobject *kobj;
1764
1765 if (dev->class)
1766 kobj = dev->class->dev_kobj;
1767 else
1768 kobj = sysfs_dev_char_kobj;
1769
1770 return kobj;
1771}
1772
1773static int device_create_sys_dev_entry(struct device *dev)
1774{
1775 struct kobject *kobj = device_to_dev_kobj(dev);
1776 int error = 0;
1777 char devt_str[15];
1778
1779 if (kobj) {
1780 format_dev_t(devt_str, dev->devt);
1781 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1782 }
1783
1784 return error;
1785}
1786
1787static void device_remove_sys_dev_entry(struct device *dev)
1788{
1789 struct kobject *kobj = device_to_dev_kobj(dev);
1790 char devt_str[15];
1791
1792 if (kobj) {
1793 format_dev_t(devt_str, dev->devt);
1794 sysfs_remove_link(kobj, devt_str);
1795 }
1796}
1797
Shaokun Zhang46d3a032018-07-15 18:08:56 +08001798static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001799{
1800 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1801 if (!dev->p)
1802 return -ENOMEM;
1803 dev->p->device = dev;
1804 klist_init(&dev->p->klist_children, klist_children_get,
1805 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001806 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001807 return 0;
1808}
1809
Dan Williamse105b8b2008-04-21 10:51:07 -07001810/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001811 * device_add - add device to device hierarchy.
1812 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001814 * This is part 2 of device_register(), though may be called
1815 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 *
Cornelia Huck57394112008-09-03 18:26:40 +02001817 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001818 * to the global and sibling lists for the device, then
1819 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001820 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001821 * Do not call this routine or device_register() more than once for
1822 * any device structure. The driver model core is not designed to work
1823 * with devices that get unregistered and then spring back to life.
1824 * (Among other things, it's very hard to guarantee that all references
1825 * to the previous incarnation of @dev have been dropped.) Allocate
1826 * and register a fresh new struct device instead.
1827 *
Cornelia Huck57394112008-09-03 18:26:40 +02001828 * NOTE: _Never_ directly free @dev after calling this function, even
1829 * if it returned an error! Always use put_device() to give up your
1830 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 */
1832int device_add(struct device *dev)
1833{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301834 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001835 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001836 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001837 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001838 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001841 if (!dev)
1842 goto done;
1843
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001844 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001845 error = device_private_init(dev);
1846 if (error)
1847 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001848 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001849
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001850 /*
1851 * for statically allocated devices, which should all be converted
1852 * some day, we need to initialize the name. We prevent reading back
1853 * the name, and force the use of dev_name()
1854 */
1855 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001856 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001857 dev->init_name = NULL;
1858 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001859
Kay Sieversca22e562011-12-14 14:29:38 -08001860 /* subsystems can specify simple device enumeration */
1861 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1862 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1863
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001864 if (!dev_name(dev)) {
1865 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001866 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001869 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001872 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001873 if (IS_ERR(kobj)) {
1874 error = PTR_ERR(kobj);
1875 goto parent_error;
1876 }
Kay Sieversca22e562011-12-14 14:29:38 -08001877 if (kobj)
1878 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Yinghai Lu0d358f22008-02-19 03:20:41 -08001880 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001881 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001882 set_dev_node(dev, dev_to_node(parent));
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001885 /* we require the name to be set before, and pass NULL */
1886 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001887 if (error) {
1888 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001890 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001891
Brian Walsh37022642006-08-14 22:43:19 -07001892 /* notify platform of device entry */
1893 if (platform_notify)
1894 platform_notify(dev);
1895
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001896 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001897 if (error)
1898 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001899
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001900 error = device_add_class_symlinks(dev);
1901 if (error)
1902 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001903 error = device_add_attrs(dev);
1904 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001905 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001906 error = bus_add_device(dev);
1907 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001909 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001910 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001911 goto DPMError;
1912 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001913
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001914 if (MAJOR(dev->devt)) {
1915 error = device_create_file(dev, &dev_attr_dev);
1916 if (error)
1917 goto DevAttrError;
1918
1919 error = device_create_sys_dev_entry(dev);
1920 if (error)
1921 goto SysEntryError;
1922
1923 devtmpfs_create_node(dev);
1924 }
1925
Alan Sternec0676ee2008-12-05 14:10:31 -05001926 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001927 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001928 */
1929 if (dev->bus)
1930 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1931 BUS_NOTIFY_ADD_DEVICE, dev);
1932
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001933 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001934 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001936 klist_add_tail(&dev->p->knode_parent,
1937 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001939 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001940 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001941 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001942 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001943 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001944
1945 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001946 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001947 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001948 if (class_intf->add_dev)
1949 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001950 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001951 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001952done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 put_device(dev);
1954 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001955 SysEntryError:
1956 if (MAJOR(dev->devt))
1957 device_remove_file(dev, &dev_attr_dev);
1958 DevAttrError:
1959 device_pm_remove(dev);
1960 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001961 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001962 bus_remove_device(dev);
1963 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001964 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001965 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001966 device_remove_class_symlinks(dev);
1967 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001968 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001969 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001970 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001971 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 kobject_del(&dev->kobj);
1973 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001974 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001975parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01001976 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001977name_error:
1978 kfree(dev->p);
1979 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001980 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
David Graham White86df2682013-07-21 20:41:14 -04001982EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001985 * device_register - register a device with the system.
1986 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001988 * This happens in two clean steps - initialize the device
1989 * and add it to the system. The two steps can be called
1990 * separately, but this is the easiest and most common.
1991 * I.e. you should only call the two helpers separately if
1992 * have a clearly defined need to use and refcount the device
1993 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001994 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001995 * For more information, see the kerneldoc for device_initialize()
1996 * and device_add().
1997 *
Cornelia Huck57394112008-09-03 18:26:40 +02001998 * NOTE: _Never_ directly free @dev after calling this function, even
1999 * if it returned an error! Always use put_device() to give up the
2000 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002int device_register(struct device *dev)
2003{
2004 device_initialize(dev);
2005 return device_add(dev);
2006}
David Graham White86df2682013-07-21 20:41:14 -04002007EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002010 * get_device - increment reference count for device.
2011 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002013 * This simply forwards the call to kobject_get(), though
2014 * we do take care to provide for the case that we get a NULL
2015 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002017struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002019 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
David Graham White86df2682013-07-21 20:41:14 -04002021EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002024 * put_device - decrement reference count.
2025 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002027void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02002029 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (dev)
2031 kobject_put(&dev->kobj);
2032}
David Graham White86df2682013-07-21 20:41:14 -04002033EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002036 * device_del - delete device from system.
2037 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002039 * This is the first part of the device unregistration
2040 * sequence. This removes the device from the lists we control
2041 * from here, has it removed from the other driver model
2042 * subsystems it was added to in device_add(), and removes it
2043 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002045 * NOTE: this should be called manually _iff_ device_add() was
2046 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002048void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002050 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002051 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002052 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Alan Sternec0676ee2008-12-05 14:10:31 -05002054 /* Notify clients of device removal. This call must come
2055 * before dpm_sysfs_remove().
2056 */
2057 if (dev->bus)
2058 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2059 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002060
Alan Stern3b98aea2008-08-07 13:06:12 -04002061 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002063 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002064 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002065 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002066 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002067 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002068 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002069 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002070 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002071
Kay Sieversca22e562011-12-14 14:29:38 -08002072 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002073 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002074 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002075 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002076 if (class_intf->remove_dev)
2077 class_intf->remove_dev(dev, class_intf);
2078 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02002079 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002080 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002081 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002082 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002083 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002084 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002085 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002086 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02002087 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002088 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /* Notify the platform of the removal, in case they
2091 * need to do anything...
2092 */
2093 if (platform_notify_remove)
2094 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02002095 if (dev->bus)
2096 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2097 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002098 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002099 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002101 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002102 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
David Graham White86df2682013-07-21 20:41:14 -04002104EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002107 * device_unregister - unregister device from system.
2108 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002110 * We do this in two parts, like we do device_register(). First,
2111 * we remove it from all the subsystems with device_del(), then
2112 * we decrement the reference count via put_device(). If that
2113 * is the final reference count, the device will be cleaned up
2114 * via device_release() above. Otherwise, the structure will
2115 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002117void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002119 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 device_del(dev);
2121 put_device(dev);
2122}
David Graham White86df2682013-07-21 20:41:14 -04002123EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002125static struct device *prev_device(struct klist_iter *i)
2126{
2127 struct klist_node *n = klist_prev(i);
2128 struct device *dev = NULL;
2129 struct device_private *p;
2130
2131 if (n) {
2132 p = to_device_private_parent(n);
2133 dev = p->device;
2134 }
2135 return dev;
2136}
2137
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002138static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002139{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002140 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002141 struct device *dev = NULL;
2142 struct device_private *p;
2143
2144 if (n) {
2145 p = to_device_private_parent(n);
2146 dev = p->device;
2147 }
2148 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002149}
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002152 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002153 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002154 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002155 * @uid: returned file owner
2156 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002157 * @tmp: possibly allocated string
2158 *
2159 * Return the relative path of a possible device node.
2160 * Non-default names may need to allocate a memory to compose
2161 * a name. This memory is returned in tmp and needs to be
2162 * freed by the caller.
2163 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002164const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002165 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002166 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002167{
2168 char *s;
2169
2170 *tmp = NULL;
2171
2172 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002173 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002174 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002175 if (*tmp)
2176 return *tmp;
2177
2178 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002179 if (dev->class && dev->class->devnode)
2180 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002181 if (*tmp)
2182 return *tmp;
2183
2184 /* return name without allocation, tmp == NULL */
2185 if (strchr(dev_name(dev), '!') == NULL)
2186 return dev_name(dev);
2187
2188 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002189 s = kstrdup(dev_name(dev), GFP_KERNEL);
2190 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002191 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002192 strreplace(s, '!', '/');
2193 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002194}
2195
2196/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002197 * device_for_each_child - device child iterator.
2198 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002199 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002200 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002202 * Iterate over @parent's child devices, and call @fn for each,
2203 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002205 * We check the return of @fn each time. If it returns anything
2206 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002208int device_for_each_child(struct device *parent, void *data,
2209 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002211 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002212 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 int error = 0;
2214
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002215 if (!parent->p)
2216 return 0;
2217
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002218 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002219 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002220 error = fn(child, data);
2221 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return error;
2223}
David Graham White86df2682013-07-21 20:41:14 -04002224EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Cornelia Huck5ab69982006-11-16 15:42:07 +01002226/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002227 * device_for_each_child_reverse - device child iterator in reversed order.
2228 * @parent: parent struct device.
2229 * @fn: function to be called for each device.
2230 * @data: data for the callback.
2231 *
2232 * Iterate over @parent's child devices, and call @fn for each,
2233 * passing it @data.
2234 *
2235 * We check the return of @fn each time. If it returns anything
2236 * other than 0, we break out and return that value.
2237 */
2238int device_for_each_child_reverse(struct device *parent, void *data,
2239 int (*fn)(struct device *dev, void *data))
2240{
2241 struct klist_iter i;
2242 struct device *child;
2243 int error = 0;
2244
2245 if (!parent->p)
2246 return 0;
2247
2248 klist_iter_init(&parent->p->klist_children, &i);
2249 while ((child = prev_device(&i)) && !error)
2250 error = fn(child, data);
2251 klist_iter_exit(&i);
2252 return error;
2253}
2254EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2255
2256/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002257 * device_find_child - device iterator for locating a particular device.
2258 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002259 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002260 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002261 *
2262 * This is similar to the device_for_each_child() function above, but it
2263 * returns a reference to a device that is 'found' for later use, as
2264 * determined by the @match callback.
2265 *
2266 * The callback should return 0 if the device doesn't match and non-zero
2267 * if it does. If the callback returns non-zero and a reference to the
2268 * current device can be obtained, this function will return to the caller
2269 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002270 *
2271 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002272 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002273struct device *device_find_child(struct device *parent, void *data,
2274 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002275{
2276 struct klist_iter i;
2277 struct device *child;
2278
2279 if (!parent)
2280 return NULL;
2281
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002282 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002283 while ((child = next_device(&i)))
2284 if (match(child, data) && get_device(child))
2285 break;
2286 klist_iter_exit(&i);
2287 return child;
2288}
David Graham White86df2682013-07-21 20:41:14 -04002289EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291int __init devices_init(void)
2292{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002293 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2294 if (!devices_kset)
2295 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002296 dev_kobj = kobject_create_and_add("dev", NULL);
2297 if (!dev_kobj)
2298 goto dev_kobj_err;
2299 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2300 if (!sysfs_dev_block_kobj)
2301 goto block_kobj_err;
2302 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2303 if (!sysfs_dev_char_kobj)
2304 goto char_kobj_err;
2305
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002306 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002307
2308 char_kobj_err:
2309 kobject_put(sysfs_dev_block_kobj);
2310 block_kobj_err:
2311 kobject_put(dev_kobj);
2312 dev_kobj_err:
2313 kset_unregister(devices_kset);
2314 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002317static int device_check_offline(struct device *dev, void *not_used)
2318{
2319 int ret;
2320
2321 ret = device_for_each_child(dev, NULL, device_check_offline);
2322 if (ret)
2323 return ret;
2324
2325 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2326}
2327
2328/**
2329 * device_offline - Prepare the device for hot-removal.
2330 * @dev: Device to be put offline.
2331 *
2332 * Execute the device bus type's .offline() callback, if present, to prepare
2333 * the device for a subsequent hot-removal. If that succeeds, the device must
2334 * not be used until either it is removed or its bus type's .online() callback
2335 * is executed.
2336 *
2337 * Call under device_hotplug_lock.
2338 */
2339int device_offline(struct device *dev)
2340{
2341 int ret;
2342
2343 if (dev->offline_disabled)
2344 return -EPERM;
2345
2346 ret = device_for_each_child(dev, NULL, device_check_offline);
2347 if (ret)
2348 return ret;
2349
2350 device_lock(dev);
2351 if (device_supports_offline(dev)) {
2352 if (dev->offline) {
2353 ret = 1;
2354 } else {
2355 ret = dev->bus->offline(dev);
2356 if (!ret) {
2357 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2358 dev->offline = true;
2359 }
2360 }
2361 }
2362 device_unlock(dev);
2363
2364 return ret;
2365}
2366
2367/**
2368 * device_online - Put the device back online after successful device_offline().
2369 * @dev: Device to be put back online.
2370 *
2371 * If device_offline() has been successfully executed for @dev, but the device
2372 * has not been removed subsequently, execute its bus type's .online() callback
2373 * to indicate that the device can be used again.
2374 *
2375 * Call under device_hotplug_lock.
2376 */
2377int device_online(struct device *dev)
2378{
2379 int ret = 0;
2380
2381 device_lock(dev);
2382 if (device_supports_offline(dev)) {
2383 if (dev->offline) {
2384 ret = dev->bus->online(dev);
2385 if (!ret) {
2386 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2387 dev->offline = false;
2388 }
2389 } else {
2390 ret = 1;
2391 }
2392 }
2393 device_unlock(dev);
2394
2395 return ret;
2396}
2397
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002398struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002399 struct device dev;
2400 struct module *owner;
2401};
2402
Josh Triplett93058422012-11-18 21:27:55 -08002403static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002404{
2405 return container_of(d, struct root_device, dev);
2406}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002407
2408static void root_device_release(struct device *dev)
2409{
2410 kfree(to_root_device(dev));
2411}
2412
2413/**
2414 * __root_device_register - allocate and register a root device
2415 * @name: root device name
2416 * @owner: owner module of the root device, usually THIS_MODULE
2417 *
2418 * This function allocates a root device and registers it
2419 * using device_register(). In order to free the returned
2420 * device, use root_device_unregister().
2421 *
2422 * Root devices are dummy devices which allow other devices
2423 * to be grouped under /sys/devices. Use this function to
2424 * allocate a root device and then use it as the parent of
2425 * any device which should appear under /sys/devices/{name}
2426 *
2427 * The /sys/devices/{name} directory will also contain a
2428 * 'module' symlink which points to the @owner directory
2429 * in sysfs.
2430 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002431 * Returns &struct device pointer on success, or ERR_PTR() on error.
2432 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002433 * Note: You probably want to use root_device_register().
2434 */
2435struct device *__root_device_register(const char *name, struct module *owner)
2436{
2437 struct root_device *root;
2438 int err = -ENOMEM;
2439
2440 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2441 if (!root)
2442 return ERR_PTR(err);
2443
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002444 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002445 if (err) {
2446 kfree(root);
2447 return ERR_PTR(err);
2448 }
2449
2450 root->dev.release = root_device_release;
2451
2452 err = device_register(&root->dev);
2453 if (err) {
2454 put_device(&root->dev);
2455 return ERR_PTR(err);
2456 }
2457
Christoph Egger1d9e8822010-05-17 16:57:58 +02002458#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002459 if (owner) {
2460 struct module_kobject *mk = &owner->mkobj;
2461
2462 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2463 if (err) {
2464 device_unregister(&root->dev);
2465 return ERR_PTR(err);
2466 }
2467 root->owner = owner;
2468 }
2469#endif
2470
2471 return &root->dev;
2472}
2473EXPORT_SYMBOL_GPL(__root_device_register);
2474
2475/**
2476 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002477 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002478 *
2479 * This function unregisters and cleans up a device that was created by
2480 * root_device_register().
2481 */
2482void root_device_unregister(struct device *dev)
2483{
2484 struct root_device *root = to_root_device(dev);
2485
2486 if (root->owner)
2487 sysfs_remove_link(&root->dev.kobj, "module");
2488
2489 device_unregister(dev);
2490}
2491EXPORT_SYMBOL_GPL(root_device_unregister);
2492
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002493
2494static void device_create_release(struct device *dev)
2495{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002496 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002497 kfree(dev);
2498}
2499
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002500static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002501device_create_groups_vargs(struct class *class, struct device *parent,
2502 dev_t devt, void *drvdata,
2503 const struct attribute_group **groups,
2504 const char *fmt, va_list args)
2505{
2506 struct device *dev = NULL;
2507 int retval = -ENODEV;
2508
2509 if (class == NULL || IS_ERR(class))
2510 goto error;
2511
2512 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2513 if (!dev) {
2514 retval = -ENOMEM;
2515 goto error;
2516 }
2517
David Herrmannbbc780f2013-11-21 20:15:48 +01002518 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002519 dev->devt = devt;
2520 dev->class = class;
2521 dev->parent = parent;
2522 dev->groups = groups;
2523 dev->release = device_create_release;
2524 dev_set_drvdata(dev, drvdata);
2525
2526 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2527 if (retval)
2528 goto error;
2529
David Herrmannbbc780f2013-11-21 20:15:48 +01002530 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002531 if (retval)
2532 goto error;
2533
2534 return dev;
2535
2536error:
2537 put_device(dev);
2538 return ERR_PTR(retval);
2539}
2540
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002541/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002542 * device_create_vargs - creates a device and registers it with sysfs
2543 * @class: pointer to the struct class that this device should be registered to
2544 * @parent: pointer to the parent struct device of this new device, if any
2545 * @devt: the dev_t for the char device to be added
2546 * @drvdata: the data to be added to the device for callbacks
2547 * @fmt: string for the device's name
2548 * @args: va_list for the device's name
2549 *
2550 * This function can be used by char device classes. A struct device
2551 * will be created in sysfs, registered to the specified class.
2552 *
2553 * A "dev" file will be created, showing the dev_t for the device, if
2554 * the dev_t is not 0,0.
2555 * If a pointer to a parent struct device is passed in, the newly created
2556 * struct device will be a child of that device in sysfs.
2557 * The pointer to the struct device will be returned from the call.
2558 * Any further sysfs files that might be required can be created using this
2559 * pointer.
2560 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002561 * Returns &struct device pointer on success, or ERR_PTR() on error.
2562 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002563 * Note: the struct class passed to this function must have previously
2564 * been created with a call to class_create().
2565 */
2566struct device *device_create_vargs(struct class *class, struct device *parent,
2567 dev_t devt, void *drvdata, const char *fmt,
2568 va_list args)
2569{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002570 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2571 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002572}
2573EXPORT_SYMBOL_GPL(device_create_vargs);
2574
2575/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002576 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002577 * @class: pointer to the struct class that this device should be registered to
2578 * @parent: pointer to the parent struct device of this new device, if any
2579 * @devt: the dev_t for the char device to be added
2580 * @drvdata: the data to be added to the device for callbacks
2581 * @fmt: string for the device's name
2582 *
2583 * This function can be used by char device classes. A struct device
2584 * will be created in sysfs, registered to the specified class.
2585 *
2586 * A "dev" file will be created, showing the dev_t for the device, if
2587 * the dev_t is not 0,0.
2588 * If a pointer to a parent struct device is passed in, the newly created
2589 * struct device will be a child of that device in sysfs.
2590 * The pointer to the struct device will be returned from the call.
2591 * Any further sysfs files that might be required can be created using this
2592 * pointer.
2593 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002594 * Returns &struct device pointer on success, or ERR_PTR() on error.
2595 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002596 * Note: the struct class passed to this function must have previously
2597 * been created with a call to class_create().
2598 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002599struct device *device_create(struct class *class, struct device *parent,
2600 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002601{
2602 va_list vargs;
2603 struct device *dev;
2604
2605 va_start(vargs, fmt);
2606 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2607 va_end(vargs);
2608 return dev;
2609}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002610EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002611
Guenter Roeck39ef3112013-07-14 16:05:57 -07002612/**
2613 * device_create_with_groups - creates a device and registers it with sysfs
2614 * @class: pointer to the struct class that this device should be registered to
2615 * @parent: pointer to the parent struct device of this new device, if any
2616 * @devt: the dev_t for the char device to be added
2617 * @drvdata: the data to be added to the device for callbacks
2618 * @groups: NULL-terminated list of attribute groups to be created
2619 * @fmt: string for the device's name
2620 *
2621 * This function can be used by char device classes. A struct device
2622 * will be created in sysfs, registered to the specified class.
2623 * Additional attributes specified in the groups parameter will also
2624 * be created automatically.
2625 *
2626 * A "dev" file will be created, showing the dev_t for the device, if
2627 * the dev_t is not 0,0.
2628 * If a pointer to a parent struct device is passed in, the newly created
2629 * struct device will be a child of that device in sysfs.
2630 * The pointer to the struct device will be returned from the call.
2631 * Any further sysfs files that might be required can be created using this
2632 * pointer.
2633 *
2634 * Returns &struct device pointer on success, or ERR_PTR() on error.
2635 *
2636 * Note: the struct class passed to this function must have previously
2637 * been created with a call to class_create().
2638 */
2639struct device *device_create_with_groups(struct class *class,
2640 struct device *parent, dev_t devt,
2641 void *drvdata,
2642 const struct attribute_group **groups,
2643 const char *fmt, ...)
2644{
2645 va_list vargs;
2646 struct device *dev;
2647
2648 va_start(vargs, fmt);
2649 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2650 fmt, vargs);
2651 va_end(vargs);
2652 return dev;
2653}
2654EXPORT_SYMBOL_GPL(device_create_with_groups);
2655
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002656static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002657{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002658 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002659
Dave Youngcd354492008-01-28 16:56:11 +08002660 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002661}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002662
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002663/**
2664 * device_destroy - removes a device that was created with device_create()
2665 * @class: pointer to the struct class that this device was registered with
2666 * @devt: the dev_t of the device that was previously registered
2667 *
2668 * This call unregisters and cleans up a device that was created with a
2669 * call to device_create().
2670 */
2671void device_destroy(struct class *class, dev_t devt)
2672{
2673 struct device *dev;
2674
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002675 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002676 if (dev) {
2677 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002678 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002679 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002680}
2681EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002682
2683/**
2684 * device_rename - renames a device
2685 * @dev: the pointer to the struct device to be renamed
2686 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002687 *
2688 * It is the responsibility of the caller to provide mutual
2689 * exclusion between two different calls of device_rename
2690 * on the same device to ensure that new_name is valid and
2691 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002692 *
Timur Tabia5462512010-12-13 14:08:52 -06002693 * Note: Don't call this function. Currently, the networking layer calls this
2694 * function, but that will change. The following text from Kay Sievers offers
2695 * some insight:
2696 *
2697 * Renaming devices is racy at many levels, symlinks and other stuff are not
2698 * replaced atomically, and you get a "move" uevent, but it's not easy to
2699 * connect the event to the old and new device. Device nodes are not renamed at
2700 * all, there isn't even support for that in the kernel now.
2701 *
2702 * In the meantime, during renaming, your target name might be taken by another
2703 * driver, creating conflicts. Or the old name is taken directly after you
2704 * renamed it -- then you get events for the same DEVPATH, before you even see
2705 * the "move" event. It's just a mess, and nothing new should ever rely on
2706 * kernel device renaming. Besides that, it's not even implemented now for
2707 * other things than (driver-core wise very simple) network devices.
2708 *
2709 * We are currently about to change network renaming in udev to completely
2710 * disallow renaming of devices in the same namespace as the kernel uses,
2711 * because we can't solve the problems properly, that arise with swapping names
2712 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2713 * be allowed to some other name than eth[0-9]*, for the aforementioned
2714 * reasons.
2715 *
2716 * Make up a "real" name in the driver before you register anything, or add
2717 * some other attributes for userspace to find the device, or use udev to add
2718 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2719 * don't even want to get into that and try to implement the missing pieces in
2720 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002721 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002722int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002723{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002724 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002725 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002726 int error;
2727
2728 dev = get_device(dev);
2729 if (!dev)
2730 return -EINVAL;
2731
ethan.zhao69df7532013-10-13 22:12:35 +08002732 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002733
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002734 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002735 if (!old_device_name) {
2736 error = -ENOMEM;
2737 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002738 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002739
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002740 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002741 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2742 kobj, old_device_name,
2743 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002744 if (error)
2745 goto out;
2746 }
Kay Sievers39aba962010-09-04 22:33:14 -07002747
Tejun Heo4b30ee52013-09-11 22:29:06 -04002748 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002749 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002750 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002751
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002752out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002753 put_device(dev);
2754
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002755 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002756
2757 return error;
2758}
Johannes Berga2807db2007-02-28 12:38:31 +01002759EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002760
2761static int device_move_class_links(struct device *dev,
2762 struct device *old_parent,
2763 struct device *new_parent)
2764{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002765 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002766
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002767 if (old_parent)
2768 sysfs_remove_link(&dev->kobj, "device");
2769 if (new_parent)
2770 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2771 "device");
2772 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002773}
2774
2775/**
2776 * device_move - moves a device to a new parent
2777 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02002778 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002779 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002780 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002781int device_move(struct device *dev, struct device *new_parent,
2782 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002783{
2784 int error;
2785 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002786 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002787
2788 dev = get_device(dev);
2789 if (!dev)
2790 return -EINVAL;
2791
Cornelia Huckffa6a702009-03-04 12:44:00 +01002792 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002793 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002794 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002795 if (IS_ERR(new_parent_kobj)) {
2796 error = PTR_ERR(new_parent_kobj);
2797 put_device(new_parent);
2798 goto out;
2799 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002800
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002801 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2802 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002803 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002804 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002805 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002806 put_device(new_parent);
2807 goto out;
2808 }
2809 old_parent = dev->parent;
2810 dev->parent = new_parent;
2811 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002812 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002813 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002814 klist_add_tail(&dev->p->knode_parent,
2815 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002816 set_dev_node(dev, dev_to_node(new_parent));
2817 }
2818
Rabin Vincentbdd40342012-04-23 09:16:36 +02002819 if (dev->class) {
2820 error = device_move_class_links(dev, old_parent, new_parent);
2821 if (error) {
2822 /* We ignore errors on cleanup since we're hosed anyway... */
2823 device_move_class_links(dev, new_parent, old_parent);
2824 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2825 if (new_parent)
2826 klist_remove(&dev->p->knode_parent);
2827 dev->parent = old_parent;
2828 if (old_parent) {
2829 klist_add_tail(&dev->p->knode_parent,
2830 &old_parent->p->klist_children);
2831 set_dev_node(dev, dev_to_node(old_parent));
2832 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002833 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002834 cleanup_glue_dir(dev, new_parent_kobj);
2835 put_device(new_parent);
2836 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002837 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002838 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002839 switch (dpm_order) {
2840 case DPM_ORDER_NONE:
2841 break;
2842 case DPM_ORDER_DEV_AFTER_PARENT:
2843 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002844 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002845 break;
2846 case DPM_ORDER_PARENT_BEFORE_DEV:
2847 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002848 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002849 break;
2850 case DPM_ORDER_DEV_LAST:
2851 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002852 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002853 break;
2854 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002855
Cornelia Huck8a824722006-11-20 17:07:51 +01002856 put_device(old_parent);
2857out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002858 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002859 put_device(dev);
2860 return error;
2861}
Cornelia Huck8a824722006-11-20 17:07:51 +01002862EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002863
2864/**
2865 * device_shutdown - call ->shutdown() on each device to shutdown.
2866 */
2867void device_shutdown(void)
2868{
Benson Leungf123db82013-09-24 20:05:11 -07002869 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002870
Pingfan Liu3297c8f2018-07-19 13:14:58 +08002871 wait_for_device_probe();
2872 device_block_probing();
2873
Hugh Daschbach62458382010-03-22 10:36:37 -07002874 spin_lock(&devices_kset->list_lock);
2875 /*
2876 * Walk the devices list backward, shutting down each in turn.
2877 * Beware that device unplug events may also start pulling
2878 * devices offline, even as the system is shutting down.
2879 */
2880 while (!list_empty(&devices_kset->list)) {
2881 dev = list_entry(devices_kset->list.prev, struct device,
2882 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002883
2884 /*
2885 * hold reference count of device's parent to
2886 * prevent it from being freed because parent's
2887 * lock is to be held
2888 */
Benson Leungf123db82013-09-24 20:05:11 -07002889 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002890 get_device(dev);
2891 /*
2892 * Make sure the device is off the kset list, in the
2893 * event that dev->*->shutdown() doesn't remove it.
2894 */
2895 list_del_init(&dev->kobj.entry);
2896 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002897
Ming Leid1c6c032012-06-22 18:01:40 +08002898 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002899 if (parent)
2900 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002901 device_lock(dev);
2902
Alan Sternfe6b91f2011-12-06 23:24:52 +01002903 /* Don't allow any more runtime suspends */
2904 pm_runtime_get_noresume(dev);
2905 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002906
Michal Suchanek75216212017-08-11 15:44:43 +02002907 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07002908 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02002909 dev_info(dev, "shutdown_pre\n");
2910 dev->class->shutdown_pre(dev);
2911 }
2912 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002913 if (initcall_debug)
2914 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002915 dev->bus->shutdown(dev);
2916 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002917 if (initcall_debug)
2918 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002919 dev->driver->shutdown(dev);
2920 }
Ming Leid1c6c032012-06-22 18:01:40 +08002921
2922 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002923 if (parent)
2924 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002925
Hugh Daschbach62458382010-03-22 10:36:37 -07002926 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002927 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002928
2929 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002930 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002931 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002932}
Joe Perches99bcf212010-06-27 01:02:34 +00002933
2934/*
2935 * Device logging functions
2936 */
2937
2938#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002939static int
2940create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002941{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002942 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002943 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002944
Kay Sieversc4e00da2012-05-03 02:29:59 +02002945 if (dev->class)
2946 subsys = dev->class->name;
2947 else if (dev->bus)
2948 subsys = dev->bus->name;
2949 else
Joe Perches798efc62012-09-12 20:11:29 -07002950 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002951
Joe Perches798efc62012-09-12 20:11:29 -07002952 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002953 if (pos >= hdrlen)
2954 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002955
2956 /*
2957 * Add device identifier DEVICE=:
2958 * b12:8 block dev_t
2959 * c127:3 char dev_t
2960 * n8 netdev ifindex
2961 * +sound:card0 subsystem:devname
2962 */
2963 if (MAJOR(dev->devt)) {
2964 char c;
2965
2966 if (strcmp(subsys, "block") == 0)
2967 c = 'b';
2968 else
2969 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002970 pos++;
2971 pos += snprintf(hdr + pos, hdrlen - pos,
2972 "DEVICE=%c%u:%u",
2973 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002974 } else if (strcmp(subsys, "net") == 0) {
2975 struct net_device *net = to_net_dev(dev);
2976
Joe Perches798efc62012-09-12 20:11:29 -07002977 pos++;
2978 pos += snprintf(hdr + pos, hdrlen - pos,
2979 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002980 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002981 pos++;
2982 pos += snprintf(hdr + pos, hdrlen - pos,
2983 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002984 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002985
Ben Hutchings655e5b72014-08-26 00:34:44 -07002986 if (pos >= hdrlen)
2987 goto overflow;
2988
Joe Perches798efc62012-09-12 20:11:29 -07002989 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002990
2991overflow:
2992 dev_WARN(dev, "device/subsystem name too long");
2993 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002994}
Joe Perches798efc62012-09-12 20:11:29 -07002995
Joe Perches05e4e5b2012-09-12 20:13:37 -07002996int dev_vprintk_emit(int level, const struct device *dev,
2997 const char *fmt, va_list args)
2998{
2999 char hdr[128];
3000 size_t hdrlen;
3001
3002 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3003
3004 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3005}
3006EXPORT_SYMBOL(dev_vprintk_emit);
3007
3008int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3009{
3010 va_list args;
3011 int r;
3012
3013 va_start(args, fmt);
3014
3015 r = dev_vprintk_emit(level, dev, fmt, args);
3016
3017 va_end(args);
3018
3019 return r;
3020}
3021EXPORT_SYMBOL(dev_printk_emit);
3022
Joe Perchesd1f10522014-12-25 15:07:04 -08003023static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07003024 struct va_format *vaf)
3025{
Joe Perchesd1f10522014-12-25 15:07:04 -08003026 if (dev)
3027 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3028 dev_driver_string(dev), dev_name(dev), vaf);
3029 else
3030 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003031}
Joe Perches99bcf212010-06-27 01:02:34 +00003032
Joe Perchesd1f10522014-12-25 15:07:04 -08003033void dev_printk(const char *level, const struct device *dev,
3034 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00003035{
3036 struct va_format vaf;
3037 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00003038
3039 va_start(args, fmt);
3040
3041 vaf.fmt = fmt;
3042 vaf.va = &args;
3043
Joe Perchesd1f10522014-12-25 15:07:04 -08003044 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003045
Joe Perches99bcf212010-06-27 01:02:34 +00003046 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00003047}
3048EXPORT_SYMBOL(dev_printk);
3049
3050#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08003051void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00003052{ \
3053 struct va_format vaf; \
3054 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003055 \
3056 va_start(args, fmt); \
3057 \
3058 vaf.fmt = fmt; \
3059 vaf.va = &args; \
3060 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003061 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003062 \
Joe Perches99bcf212010-06-27 01:02:34 +00003063 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003064} \
3065EXPORT_SYMBOL(func);
3066
Joe Perches663336e2018-05-09 08:15:46 -07003067define_dev_printk_level(_dev_emerg, KERN_EMERG);
3068define_dev_printk_level(_dev_alert, KERN_ALERT);
3069define_dev_printk_level(_dev_crit, KERN_CRIT);
3070define_dev_printk_level(_dev_err, KERN_ERR);
3071define_dev_printk_level(_dev_warn, KERN_WARNING);
3072define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00003073define_dev_printk_level(_dev_info, KERN_INFO);
3074
3075#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003076
3077static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3078{
3079 return fwnode && !IS_ERR(fwnode->secondary);
3080}
3081
3082/**
3083 * set_primary_fwnode - Change the primary firmware node of a given device.
3084 * @dev: Device to handle.
3085 * @fwnode: New primary firmware node of the device.
3086 *
3087 * Set the device's firmware node pointer to @fwnode, but if a secondary
3088 * firmware node of the device is present, preserve it.
3089 */
3090void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3091{
3092 if (fwnode) {
3093 struct fwnode_handle *fn = dev->fwnode;
3094
3095 if (fwnode_is_primary(fn))
3096 fn = fn->secondary;
3097
Mika Westerberg55f89a82015-11-30 17:11:39 +02003098 if (fn) {
3099 WARN_ON(fwnode->secondary);
3100 fwnode->secondary = fn;
3101 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003102 dev->fwnode = fwnode;
3103 } else {
3104 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3105 dev->fwnode->secondary : NULL;
3106 }
3107}
3108EXPORT_SYMBOL_GPL(set_primary_fwnode);
3109
3110/**
3111 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3112 * @dev: Device to handle.
3113 * @fwnode: New secondary firmware node of the device.
3114 *
3115 * If a primary firmware node of the device is present, set its secondary
3116 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3117 * @fwnode.
3118 */
3119void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3120{
3121 if (fwnode)
3122 fwnode->secondary = ERR_PTR(-ENODEV);
3123
3124 if (fwnode_is_primary(dev->fwnode))
3125 dev->fwnode->secondary = fwnode;
3126 else
3127 dev->fwnode = fwnode;
3128}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003129
3130/**
3131 * device_set_of_node_from_dev - reuse device-tree node of another device
3132 * @dev: device whose device-tree node is being set
3133 * @dev2: device whose device-tree node is being reused
3134 *
3135 * Takes another reference to the new device-tree node after first dropping
3136 * any reference held to the old node.
3137 */
3138void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3139{
3140 of_node_put(dev->of_node);
3141 dev->of_node = of_node_get(dev2->of_node);
3142 dev->of_node_reused = true;
3143}
3144EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);