blob: 3972ef3f080bdec3bb82688f35daa48b6d0df4c9 [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
Heikki Krogerus07de0e82018-11-09 17:21:34 +0300731static int
732device_platform_notify(struct device *dev, enum kobject_action action)
733{
734 if (platform_notify && action == KOBJ_ADD)
735 platform_notify(dev);
736 else if (platform_notify_remove && action == KOBJ_REMOVE)
737 platform_notify_remove(dev);
738 return 0;
739}
740
Alan Stern3e956372006-06-16 17:10:48 -0400741/**
742 * dev_driver_string - Return a device's driver name, if at all possible
743 * @dev: struct device to get the name of
744 *
745 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800746 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400747 * it is attached to. If it is not attached to a bus either, an empty
748 * string will be returned.
749 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700750const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400751{
Alan Stern35899722009-12-04 11:06:57 -0500752 struct device_driver *drv;
753
754 /* dev->driver can change to NULL underneath us because of unbinding,
755 * so be careful about accessing it. dev->bus and dev->class should
756 * never change once they are set, so they don't need special care.
757 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700758 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500759 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100760 (dev->bus ? dev->bus->name :
761 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400762}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600763EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
766
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800767static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
768 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800770 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200771 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500772 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400775 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800776 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900777 printk("dev_attr_show: %pS returned bad count\n",
778 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return ret;
781}
782
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800783static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
784 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800786 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200787 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500788 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400791 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return ret;
793}
794
Emese Revfy52cf25d2010-01-19 02:58:23 +0100795static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 .show = dev_attr_show,
797 .store = dev_attr_store,
798};
799
Kay Sieversca22e562011-12-14 14:29:38 -0800800#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
801
802ssize_t device_store_ulong(struct device *dev,
803 struct device_attribute *attr,
804 const char *buf, size_t size)
805{
806 struct dev_ext_attribute *ea = to_ext_attr(attr);
807 char *end;
808 unsigned long new = simple_strtoul(buf, &end, 0);
809 if (end == buf)
810 return -EINVAL;
811 *(unsigned long *)(ea->var) = new;
812 /* Always return full write size even if we didn't consume all */
813 return size;
814}
815EXPORT_SYMBOL_GPL(device_store_ulong);
816
817ssize_t device_show_ulong(struct device *dev,
818 struct device_attribute *attr,
819 char *buf)
820{
821 struct dev_ext_attribute *ea = to_ext_attr(attr);
822 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
823}
824EXPORT_SYMBOL_GPL(device_show_ulong);
825
826ssize_t device_store_int(struct device *dev,
827 struct device_attribute *attr,
828 const char *buf, size_t size)
829{
830 struct dev_ext_attribute *ea = to_ext_attr(attr);
831 char *end;
832 long new = simple_strtol(buf, &end, 0);
833 if (end == buf || new > INT_MAX || new < INT_MIN)
834 return -EINVAL;
835 *(int *)(ea->var) = new;
836 /* Always return full write size even if we didn't consume all */
837 return size;
838}
839EXPORT_SYMBOL_GPL(device_store_int);
840
841ssize_t device_show_int(struct device *dev,
842 struct device_attribute *attr,
843 char *buf)
844{
845 struct dev_ext_attribute *ea = to_ext_attr(attr);
846
847 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
848}
849EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Borislav Petkov91872392012-10-09 19:52:05 +0200851ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
852 const char *buf, size_t size)
853{
854 struct dev_ext_attribute *ea = to_ext_attr(attr);
855
856 if (strtobool(buf, ea->var) < 0)
857 return -EINVAL;
858
859 return size;
860}
861EXPORT_SYMBOL_GPL(device_store_bool);
862
863ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
864 char *buf)
865{
866 struct dev_ext_attribute *ea = to_ext_attr(attr);
867
868 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
869}
870EXPORT_SYMBOL_GPL(device_show_bool);
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400873 * device_release - free device structure.
874 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400876 * This is called once the reference count for the object
877 * reaches 0. We forward the call to the device's release
878 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800880static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200882 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800883 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Ming Leia525a3d2012-07-25 01:42:29 +0800885 /*
886 * Some platform devices are driven without driver attached
887 * and managed resources may have been acquired. Make sure
888 * all resources are released.
889 *
890 * Drivers still can add resources into device after device
891 * is deleted but alive, so release devres here to avoid
892 * possible memory leak.
893 */
894 devres_release_all(dev);
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (dev->release)
897 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200898 else if (dev->type && dev->type->release)
899 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700900 else if (dev->class && dev->class->dev_release)
901 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700902 else
903 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800904 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100905 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800906 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700909static const void *device_namespace(struct kobject *kobj)
910{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200911 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700912 const void *ns = NULL;
913
914 if (dev->class && dev->class->ns_type)
915 ns = dev->class->namespace(dev);
916
917 return ns;
918}
919
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000920static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
921{
922 struct device *dev = kobj_to_dev(kobj);
923
924 if (dev->class && dev->class->get_ownership)
925 dev->class->get_ownership(dev, uid, gid);
926}
927
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600928static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 .release = device_release,
930 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700931 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000932 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933};
934
935
Kay Sievers312c0042005-11-16 09:00:00 +0100936static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 struct kobj_type *ktype = get_ktype(kobj);
939
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600940 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200941 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (dev->bus)
943 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700944 if (dev->class)
945 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 return 0;
948}
949
Kay Sievers312c0042005-11-16 09:00:00 +0100950static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200952 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700954 if (dev->bus)
955 return dev->bus->name;
956 if (dev->class)
957 return dev->class->name;
958 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Kay Sievers7eff2e72007-08-14 15:15:12 +0200961static int dev_uevent(struct kset *kset, struct kobject *kobj,
962 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200964 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int retval = 0;
966
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200967 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700968 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200969 const char *tmp;
970 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400971 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700972 kuid_t uid = GLOBAL_ROOT_UID;
973 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200974
Kay Sievers7eff2e72007-08-14 15:15:12 +0200975 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
976 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700977 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200978 if (name) {
979 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200980 if (mode)
981 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700982 if (!uid_eq(uid, GLOBAL_ROOT_UID))
983 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
984 if (!gid_eq(gid, GLOBAL_ROOT_GID))
985 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700986 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200987 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700988 }
989
Kay Sievers414264f2007-03-12 21:08:57 +0100990 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200991 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100992
Kay Sievers239378f2006-10-07 21:54:55 +0200993 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200994 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200995
Grant Likely07d57a32012-02-01 11:22:22 -0700996 /* Add common DT information about the device */
997 of_device_uevent(dev, env);
998
Kay Sievers7eff2e72007-08-14 15:15:12 +0200999 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01001000 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001001 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001002 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001003 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001004 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
Kay Sievers7eff2e72007-08-14 15:15:12 +02001007 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001008 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001009 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001010 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001011 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001012 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001013 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001014 }
1015
Stefan Weileef35c22010-08-06 21:11:15 +02001016 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02001017 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001018 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001019 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001020 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001021 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001022 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001023 }
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return retval;
1026}
1027
Emese Revfy9cd43612009-12-31 14:52:51 +01001028static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01001029 .filter = dev_uevent_filter,
1030 .name = dev_uevent_name,
1031 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032};
1033
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001034static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02001035 char *buf)
1036{
1037 struct kobject *top_kobj;
1038 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001039 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02001040 int i;
1041 size_t count = 0;
1042 int retval;
1043
1044 /* search the kset, the device belongs to */
1045 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001046 while (!top_kobj->kset && top_kobj->parent)
1047 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02001048 if (!top_kobj->kset)
1049 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001050
Kay Sievers16574dc2007-04-06 01:40:38 +02001051 kset = top_kobj->kset;
1052 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1053 goto out;
1054
1055 /* respect filter */
1056 if (kset->uevent_ops && kset->uevent_ops->filter)
1057 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1058 goto out;
1059
Kay Sievers7eff2e72007-08-14 15:15:12 +02001060 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1061 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001062 return -ENOMEM;
1063
Kay Sievers16574dc2007-04-06 01:40:38 +02001064 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001065 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001066 if (retval)
1067 goto out;
1068
1069 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001070 for (i = 0; i < env->envp_idx; i++)
1071 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001072out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001073 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001074 return count;
1075}
1076
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001077static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001078 const char *buf, size_t count)
1079{
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001080 if (kobject_synth_uevent(&dev->kobj, buf, count))
1081 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Kay Sievers60a96a52007-07-08 22:29:26 +02001082
Kay Sieversa7fd6702005-10-01 14:49:43 +02001083 return count;
1084}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001085static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001086
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001087static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001088 char *buf)
1089{
1090 bool val;
1091
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001092 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001093 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001094 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001095 return sprintf(buf, "%u\n", val);
1096}
1097
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001098static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001099 const char *buf, size_t count)
1100{
1101 bool val;
1102 int ret;
1103
1104 ret = strtobool(buf, &val);
1105 if (ret < 0)
1106 return ret;
1107
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001108 ret = lock_device_hotplug_sysfs();
1109 if (ret)
1110 return ret;
1111
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001112 ret = val ? device_online(dev) : device_offline(dev);
1113 unlock_device_hotplug();
1114 return ret < 0 ? ret : count;
1115}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001116static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001117
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001118int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001119{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001120 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001121}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001122EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001123
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001124void device_remove_groups(struct device *dev,
1125 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001126{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001127 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001128}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001129EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001130
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001131union device_attr_group_devres {
1132 const struct attribute_group *group;
1133 const struct attribute_group **groups;
1134};
1135
1136static int devm_attr_group_match(struct device *dev, void *res, void *data)
1137{
1138 return ((union device_attr_group_devres *)res)->group == data;
1139}
1140
1141static void devm_attr_group_remove(struct device *dev, void *res)
1142{
1143 union device_attr_group_devres *devres = res;
1144 const struct attribute_group *group = devres->group;
1145
1146 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1147 sysfs_remove_group(&dev->kobj, group);
1148}
1149
1150static void devm_attr_groups_remove(struct device *dev, void *res)
1151{
1152 union device_attr_group_devres *devres = res;
1153 const struct attribute_group **groups = devres->groups;
1154
1155 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1156 sysfs_remove_groups(&dev->kobj, groups);
1157}
1158
1159/**
1160 * devm_device_add_group - given a device, create a managed attribute group
1161 * @dev: The device to create the group for
1162 * @grp: The attribute group to create
1163 *
1164 * This function creates a group for the first time. It will explicitly
1165 * warn and error if any of the attribute files being created already exist.
1166 *
1167 * Returns 0 on success or error code on failure.
1168 */
1169int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1170{
1171 union device_attr_group_devres *devres;
1172 int error;
1173
1174 devres = devres_alloc(devm_attr_group_remove,
1175 sizeof(*devres), GFP_KERNEL);
1176 if (!devres)
1177 return -ENOMEM;
1178
1179 error = sysfs_create_group(&dev->kobj, grp);
1180 if (error) {
1181 devres_free(devres);
1182 return error;
1183 }
1184
1185 devres->group = grp;
1186 devres_add(dev, devres);
1187 return 0;
1188}
1189EXPORT_SYMBOL_GPL(devm_device_add_group);
1190
1191/**
1192 * devm_device_remove_group: remove a managed group from a device
1193 * @dev: device to remove the group from
1194 * @grp: group to remove
1195 *
1196 * This function removes a group of attributes from a device. The attributes
1197 * previously have to have been created for this group, otherwise it will fail.
1198 */
1199void devm_device_remove_group(struct device *dev,
1200 const struct attribute_group *grp)
1201{
1202 WARN_ON(devres_release(dev, devm_attr_group_remove,
1203 devm_attr_group_match,
1204 /* cast away const */ (void *)grp));
1205}
1206EXPORT_SYMBOL_GPL(devm_device_remove_group);
1207
1208/**
1209 * devm_device_add_groups - create a bunch of managed attribute groups
1210 * @dev: The device to create the group for
1211 * @groups: The attribute groups to create, NULL terminated
1212 *
1213 * This function creates a bunch of managed attribute groups. If an error
1214 * occurs when creating a group, all previously created groups will be
1215 * removed, unwinding everything back to the original state when this
1216 * function was called. It will explicitly warn and error if any of the
1217 * attribute files being created already exist.
1218 *
1219 * Returns 0 on success or error code from sysfs_create_group on failure.
1220 */
1221int devm_device_add_groups(struct device *dev,
1222 const struct attribute_group **groups)
1223{
1224 union device_attr_group_devres *devres;
1225 int error;
1226
1227 devres = devres_alloc(devm_attr_groups_remove,
1228 sizeof(*devres), GFP_KERNEL);
1229 if (!devres)
1230 return -ENOMEM;
1231
1232 error = sysfs_create_groups(&dev->kobj, groups);
1233 if (error) {
1234 devres_free(devres);
1235 return error;
1236 }
1237
1238 devres->groups = groups;
1239 devres_add(dev, devres);
1240 return 0;
1241}
1242EXPORT_SYMBOL_GPL(devm_device_add_groups);
1243
1244/**
1245 * devm_device_remove_groups - remove a list of managed groups
1246 *
1247 * @dev: The device for the groups to be removed from
1248 * @groups: NULL terminated list of groups to be removed
1249 *
1250 * If groups is not NULL, remove the specified groups from the device.
1251 */
1252void devm_device_remove_groups(struct device *dev,
1253 const struct attribute_group **groups)
1254{
1255 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1256 devm_attr_group_match,
1257 /* cast away const */ (void *)groups));
1258}
1259EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001261static int device_add_attrs(struct device *dev)
1262{
1263 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001264 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001265 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001266
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001267 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001268 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001269 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001270 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001271 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001272
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001273 if (type) {
1274 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001275 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001276 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001277 }
1278
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001279 error = device_add_groups(dev, dev->groups);
1280 if (error)
1281 goto err_remove_type_groups;
1282
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001283 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001284 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001285 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001286 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001287 }
1288
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001289 return 0;
1290
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001291 err_remove_dev_groups:
1292 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001293 err_remove_type_groups:
1294 if (type)
1295 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001296 err_remove_class_groups:
1297 if (class)
1298 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001299
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001300 return error;
1301}
1302
1303static void device_remove_attrs(struct device *dev)
1304{
1305 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001306 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001307
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001308 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001309 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001310
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001311 if (type)
1312 device_remove_groups(dev, type->groups);
1313
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001314 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001315 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001316}
1317
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001318static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001319 char *buf)
1320{
1321 return print_dev_t(buf, dev->devt);
1322}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001323static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001324
Kay Sieversca22e562011-12-14 14:29:38 -08001325/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001326struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001329 * devices_kset_move_before - Move device in the devices_kset's list.
1330 * @deva: Device to move.
1331 * @devb: Device @deva should come before.
1332 */
1333static void devices_kset_move_before(struct device *deva, struct device *devb)
1334{
1335 if (!devices_kset)
1336 return;
1337 pr_debug("devices_kset: Moving %s before %s\n",
1338 dev_name(deva), dev_name(devb));
1339 spin_lock(&devices_kset->list_lock);
1340 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1341 spin_unlock(&devices_kset->list_lock);
1342}
1343
1344/**
1345 * devices_kset_move_after - Move device in the devices_kset's list.
1346 * @deva: Device to move
1347 * @devb: Device @deva should come after.
1348 */
1349static void devices_kset_move_after(struct device *deva, struct device *devb)
1350{
1351 if (!devices_kset)
1352 return;
1353 pr_debug("devices_kset: Moving %s after %s\n",
1354 dev_name(deva), dev_name(devb));
1355 spin_lock(&devices_kset->list_lock);
1356 list_move(&deva->kobj.entry, &devb->kobj.entry);
1357 spin_unlock(&devices_kset->list_lock);
1358}
1359
1360/**
1361 * devices_kset_move_last - move the device to the end of devices_kset's list.
1362 * @dev: device to move
1363 */
1364void devices_kset_move_last(struct device *dev)
1365{
1366 if (!devices_kset)
1367 return;
1368 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1369 spin_lock(&devices_kset->list_lock);
1370 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1371 spin_unlock(&devices_kset->list_lock);
1372}
1373
1374/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001375 * device_create_file - create sysfs attribute file for device.
1376 * @dev: device.
1377 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001379int device_create_file(struct device *dev,
1380 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001383
1384 if (dev) {
1385 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001386 "Attribute %s: write permission without 'store'\n",
1387 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001388 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001389 "Attribute %s: read permission without 'show'\n",
1390 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001392 }
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return error;
1395}
David Graham White86df2682013-07-21 20:41:14 -04001396EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001399 * device_remove_file - remove sysfs attribute file.
1400 * @dev: device.
1401 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001403void device_remove_file(struct device *dev,
1404 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001406 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
David Graham White86df2682013-07-21 20:41:14 -04001409EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001411/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001412 * device_remove_file_self - remove sysfs attribute file from its own method.
1413 * @dev: device.
1414 * @attr: device attribute descriptor.
1415 *
1416 * See kernfs_remove_self() for details.
1417 */
1418bool device_remove_file_self(struct device *dev,
1419 const struct device_attribute *attr)
1420{
1421 if (dev)
1422 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1423 else
1424 return false;
1425}
1426EXPORT_SYMBOL_GPL(device_remove_file_self);
1427
1428/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001429 * device_create_bin_file - create sysfs binary attribute file for device.
1430 * @dev: device.
1431 * @attr: device binary attribute descriptor.
1432 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001433int device_create_bin_file(struct device *dev,
1434 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001435{
1436 int error = -EINVAL;
1437 if (dev)
1438 error = sysfs_create_bin_file(&dev->kobj, attr);
1439 return error;
1440}
1441EXPORT_SYMBOL_GPL(device_create_bin_file);
1442
1443/**
1444 * device_remove_bin_file - remove sysfs binary attribute file
1445 * @dev: device.
1446 * @attr: device binary attribute descriptor.
1447 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001448void device_remove_bin_file(struct device *dev,
1449 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001450{
1451 if (dev)
1452 sysfs_remove_bin_file(&dev->kobj, attr);
1453}
1454EXPORT_SYMBOL_GPL(device_remove_bin_file);
1455
James Bottomley34bb61f2005-09-06 16:56:51 -07001456static void klist_children_get(struct klist_node *n)
1457{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001458 struct device_private *p = to_device_private_parent(n);
1459 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001460
1461 get_device(dev);
1462}
1463
1464static void klist_children_put(struct klist_node *n)
1465{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001466 struct device_private *p = to_device_private_parent(n);
1467 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001468
1469 put_device(dev);
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001473 * device_initialize - init device structure.
1474 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 *
Cornelia Huck57394112008-09-03 18:26:40 +02001476 * This prepares the device for use by other layers by initializing
1477 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001478 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001479 * that function, though it can also be called separately, so one
1480 * may use @dev's fields. In particular, get_device()/put_device()
1481 * may be used for reference counting of @dev after calling this
1482 * function.
1483 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001484 * All fields in @dev must be initialized by the caller to 0, except
1485 * for those explicitly set to some other value. The simplest
1486 * approach is to use kzalloc() to allocate the structure containing
1487 * @dev.
1488 *
Cornelia Huck57394112008-09-03 18:26:40 +02001489 * NOTE: Use put_device() to give up your reference instead of freeing
1490 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492void device_initialize(struct device *dev)
1493{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001494 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001495 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001497 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001498 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001499 spin_lock_init(&dev->devres_lock);
1500 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001501 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001502 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001503#ifdef CONFIG_GENERIC_MSI_IRQ
1504 INIT_LIST_HEAD(&dev->msi_list);
1505#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001506 INIT_LIST_HEAD(&dev->links.consumers);
1507 INIT_LIST_HEAD(&dev->links.suppliers);
1508 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
David Graham White86df2682013-07-21 20:41:14 -04001510EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Tejun Heod73ce002013-03-12 11:30:05 -07001512struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001513{
Kay Sievers86406242007-03-14 03:25:56 +01001514 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001515
Kay Sievers86406242007-03-14 03:25:56 +01001516 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001517 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001518 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001519
Kay Sievers86406242007-03-14 03:25:56 +01001520 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001521}
1522
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001523struct class_dir {
1524 struct kobject kobj;
1525 struct class *class;
1526};
1527
1528#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1529
1530static void class_dir_release(struct kobject *kobj)
1531{
1532 struct class_dir *dir = to_class_dir(kobj);
1533 kfree(dir);
1534}
1535
1536static const
1537struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1538{
1539 struct class_dir *dir = to_class_dir(kobj);
1540 return dir->class->ns_type;
1541}
1542
1543static struct kobj_type class_dir_ktype = {
1544 .release = class_dir_release,
1545 .sysfs_ops = &kobj_sysfs_ops,
1546 .child_ns_type = class_dir_child_ns_type
1547};
1548
1549static struct kobject *
1550class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1551{
1552 struct class_dir *dir;
1553 int retval;
1554
1555 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1556 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001557 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001558
1559 dir->class = class;
1560 kobject_init(&dir->kobj, &class_dir_ktype);
1561
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001562 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001563
1564 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1565 if (retval < 0) {
1566 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001567 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001568 }
1569 return &dir->kobj;
1570}
1571
Yijing Wange4a60d12014-11-07 12:05:49 +08001572static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001573
Kay Sieversda231fd2007-11-21 17:29:15 +01001574static struct kobject *get_device_parent(struct device *dev,
1575 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001576{
Kay Sievers86406242007-03-14 03:25:56 +01001577 if (dev->class) {
1578 struct kobject *kobj = NULL;
1579 struct kobject *parent_kobj;
1580 struct kobject *k;
1581
Randy Dunlapead454f2010-09-24 14:36:49 -07001582#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001583 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001584 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001585 if (parent && parent->class == &block_class)
1586 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001587 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001588 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001589#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001590
Kay Sievers86406242007-03-14 03:25:56 +01001591 /*
1592 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001593 * Class-devices with a non class-device as parent, live
1594 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001595 */
1596 if (parent == NULL)
1597 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001598 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001599 return &parent->kobj;
1600 else
1601 parent_kobj = &parent->kobj;
1602
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001603 mutex_lock(&gdp_mutex);
1604
Kay Sievers86406242007-03-14 03:25:56 +01001605 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001606 spin_lock(&dev->class->p->glue_dirs.list_lock);
1607 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001608 if (k->parent == parent_kobj) {
1609 kobj = kobject_get(k);
1610 break;
1611 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001612 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001613 if (kobj) {
1614 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001615 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001616 }
Kay Sievers86406242007-03-14 03:25:56 +01001617
1618 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001619 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001620 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001621 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001622 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001623 }
1624
Kay Sieversca22e562011-12-14 14:29:38 -08001625 /* subsystems can specify a default root directory for their devices */
1626 if (!parent && dev->bus && dev->bus->dev_root)
1627 return &dev->bus->dev_root->kobj;
1628
Kay Sievers86406242007-03-14 03:25:56 +01001629 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001630 return &parent->kobj;
1631 return NULL;
1632}
Kay Sieversda231fd2007-11-21 17:29:15 +01001633
Ming Leicebf8fd2016-07-10 19:27:36 +08001634static inline bool live_in_glue_dir(struct kobject *kobj,
1635 struct device *dev)
1636{
1637 if (!kobj || !dev->class ||
1638 kobj->kset != &dev->class->p->glue_dirs)
1639 return false;
1640 return true;
1641}
1642
1643static inline struct kobject *get_glue_dir(struct device *dev)
1644{
1645 return dev->kobj.parent;
1646}
1647
1648/*
1649 * make sure cleaning up dir as the last step, we need to make
1650 * sure .release handler of kobject is run with holding the
1651 * global lock
1652 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001653static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001654{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001655 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001656 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001657 return;
1658
Yijing Wange4a60d12014-11-07 12:05:49 +08001659 mutex_lock(&gdp_mutex);
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10001660 if (!kobject_has_children(glue_dir))
1661 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001662 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001663 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001664}
Cornelia Huck63b69712008-01-21 16:09:44 +01001665
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001666static int device_add_class_symlinks(struct device *dev)
1667{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001668 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001669 int error;
1670
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001671 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001672 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001673 if (error)
1674 dev_warn(dev, "Error %d creating of_node link\n",error);
1675 /* An error here doesn't warrant bringing down the device */
1676 }
1677
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001678 if (!dev->class)
1679 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001680
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001681 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001682 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001683 "subsystem");
1684 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001685 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001686
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001687 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001688 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1689 "device");
1690 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001691 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001692 }
Kay Sievers39aba962010-09-04 22:33:14 -07001693
Randy Dunlapead454f2010-09-24 14:36:49 -07001694#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001695 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001696 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001697 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001698#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001699
1700 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001701 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001702 &dev->kobj, dev_name(dev));
1703 if (error)
1704 goto out_device;
1705
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001706 return 0;
1707
Kay Sievers39aba962010-09-04 22:33:14 -07001708out_device:
1709 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001710
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001711out_subsys:
1712 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001713out_devnode:
1714 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001715 return error;
1716}
1717
1718static void device_remove_class_symlinks(struct device *dev)
1719{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001720 if (dev_of_node(dev))
1721 sysfs_remove_link(&dev->kobj, "of_node");
1722
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001723 if (!dev->class)
1724 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001725
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001726 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001727 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001728 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001729#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001730 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001731 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001732#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001733 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001734}
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001737 * dev_set_name - set a device name
1738 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001739 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001740 */
1741int dev_set_name(struct device *dev, const char *fmt, ...)
1742{
1743 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001744 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001745
1746 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001747 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001748 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001749 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001750}
1751EXPORT_SYMBOL_GPL(dev_set_name);
1752
1753/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001754 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1755 * @dev: device
1756 *
1757 * By default we select char/ for new entries. Setting class->dev_obj
1758 * to NULL prevents an entry from being created. class->dev_kobj must
1759 * be set (or cleared) before any devices are registered to the class
1760 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001761 * device_remove_sys_dev_entry() will disagree about the presence of
1762 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001763 */
1764static struct kobject *device_to_dev_kobj(struct device *dev)
1765{
1766 struct kobject *kobj;
1767
1768 if (dev->class)
1769 kobj = dev->class->dev_kobj;
1770 else
1771 kobj = sysfs_dev_char_kobj;
1772
1773 return kobj;
1774}
1775
1776static int device_create_sys_dev_entry(struct device *dev)
1777{
1778 struct kobject *kobj = device_to_dev_kobj(dev);
1779 int error = 0;
1780 char devt_str[15];
1781
1782 if (kobj) {
1783 format_dev_t(devt_str, dev->devt);
1784 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1785 }
1786
1787 return error;
1788}
1789
1790static void device_remove_sys_dev_entry(struct device *dev)
1791{
1792 struct kobject *kobj = device_to_dev_kobj(dev);
1793 char devt_str[15];
1794
1795 if (kobj) {
1796 format_dev_t(devt_str, dev->devt);
1797 sysfs_remove_link(kobj, devt_str);
1798 }
1799}
1800
Shaokun Zhang46d3a032018-07-15 18:08:56 +08001801static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001802{
1803 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1804 if (!dev->p)
1805 return -ENOMEM;
1806 dev->p->device = dev;
1807 klist_init(&dev->p->klist_children, klist_children_get,
1808 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001809 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001810 return 0;
1811}
1812
Dan Williamse105b8b2008-04-21 10:51:07 -07001813/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001814 * device_add - add device to device hierarchy.
1815 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001817 * This is part 2 of device_register(), though may be called
1818 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 *
Cornelia Huck57394112008-09-03 18:26:40 +02001820 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001821 * to the global and sibling lists for the device, then
1822 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001823 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001824 * Do not call this routine or device_register() more than once for
1825 * any device structure. The driver model core is not designed to work
1826 * with devices that get unregistered and then spring back to life.
1827 * (Among other things, it's very hard to guarantee that all references
1828 * to the previous incarnation of @dev have been dropped.) Allocate
1829 * and register a fresh new struct device instead.
1830 *
Cornelia Huck57394112008-09-03 18:26:40 +02001831 * NOTE: _Never_ directly free @dev after calling this function, even
1832 * if it returned an error! Always use put_device() to give up your
1833 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
1835int device_add(struct device *dev)
1836{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301837 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001838 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001839 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001840 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001841 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001844 if (!dev)
1845 goto done;
1846
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001847 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001848 error = device_private_init(dev);
1849 if (error)
1850 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001851 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001852
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001853 /*
1854 * for statically allocated devices, which should all be converted
1855 * some day, we need to initialize the name. We prevent reading back
1856 * the name, and force the use of dev_name()
1857 */
1858 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001859 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001860 dev->init_name = NULL;
1861 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001862
Kay Sieversca22e562011-12-14 14:29:38 -08001863 /* subsystems can specify simple device enumeration */
1864 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1865 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1866
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001867 if (!dev_name(dev)) {
1868 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001869 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001872 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001875 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001876 if (IS_ERR(kobj)) {
1877 error = PTR_ERR(kobj);
1878 goto parent_error;
1879 }
Kay Sieversca22e562011-12-14 14:29:38 -08001880 if (kobj)
1881 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Yinghai Lu0d358f22008-02-19 03:20:41 -08001883 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001884 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001885 set_dev_node(dev, dev_to_node(parent));
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001888 /* we require the name to be set before, and pass NULL */
1889 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001890 if (error) {
1891 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001893 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001894
Brian Walsh37022642006-08-14 22:43:19 -07001895 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001896 error = device_platform_notify(dev, KOBJ_ADD);
1897 if (error)
1898 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07001899
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001900 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001901 if (error)
1902 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001903
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001904 error = device_add_class_symlinks(dev);
1905 if (error)
1906 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001907 error = device_add_attrs(dev);
1908 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001909 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001910 error = bus_add_device(dev);
1911 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001913 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001914 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001915 goto DPMError;
1916 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001917
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001918 if (MAJOR(dev->devt)) {
1919 error = device_create_file(dev, &dev_attr_dev);
1920 if (error)
1921 goto DevAttrError;
1922
1923 error = device_create_sys_dev_entry(dev);
1924 if (error)
1925 goto SysEntryError;
1926
1927 devtmpfs_create_node(dev);
1928 }
1929
Alan Sternec0676ee2008-12-05 14:10:31 -05001930 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001931 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001932 */
1933 if (dev->bus)
1934 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1935 BUS_NOTIFY_ADD_DEVICE, dev);
1936
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001937 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001938 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001940 klist_add_tail(&dev->p->knode_parent,
1941 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001943 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001944 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001945 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001946 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001947 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001948
1949 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001950 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001951 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001952 if (class_intf->add_dev)
1953 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001954 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001955 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001956done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 put_device(dev);
1958 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001959 SysEntryError:
1960 if (MAJOR(dev->devt))
1961 device_remove_file(dev, &dev_attr_dev);
1962 DevAttrError:
1963 device_pm_remove(dev);
1964 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001965 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001966 bus_remove_device(dev);
1967 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001968 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001969 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001970 device_remove_class_symlinks(dev);
1971 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001972 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001973 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001974 device_platform_notify(dev, KOBJ_REMOVE);
1975platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01001976 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001977 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 kobject_del(&dev->kobj);
1979 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001980 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001981parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01001982 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001983name_error:
1984 kfree(dev->p);
1985 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001986 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
David Graham White86df2682013-07-21 20:41:14 -04001988EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001991 * device_register - register a device with the system.
1992 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001994 * This happens in two clean steps - initialize the device
1995 * and add it to the system. The two steps can be called
1996 * separately, but this is the easiest and most common.
1997 * I.e. you should only call the two helpers separately if
1998 * have a clearly defined need to use and refcount the device
1999 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02002000 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002001 * For more information, see the kerneldoc for device_initialize()
2002 * and device_add().
2003 *
Cornelia Huck57394112008-09-03 18:26:40 +02002004 * NOTE: _Never_ directly free @dev after calling this function, even
2005 * if it returned an error! Always use put_device() to give up the
2006 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008int device_register(struct device *dev)
2009{
2010 device_initialize(dev);
2011 return device_add(dev);
2012}
David Graham White86df2682013-07-21 20:41:14 -04002013EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002016 * get_device - increment reference count for device.
2017 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002019 * This simply forwards the call to kobject_get(), though
2020 * we do take care to provide for the case that we get a NULL
2021 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002023struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002025 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
David Graham White86df2682013-07-21 20:41:14 -04002027EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002030 * put_device - decrement reference count.
2031 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002033void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02002035 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (dev)
2037 kobject_put(&dev->kobj);
2038}
David Graham White86df2682013-07-21 20:41:14 -04002039EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002042 * device_del - delete device from system.
2043 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002045 * This is the first part of the device unregistration
2046 * sequence. This removes the device from the lists we control
2047 * from here, has it removed from the other driver model
2048 * subsystems it was added to in device_add(), and removes it
2049 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002051 * NOTE: this should be called manually _iff_ device_add() was
2052 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002054void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002056 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002057 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002058 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Alan Sternec0676ee2008-12-05 14:10:31 -05002060 /* Notify clients of device removal. This call must come
2061 * before dpm_sysfs_remove().
2062 */
2063 if (dev->bus)
2064 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2065 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002066
Alan Stern3b98aea2008-08-07 13:06:12 -04002067 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002069 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002070 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002071 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002072 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002073 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002074 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002075 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002076 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002077
Kay Sieversca22e562011-12-14 14:29:38 -08002078 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002079 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002080 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002081 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002082 if (class_intf->remove_dev)
2083 class_intf->remove_dev(dev, class_intf);
2084 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02002085 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002086 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002087 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002088 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002089 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002090 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002091 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002092 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002093 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02002094 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002095 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Joerg Roedel599bad32014-09-30 13:02:02 +02002097 if (dev->bus)
2098 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2099 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002100 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002101 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002103 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002104 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
David Graham White86df2682013-07-21 20:41:14 -04002106EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002109 * device_unregister - unregister device from system.
2110 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002112 * We do this in two parts, like we do device_register(). First,
2113 * we remove it from all the subsystems with device_del(), then
2114 * we decrement the reference count via put_device(). If that
2115 * is the final reference count, the device will be cleaned up
2116 * via device_release() above. Otherwise, the structure will
2117 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002119void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002121 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 device_del(dev);
2123 put_device(dev);
2124}
David Graham White86df2682013-07-21 20:41:14 -04002125EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002127static struct device *prev_device(struct klist_iter *i)
2128{
2129 struct klist_node *n = klist_prev(i);
2130 struct device *dev = NULL;
2131 struct device_private *p;
2132
2133 if (n) {
2134 p = to_device_private_parent(n);
2135 dev = p->device;
2136 }
2137 return dev;
2138}
2139
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002140static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002141{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002142 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002143 struct device *dev = NULL;
2144 struct device_private *p;
2145
2146 if (n) {
2147 p = to_device_private_parent(n);
2148 dev = p->device;
2149 }
2150 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002151}
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002154 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002155 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002156 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002157 * @uid: returned file owner
2158 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002159 * @tmp: possibly allocated string
2160 *
2161 * Return the relative path of a possible device node.
2162 * Non-default names may need to allocate a memory to compose
2163 * a name. This memory is returned in tmp and needs to be
2164 * freed by the caller.
2165 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002166const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002167 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002168 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002169{
2170 char *s;
2171
2172 *tmp = NULL;
2173
2174 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002175 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002176 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002177 if (*tmp)
2178 return *tmp;
2179
2180 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002181 if (dev->class && dev->class->devnode)
2182 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002183 if (*tmp)
2184 return *tmp;
2185
2186 /* return name without allocation, tmp == NULL */
2187 if (strchr(dev_name(dev), '!') == NULL)
2188 return dev_name(dev);
2189
2190 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002191 s = kstrdup(dev_name(dev), GFP_KERNEL);
2192 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002193 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002194 strreplace(s, '!', '/');
2195 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002196}
2197
2198/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002199 * device_for_each_child - device child iterator.
2200 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002201 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002202 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002204 * Iterate over @parent's child devices, and call @fn for each,
2205 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002207 * We check the return of @fn each time. If it returns anything
2208 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002210int device_for_each_child(struct device *parent, void *data,
2211 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002213 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002214 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 int error = 0;
2216
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002217 if (!parent->p)
2218 return 0;
2219
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002220 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002221 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002222 error = fn(child, data);
2223 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return error;
2225}
David Graham White86df2682013-07-21 20:41:14 -04002226EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Cornelia Huck5ab69982006-11-16 15:42:07 +01002228/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002229 * device_for_each_child_reverse - device child iterator in reversed order.
2230 * @parent: parent struct device.
2231 * @fn: function to be called for each device.
2232 * @data: data for the callback.
2233 *
2234 * Iterate over @parent's child devices, and call @fn for each,
2235 * passing it @data.
2236 *
2237 * We check the return of @fn each time. If it returns anything
2238 * other than 0, we break out and return that value.
2239 */
2240int device_for_each_child_reverse(struct device *parent, void *data,
2241 int (*fn)(struct device *dev, void *data))
2242{
2243 struct klist_iter i;
2244 struct device *child;
2245 int error = 0;
2246
2247 if (!parent->p)
2248 return 0;
2249
2250 klist_iter_init(&parent->p->klist_children, &i);
2251 while ((child = prev_device(&i)) && !error)
2252 error = fn(child, data);
2253 klist_iter_exit(&i);
2254 return error;
2255}
2256EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2257
2258/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002259 * device_find_child - device iterator for locating a particular device.
2260 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002261 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002262 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002263 *
2264 * This is similar to the device_for_each_child() function above, but it
2265 * returns a reference to a device that is 'found' for later use, as
2266 * determined by the @match callback.
2267 *
2268 * The callback should return 0 if the device doesn't match and non-zero
2269 * if it does. If the callback returns non-zero and a reference to the
2270 * current device can be obtained, this function will return to the caller
2271 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002272 *
2273 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002274 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002275struct device *device_find_child(struct device *parent, void *data,
2276 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002277{
2278 struct klist_iter i;
2279 struct device *child;
2280
2281 if (!parent)
2282 return NULL;
2283
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002284 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002285 while ((child = next_device(&i)))
2286 if (match(child, data) && get_device(child))
2287 break;
2288 klist_iter_exit(&i);
2289 return child;
2290}
David Graham White86df2682013-07-21 20:41:14 -04002291EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293int __init devices_init(void)
2294{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002295 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2296 if (!devices_kset)
2297 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002298 dev_kobj = kobject_create_and_add("dev", NULL);
2299 if (!dev_kobj)
2300 goto dev_kobj_err;
2301 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2302 if (!sysfs_dev_block_kobj)
2303 goto block_kobj_err;
2304 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2305 if (!sysfs_dev_char_kobj)
2306 goto char_kobj_err;
2307
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002308 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002309
2310 char_kobj_err:
2311 kobject_put(sysfs_dev_block_kobj);
2312 block_kobj_err:
2313 kobject_put(dev_kobj);
2314 dev_kobj_err:
2315 kset_unregister(devices_kset);
2316 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002319static int device_check_offline(struct device *dev, void *not_used)
2320{
2321 int ret;
2322
2323 ret = device_for_each_child(dev, NULL, device_check_offline);
2324 if (ret)
2325 return ret;
2326
2327 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2328}
2329
2330/**
2331 * device_offline - Prepare the device for hot-removal.
2332 * @dev: Device to be put offline.
2333 *
2334 * Execute the device bus type's .offline() callback, if present, to prepare
2335 * the device for a subsequent hot-removal. If that succeeds, the device must
2336 * not be used until either it is removed or its bus type's .online() callback
2337 * is executed.
2338 *
2339 * Call under device_hotplug_lock.
2340 */
2341int device_offline(struct device *dev)
2342{
2343 int ret;
2344
2345 if (dev->offline_disabled)
2346 return -EPERM;
2347
2348 ret = device_for_each_child(dev, NULL, device_check_offline);
2349 if (ret)
2350 return ret;
2351
2352 device_lock(dev);
2353 if (device_supports_offline(dev)) {
2354 if (dev->offline) {
2355 ret = 1;
2356 } else {
2357 ret = dev->bus->offline(dev);
2358 if (!ret) {
2359 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2360 dev->offline = true;
2361 }
2362 }
2363 }
2364 device_unlock(dev);
2365
2366 return ret;
2367}
2368
2369/**
2370 * device_online - Put the device back online after successful device_offline().
2371 * @dev: Device to be put back online.
2372 *
2373 * If device_offline() has been successfully executed for @dev, but the device
2374 * has not been removed subsequently, execute its bus type's .online() callback
2375 * to indicate that the device can be used again.
2376 *
2377 * Call under device_hotplug_lock.
2378 */
2379int device_online(struct device *dev)
2380{
2381 int ret = 0;
2382
2383 device_lock(dev);
2384 if (device_supports_offline(dev)) {
2385 if (dev->offline) {
2386 ret = dev->bus->online(dev);
2387 if (!ret) {
2388 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2389 dev->offline = false;
2390 }
2391 } else {
2392 ret = 1;
2393 }
2394 }
2395 device_unlock(dev);
2396
2397 return ret;
2398}
2399
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002400struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002401 struct device dev;
2402 struct module *owner;
2403};
2404
Josh Triplett93058422012-11-18 21:27:55 -08002405static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002406{
2407 return container_of(d, struct root_device, dev);
2408}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002409
2410static void root_device_release(struct device *dev)
2411{
2412 kfree(to_root_device(dev));
2413}
2414
2415/**
2416 * __root_device_register - allocate and register a root device
2417 * @name: root device name
2418 * @owner: owner module of the root device, usually THIS_MODULE
2419 *
2420 * This function allocates a root device and registers it
2421 * using device_register(). In order to free the returned
2422 * device, use root_device_unregister().
2423 *
2424 * Root devices are dummy devices which allow other devices
2425 * to be grouped under /sys/devices. Use this function to
2426 * allocate a root device and then use it as the parent of
2427 * any device which should appear under /sys/devices/{name}
2428 *
2429 * The /sys/devices/{name} directory will also contain a
2430 * 'module' symlink which points to the @owner directory
2431 * in sysfs.
2432 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002433 * Returns &struct device pointer on success, or ERR_PTR() on error.
2434 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002435 * Note: You probably want to use root_device_register().
2436 */
2437struct device *__root_device_register(const char *name, struct module *owner)
2438{
2439 struct root_device *root;
2440 int err = -ENOMEM;
2441
2442 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2443 if (!root)
2444 return ERR_PTR(err);
2445
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002446 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002447 if (err) {
2448 kfree(root);
2449 return ERR_PTR(err);
2450 }
2451
2452 root->dev.release = root_device_release;
2453
2454 err = device_register(&root->dev);
2455 if (err) {
2456 put_device(&root->dev);
2457 return ERR_PTR(err);
2458 }
2459
Christoph Egger1d9e8822010-05-17 16:57:58 +02002460#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002461 if (owner) {
2462 struct module_kobject *mk = &owner->mkobj;
2463
2464 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2465 if (err) {
2466 device_unregister(&root->dev);
2467 return ERR_PTR(err);
2468 }
2469 root->owner = owner;
2470 }
2471#endif
2472
2473 return &root->dev;
2474}
2475EXPORT_SYMBOL_GPL(__root_device_register);
2476
2477/**
2478 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002479 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002480 *
2481 * This function unregisters and cleans up a device that was created by
2482 * root_device_register().
2483 */
2484void root_device_unregister(struct device *dev)
2485{
2486 struct root_device *root = to_root_device(dev);
2487
2488 if (root->owner)
2489 sysfs_remove_link(&root->dev.kobj, "module");
2490
2491 device_unregister(dev);
2492}
2493EXPORT_SYMBOL_GPL(root_device_unregister);
2494
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002495
2496static void device_create_release(struct device *dev)
2497{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002498 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002499 kfree(dev);
2500}
2501
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002502static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002503device_create_groups_vargs(struct class *class, struct device *parent,
2504 dev_t devt, void *drvdata,
2505 const struct attribute_group **groups,
2506 const char *fmt, va_list args)
2507{
2508 struct device *dev = NULL;
2509 int retval = -ENODEV;
2510
2511 if (class == NULL || IS_ERR(class))
2512 goto error;
2513
2514 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2515 if (!dev) {
2516 retval = -ENOMEM;
2517 goto error;
2518 }
2519
David Herrmannbbc780f2013-11-21 20:15:48 +01002520 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002521 dev->devt = devt;
2522 dev->class = class;
2523 dev->parent = parent;
2524 dev->groups = groups;
2525 dev->release = device_create_release;
2526 dev_set_drvdata(dev, drvdata);
2527
2528 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2529 if (retval)
2530 goto error;
2531
David Herrmannbbc780f2013-11-21 20:15:48 +01002532 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002533 if (retval)
2534 goto error;
2535
2536 return dev;
2537
2538error:
2539 put_device(dev);
2540 return ERR_PTR(retval);
2541}
2542
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002543/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002544 * device_create_vargs - creates a device and registers it with sysfs
2545 * @class: pointer to the struct class that this device should be registered to
2546 * @parent: pointer to the parent struct device of this new device, if any
2547 * @devt: the dev_t for the char device to be added
2548 * @drvdata: the data to be added to the device for callbacks
2549 * @fmt: string for the device's name
2550 * @args: va_list for the device's name
2551 *
2552 * This function can be used by char device classes. A struct device
2553 * will be created in sysfs, registered to the specified class.
2554 *
2555 * A "dev" file will be created, showing the dev_t for the device, if
2556 * the dev_t is not 0,0.
2557 * If a pointer to a parent struct device is passed in, the newly created
2558 * struct device will be a child of that device in sysfs.
2559 * The pointer to the struct device will be returned from the call.
2560 * Any further sysfs files that might be required can be created using this
2561 * pointer.
2562 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002563 * Returns &struct device pointer on success, or ERR_PTR() on error.
2564 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002565 * Note: the struct class passed to this function must have previously
2566 * been created with a call to class_create().
2567 */
2568struct device *device_create_vargs(struct class *class, struct device *parent,
2569 dev_t devt, void *drvdata, const char *fmt,
2570 va_list args)
2571{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002572 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2573 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002574}
2575EXPORT_SYMBOL_GPL(device_create_vargs);
2576
2577/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002578 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002579 * @class: pointer to the struct class that this device should be registered to
2580 * @parent: pointer to the parent struct device of this new device, if any
2581 * @devt: the dev_t for the char device to be added
2582 * @drvdata: the data to be added to the device for callbacks
2583 * @fmt: string for the device's name
2584 *
2585 * This function can be used by char device classes. A struct device
2586 * will be created in sysfs, registered to the specified class.
2587 *
2588 * A "dev" file will be created, showing the dev_t for the device, if
2589 * the dev_t is not 0,0.
2590 * If a pointer to a parent struct device is passed in, the newly created
2591 * struct device will be a child of that device in sysfs.
2592 * The pointer to the struct device will be returned from the call.
2593 * Any further sysfs files that might be required can be created using this
2594 * pointer.
2595 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002596 * Returns &struct device pointer on success, or ERR_PTR() on error.
2597 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002598 * Note: the struct class passed to this function must have previously
2599 * been created with a call to class_create().
2600 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002601struct device *device_create(struct class *class, struct device *parent,
2602 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002603{
2604 va_list vargs;
2605 struct device *dev;
2606
2607 va_start(vargs, fmt);
2608 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2609 va_end(vargs);
2610 return dev;
2611}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002612EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002613
Guenter Roeck39ef3112013-07-14 16:05:57 -07002614/**
2615 * device_create_with_groups - creates a device and registers it with sysfs
2616 * @class: pointer to the struct class that this device should be registered to
2617 * @parent: pointer to the parent struct device of this new device, if any
2618 * @devt: the dev_t for the char device to be added
2619 * @drvdata: the data to be added to the device for callbacks
2620 * @groups: NULL-terminated list of attribute groups to be created
2621 * @fmt: string for the device's name
2622 *
2623 * This function can be used by char device classes. A struct device
2624 * will be created in sysfs, registered to the specified class.
2625 * Additional attributes specified in the groups parameter will also
2626 * be created automatically.
2627 *
2628 * A "dev" file will be created, showing the dev_t for the device, if
2629 * the dev_t is not 0,0.
2630 * If a pointer to a parent struct device is passed in, the newly created
2631 * struct device will be a child of that device in sysfs.
2632 * The pointer to the struct device will be returned from the call.
2633 * Any further sysfs files that might be required can be created using this
2634 * pointer.
2635 *
2636 * Returns &struct device pointer on success, or ERR_PTR() on error.
2637 *
2638 * Note: the struct class passed to this function must have previously
2639 * been created with a call to class_create().
2640 */
2641struct device *device_create_with_groups(struct class *class,
2642 struct device *parent, dev_t devt,
2643 void *drvdata,
2644 const struct attribute_group **groups,
2645 const char *fmt, ...)
2646{
2647 va_list vargs;
2648 struct device *dev;
2649
2650 va_start(vargs, fmt);
2651 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2652 fmt, vargs);
2653 va_end(vargs);
2654 return dev;
2655}
2656EXPORT_SYMBOL_GPL(device_create_with_groups);
2657
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002658static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002659{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002660 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002661
Dave Youngcd354492008-01-28 16:56:11 +08002662 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002663}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002664
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002665/**
2666 * device_destroy - removes a device that was created with device_create()
2667 * @class: pointer to the struct class that this device was registered with
2668 * @devt: the dev_t of the device that was previously registered
2669 *
2670 * This call unregisters and cleans up a device that was created with a
2671 * call to device_create().
2672 */
2673void device_destroy(struct class *class, dev_t devt)
2674{
2675 struct device *dev;
2676
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002677 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002678 if (dev) {
2679 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002680 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002681 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002682}
2683EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002684
2685/**
2686 * device_rename - renames a device
2687 * @dev: the pointer to the struct device to be renamed
2688 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002689 *
2690 * It is the responsibility of the caller to provide mutual
2691 * exclusion between two different calls of device_rename
2692 * on the same device to ensure that new_name is valid and
2693 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002694 *
Timur Tabia5462512010-12-13 14:08:52 -06002695 * Note: Don't call this function. Currently, the networking layer calls this
2696 * function, but that will change. The following text from Kay Sievers offers
2697 * some insight:
2698 *
2699 * Renaming devices is racy at many levels, symlinks and other stuff are not
2700 * replaced atomically, and you get a "move" uevent, but it's not easy to
2701 * connect the event to the old and new device. Device nodes are not renamed at
2702 * all, there isn't even support for that in the kernel now.
2703 *
2704 * In the meantime, during renaming, your target name might be taken by another
2705 * driver, creating conflicts. Or the old name is taken directly after you
2706 * renamed it -- then you get events for the same DEVPATH, before you even see
2707 * the "move" event. It's just a mess, and nothing new should ever rely on
2708 * kernel device renaming. Besides that, it's not even implemented now for
2709 * other things than (driver-core wise very simple) network devices.
2710 *
2711 * We are currently about to change network renaming in udev to completely
2712 * disallow renaming of devices in the same namespace as the kernel uses,
2713 * because we can't solve the problems properly, that arise with swapping names
2714 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2715 * be allowed to some other name than eth[0-9]*, for the aforementioned
2716 * reasons.
2717 *
2718 * Make up a "real" name in the driver before you register anything, or add
2719 * some other attributes for userspace to find the device, or use udev to add
2720 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2721 * don't even want to get into that and try to implement the missing pieces in
2722 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002723 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002724int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002725{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002726 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002727 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002728 int error;
2729
2730 dev = get_device(dev);
2731 if (!dev)
2732 return -EINVAL;
2733
ethan.zhao69df7532013-10-13 22:12:35 +08002734 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002735
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002736 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002737 if (!old_device_name) {
2738 error = -ENOMEM;
2739 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002740 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002741
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002742 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002743 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2744 kobj, old_device_name,
2745 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002746 if (error)
2747 goto out;
2748 }
Kay Sievers39aba962010-09-04 22:33:14 -07002749
Tejun Heo4b30ee52013-09-11 22:29:06 -04002750 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002751 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002752 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002753
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002754out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002755 put_device(dev);
2756
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002757 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002758
2759 return error;
2760}
Johannes Berga2807db2007-02-28 12:38:31 +01002761EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002762
2763static int device_move_class_links(struct device *dev,
2764 struct device *old_parent,
2765 struct device *new_parent)
2766{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002767 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002768
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002769 if (old_parent)
2770 sysfs_remove_link(&dev->kobj, "device");
2771 if (new_parent)
2772 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2773 "device");
2774 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002775}
2776
2777/**
2778 * device_move - moves a device to a new parent
2779 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02002780 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002781 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002782 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002783int device_move(struct device *dev, struct device *new_parent,
2784 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002785{
2786 int error;
2787 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002788 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002789
2790 dev = get_device(dev);
2791 if (!dev)
2792 return -EINVAL;
2793
Cornelia Huckffa6a702009-03-04 12:44:00 +01002794 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002795 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002796 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002797 if (IS_ERR(new_parent_kobj)) {
2798 error = PTR_ERR(new_parent_kobj);
2799 put_device(new_parent);
2800 goto out;
2801 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002802
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002803 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2804 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002805 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002806 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002807 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002808 put_device(new_parent);
2809 goto out;
2810 }
2811 old_parent = dev->parent;
2812 dev->parent = new_parent;
2813 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002814 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002815 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002816 klist_add_tail(&dev->p->knode_parent,
2817 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002818 set_dev_node(dev, dev_to_node(new_parent));
2819 }
2820
Rabin Vincentbdd40342012-04-23 09:16:36 +02002821 if (dev->class) {
2822 error = device_move_class_links(dev, old_parent, new_parent);
2823 if (error) {
2824 /* We ignore errors on cleanup since we're hosed anyway... */
2825 device_move_class_links(dev, new_parent, old_parent);
2826 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2827 if (new_parent)
2828 klist_remove(&dev->p->knode_parent);
2829 dev->parent = old_parent;
2830 if (old_parent) {
2831 klist_add_tail(&dev->p->knode_parent,
2832 &old_parent->p->klist_children);
2833 set_dev_node(dev, dev_to_node(old_parent));
2834 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002835 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002836 cleanup_glue_dir(dev, new_parent_kobj);
2837 put_device(new_parent);
2838 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002839 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002840 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002841 switch (dpm_order) {
2842 case DPM_ORDER_NONE:
2843 break;
2844 case DPM_ORDER_DEV_AFTER_PARENT:
2845 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002846 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002847 break;
2848 case DPM_ORDER_PARENT_BEFORE_DEV:
2849 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002850 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002851 break;
2852 case DPM_ORDER_DEV_LAST:
2853 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002854 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002855 break;
2856 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002857
Cornelia Huck8a824722006-11-20 17:07:51 +01002858 put_device(old_parent);
2859out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002860 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002861 put_device(dev);
2862 return error;
2863}
Cornelia Huck8a824722006-11-20 17:07:51 +01002864EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002865
2866/**
2867 * device_shutdown - call ->shutdown() on each device to shutdown.
2868 */
2869void device_shutdown(void)
2870{
Benson Leungf123db82013-09-24 20:05:11 -07002871 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002872
Pingfan Liu3297c8f2018-07-19 13:14:58 +08002873 wait_for_device_probe();
2874 device_block_probing();
2875
Hugh Daschbach62458382010-03-22 10:36:37 -07002876 spin_lock(&devices_kset->list_lock);
2877 /*
2878 * Walk the devices list backward, shutting down each in turn.
2879 * Beware that device unplug events may also start pulling
2880 * devices offline, even as the system is shutting down.
2881 */
2882 while (!list_empty(&devices_kset->list)) {
2883 dev = list_entry(devices_kset->list.prev, struct device,
2884 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002885
2886 /*
2887 * hold reference count of device's parent to
2888 * prevent it from being freed because parent's
2889 * lock is to be held
2890 */
Benson Leungf123db82013-09-24 20:05:11 -07002891 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002892 get_device(dev);
2893 /*
2894 * Make sure the device is off the kset list, in the
2895 * event that dev->*->shutdown() doesn't remove it.
2896 */
2897 list_del_init(&dev->kobj.entry);
2898 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002899
Ming Leid1c6c032012-06-22 18:01:40 +08002900 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002901 if (parent)
2902 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002903 device_lock(dev);
2904
Alan Sternfe6b91f2011-12-06 23:24:52 +01002905 /* Don't allow any more runtime suspends */
2906 pm_runtime_get_noresume(dev);
2907 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002908
Michal Suchanek75216212017-08-11 15:44:43 +02002909 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07002910 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02002911 dev_info(dev, "shutdown_pre\n");
2912 dev->class->shutdown_pre(dev);
2913 }
2914 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002915 if (initcall_debug)
2916 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002917 dev->bus->shutdown(dev);
2918 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002919 if (initcall_debug)
2920 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002921 dev->driver->shutdown(dev);
2922 }
Ming Leid1c6c032012-06-22 18:01:40 +08002923
2924 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002925 if (parent)
2926 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002927
Hugh Daschbach62458382010-03-22 10:36:37 -07002928 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002929 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002930
2931 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002932 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002933 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002934}
Joe Perches99bcf212010-06-27 01:02:34 +00002935
2936/*
2937 * Device logging functions
2938 */
2939
2940#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002941static int
2942create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002943{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002944 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002945 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002946
Kay Sieversc4e00da2012-05-03 02:29:59 +02002947 if (dev->class)
2948 subsys = dev->class->name;
2949 else if (dev->bus)
2950 subsys = dev->bus->name;
2951 else
Joe Perches798efc62012-09-12 20:11:29 -07002952 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002953
Joe Perches798efc62012-09-12 20:11:29 -07002954 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002955 if (pos >= hdrlen)
2956 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002957
2958 /*
2959 * Add device identifier DEVICE=:
2960 * b12:8 block dev_t
2961 * c127:3 char dev_t
2962 * n8 netdev ifindex
2963 * +sound:card0 subsystem:devname
2964 */
2965 if (MAJOR(dev->devt)) {
2966 char c;
2967
2968 if (strcmp(subsys, "block") == 0)
2969 c = 'b';
2970 else
2971 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002972 pos++;
2973 pos += snprintf(hdr + pos, hdrlen - pos,
2974 "DEVICE=%c%u:%u",
2975 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002976 } else if (strcmp(subsys, "net") == 0) {
2977 struct net_device *net = to_net_dev(dev);
2978
Joe Perches798efc62012-09-12 20:11:29 -07002979 pos++;
2980 pos += snprintf(hdr + pos, hdrlen - pos,
2981 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002982 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002983 pos++;
2984 pos += snprintf(hdr + pos, hdrlen - pos,
2985 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002986 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002987
Ben Hutchings655e5b72014-08-26 00:34:44 -07002988 if (pos >= hdrlen)
2989 goto overflow;
2990
Joe Perches798efc62012-09-12 20:11:29 -07002991 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002992
2993overflow:
2994 dev_WARN(dev, "device/subsystem name too long");
2995 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002996}
Joe Perches798efc62012-09-12 20:11:29 -07002997
Joe Perches05e4e5b2012-09-12 20:13:37 -07002998int dev_vprintk_emit(int level, const struct device *dev,
2999 const char *fmt, va_list args)
3000{
3001 char hdr[128];
3002 size_t hdrlen;
3003
3004 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3005
3006 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3007}
3008EXPORT_SYMBOL(dev_vprintk_emit);
3009
3010int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3011{
3012 va_list args;
3013 int r;
3014
3015 va_start(args, fmt);
3016
3017 r = dev_vprintk_emit(level, dev, fmt, args);
3018
3019 va_end(args);
3020
3021 return r;
3022}
3023EXPORT_SYMBOL(dev_printk_emit);
3024
Joe Perchesd1f10522014-12-25 15:07:04 -08003025static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07003026 struct va_format *vaf)
3027{
Joe Perchesd1f10522014-12-25 15:07:04 -08003028 if (dev)
3029 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3030 dev_driver_string(dev), dev_name(dev), vaf);
3031 else
3032 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003033}
Joe Perches99bcf212010-06-27 01:02:34 +00003034
Joe Perchesd1f10522014-12-25 15:07:04 -08003035void dev_printk(const char *level, const struct device *dev,
3036 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00003037{
3038 struct va_format vaf;
3039 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00003040
3041 va_start(args, fmt);
3042
3043 vaf.fmt = fmt;
3044 vaf.va = &args;
3045
Joe Perchesd1f10522014-12-25 15:07:04 -08003046 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003047
Joe Perches99bcf212010-06-27 01:02:34 +00003048 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00003049}
3050EXPORT_SYMBOL(dev_printk);
3051
3052#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08003053void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00003054{ \
3055 struct va_format vaf; \
3056 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003057 \
3058 va_start(args, fmt); \
3059 \
3060 vaf.fmt = fmt; \
3061 vaf.va = &args; \
3062 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003063 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003064 \
Joe Perches99bcf212010-06-27 01:02:34 +00003065 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003066} \
3067EXPORT_SYMBOL(func);
3068
Joe Perches663336e2018-05-09 08:15:46 -07003069define_dev_printk_level(_dev_emerg, KERN_EMERG);
3070define_dev_printk_level(_dev_alert, KERN_ALERT);
3071define_dev_printk_level(_dev_crit, KERN_CRIT);
3072define_dev_printk_level(_dev_err, KERN_ERR);
3073define_dev_printk_level(_dev_warn, KERN_WARNING);
3074define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00003075define_dev_printk_level(_dev_info, KERN_INFO);
3076
3077#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003078
3079static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3080{
3081 return fwnode && !IS_ERR(fwnode->secondary);
3082}
3083
3084/**
3085 * set_primary_fwnode - Change the primary firmware node of a given device.
3086 * @dev: Device to handle.
3087 * @fwnode: New primary firmware node of the device.
3088 *
3089 * Set the device's firmware node pointer to @fwnode, but if a secondary
3090 * firmware node of the device is present, preserve it.
3091 */
3092void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3093{
3094 if (fwnode) {
3095 struct fwnode_handle *fn = dev->fwnode;
3096
3097 if (fwnode_is_primary(fn))
3098 fn = fn->secondary;
3099
Mika Westerberg55f89a82015-11-30 17:11:39 +02003100 if (fn) {
3101 WARN_ON(fwnode->secondary);
3102 fwnode->secondary = fn;
3103 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003104 dev->fwnode = fwnode;
3105 } else {
3106 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3107 dev->fwnode->secondary : NULL;
3108 }
3109}
3110EXPORT_SYMBOL_GPL(set_primary_fwnode);
3111
3112/**
3113 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3114 * @dev: Device to handle.
3115 * @fwnode: New secondary firmware node of the device.
3116 *
3117 * If a primary firmware node of the device is present, set its secondary
3118 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3119 * @fwnode.
3120 */
3121void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3122{
3123 if (fwnode)
3124 fwnode->secondary = ERR_PTR(-ENODEV);
3125
3126 if (fwnode_is_primary(dev->fwnode))
3127 dev->fwnode->secondary = fwnode;
3128 else
3129 dev->fwnode = fwnode;
3130}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003131
3132/**
3133 * device_set_of_node_from_dev - reuse device-tree node of another device
3134 * @dev: device whose device-tree node is being set
3135 * @dev2: device whose device-tree node is being reused
3136 *
3137 * Takes another reference to the new device-tree node after first dropping
3138 * any reference held to the old node.
3139 */
3140void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3141{
3142 of_node_put(dev->of_node);
3143 dev->of_node = of_node_get(dev2->of_node);
3144 dev->of_node_reused = true;
3145}
3146EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);