blob: 04bbcd779e114ef1ecad0df77591c0801a0bf84b [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/base/core.c - core driver model code (device registration, etc)
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07007 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020013#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070018#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100019#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070020#include <linux/of.h>
21#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010022#include <linux/genhd.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070023#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010024#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020025#include <linux/netdevice.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070027#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "base.h"
30#include "power/power.h"
31
Andi Kleene52eec12010-09-08 16:54:17 +020032#ifdef CONFIG_SYSFS_DEPRECATED
33#ifdef CONFIG_SYSFS_DEPRECATED_V2
34long sysfs_deprecated = 1;
35#else
36long sysfs_deprecated = 0;
37#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080038static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020039{
Jingoo Han34da5e62013-07-26 13:10:22 +090040 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020041}
42early_param("sysfs.deprecated", sysfs_deprecated_setup);
43#endif
44
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010045/* Device links support. */
46
47#ifdef CONFIG_SRCU
48static DEFINE_MUTEX(device_links_lock);
49DEFINE_STATIC_SRCU(device_links_srcu);
50
51static inline void device_links_write_lock(void)
52{
53 mutex_lock(&device_links_lock);
54}
55
56static inline void device_links_write_unlock(void)
57{
58 mutex_unlock(&device_links_lock);
59}
60
61int device_links_read_lock(void)
62{
63 return srcu_read_lock(&device_links_srcu);
64}
65
66void device_links_read_unlock(int idx)
67{
68 srcu_read_unlock(&device_links_srcu, idx);
69}
70#else /* !CONFIG_SRCU */
71static DECLARE_RWSEM(device_links_lock);
72
73static inline void device_links_write_lock(void)
74{
75 down_write(&device_links_lock);
76}
77
78static inline void device_links_write_unlock(void)
79{
80 up_write(&device_links_lock);
81}
82
83int device_links_read_lock(void)
84{
85 down_read(&device_links_lock);
86 return 0;
87}
88
89void device_links_read_unlock(int not_used)
90{
91 up_read(&device_links_lock);
92}
93#endif /* !CONFIG_SRCU */
94
95/**
96 * device_is_dependent - Check if one device depends on another one
97 * @dev: Device to check dependencies for.
98 * @target: Device to check against.
99 *
100 * Check if @target depends on @dev or any device dependent on it (its child or
101 * its consumer etc). Return 1 if that is the case or 0 otherwise.
102 */
103static int device_is_dependent(struct device *dev, void *target)
104{
105 struct device_link *link;
106 int ret;
107
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200108 if (dev == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100109 return 1;
110
111 ret = device_for_each_child(dev, target, device_is_dependent);
112 if (ret)
113 return ret;
114
115 list_for_each_entry(link, &dev->links.consumers, s_node) {
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200116 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100117 return 1;
118
119 ret = device_is_dependent(link->consumer, target);
120 if (ret)
121 break;
122 }
123 return ret;
124}
125
126static int device_reorder_to_tail(struct device *dev, void *not_used)
127{
128 struct device_link *link;
129
130 /*
131 * Devices that have not been registered yet will be put to the ends
132 * of the lists during the registration, so skip them here.
133 */
134 if (device_is_registered(dev))
135 devices_kset_move_last(dev);
136
137 if (device_pm_initialized(dev))
138 device_pm_move_last(dev);
139
140 device_for_each_child(dev, NULL, device_reorder_to_tail);
141 list_for_each_entry(link, &dev->links.consumers, s_node)
142 device_reorder_to_tail(link->consumer, NULL);
143
144 return 0;
145}
146
147/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700148 * device_pm_move_to_tail - Move set of devices to the end of device lists
149 * @dev: Device to move
150 *
151 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
152 *
153 * It moves the @dev along with all of its children and all of its consumers
154 * to the ends of the device_kset and dpm_list, recursively.
155 */
156void device_pm_move_to_tail(struct device *dev)
157{
158 int idx;
159
160 idx = device_links_read_lock();
161 device_pm_lock();
162 device_reorder_to_tail(dev, NULL);
163 device_pm_unlock();
164 device_links_read_unlock(idx);
165}
166
167/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100168 * device_link_add - Create a link between two devices.
169 * @consumer: Consumer end of the link.
170 * @supplier: Supplier end of the link.
171 * @flags: Link flags.
172 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100173 * The caller is responsible for the proper synchronization of the link creation
174 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
175 * runtime PM framework to take the link into account. Second, if the
176 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
177 * be forced into the active metastate and reference-counted upon the creation
178 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
179 * ignored.
180 *
Vivek Gautame88728f2018-06-27 18:20:55 +0530181 * If the DL_FLAG_AUTOREMOVE_CONSUMER is set, the link will be removed
182 * automatically when the consumer device driver unbinds from it.
183 * The combination of both DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_STATELESS
184 * set is invalid and will cause NULL to be returned.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100185 *
186 * A side effect of the link creation is re-ordering of dpm_list and the
187 * devices_kset list by moving the consumer device and all devices depending
188 * on it to the ends of these lists (that does not happen to devices that have
189 * not been registered when this function is called).
190 *
191 * The supplier device is required to be registered when this function is called
192 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100193 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100194 */
195struct device_link *device_link_add(struct device *consumer,
196 struct device *supplier, u32 flags)
197{
198 struct device_link *link;
199
200 if (!consumer || !supplier ||
Vivek Gautame88728f2018-06-27 18:20:55 +0530201 ((flags & DL_FLAG_STATELESS) &&
202 (flags & DL_FLAG_AUTOREMOVE_CONSUMER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100203 return NULL;
204
205 device_links_write_lock();
206 device_pm_lock();
207
208 /*
209 * If the supplier has not been fully registered yet or there is a
210 * reverse dependency between the consumer and the supplier already in
211 * the graph, return NULL.
212 */
213 if (!device_pm_initialized(supplier)
214 || device_is_dependent(consumer, supplier)) {
215 link = NULL;
216 goto out;
217 }
218
219 list_for_each_entry(link, &supplier->links.consumers, s_node)
Lukas Wunneread18c22018-02-10 19:27:12 +0100220 if (link->consumer == consumer) {
221 kref_get(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100222 goto out;
Lukas Wunneread18c22018-02-10 19:27:12 +0100223 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100224
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100225 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100226 if (!link)
227 goto out;
228
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100229 if (flags & DL_FLAG_PM_RUNTIME) {
230 if (flags & DL_FLAG_RPM_ACTIVE) {
231 if (pm_runtime_get_sync(supplier) < 0) {
232 pm_runtime_put_noidle(supplier);
233 kfree(link);
234 link = NULL;
235 goto out;
236 }
237 link->rpm_active = true;
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100238 }
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100239 pm_runtime_new_link(consumer);
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200240 /*
241 * If the link is being added by the consumer driver at probe
242 * time, balance the decrementation of the supplier's runtime PM
243 * usage counter after consumer probe in driver_probe_device().
244 */
245 if (consumer->links.status == DL_DEV_PROBING)
246 pm_runtime_get_noresume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100247 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100248 get_device(supplier);
249 link->supplier = supplier;
250 INIT_LIST_HEAD(&link->s_node);
251 get_device(consumer);
252 link->consumer = consumer;
253 INIT_LIST_HEAD(&link->c_node);
254 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100255 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100256
Lukas Wunner64df1142016-12-04 13:10:04 +0100257 /* Determine the initial link state. */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100258 if (flags & DL_FLAG_STATELESS) {
259 link->status = DL_STATE_NONE;
260 } else {
261 switch (supplier->links.status) {
262 case DL_DEV_DRIVER_BOUND:
263 switch (consumer->links.status) {
264 case DL_DEV_PROBING:
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100265 /*
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200266 * Some callers expect the link creation during
267 * consumer driver probe to resume the supplier
268 * even without DL_FLAG_RPM_ACTIVE.
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100269 */
270 if (flags & DL_FLAG_PM_RUNTIME)
Rafael J. Wysocki47e5abf2018-06-14 10:01:52 +0200271 pm_runtime_resume(supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100272
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100273 link->status = DL_STATE_CONSUMER_PROBE;
274 break;
275 case DL_DEV_DRIVER_BOUND:
276 link->status = DL_STATE_ACTIVE;
277 break;
278 default:
279 link->status = DL_STATE_AVAILABLE;
280 break;
281 }
282 break;
283 case DL_DEV_UNBINDING:
284 link->status = DL_STATE_SUPPLIER_UNBIND;
285 break;
286 default:
287 link->status = DL_STATE_DORMANT;
288 break;
289 }
290 }
291
292 /*
293 * Move the consumer and all of the devices depending on it to the end
294 * of dpm_list and the devices_kset list.
295 *
296 * It is necessary to hold dpm_list locked throughout all that or else
297 * we may end up suspending with a wrong ordering of it.
298 */
299 device_reorder_to_tail(consumer, NULL);
300
301 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
302 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
303
304 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
305
306 out:
307 device_pm_unlock();
308 device_links_write_unlock();
309 return link;
310}
311EXPORT_SYMBOL_GPL(device_link_add);
312
313static void device_link_free(struct device_link *link)
314{
315 put_device(link->consumer);
316 put_device(link->supplier);
317 kfree(link);
318}
319
320#ifdef CONFIG_SRCU
321static void __device_link_free_srcu(struct rcu_head *rhead)
322{
323 device_link_free(container_of(rhead, struct device_link, rcu_head));
324}
325
Lukas Wunneread18c22018-02-10 19:27:12 +0100326static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100327{
Lukas Wunneread18c22018-02-10 19:27:12 +0100328 struct device_link *link = container_of(kref, struct device_link, kref);
329
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100330 dev_info(link->consumer, "Dropping the link to %s\n",
331 dev_name(link->supplier));
332
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100333 if (link->flags & DL_FLAG_PM_RUNTIME)
334 pm_runtime_drop_link(link->consumer);
335
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100336 list_del_rcu(&link->s_node);
337 list_del_rcu(&link->c_node);
338 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
339}
340#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100341static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100342{
Lukas Wunneread18c22018-02-10 19:27:12 +0100343 struct device_link *link = container_of(kref, struct device_link, kref);
344
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100345 dev_info(link->consumer, "Dropping the link to %s\n",
346 dev_name(link->supplier));
347
Lukas Wunner433986c2018-02-10 19:13:58 +0100348 if (link->flags & DL_FLAG_PM_RUNTIME)
349 pm_runtime_drop_link(link->consumer);
350
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100351 list_del(&link->s_node);
352 list_del(&link->c_node);
353 device_link_free(link);
354}
355#endif /* !CONFIG_SRCU */
356
357/**
358 * device_link_del - Delete a link between two devices.
359 * @link: Device link to delete.
360 *
361 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100362 * PM. If the link was added multiple times, it needs to be deleted as often.
363 * Care is required for hotplugged devices: Their links are purged on removal
364 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100365 */
366void device_link_del(struct device_link *link)
367{
368 device_links_write_lock();
369 device_pm_lock();
Lukas Wunneread18c22018-02-10 19:27:12 +0100370 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100371 device_pm_unlock();
372 device_links_write_unlock();
373}
374EXPORT_SYMBOL_GPL(device_link_del);
375
pascal pailletd8842212018-07-05 14:25:56 +0000376/**
377 * device_link_remove - remove a link between two devices.
378 * @consumer: Consumer end of the link.
379 * @supplier: Supplier end of the link.
380 *
381 * The caller must ensure proper synchronization of this function with runtime
382 * PM.
383 */
384void device_link_remove(void *consumer, struct device *supplier)
385{
386 struct device_link *link;
387
388 if (WARN_ON(consumer == supplier))
389 return;
390
391 device_links_write_lock();
392 device_pm_lock();
393
394 list_for_each_entry(link, &supplier->links.consumers, s_node) {
395 if (link->consumer == consumer) {
396 kref_put(&link->kref, __device_link_del);
397 break;
398 }
399 }
400
401 device_pm_unlock();
402 device_links_write_unlock();
403}
404EXPORT_SYMBOL_GPL(device_link_remove);
405
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100406static void device_links_missing_supplier(struct device *dev)
407{
408 struct device_link *link;
409
410 list_for_each_entry(link, &dev->links.suppliers, c_node)
411 if (link->status == DL_STATE_CONSUMER_PROBE)
412 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
413}
414
415/**
416 * device_links_check_suppliers - Check presence of supplier drivers.
417 * @dev: Consumer device.
418 *
419 * Check links from this device to any suppliers. Walk the list of the device's
420 * links to suppliers and see if all of them are available. If not, simply
421 * return -EPROBE_DEFER.
422 *
423 * We need to guarantee that the supplier will not go away after the check has
424 * been positive here. It only can go away in __device_release_driver() and
425 * that function checks the device's links to consumers. This means we need to
426 * mark the link as "consumer probe in progress" to make the supplier removal
427 * wait for us to complete (or bad things may happen).
428 *
429 * Links with the DL_FLAG_STATELESS flag set are ignored.
430 */
431int device_links_check_suppliers(struct device *dev)
432{
433 struct device_link *link;
434 int ret = 0;
435
436 device_links_write_lock();
437
438 list_for_each_entry(link, &dev->links.suppliers, c_node) {
439 if (link->flags & DL_FLAG_STATELESS)
440 continue;
441
442 if (link->status != DL_STATE_AVAILABLE) {
443 device_links_missing_supplier(dev);
444 ret = -EPROBE_DEFER;
445 break;
446 }
447 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
448 }
449 dev->links.status = DL_DEV_PROBING;
450
451 device_links_write_unlock();
452 return ret;
453}
454
455/**
456 * device_links_driver_bound - Update device links after probing its driver.
457 * @dev: Device to update the links for.
458 *
459 * The probe has been successful, so update links from this device to any
460 * consumers by changing their status to "available".
461 *
462 * Also change the status of @dev's links to suppliers to "active".
463 *
464 * Links with the DL_FLAG_STATELESS flag set are ignored.
465 */
466void device_links_driver_bound(struct device *dev)
467{
468 struct device_link *link;
469
470 device_links_write_lock();
471
472 list_for_each_entry(link, &dev->links.consumers, s_node) {
473 if (link->flags & DL_FLAG_STATELESS)
474 continue;
475
476 WARN_ON(link->status != DL_STATE_DORMANT);
477 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
478 }
479
480 list_for_each_entry(link, &dev->links.suppliers, c_node) {
481 if (link->flags & DL_FLAG_STATELESS)
482 continue;
483
484 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
485 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
486 }
487
488 dev->links.status = DL_DEV_DRIVER_BOUND;
489
490 device_links_write_unlock();
491}
492
493/**
494 * __device_links_no_driver - Update links of a device without a driver.
495 * @dev: Device without a drvier.
496 *
497 * Delete all non-persistent links from this device to any suppliers.
498 *
499 * Persistent links stay around, but their status is changed to "available",
500 * unless they already are in the "supplier unbind in progress" state in which
501 * case they need not be updated.
502 *
503 * Links with the DL_FLAG_STATELESS flag set are ignored.
504 */
505static void __device_links_no_driver(struct device *dev)
506{
507 struct device_link *link, *ln;
508
509 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
510 if (link->flags & DL_FLAG_STATELESS)
511 continue;
512
Vivek Gautame88728f2018-06-27 18:20:55 +0530513 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Lukas Wunneread18c22018-02-10 19:27:12 +0100514 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100515 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
516 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
517 }
518
519 dev->links.status = DL_DEV_NO_DRIVER;
520}
521
522void device_links_no_driver(struct device *dev)
523{
524 device_links_write_lock();
525 __device_links_no_driver(dev);
526 device_links_write_unlock();
527}
528
529/**
530 * device_links_driver_cleanup - Update links after driver removal.
531 * @dev: Device whose driver has just gone away.
532 *
533 * Update links to consumers for @dev by changing their status to "dormant" and
534 * invoke %__device_links_no_driver() to update links to suppliers for it as
535 * appropriate.
536 *
537 * Links with the DL_FLAG_STATELESS flag set are ignored.
538 */
539void device_links_driver_cleanup(struct device *dev)
540{
541 struct device_link *link;
542
543 device_links_write_lock();
544
545 list_for_each_entry(link, &dev->links.consumers, s_node) {
546 if (link->flags & DL_FLAG_STATELESS)
547 continue;
548
Vivek Gautame88728f2018-06-27 18:20:55 +0530549 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100550 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530551
552 /*
553 * autoremove the links between this @dev and its consumer
554 * devices that are not active, i.e. where the link state
555 * has moved to DL_STATE_SUPPLIER_UNBIND.
556 */
557 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
558 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
559 kref_put(&link->kref, __device_link_del);
560
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100561 WRITE_ONCE(link->status, DL_STATE_DORMANT);
562 }
563
564 __device_links_no_driver(dev);
565
566 device_links_write_unlock();
567}
568
569/**
570 * device_links_busy - Check if there are any busy links to consumers.
571 * @dev: Device to check.
572 *
573 * Check each consumer of the device and return 'true' if its link's status
574 * is one of "consumer probe" or "active" (meaning that the given consumer is
575 * probing right now or its driver is present). Otherwise, change the link
576 * state to "supplier unbind" to prevent the consumer from being probed
577 * successfully going forward.
578 *
579 * Return 'false' if there are no probing or active consumers.
580 *
581 * Links with the DL_FLAG_STATELESS flag set are ignored.
582 */
583bool device_links_busy(struct device *dev)
584{
585 struct device_link *link;
586 bool ret = false;
587
588 device_links_write_lock();
589
590 list_for_each_entry(link, &dev->links.consumers, s_node) {
591 if (link->flags & DL_FLAG_STATELESS)
592 continue;
593
594 if (link->status == DL_STATE_CONSUMER_PROBE
595 || link->status == DL_STATE_ACTIVE) {
596 ret = true;
597 break;
598 }
599 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
600 }
601
602 dev->links.status = DL_DEV_UNBINDING;
603
604 device_links_write_unlock();
605 return ret;
606}
607
608/**
609 * device_links_unbind_consumers - Force unbind consumers of the given device.
610 * @dev: Device to unbind the consumers of.
611 *
612 * Walk the list of links to consumers for @dev and if any of them is in the
613 * "consumer probe" state, wait for all device probes in progress to complete
614 * and start over.
615 *
616 * If that's not the case, change the status of the link to "supplier unbind"
617 * and check if the link was in the "active" state. If so, force the consumer
618 * driver to unbind and start over (the consumer will not re-probe as we have
619 * changed the state of the link already).
620 *
621 * Links with the DL_FLAG_STATELESS flag set are ignored.
622 */
623void device_links_unbind_consumers(struct device *dev)
624{
625 struct device_link *link;
626
627 start:
628 device_links_write_lock();
629
630 list_for_each_entry(link, &dev->links.consumers, s_node) {
631 enum device_link_state status;
632
633 if (link->flags & DL_FLAG_STATELESS)
634 continue;
635
636 status = link->status;
637 if (status == DL_STATE_CONSUMER_PROBE) {
638 device_links_write_unlock();
639
640 wait_for_device_probe();
641 goto start;
642 }
643 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
644 if (status == DL_STATE_ACTIVE) {
645 struct device *consumer = link->consumer;
646
647 get_device(consumer);
648
649 device_links_write_unlock();
650
651 device_release_driver_internal(consumer, NULL,
652 consumer->parent);
653 put_device(consumer);
654 goto start;
655 }
656 }
657
658 device_links_write_unlock();
659}
660
661/**
662 * device_links_purge - Delete existing links to other devices.
663 * @dev: Target device.
664 */
665static void device_links_purge(struct device *dev)
666{
667 struct device_link *link, *ln;
668
669 /*
670 * Delete all of the remaining links from this device to any other
671 * devices (either consumers or suppliers).
672 */
673 device_links_write_lock();
674
675 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
676 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100677 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100678 }
679
680 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
681 WARN_ON(link->status != DL_STATE_DORMANT &&
682 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100683 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100684 }
685
686 device_links_write_unlock();
687}
688
689/* Device links support end. */
690
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800691int (*platform_notify)(struct device *dev) = NULL;
692int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700693static struct kobject *dev_kobj;
694struct kobject *sysfs_dev_char_kobj;
695struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200697static DEFINE_MUTEX(device_hotplug_lock);
698
699void lock_device_hotplug(void)
700{
701 mutex_lock(&device_hotplug_lock);
702}
703
704void unlock_device_hotplug(void)
705{
706 mutex_unlock(&device_hotplug_lock);
707}
708
709int lock_device_hotplug_sysfs(void)
710{
711 if (mutex_trylock(&device_hotplug_lock))
712 return 0;
713
714 /* Avoid busy looping (5 ms of sleep should do). */
715 msleep(5);
716 return restart_syscall();
717}
718
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800719#ifdef CONFIG_BLOCK
720static inline int device_is_not_partition(struct device *dev)
721{
722 return !(dev->type == &part_type);
723}
724#else
725static inline int device_is_not_partition(struct device *dev)
726{
727 return 1;
728}
729#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Alan Stern3e956372006-06-16 17:10:48 -0400731/**
732 * dev_driver_string - Return a device's driver name, if at all possible
733 * @dev: struct device to get the name of
734 *
735 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800736 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400737 * it is attached to. If it is not attached to a bus either, an empty
738 * string will be returned.
739 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700740const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400741{
Alan Stern35899722009-12-04 11:06:57 -0500742 struct device_driver *drv;
743
744 /* dev->driver can change to NULL underneath us because of unbinding,
745 * so be careful about accessing it. dev->bus and dev->class should
746 * never change once they are set, so they don't need special care.
747 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700748 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500749 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100750 (dev->bus ? dev->bus->name :
751 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400752}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600753EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
756
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800757static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
758 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800760 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200761 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500762 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400765 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800766 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900767 printk("dev_attr_show: %pS returned bad count\n",
768 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return ret;
771}
772
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800773static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
774 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800776 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200777 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500778 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400781 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return ret;
783}
784
Emese Revfy52cf25d2010-01-19 02:58:23 +0100785static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .show = dev_attr_show,
787 .store = dev_attr_store,
788};
789
Kay Sieversca22e562011-12-14 14:29:38 -0800790#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
791
792ssize_t device_store_ulong(struct device *dev,
793 struct device_attribute *attr,
794 const char *buf, size_t size)
795{
796 struct dev_ext_attribute *ea = to_ext_attr(attr);
797 char *end;
798 unsigned long new = simple_strtoul(buf, &end, 0);
799 if (end == buf)
800 return -EINVAL;
801 *(unsigned long *)(ea->var) = new;
802 /* Always return full write size even if we didn't consume all */
803 return size;
804}
805EXPORT_SYMBOL_GPL(device_store_ulong);
806
807ssize_t device_show_ulong(struct device *dev,
808 struct device_attribute *attr,
809 char *buf)
810{
811 struct dev_ext_attribute *ea = to_ext_attr(attr);
812 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
813}
814EXPORT_SYMBOL_GPL(device_show_ulong);
815
816ssize_t device_store_int(struct device *dev,
817 struct device_attribute *attr,
818 const char *buf, size_t size)
819{
820 struct dev_ext_attribute *ea = to_ext_attr(attr);
821 char *end;
822 long new = simple_strtol(buf, &end, 0);
823 if (end == buf || new > INT_MAX || new < INT_MIN)
824 return -EINVAL;
825 *(int *)(ea->var) = new;
826 /* Always return full write size even if we didn't consume all */
827 return size;
828}
829EXPORT_SYMBOL_GPL(device_store_int);
830
831ssize_t device_show_int(struct device *dev,
832 struct device_attribute *attr,
833 char *buf)
834{
835 struct dev_ext_attribute *ea = to_ext_attr(attr);
836
837 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
838}
839EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Borislav Petkov91872392012-10-09 19:52:05 +0200841ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
842 const char *buf, size_t size)
843{
844 struct dev_ext_attribute *ea = to_ext_attr(attr);
845
846 if (strtobool(buf, ea->var) < 0)
847 return -EINVAL;
848
849 return size;
850}
851EXPORT_SYMBOL_GPL(device_store_bool);
852
853ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
854 char *buf)
855{
856 struct dev_ext_attribute *ea = to_ext_attr(attr);
857
858 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
859}
860EXPORT_SYMBOL_GPL(device_show_bool);
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400863 * device_release - free device structure.
864 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400866 * This is called once the reference count for the object
867 * reaches 0. We forward the call to the device's release
868 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800870static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200872 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800873 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Ming Leia525a3d2012-07-25 01:42:29 +0800875 /*
876 * Some platform devices are driven without driver attached
877 * and managed resources may have been acquired. Make sure
878 * all resources are released.
879 *
880 * Drivers still can add resources into device after device
881 * is deleted but alive, so release devres here to avoid
882 * possible memory leak.
883 */
884 devres_release_all(dev);
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (dev->release)
887 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200888 else if (dev->type && dev->type->release)
889 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700890 else if (dev->class && dev->class->dev_release)
891 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700892 else
893 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800894 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100895 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800896 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700899static const void *device_namespace(struct kobject *kobj)
900{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200901 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700902 const void *ns = NULL;
903
904 if (dev->class && dev->class->ns_type)
905 ns = dev->class->namespace(dev);
906
907 return ns;
908}
909
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000910static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
911{
912 struct device *dev = kobj_to_dev(kobj);
913
914 if (dev->class && dev->class->get_ownership)
915 dev->class->get_ownership(dev, uid, gid);
916}
917
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600918static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 .release = device_release,
920 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700921 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +0000922 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923};
924
925
Kay Sievers312c0042005-11-16 09:00:00 +0100926static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 struct kobj_type *ktype = get_ktype(kobj);
929
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600930 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200931 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (dev->bus)
933 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700934 if (dev->class)
935 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 return 0;
938}
939
Kay Sievers312c0042005-11-16 09:00:00 +0100940static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200942 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700944 if (dev->bus)
945 return dev->bus->name;
946 if (dev->class)
947 return dev->class->name;
948 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Kay Sievers7eff2e72007-08-14 15:15:12 +0200951static int dev_uevent(struct kset *kset, struct kobject *kobj,
952 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200954 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int retval = 0;
956
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200957 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700958 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200959 const char *tmp;
960 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400961 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700962 kuid_t uid = GLOBAL_ROOT_UID;
963 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200964
Kay Sievers7eff2e72007-08-14 15:15:12 +0200965 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
966 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700967 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200968 if (name) {
969 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200970 if (mode)
971 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700972 if (!uid_eq(uid, GLOBAL_ROOT_UID))
973 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
974 if (!gid_eq(gid, GLOBAL_ROOT_GID))
975 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700976 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200977 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700978 }
979
Kay Sievers414264f2007-03-12 21:08:57 +0100980 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200981 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100982
Kay Sievers239378f2006-10-07 21:54:55 +0200983 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200984 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200985
Grant Likely07d57a32012-02-01 11:22:22 -0700986 /* Add common DT information about the device */
987 of_device_uevent(dev, env);
988
Kay Sievers7eff2e72007-08-14 15:15:12 +0200989 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100990 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200991 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200992 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800993 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100994 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
Kay Sievers7eff2e72007-08-14 15:15:12 +0200997 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700998 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200999 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001000 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001001 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001002 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001003 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001004 }
1005
Stefan Weileef35c22010-08-06 21:11:15 +02001006 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02001007 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001008 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001009 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001010 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001011 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001012 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001013 }
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return retval;
1016}
1017
Emese Revfy9cd43612009-12-31 14:52:51 +01001018static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01001019 .filter = dev_uevent_filter,
1020 .name = dev_uevent_name,
1021 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022};
1023
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001024static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02001025 char *buf)
1026{
1027 struct kobject *top_kobj;
1028 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001029 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02001030 int i;
1031 size_t count = 0;
1032 int retval;
1033
1034 /* search the kset, the device belongs to */
1035 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001036 while (!top_kobj->kset && top_kobj->parent)
1037 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02001038 if (!top_kobj->kset)
1039 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001040
Kay Sievers16574dc2007-04-06 01:40:38 +02001041 kset = top_kobj->kset;
1042 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1043 goto out;
1044
1045 /* respect filter */
1046 if (kset->uevent_ops && kset->uevent_ops->filter)
1047 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1048 goto out;
1049
Kay Sievers7eff2e72007-08-14 15:15:12 +02001050 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1051 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001052 return -ENOMEM;
1053
Kay Sievers16574dc2007-04-06 01:40:38 +02001054 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001055 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001056 if (retval)
1057 goto out;
1058
1059 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001060 for (i = 0; i < env->envp_idx; i++)
1061 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001062out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001063 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001064 return count;
1065}
1066
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001067static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001068 const char *buf, size_t count)
1069{
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001070 if (kobject_synth_uevent(&dev->kobj, buf, count))
1071 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Kay Sievers60a96a52007-07-08 22:29:26 +02001072
Kay Sieversa7fd6702005-10-01 14:49:43 +02001073 return count;
1074}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001075static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001076
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001077static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001078 char *buf)
1079{
1080 bool val;
1081
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001082 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001083 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001084 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001085 return sprintf(buf, "%u\n", val);
1086}
1087
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001088static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001089 const char *buf, size_t count)
1090{
1091 bool val;
1092 int ret;
1093
1094 ret = strtobool(buf, &val);
1095 if (ret < 0)
1096 return ret;
1097
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001098 ret = lock_device_hotplug_sysfs();
1099 if (ret)
1100 return ret;
1101
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001102 ret = val ? device_online(dev) : device_offline(dev);
1103 unlock_device_hotplug();
1104 return ret < 0 ? ret : count;
1105}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001106static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001107
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001108int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001109{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001110 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001111}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001112EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001113
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001114void device_remove_groups(struct device *dev,
1115 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001116{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001117 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001118}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001119EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001120
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001121union device_attr_group_devres {
1122 const struct attribute_group *group;
1123 const struct attribute_group **groups;
1124};
1125
1126static int devm_attr_group_match(struct device *dev, void *res, void *data)
1127{
1128 return ((union device_attr_group_devres *)res)->group == data;
1129}
1130
1131static void devm_attr_group_remove(struct device *dev, void *res)
1132{
1133 union device_attr_group_devres *devres = res;
1134 const struct attribute_group *group = devres->group;
1135
1136 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1137 sysfs_remove_group(&dev->kobj, group);
1138}
1139
1140static void devm_attr_groups_remove(struct device *dev, void *res)
1141{
1142 union device_attr_group_devres *devres = res;
1143 const struct attribute_group **groups = devres->groups;
1144
1145 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1146 sysfs_remove_groups(&dev->kobj, groups);
1147}
1148
1149/**
1150 * devm_device_add_group - given a device, create a managed attribute group
1151 * @dev: The device to create the group for
1152 * @grp: The attribute group to create
1153 *
1154 * This function creates a group for the first time. It will explicitly
1155 * warn and error if any of the attribute files being created already exist.
1156 *
1157 * Returns 0 on success or error code on failure.
1158 */
1159int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1160{
1161 union device_attr_group_devres *devres;
1162 int error;
1163
1164 devres = devres_alloc(devm_attr_group_remove,
1165 sizeof(*devres), GFP_KERNEL);
1166 if (!devres)
1167 return -ENOMEM;
1168
1169 error = sysfs_create_group(&dev->kobj, grp);
1170 if (error) {
1171 devres_free(devres);
1172 return error;
1173 }
1174
1175 devres->group = grp;
1176 devres_add(dev, devres);
1177 return 0;
1178}
1179EXPORT_SYMBOL_GPL(devm_device_add_group);
1180
1181/**
1182 * devm_device_remove_group: remove a managed group from a device
1183 * @dev: device to remove the group from
1184 * @grp: group to remove
1185 *
1186 * This function removes a group of attributes from a device. The attributes
1187 * previously have to have been created for this group, otherwise it will fail.
1188 */
1189void devm_device_remove_group(struct device *dev,
1190 const struct attribute_group *grp)
1191{
1192 WARN_ON(devres_release(dev, devm_attr_group_remove,
1193 devm_attr_group_match,
1194 /* cast away const */ (void *)grp));
1195}
1196EXPORT_SYMBOL_GPL(devm_device_remove_group);
1197
1198/**
1199 * devm_device_add_groups - create a bunch of managed attribute groups
1200 * @dev: The device to create the group for
1201 * @groups: The attribute groups to create, NULL terminated
1202 *
1203 * This function creates a bunch of managed attribute groups. If an error
1204 * occurs when creating a group, all previously created groups will be
1205 * removed, unwinding everything back to the original state when this
1206 * function was called. It will explicitly warn and error if any of the
1207 * attribute files being created already exist.
1208 *
1209 * Returns 0 on success or error code from sysfs_create_group on failure.
1210 */
1211int devm_device_add_groups(struct device *dev,
1212 const struct attribute_group **groups)
1213{
1214 union device_attr_group_devres *devres;
1215 int error;
1216
1217 devres = devres_alloc(devm_attr_groups_remove,
1218 sizeof(*devres), GFP_KERNEL);
1219 if (!devres)
1220 return -ENOMEM;
1221
1222 error = sysfs_create_groups(&dev->kobj, groups);
1223 if (error) {
1224 devres_free(devres);
1225 return error;
1226 }
1227
1228 devres->groups = groups;
1229 devres_add(dev, devres);
1230 return 0;
1231}
1232EXPORT_SYMBOL_GPL(devm_device_add_groups);
1233
1234/**
1235 * devm_device_remove_groups - remove a list of managed groups
1236 *
1237 * @dev: The device for the groups to be removed from
1238 * @groups: NULL terminated list of groups to be removed
1239 *
1240 * If groups is not NULL, remove the specified groups from the device.
1241 */
1242void devm_device_remove_groups(struct device *dev,
1243 const struct attribute_group **groups)
1244{
1245 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1246 devm_attr_group_match,
1247 /* cast away const */ (void *)groups));
1248}
1249EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001251static int device_add_attrs(struct device *dev)
1252{
1253 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001254 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001255 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001256
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001257 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001258 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001259 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001260 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001261 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001262
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001263 if (type) {
1264 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001265 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001266 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001267 }
1268
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001269 error = device_add_groups(dev, dev->groups);
1270 if (error)
1271 goto err_remove_type_groups;
1272
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001273 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001274 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001275 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001276 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001277 }
1278
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001279 return 0;
1280
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001281 err_remove_dev_groups:
1282 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001283 err_remove_type_groups:
1284 if (type)
1285 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001286 err_remove_class_groups:
1287 if (class)
1288 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001289
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001290 return error;
1291}
1292
1293static void device_remove_attrs(struct device *dev)
1294{
1295 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001296 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001297
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001298 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001299 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001300
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001301 if (type)
1302 device_remove_groups(dev, type->groups);
1303
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001304 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001305 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001306}
1307
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001308static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001309 char *buf)
1310{
1311 return print_dev_t(buf, dev->devt);
1312}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001313static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001314
Kay Sieversca22e562011-12-14 14:29:38 -08001315/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001316struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001319 * devices_kset_move_before - Move device in the devices_kset's list.
1320 * @deva: Device to move.
1321 * @devb: Device @deva should come before.
1322 */
1323static void devices_kset_move_before(struct device *deva, struct device *devb)
1324{
1325 if (!devices_kset)
1326 return;
1327 pr_debug("devices_kset: Moving %s before %s\n",
1328 dev_name(deva), dev_name(devb));
1329 spin_lock(&devices_kset->list_lock);
1330 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1331 spin_unlock(&devices_kset->list_lock);
1332}
1333
1334/**
1335 * devices_kset_move_after - Move device in the devices_kset's list.
1336 * @deva: Device to move
1337 * @devb: Device @deva should come after.
1338 */
1339static void devices_kset_move_after(struct device *deva, struct device *devb)
1340{
1341 if (!devices_kset)
1342 return;
1343 pr_debug("devices_kset: Moving %s after %s\n",
1344 dev_name(deva), dev_name(devb));
1345 spin_lock(&devices_kset->list_lock);
1346 list_move(&deva->kobj.entry, &devb->kobj.entry);
1347 spin_unlock(&devices_kset->list_lock);
1348}
1349
1350/**
1351 * devices_kset_move_last - move the device to the end of devices_kset's list.
1352 * @dev: device to move
1353 */
1354void devices_kset_move_last(struct device *dev)
1355{
1356 if (!devices_kset)
1357 return;
1358 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1359 spin_lock(&devices_kset->list_lock);
1360 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1361 spin_unlock(&devices_kset->list_lock);
1362}
1363
1364/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001365 * device_create_file - create sysfs attribute file for device.
1366 * @dev: device.
1367 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001369int device_create_file(struct device *dev,
1370 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001373
1374 if (dev) {
1375 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001376 "Attribute %s: write permission without 'store'\n",
1377 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001378 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001379 "Attribute %s: read permission without 'show'\n",
1380 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001382 }
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return error;
1385}
David Graham White86df2682013-07-21 20:41:14 -04001386EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001389 * device_remove_file - remove sysfs attribute file.
1390 * @dev: device.
1391 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001393void device_remove_file(struct device *dev,
1394 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001396 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
David Graham White86df2682013-07-21 20:41:14 -04001399EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001401/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001402 * device_remove_file_self - remove sysfs attribute file from its own method.
1403 * @dev: device.
1404 * @attr: device attribute descriptor.
1405 *
1406 * See kernfs_remove_self() for details.
1407 */
1408bool device_remove_file_self(struct device *dev,
1409 const struct device_attribute *attr)
1410{
1411 if (dev)
1412 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1413 else
1414 return false;
1415}
1416EXPORT_SYMBOL_GPL(device_remove_file_self);
1417
1418/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001419 * device_create_bin_file - create sysfs binary attribute file for device.
1420 * @dev: device.
1421 * @attr: device binary attribute descriptor.
1422 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001423int device_create_bin_file(struct device *dev,
1424 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001425{
1426 int error = -EINVAL;
1427 if (dev)
1428 error = sysfs_create_bin_file(&dev->kobj, attr);
1429 return error;
1430}
1431EXPORT_SYMBOL_GPL(device_create_bin_file);
1432
1433/**
1434 * device_remove_bin_file - remove sysfs binary attribute file
1435 * @dev: device.
1436 * @attr: device binary attribute descriptor.
1437 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001438void device_remove_bin_file(struct device *dev,
1439 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001440{
1441 if (dev)
1442 sysfs_remove_bin_file(&dev->kobj, attr);
1443}
1444EXPORT_SYMBOL_GPL(device_remove_bin_file);
1445
James Bottomley34bb61f2005-09-06 16:56:51 -07001446static void klist_children_get(struct klist_node *n)
1447{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001448 struct device_private *p = to_device_private_parent(n);
1449 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001450
1451 get_device(dev);
1452}
1453
1454static void klist_children_put(struct klist_node *n)
1455{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001456 struct device_private *p = to_device_private_parent(n);
1457 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001458
1459 put_device(dev);
1460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001463 * device_initialize - init device structure.
1464 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 *
Cornelia Huck57394112008-09-03 18:26:40 +02001466 * This prepares the device for use by other layers by initializing
1467 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001468 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001469 * that function, though it can also be called separately, so one
1470 * may use @dev's fields. In particular, get_device()/put_device()
1471 * may be used for reference counting of @dev after calling this
1472 * function.
1473 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001474 * All fields in @dev must be initialized by the caller to 0, except
1475 * for those explicitly set to some other value. The simplest
1476 * approach is to use kzalloc() to allocate the structure containing
1477 * @dev.
1478 *
Cornelia Huck57394112008-09-03 18:26:40 +02001479 * NOTE: Use put_device() to give up your reference instead of freeing
1480 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482void device_initialize(struct device *dev)
1483{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001484 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001485 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001487 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001488 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001489 spin_lock_init(&dev->devres_lock);
1490 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001491 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001492 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001493#ifdef CONFIG_GENERIC_MSI_IRQ
1494 INIT_LIST_HEAD(&dev->msi_list);
1495#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001496 INIT_LIST_HEAD(&dev->links.consumers);
1497 INIT_LIST_HEAD(&dev->links.suppliers);
1498 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
David Graham White86df2682013-07-21 20:41:14 -04001500EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Tejun Heod73ce002013-03-12 11:30:05 -07001502struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001503{
Kay Sievers86406242007-03-14 03:25:56 +01001504 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001505
Kay Sievers86406242007-03-14 03:25:56 +01001506 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001507 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001508 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001509
Kay Sievers86406242007-03-14 03:25:56 +01001510 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001511}
1512
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001513struct class_dir {
1514 struct kobject kobj;
1515 struct class *class;
1516};
1517
1518#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1519
1520static void class_dir_release(struct kobject *kobj)
1521{
1522 struct class_dir *dir = to_class_dir(kobj);
1523 kfree(dir);
1524}
1525
1526static const
1527struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1528{
1529 struct class_dir *dir = to_class_dir(kobj);
1530 return dir->class->ns_type;
1531}
1532
1533static struct kobj_type class_dir_ktype = {
1534 .release = class_dir_release,
1535 .sysfs_ops = &kobj_sysfs_ops,
1536 .child_ns_type = class_dir_child_ns_type
1537};
1538
1539static struct kobject *
1540class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1541{
1542 struct class_dir *dir;
1543 int retval;
1544
1545 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1546 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001547 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001548
1549 dir->class = class;
1550 kobject_init(&dir->kobj, &class_dir_ktype);
1551
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001552 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001553
1554 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1555 if (retval < 0) {
1556 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001557 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001558 }
1559 return &dir->kobj;
1560}
1561
Yijing Wange4a60d12014-11-07 12:05:49 +08001562static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001563
Kay Sieversda231fd2007-11-21 17:29:15 +01001564static struct kobject *get_device_parent(struct device *dev,
1565 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001566{
Kay Sievers86406242007-03-14 03:25:56 +01001567 if (dev->class) {
1568 struct kobject *kobj = NULL;
1569 struct kobject *parent_kobj;
1570 struct kobject *k;
1571
Randy Dunlapead454f2010-09-24 14:36:49 -07001572#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001573 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001574 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001575 if (parent && parent->class == &block_class)
1576 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001577 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001578 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001579#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001580
Kay Sievers86406242007-03-14 03:25:56 +01001581 /*
1582 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001583 * Class-devices with a non class-device as parent, live
1584 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001585 */
1586 if (parent == NULL)
1587 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001588 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001589 return &parent->kobj;
1590 else
1591 parent_kobj = &parent->kobj;
1592
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001593 mutex_lock(&gdp_mutex);
1594
Kay Sievers86406242007-03-14 03:25:56 +01001595 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001596 spin_lock(&dev->class->p->glue_dirs.list_lock);
1597 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001598 if (k->parent == parent_kobj) {
1599 kobj = kobject_get(k);
1600 break;
1601 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001602 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001603 if (kobj) {
1604 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001605 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001606 }
Kay Sievers86406242007-03-14 03:25:56 +01001607
1608 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001609 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001610 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001611 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001612 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001613 }
1614
Kay Sieversca22e562011-12-14 14:29:38 -08001615 /* subsystems can specify a default root directory for their devices */
1616 if (!parent && dev->bus && dev->bus->dev_root)
1617 return &dev->bus->dev_root->kobj;
1618
Kay Sievers86406242007-03-14 03:25:56 +01001619 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001620 return &parent->kobj;
1621 return NULL;
1622}
Kay Sieversda231fd2007-11-21 17:29:15 +01001623
Ming Leicebf8fd2016-07-10 19:27:36 +08001624static inline bool live_in_glue_dir(struct kobject *kobj,
1625 struct device *dev)
1626{
1627 if (!kobj || !dev->class ||
1628 kobj->kset != &dev->class->p->glue_dirs)
1629 return false;
1630 return true;
1631}
1632
1633static inline struct kobject *get_glue_dir(struct device *dev)
1634{
1635 return dev->kobj.parent;
1636}
1637
1638/*
1639 * make sure cleaning up dir as the last step, we need to make
1640 * sure .release handler of kobject is run with holding the
1641 * global lock
1642 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001643static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001644{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001645 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001646 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001647 return;
1648
Yijing Wange4a60d12014-11-07 12:05:49 +08001649 mutex_lock(&gdp_mutex);
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10001650 if (!kobject_has_children(glue_dir))
1651 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001652 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001653 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001654}
Cornelia Huck63b69712008-01-21 16:09:44 +01001655
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001656static int device_add_class_symlinks(struct device *dev)
1657{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001658 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001659 int error;
1660
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001661 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001662 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001663 if (error)
1664 dev_warn(dev, "Error %d creating of_node link\n",error);
1665 /* An error here doesn't warrant bringing down the device */
1666 }
1667
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001668 if (!dev->class)
1669 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001670
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001671 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001672 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001673 "subsystem");
1674 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001675 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001676
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001677 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001678 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1679 "device");
1680 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001681 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001682 }
Kay Sievers39aba962010-09-04 22:33:14 -07001683
Randy Dunlapead454f2010-09-24 14:36:49 -07001684#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001685 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001686 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001687 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001688#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001689
1690 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001691 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001692 &dev->kobj, dev_name(dev));
1693 if (error)
1694 goto out_device;
1695
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001696 return 0;
1697
Kay Sievers39aba962010-09-04 22:33:14 -07001698out_device:
1699 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001700
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001701out_subsys:
1702 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001703out_devnode:
1704 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001705 return error;
1706}
1707
1708static void device_remove_class_symlinks(struct device *dev)
1709{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001710 if (dev_of_node(dev))
1711 sysfs_remove_link(&dev->kobj, "of_node");
1712
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001713 if (!dev->class)
1714 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001715
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001716 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001717 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001718 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001719#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001720 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001721 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001722#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001723 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001727 * dev_set_name - set a device name
1728 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001729 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001730 */
1731int dev_set_name(struct device *dev, const char *fmt, ...)
1732{
1733 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001734 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001735
1736 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001737 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001738 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001739 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001740}
1741EXPORT_SYMBOL_GPL(dev_set_name);
1742
1743/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001744 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1745 * @dev: device
1746 *
1747 * By default we select char/ for new entries. Setting class->dev_obj
1748 * to NULL prevents an entry from being created. class->dev_kobj must
1749 * be set (or cleared) before any devices are registered to the class
1750 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001751 * device_remove_sys_dev_entry() will disagree about the presence of
1752 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001753 */
1754static struct kobject *device_to_dev_kobj(struct device *dev)
1755{
1756 struct kobject *kobj;
1757
1758 if (dev->class)
1759 kobj = dev->class->dev_kobj;
1760 else
1761 kobj = sysfs_dev_char_kobj;
1762
1763 return kobj;
1764}
1765
1766static int device_create_sys_dev_entry(struct device *dev)
1767{
1768 struct kobject *kobj = device_to_dev_kobj(dev);
1769 int error = 0;
1770 char devt_str[15];
1771
1772 if (kobj) {
1773 format_dev_t(devt_str, dev->devt);
1774 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1775 }
1776
1777 return error;
1778}
1779
1780static void device_remove_sys_dev_entry(struct device *dev)
1781{
1782 struct kobject *kobj = device_to_dev_kobj(dev);
1783 char devt_str[15];
1784
1785 if (kobj) {
1786 format_dev_t(devt_str, dev->devt);
1787 sysfs_remove_link(kobj, devt_str);
1788 }
1789}
1790
Shaokun Zhang46d3a032018-07-15 18:08:56 +08001791static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001792{
1793 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1794 if (!dev->p)
1795 return -ENOMEM;
1796 dev->p->device = dev;
1797 klist_init(&dev->p->klist_children, klist_children_get,
1798 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001799 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001800 return 0;
1801}
1802
Dan Williamse105b8b2008-04-21 10:51:07 -07001803/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001804 * device_add - add device to device hierarchy.
1805 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001807 * This is part 2 of device_register(), though may be called
1808 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 *
Cornelia Huck57394112008-09-03 18:26:40 +02001810 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001811 * to the global and sibling lists for the device, then
1812 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001813 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001814 * Do not call this routine or device_register() more than once for
1815 * any device structure. The driver model core is not designed to work
1816 * with devices that get unregistered and then spring back to life.
1817 * (Among other things, it's very hard to guarantee that all references
1818 * to the previous incarnation of @dev have been dropped.) Allocate
1819 * and register a fresh new struct device instead.
1820 *
Cornelia Huck57394112008-09-03 18:26:40 +02001821 * NOTE: _Never_ directly free @dev after calling this function, even
1822 * if it returned an error! Always use put_device() to give up your
1823 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
1825int device_add(struct device *dev)
1826{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301827 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001828 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001829 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001830 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001831 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001834 if (!dev)
1835 goto done;
1836
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001837 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001838 error = device_private_init(dev);
1839 if (error)
1840 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001841 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001842
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001843 /*
1844 * for statically allocated devices, which should all be converted
1845 * some day, we need to initialize the name. We prevent reading back
1846 * the name, and force the use of dev_name()
1847 */
1848 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001849 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001850 dev->init_name = NULL;
1851 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001852
Kay Sieversca22e562011-12-14 14:29:38 -08001853 /* subsystems can specify simple device enumeration */
1854 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1855 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1856
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001857 if (!dev_name(dev)) {
1858 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001859 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001862 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001865 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001866 if (IS_ERR(kobj)) {
1867 error = PTR_ERR(kobj);
1868 goto parent_error;
1869 }
Kay Sieversca22e562011-12-14 14:29:38 -08001870 if (kobj)
1871 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Yinghai Lu0d358f22008-02-19 03:20:41 -08001873 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001874 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001875 set_dev_node(dev, dev_to_node(parent));
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001878 /* we require the name to be set before, and pass NULL */
1879 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001880 if (error) {
1881 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001883 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001884
Brian Walsh37022642006-08-14 22:43:19 -07001885 /* notify platform of device entry */
1886 if (platform_notify)
1887 platform_notify(dev);
1888
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001889 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001890 if (error)
1891 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001892
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001893 error = device_add_class_symlinks(dev);
1894 if (error)
1895 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001896 error = device_add_attrs(dev);
1897 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001898 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001899 error = bus_add_device(dev);
1900 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001902 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001903 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001904 goto DPMError;
1905 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001906
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001907 if (MAJOR(dev->devt)) {
1908 error = device_create_file(dev, &dev_attr_dev);
1909 if (error)
1910 goto DevAttrError;
1911
1912 error = device_create_sys_dev_entry(dev);
1913 if (error)
1914 goto SysEntryError;
1915
1916 devtmpfs_create_node(dev);
1917 }
1918
Alan Sternec0676ee2008-12-05 14:10:31 -05001919 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001920 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001921 */
1922 if (dev->bus)
1923 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1924 BUS_NOTIFY_ADD_DEVICE, dev);
1925
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001926 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001927 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001929 klist_add_tail(&dev->p->knode_parent,
1930 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001932 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001933 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001934 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001935 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001936 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001937
1938 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001939 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001940 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001941 if (class_intf->add_dev)
1942 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001943 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001944 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001945done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 put_device(dev);
1947 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001948 SysEntryError:
1949 if (MAJOR(dev->devt))
1950 device_remove_file(dev, &dev_attr_dev);
1951 DevAttrError:
1952 device_pm_remove(dev);
1953 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001954 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001955 bus_remove_device(dev);
1956 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001957 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001958 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001959 device_remove_class_symlinks(dev);
1960 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001961 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001962 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001963 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001964 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 kobject_del(&dev->kobj);
1966 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001967 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001968parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01001969 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001970name_error:
1971 kfree(dev->p);
1972 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001973 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
David Graham White86df2682013-07-21 20:41:14 -04001975EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001978 * device_register - register a device with the system.
1979 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001981 * This happens in two clean steps - initialize the device
1982 * and add it to the system. The two steps can be called
1983 * separately, but this is the easiest and most common.
1984 * I.e. you should only call the two helpers separately if
1985 * have a clearly defined need to use and refcount the device
1986 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001987 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001988 * For more information, see the kerneldoc for device_initialize()
1989 * and device_add().
1990 *
Cornelia Huck57394112008-09-03 18:26:40 +02001991 * NOTE: _Never_ directly free @dev after calling this function, even
1992 * if it returned an error! Always use put_device() to give up the
1993 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995int device_register(struct device *dev)
1996{
1997 device_initialize(dev);
1998 return device_add(dev);
1999}
David Graham White86df2682013-07-21 20:41:14 -04002000EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002003 * get_device - increment reference count for device.
2004 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002006 * This simply forwards the call to kobject_get(), though
2007 * we do take care to provide for the case that we get a NULL
2008 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002010struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002012 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
David Graham White86df2682013-07-21 20:41:14 -04002014EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002017 * put_device - decrement reference count.
2018 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002020void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02002022 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (dev)
2024 kobject_put(&dev->kobj);
2025}
David Graham White86df2682013-07-21 20:41:14 -04002026EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002029 * device_del - delete device from system.
2030 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002032 * This is the first part of the device unregistration
2033 * sequence. This removes the device from the lists we control
2034 * from here, has it removed from the other driver model
2035 * subsystems it was added to in device_add(), and removes it
2036 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002038 * NOTE: this should be called manually _iff_ device_add() was
2039 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002041void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002043 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002044 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002045 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Alan Sternec0676ee2008-12-05 14:10:31 -05002047 /* Notify clients of device removal. This call must come
2048 * before dpm_sysfs_remove().
2049 */
2050 if (dev->bus)
2051 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2052 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002053
Alan Stern3b98aea2008-08-07 13:06:12 -04002054 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002056 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002057 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002058 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002059 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002060 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002061 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002062 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002063 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002064
Kay Sieversca22e562011-12-14 14:29:38 -08002065 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002066 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002067 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002068 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002069 if (class_intf->remove_dev)
2070 class_intf->remove_dev(dev, class_intf);
2071 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02002072 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002073 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002074 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002075 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002076 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002077 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002078 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002079 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02002080 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002081 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 /* Notify the platform of the removal, in case they
2084 * need to do anything...
2085 */
2086 if (platform_notify_remove)
2087 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02002088 if (dev->bus)
2089 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2090 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002091 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002092 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002094 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002095 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
David Graham White86df2682013-07-21 20:41:14 -04002097EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002100 * device_unregister - unregister device from system.
2101 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002103 * We do this in two parts, like we do device_register(). First,
2104 * we remove it from all the subsystems with device_del(), then
2105 * we decrement the reference count via put_device(). If that
2106 * is the final reference count, the device will be cleaned up
2107 * via device_release() above. Otherwise, the structure will
2108 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002110void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002112 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 device_del(dev);
2114 put_device(dev);
2115}
David Graham White86df2682013-07-21 20:41:14 -04002116EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002118static struct device *prev_device(struct klist_iter *i)
2119{
2120 struct klist_node *n = klist_prev(i);
2121 struct device *dev = NULL;
2122 struct device_private *p;
2123
2124 if (n) {
2125 p = to_device_private_parent(n);
2126 dev = p->device;
2127 }
2128 return dev;
2129}
2130
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002131static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002132{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002133 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002134 struct device *dev = NULL;
2135 struct device_private *p;
2136
2137 if (n) {
2138 p = to_device_private_parent(n);
2139 dev = p->device;
2140 }
2141 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002142}
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002145 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002146 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002147 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002148 * @uid: returned file owner
2149 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002150 * @tmp: possibly allocated string
2151 *
2152 * Return the relative path of a possible device node.
2153 * Non-default names may need to allocate a memory to compose
2154 * a name. This memory is returned in tmp and needs to be
2155 * freed by the caller.
2156 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002157const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002158 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002159 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002160{
2161 char *s;
2162
2163 *tmp = NULL;
2164
2165 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002166 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002167 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002168 if (*tmp)
2169 return *tmp;
2170
2171 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002172 if (dev->class && dev->class->devnode)
2173 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002174 if (*tmp)
2175 return *tmp;
2176
2177 /* return name without allocation, tmp == NULL */
2178 if (strchr(dev_name(dev), '!') == NULL)
2179 return dev_name(dev);
2180
2181 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002182 s = kstrdup(dev_name(dev), GFP_KERNEL);
2183 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002184 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002185 strreplace(s, '!', '/');
2186 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002187}
2188
2189/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002190 * device_for_each_child - device child iterator.
2191 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002192 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002193 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002195 * Iterate over @parent's child devices, and call @fn for each,
2196 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002198 * We check the return of @fn each time. If it returns anything
2199 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002201int device_for_each_child(struct device *parent, void *data,
2202 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002204 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002205 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 int error = 0;
2207
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002208 if (!parent->p)
2209 return 0;
2210
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002211 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002212 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002213 error = fn(child, data);
2214 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return error;
2216}
David Graham White86df2682013-07-21 20:41:14 -04002217EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Cornelia Huck5ab69982006-11-16 15:42:07 +01002219/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002220 * device_for_each_child_reverse - device child iterator in reversed order.
2221 * @parent: parent struct device.
2222 * @fn: function to be called for each device.
2223 * @data: data for the callback.
2224 *
2225 * Iterate over @parent's child devices, and call @fn for each,
2226 * passing it @data.
2227 *
2228 * We check the return of @fn each time. If it returns anything
2229 * other than 0, we break out and return that value.
2230 */
2231int device_for_each_child_reverse(struct device *parent, void *data,
2232 int (*fn)(struct device *dev, void *data))
2233{
2234 struct klist_iter i;
2235 struct device *child;
2236 int error = 0;
2237
2238 if (!parent->p)
2239 return 0;
2240
2241 klist_iter_init(&parent->p->klist_children, &i);
2242 while ((child = prev_device(&i)) && !error)
2243 error = fn(child, data);
2244 klist_iter_exit(&i);
2245 return error;
2246}
2247EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2248
2249/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002250 * device_find_child - device iterator for locating a particular device.
2251 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002252 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002253 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002254 *
2255 * This is similar to the device_for_each_child() function above, but it
2256 * returns a reference to a device that is 'found' for later use, as
2257 * determined by the @match callback.
2258 *
2259 * The callback should return 0 if the device doesn't match and non-zero
2260 * if it does. If the callback returns non-zero and a reference to the
2261 * current device can be obtained, this function will return to the caller
2262 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002263 *
2264 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002265 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002266struct device *device_find_child(struct device *parent, void *data,
2267 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002268{
2269 struct klist_iter i;
2270 struct device *child;
2271
2272 if (!parent)
2273 return NULL;
2274
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002275 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002276 while ((child = next_device(&i)))
2277 if (match(child, data) && get_device(child))
2278 break;
2279 klist_iter_exit(&i);
2280 return child;
2281}
David Graham White86df2682013-07-21 20:41:14 -04002282EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284int __init devices_init(void)
2285{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002286 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2287 if (!devices_kset)
2288 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002289 dev_kobj = kobject_create_and_add("dev", NULL);
2290 if (!dev_kobj)
2291 goto dev_kobj_err;
2292 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2293 if (!sysfs_dev_block_kobj)
2294 goto block_kobj_err;
2295 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2296 if (!sysfs_dev_char_kobj)
2297 goto char_kobj_err;
2298
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002299 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002300
2301 char_kobj_err:
2302 kobject_put(sysfs_dev_block_kobj);
2303 block_kobj_err:
2304 kobject_put(dev_kobj);
2305 dev_kobj_err:
2306 kset_unregister(devices_kset);
2307 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002310static int device_check_offline(struct device *dev, void *not_used)
2311{
2312 int ret;
2313
2314 ret = device_for_each_child(dev, NULL, device_check_offline);
2315 if (ret)
2316 return ret;
2317
2318 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2319}
2320
2321/**
2322 * device_offline - Prepare the device for hot-removal.
2323 * @dev: Device to be put offline.
2324 *
2325 * Execute the device bus type's .offline() callback, if present, to prepare
2326 * the device for a subsequent hot-removal. If that succeeds, the device must
2327 * not be used until either it is removed or its bus type's .online() callback
2328 * is executed.
2329 *
2330 * Call under device_hotplug_lock.
2331 */
2332int device_offline(struct device *dev)
2333{
2334 int ret;
2335
2336 if (dev->offline_disabled)
2337 return -EPERM;
2338
2339 ret = device_for_each_child(dev, NULL, device_check_offline);
2340 if (ret)
2341 return ret;
2342
2343 device_lock(dev);
2344 if (device_supports_offline(dev)) {
2345 if (dev->offline) {
2346 ret = 1;
2347 } else {
2348 ret = dev->bus->offline(dev);
2349 if (!ret) {
2350 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2351 dev->offline = true;
2352 }
2353 }
2354 }
2355 device_unlock(dev);
2356
2357 return ret;
2358}
2359
2360/**
2361 * device_online - Put the device back online after successful device_offline().
2362 * @dev: Device to be put back online.
2363 *
2364 * If device_offline() has been successfully executed for @dev, but the device
2365 * has not been removed subsequently, execute its bus type's .online() callback
2366 * to indicate that the device can be used again.
2367 *
2368 * Call under device_hotplug_lock.
2369 */
2370int device_online(struct device *dev)
2371{
2372 int ret = 0;
2373
2374 device_lock(dev);
2375 if (device_supports_offline(dev)) {
2376 if (dev->offline) {
2377 ret = dev->bus->online(dev);
2378 if (!ret) {
2379 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2380 dev->offline = false;
2381 }
2382 } else {
2383 ret = 1;
2384 }
2385 }
2386 device_unlock(dev);
2387
2388 return ret;
2389}
2390
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002391struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002392 struct device dev;
2393 struct module *owner;
2394};
2395
Josh Triplett93058422012-11-18 21:27:55 -08002396static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002397{
2398 return container_of(d, struct root_device, dev);
2399}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002400
2401static void root_device_release(struct device *dev)
2402{
2403 kfree(to_root_device(dev));
2404}
2405
2406/**
2407 * __root_device_register - allocate and register a root device
2408 * @name: root device name
2409 * @owner: owner module of the root device, usually THIS_MODULE
2410 *
2411 * This function allocates a root device and registers it
2412 * using device_register(). In order to free the returned
2413 * device, use root_device_unregister().
2414 *
2415 * Root devices are dummy devices which allow other devices
2416 * to be grouped under /sys/devices. Use this function to
2417 * allocate a root device and then use it as the parent of
2418 * any device which should appear under /sys/devices/{name}
2419 *
2420 * The /sys/devices/{name} directory will also contain a
2421 * 'module' symlink which points to the @owner directory
2422 * in sysfs.
2423 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002424 * Returns &struct device pointer on success, or ERR_PTR() on error.
2425 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002426 * Note: You probably want to use root_device_register().
2427 */
2428struct device *__root_device_register(const char *name, struct module *owner)
2429{
2430 struct root_device *root;
2431 int err = -ENOMEM;
2432
2433 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2434 if (!root)
2435 return ERR_PTR(err);
2436
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002437 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002438 if (err) {
2439 kfree(root);
2440 return ERR_PTR(err);
2441 }
2442
2443 root->dev.release = root_device_release;
2444
2445 err = device_register(&root->dev);
2446 if (err) {
2447 put_device(&root->dev);
2448 return ERR_PTR(err);
2449 }
2450
Christoph Egger1d9e8822010-05-17 16:57:58 +02002451#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002452 if (owner) {
2453 struct module_kobject *mk = &owner->mkobj;
2454
2455 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2456 if (err) {
2457 device_unregister(&root->dev);
2458 return ERR_PTR(err);
2459 }
2460 root->owner = owner;
2461 }
2462#endif
2463
2464 return &root->dev;
2465}
2466EXPORT_SYMBOL_GPL(__root_device_register);
2467
2468/**
2469 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002470 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002471 *
2472 * This function unregisters and cleans up a device that was created by
2473 * root_device_register().
2474 */
2475void root_device_unregister(struct device *dev)
2476{
2477 struct root_device *root = to_root_device(dev);
2478
2479 if (root->owner)
2480 sysfs_remove_link(&root->dev.kobj, "module");
2481
2482 device_unregister(dev);
2483}
2484EXPORT_SYMBOL_GPL(root_device_unregister);
2485
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002486
2487static void device_create_release(struct device *dev)
2488{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002489 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002490 kfree(dev);
2491}
2492
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002493static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002494device_create_groups_vargs(struct class *class, struct device *parent,
2495 dev_t devt, void *drvdata,
2496 const struct attribute_group **groups,
2497 const char *fmt, va_list args)
2498{
2499 struct device *dev = NULL;
2500 int retval = -ENODEV;
2501
2502 if (class == NULL || IS_ERR(class))
2503 goto error;
2504
2505 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2506 if (!dev) {
2507 retval = -ENOMEM;
2508 goto error;
2509 }
2510
David Herrmannbbc780f2013-11-21 20:15:48 +01002511 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002512 dev->devt = devt;
2513 dev->class = class;
2514 dev->parent = parent;
2515 dev->groups = groups;
2516 dev->release = device_create_release;
2517 dev_set_drvdata(dev, drvdata);
2518
2519 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2520 if (retval)
2521 goto error;
2522
David Herrmannbbc780f2013-11-21 20:15:48 +01002523 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002524 if (retval)
2525 goto error;
2526
2527 return dev;
2528
2529error:
2530 put_device(dev);
2531 return ERR_PTR(retval);
2532}
2533
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002534/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002535 * device_create_vargs - creates a device and registers it with sysfs
2536 * @class: pointer to the struct class that this device should be registered to
2537 * @parent: pointer to the parent struct device of this new device, if any
2538 * @devt: the dev_t for the char device to be added
2539 * @drvdata: the data to be added to the device for callbacks
2540 * @fmt: string for the device's name
2541 * @args: va_list for the device's name
2542 *
2543 * This function can be used by char device classes. A struct device
2544 * will be created in sysfs, registered to the specified class.
2545 *
2546 * A "dev" file will be created, showing the dev_t for the device, if
2547 * the dev_t is not 0,0.
2548 * If a pointer to a parent struct device is passed in, the newly created
2549 * struct device will be a child of that device in sysfs.
2550 * The pointer to the struct device will be returned from the call.
2551 * Any further sysfs files that might be required can be created using this
2552 * pointer.
2553 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002554 * Returns &struct device pointer on success, or ERR_PTR() on error.
2555 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002556 * Note: the struct class passed to this function must have previously
2557 * been created with a call to class_create().
2558 */
2559struct device *device_create_vargs(struct class *class, struct device *parent,
2560 dev_t devt, void *drvdata, const char *fmt,
2561 va_list args)
2562{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002563 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2564 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002565}
2566EXPORT_SYMBOL_GPL(device_create_vargs);
2567
2568/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002569 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002570 * @class: pointer to the struct class that this device should be registered to
2571 * @parent: pointer to the parent struct device of this new device, if any
2572 * @devt: the dev_t for the char device to be added
2573 * @drvdata: the data to be added to the device for callbacks
2574 * @fmt: string for the device's name
2575 *
2576 * This function can be used by char device classes. A struct device
2577 * will be created in sysfs, registered to the specified class.
2578 *
2579 * A "dev" file will be created, showing the dev_t for the device, if
2580 * the dev_t is not 0,0.
2581 * If a pointer to a parent struct device is passed in, the newly created
2582 * struct device will be a child of that device in sysfs.
2583 * The pointer to the struct device will be returned from the call.
2584 * Any further sysfs files that might be required can be created using this
2585 * pointer.
2586 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002587 * Returns &struct device pointer on success, or ERR_PTR() on error.
2588 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002589 * Note: the struct class passed to this function must have previously
2590 * been created with a call to class_create().
2591 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002592struct device *device_create(struct class *class, struct device *parent,
2593 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002594{
2595 va_list vargs;
2596 struct device *dev;
2597
2598 va_start(vargs, fmt);
2599 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2600 va_end(vargs);
2601 return dev;
2602}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002603EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002604
Guenter Roeck39ef3112013-07-14 16:05:57 -07002605/**
2606 * device_create_with_groups - creates a device and registers it with sysfs
2607 * @class: pointer to the struct class that this device should be registered to
2608 * @parent: pointer to the parent struct device of this new device, if any
2609 * @devt: the dev_t for the char device to be added
2610 * @drvdata: the data to be added to the device for callbacks
2611 * @groups: NULL-terminated list of attribute groups to be created
2612 * @fmt: string for the device's name
2613 *
2614 * This function can be used by char device classes. A struct device
2615 * will be created in sysfs, registered to the specified class.
2616 * Additional attributes specified in the groups parameter will also
2617 * be created automatically.
2618 *
2619 * A "dev" file will be created, showing the dev_t for the device, if
2620 * the dev_t is not 0,0.
2621 * If a pointer to a parent struct device is passed in, the newly created
2622 * struct device will be a child of that device in sysfs.
2623 * The pointer to the struct device will be returned from the call.
2624 * Any further sysfs files that might be required can be created using this
2625 * pointer.
2626 *
2627 * Returns &struct device pointer on success, or ERR_PTR() on error.
2628 *
2629 * Note: the struct class passed to this function must have previously
2630 * been created with a call to class_create().
2631 */
2632struct device *device_create_with_groups(struct class *class,
2633 struct device *parent, dev_t devt,
2634 void *drvdata,
2635 const struct attribute_group **groups,
2636 const char *fmt, ...)
2637{
2638 va_list vargs;
2639 struct device *dev;
2640
2641 va_start(vargs, fmt);
2642 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2643 fmt, vargs);
2644 va_end(vargs);
2645 return dev;
2646}
2647EXPORT_SYMBOL_GPL(device_create_with_groups);
2648
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002649static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002650{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002651 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002652
Dave Youngcd354492008-01-28 16:56:11 +08002653 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002654}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002655
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002656/**
2657 * device_destroy - removes a device that was created with device_create()
2658 * @class: pointer to the struct class that this device was registered with
2659 * @devt: the dev_t of the device that was previously registered
2660 *
2661 * This call unregisters and cleans up a device that was created with a
2662 * call to device_create().
2663 */
2664void device_destroy(struct class *class, dev_t devt)
2665{
2666 struct device *dev;
2667
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002668 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002669 if (dev) {
2670 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002671 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002672 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002673}
2674EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002675
2676/**
2677 * device_rename - renames a device
2678 * @dev: the pointer to the struct device to be renamed
2679 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002680 *
2681 * It is the responsibility of the caller to provide mutual
2682 * exclusion between two different calls of device_rename
2683 * on the same device to ensure that new_name is valid and
2684 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002685 *
Timur Tabia5462512010-12-13 14:08:52 -06002686 * Note: Don't call this function. Currently, the networking layer calls this
2687 * function, but that will change. The following text from Kay Sievers offers
2688 * some insight:
2689 *
2690 * Renaming devices is racy at many levels, symlinks and other stuff are not
2691 * replaced atomically, and you get a "move" uevent, but it's not easy to
2692 * connect the event to the old and new device. Device nodes are not renamed at
2693 * all, there isn't even support for that in the kernel now.
2694 *
2695 * In the meantime, during renaming, your target name might be taken by another
2696 * driver, creating conflicts. Or the old name is taken directly after you
2697 * renamed it -- then you get events for the same DEVPATH, before you even see
2698 * the "move" event. It's just a mess, and nothing new should ever rely on
2699 * kernel device renaming. Besides that, it's not even implemented now for
2700 * other things than (driver-core wise very simple) network devices.
2701 *
2702 * We are currently about to change network renaming in udev to completely
2703 * disallow renaming of devices in the same namespace as the kernel uses,
2704 * because we can't solve the problems properly, that arise with swapping names
2705 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2706 * be allowed to some other name than eth[0-9]*, for the aforementioned
2707 * reasons.
2708 *
2709 * Make up a "real" name in the driver before you register anything, or add
2710 * some other attributes for userspace to find the device, or use udev to add
2711 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2712 * don't even want to get into that and try to implement the missing pieces in
2713 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002714 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002715int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002716{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002717 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002718 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002719 int error;
2720
2721 dev = get_device(dev);
2722 if (!dev)
2723 return -EINVAL;
2724
ethan.zhao69df7532013-10-13 22:12:35 +08002725 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002726
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002727 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002728 if (!old_device_name) {
2729 error = -ENOMEM;
2730 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002731 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002732
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002733 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002734 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2735 kobj, old_device_name,
2736 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002737 if (error)
2738 goto out;
2739 }
Kay Sievers39aba962010-09-04 22:33:14 -07002740
Tejun Heo4b30ee52013-09-11 22:29:06 -04002741 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002742 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002743 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002744
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002745out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002746 put_device(dev);
2747
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002748 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002749
2750 return error;
2751}
Johannes Berga2807db2007-02-28 12:38:31 +01002752EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002753
2754static int device_move_class_links(struct device *dev,
2755 struct device *old_parent,
2756 struct device *new_parent)
2757{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002758 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002759
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002760 if (old_parent)
2761 sysfs_remove_link(&dev->kobj, "device");
2762 if (new_parent)
2763 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2764 "device");
2765 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002766}
2767
2768/**
2769 * device_move - moves a device to a new parent
2770 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02002771 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002772 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002773 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002774int device_move(struct device *dev, struct device *new_parent,
2775 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002776{
2777 int error;
2778 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002779 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002780
2781 dev = get_device(dev);
2782 if (!dev)
2783 return -EINVAL;
2784
Cornelia Huckffa6a702009-03-04 12:44:00 +01002785 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002786 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002787 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002788 if (IS_ERR(new_parent_kobj)) {
2789 error = PTR_ERR(new_parent_kobj);
2790 put_device(new_parent);
2791 goto out;
2792 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002793
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002794 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2795 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002796 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002797 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002798 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002799 put_device(new_parent);
2800 goto out;
2801 }
2802 old_parent = dev->parent;
2803 dev->parent = new_parent;
2804 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002805 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002806 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002807 klist_add_tail(&dev->p->knode_parent,
2808 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002809 set_dev_node(dev, dev_to_node(new_parent));
2810 }
2811
Rabin Vincentbdd40342012-04-23 09:16:36 +02002812 if (dev->class) {
2813 error = device_move_class_links(dev, old_parent, new_parent);
2814 if (error) {
2815 /* We ignore errors on cleanup since we're hosed anyway... */
2816 device_move_class_links(dev, new_parent, old_parent);
2817 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2818 if (new_parent)
2819 klist_remove(&dev->p->knode_parent);
2820 dev->parent = old_parent;
2821 if (old_parent) {
2822 klist_add_tail(&dev->p->knode_parent,
2823 &old_parent->p->klist_children);
2824 set_dev_node(dev, dev_to_node(old_parent));
2825 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002826 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002827 cleanup_glue_dir(dev, new_parent_kobj);
2828 put_device(new_parent);
2829 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002830 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002831 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002832 switch (dpm_order) {
2833 case DPM_ORDER_NONE:
2834 break;
2835 case DPM_ORDER_DEV_AFTER_PARENT:
2836 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002837 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002838 break;
2839 case DPM_ORDER_PARENT_BEFORE_DEV:
2840 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002841 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002842 break;
2843 case DPM_ORDER_DEV_LAST:
2844 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002845 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002846 break;
2847 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002848
Cornelia Huck8a824722006-11-20 17:07:51 +01002849 put_device(old_parent);
2850out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002851 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002852 put_device(dev);
2853 return error;
2854}
Cornelia Huck8a824722006-11-20 17:07:51 +01002855EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002856
2857/**
2858 * device_shutdown - call ->shutdown() on each device to shutdown.
2859 */
2860void device_shutdown(void)
2861{
Benson Leungf123db82013-09-24 20:05:11 -07002862 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002863
Pingfan Liu3297c8f2018-07-19 13:14:58 +08002864 wait_for_device_probe();
2865 device_block_probing();
2866
Hugh Daschbach62458382010-03-22 10:36:37 -07002867 spin_lock(&devices_kset->list_lock);
2868 /*
2869 * Walk the devices list backward, shutting down each in turn.
2870 * Beware that device unplug events may also start pulling
2871 * devices offline, even as the system is shutting down.
2872 */
2873 while (!list_empty(&devices_kset->list)) {
2874 dev = list_entry(devices_kset->list.prev, struct device,
2875 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002876
2877 /*
2878 * hold reference count of device's parent to
2879 * prevent it from being freed because parent's
2880 * lock is to be held
2881 */
Benson Leungf123db82013-09-24 20:05:11 -07002882 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002883 get_device(dev);
2884 /*
2885 * Make sure the device is off the kset list, in the
2886 * event that dev->*->shutdown() doesn't remove it.
2887 */
2888 list_del_init(&dev->kobj.entry);
2889 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002890
Ming Leid1c6c032012-06-22 18:01:40 +08002891 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002892 if (parent)
2893 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002894 device_lock(dev);
2895
Alan Sternfe6b91f2011-12-06 23:24:52 +01002896 /* Don't allow any more runtime suspends */
2897 pm_runtime_get_noresume(dev);
2898 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002899
Michal Suchanek75216212017-08-11 15:44:43 +02002900 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07002901 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02002902 dev_info(dev, "shutdown_pre\n");
2903 dev->class->shutdown_pre(dev);
2904 }
2905 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002906 if (initcall_debug)
2907 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002908 dev->bus->shutdown(dev);
2909 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002910 if (initcall_debug)
2911 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002912 dev->driver->shutdown(dev);
2913 }
Ming Leid1c6c032012-06-22 18:01:40 +08002914
2915 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002916 if (parent)
2917 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002918
Hugh Daschbach62458382010-03-22 10:36:37 -07002919 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002920 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002921
2922 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002923 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002924 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002925}
Joe Perches99bcf212010-06-27 01:02:34 +00002926
2927/*
2928 * Device logging functions
2929 */
2930
2931#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002932static int
2933create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002934{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002935 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002936 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002937
Kay Sieversc4e00da2012-05-03 02:29:59 +02002938 if (dev->class)
2939 subsys = dev->class->name;
2940 else if (dev->bus)
2941 subsys = dev->bus->name;
2942 else
Joe Perches798efc62012-09-12 20:11:29 -07002943 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002944
Joe Perches798efc62012-09-12 20:11:29 -07002945 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002946 if (pos >= hdrlen)
2947 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002948
2949 /*
2950 * Add device identifier DEVICE=:
2951 * b12:8 block dev_t
2952 * c127:3 char dev_t
2953 * n8 netdev ifindex
2954 * +sound:card0 subsystem:devname
2955 */
2956 if (MAJOR(dev->devt)) {
2957 char c;
2958
2959 if (strcmp(subsys, "block") == 0)
2960 c = 'b';
2961 else
2962 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002963 pos++;
2964 pos += snprintf(hdr + pos, hdrlen - pos,
2965 "DEVICE=%c%u:%u",
2966 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002967 } else if (strcmp(subsys, "net") == 0) {
2968 struct net_device *net = to_net_dev(dev);
2969
Joe Perches798efc62012-09-12 20:11:29 -07002970 pos++;
2971 pos += snprintf(hdr + pos, hdrlen - pos,
2972 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002973 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002974 pos++;
2975 pos += snprintf(hdr + pos, hdrlen - pos,
2976 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002977 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002978
Ben Hutchings655e5b72014-08-26 00:34:44 -07002979 if (pos >= hdrlen)
2980 goto overflow;
2981
Joe Perches798efc62012-09-12 20:11:29 -07002982 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002983
2984overflow:
2985 dev_WARN(dev, "device/subsystem name too long");
2986 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002987}
Joe Perches798efc62012-09-12 20:11:29 -07002988
Joe Perches05e4e5b2012-09-12 20:13:37 -07002989int dev_vprintk_emit(int level, const struct device *dev,
2990 const char *fmt, va_list args)
2991{
2992 char hdr[128];
2993 size_t hdrlen;
2994
2995 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2996
2997 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2998}
2999EXPORT_SYMBOL(dev_vprintk_emit);
3000
3001int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3002{
3003 va_list args;
3004 int r;
3005
3006 va_start(args, fmt);
3007
3008 r = dev_vprintk_emit(level, dev, fmt, args);
3009
3010 va_end(args);
3011
3012 return r;
3013}
3014EXPORT_SYMBOL(dev_printk_emit);
3015
Joe Perchesd1f10522014-12-25 15:07:04 -08003016static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07003017 struct va_format *vaf)
3018{
Joe Perchesd1f10522014-12-25 15:07:04 -08003019 if (dev)
3020 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3021 dev_driver_string(dev), dev_name(dev), vaf);
3022 else
3023 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003024}
Joe Perches99bcf212010-06-27 01:02:34 +00003025
Joe Perchesd1f10522014-12-25 15:07:04 -08003026void dev_printk(const char *level, const struct device *dev,
3027 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00003028{
3029 struct va_format vaf;
3030 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00003031
3032 va_start(args, fmt);
3033
3034 vaf.fmt = fmt;
3035 vaf.va = &args;
3036
Joe Perchesd1f10522014-12-25 15:07:04 -08003037 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003038
Joe Perches99bcf212010-06-27 01:02:34 +00003039 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00003040}
3041EXPORT_SYMBOL(dev_printk);
3042
3043#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08003044void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00003045{ \
3046 struct va_format vaf; \
3047 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003048 \
3049 va_start(args, fmt); \
3050 \
3051 vaf.fmt = fmt; \
3052 vaf.va = &args; \
3053 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003054 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003055 \
Joe Perches99bcf212010-06-27 01:02:34 +00003056 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003057} \
3058EXPORT_SYMBOL(func);
3059
Joe Perches663336e2018-05-09 08:15:46 -07003060define_dev_printk_level(_dev_emerg, KERN_EMERG);
3061define_dev_printk_level(_dev_alert, KERN_ALERT);
3062define_dev_printk_level(_dev_crit, KERN_CRIT);
3063define_dev_printk_level(_dev_err, KERN_ERR);
3064define_dev_printk_level(_dev_warn, KERN_WARNING);
3065define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00003066define_dev_printk_level(_dev_info, KERN_INFO);
3067
3068#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003069
3070static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3071{
3072 return fwnode && !IS_ERR(fwnode->secondary);
3073}
3074
3075/**
3076 * set_primary_fwnode - Change the primary firmware node of a given device.
3077 * @dev: Device to handle.
3078 * @fwnode: New primary firmware node of the device.
3079 *
3080 * Set the device's firmware node pointer to @fwnode, but if a secondary
3081 * firmware node of the device is present, preserve it.
3082 */
3083void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3084{
3085 if (fwnode) {
3086 struct fwnode_handle *fn = dev->fwnode;
3087
3088 if (fwnode_is_primary(fn))
3089 fn = fn->secondary;
3090
Mika Westerberg55f89a82015-11-30 17:11:39 +02003091 if (fn) {
3092 WARN_ON(fwnode->secondary);
3093 fwnode->secondary = fn;
3094 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003095 dev->fwnode = fwnode;
3096 } else {
3097 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3098 dev->fwnode->secondary : NULL;
3099 }
3100}
3101EXPORT_SYMBOL_GPL(set_primary_fwnode);
3102
3103/**
3104 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3105 * @dev: Device to handle.
3106 * @fwnode: New secondary firmware node of the device.
3107 *
3108 * If a primary firmware node of the device is present, set its secondary
3109 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3110 * @fwnode.
3111 */
3112void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3113{
3114 if (fwnode)
3115 fwnode->secondary = ERR_PTR(-ENODEV);
3116
3117 if (fwnode_is_primary(dev->fwnode))
3118 dev->fwnode->secondary = fwnode;
3119 else
3120 dev->fwnode = fwnode;
3121}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003122
3123/**
3124 * device_set_of_node_from_dev - reuse device-tree node of another device
3125 * @dev: device whose device-tree node is being set
3126 * @dev2: device whose device-tree node is being reused
3127 *
3128 * Takes another reference to the new device-tree node after first dropping
3129 * any reference held to the old node.
3130 */
3131void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3132{
3133 of_node_put(dev->of_node);
3134 dev->of_node = of_node_get(dev2->of_node);
3135 dev->of_node_reused = true;
3136}
3137EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);