blob: d58ea075f807362cc5415c0268a0c14afde017ae [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/base/core.c - core driver model code (device registration, etc)
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07007 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Heikki Krogerus7847a142018-11-09 17:21:35 +030011#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/device.h>
13#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020014#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010023#include <linux/genhd.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070024#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010025#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020026#include <linux/netdevice.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010027#include <linux/sched/signal.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070028#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "base.h"
31#include "power/power.h"
32
Andi Kleene52eec12010-09-08 16:54:17 +020033#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080039static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020040{
Jingoo Han34da5e62013-07-26 13:10:22 +090041 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020042}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010046/* Device links support. */
47
48#ifdef CONFIG_SRCU
49static DEFINE_MUTEX(device_links_lock);
50DEFINE_STATIC_SRCU(device_links_srcu);
51
52static inline void device_links_write_lock(void)
53{
54 mutex_lock(&device_links_lock);
55}
56
57static inline void device_links_write_unlock(void)
58{
59 mutex_unlock(&device_links_lock);
60}
61
62int device_links_read_lock(void)
63{
64 return srcu_read_lock(&device_links_srcu);
65}
66
67void device_links_read_unlock(int idx)
68{
69 srcu_read_unlock(&device_links_srcu, idx);
70}
71#else /* !CONFIG_SRCU */
72static DECLARE_RWSEM(device_links_lock);
73
74static inline void device_links_write_lock(void)
75{
76 down_write(&device_links_lock);
77}
78
79static inline void device_links_write_unlock(void)
80{
81 up_write(&device_links_lock);
82}
83
84int device_links_read_lock(void)
85{
86 down_read(&device_links_lock);
87 return 0;
88}
89
90void device_links_read_unlock(int not_used)
91{
92 up_read(&device_links_lock);
93}
94#endif /* !CONFIG_SRCU */
95
96/**
97 * device_is_dependent - Check if one device depends on another one
98 * @dev: Device to check dependencies for.
99 * @target: Device to check against.
100 *
101 * Check if @target depends on @dev or any device dependent on it (its child or
102 * its consumer etc). Return 1 if that is the case or 0 otherwise.
103 */
104static int device_is_dependent(struct device *dev, void *target)
105{
106 struct device_link *link;
107 int ret;
108
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200109 if (dev == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100110 return 1;
111
112 ret = device_for_each_child(dev, target, device_is_dependent);
113 if (ret)
114 return ret;
115
116 list_for_each_entry(link, &dev->links.consumers, s_node) {
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200117 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100118 return 1;
119
120 ret = device_is_dependent(link->consumer, target);
121 if (ret)
122 break;
123 }
124 return ret;
125}
126
127static int device_reorder_to_tail(struct device *dev, void *not_used)
128{
129 struct device_link *link;
130
131 /*
132 * Devices that have not been registered yet will be put to the ends
133 * of the lists during the registration, so skip them here.
134 */
135 if (device_is_registered(dev))
136 devices_kset_move_last(dev);
137
138 if (device_pm_initialized(dev))
139 device_pm_move_last(dev);
140
141 device_for_each_child(dev, NULL, device_reorder_to_tail);
142 list_for_each_entry(link, &dev->links.consumers, s_node)
143 device_reorder_to_tail(link->consumer, NULL);
144
145 return 0;
146}
147
148/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700149 * device_pm_move_to_tail - Move set of devices to the end of device lists
150 * @dev: Device to move
151 *
152 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
153 *
154 * It moves the @dev along with all of its children and all of its consumers
155 * to the ends of the device_kset and dpm_list, recursively.
156 */
157void device_pm_move_to_tail(struct device *dev)
158{
159 int idx;
160
161 idx = device_links_read_lock();
162 device_pm_lock();
163 device_reorder_to_tail(dev, NULL);
164 device_pm_unlock();
165 device_links_read_unlock(idx);
166}
167
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100168static void device_link_rpm_prepare(struct device *consumer,
169 struct device *supplier)
170{
171 pm_runtime_new_link(consumer);
172 /*
173 * If the link is being added by the consumer driver at probe time,
174 * balance the decrementation of the supplier's runtime PM usage counter
175 * after consumer probe in driver_probe_device().
176 */
177 if (consumer->links.status == DL_DEV_PROBING)
178 pm_runtime_get_noresume(supplier);
179}
180
Feng Kan494fd7b2018-04-10 16:57:06 -0700181/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100182 * device_link_add - Create a link between two devices.
183 * @consumer: Consumer end of the link.
184 * @supplier: Supplier end of the link.
185 * @flags: Link flags.
186 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100187 * The caller is responsible for the proper synchronization of the link creation
188 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
189 * runtime PM framework to take the link into account. Second, if the
190 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
191 * be forced into the active metastate and reference-counted upon the creation
192 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
193 * ignored.
194 *
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100195 * If the DL_FLAG_AUTOREMOVE_CONSUMER flag is set, the link will be removed
196 * automatically when the consumer device driver unbinds from it. Analogously,
197 * if DL_FLAG_AUTOREMOVE_SUPPLIER is set in @flags, the link will be removed
198 * automatically when the supplier device driver unbinds from it.
199 *
200 * The combination of DL_FLAG_STATELESS and either DL_FLAG_AUTOREMOVE_CONSUMER
201 * or DL_FLAG_AUTOREMOVE_SUPPLIER set in @flags at the same time is invalid and
202 * will cause NULL to be returned upfront.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100203 *
204 * A side effect of the link creation is re-ordering of dpm_list and the
205 * devices_kset list by moving the consumer device and all devices depending
206 * on it to the ends of these lists (that does not happen to devices that have
207 * not been registered when this function is called).
208 *
209 * The supplier device is required to be registered when this function is called
210 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100211 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100212 */
213struct device_link *device_link_add(struct device *consumer,
214 struct device *supplier, u32 flags)
215{
216 struct device_link *link;
217
218 if (!consumer || !supplier ||
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100219 (flags & DL_FLAG_STATELESS &&
220 flags & (DL_FLAG_AUTOREMOVE_CONSUMER | DL_FLAG_AUTOREMOVE_SUPPLIER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100221 return NULL;
222
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100223 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
224 if (pm_runtime_get_sync(supplier) < 0) {
225 pm_runtime_put_noidle(supplier);
226 return NULL;
227 }
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100228 }
229
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100230 device_links_write_lock();
231 device_pm_lock();
232
233 /*
234 * If the supplier has not been fully registered yet or there is a
235 * reverse dependency between the consumer and the supplier already in
236 * the graph, return NULL.
237 */
238 if (!device_pm_initialized(supplier)
239 || device_is_dependent(consumer, supplier)) {
240 link = NULL;
241 goto out;
242 }
243
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100244 list_for_each_entry(link, &supplier->links.consumers, s_node) {
245 if (link->consumer != consumer)
246 continue;
247
248 /*
249 * Don't return a stateless link if the caller wants a stateful
250 * one and vice versa.
251 */
252 if (WARN_ON((flags & DL_FLAG_STATELESS) != (link->flags & DL_FLAG_STATELESS))) {
253 link = NULL;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100254 goto out;
Lukas Wunneread18c22018-02-10 19:27:12 +0100255 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100256
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100257 if (flags & DL_FLAG_AUTOREMOVE_CONSUMER)
258 link->flags |= DL_FLAG_AUTOREMOVE_CONSUMER;
259
260 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
261 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
262
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100263 if (flags & DL_FLAG_PM_RUNTIME) {
264 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
265 device_link_rpm_prepare(consumer, supplier);
266 link->flags |= DL_FLAG_PM_RUNTIME;
267 }
268 if (flags & DL_FLAG_RPM_ACTIVE)
269 refcount_inc(&link->rpm_active);
270 }
271
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100272 kref_get(&link->kref);
273 goto out;
274 }
275
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100276 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100277 if (!link)
278 goto out;
279
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100280 refcount_set(&link->rpm_active, 1);
281
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100282 if (flags & DL_FLAG_PM_RUNTIME) {
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100283 if (flags & DL_FLAG_RPM_ACTIVE)
284 refcount_inc(&link->rpm_active);
285
286 device_link_rpm_prepare(consumer, supplier);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100287 }
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100288
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100289 get_device(supplier);
290 link->supplier = supplier;
291 INIT_LIST_HEAD(&link->s_node);
292 get_device(consumer);
293 link->consumer = consumer;
294 INIT_LIST_HEAD(&link->c_node);
295 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100296 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100297
Lukas Wunner64df1142016-12-04 13:10:04 +0100298 /* Determine the initial link state. */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100299 if (flags & DL_FLAG_STATELESS) {
300 link->status = DL_STATE_NONE;
301 } else {
302 switch (supplier->links.status) {
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100303 case DL_DEV_PROBING:
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100304 switch (consumer->links.status) {
305 case DL_DEV_PROBING:
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100306 /*
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100307 * A consumer driver can create a link to a
308 * supplier that has not completed its probing
309 * yet as long as it knows that the supplier is
310 * already functional (for example, it has just
311 * acquired some resources from the supplier).
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100312 */
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100313 link->status = DL_STATE_CONSUMER_PROBE;
314 break;
315 default:
316 link->status = DL_STATE_DORMANT;
317 break;
318 }
319 break;
320 case DL_DEV_DRIVER_BOUND:
321 switch (consumer->links.status) {
322 case DL_DEV_PROBING:
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100323 link->status = DL_STATE_CONSUMER_PROBE;
324 break;
325 case DL_DEV_DRIVER_BOUND:
326 link->status = DL_STATE_ACTIVE;
327 break;
328 default:
329 link->status = DL_STATE_AVAILABLE;
330 break;
331 }
332 break;
333 case DL_DEV_UNBINDING:
334 link->status = DL_STATE_SUPPLIER_UNBIND;
335 break;
336 default:
337 link->status = DL_STATE_DORMANT;
338 break;
339 }
340 }
341
342 /*
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100343 * Some callers expect the link creation during consumer driver probe to
344 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
345 */
346 if (link->status == DL_STATE_CONSUMER_PROBE &&
347 flags & DL_FLAG_PM_RUNTIME)
348 pm_runtime_resume(supplier);
349
350 /*
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100351 * Move the consumer and all of the devices depending on it to the end
352 * of dpm_list and the devices_kset list.
353 *
354 * It is necessary to hold dpm_list locked throughout all that or else
355 * we may end up suspending with a wrong ordering of it.
356 */
357 device_reorder_to_tail(consumer, NULL);
358
359 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
360 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
361
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100362 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100363
364 out:
365 device_pm_unlock();
366 device_links_write_unlock();
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100367
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100368 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100369 pm_runtime_put(supplier);
370
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100371 return link;
372}
373EXPORT_SYMBOL_GPL(device_link_add);
374
375static void device_link_free(struct device_link *link)
376{
377 put_device(link->consumer);
378 put_device(link->supplier);
379 kfree(link);
380}
381
382#ifdef CONFIG_SRCU
383static void __device_link_free_srcu(struct rcu_head *rhead)
384{
385 device_link_free(container_of(rhead, struct device_link, rcu_head));
386}
387
Lukas Wunneread18c22018-02-10 19:27:12 +0100388static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100389{
Lukas Wunneread18c22018-02-10 19:27:12 +0100390 struct device_link *link = container_of(kref, struct device_link, kref);
391
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100392 dev_dbg(link->consumer, "Dropping the link to %s\n",
393 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100394
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100395 if (link->flags & DL_FLAG_PM_RUNTIME)
396 pm_runtime_drop_link(link->consumer);
397
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100398 list_del_rcu(&link->s_node);
399 list_del_rcu(&link->c_node);
400 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
401}
402#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100403static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100404{
Lukas Wunneread18c22018-02-10 19:27:12 +0100405 struct device_link *link = container_of(kref, struct device_link, kref);
406
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100407 dev_info(link->consumer, "Dropping the link to %s\n",
408 dev_name(link->supplier));
409
Lukas Wunner433986c2018-02-10 19:13:58 +0100410 if (link->flags & DL_FLAG_PM_RUNTIME)
411 pm_runtime_drop_link(link->consumer);
412
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100413 list_del(&link->s_node);
414 list_del(&link->c_node);
415 device_link_free(link);
416}
417#endif /* !CONFIG_SRCU */
418
419/**
420 * device_link_del - Delete a link between two devices.
421 * @link: Device link to delete.
422 *
423 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100424 * PM. If the link was added multiple times, it needs to be deleted as often.
425 * Care is required for hotplugged devices: Their links are purged on removal
426 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100427 */
428void device_link_del(struct device_link *link)
429{
430 device_links_write_lock();
431 device_pm_lock();
Lukas Wunneread18c22018-02-10 19:27:12 +0100432 kref_put(&link->kref, __device_link_del);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100433 device_pm_unlock();
434 device_links_write_unlock();
435}
436EXPORT_SYMBOL_GPL(device_link_del);
437
pascal pailletd8842212018-07-05 14:25:56 +0000438/**
439 * device_link_remove - remove a link between two devices.
440 * @consumer: Consumer end of the link.
441 * @supplier: Supplier end of the link.
442 *
443 * The caller must ensure proper synchronization of this function with runtime
444 * PM.
445 */
446void device_link_remove(void *consumer, struct device *supplier)
447{
448 struct device_link *link;
449
450 if (WARN_ON(consumer == supplier))
451 return;
452
453 device_links_write_lock();
454 device_pm_lock();
455
456 list_for_each_entry(link, &supplier->links.consumers, s_node) {
457 if (link->consumer == consumer) {
458 kref_put(&link->kref, __device_link_del);
459 break;
460 }
461 }
462
463 device_pm_unlock();
464 device_links_write_unlock();
465}
466EXPORT_SYMBOL_GPL(device_link_remove);
467
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100468static void device_links_missing_supplier(struct device *dev)
469{
470 struct device_link *link;
471
472 list_for_each_entry(link, &dev->links.suppliers, c_node)
473 if (link->status == DL_STATE_CONSUMER_PROBE)
474 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
475}
476
477/**
478 * device_links_check_suppliers - Check presence of supplier drivers.
479 * @dev: Consumer device.
480 *
481 * Check links from this device to any suppliers. Walk the list of the device's
482 * links to suppliers and see if all of them are available. If not, simply
483 * return -EPROBE_DEFER.
484 *
485 * We need to guarantee that the supplier will not go away after the check has
486 * been positive here. It only can go away in __device_release_driver() and
487 * that function checks the device's links to consumers. This means we need to
488 * mark the link as "consumer probe in progress" to make the supplier removal
489 * wait for us to complete (or bad things may happen).
490 *
491 * Links with the DL_FLAG_STATELESS flag set are ignored.
492 */
493int device_links_check_suppliers(struct device *dev)
494{
495 struct device_link *link;
496 int ret = 0;
497
498 device_links_write_lock();
499
500 list_for_each_entry(link, &dev->links.suppliers, c_node) {
501 if (link->flags & DL_FLAG_STATELESS)
502 continue;
503
504 if (link->status != DL_STATE_AVAILABLE) {
505 device_links_missing_supplier(dev);
506 ret = -EPROBE_DEFER;
507 break;
508 }
509 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
510 }
511 dev->links.status = DL_DEV_PROBING;
512
513 device_links_write_unlock();
514 return ret;
515}
516
517/**
518 * device_links_driver_bound - Update device links after probing its driver.
519 * @dev: Device to update the links for.
520 *
521 * The probe has been successful, so update links from this device to any
522 * consumers by changing their status to "available".
523 *
524 * Also change the status of @dev's links to suppliers to "active".
525 *
526 * Links with the DL_FLAG_STATELESS flag set are ignored.
527 */
528void device_links_driver_bound(struct device *dev)
529{
530 struct device_link *link;
531
532 device_links_write_lock();
533
534 list_for_each_entry(link, &dev->links.consumers, s_node) {
535 if (link->flags & DL_FLAG_STATELESS)
536 continue;
537
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100538 /*
539 * Links created during consumer probe may be in the "consumer
540 * probe" state to start with if the supplier is still probing
541 * when they are created and they may become "active" if the
542 * consumer probe returns first. Skip them here.
543 */
544 if (link->status == DL_STATE_CONSUMER_PROBE ||
545 link->status == DL_STATE_ACTIVE)
546 continue;
547
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100548 WARN_ON(link->status != DL_STATE_DORMANT);
549 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
550 }
551
552 list_for_each_entry(link, &dev->links.suppliers, c_node) {
553 if (link->flags & DL_FLAG_STATELESS)
554 continue;
555
556 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
557 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
558 }
559
560 dev->links.status = DL_DEV_DRIVER_BOUND;
561
562 device_links_write_unlock();
563}
564
565/**
566 * __device_links_no_driver - Update links of a device without a driver.
567 * @dev: Device without a drvier.
568 *
569 * Delete all non-persistent links from this device to any suppliers.
570 *
571 * Persistent links stay around, but their status is changed to "available",
572 * unless they already are in the "supplier unbind in progress" state in which
573 * case they need not be updated.
574 *
575 * Links with the DL_FLAG_STATELESS flag set are ignored.
576 */
577static void __device_links_no_driver(struct device *dev)
578{
579 struct device_link *link, *ln;
580
581 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
582 if (link->flags & DL_FLAG_STATELESS)
583 continue;
584
Vivek Gautame88728f2018-06-27 18:20:55 +0530585 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Yong Wu0fe6f782019-01-01 12:51:05 +0800586 __device_link_del(&link->kref);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100587 else if (link->status == DL_STATE_CONSUMER_PROBE ||
588 link->status == DL_STATE_ACTIVE)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100589 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
590 }
591
592 dev->links.status = DL_DEV_NO_DRIVER;
593}
594
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100595/**
596 * device_links_no_driver - Update links after failing driver probe.
597 * @dev: Device whose driver has just failed to probe.
598 *
599 * Clean up leftover links to consumers for @dev and invoke
600 * %__device_links_no_driver() to update links to suppliers for it as
601 * appropriate.
602 *
603 * Links with the DL_FLAG_STATELESS flag set are ignored.
604 */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100605void device_links_no_driver(struct device *dev)
606{
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100607 struct device_link *link;
608
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100609 device_links_write_lock();
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100610
611 list_for_each_entry(link, &dev->links.consumers, s_node) {
612 if (link->flags & DL_FLAG_STATELESS)
613 continue;
614
615 /*
616 * The probe has failed, so if the status of the link is
617 * "consumer probe" or "active", it must have been added by
618 * a probing consumer while this device was still probing.
619 * Change its state to "dormant", as it represents a valid
620 * relationship, but it is not functionally meaningful.
621 */
622 if (link->status == DL_STATE_CONSUMER_PROBE ||
623 link->status == DL_STATE_ACTIVE)
624 WRITE_ONCE(link->status, DL_STATE_DORMANT);
625 }
626
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100627 __device_links_no_driver(dev);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100628
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100629 device_links_write_unlock();
630}
631
632/**
633 * device_links_driver_cleanup - Update links after driver removal.
634 * @dev: Device whose driver has just gone away.
635 *
636 * Update links to consumers for @dev by changing their status to "dormant" and
637 * invoke %__device_links_no_driver() to update links to suppliers for it as
638 * appropriate.
639 *
640 * Links with the DL_FLAG_STATELESS flag set are ignored.
641 */
642void device_links_driver_cleanup(struct device *dev)
643{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100644 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100645
646 device_links_write_lock();
647
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100648 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100649 if (link->flags & DL_FLAG_STATELESS)
650 continue;
651
Vivek Gautame88728f2018-06-27 18:20:55 +0530652 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100653 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530654
655 /*
656 * autoremove the links between this @dev and its consumer
657 * devices that are not active, i.e. where the link state
658 * has moved to DL_STATE_SUPPLIER_UNBIND.
659 */
660 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
661 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Yong Wu0fe6f782019-01-01 12:51:05 +0800662 __device_link_del(&link->kref);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530663
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100664 WRITE_ONCE(link->status, DL_STATE_DORMANT);
665 }
666
667 __device_links_no_driver(dev);
668
669 device_links_write_unlock();
670}
671
672/**
673 * device_links_busy - Check if there are any busy links to consumers.
674 * @dev: Device to check.
675 *
676 * Check each consumer of the device and return 'true' if its link's status
677 * is one of "consumer probe" or "active" (meaning that the given consumer is
678 * probing right now or its driver is present). Otherwise, change the link
679 * state to "supplier unbind" to prevent the consumer from being probed
680 * successfully going forward.
681 *
682 * Return 'false' if there are no probing or active consumers.
683 *
684 * Links with the DL_FLAG_STATELESS flag set are ignored.
685 */
686bool device_links_busy(struct device *dev)
687{
688 struct device_link *link;
689 bool ret = false;
690
691 device_links_write_lock();
692
693 list_for_each_entry(link, &dev->links.consumers, s_node) {
694 if (link->flags & DL_FLAG_STATELESS)
695 continue;
696
697 if (link->status == DL_STATE_CONSUMER_PROBE
698 || link->status == DL_STATE_ACTIVE) {
699 ret = true;
700 break;
701 }
702 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
703 }
704
705 dev->links.status = DL_DEV_UNBINDING;
706
707 device_links_write_unlock();
708 return ret;
709}
710
711/**
712 * device_links_unbind_consumers - Force unbind consumers of the given device.
713 * @dev: Device to unbind the consumers of.
714 *
715 * Walk the list of links to consumers for @dev and if any of them is in the
716 * "consumer probe" state, wait for all device probes in progress to complete
717 * and start over.
718 *
719 * If that's not the case, change the status of the link to "supplier unbind"
720 * and check if the link was in the "active" state. If so, force the consumer
721 * driver to unbind and start over (the consumer will not re-probe as we have
722 * changed the state of the link already).
723 *
724 * Links with the DL_FLAG_STATELESS flag set are ignored.
725 */
726void device_links_unbind_consumers(struct device *dev)
727{
728 struct device_link *link;
729
730 start:
731 device_links_write_lock();
732
733 list_for_each_entry(link, &dev->links.consumers, s_node) {
734 enum device_link_state status;
735
736 if (link->flags & DL_FLAG_STATELESS)
737 continue;
738
739 status = link->status;
740 if (status == DL_STATE_CONSUMER_PROBE) {
741 device_links_write_unlock();
742
743 wait_for_device_probe();
744 goto start;
745 }
746 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
747 if (status == DL_STATE_ACTIVE) {
748 struct device *consumer = link->consumer;
749
750 get_device(consumer);
751
752 device_links_write_unlock();
753
754 device_release_driver_internal(consumer, NULL,
755 consumer->parent);
756 put_device(consumer);
757 goto start;
758 }
759 }
760
761 device_links_write_unlock();
762}
763
764/**
765 * device_links_purge - Delete existing links to other devices.
766 * @dev: Target device.
767 */
768static void device_links_purge(struct device *dev)
769{
770 struct device_link *link, *ln;
771
772 /*
773 * Delete all of the remaining links from this device to any other
774 * devices (either consumers or suppliers).
775 */
776 device_links_write_lock();
777
778 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
779 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100780 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100781 }
782
783 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
784 WARN_ON(link->status != DL_STATE_DORMANT &&
785 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100786 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100787 }
788
789 device_links_write_unlock();
790}
791
792/* Device links support end. */
793
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800794int (*platform_notify)(struct device *dev) = NULL;
795int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700796static struct kobject *dev_kobj;
797struct kobject *sysfs_dev_char_kobj;
798struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200800static DEFINE_MUTEX(device_hotplug_lock);
801
802void lock_device_hotplug(void)
803{
804 mutex_lock(&device_hotplug_lock);
805}
806
807void unlock_device_hotplug(void)
808{
809 mutex_unlock(&device_hotplug_lock);
810}
811
812int lock_device_hotplug_sysfs(void)
813{
814 if (mutex_trylock(&device_hotplug_lock))
815 return 0;
816
817 /* Avoid busy looping (5 ms of sleep should do). */
818 msleep(5);
819 return restart_syscall();
820}
821
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800822#ifdef CONFIG_BLOCK
823static inline int device_is_not_partition(struct device *dev)
824{
825 return !(dev->type == &part_type);
826}
827#else
828static inline int device_is_not_partition(struct device *dev)
829{
830 return 1;
831}
832#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Heikki Krogerus07de0e82018-11-09 17:21:34 +0300834static int
835device_platform_notify(struct device *dev, enum kobject_action action)
836{
Heikki Krogerus7847a142018-11-09 17:21:35 +0300837 int ret;
838
839 ret = acpi_platform_notify(dev, action);
840 if (ret)
841 return ret;
842
Heikki Krogerus59abd832018-11-09 17:21:36 +0300843 ret = software_node_notify(dev, action);
844 if (ret)
845 return ret;
846
Heikki Krogerus07de0e82018-11-09 17:21:34 +0300847 if (platform_notify && action == KOBJ_ADD)
848 platform_notify(dev);
849 else if (platform_notify_remove && action == KOBJ_REMOVE)
850 platform_notify_remove(dev);
851 return 0;
852}
853
Alan Stern3e956372006-06-16 17:10:48 -0400854/**
855 * dev_driver_string - Return a device's driver name, if at all possible
856 * @dev: struct device to get the name of
857 *
858 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800859 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400860 * it is attached to. If it is not attached to a bus either, an empty
861 * string will be returned.
862 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700863const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400864{
Alan Stern35899722009-12-04 11:06:57 -0500865 struct device_driver *drv;
866
867 /* dev->driver can change to NULL underneath us because of unbinding,
868 * so be careful about accessing it. dev->bus and dev->class should
869 * never change once they are set, so they don't need special care.
870 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700871 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500872 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100873 (dev->bus ? dev->bus->name :
874 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400875}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600876EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
879
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800880static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
881 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800883 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200884 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500885 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400888 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800889 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900890 printk("dev_attr_show: %pS returned bad count\n",
891 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return ret;
894}
895
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800896static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
897 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800899 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200900 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500901 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400904 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return ret;
906}
907
Emese Revfy52cf25d2010-01-19 02:58:23 +0100908static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 .show = dev_attr_show,
910 .store = dev_attr_store,
911};
912
Kay Sieversca22e562011-12-14 14:29:38 -0800913#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
914
915ssize_t device_store_ulong(struct device *dev,
916 struct device_attribute *attr,
917 const char *buf, size_t size)
918{
919 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -0800920 int ret;
921 unsigned long new;
922
923 ret = kstrtoul(buf, 0, &new);
924 if (ret)
925 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -0800926 *(unsigned long *)(ea->var) = new;
927 /* Always return full write size even if we didn't consume all */
928 return size;
929}
930EXPORT_SYMBOL_GPL(device_store_ulong);
931
932ssize_t device_show_ulong(struct device *dev,
933 struct device_attribute *attr,
934 char *buf)
935{
936 struct dev_ext_attribute *ea = to_ext_attr(attr);
937 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
938}
939EXPORT_SYMBOL_GPL(device_show_ulong);
940
941ssize_t device_store_int(struct device *dev,
942 struct device_attribute *attr,
943 const char *buf, size_t size)
944{
945 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -0800946 int ret;
947 long new;
948
949 ret = kstrtol(buf, 0, &new);
950 if (ret)
951 return ret;
952
953 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -0800954 return -EINVAL;
955 *(int *)(ea->var) = new;
956 /* Always return full write size even if we didn't consume all */
957 return size;
958}
959EXPORT_SYMBOL_GPL(device_store_int);
960
961ssize_t device_show_int(struct device *dev,
962 struct device_attribute *attr,
963 char *buf)
964{
965 struct dev_ext_attribute *ea = to_ext_attr(attr);
966
967 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
968}
969EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Borislav Petkov91872392012-10-09 19:52:05 +0200971ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
972 const char *buf, size_t size)
973{
974 struct dev_ext_attribute *ea = to_ext_attr(attr);
975
976 if (strtobool(buf, ea->var) < 0)
977 return -EINVAL;
978
979 return size;
980}
981EXPORT_SYMBOL_GPL(device_store_bool);
982
983ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
984 char *buf)
985{
986 struct dev_ext_attribute *ea = to_ext_attr(attr);
987
988 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
989}
990EXPORT_SYMBOL_GPL(device_show_bool);
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400993 * device_release - free device structure.
994 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400996 * This is called once the reference count for the object
997 * reaches 0. We forward the call to the device's release
998 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001000static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001002 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001003 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Ming Leia525a3d2012-07-25 01:42:29 +08001005 /*
1006 * Some platform devices are driven without driver attached
1007 * and managed resources may have been acquired. Make sure
1008 * all resources are released.
1009 *
1010 * Drivers still can add resources into device after device
1011 * is deleted but alive, so release devres here to avoid
1012 * possible memory leak.
1013 */
1014 devres_release_all(dev);
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (dev->release)
1017 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001018 else if (dev->type && dev->type->release)
1019 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001020 else if (dev->class && dev->class->dev_release)
1021 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -07001022 else
Ezequiel Garcia186bddb2018-12-03 13:44:35 -03001023 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001024 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001025 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001028static const void *device_namespace(struct kobject *kobj)
1029{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001030 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001031 const void *ns = NULL;
1032
1033 if (dev->class && dev->class->ns_type)
1034 ns = dev->class->namespace(dev);
1035
1036 return ns;
1037}
1038
Dmitry Torokhov9944e892018-07-20 21:56:50 +00001039static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1040{
1041 struct device *dev = kobj_to_dev(kobj);
1042
1043 if (dev->class && dev->class->get_ownership)
1044 dev->class->get_ownership(dev, uid, gid);
1045}
1046
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06001047static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 .release = device_release,
1049 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001050 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +00001051 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052};
1053
1054
Kay Sievers312c0042005-11-16 09:00:00 +01001055static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct kobj_type *ktype = get_ktype(kobj);
1058
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06001059 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001060 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (dev->bus)
1062 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001063 if (dev->class)
1064 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 return 0;
1067}
1068
Kay Sievers312c0042005-11-16 09:00:00 +01001069static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001071 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001073 if (dev->bus)
1074 return dev->bus->name;
1075 if (dev->class)
1076 return dev->class->name;
1077 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Kay Sievers7eff2e72007-08-14 15:15:12 +02001080static int dev_uevent(struct kset *kset, struct kobject *kobj,
1081 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001083 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 int retval = 0;
1085
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001086 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001087 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001088 const char *tmp;
1089 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04001090 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001091 kuid_t uid = GLOBAL_ROOT_UID;
1092 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001093
Kay Sievers7eff2e72007-08-14 15:15:12 +02001094 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1095 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001096 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001097 if (name) {
1098 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02001099 if (mode)
1100 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001101 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1102 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1103 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1104 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001105 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001106 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001107 }
1108
Kay Sievers414264f2007-03-12 21:08:57 +01001109 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001110 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01001111
Kay Sievers239378f2006-10-07 21:54:55 +02001112 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001113 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02001114
Grant Likely07d57a32012-02-01 11:22:22 -07001115 /* Add common DT information about the device */
1116 of_device_uevent(dev, env);
1117
Kay Sievers7eff2e72007-08-14 15:15:12 +02001118 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01001119 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001120 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001121 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001122 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001123 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
Kay Sievers7eff2e72007-08-14 15:15:12 +02001126 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001127 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001128 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001129 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001130 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001131 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001132 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001133 }
1134
Stefan Weileef35c22010-08-06 21:11:15 +02001135 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02001136 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001137 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001138 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001139 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001140 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001141 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001142 }
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return retval;
1145}
1146
Emese Revfy9cd43612009-12-31 14:52:51 +01001147static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01001148 .filter = dev_uevent_filter,
1149 .name = dev_uevent_name,
1150 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151};
1152
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001153static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02001154 char *buf)
1155{
1156 struct kobject *top_kobj;
1157 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001158 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02001159 int i;
1160 size_t count = 0;
1161 int retval;
1162
1163 /* search the kset, the device belongs to */
1164 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001165 while (!top_kobj->kset && top_kobj->parent)
1166 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02001167 if (!top_kobj->kset)
1168 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001169
Kay Sievers16574dc2007-04-06 01:40:38 +02001170 kset = top_kobj->kset;
1171 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1172 goto out;
1173
1174 /* respect filter */
1175 if (kset->uevent_ops && kset->uevent_ops->filter)
1176 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1177 goto out;
1178
Kay Sievers7eff2e72007-08-14 15:15:12 +02001179 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1180 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001181 return -ENOMEM;
1182
Kay Sievers16574dc2007-04-06 01:40:38 +02001183 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001184 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001185 if (retval)
1186 goto out;
1187
1188 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001189 for (i = 0; i < env->envp_idx; i++)
1190 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001191out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001192 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001193 return count;
1194}
1195
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001196static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001197 const char *buf, size_t count)
1198{
Peter Rajnohadf44b472018-12-05 12:27:44 +01001199 int rc;
1200
1201 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1202
1203 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001204 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01001205 return rc;
1206 }
Kay Sievers60a96a52007-07-08 22:29:26 +02001207
Kay Sieversa7fd6702005-10-01 14:49:43 +02001208 return count;
1209}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001210static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001211
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001212static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001213 char *buf)
1214{
1215 bool val;
1216
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001217 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001218 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001219 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001220 return sprintf(buf, "%u\n", val);
1221}
1222
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001223static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001224 const char *buf, size_t count)
1225{
1226 bool val;
1227 int ret;
1228
1229 ret = strtobool(buf, &val);
1230 if (ret < 0)
1231 return ret;
1232
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001233 ret = lock_device_hotplug_sysfs();
1234 if (ret)
1235 return ret;
1236
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001237 ret = val ? device_online(dev) : device_offline(dev);
1238 unlock_device_hotplug();
1239 return ret < 0 ? ret : count;
1240}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001241static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001242
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001243int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001244{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001245 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001246}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001247EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001248
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001249void device_remove_groups(struct device *dev,
1250 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001251{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001252 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001253}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001254EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001255
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001256union device_attr_group_devres {
1257 const struct attribute_group *group;
1258 const struct attribute_group **groups;
1259};
1260
1261static int devm_attr_group_match(struct device *dev, void *res, void *data)
1262{
1263 return ((union device_attr_group_devres *)res)->group == data;
1264}
1265
1266static void devm_attr_group_remove(struct device *dev, void *res)
1267{
1268 union device_attr_group_devres *devres = res;
1269 const struct attribute_group *group = devres->group;
1270
1271 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1272 sysfs_remove_group(&dev->kobj, group);
1273}
1274
1275static void devm_attr_groups_remove(struct device *dev, void *res)
1276{
1277 union device_attr_group_devres *devres = res;
1278 const struct attribute_group **groups = devres->groups;
1279
1280 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1281 sysfs_remove_groups(&dev->kobj, groups);
1282}
1283
1284/**
1285 * devm_device_add_group - given a device, create a managed attribute group
1286 * @dev: The device to create the group for
1287 * @grp: The attribute group to create
1288 *
1289 * This function creates a group for the first time. It will explicitly
1290 * warn and error if any of the attribute files being created already exist.
1291 *
1292 * Returns 0 on success or error code on failure.
1293 */
1294int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1295{
1296 union device_attr_group_devres *devres;
1297 int error;
1298
1299 devres = devres_alloc(devm_attr_group_remove,
1300 sizeof(*devres), GFP_KERNEL);
1301 if (!devres)
1302 return -ENOMEM;
1303
1304 error = sysfs_create_group(&dev->kobj, grp);
1305 if (error) {
1306 devres_free(devres);
1307 return error;
1308 }
1309
1310 devres->group = grp;
1311 devres_add(dev, devres);
1312 return 0;
1313}
1314EXPORT_SYMBOL_GPL(devm_device_add_group);
1315
1316/**
1317 * devm_device_remove_group: remove a managed group from a device
1318 * @dev: device to remove the group from
1319 * @grp: group to remove
1320 *
1321 * This function removes a group of attributes from a device. The attributes
1322 * previously have to have been created for this group, otherwise it will fail.
1323 */
1324void devm_device_remove_group(struct device *dev,
1325 const struct attribute_group *grp)
1326{
1327 WARN_ON(devres_release(dev, devm_attr_group_remove,
1328 devm_attr_group_match,
1329 /* cast away const */ (void *)grp));
1330}
1331EXPORT_SYMBOL_GPL(devm_device_remove_group);
1332
1333/**
1334 * devm_device_add_groups - create a bunch of managed attribute groups
1335 * @dev: The device to create the group for
1336 * @groups: The attribute groups to create, NULL terminated
1337 *
1338 * This function creates a bunch of managed attribute groups. If an error
1339 * occurs when creating a group, all previously created groups will be
1340 * removed, unwinding everything back to the original state when this
1341 * function was called. It will explicitly warn and error if any of the
1342 * attribute files being created already exist.
1343 *
1344 * Returns 0 on success or error code from sysfs_create_group on failure.
1345 */
1346int devm_device_add_groups(struct device *dev,
1347 const struct attribute_group **groups)
1348{
1349 union device_attr_group_devres *devres;
1350 int error;
1351
1352 devres = devres_alloc(devm_attr_groups_remove,
1353 sizeof(*devres), GFP_KERNEL);
1354 if (!devres)
1355 return -ENOMEM;
1356
1357 error = sysfs_create_groups(&dev->kobj, groups);
1358 if (error) {
1359 devres_free(devres);
1360 return error;
1361 }
1362
1363 devres->groups = groups;
1364 devres_add(dev, devres);
1365 return 0;
1366}
1367EXPORT_SYMBOL_GPL(devm_device_add_groups);
1368
1369/**
1370 * devm_device_remove_groups - remove a list of managed groups
1371 *
1372 * @dev: The device for the groups to be removed from
1373 * @groups: NULL terminated list of groups to be removed
1374 *
1375 * If groups is not NULL, remove the specified groups from the device.
1376 */
1377void devm_device_remove_groups(struct device *dev,
1378 const struct attribute_group **groups)
1379{
1380 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1381 devm_attr_group_match,
1382 /* cast away const */ (void *)groups));
1383}
1384EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001386static int device_add_attrs(struct device *dev)
1387{
1388 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001389 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001390 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001391
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001392 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001393 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001394 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001395 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001396 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001397
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001398 if (type) {
1399 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001400 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001401 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001402 }
1403
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001404 error = device_add_groups(dev, dev->groups);
1405 if (error)
1406 goto err_remove_type_groups;
1407
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001408 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001409 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001410 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001411 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001412 }
1413
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001414 return 0;
1415
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001416 err_remove_dev_groups:
1417 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001418 err_remove_type_groups:
1419 if (type)
1420 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001421 err_remove_class_groups:
1422 if (class)
1423 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001424
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001425 return error;
1426}
1427
1428static void device_remove_attrs(struct device *dev)
1429{
1430 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001431 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001432
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001433 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001434 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001435
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001436 if (type)
1437 device_remove_groups(dev, type->groups);
1438
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001439 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001440 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001441}
1442
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001443static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001444 char *buf)
1445{
1446 return print_dev_t(buf, dev->devt);
1447}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001448static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001449
Kay Sieversca22e562011-12-14 14:29:38 -08001450/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001451struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001454 * devices_kset_move_before - Move device in the devices_kset's list.
1455 * @deva: Device to move.
1456 * @devb: Device @deva should come before.
1457 */
1458static void devices_kset_move_before(struct device *deva, struct device *devb)
1459{
1460 if (!devices_kset)
1461 return;
1462 pr_debug("devices_kset: Moving %s before %s\n",
1463 dev_name(deva), dev_name(devb));
1464 spin_lock(&devices_kset->list_lock);
1465 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1466 spin_unlock(&devices_kset->list_lock);
1467}
1468
1469/**
1470 * devices_kset_move_after - Move device in the devices_kset's list.
1471 * @deva: Device to move
1472 * @devb: Device @deva should come after.
1473 */
1474static void devices_kset_move_after(struct device *deva, struct device *devb)
1475{
1476 if (!devices_kset)
1477 return;
1478 pr_debug("devices_kset: Moving %s after %s\n",
1479 dev_name(deva), dev_name(devb));
1480 spin_lock(&devices_kset->list_lock);
1481 list_move(&deva->kobj.entry, &devb->kobj.entry);
1482 spin_unlock(&devices_kset->list_lock);
1483}
1484
1485/**
1486 * devices_kset_move_last - move the device to the end of devices_kset's list.
1487 * @dev: device to move
1488 */
1489void devices_kset_move_last(struct device *dev)
1490{
1491 if (!devices_kset)
1492 return;
1493 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1494 spin_lock(&devices_kset->list_lock);
1495 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1496 spin_unlock(&devices_kset->list_lock);
1497}
1498
1499/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001500 * device_create_file - create sysfs attribute file for device.
1501 * @dev: device.
1502 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001504int device_create_file(struct device *dev,
1505 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001508
1509 if (dev) {
1510 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001511 "Attribute %s: write permission without 'store'\n",
1512 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001513 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001514 "Attribute %s: read permission without 'show'\n",
1515 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001517 }
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return error;
1520}
David Graham White86df2682013-07-21 20:41:14 -04001521EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001524 * device_remove_file - remove sysfs attribute file.
1525 * @dev: device.
1526 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001528void device_remove_file(struct device *dev,
1529 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001531 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
David Graham White86df2682013-07-21 20:41:14 -04001534EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001536/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001537 * device_remove_file_self - remove sysfs attribute file from its own method.
1538 * @dev: device.
1539 * @attr: device attribute descriptor.
1540 *
1541 * See kernfs_remove_self() for details.
1542 */
1543bool device_remove_file_self(struct device *dev,
1544 const struct device_attribute *attr)
1545{
1546 if (dev)
1547 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1548 else
1549 return false;
1550}
1551EXPORT_SYMBOL_GPL(device_remove_file_self);
1552
1553/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001554 * device_create_bin_file - create sysfs binary attribute file for device.
1555 * @dev: device.
1556 * @attr: device binary attribute descriptor.
1557 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001558int device_create_bin_file(struct device *dev,
1559 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001560{
1561 int error = -EINVAL;
1562 if (dev)
1563 error = sysfs_create_bin_file(&dev->kobj, attr);
1564 return error;
1565}
1566EXPORT_SYMBOL_GPL(device_create_bin_file);
1567
1568/**
1569 * device_remove_bin_file - remove sysfs binary attribute file
1570 * @dev: device.
1571 * @attr: device binary attribute descriptor.
1572 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001573void device_remove_bin_file(struct device *dev,
1574 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001575{
1576 if (dev)
1577 sysfs_remove_bin_file(&dev->kobj, attr);
1578}
1579EXPORT_SYMBOL_GPL(device_remove_bin_file);
1580
James Bottomley34bb61f2005-09-06 16:56:51 -07001581static void klist_children_get(struct klist_node *n)
1582{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001583 struct device_private *p = to_device_private_parent(n);
1584 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001585
1586 get_device(dev);
1587}
1588
1589static void klist_children_put(struct klist_node *n)
1590{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001591 struct device_private *p = to_device_private_parent(n);
1592 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001593
1594 put_device(dev);
1595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001598 * device_initialize - init device structure.
1599 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 *
Cornelia Huck57394112008-09-03 18:26:40 +02001601 * This prepares the device for use by other layers by initializing
1602 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001603 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001604 * that function, though it can also be called separately, so one
1605 * may use @dev's fields. In particular, get_device()/put_device()
1606 * may be used for reference counting of @dev after calling this
1607 * function.
1608 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001609 * All fields in @dev must be initialized by the caller to 0, except
1610 * for those explicitly set to some other value. The simplest
1611 * approach is to use kzalloc() to allocate the structure containing
1612 * @dev.
1613 *
Cornelia Huck57394112008-09-03 18:26:40 +02001614 * NOTE: Use put_device() to give up your reference instead of freeing
1615 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617void device_initialize(struct device *dev)
1618{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001619 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001620 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001622 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001623 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001624 spin_lock_init(&dev->devres_lock);
1625 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001626 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001627 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001628#ifdef CONFIG_GENERIC_MSI_IRQ
1629 INIT_LIST_HEAD(&dev->msi_list);
1630#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001631 INIT_LIST_HEAD(&dev->links.consumers);
1632 INIT_LIST_HEAD(&dev->links.suppliers);
1633 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
David Graham White86df2682013-07-21 20:41:14 -04001635EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Tejun Heod73ce002013-03-12 11:30:05 -07001637struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001638{
Kay Sievers86406242007-03-14 03:25:56 +01001639 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001640
Kay Sievers86406242007-03-14 03:25:56 +01001641 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001642 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001643 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001644
Kay Sievers86406242007-03-14 03:25:56 +01001645 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001646}
1647
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001648struct class_dir {
1649 struct kobject kobj;
1650 struct class *class;
1651};
1652
1653#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1654
1655static void class_dir_release(struct kobject *kobj)
1656{
1657 struct class_dir *dir = to_class_dir(kobj);
1658 kfree(dir);
1659}
1660
1661static const
1662struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1663{
1664 struct class_dir *dir = to_class_dir(kobj);
1665 return dir->class->ns_type;
1666}
1667
1668static struct kobj_type class_dir_ktype = {
1669 .release = class_dir_release,
1670 .sysfs_ops = &kobj_sysfs_ops,
1671 .child_ns_type = class_dir_child_ns_type
1672};
1673
1674static struct kobject *
1675class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1676{
1677 struct class_dir *dir;
1678 int retval;
1679
1680 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1681 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001682 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001683
1684 dir->class = class;
1685 kobject_init(&dir->kobj, &class_dir_ktype);
1686
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001687 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001688
1689 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1690 if (retval < 0) {
1691 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001692 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001693 }
1694 return &dir->kobj;
1695}
1696
Yijing Wange4a60d12014-11-07 12:05:49 +08001697static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001698
Kay Sieversda231fd2007-11-21 17:29:15 +01001699static struct kobject *get_device_parent(struct device *dev,
1700 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001701{
Kay Sievers86406242007-03-14 03:25:56 +01001702 if (dev->class) {
1703 struct kobject *kobj = NULL;
1704 struct kobject *parent_kobj;
1705 struct kobject *k;
1706
Randy Dunlapead454f2010-09-24 14:36:49 -07001707#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001708 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001709 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001710 if (parent && parent->class == &block_class)
1711 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001712 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001713 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001714#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001715
Kay Sievers86406242007-03-14 03:25:56 +01001716 /*
1717 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001718 * Class-devices with a non class-device as parent, live
1719 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001720 */
1721 if (parent == NULL)
1722 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001723 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001724 return &parent->kobj;
1725 else
1726 parent_kobj = &parent->kobj;
1727
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001728 mutex_lock(&gdp_mutex);
1729
Kay Sievers86406242007-03-14 03:25:56 +01001730 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001731 spin_lock(&dev->class->p->glue_dirs.list_lock);
1732 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001733 if (k->parent == parent_kobj) {
1734 kobj = kobject_get(k);
1735 break;
1736 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001737 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001738 if (kobj) {
1739 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001740 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001741 }
Kay Sievers86406242007-03-14 03:25:56 +01001742
1743 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001744 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001745 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001746 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001747 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001748 }
1749
Kay Sieversca22e562011-12-14 14:29:38 -08001750 /* subsystems can specify a default root directory for their devices */
1751 if (!parent && dev->bus && dev->bus->dev_root)
1752 return &dev->bus->dev_root->kobj;
1753
Kay Sievers86406242007-03-14 03:25:56 +01001754 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001755 return &parent->kobj;
1756 return NULL;
1757}
Kay Sieversda231fd2007-11-21 17:29:15 +01001758
Ming Leicebf8fd2016-07-10 19:27:36 +08001759static inline bool live_in_glue_dir(struct kobject *kobj,
1760 struct device *dev)
1761{
1762 if (!kobj || !dev->class ||
1763 kobj->kset != &dev->class->p->glue_dirs)
1764 return false;
1765 return true;
1766}
1767
1768static inline struct kobject *get_glue_dir(struct device *dev)
1769{
1770 return dev->kobj.parent;
1771}
1772
1773/*
1774 * make sure cleaning up dir as the last step, we need to make
1775 * sure .release handler of kobject is run with holding the
1776 * global lock
1777 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001778static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001779{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001780 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001781 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001782 return;
1783
Yijing Wange4a60d12014-11-07 12:05:49 +08001784 mutex_lock(&gdp_mutex);
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10001785 if (!kobject_has_children(glue_dir))
1786 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001787 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001788 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001789}
Cornelia Huck63b69712008-01-21 16:09:44 +01001790
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001791static int device_add_class_symlinks(struct device *dev)
1792{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001793 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001794 int error;
1795
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001796 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001797 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001798 if (error)
1799 dev_warn(dev, "Error %d creating of_node link\n",error);
1800 /* An error here doesn't warrant bringing down the device */
1801 }
1802
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001803 if (!dev->class)
1804 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001805
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001806 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001807 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001808 "subsystem");
1809 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001810 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001811
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001812 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001813 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1814 "device");
1815 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001816 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001817 }
Kay Sievers39aba962010-09-04 22:33:14 -07001818
Randy Dunlapead454f2010-09-24 14:36:49 -07001819#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001820 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001821 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001822 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001823#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001824
1825 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001826 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001827 &dev->kobj, dev_name(dev));
1828 if (error)
1829 goto out_device;
1830
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001831 return 0;
1832
Kay Sievers39aba962010-09-04 22:33:14 -07001833out_device:
1834 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001835
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001836out_subsys:
1837 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001838out_devnode:
1839 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001840 return error;
1841}
1842
1843static void device_remove_class_symlinks(struct device *dev)
1844{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001845 if (dev_of_node(dev))
1846 sysfs_remove_link(&dev->kobj, "of_node");
1847
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001848 if (!dev->class)
1849 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001850
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001851 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001852 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001853 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001854#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001855 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001856 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001857#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001858 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001859}
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001862 * dev_set_name - set a device name
1863 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001864 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001865 */
1866int dev_set_name(struct device *dev, const char *fmt, ...)
1867{
1868 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001869 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001870
1871 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001872 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001873 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001874 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001875}
1876EXPORT_SYMBOL_GPL(dev_set_name);
1877
1878/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001879 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1880 * @dev: device
1881 *
1882 * By default we select char/ for new entries. Setting class->dev_obj
1883 * to NULL prevents an entry from being created. class->dev_kobj must
1884 * be set (or cleared) before any devices are registered to the class
1885 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001886 * device_remove_sys_dev_entry() will disagree about the presence of
1887 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001888 */
1889static struct kobject *device_to_dev_kobj(struct device *dev)
1890{
1891 struct kobject *kobj;
1892
1893 if (dev->class)
1894 kobj = dev->class->dev_kobj;
1895 else
1896 kobj = sysfs_dev_char_kobj;
1897
1898 return kobj;
1899}
1900
1901static int device_create_sys_dev_entry(struct device *dev)
1902{
1903 struct kobject *kobj = device_to_dev_kobj(dev);
1904 int error = 0;
1905 char devt_str[15];
1906
1907 if (kobj) {
1908 format_dev_t(devt_str, dev->devt);
1909 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1910 }
1911
1912 return error;
1913}
1914
1915static void device_remove_sys_dev_entry(struct device *dev)
1916{
1917 struct kobject *kobj = device_to_dev_kobj(dev);
1918 char devt_str[15];
1919
1920 if (kobj) {
1921 format_dev_t(devt_str, dev->devt);
1922 sysfs_remove_link(kobj, devt_str);
1923 }
1924}
1925
Shaokun Zhang46d3a032018-07-15 18:08:56 +08001926static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001927{
1928 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1929 if (!dev->p)
1930 return -ENOMEM;
1931 dev->p->device = dev;
1932 klist_init(&dev->p->klist_children, klist_children_get,
1933 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001934 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001935 return 0;
1936}
1937
Dan Williamse105b8b2008-04-21 10:51:07 -07001938/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001939 * device_add - add device to device hierarchy.
1940 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001942 * This is part 2 of device_register(), though may be called
1943 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 *
Cornelia Huck57394112008-09-03 18:26:40 +02001945 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001946 * to the global and sibling lists for the device, then
1947 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001948 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001949 * Do not call this routine or device_register() more than once for
1950 * any device structure. The driver model core is not designed to work
1951 * with devices that get unregistered and then spring back to life.
1952 * (Among other things, it's very hard to guarantee that all references
1953 * to the previous incarnation of @dev have been dropped.) Allocate
1954 * and register a fresh new struct device instead.
1955 *
Cornelia Huck57394112008-09-03 18:26:40 +02001956 * NOTE: _Never_ directly free @dev after calling this function, even
1957 * if it returned an error! Always use put_device() to give up your
1958 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
1960int device_add(struct device *dev)
1961{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301962 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001963 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001964 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001965 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001966 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001969 if (!dev)
1970 goto done;
1971
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001972 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001973 error = device_private_init(dev);
1974 if (error)
1975 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001976 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001977
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001978 /*
1979 * for statically allocated devices, which should all be converted
1980 * some day, we need to initialize the name. We prevent reading back
1981 * the name, and force the use of dev_name()
1982 */
1983 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001984 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001985 dev->init_name = NULL;
1986 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001987
Kay Sieversca22e562011-12-14 14:29:38 -08001988 /* subsystems can specify simple device enumeration */
1989 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1990 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1991
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001992 if (!dev_name(dev)) {
1993 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001994 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001997 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08002000 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002001 if (IS_ERR(kobj)) {
2002 error = PTR_ERR(kobj);
2003 goto parent_error;
2004 }
Kay Sieversca22e562011-12-14 14:29:38 -08002005 if (kobj)
2006 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Yinghai Lu0d358f22008-02-19 03:20:41 -08002008 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08002009 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08002010 set_dev_node(dev, dev_to_node(parent));
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07002013 /* we require the name to be set before, and pass NULL */
2014 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08002015 if (error) {
2016 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08002018 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02002019
Brian Walsh37022642006-08-14 22:43:19 -07002020 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002021 error = device_platform_notify(dev, KOBJ_ADD);
2022 if (error)
2023 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07002024
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002025 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02002026 if (error)
2027 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02002028
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002029 error = device_add_class_symlinks(dev);
2030 if (error)
2031 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07002032 error = device_add_attrs(dev);
2033 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002034 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07002035 error = bus_add_device(dev);
2036 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04002038 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01002039 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04002040 goto DPMError;
2041 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05002042
Sergey Klyaus0cd75042014-10-08 11:31:54 +04002043 if (MAJOR(dev->devt)) {
2044 error = device_create_file(dev, &dev_attr_dev);
2045 if (error)
2046 goto DevAttrError;
2047
2048 error = device_create_sys_dev_entry(dev);
2049 if (error)
2050 goto SysEntryError;
2051
2052 devtmpfs_create_node(dev);
2053 }
2054
Alan Sternec0676ee2008-12-05 14:10:31 -05002055 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00002056 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05002057 */
2058 if (dev->bus)
2059 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2060 BUS_NOTIFY_ADD_DEVICE, dev);
2061
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02002062 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04002063 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002065 klist_add_tail(&dev->p->knode_parent,
2066 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07002068 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08002069 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002070 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08002071 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002072 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002073
2074 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002075 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002076 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002077 if (class_intf->add_dev)
2078 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08002079 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07002080 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002081done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 put_device(dev);
2083 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04002084 SysEntryError:
2085 if (MAJOR(dev->devt))
2086 device_remove_file(dev, &dev_attr_dev);
2087 DevAttrError:
2088 device_pm_remove(dev);
2089 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04002090 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01002091 bus_remove_device(dev);
2092 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00002093 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002094 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002095 device_remove_class_symlinks(dev);
2096 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002097 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002098 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002099 device_platform_notify(dev, KOBJ_REMOVE);
2100platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01002101 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002102 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 kobject_del(&dev->kobj);
2104 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08002105 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002106parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01002107 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07002108name_error:
2109 kfree(dev->p);
2110 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002111 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
David Graham White86df2682013-07-21 20:41:14 -04002113EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002116 * device_register - register a device with the system.
2117 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002119 * This happens in two clean steps - initialize the device
2120 * and add it to the system. The two steps can be called
2121 * separately, but this is the easiest and most common.
2122 * I.e. you should only call the two helpers separately if
2123 * have a clearly defined need to use and refcount the device
2124 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02002125 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002126 * For more information, see the kerneldoc for device_initialize()
2127 * and device_add().
2128 *
Cornelia Huck57394112008-09-03 18:26:40 +02002129 * NOTE: _Never_ directly free @dev after calling this function, even
2130 * if it returned an error! Always use put_device() to give up the
2131 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133int device_register(struct device *dev)
2134{
2135 device_initialize(dev);
2136 return device_add(dev);
2137}
David Graham White86df2682013-07-21 20:41:14 -04002138EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002141 * get_device - increment reference count for device.
2142 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002144 * This simply forwards the call to kobject_get(), though
2145 * we do take care to provide for the case that we get a NULL
2146 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002148struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002150 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
David Graham White86df2682013-07-21 20:41:14 -04002152EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002155 * put_device - decrement reference count.
2156 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002158void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02002160 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (dev)
2162 kobject_put(&dev->kobj);
2163}
David Graham White86df2682013-07-21 20:41:14 -04002164EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002167 * device_del - delete device from system.
2168 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002170 * This is the first part of the device unregistration
2171 * sequence. This removes the device from the lists we control
2172 * from here, has it removed from the other driver model
2173 * subsystems it was added to in device_add(), and removes it
2174 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002176 * NOTE: this should be called manually _iff_ device_add() was
2177 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002179void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002181 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002182 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002183 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Alexander Duyck3451a492019-01-22 10:39:10 -08002185 /*
2186 * Hold the device lock and set the "dead" flag to guarantee that
2187 * the update behavior is consistent with the other bitfields near
2188 * it and that we cannot have an asynchronous probe routine trying
2189 * to run while we are tearing out the bus/class/sysfs from
2190 * underneath the device.
2191 */
2192 device_lock(dev);
2193 dev->p->dead = true;
2194 device_unlock(dev);
2195
Alan Sternec0676ee2008-12-05 14:10:31 -05002196 /* Notify clients of device removal. This call must come
2197 * before dpm_sysfs_remove().
2198 */
2199 if (dev->bus)
2200 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2201 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002202
Alan Stern3b98aea2008-08-07 13:06:12 -04002203 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002205 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002206 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002207 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002208 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002209 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002210 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002211 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002212 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002213
Kay Sieversca22e562011-12-14 14:29:38 -08002214 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002215 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002216 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002217 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002218 if (class_intf->remove_dev)
2219 class_intf->remove_dev(dev, class_intf);
2220 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08002221 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002222 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002223 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002224 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002225 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002226 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002227 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002228 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002229 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02002230 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002231 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Joerg Roedel599bad32014-09-30 13:02:02 +02002233 if (dev->bus)
2234 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2235 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002236 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002237 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002239 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002240 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
David Graham White86df2682013-07-21 20:41:14 -04002242EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002245 * device_unregister - unregister device from system.
2246 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002248 * We do this in two parts, like we do device_register(). First,
2249 * we remove it from all the subsystems with device_del(), then
2250 * we decrement the reference count via put_device(). If that
2251 * is the final reference count, the device will be cleaned up
2252 * via device_release() above. Otherwise, the structure will
2253 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002255void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002257 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 device_del(dev);
2259 put_device(dev);
2260}
David Graham White86df2682013-07-21 20:41:14 -04002261EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002263static struct device *prev_device(struct klist_iter *i)
2264{
2265 struct klist_node *n = klist_prev(i);
2266 struct device *dev = NULL;
2267 struct device_private *p;
2268
2269 if (n) {
2270 p = to_device_private_parent(n);
2271 dev = p->device;
2272 }
2273 return dev;
2274}
2275
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002276static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002277{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002278 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002279 struct device *dev = NULL;
2280 struct device_private *p;
2281
2282 if (n) {
2283 p = to_device_private_parent(n);
2284 dev = p->device;
2285 }
2286 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002287}
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002290 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002291 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002292 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002293 * @uid: returned file owner
2294 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002295 * @tmp: possibly allocated string
2296 *
2297 * Return the relative path of a possible device node.
2298 * Non-default names may need to allocate a memory to compose
2299 * a name. This memory is returned in tmp and needs to be
2300 * freed by the caller.
2301 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002302const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002303 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002304 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002305{
2306 char *s;
2307
2308 *tmp = NULL;
2309
2310 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002311 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002312 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002313 if (*tmp)
2314 return *tmp;
2315
2316 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002317 if (dev->class && dev->class->devnode)
2318 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002319 if (*tmp)
2320 return *tmp;
2321
2322 /* return name without allocation, tmp == NULL */
2323 if (strchr(dev_name(dev), '!') == NULL)
2324 return dev_name(dev);
2325
2326 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002327 s = kstrdup(dev_name(dev), GFP_KERNEL);
2328 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002329 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002330 strreplace(s, '!', '/');
2331 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002332}
2333
2334/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002335 * device_for_each_child - device child iterator.
2336 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002337 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002338 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002340 * Iterate over @parent's child devices, and call @fn for each,
2341 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002343 * We check the return of @fn each time. If it returns anything
2344 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002346int device_for_each_child(struct device *parent, void *data,
2347 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002349 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002350 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int error = 0;
2352
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002353 if (!parent->p)
2354 return 0;
2355
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002356 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002357 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002358 error = fn(child, data);
2359 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return error;
2361}
David Graham White86df2682013-07-21 20:41:14 -04002362EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Cornelia Huck5ab69982006-11-16 15:42:07 +01002364/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002365 * device_for_each_child_reverse - device child iterator in reversed order.
2366 * @parent: parent struct device.
2367 * @fn: function to be called for each device.
2368 * @data: data for the callback.
2369 *
2370 * Iterate over @parent's child devices, and call @fn for each,
2371 * passing it @data.
2372 *
2373 * We check the return of @fn each time. If it returns anything
2374 * other than 0, we break out and return that value.
2375 */
2376int device_for_each_child_reverse(struct device *parent, void *data,
2377 int (*fn)(struct device *dev, void *data))
2378{
2379 struct klist_iter i;
2380 struct device *child;
2381 int error = 0;
2382
2383 if (!parent->p)
2384 return 0;
2385
2386 klist_iter_init(&parent->p->klist_children, &i);
2387 while ((child = prev_device(&i)) && !error)
2388 error = fn(child, data);
2389 klist_iter_exit(&i);
2390 return error;
2391}
2392EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2393
2394/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002395 * device_find_child - device iterator for locating a particular device.
2396 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002397 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002398 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002399 *
2400 * This is similar to the device_for_each_child() function above, but it
2401 * returns a reference to a device that is 'found' for later use, as
2402 * determined by the @match callback.
2403 *
2404 * The callback should return 0 if the device doesn't match and non-zero
2405 * if it does. If the callback returns non-zero and a reference to the
2406 * current device can be obtained, this function will return to the caller
2407 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002408 *
2409 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002410 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002411struct device *device_find_child(struct device *parent, void *data,
2412 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002413{
2414 struct klist_iter i;
2415 struct device *child;
2416
2417 if (!parent)
2418 return NULL;
2419
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002420 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002421 while ((child = next_device(&i)))
2422 if (match(child, data) && get_device(child))
2423 break;
2424 klist_iter_exit(&i);
2425 return child;
2426}
David Graham White86df2682013-07-21 20:41:14 -04002427EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429int __init devices_init(void)
2430{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002431 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2432 if (!devices_kset)
2433 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002434 dev_kobj = kobject_create_and_add("dev", NULL);
2435 if (!dev_kobj)
2436 goto dev_kobj_err;
2437 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2438 if (!sysfs_dev_block_kobj)
2439 goto block_kobj_err;
2440 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2441 if (!sysfs_dev_char_kobj)
2442 goto char_kobj_err;
2443
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002444 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002445
2446 char_kobj_err:
2447 kobject_put(sysfs_dev_block_kobj);
2448 block_kobj_err:
2449 kobject_put(dev_kobj);
2450 dev_kobj_err:
2451 kset_unregister(devices_kset);
2452 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002455static int device_check_offline(struct device *dev, void *not_used)
2456{
2457 int ret;
2458
2459 ret = device_for_each_child(dev, NULL, device_check_offline);
2460 if (ret)
2461 return ret;
2462
2463 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2464}
2465
2466/**
2467 * device_offline - Prepare the device for hot-removal.
2468 * @dev: Device to be put offline.
2469 *
2470 * Execute the device bus type's .offline() callback, if present, to prepare
2471 * the device for a subsequent hot-removal. If that succeeds, the device must
2472 * not be used until either it is removed or its bus type's .online() callback
2473 * is executed.
2474 *
2475 * Call under device_hotplug_lock.
2476 */
2477int device_offline(struct device *dev)
2478{
2479 int ret;
2480
2481 if (dev->offline_disabled)
2482 return -EPERM;
2483
2484 ret = device_for_each_child(dev, NULL, device_check_offline);
2485 if (ret)
2486 return ret;
2487
2488 device_lock(dev);
2489 if (device_supports_offline(dev)) {
2490 if (dev->offline) {
2491 ret = 1;
2492 } else {
2493 ret = dev->bus->offline(dev);
2494 if (!ret) {
2495 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2496 dev->offline = true;
2497 }
2498 }
2499 }
2500 device_unlock(dev);
2501
2502 return ret;
2503}
2504
2505/**
2506 * device_online - Put the device back online after successful device_offline().
2507 * @dev: Device to be put back online.
2508 *
2509 * If device_offline() has been successfully executed for @dev, but the device
2510 * has not been removed subsequently, execute its bus type's .online() callback
2511 * to indicate that the device can be used again.
2512 *
2513 * Call under device_hotplug_lock.
2514 */
2515int device_online(struct device *dev)
2516{
2517 int ret = 0;
2518
2519 device_lock(dev);
2520 if (device_supports_offline(dev)) {
2521 if (dev->offline) {
2522 ret = dev->bus->online(dev);
2523 if (!ret) {
2524 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2525 dev->offline = false;
2526 }
2527 } else {
2528 ret = 1;
2529 }
2530 }
2531 device_unlock(dev);
2532
2533 return ret;
2534}
2535
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002536struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002537 struct device dev;
2538 struct module *owner;
2539};
2540
Josh Triplett93058422012-11-18 21:27:55 -08002541static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002542{
2543 return container_of(d, struct root_device, dev);
2544}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002545
2546static void root_device_release(struct device *dev)
2547{
2548 kfree(to_root_device(dev));
2549}
2550
2551/**
2552 * __root_device_register - allocate and register a root device
2553 * @name: root device name
2554 * @owner: owner module of the root device, usually THIS_MODULE
2555 *
2556 * This function allocates a root device and registers it
2557 * using device_register(). In order to free the returned
2558 * device, use root_device_unregister().
2559 *
2560 * Root devices are dummy devices which allow other devices
2561 * to be grouped under /sys/devices. Use this function to
2562 * allocate a root device and then use it as the parent of
2563 * any device which should appear under /sys/devices/{name}
2564 *
2565 * The /sys/devices/{name} directory will also contain a
2566 * 'module' symlink which points to the @owner directory
2567 * in sysfs.
2568 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002569 * Returns &struct device pointer on success, or ERR_PTR() on error.
2570 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002571 * Note: You probably want to use root_device_register().
2572 */
2573struct device *__root_device_register(const char *name, struct module *owner)
2574{
2575 struct root_device *root;
2576 int err = -ENOMEM;
2577
2578 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2579 if (!root)
2580 return ERR_PTR(err);
2581
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002582 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002583 if (err) {
2584 kfree(root);
2585 return ERR_PTR(err);
2586 }
2587
2588 root->dev.release = root_device_release;
2589
2590 err = device_register(&root->dev);
2591 if (err) {
2592 put_device(&root->dev);
2593 return ERR_PTR(err);
2594 }
2595
Christoph Egger1d9e8822010-05-17 16:57:58 +02002596#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002597 if (owner) {
2598 struct module_kobject *mk = &owner->mkobj;
2599
2600 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2601 if (err) {
2602 device_unregister(&root->dev);
2603 return ERR_PTR(err);
2604 }
2605 root->owner = owner;
2606 }
2607#endif
2608
2609 return &root->dev;
2610}
2611EXPORT_SYMBOL_GPL(__root_device_register);
2612
2613/**
2614 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002615 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002616 *
2617 * This function unregisters and cleans up a device that was created by
2618 * root_device_register().
2619 */
2620void root_device_unregister(struct device *dev)
2621{
2622 struct root_device *root = to_root_device(dev);
2623
2624 if (root->owner)
2625 sysfs_remove_link(&root->dev.kobj, "module");
2626
2627 device_unregister(dev);
2628}
2629EXPORT_SYMBOL_GPL(root_device_unregister);
2630
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002631
2632static void device_create_release(struct device *dev)
2633{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002634 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002635 kfree(dev);
2636}
2637
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002638static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002639device_create_groups_vargs(struct class *class, struct device *parent,
2640 dev_t devt, void *drvdata,
2641 const struct attribute_group **groups,
2642 const char *fmt, va_list args)
2643{
2644 struct device *dev = NULL;
2645 int retval = -ENODEV;
2646
2647 if (class == NULL || IS_ERR(class))
2648 goto error;
2649
2650 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2651 if (!dev) {
2652 retval = -ENOMEM;
2653 goto error;
2654 }
2655
David Herrmannbbc780f2013-11-21 20:15:48 +01002656 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002657 dev->devt = devt;
2658 dev->class = class;
2659 dev->parent = parent;
2660 dev->groups = groups;
2661 dev->release = device_create_release;
2662 dev_set_drvdata(dev, drvdata);
2663
2664 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2665 if (retval)
2666 goto error;
2667
David Herrmannbbc780f2013-11-21 20:15:48 +01002668 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002669 if (retval)
2670 goto error;
2671
2672 return dev;
2673
2674error:
2675 put_device(dev);
2676 return ERR_PTR(retval);
2677}
2678
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002679/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002680 * device_create_vargs - creates a device and registers it with sysfs
2681 * @class: pointer to the struct class that this device should be registered to
2682 * @parent: pointer to the parent struct device of this new device, if any
2683 * @devt: the dev_t for the char device to be added
2684 * @drvdata: the data to be added to the device for callbacks
2685 * @fmt: string for the device's name
2686 * @args: va_list for the device's name
2687 *
2688 * This function can be used by char device classes. A struct device
2689 * will be created in sysfs, registered to the specified class.
2690 *
2691 * A "dev" file will be created, showing the dev_t for the device, if
2692 * the dev_t is not 0,0.
2693 * If a pointer to a parent struct device is passed in, the newly created
2694 * struct device will be a child of that device in sysfs.
2695 * The pointer to the struct device will be returned from the call.
2696 * Any further sysfs files that might be required can be created using this
2697 * pointer.
2698 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002699 * Returns &struct device pointer on success, or ERR_PTR() on error.
2700 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002701 * Note: the struct class passed to this function must have previously
2702 * been created with a call to class_create().
2703 */
2704struct device *device_create_vargs(struct class *class, struct device *parent,
2705 dev_t devt, void *drvdata, const char *fmt,
2706 va_list args)
2707{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002708 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2709 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002710}
2711EXPORT_SYMBOL_GPL(device_create_vargs);
2712
2713/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002714 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002715 * @class: pointer to the struct class that this device should be registered to
2716 * @parent: pointer to the parent struct device of this new device, if any
2717 * @devt: the dev_t for the char device to be added
2718 * @drvdata: the data to be added to the device for callbacks
2719 * @fmt: string for the device's name
2720 *
2721 * This function can be used by char device classes. A struct device
2722 * will be created in sysfs, registered to the specified class.
2723 *
2724 * A "dev" file will be created, showing the dev_t for the device, if
2725 * the dev_t is not 0,0.
2726 * If a pointer to a parent struct device is passed in, the newly created
2727 * struct device will be a child of that device in sysfs.
2728 * The pointer to the struct device will be returned from the call.
2729 * Any further sysfs files that might be required can be created using this
2730 * pointer.
2731 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002732 * Returns &struct device pointer on success, or ERR_PTR() on error.
2733 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002734 * Note: the struct class passed to this function must have previously
2735 * been created with a call to class_create().
2736 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002737struct device *device_create(struct class *class, struct device *parent,
2738 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002739{
2740 va_list vargs;
2741 struct device *dev;
2742
2743 va_start(vargs, fmt);
2744 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2745 va_end(vargs);
2746 return dev;
2747}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002748EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002749
Guenter Roeck39ef3112013-07-14 16:05:57 -07002750/**
2751 * device_create_with_groups - creates a device and registers it with sysfs
2752 * @class: pointer to the struct class that this device should be registered to
2753 * @parent: pointer to the parent struct device of this new device, if any
2754 * @devt: the dev_t for the char device to be added
2755 * @drvdata: the data to be added to the device for callbacks
2756 * @groups: NULL-terminated list of attribute groups to be created
2757 * @fmt: string for the device's name
2758 *
2759 * This function can be used by char device classes. A struct device
2760 * will be created in sysfs, registered to the specified class.
2761 * Additional attributes specified in the groups parameter will also
2762 * be created automatically.
2763 *
2764 * A "dev" file will be created, showing the dev_t for the device, if
2765 * the dev_t is not 0,0.
2766 * If a pointer to a parent struct device is passed in, the newly created
2767 * struct device will be a child of that device in sysfs.
2768 * The pointer to the struct device will be returned from the call.
2769 * Any further sysfs files that might be required can be created using this
2770 * pointer.
2771 *
2772 * Returns &struct device pointer on success, or ERR_PTR() on error.
2773 *
2774 * Note: the struct class passed to this function must have previously
2775 * been created with a call to class_create().
2776 */
2777struct device *device_create_with_groups(struct class *class,
2778 struct device *parent, dev_t devt,
2779 void *drvdata,
2780 const struct attribute_group **groups,
2781 const char *fmt, ...)
2782{
2783 va_list vargs;
2784 struct device *dev;
2785
2786 va_start(vargs, fmt);
2787 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2788 fmt, vargs);
2789 va_end(vargs);
2790 return dev;
2791}
2792EXPORT_SYMBOL_GPL(device_create_with_groups);
2793
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002794static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002795{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002796 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002797
Dave Youngcd354492008-01-28 16:56:11 +08002798 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002799}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002800
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002801/**
2802 * device_destroy - removes a device that was created with device_create()
2803 * @class: pointer to the struct class that this device was registered with
2804 * @devt: the dev_t of the device that was previously registered
2805 *
2806 * This call unregisters and cleans up a device that was created with a
2807 * call to device_create().
2808 */
2809void device_destroy(struct class *class, dev_t devt)
2810{
2811 struct device *dev;
2812
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002813 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002814 if (dev) {
2815 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002816 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002817 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002818}
2819EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002820
2821/**
2822 * device_rename - renames a device
2823 * @dev: the pointer to the struct device to be renamed
2824 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002825 *
2826 * It is the responsibility of the caller to provide mutual
2827 * exclusion between two different calls of device_rename
2828 * on the same device to ensure that new_name is valid and
2829 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002830 *
Timur Tabia5462512010-12-13 14:08:52 -06002831 * Note: Don't call this function. Currently, the networking layer calls this
2832 * function, but that will change. The following text from Kay Sievers offers
2833 * some insight:
2834 *
2835 * Renaming devices is racy at many levels, symlinks and other stuff are not
2836 * replaced atomically, and you get a "move" uevent, but it's not easy to
2837 * connect the event to the old and new device. Device nodes are not renamed at
2838 * all, there isn't even support for that in the kernel now.
2839 *
2840 * In the meantime, during renaming, your target name might be taken by another
2841 * driver, creating conflicts. Or the old name is taken directly after you
2842 * renamed it -- then you get events for the same DEVPATH, before you even see
2843 * the "move" event. It's just a mess, and nothing new should ever rely on
2844 * kernel device renaming. Besides that, it's not even implemented now for
2845 * other things than (driver-core wise very simple) network devices.
2846 *
2847 * We are currently about to change network renaming in udev to completely
2848 * disallow renaming of devices in the same namespace as the kernel uses,
2849 * because we can't solve the problems properly, that arise with swapping names
2850 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2851 * be allowed to some other name than eth[0-9]*, for the aforementioned
2852 * reasons.
2853 *
2854 * Make up a "real" name in the driver before you register anything, or add
2855 * some other attributes for userspace to find the device, or use udev to add
2856 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2857 * don't even want to get into that and try to implement the missing pieces in
2858 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002859 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002860int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002861{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002862 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002863 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002864 int error;
2865
2866 dev = get_device(dev);
2867 if (!dev)
2868 return -EINVAL;
2869
ethan.zhao69df7532013-10-13 22:12:35 +08002870 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002871
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002872 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002873 if (!old_device_name) {
2874 error = -ENOMEM;
2875 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002876 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002877
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002878 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002879 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2880 kobj, old_device_name,
2881 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002882 if (error)
2883 goto out;
2884 }
Kay Sievers39aba962010-09-04 22:33:14 -07002885
Tejun Heo4b30ee52013-09-11 22:29:06 -04002886 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002887 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002888 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002889
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002890out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002891 put_device(dev);
2892
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002893 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002894
2895 return error;
2896}
Johannes Berga2807db2007-02-28 12:38:31 +01002897EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002898
2899static int device_move_class_links(struct device *dev,
2900 struct device *old_parent,
2901 struct device *new_parent)
2902{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002903 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002904
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002905 if (old_parent)
2906 sysfs_remove_link(&dev->kobj, "device");
2907 if (new_parent)
2908 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2909 "device");
2910 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002911}
2912
2913/**
2914 * device_move - moves a device to a new parent
2915 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02002916 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002917 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002918 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002919int device_move(struct device *dev, struct device *new_parent,
2920 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002921{
2922 int error;
2923 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002924 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002925
2926 dev = get_device(dev);
2927 if (!dev)
2928 return -EINVAL;
2929
Cornelia Huckffa6a702009-03-04 12:44:00 +01002930 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002931 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002932 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002933 if (IS_ERR(new_parent_kobj)) {
2934 error = PTR_ERR(new_parent_kobj);
2935 put_device(new_parent);
2936 goto out;
2937 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002938
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002939 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2940 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002941 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002942 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002943 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002944 put_device(new_parent);
2945 goto out;
2946 }
2947 old_parent = dev->parent;
2948 dev->parent = new_parent;
2949 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002950 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002951 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002952 klist_add_tail(&dev->p->knode_parent,
2953 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002954 set_dev_node(dev, dev_to_node(new_parent));
2955 }
2956
Rabin Vincentbdd40342012-04-23 09:16:36 +02002957 if (dev->class) {
2958 error = device_move_class_links(dev, old_parent, new_parent);
2959 if (error) {
2960 /* We ignore errors on cleanup since we're hosed anyway... */
2961 device_move_class_links(dev, new_parent, old_parent);
2962 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2963 if (new_parent)
2964 klist_remove(&dev->p->knode_parent);
2965 dev->parent = old_parent;
2966 if (old_parent) {
2967 klist_add_tail(&dev->p->knode_parent,
2968 &old_parent->p->klist_children);
2969 set_dev_node(dev, dev_to_node(old_parent));
2970 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002971 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002972 cleanup_glue_dir(dev, new_parent_kobj);
2973 put_device(new_parent);
2974 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002975 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002976 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002977 switch (dpm_order) {
2978 case DPM_ORDER_NONE:
2979 break;
2980 case DPM_ORDER_DEV_AFTER_PARENT:
2981 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002982 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002983 break;
2984 case DPM_ORDER_PARENT_BEFORE_DEV:
2985 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002986 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002987 break;
2988 case DPM_ORDER_DEV_LAST:
2989 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002990 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002991 break;
2992 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002993
Cornelia Huck8a824722006-11-20 17:07:51 +01002994 put_device(old_parent);
2995out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002996 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002997 put_device(dev);
2998 return error;
2999}
Cornelia Huck8a824722006-11-20 17:07:51 +01003000EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003001
3002/**
3003 * device_shutdown - call ->shutdown() on each device to shutdown.
3004 */
3005void device_shutdown(void)
3006{
Benson Leungf123db82013-09-24 20:05:11 -07003007 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003008
Pingfan Liu3297c8f2018-07-19 13:14:58 +08003009 wait_for_device_probe();
3010 device_block_probing();
3011
Hugh Daschbach62458382010-03-22 10:36:37 -07003012 spin_lock(&devices_kset->list_lock);
3013 /*
3014 * Walk the devices list backward, shutting down each in turn.
3015 * Beware that device unplug events may also start pulling
3016 * devices offline, even as the system is shutting down.
3017 */
3018 while (!list_empty(&devices_kset->list)) {
3019 dev = list_entry(devices_kset->list.prev, struct device,
3020 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08003021
3022 /*
3023 * hold reference count of device's parent to
3024 * prevent it from being freed because parent's
3025 * lock is to be held
3026 */
Benson Leungf123db82013-09-24 20:05:11 -07003027 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07003028 get_device(dev);
3029 /*
3030 * Make sure the device is off the kset list, in the
3031 * event that dev->*->shutdown() doesn't remove it.
3032 */
3033 list_del_init(&dev->kobj.entry);
3034 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01003035
Ming Leid1c6c032012-06-22 18:01:40 +08003036 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07003037 if (parent)
3038 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08003039 device_lock(dev);
3040
Alan Sternfe6b91f2011-12-06 23:24:52 +01003041 /* Don't allow any more runtime suspends */
3042 pm_runtime_get_noresume(dev);
3043 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07003044
Michal Suchanek75216212017-08-11 15:44:43 +02003045 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07003046 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02003047 dev_info(dev, "shutdown_pre\n");
3048 dev->class->shutdown_pre(dev);
3049 }
3050 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08003051 if (initcall_debug)
3052 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003053 dev->bus->shutdown(dev);
3054 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08003055 if (initcall_debug)
3056 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003057 dev->driver->shutdown(dev);
3058 }
Ming Leid1c6c032012-06-22 18:01:40 +08003059
3060 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07003061 if (parent)
3062 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08003063
Hugh Daschbach62458382010-03-22 10:36:37 -07003064 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07003065 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07003066
3067 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003068 }
Hugh Daschbach62458382010-03-22 10:36:37 -07003069 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003070}
Joe Perches99bcf212010-06-27 01:02:34 +00003071
3072/*
3073 * Device logging functions
3074 */
3075
3076#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07003077static int
3078create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00003079{
Kay Sieversc4e00da2012-05-03 02:29:59 +02003080 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07003081 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003082
Kay Sieversc4e00da2012-05-03 02:29:59 +02003083 if (dev->class)
3084 subsys = dev->class->name;
3085 else if (dev->bus)
3086 subsys = dev->bus->name;
3087 else
Joe Perches798efc62012-09-12 20:11:29 -07003088 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003089
Joe Perches798efc62012-09-12 20:11:29 -07003090 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07003091 if (pos >= hdrlen)
3092 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003093
3094 /*
3095 * Add device identifier DEVICE=:
3096 * b12:8 block dev_t
3097 * c127:3 char dev_t
3098 * n8 netdev ifindex
3099 * +sound:card0 subsystem:devname
3100 */
3101 if (MAJOR(dev->devt)) {
3102 char c;
3103
3104 if (strcmp(subsys, "block") == 0)
3105 c = 'b';
3106 else
3107 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07003108 pos++;
3109 pos += snprintf(hdr + pos, hdrlen - pos,
3110 "DEVICE=%c%u:%u",
3111 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003112 } else if (strcmp(subsys, "net") == 0) {
3113 struct net_device *net = to_net_dev(dev);
3114
Joe Perches798efc62012-09-12 20:11:29 -07003115 pos++;
3116 pos += snprintf(hdr + pos, hdrlen - pos,
3117 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02003118 } else {
Joe Perches798efc62012-09-12 20:11:29 -07003119 pos++;
3120 pos += snprintf(hdr + pos, hdrlen - pos,
3121 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003122 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06003123
Ben Hutchings655e5b72014-08-26 00:34:44 -07003124 if (pos >= hdrlen)
3125 goto overflow;
3126
Joe Perches798efc62012-09-12 20:11:29 -07003127 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07003128
3129overflow:
3130 dev_WARN(dev, "device/subsystem name too long");
3131 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003132}
Joe Perches798efc62012-09-12 20:11:29 -07003133
Joe Perches05e4e5b2012-09-12 20:13:37 -07003134int dev_vprintk_emit(int level, const struct device *dev,
3135 const char *fmt, va_list args)
3136{
3137 char hdr[128];
3138 size_t hdrlen;
3139
3140 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3141
3142 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3143}
3144EXPORT_SYMBOL(dev_vprintk_emit);
3145
3146int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3147{
3148 va_list args;
3149 int r;
3150
3151 va_start(args, fmt);
3152
3153 r = dev_vprintk_emit(level, dev, fmt, args);
3154
3155 va_end(args);
3156
3157 return r;
3158}
3159EXPORT_SYMBOL(dev_printk_emit);
3160
Joe Perchesd1f10522014-12-25 15:07:04 -08003161static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07003162 struct va_format *vaf)
3163{
Joe Perchesd1f10522014-12-25 15:07:04 -08003164 if (dev)
3165 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3166 dev_driver_string(dev), dev_name(dev), vaf);
3167 else
3168 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003169}
Joe Perches99bcf212010-06-27 01:02:34 +00003170
Joe Perchesd1f10522014-12-25 15:07:04 -08003171void dev_printk(const char *level, const struct device *dev,
3172 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00003173{
3174 struct va_format vaf;
3175 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00003176
3177 va_start(args, fmt);
3178
3179 vaf.fmt = fmt;
3180 vaf.va = &args;
3181
Joe Perchesd1f10522014-12-25 15:07:04 -08003182 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003183
Joe Perches99bcf212010-06-27 01:02:34 +00003184 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00003185}
3186EXPORT_SYMBOL(dev_printk);
3187
3188#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08003189void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00003190{ \
3191 struct va_format vaf; \
3192 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003193 \
3194 va_start(args, fmt); \
3195 \
3196 vaf.fmt = fmt; \
3197 vaf.va = &args; \
3198 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003199 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003200 \
Joe Perches99bcf212010-06-27 01:02:34 +00003201 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003202} \
3203EXPORT_SYMBOL(func);
3204
Joe Perches663336e2018-05-09 08:15:46 -07003205define_dev_printk_level(_dev_emerg, KERN_EMERG);
3206define_dev_printk_level(_dev_alert, KERN_ALERT);
3207define_dev_printk_level(_dev_crit, KERN_CRIT);
3208define_dev_printk_level(_dev_err, KERN_ERR);
3209define_dev_printk_level(_dev_warn, KERN_WARNING);
3210define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00003211define_dev_printk_level(_dev_info, KERN_INFO);
3212
3213#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003214
3215static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3216{
3217 return fwnode && !IS_ERR(fwnode->secondary);
3218}
3219
3220/**
3221 * set_primary_fwnode - Change the primary firmware node of a given device.
3222 * @dev: Device to handle.
3223 * @fwnode: New primary firmware node of the device.
3224 *
3225 * Set the device's firmware node pointer to @fwnode, but if a secondary
3226 * firmware node of the device is present, preserve it.
3227 */
3228void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3229{
3230 if (fwnode) {
3231 struct fwnode_handle *fn = dev->fwnode;
3232
3233 if (fwnode_is_primary(fn))
3234 fn = fn->secondary;
3235
Mika Westerberg55f89a82015-11-30 17:11:39 +02003236 if (fn) {
3237 WARN_ON(fwnode->secondary);
3238 fwnode->secondary = fn;
3239 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003240 dev->fwnode = fwnode;
3241 } else {
3242 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3243 dev->fwnode->secondary : NULL;
3244 }
3245}
3246EXPORT_SYMBOL_GPL(set_primary_fwnode);
3247
3248/**
3249 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3250 * @dev: Device to handle.
3251 * @fwnode: New secondary firmware node of the device.
3252 *
3253 * If a primary firmware node of the device is present, set its secondary
3254 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3255 * @fwnode.
3256 */
3257void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3258{
3259 if (fwnode)
3260 fwnode->secondary = ERR_PTR(-ENODEV);
3261
3262 if (fwnode_is_primary(dev->fwnode))
3263 dev->fwnode->secondary = fwnode;
3264 else
3265 dev->fwnode = fwnode;
3266}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003267
3268/**
3269 * device_set_of_node_from_dev - reuse device-tree node of another device
3270 * @dev: device whose device-tree node is being set
3271 * @dev2: device whose device-tree node is being reused
3272 *
3273 * Takes another reference to the new device-tree node after first dropping
3274 * any reference held to the old node.
3275 */
3276void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3277{
3278 of_node_put(dev->of_node);
3279 dev->of_node = of_node_get(dev2->of_node);
3280 dev->of_node_reused = true;
3281}
3282EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);