blob: 12ebd055724cd6cdc01edff013583f38979fe92a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/core.c - core driver model code (device registration, etc)
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020015#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070020#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100021#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070022#include <linux/of.h>
23#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010024#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080025#include <linux/kallsyms.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070026#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010027#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020028#include <linux/netdevice.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010029#include <linux/sched/signal.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070030#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include "base.h"
33#include "power/power.h"
34
Andi Kleene52eec12010-09-08 16:54:17 +020035#ifdef CONFIG_SYSFS_DEPRECATED
36#ifdef CONFIG_SYSFS_DEPRECATED_V2
37long sysfs_deprecated = 1;
38#else
39long sysfs_deprecated = 0;
40#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080041static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020042{
Jingoo Han34da5e62013-07-26 13:10:22 +090043 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020044}
45early_param("sysfs.deprecated", sysfs_deprecated_setup);
46#endif
47
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010048/* Device links support. */
49
50#ifdef CONFIG_SRCU
51static DEFINE_MUTEX(device_links_lock);
52DEFINE_STATIC_SRCU(device_links_srcu);
53
54static inline void device_links_write_lock(void)
55{
56 mutex_lock(&device_links_lock);
57}
58
59static inline void device_links_write_unlock(void)
60{
61 mutex_unlock(&device_links_lock);
62}
63
64int device_links_read_lock(void)
65{
66 return srcu_read_lock(&device_links_srcu);
67}
68
69void device_links_read_unlock(int idx)
70{
71 srcu_read_unlock(&device_links_srcu, idx);
72}
73#else /* !CONFIG_SRCU */
74static DECLARE_RWSEM(device_links_lock);
75
76static inline void device_links_write_lock(void)
77{
78 down_write(&device_links_lock);
79}
80
81static inline void device_links_write_unlock(void)
82{
83 up_write(&device_links_lock);
84}
85
86int device_links_read_lock(void)
87{
88 down_read(&device_links_lock);
89 return 0;
90}
91
92void device_links_read_unlock(int not_used)
93{
94 up_read(&device_links_lock);
95}
96#endif /* !CONFIG_SRCU */
97
98/**
99 * device_is_dependent - Check if one device depends on another one
100 * @dev: Device to check dependencies for.
101 * @target: Device to check against.
102 *
103 * Check if @target depends on @dev or any device dependent on it (its child or
104 * its consumer etc). Return 1 if that is the case or 0 otherwise.
105 */
106static int device_is_dependent(struct device *dev, void *target)
107{
108 struct device_link *link;
109 int ret;
110
111 if (WARN_ON(dev == target))
112 return 1;
113
114 ret = device_for_each_child(dev, target, device_is_dependent);
115 if (ret)
116 return ret;
117
118 list_for_each_entry(link, &dev->links.consumers, s_node) {
119 if (WARN_ON(link->consumer == target))
120 return 1;
121
122 ret = device_is_dependent(link->consumer, target);
123 if (ret)
124 break;
125 }
126 return ret;
127}
128
129static int device_reorder_to_tail(struct device *dev, void *not_used)
130{
131 struct device_link *link;
132
133 /*
134 * Devices that have not been registered yet will be put to the ends
135 * of the lists during the registration, so skip them here.
136 */
137 if (device_is_registered(dev))
138 devices_kset_move_last(dev);
139
140 if (device_pm_initialized(dev))
141 device_pm_move_last(dev);
142
143 device_for_each_child(dev, NULL, device_reorder_to_tail);
144 list_for_each_entry(link, &dev->links.consumers, s_node)
145 device_reorder_to_tail(link->consumer, NULL);
146
147 return 0;
148}
149
150/**
151 * device_link_add - Create a link between two devices.
152 * @consumer: Consumer end of the link.
153 * @supplier: Supplier end of the link.
154 * @flags: Link flags.
155 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100156 * The caller is responsible for the proper synchronization of the link creation
157 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
158 * runtime PM framework to take the link into account. Second, if the
159 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
160 * be forced into the active metastate and reference-counted upon the creation
161 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
162 * ignored.
163 *
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100164 * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically
165 * when the consumer device driver unbinds from it. The combination of both
166 * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL
167 * to be returned.
168 *
169 * A side effect of the link creation is re-ordering of dpm_list and the
170 * devices_kset list by moving the consumer device and all devices depending
171 * on it to the ends of these lists (that does not happen to devices that have
172 * not been registered when this function is called).
173 *
174 * The supplier device is required to be registered when this function is called
175 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100176 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100177 */
178struct device_link *device_link_add(struct device *consumer,
179 struct device *supplier, u32 flags)
180{
181 struct device_link *link;
182
183 if (!consumer || !supplier ||
184 ((flags & DL_FLAG_STATELESS) && (flags & DL_FLAG_AUTOREMOVE)))
185 return NULL;
186
187 device_links_write_lock();
188 device_pm_lock();
189
190 /*
191 * If the supplier has not been fully registered yet or there is a
192 * reverse dependency between the consumer and the supplier already in
193 * the graph, return NULL.
194 */
195 if (!device_pm_initialized(supplier)
196 || device_is_dependent(consumer, supplier)) {
197 link = NULL;
198 goto out;
199 }
200
201 list_for_each_entry(link, &supplier->links.consumers, s_node)
202 if (link->consumer == consumer)
203 goto out;
204
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100205 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100206 if (!link)
207 goto out;
208
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100209 if (flags & DL_FLAG_PM_RUNTIME) {
210 if (flags & DL_FLAG_RPM_ACTIVE) {
211 if (pm_runtime_get_sync(supplier) < 0) {
212 pm_runtime_put_noidle(supplier);
213 kfree(link);
214 link = NULL;
215 goto out;
216 }
217 link->rpm_active = true;
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100218 }
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100219 pm_runtime_new_link(consumer);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100220 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100221 get_device(supplier);
222 link->supplier = supplier;
223 INIT_LIST_HEAD(&link->s_node);
224 get_device(consumer);
225 link->consumer = consumer;
226 INIT_LIST_HEAD(&link->c_node);
227 link->flags = flags;
228
Lukas Wunner64df1142016-12-04 13:10:04 +0100229 /* Determine the initial link state. */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100230 if (flags & DL_FLAG_STATELESS) {
231 link->status = DL_STATE_NONE;
232 } else {
233 switch (supplier->links.status) {
234 case DL_DEV_DRIVER_BOUND:
235 switch (consumer->links.status) {
236 case DL_DEV_PROBING:
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100237 /*
238 * Balance the decrementation of the supplier's
239 * runtime PM usage counter after consumer probe
240 * in driver_probe_device().
241 */
242 if (flags & DL_FLAG_PM_RUNTIME)
243 pm_runtime_get_sync(supplier);
244
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100245 link->status = DL_STATE_CONSUMER_PROBE;
246 break;
247 case DL_DEV_DRIVER_BOUND:
248 link->status = DL_STATE_ACTIVE;
249 break;
250 default:
251 link->status = DL_STATE_AVAILABLE;
252 break;
253 }
254 break;
255 case DL_DEV_UNBINDING:
256 link->status = DL_STATE_SUPPLIER_UNBIND;
257 break;
258 default:
259 link->status = DL_STATE_DORMANT;
260 break;
261 }
262 }
263
264 /*
265 * Move the consumer and all of the devices depending on it to the end
266 * of dpm_list and the devices_kset list.
267 *
268 * It is necessary to hold dpm_list locked throughout all that or else
269 * we may end up suspending with a wrong ordering of it.
270 */
271 device_reorder_to_tail(consumer, NULL);
272
273 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
274 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
275
276 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
277
278 out:
279 device_pm_unlock();
280 device_links_write_unlock();
281 return link;
282}
283EXPORT_SYMBOL_GPL(device_link_add);
284
285static void device_link_free(struct device_link *link)
286{
287 put_device(link->consumer);
288 put_device(link->supplier);
289 kfree(link);
290}
291
292#ifdef CONFIG_SRCU
293static void __device_link_free_srcu(struct rcu_head *rhead)
294{
295 device_link_free(container_of(rhead, struct device_link, rcu_head));
296}
297
298static void __device_link_del(struct device_link *link)
299{
300 dev_info(link->consumer, "Dropping the link to %s\n",
301 dev_name(link->supplier));
302
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100303 if (link->flags & DL_FLAG_PM_RUNTIME)
304 pm_runtime_drop_link(link->consumer);
305
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100306 list_del_rcu(&link->s_node);
307 list_del_rcu(&link->c_node);
308 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
309}
310#else /* !CONFIG_SRCU */
311static void __device_link_del(struct device_link *link)
312{
313 dev_info(link->consumer, "Dropping the link to %s\n",
314 dev_name(link->supplier));
315
316 list_del(&link->s_node);
317 list_del(&link->c_node);
318 device_link_free(link);
319}
320#endif /* !CONFIG_SRCU */
321
322/**
323 * device_link_del - Delete a link between two devices.
324 * @link: Device link to delete.
325 *
326 * The caller must ensure proper synchronization of this function with runtime
327 * PM.
328 */
329void device_link_del(struct device_link *link)
330{
331 device_links_write_lock();
332 device_pm_lock();
333 __device_link_del(link);
334 device_pm_unlock();
335 device_links_write_unlock();
336}
337EXPORT_SYMBOL_GPL(device_link_del);
338
339static void device_links_missing_supplier(struct device *dev)
340{
341 struct device_link *link;
342
343 list_for_each_entry(link, &dev->links.suppliers, c_node)
344 if (link->status == DL_STATE_CONSUMER_PROBE)
345 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
346}
347
348/**
349 * device_links_check_suppliers - Check presence of supplier drivers.
350 * @dev: Consumer device.
351 *
352 * Check links from this device to any suppliers. Walk the list of the device's
353 * links to suppliers and see if all of them are available. If not, simply
354 * return -EPROBE_DEFER.
355 *
356 * We need to guarantee that the supplier will not go away after the check has
357 * been positive here. It only can go away in __device_release_driver() and
358 * that function checks the device's links to consumers. This means we need to
359 * mark the link as "consumer probe in progress" to make the supplier removal
360 * wait for us to complete (or bad things may happen).
361 *
362 * Links with the DL_FLAG_STATELESS flag set are ignored.
363 */
364int device_links_check_suppliers(struct device *dev)
365{
366 struct device_link *link;
367 int ret = 0;
368
369 device_links_write_lock();
370
371 list_for_each_entry(link, &dev->links.suppliers, c_node) {
372 if (link->flags & DL_FLAG_STATELESS)
373 continue;
374
375 if (link->status != DL_STATE_AVAILABLE) {
376 device_links_missing_supplier(dev);
377 ret = -EPROBE_DEFER;
378 break;
379 }
380 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
381 }
382 dev->links.status = DL_DEV_PROBING;
383
384 device_links_write_unlock();
385 return ret;
386}
387
388/**
389 * device_links_driver_bound - Update device links after probing its driver.
390 * @dev: Device to update the links for.
391 *
392 * The probe has been successful, so update links from this device to any
393 * consumers by changing their status to "available".
394 *
395 * Also change the status of @dev's links to suppliers to "active".
396 *
397 * Links with the DL_FLAG_STATELESS flag set are ignored.
398 */
399void device_links_driver_bound(struct device *dev)
400{
401 struct device_link *link;
402
403 device_links_write_lock();
404
405 list_for_each_entry(link, &dev->links.consumers, s_node) {
406 if (link->flags & DL_FLAG_STATELESS)
407 continue;
408
409 WARN_ON(link->status != DL_STATE_DORMANT);
410 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
411 }
412
413 list_for_each_entry(link, &dev->links.suppliers, c_node) {
414 if (link->flags & DL_FLAG_STATELESS)
415 continue;
416
417 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
418 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
419 }
420
421 dev->links.status = DL_DEV_DRIVER_BOUND;
422
423 device_links_write_unlock();
424}
425
426/**
427 * __device_links_no_driver - Update links of a device without a driver.
428 * @dev: Device without a drvier.
429 *
430 * Delete all non-persistent links from this device to any suppliers.
431 *
432 * Persistent links stay around, but their status is changed to "available",
433 * unless they already are in the "supplier unbind in progress" state in which
434 * case they need not be updated.
435 *
436 * Links with the DL_FLAG_STATELESS flag set are ignored.
437 */
438static void __device_links_no_driver(struct device *dev)
439{
440 struct device_link *link, *ln;
441
442 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
443 if (link->flags & DL_FLAG_STATELESS)
444 continue;
445
446 if (link->flags & DL_FLAG_AUTOREMOVE)
447 __device_link_del(link);
448 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
449 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
450 }
451
452 dev->links.status = DL_DEV_NO_DRIVER;
453}
454
455void device_links_no_driver(struct device *dev)
456{
457 device_links_write_lock();
458 __device_links_no_driver(dev);
459 device_links_write_unlock();
460}
461
462/**
463 * device_links_driver_cleanup - Update links after driver removal.
464 * @dev: Device whose driver has just gone away.
465 *
466 * Update links to consumers for @dev by changing their status to "dormant" and
467 * invoke %__device_links_no_driver() to update links to suppliers for it as
468 * appropriate.
469 *
470 * Links with the DL_FLAG_STATELESS flag set are ignored.
471 */
472void device_links_driver_cleanup(struct device *dev)
473{
474 struct device_link *link;
475
476 device_links_write_lock();
477
478 list_for_each_entry(link, &dev->links.consumers, s_node) {
479 if (link->flags & DL_FLAG_STATELESS)
480 continue;
481
482 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
483 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
484 WRITE_ONCE(link->status, DL_STATE_DORMANT);
485 }
486
487 __device_links_no_driver(dev);
488
489 device_links_write_unlock();
490}
491
492/**
493 * device_links_busy - Check if there are any busy links to consumers.
494 * @dev: Device to check.
495 *
496 * Check each consumer of the device and return 'true' if its link's status
497 * is one of "consumer probe" or "active" (meaning that the given consumer is
498 * probing right now or its driver is present). Otherwise, change the link
499 * state to "supplier unbind" to prevent the consumer from being probed
500 * successfully going forward.
501 *
502 * Return 'false' if there are no probing or active consumers.
503 *
504 * Links with the DL_FLAG_STATELESS flag set are ignored.
505 */
506bool device_links_busy(struct device *dev)
507{
508 struct device_link *link;
509 bool ret = false;
510
511 device_links_write_lock();
512
513 list_for_each_entry(link, &dev->links.consumers, s_node) {
514 if (link->flags & DL_FLAG_STATELESS)
515 continue;
516
517 if (link->status == DL_STATE_CONSUMER_PROBE
518 || link->status == DL_STATE_ACTIVE) {
519 ret = true;
520 break;
521 }
522 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
523 }
524
525 dev->links.status = DL_DEV_UNBINDING;
526
527 device_links_write_unlock();
528 return ret;
529}
530
531/**
532 * device_links_unbind_consumers - Force unbind consumers of the given device.
533 * @dev: Device to unbind the consumers of.
534 *
535 * Walk the list of links to consumers for @dev and if any of them is in the
536 * "consumer probe" state, wait for all device probes in progress to complete
537 * and start over.
538 *
539 * If that's not the case, change the status of the link to "supplier unbind"
540 * and check if the link was in the "active" state. If so, force the consumer
541 * driver to unbind and start over (the consumer will not re-probe as we have
542 * changed the state of the link already).
543 *
544 * Links with the DL_FLAG_STATELESS flag set are ignored.
545 */
546void device_links_unbind_consumers(struct device *dev)
547{
548 struct device_link *link;
549
550 start:
551 device_links_write_lock();
552
553 list_for_each_entry(link, &dev->links.consumers, s_node) {
554 enum device_link_state status;
555
556 if (link->flags & DL_FLAG_STATELESS)
557 continue;
558
559 status = link->status;
560 if (status == DL_STATE_CONSUMER_PROBE) {
561 device_links_write_unlock();
562
563 wait_for_device_probe();
564 goto start;
565 }
566 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
567 if (status == DL_STATE_ACTIVE) {
568 struct device *consumer = link->consumer;
569
570 get_device(consumer);
571
572 device_links_write_unlock();
573
574 device_release_driver_internal(consumer, NULL,
575 consumer->parent);
576 put_device(consumer);
577 goto start;
578 }
579 }
580
581 device_links_write_unlock();
582}
583
584/**
585 * device_links_purge - Delete existing links to other devices.
586 * @dev: Target device.
587 */
588static void device_links_purge(struct device *dev)
589{
590 struct device_link *link, *ln;
591
592 /*
593 * Delete all of the remaining links from this device to any other
594 * devices (either consumers or suppliers).
595 */
596 device_links_write_lock();
597
598 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
599 WARN_ON(link->status == DL_STATE_ACTIVE);
600 __device_link_del(link);
601 }
602
603 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
604 WARN_ON(link->status != DL_STATE_DORMANT &&
605 link->status != DL_STATE_NONE);
606 __device_link_del(link);
607 }
608
609 device_links_write_unlock();
610}
611
612/* Device links support end. */
613
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800614int (*platform_notify)(struct device *dev) = NULL;
615int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700616static struct kobject *dev_kobj;
617struct kobject *sysfs_dev_char_kobj;
618struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200620static DEFINE_MUTEX(device_hotplug_lock);
621
622void lock_device_hotplug(void)
623{
624 mutex_lock(&device_hotplug_lock);
625}
626
627void unlock_device_hotplug(void)
628{
629 mutex_unlock(&device_hotplug_lock);
630}
631
632int lock_device_hotplug_sysfs(void)
633{
634 if (mutex_trylock(&device_hotplug_lock))
635 return 0;
636
637 /* Avoid busy looping (5 ms of sleep should do). */
638 msleep(5);
639 return restart_syscall();
640}
641
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800642#ifdef CONFIG_BLOCK
643static inline int device_is_not_partition(struct device *dev)
644{
645 return !(dev->type == &part_type);
646}
647#else
648static inline int device_is_not_partition(struct device *dev)
649{
650 return 1;
651}
652#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Alan Stern3e956372006-06-16 17:10:48 -0400654/**
655 * dev_driver_string - Return a device's driver name, if at all possible
656 * @dev: struct device to get the name of
657 *
658 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800659 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400660 * it is attached to. If it is not attached to a bus either, an empty
661 * string will be returned.
662 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700663const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400664{
Alan Stern35899722009-12-04 11:06:57 -0500665 struct device_driver *drv;
666
667 /* dev->driver can change to NULL underneath us because of unbinding,
668 * so be careful about accessing it. dev->bus and dev->class should
669 * never change once they are set, so they don't need special care.
670 */
671 drv = ACCESS_ONCE(dev->driver);
672 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100673 (dev->bus ? dev->bus->name :
674 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400675}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600676EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
679
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800680static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
681 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800683 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200684 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500685 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400688 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800689 if (ret >= (ssize_t)PAGE_SIZE) {
Greg Kroah-Hartman53a9c872013-01-17 13:10:23 -0800690 print_symbol("dev_attr_show: %s returned bad count\n",
691 (unsigned long)dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return ret;
694}
695
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800696static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
697 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800699 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200700 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500701 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400704 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return ret;
706}
707
Emese Revfy52cf25d2010-01-19 02:58:23 +0100708static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 .show = dev_attr_show,
710 .store = dev_attr_store,
711};
712
Kay Sieversca22e562011-12-14 14:29:38 -0800713#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
714
715ssize_t device_store_ulong(struct device *dev,
716 struct device_attribute *attr,
717 const char *buf, size_t size)
718{
719 struct dev_ext_attribute *ea = to_ext_attr(attr);
720 char *end;
721 unsigned long new = simple_strtoul(buf, &end, 0);
722 if (end == buf)
723 return -EINVAL;
724 *(unsigned long *)(ea->var) = new;
725 /* Always return full write size even if we didn't consume all */
726 return size;
727}
728EXPORT_SYMBOL_GPL(device_store_ulong);
729
730ssize_t device_show_ulong(struct device *dev,
731 struct device_attribute *attr,
732 char *buf)
733{
734 struct dev_ext_attribute *ea = to_ext_attr(attr);
735 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
736}
737EXPORT_SYMBOL_GPL(device_show_ulong);
738
739ssize_t device_store_int(struct device *dev,
740 struct device_attribute *attr,
741 const char *buf, size_t size)
742{
743 struct dev_ext_attribute *ea = to_ext_attr(attr);
744 char *end;
745 long new = simple_strtol(buf, &end, 0);
746 if (end == buf || new > INT_MAX || new < INT_MIN)
747 return -EINVAL;
748 *(int *)(ea->var) = new;
749 /* Always return full write size even if we didn't consume all */
750 return size;
751}
752EXPORT_SYMBOL_GPL(device_store_int);
753
754ssize_t device_show_int(struct device *dev,
755 struct device_attribute *attr,
756 char *buf)
757{
758 struct dev_ext_attribute *ea = to_ext_attr(attr);
759
760 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
761}
762EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Borislav Petkov91872392012-10-09 19:52:05 +0200764ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
765 const char *buf, size_t size)
766{
767 struct dev_ext_attribute *ea = to_ext_attr(attr);
768
769 if (strtobool(buf, ea->var) < 0)
770 return -EINVAL;
771
772 return size;
773}
774EXPORT_SYMBOL_GPL(device_store_bool);
775
776ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
777 char *buf)
778{
779 struct dev_ext_attribute *ea = to_ext_attr(attr);
780
781 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
782}
783EXPORT_SYMBOL_GPL(device_show_bool);
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400786 * device_release - free device structure.
787 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400789 * This is called once the reference count for the object
790 * reaches 0. We forward the call to the device's release
791 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800793static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200795 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800796 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Ming Leia525a3d2012-07-25 01:42:29 +0800798 /*
799 * Some platform devices are driven without driver attached
800 * and managed resources may have been acquired. Make sure
801 * all resources are released.
802 *
803 * Drivers still can add resources into device after device
804 * is deleted but alive, so release devres here to avoid
805 * possible memory leak.
806 */
807 devres_release_all(dev);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (dev->release)
810 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200811 else if (dev->type && dev->type->release)
812 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700813 else if (dev->class && dev->class->dev_release)
814 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700815 else
816 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800817 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100818 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800819 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700822static const void *device_namespace(struct kobject *kobj)
823{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200824 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700825 const void *ns = NULL;
826
827 if (dev->class && dev->class->ns_type)
828 ns = dev->class->namespace(dev);
829
830 return ns;
831}
832
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600833static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 .release = device_release,
835 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700836 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837};
838
839
Kay Sievers312c0042005-11-16 09:00:00 +0100840static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct kobj_type *ktype = get_ktype(kobj);
843
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600844 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200845 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (dev->bus)
847 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700848 if (dev->class)
849 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 return 0;
852}
853
Kay Sievers312c0042005-11-16 09:00:00 +0100854static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200856 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700858 if (dev->bus)
859 return dev->bus->name;
860 if (dev->class)
861 return dev->class->name;
862 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Kay Sievers7eff2e72007-08-14 15:15:12 +0200865static int dev_uevent(struct kset *kset, struct kobject *kobj,
866 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200868 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 int retval = 0;
870
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200871 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700872 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200873 const char *tmp;
874 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400875 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700876 kuid_t uid = GLOBAL_ROOT_UID;
877 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200878
Kay Sievers7eff2e72007-08-14 15:15:12 +0200879 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
880 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700881 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200882 if (name) {
883 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200884 if (mode)
885 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700886 if (!uid_eq(uid, GLOBAL_ROOT_UID))
887 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
888 if (!gid_eq(gid, GLOBAL_ROOT_GID))
889 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700890 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200891 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700892 }
893
Kay Sievers414264f2007-03-12 21:08:57 +0100894 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200895 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100896
Kay Sievers239378f2006-10-07 21:54:55 +0200897 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200898 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200899
Grant Likely07d57a32012-02-01 11:22:22 -0700900 /* Add common DT information about the device */
901 of_device_uevent(dev, env);
902
Kay Sievers7eff2e72007-08-14 15:15:12 +0200903 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100904 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200905 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200906 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800907 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100908 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
Kay Sievers7eff2e72007-08-14 15:15:12 +0200911 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700912 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200913 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200914 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800915 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100916 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800917 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200918 }
919
Stefan Weileef35c22010-08-06 21:11:15 +0200920 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200921 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200922 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200923 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800924 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100925 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800926 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700927 }
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return retval;
930}
931
Emese Revfy9cd43612009-12-31 14:52:51 +0100932static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100933 .filter = dev_uevent_filter,
934 .name = dev_uevent_name,
935 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936};
937
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700938static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +0200939 char *buf)
940{
941 struct kobject *top_kobj;
942 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200943 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200944 int i;
945 size_t count = 0;
946 int retval;
947
948 /* search the kset, the device belongs to */
949 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200950 while (!top_kobj->kset && top_kobj->parent)
951 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200952 if (!top_kobj->kset)
953 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200954
Kay Sievers16574dc2007-04-06 01:40:38 +0200955 kset = top_kobj->kset;
956 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
957 goto out;
958
959 /* respect filter */
960 if (kset->uevent_ops && kset->uevent_ops->filter)
961 if (!kset->uevent_ops->filter(kset, &dev->kobj))
962 goto out;
963
Kay Sievers7eff2e72007-08-14 15:15:12 +0200964 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
965 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200966 return -ENOMEM;
967
Kay Sievers16574dc2007-04-06 01:40:38 +0200968 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200969 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200970 if (retval)
971 goto out;
972
973 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200974 for (i = 0; i < env->envp_idx; i++)
975 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200976out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200977 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200978 return count;
979}
980
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700981static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200982 const char *buf, size_t count)
983{
Peter Rajnohaf36776f2017-05-09 15:22:30 +0200984 if (kobject_synth_uevent(&dev->kobj, buf, count))
985 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Kay Sievers60a96a52007-07-08 22:29:26 +0200986
Kay Sieversa7fd6702005-10-01 14:49:43 +0200987 return count;
988}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700989static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200990
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700991static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200992 char *buf)
993{
994 bool val;
995
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200996 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200997 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200998 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200999 return sprintf(buf, "%u\n", val);
1000}
1001
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001002static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001003 const char *buf, size_t count)
1004{
1005 bool val;
1006 int ret;
1007
1008 ret = strtobool(buf, &val);
1009 if (ret < 0)
1010 return ret;
1011
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001012 ret = lock_device_hotplug_sysfs();
1013 if (ret)
1014 return ret;
1015
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001016 ret = val ? device_online(dev) : device_offline(dev);
1017 unlock_device_hotplug();
1018 return ret < 0 ? ret : count;
1019}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001020static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001021
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001022int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001023{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001024 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001025}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001026EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001027
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001028void device_remove_groups(struct device *dev,
1029 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001030{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001031 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001032}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001033EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001034
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001035union device_attr_group_devres {
1036 const struct attribute_group *group;
1037 const struct attribute_group **groups;
1038};
1039
1040static int devm_attr_group_match(struct device *dev, void *res, void *data)
1041{
1042 return ((union device_attr_group_devres *)res)->group == data;
1043}
1044
1045static void devm_attr_group_remove(struct device *dev, void *res)
1046{
1047 union device_attr_group_devres *devres = res;
1048 const struct attribute_group *group = devres->group;
1049
1050 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1051 sysfs_remove_group(&dev->kobj, group);
1052}
1053
1054static void devm_attr_groups_remove(struct device *dev, void *res)
1055{
1056 union device_attr_group_devres *devres = res;
1057 const struct attribute_group **groups = devres->groups;
1058
1059 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1060 sysfs_remove_groups(&dev->kobj, groups);
1061}
1062
1063/**
1064 * devm_device_add_group - given a device, create a managed attribute group
1065 * @dev: The device to create the group for
1066 * @grp: The attribute group to create
1067 *
1068 * This function creates a group for the first time. It will explicitly
1069 * warn and error if any of the attribute files being created already exist.
1070 *
1071 * Returns 0 on success or error code on failure.
1072 */
1073int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1074{
1075 union device_attr_group_devres *devres;
1076 int error;
1077
1078 devres = devres_alloc(devm_attr_group_remove,
1079 sizeof(*devres), GFP_KERNEL);
1080 if (!devres)
1081 return -ENOMEM;
1082
1083 error = sysfs_create_group(&dev->kobj, grp);
1084 if (error) {
1085 devres_free(devres);
1086 return error;
1087 }
1088
1089 devres->group = grp;
1090 devres_add(dev, devres);
1091 return 0;
1092}
1093EXPORT_SYMBOL_GPL(devm_device_add_group);
1094
1095/**
1096 * devm_device_remove_group: remove a managed group from a device
1097 * @dev: device to remove the group from
1098 * @grp: group to remove
1099 *
1100 * This function removes a group of attributes from a device. The attributes
1101 * previously have to have been created for this group, otherwise it will fail.
1102 */
1103void devm_device_remove_group(struct device *dev,
1104 const struct attribute_group *grp)
1105{
1106 WARN_ON(devres_release(dev, devm_attr_group_remove,
1107 devm_attr_group_match,
1108 /* cast away const */ (void *)grp));
1109}
1110EXPORT_SYMBOL_GPL(devm_device_remove_group);
1111
1112/**
1113 * devm_device_add_groups - create a bunch of managed attribute groups
1114 * @dev: The device to create the group for
1115 * @groups: The attribute groups to create, NULL terminated
1116 *
1117 * This function creates a bunch of managed attribute groups. If an error
1118 * occurs when creating a group, all previously created groups will be
1119 * removed, unwinding everything back to the original state when this
1120 * function was called. It will explicitly warn and error if any of the
1121 * attribute files being created already exist.
1122 *
1123 * Returns 0 on success or error code from sysfs_create_group on failure.
1124 */
1125int devm_device_add_groups(struct device *dev,
1126 const struct attribute_group **groups)
1127{
1128 union device_attr_group_devres *devres;
1129 int error;
1130
1131 devres = devres_alloc(devm_attr_groups_remove,
1132 sizeof(*devres), GFP_KERNEL);
1133 if (!devres)
1134 return -ENOMEM;
1135
1136 error = sysfs_create_groups(&dev->kobj, groups);
1137 if (error) {
1138 devres_free(devres);
1139 return error;
1140 }
1141
1142 devres->groups = groups;
1143 devres_add(dev, devres);
1144 return 0;
1145}
1146EXPORT_SYMBOL_GPL(devm_device_add_groups);
1147
1148/**
1149 * devm_device_remove_groups - remove a list of managed groups
1150 *
1151 * @dev: The device for the groups to be removed from
1152 * @groups: NULL terminated list of groups to be removed
1153 *
1154 * If groups is not NULL, remove the specified groups from the device.
1155 */
1156void devm_device_remove_groups(struct device *dev,
1157 const struct attribute_group **groups)
1158{
1159 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1160 devm_attr_group_match,
1161 /* cast away const */ (void *)groups));
1162}
1163EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001165static int device_add_attrs(struct device *dev)
1166{
1167 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001168 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001169 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001170
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001171 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001172 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001173 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001174 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001175 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001176
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001177 if (type) {
1178 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001179 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001180 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001181 }
1182
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001183 error = device_add_groups(dev, dev->groups);
1184 if (error)
1185 goto err_remove_type_groups;
1186
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001187 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001188 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001189 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001190 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001191 }
1192
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001193 return 0;
1194
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001195 err_remove_dev_groups:
1196 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001197 err_remove_type_groups:
1198 if (type)
1199 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001200 err_remove_class_groups:
1201 if (class)
1202 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001203
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001204 return error;
1205}
1206
1207static void device_remove_attrs(struct device *dev)
1208{
1209 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001210 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001211
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001212 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001213 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001214
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001215 if (type)
1216 device_remove_groups(dev, type->groups);
1217
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001218 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001219 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001220}
1221
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001222static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001223 char *buf)
1224{
1225 return print_dev_t(buf, dev->devt);
1226}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001227static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001228
Kay Sieversca22e562011-12-14 14:29:38 -08001229/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001230struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001233 * devices_kset_move_before - Move device in the devices_kset's list.
1234 * @deva: Device to move.
1235 * @devb: Device @deva should come before.
1236 */
1237static void devices_kset_move_before(struct device *deva, struct device *devb)
1238{
1239 if (!devices_kset)
1240 return;
1241 pr_debug("devices_kset: Moving %s before %s\n",
1242 dev_name(deva), dev_name(devb));
1243 spin_lock(&devices_kset->list_lock);
1244 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1245 spin_unlock(&devices_kset->list_lock);
1246}
1247
1248/**
1249 * devices_kset_move_after - Move device in the devices_kset's list.
1250 * @deva: Device to move
1251 * @devb: Device @deva should come after.
1252 */
1253static void devices_kset_move_after(struct device *deva, struct device *devb)
1254{
1255 if (!devices_kset)
1256 return;
1257 pr_debug("devices_kset: Moving %s after %s\n",
1258 dev_name(deva), dev_name(devb));
1259 spin_lock(&devices_kset->list_lock);
1260 list_move(&deva->kobj.entry, &devb->kobj.entry);
1261 spin_unlock(&devices_kset->list_lock);
1262}
1263
1264/**
1265 * devices_kset_move_last - move the device to the end of devices_kset's list.
1266 * @dev: device to move
1267 */
1268void devices_kset_move_last(struct device *dev)
1269{
1270 if (!devices_kset)
1271 return;
1272 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1273 spin_lock(&devices_kset->list_lock);
1274 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1275 spin_unlock(&devices_kset->list_lock);
1276}
1277
1278/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001279 * device_create_file - create sysfs attribute file for device.
1280 * @dev: device.
1281 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001283int device_create_file(struct device *dev,
1284 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001287
1288 if (dev) {
1289 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001290 "Attribute %s: write permission without 'store'\n",
1291 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001292 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001293 "Attribute %s: read permission without 'show'\n",
1294 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001296 }
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return error;
1299}
David Graham White86df2682013-07-21 20:41:14 -04001300EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001303 * device_remove_file - remove sysfs attribute file.
1304 * @dev: device.
1305 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001307void device_remove_file(struct device *dev,
1308 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001310 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
David Graham White86df2682013-07-21 20:41:14 -04001313EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001315/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001316 * device_remove_file_self - remove sysfs attribute file from its own method.
1317 * @dev: device.
1318 * @attr: device attribute descriptor.
1319 *
1320 * See kernfs_remove_self() for details.
1321 */
1322bool device_remove_file_self(struct device *dev,
1323 const struct device_attribute *attr)
1324{
1325 if (dev)
1326 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1327 else
1328 return false;
1329}
1330EXPORT_SYMBOL_GPL(device_remove_file_self);
1331
1332/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001333 * device_create_bin_file - create sysfs binary attribute file for device.
1334 * @dev: device.
1335 * @attr: device binary attribute descriptor.
1336 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001337int device_create_bin_file(struct device *dev,
1338 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001339{
1340 int error = -EINVAL;
1341 if (dev)
1342 error = sysfs_create_bin_file(&dev->kobj, attr);
1343 return error;
1344}
1345EXPORT_SYMBOL_GPL(device_create_bin_file);
1346
1347/**
1348 * device_remove_bin_file - remove sysfs binary attribute file
1349 * @dev: device.
1350 * @attr: device binary attribute descriptor.
1351 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001352void device_remove_bin_file(struct device *dev,
1353 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001354{
1355 if (dev)
1356 sysfs_remove_bin_file(&dev->kobj, attr);
1357}
1358EXPORT_SYMBOL_GPL(device_remove_bin_file);
1359
James Bottomley34bb61f2005-09-06 16:56:51 -07001360static void klist_children_get(struct klist_node *n)
1361{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001362 struct device_private *p = to_device_private_parent(n);
1363 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001364
1365 get_device(dev);
1366}
1367
1368static void klist_children_put(struct klist_node *n)
1369{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001370 struct device_private *p = to_device_private_parent(n);
1371 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001372
1373 put_device(dev);
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001377 * device_initialize - init device structure.
1378 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 *
Cornelia Huck57394112008-09-03 18:26:40 +02001380 * This prepares the device for use by other layers by initializing
1381 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001382 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001383 * that function, though it can also be called separately, so one
1384 * may use @dev's fields. In particular, get_device()/put_device()
1385 * may be used for reference counting of @dev after calling this
1386 * function.
1387 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001388 * All fields in @dev must be initialized by the caller to 0, except
1389 * for those explicitly set to some other value. The simplest
1390 * approach is to use kzalloc() to allocate the structure containing
1391 * @dev.
1392 *
Cornelia Huck57394112008-09-03 18:26:40 +02001393 * NOTE: Use put_device() to give up your reference instead of freeing
1394 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396void device_initialize(struct device *dev)
1397{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001398 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001399 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001401 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001402 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001403 spin_lock_init(&dev->devres_lock);
1404 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001405 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001406 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001407#ifdef CONFIG_GENERIC_MSI_IRQ
1408 INIT_LIST_HEAD(&dev->msi_list);
1409#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001410 INIT_LIST_HEAD(&dev->links.consumers);
1411 INIT_LIST_HEAD(&dev->links.suppliers);
1412 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
David Graham White86df2682013-07-21 20:41:14 -04001414EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Tejun Heod73ce002013-03-12 11:30:05 -07001416struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001417{
Kay Sievers86406242007-03-14 03:25:56 +01001418 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001419
Kay Sievers86406242007-03-14 03:25:56 +01001420 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001421 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001422 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001423
Kay Sievers86406242007-03-14 03:25:56 +01001424 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001425}
1426
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001427struct class_dir {
1428 struct kobject kobj;
1429 struct class *class;
1430};
1431
1432#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1433
1434static void class_dir_release(struct kobject *kobj)
1435{
1436 struct class_dir *dir = to_class_dir(kobj);
1437 kfree(dir);
1438}
1439
1440static const
1441struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1442{
1443 struct class_dir *dir = to_class_dir(kobj);
1444 return dir->class->ns_type;
1445}
1446
1447static struct kobj_type class_dir_ktype = {
1448 .release = class_dir_release,
1449 .sysfs_ops = &kobj_sysfs_ops,
1450 .child_ns_type = class_dir_child_ns_type
1451};
1452
1453static struct kobject *
1454class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1455{
1456 struct class_dir *dir;
1457 int retval;
1458
1459 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1460 if (!dir)
1461 return NULL;
1462
1463 dir->class = class;
1464 kobject_init(&dir->kobj, &class_dir_ktype);
1465
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001466 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001467
1468 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1469 if (retval < 0) {
1470 kobject_put(&dir->kobj);
1471 return NULL;
1472 }
1473 return &dir->kobj;
1474}
1475
Yijing Wange4a60d12014-11-07 12:05:49 +08001476static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001477
Kay Sieversda231fd2007-11-21 17:29:15 +01001478static struct kobject *get_device_parent(struct device *dev,
1479 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001480{
Kay Sievers86406242007-03-14 03:25:56 +01001481 if (dev->class) {
1482 struct kobject *kobj = NULL;
1483 struct kobject *parent_kobj;
1484 struct kobject *k;
1485
Randy Dunlapead454f2010-09-24 14:36:49 -07001486#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001487 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001488 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001489 if (parent && parent->class == &block_class)
1490 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001491 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001492 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001493#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001494
Kay Sievers86406242007-03-14 03:25:56 +01001495 /*
1496 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001497 * Class-devices with a non class-device as parent, live
1498 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001499 */
1500 if (parent == NULL)
1501 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001502 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001503 return &parent->kobj;
1504 else
1505 parent_kobj = &parent->kobj;
1506
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001507 mutex_lock(&gdp_mutex);
1508
Kay Sievers86406242007-03-14 03:25:56 +01001509 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001510 spin_lock(&dev->class->p->glue_dirs.list_lock);
1511 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001512 if (k->parent == parent_kobj) {
1513 kobj = kobject_get(k);
1514 break;
1515 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001516 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001517 if (kobj) {
1518 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001519 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001520 }
Kay Sievers86406242007-03-14 03:25:56 +01001521
1522 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001523 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001524 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001525 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001526 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001527 }
1528
Kay Sieversca22e562011-12-14 14:29:38 -08001529 /* subsystems can specify a default root directory for their devices */
1530 if (!parent && dev->bus && dev->bus->dev_root)
1531 return &dev->bus->dev_root->kobj;
1532
Kay Sievers86406242007-03-14 03:25:56 +01001533 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001534 return &parent->kobj;
1535 return NULL;
1536}
Kay Sieversda231fd2007-11-21 17:29:15 +01001537
Ming Leicebf8fd2016-07-10 19:27:36 +08001538static inline bool live_in_glue_dir(struct kobject *kobj,
1539 struct device *dev)
1540{
1541 if (!kobj || !dev->class ||
1542 kobj->kset != &dev->class->p->glue_dirs)
1543 return false;
1544 return true;
1545}
1546
1547static inline struct kobject *get_glue_dir(struct device *dev)
1548{
1549 return dev->kobj.parent;
1550}
1551
1552/*
1553 * make sure cleaning up dir as the last step, we need to make
1554 * sure .release handler of kobject is run with holding the
1555 * global lock
1556 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001557static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001558{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001559 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001560 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001561 return;
1562
Yijing Wange4a60d12014-11-07 12:05:49 +08001563 mutex_lock(&gdp_mutex);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001564 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001565 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001566}
Cornelia Huck63b69712008-01-21 16:09:44 +01001567
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001568static int device_add_class_symlinks(struct device *dev)
1569{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001570 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001571 int error;
1572
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001573 if (of_node) {
1574 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
1575 if (error)
1576 dev_warn(dev, "Error %d creating of_node link\n",error);
1577 /* An error here doesn't warrant bringing down the device */
1578 }
1579
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001580 if (!dev->class)
1581 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001582
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001583 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001584 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001585 "subsystem");
1586 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001587 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001588
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001589 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001590 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1591 "device");
1592 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001593 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001594 }
Kay Sievers39aba962010-09-04 22:33:14 -07001595
Randy Dunlapead454f2010-09-24 14:36:49 -07001596#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001597 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001598 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001599 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001600#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001601
1602 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001603 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001604 &dev->kobj, dev_name(dev));
1605 if (error)
1606 goto out_device;
1607
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001608 return 0;
1609
Kay Sievers39aba962010-09-04 22:33:14 -07001610out_device:
1611 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001612
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001613out_subsys:
1614 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001615out_devnode:
1616 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001617 return error;
1618}
1619
1620static void device_remove_class_symlinks(struct device *dev)
1621{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001622 if (dev_of_node(dev))
1623 sysfs_remove_link(&dev->kobj, "of_node");
1624
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001625 if (!dev->class)
1626 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001627
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001628 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001629 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001630 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001631#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001632 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001633 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001634#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001635 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001636}
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001639 * dev_set_name - set a device name
1640 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001641 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001642 */
1643int dev_set_name(struct device *dev, const char *fmt, ...)
1644{
1645 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001646 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001647
1648 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001649 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001650 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001651 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001652}
1653EXPORT_SYMBOL_GPL(dev_set_name);
1654
1655/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001656 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1657 * @dev: device
1658 *
1659 * By default we select char/ for new entries. Setting class->dev_obj
1660 * to NULL prevents an entry from being created. class->dev_kobj must
1661 * be set (or cleared) before any devices are registered to the class
1662 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001663 * device_remove_sys_dev_entry() will disagree about the presence of
1664 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001665 */
1666static struct kobject *device_to_dev_kobj(struct device *dev)
1667{
1668 struct kobject *kobj;
1669
1670 if (dev->class)
1671 kobj = dev->class->dev_kobj;
1672 else
1673 kobj = sysfs_dev_char_kobj;
1674
1675 return kobj;
1676}
1677
1678static int device_create_sys_dev_entry(struct device *dev)
1679{
1680 struct kobject *kobj = device_to_dev_kobj(dev);
1681 int error = 0;
1682 char devt_str[15];
1683
1684 if (kobj) {
1685 format_dev_t(devt_str, dev->devt);
1686 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1687 }
1688
1689 return error;
1690}
1691
1692static void device_remove_sys_dev_entry(struct device *dev)
1693{
1694 struct kobject *kobj = device_to_dev_kobj(dev);
1695 char devt_str[15];
1696
1697 if (kobj) {
1698 format_dev_t(devt_str, dev->devt);
1699 sysfs_remove_link(kobj, devt_str);
1700 }
1701}
1702
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001703int device_private_init(struct device *dev)
1704{
1705 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1706 if (!dev->p)
1707 return -ENOMEM;
1708 dev->p->device = dev;
1709 klist_init(&dev->p->klist_children, klist_children_get,
1710 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001711 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001712 return 0;
1713}
1714
Dan Williamse105b8b2008-04-21 10:51:07 -07001715/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001716 * device_add - add device to device hierarchy.
1717 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001719 * This is part 2 of device_register(), though may be called
1720 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 *
Cornelia Huck57394112008-09-03 18:26:40 +02001722 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001723 * to the global and sibling lists for the device, then
1724 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001725 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001726 * Do not call this routine or device_register() more than once for
1727 * any device structure. The driver model core is not designed to work
1728 * with devices that get unregistered and then spring back to life.
1729 * (Among other things, it's very hard to guarantee that all references
1730 * to the previous incarnation of @dev have been dropped.) Allocate
1731 * and register a fresh new struct device instead.
1732 *
Cornelia Huck57394112008-09-03 18:26:40 +02001733 * NOTE: _Never_ directly free @dev after calling this function, even
1734 * if it returned an error! Always use put_device() to give up your
1735 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
1737int device_add(struct device *dev)
1738{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05301739 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08001740 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001741 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001742 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001743 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001746 if (!dev)
1747 goto done;
1748
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001749 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001750 error = device_private_init(dev);
1751 if (error)
1752 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001753 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001754
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001755 /*
1756 * for statically allocated devices, which should all be converted
1757 * some day, we need to initialize the name. We prevent reading back
1758 * the name, and force the use of dev_name()
1759 */
1760 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001761 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001762 dev->init_name = NULL;
1763 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001764
Kay Sieversca22e562011-12-14 14:29:38 -08001765 /* subsystems can specify simple device enumeration */
1766 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1767 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1768
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001769 if (!dev_name(dev)) {
1770 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001771 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001774 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001777 kobj = get_device_parent(dev, parent);
1778 if (kobj)
1779 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Yinghai Lu0d358f22008-02-19 03:20:41 -08001781 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001782 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001783 set_dev_node(dev, dev_to_node(parent));
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001786 /* we require the name to be set before, and pass NULL */
1787 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001788 if (error) {
1789 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001791 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001792
Brian Walsh37022642006-08-14 22:43:19 -07001793 /* notify platform of device entry */
1794 if (platform_notify)
1795 platform_notify(dev);
1796
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001797 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001798 if (error)
1799 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001800
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001801 error = device_add_class_symlinks(dev);
1802 if (error)
1803 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001804 error = device_add_attrs(dev);
1805 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001806 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001807 error = bus_add_device(dev);
1808 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001810 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001811 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001812 goto DPMError;
1813 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001814
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001815 if (MAJOR(dev->devt)) {
1816 error = device_create_file(dev, &dev_attr_dev);
1817 if (error)
1818 goto DevAttrError;
1819
1820 error = device_create_sys_dev_entry(dev);
1821 if (error)
1822 goto SysEntryError;
1823
1824 devtmpfs_create_node(dev);
1825 }
1826
Alan Sternec0676ee2008-12-05 14:10:31 -05001827 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001828 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001829 */
1830 if (dev->bus)
1831 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1832 BUS_NOTIFY_ADD_DEVICE, dev);
1833
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001834 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001835 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001837 klist_add_tail(&dev->p->knode_parent,
1838 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001840 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001841 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001842 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001843 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001844 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001845
1846 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001847 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001848 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001849 if (class_intf->add_dev)
1850 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001851 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001852 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001853done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 put_device(dev);
1855 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001856 SysEntryError:
1857 if (MAJOR(dev->devt))
1858 device_remove_file(dev, &dev_attr_dev);
1859 DevAttrError:
1860 device_pm_remove(dev);
1861 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001862 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001863 bus_remove_device(dev);
1864 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001865 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001866 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001867 device_remove_class_symlinks(dev);
1868 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001869 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001870 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001871 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001872 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 kobject_del(&dev->kobj);
1874 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001875 cleanup_glue_dir(dev, glue_dir);
Markus Elfring5f0163a2015-02-05 11:48:26 +01001876 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001877name_error:
1878 kfree(dev->p);
1879 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001880 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
David Graham White86df2682013-07-21 20:41:14 -04001882EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001885 * device_register - register a device with the system.
1886 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001888 * This happens in two clean steps - initialize the device
1889 * and add it to the system. The two steps can be called
1890 * separately, but this is the easiest and most common.
1891 * I.e. you should only call the two helpers separately if
1892 * have a clearly defined need to use and refcount the device
1893 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001894 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001895 * For more information, see the kerneldoc for device_initialize()
1896 * and device_add().
1897 *
Cornelia Huck57394112008-09-03 18:26:40 +02001898 * NOTE: _Never_ directly free @dev after calling this function, even
1899 * if it returned an error! Always use put_device() to give up the
1900 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902int device_register(struct device *dev)
1903{
1904 device_initialize(dev);
1905 return device_add(dev);
1906}
David Graham White86df2682013-07-21 20:41:14 -04001907EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001910 * get_device - increment reference count for device.
1911 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001913 * This simply forwards the call to kobject_get(), though
1914 * we do take care to provide for the case that we get a NULL
1915 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001917struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001919 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
David Graham White86df2682013-07-21 20:41:14 -04001921EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001924 * put_device - decrement reference count.
1925 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001927void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001929 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (dev)
1931 kobject_put(&dev->kobj);
1932}
David Graham White86df2682013-07-21 20:41:14 -04001933EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001936 * device_del - delete device from system.
1937 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001939 * This is the first part of the device unregistration
1940 * sequence. This removes the device from the lists we control
1941 * from here, has it removed from the other driver model
1942 * subsystems it was added to in device_add(), and removes it
1943 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001945 * NOTE: this should be called manually _iff_ device_add() was
1946 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001948void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001950 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08001951 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001952 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Alan Sternec0676ee2008-12-05 14:10:31 -05001954 /* Notify clients of device removal. This call must come
1955 * before dpm_sysfs_remove().
1956 */
1957 if (dev->bus)
1958 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1959 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001960
1961 device_links_purge(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001962 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001964 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001965 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001966 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001967 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001968 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001969 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001970 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001971 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001972
Kay Sieversca22e562011-12-14 14:29:38 -08001973 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001974 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001975 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001976 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001977 if (class_intf->remove_dev)
1978 class_intf->remove_dev(dev, class_intf);
1979 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001980 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001981 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001982 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001983 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001984 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001985 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001986 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001987 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02001988 device_remove_properties(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /* Notify the platform of the removal, in case they
1991 * need to do anything...
1992 */
1993 if (platform_notify_remove)
1994 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02001995 if (dev->bus)
1996 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1997 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001998 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001999 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002001 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002002 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
David Graham White86df2682013-07-21 20:41:14 -04002004EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002007 * device_unregister - unregister device from system.
2008 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002010 * We do this in two parts, like we do device_register(). First,
2011 * we remove it from all the subsystems with device_del(), then
2012 * we decrement the reference count via put_device(). If that
2013 * is the final reference count, the device will be cleaned up
2014 * via device_release() above. Otherwise, the structure will
2015 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002017void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002019 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 device_del(dev);
2021 put_device(dev);
2022}
David Graham White86df2682013-07-21 20:41:14 -04002023EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002025static struct device *prev_device(struct klist_iter *i)
2026{
2027 struct klist_node *n = klist_prev(i);
2028 struct device *dev = NULL;
2029 struct device_private *p;
2030
2031 if (n) {
2032 p = to_device_private_parent(n);
2033 dev = p->device;
2034 }
2035 return dev;
2036}
2037
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002038static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002039{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002040 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002041 struct device *dev = NULL;
2042 struct device_private *p;
2043
2044 if (n) {
2045 p = to_device_private_parent(n);
2046 dev = p->device;
2047 }
2048 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002052 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002053 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002054 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002055 * @uid: returned file owner
2056 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002057 * @tmp: possibly allocated string
2058 *
2059 * Return the relative path of a possible device node.
2060 * Non-default names may need to allocate a memory to compose
2061 * a name. This memory is returned in tmp and needs to be
2062 * freed by the caller.
2063 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002064const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002065 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002066 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002067{
2068 char *s;
2069
2070 *tmp = NULL;
2071
2072 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002073 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002074 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002075 if (*tmp)
2076 return *tmp;
2077
2078 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002079 if (dev->class && dev->class->devnode)
2080 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002081 if (*tmp)
2082 return *tmp;
2083
2084 /* return name without allocation, tmp == NULL */
2085 if (strchr(dev_name(dev), '!') == NULL)
2086 return dev_name(dev);
2087
2088 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002089 s = kstrdup(dev_name(dev), GFP_KERNEL);
2090 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002091 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002092 strreplace(s, '!', '/');
2093 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002094}
2095
2096/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002097 * device_for_each_child - device child iterator.
2098 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002099 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002100 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002102 * Iterate over @parent's child devices, and call @fn for each,
2103 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002105 * We check the return of @fn each time. If it returns anything
2106 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002108int device_for_each_child(struct device *parent, void *data,
2109 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002111 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002112 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int error = 0;
2114
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002115 if (!parent->p)
2116 return 0;
2117
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002118 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002119 while ((child = next_device(&i)) && !error)
2120 error = fn(child, data);
2121 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return error;
2123}
David Graham White86df2682013-07-21 20:41:14 -04002124EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Cornelia Huck5ab69982006-11-16 15:42:07 +01002126/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002127 * device_for_each_child_reverse - device child iterator in reversed order.
2128 * @parent: parent struct device.
2129 * @fn: function to be called for each device.
2130 * @data: data for the callback.
2131 *
2132 * Iterate over @parent's child devices, and call @fn for each,
2133 * passing it @data.
2134 *
2135 * We check the return of @fn each time. If it returns anything
2136 * other than 0, we break out and return that value.
2137 */
2138int device_for_each_child_reverse(struct device *parent, void *data,
2139 int (*fn)(struct device *dev, void *data))
2140{
2141 struct klist_iter i;
2142 struct device *child;
2143 int error = 0;
2144
2145 if (!parent->p)
2146 return 0;
2147
2148 klist_iter_init(&parent->p->klist_children, &i);
2149 while ((child = prev_device(&i)) && !error)
2150 error = fn(child, data);
2151 klist_iter_exit(&i);
2152 return error;
2153}
2154EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2155
2156/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002157 * device_find_child - device iterator for locating a particular device.
2158 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002159 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002160 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002161 *
2162 * This is similar to the device_for_each_child() function above, but it
2163 * returns a reference to a device that is 'found' for later use, as
2164 * determined by the @match callback.
2165 *
2166 * The callback should return 0 if the device doesn't match and non-zero
2167 * if it does. If the callback returns non-zero and a reference to the
2168 * current device can be obtained, this function will return to the caller
2169 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002170 *
2171 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002172 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002173struct device *device_find_child(struct device *parent, void *data,
2174 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002175{
2176 struct klist_iter i;
2177 struct device *child;
2178
2179 if (!parent)
2180 return NULL;
2181
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002182 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002183 while ((child = next_device(&i)))
2184 if (match(child, data) && get_device(child))
2185 break;
2186 klist_iter_exit(&i);
2187 return child;
2188}
David Graham White86df2682013-07-21 20:41:14 -04002189EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191int __init devices_init(void)
2192{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002193 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2194 if (!devices_kset)
2195 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002196 dev_kobj = kobject_create_and_add("dev", NULL);
2197 if (!dev_kobj)
2198 goto dev_kobj_err;
2199 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2200 if (!sysfs_dev_block_kobj)
2201 goto block_kobj_err;
2202 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2203 if (!sysfs_dev_char_kobj)
2204 goto char_kobj_err;
2205
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002206 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002207
2208 char_kobj_err:
2209 kobject_put(sysfs_dev_block_kobj);
2210 block_kobj_err:
2211 kobject_put(dev_kobj);
2212 dev_kobj_err:
2213 kset_unregister(devices_kset);
2214 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002217static int device_check_offline(struct device *dev, void *not_used)
2218{
2219 int ret;
2220
2221 ret = device_for_each_child(dev, NULL, device_check_offline);
2222 if (ret)
2223 return ret;
2224
2225 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2226}
2227
2228/**
2229 * device_offline - Prepare the device for hot-removal.
2230 * @dev: Device to be put offline.
2231 *
2232 * Execute the device bus type's .offline() callback, if present, to prepare
2233 * the device for a subsequent hot-removal. If that succeeds, the device must
2234 * not be used until either it is removed or its bus type's .online() callback
2235 * is executed.
2236 *
2237 * Call under device_hotplug_lock.
2238 */
2239int device_offline(struct device *dev)
2240{
2241 int ret;
2242
2243 if (dev->offline_disabled)
2244 return -EPERM;
2245
2246 ret = device_for_each_child(dev, NULL, device_check_offline);
2247 if (ret)
2248 return ret;
2249
2250 device_lock(dev);
2251 if (device_supports_offline(dev)) {
2252 if (dev->offline) {
2253 ret = 1;
2254 } else {
2255 ret = dev->bus->offline(dev);
2256 if (!ret) {
2257 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2258 dev->offline = true;
2259 }
2260 }
2261 }
2262 device_unlock(dev);
2263
2264 return ret;
2265}
2266
2267/**
2268 * device_online - Put the device back online after successful device_offline().
2269 * @dev: Device to be put back online.
2270 *
2271 * If device_offline() has been successfully executed for @dev, but the device
2272 * has not been removed subsequently, execute its bus type's .online() callback
2273 * to indicate that the device can be used again.
2274 *
2275 * Call under device_hotplug_lock.
2276 */
2277int device_online(struct device *dev)
2278{
2279 int ret = 0;
2280
2281 device_lock(dev);
2282 if (device_supports_offline(dev)) {
2283 if (dev->offline) {
2284 ret = dev->bus->online(dev);
2285 if (!ret) {
2286 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2287 dev->offline = false;
2288 }
2289 } else {
2290 ret = 1;
2291 }
2292 }
2293 device_unlock(dev);
2294
2295 return ret;
2296}
2297
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002298struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002299 struct device dev;
2300 struct module *owner;
2301};
2302
Josh Triplett93058422012-11-18 21:27:55 -08002303static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002304{
2305 return container_of(d, struct root_device, dev);
2306}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002307
2308static void root_device_release(struct device *dev)
2309{
2310 kfree(to_root_device(dev));
2311}
2312
2313/**
2314 * __root_device_register - allocate and register a root device
2315 * @name: root device name
2316 * @owner: owner module of the root device, usually THIS_MODULE
2317 *
2318 * This function allocates a root device and registers it
2319 * using device_register(). In order to free the returned
2320 * device, use root_device_unregister().
2321 *
2322 * Root devices are dummy devices which allow other devices
2323 * to be grouped under /sys/devices. Use this function to
2324 * allocate a root device and then use it as the parent of
2325 * any device which should appear under /sys/devices/{name}
2326 *
2327 * The /sys/devices/{name} directory will also contain a
2328 * 'module' symlink which points to the @owner directory
2329 * in sysfs.
2330 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002331 * Returns &struct device pointer on success, or ERR_PTR() on error.
2332 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002333 * Note: You probably want to use root_device_register().
2334 */
2335struct device *__root_device_register(const char *name, struct module *owner)
2336{
2337 struct root_device *root;
2338 int err = -ENOMEM;
2339
2340 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2341 if (!root)
2342 return ERR_PTR(err);
2343
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002344 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002345 if (err) {
2346 kfree(root);
2347 return ERR_PTR(err);
2348 }
2349
2350 root->dev.release = root_device_release;
2351
2352 err = device_register(&root->dev);
2353 if (err) {
2354 put_device(&root->dev);
2355 return ERR_PTR(err);
2356 }
2357
Christoph Egger1d9e8822010-05-17 16:57:58 +02002358#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002359 if (owner) {
2360 struct module_kobject *mk = &owner->mkobj;
2361
2362 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2363 if (err) {
2364 device_unregister(&root->dev);
2365 return ERR_PTR(err);
2366 }
2367 root->owner = owner;
2368 }
2369#endif
2370
2371 return &root->dev;
2372}
2373EXPORT_SYMBOL_GPL(__root_device_register);
2374
2375/**
2376 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002377 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002378 *
2379 * This function unregisters and cleans up a device that was created by
2380 * root_device_register().
2381 */
2382void root_device_unregister(struct device *dev)
2383{
2384 struct root_device *root = to_root_device(dev);
2385
2386 if (root->owner)
2387 sysfs_remove_link(&root->dev.kobj, "module");
2388
2389 device_unregister(dev);
2390}
2391EXPORT_SYMBOL_GPL(root_device_unregister);
2392
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002393
2394static void device_create_release(struct device *dev)
2395{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002396 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002397 kfree(dev);
2398}
2399
Guenter Roeck39ef3112013-07-14 16:05:57 -07002400static struct device *
2401device_create_groups_vargs(struct class *class, struct device *parent,
2402 dev_t devt, void *drvdata,
2403 const struct attribute_group **groups,
2404 const char *fmt, va_list args)
2405{
2406 struct device *dev = NULL;
2407 int retval = -ENODEV;
2408
2409 if (class == NULL || IS_ERR(class))
2410 goto error;
2411
2412 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2413 if (!dev) {
2414 retval = -ENOMEM;
2415 goto error;
2416 }
2417
David Herrmannbbc780f2013-11-21 20:15:48 +01002418 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002419 dev->devt = devt;
2420 dev->class = class;
2421 dev->parent = parent;
2422 dev->groups = groups;
2423 dev->release = device_create_release;
2424 dev_set_drvdata(dev, drvdata);
2425
2426 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2427 if (retval)
2428 goto error;
2429
David Herrmannbbc780f2013-11-21 20:15:48 +01002430 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002431 if (retval)
2432 goto error;
2433
2434 return dev;
2435
2436error:
2437 put_device(dev);
2438 return ERR_PTR(retval);
2439}
2440
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002441/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002442 * device_create_vargs - creates a device and registers it with sysfs
2443 * @class: pointer to the struct class that this device should be registered to
2444 * @parent: pointer to the parent struct device of this new device, if any
2445 * @devt: the dev_t for the char device to be added
2446 * @drvdata: the data to be added to the device for callbacks
2447 * @fmt: string for the device's name
2448 * @args: va_list for the device's name
2449 *
2450 * This function can be used by char device classes. A struct device
2451 * will be created in sysfs, registered to the specified class.
2452 *
2453 * A "dev" file will be created, showing the dev_t for the device, if
2454 * the dev_t is not 0,0.
2455 * If a pointer to a parent struct device is passed in, the newly created
2456 * struct device will be a child of that device in sysfs.
2457 * The pointer to the struct device will be returned from the call.
2458 * Any further sysfs files that might be required can be created using this
2459 * pointer.
2460 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002461 * Returns &struct device pointer on success, or ERR_PTR() on error.
2462 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002463 * Note: the struct class passed to this function must have previously
2464 * been created with a call to class_create().
2465 */
2466struct device *device_create_vargs(struct class *class, struct device *parent,
2467 dev_t devt, void *drvdata, const char *fmt,
2468 va_list args)
2469{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002470 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2471 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002472}
2473EXPORT_SYMBOL_GPL(device_create_vargs);
2474
2475/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002476 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002477 * @class: pointer to the struct class that this device should be registered to
2478 * @parent: pointer to the parent struct device of this new device, if any
2479 * @devt: the dev_t for the char device to be added
2480 * @drvdata: the data to be added to the device for callbacks
2481 * @fmt: string for the device's name
2482 *
2483 * This function can be used by char device classes. A struct device
2484 * will be created in sysfs, registered to the specified class.
2485 *
2486 * A "dev" file will be created, showing the dev_t for the device, if
2487 * the dev_t is not 0,0.
2488 * If a pointer to a parent struct device is passed in, the newly created
2489 * struct device will be a child of that device in sysfs.
2490 * The pointer to the struct device will be returned from the call.
2491 * Any further sysfs files that might be required can be created using this
2492 * pointer.
2493 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002494 * Returns &struct device pointer on success, or ERR_PTR() on error.
2495 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002496 * Note: the struct class passed to this function must have previously
2497 * been created with a call to class_create().
2498 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002499struct device *device_create(struct class *class, struct device *parent,
2500 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002501{
2502 va_list vargs;
2503 struct device *dev;
2504
2505 va_start(vargs, fmt);
2506 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2507 va_end(vargs);
2508 return dev;
2509}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002510EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002511
Guenter Roeck39ef3112013-07-14 16:05:57 -07002512/**
2513 * device_create_with_groups - creates a device and registers it with sysfs
2514 * @class: pointer to the struct class that this device should be registered to
2515 * @parent: pointer to the parent struct device of this new device, if any
2516 * @devt: the dev_t for the char device to be added
2517 * @drvdata: the data to be added to the device for callbacks
2518 * @groups: NULL-terminated list of attribute groups to be created
2519 * @fmt: string for the device's name
2520 *
2521 * This function can be used by char device classes. A struct device
2522 * will be created in sysfs, registered to the specified class.
2523 * Additional attributes specified in the groups parameter will also
2524 * be created automatically.
2525 *
2526 * A "dev" file will be created, showing the dev_t for the device, if
2527 * the dev_t is not 0,0.
2528 * If a pointer to a parent struct device is passed in, the newly created
2529 * struct device will be a child of that device in sysfs.
2530 * The pointer to the struct device will be returned from the call.
2531 * Any further sysfs files that might be required can be created using this
2532 * pointer.
2533 *
2534 * Returns &struct device pointer on success, or ERR_PTR() on error.
2535 *
2536 * Note: the struct class passed to this function must have previously
2537 * been created with a call to class_create().
2538 */
2539struct device *device_create_with_groups(struct class *class,
2540 struct device *parent, dev_t devt,
2541 void *drvdata,
2542 const struct attribute_group **groups,
2543 const char *fmt, ...)
2544{
2545 va_list vargs;
2546 struct device *dev;
2547
2548 va_start(vargs, fmt);
2549 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2550 fmt, vargs);
2551 va_end(vargs);
2552 return dev;
2553}
2554EXPORT_SYMBOL_GPL(device_create_with_groups);
2555
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002556static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002557{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002558 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002559
Dave Youngcd354492008-01-28 16:56:11 +08002560 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002561}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002562
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002563/**
2564 * device_destroy - removes a device that was created with device_create()
2565 * @class: pointer to the struct class that this device was registered with
2566 * @devt: the dev_t of the device that was previously registered
2567 *
2568 * This call unregisters and cleans up a device that was created with a
2569 * call to device_create().
2570 */
2571void device_destroy(struct class *class, dev_t devt)
2572{
2573 struct device *dev;
2574
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002575 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002576 if (dev) {
2577 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002578 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002579 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002580}
2581EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002582
2583/**
2584 * device_rename - renames a device
2585 * @dev: the pointer to the struct device to be renamed
2586 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002587 *
2588 * It is the responsibility of the caller to provide mutual
2589 * exclusion between two different calls of device_rename
2590 * on the same device to ensure that new_name is valid and
2591 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002592 *
Timur Tabia5462512010-12-13 14:08:52 -06002593 * Note: Don't call this function. Currently, the networking layer calls this
2594 * function, but that will change. The following text from Kay Sievers offers
2595 * some insight:
2596 *
2597 * Renaming devices is racy at many levels, symlinks and other stuff are not
2598 * replaced atomically, and you get a "move" uevent, but it's not easy to
2599 * connect the event to the old and new device. Device nodes are not renamed at
2600 * all, there isn't even support for that in the kernel now.
2601 *
2602 * In the meantime, during renaming, your target name might be taken by another
2603 * driver, creating conflicts. Or the old name is taken directly after you
2604 * renamed it -- then you get events for the same DEVPATH, before you even see
2605 * the "move" event. It's just a mess, and nothing new should ever rely on
2606 * kernel device renaming. Besides that, it's not even implemented now for
2607 * other things than (driver-core wise very simple) network devices.
2608 *
2609 * We are currently about to change network renaming in udev to completely
2610 * disallow renaming of devices in the same namespace as the kernel uses,
2611 * because we can't solve the problems properly, that arise with swapping names
2612 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2613 * be allowed to some other name than eth[0-9]*, for the aforementioned
2614 * reasons.
2615 *
2616 * Make up a "real" name in the driver before you register anything, or add
2617 * some other attributes for userspace to find the device, or use udev to add
2618 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2619 * don't even want to get into that and try to implement the missing pieces in
2620 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002621 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002622int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002623{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002624 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002625 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002626 int error;
2627
2628 dev = get_device(dev);
2629 if (!dev)
2630 return -EINVAL;
2631
ethan.zhao69df7532013-10-13 22:12:35 +08002632 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002633
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002634 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002635 if (!old_device_name) {
2636 error = -ENOMEM;
2637 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002638 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002639
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002640 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002641 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2642 kobj, old_device_name,
2643 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002644 if (error)
2645 goto out;
2646 }
Kay Sievers39aba962010-09-04 22:33:14 -07002647
Tejun Heo4b30ee52013-09-11 22:29:06 -04002648 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002649 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002650 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002651
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002652out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002653 put_device(dev);
2654
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002655 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002656
2657 return error;
2658}
Johannes Berga2807db2007-02-28 12:38:31 +01002659EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002660
2661static int device_move_class_links(struct device *dev,
2662 struct device *old_parent,
2663 struct device *new_parent)
2664{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002665 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002666
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002667 if (old_parent)
2668 sysfs_remove_link(&dev->kobj, "device");
2669 if (new_parent)
2670 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2671 "device");
2672 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002673}
2674
2675/**
2676 * device_move - moves a device to a new parent
2677 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002678 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002679 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002680 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002681int device_move(struct device *dev, struct device *new_parent,
2682 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002683{
2684 int error;
2685 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002686 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002687
2688 dev = get_device(dev);
2689 if (!dev)
2690 return -EINVAL;
2691
Cornelia Huckffa6a702009-03-04 12:44:00 +01002692 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002693 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002694 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01002695
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002696 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2697 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002698 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002699 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002700 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002701 put_device(new_parent);
2702 goto out;
2703 }
2704 old_parent = dev->parent;
2705 dev->parent = new_parent;
2706 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002707 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002708 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002709 klist_add_tail(&dev->p->knode_parent,
2710 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002711 set_dev_node(dev, dev_to_node(new_parent));
2712 }
2713
Rabin Vincentbdd40342012-04-23 09:16:36 +02002714 if (dev->class) {
2715 error = device_move_class_links(dev, old_parent, new_parent);
2716 if (error) {
2717 /* We ignore errors on cleanup since we're hosed anyway... */
2718 device_move_class_links(dev, new_parent, old_parent);
2719 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2720 if (new_parent)
2721 klist_remove(&dev->p->knode_parent);
2722 dev->parent = old_parent;
2723 if (old_parent) {
2724 klist_add_tail(&dev->p->knode_parent,
2725 &old_parent->p->klist_children);
2726 set_dev_node(dev, dev_to_node(old_parent));
2727 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002728 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002729 cleanup_glue_dir(dev, new_parent_kobj);
2730 put_device(new_parent);
2731 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002732 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002733 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002734 switch (dpm_order) {
2735 case DPM_ORDER_NONE:
2736 break;
2737 case DPM_ORDER_DEV_AFTER_PARENT:
2738 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002739 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002740 break;
2741 case DPM_ORDER_PARENT_BEFORE_DEV:
2742 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002743 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002744 break;
2745 case DPM_ORDER_DEV_LAST:
2746 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002747 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002748 break;
2749 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002750
Cornelia Huck8a824722006-11-20 17:07:51 +01002751 put_device(old_parent);
2752out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002753 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002754 put_device(dev);
2755 return error;
2756}
Cornelia Huck8a824722006-11-20 17:07:51 +01002757EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002758
2759/**
2760 * device_shutdown - call ->shutdown() on each device to shutdown.
2761 */
2762void device_shutdown(void)
2763{
Benson Leungf123db82013-09-24 20:05:11 -07002764 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002765
Hugh Daschbach62458382010-03-22 10:36:37 -07002766 spin_lock(&devices_kset->list_lock);
2767 /*
2768 * Walk the devices list backward, shutting down each in turn.
2769 * Beware that device unplug events may also start pulling
2770 * devices offline, even as the system is shutting down.
2771 */
2772 while (!list_empty(&devices_kset->list)) {
2773 dev = list_entry(devices_kset->list.prev, struct device,
2774 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002775
2776 /*
2777 * hold reference count of device's parent to
2778 * prevent it from being freed because parent's
2779 * lock is to be held
2780 */
Benson Leungf123db82013-09-24 20:05:11 -07002781 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002782 get_device(dev);
2783 /*
2784 * Make sure the device is off the kset list, in the
2785 * event that dev->*->shutdown() doesn't remove it.
2786 */
2787 list_del_init(&dev->kobj.entry);
2788 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002789
Ming Leid1c6c032012-06-22 18:01:40 +08002790 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002791 if (parent)
2792 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002793 device_lock(dev);
2794
Alan Sternfe6b91f2011-12-06 23:24:52 +01002795 /* Don't allow any more runtime suspends */
2796 pm_runtime_get_noresume(dev);
2797 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002798
Michal Suchanek75216212017-08-11 15:44:43 +02002799 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07002800 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02002801 dev_info(dev, "shutdown_pre\n");
2802 dev->class->shutdown_pre(dev);
2803 }
2804 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002805 if (initcall_debug)
2806 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002807 dev->bus->shutdown(dev);
2808 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002809 if (initcall_debug)
2810 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002811 dev->driver->shutdown(dev);
2812 }
Ming Leid1c6c032012-06-22 18:01:40 +08002813
2814 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002815 if (parent)
2816 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002817
Hugh Daschbach62458382010-03-22 10:36:37 -07002818 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002819 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002820
2821 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002822 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002823 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002824}
Joe Perches99bcf212010-06-27 01:02:34 +00002825
2826/*
2827 * Device logging functions
2828 */
2829
2830#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002831static int
2832create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002833{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002834 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002835 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002836
Kay Sieversc4e00da2012-05-03 02:29:59 +02002837 if (dev->class)
2838 subsys = dev->class->name;
2839 else if (dev->bus)
2840 subsys = dev->bus->name;
2841 else
Joe Perches798efc62012-09-12 20:11:29 -07002842 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002843
Joe Perches798efc62012-09-12 20:11:29 -07002844 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002845 if (pos >= hdrlen)
2846 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002847
2848 /*
2849 * Add device identifier DEVICE=:
2850 * b12:8 block dev_t
2851 * c127:3 char dev_t
2852 * n8 netdev ifindex
2853 * +sound:card0 subsystem:devname
2854 */
2855 if (MAJOR(dev->devt)) {
2856 char c;
2857
2858 if (strcmp(subsys, "block") == 0)
2859 c = 'b';
2860 else
2861 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002862 pos++;
2863 pos += snprintf(hdr + pos, hdrlen - pos,
2864 "DEVICE=%c%u:%u",
2865 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002866 } else if (strcmp(subsys, "net") == 0) {
2867 struct net_device *net = to_net_dev(dev);
2868
Joe Perches798efc62012-09-12 20:11:29 -07002869 pos++;
2870 pos += snprintf(hdr + pos, hdrlen - pos,
2871 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002872 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002873 pos++;
2874 pos += snprintf(hdr + pos, hdrlen - pos,
2875 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002876 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002877
Ben Hutchings655e5b72014-08-26 00:34:44 -07002878 if (pos >= hdrlen)
2879 goto overflow;
2880
Joe Perches798efc62012-09-12 20:11:29 -07002881 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002882
2883overflow:
2884 dev_WARN(dev, "device/subsystem name too long");
2885 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002886}
Joe Perches798efc62012-09-12 20:11:29 -07002887
Joe Perches05e4e5b2012-09-12 20:13:37 -07002888int dev_vprintk_emit(int level, const struct device *dev,
2889 const char *fmt, va_list args)
2890{
2891 char hdr[128];
2892 size_t hdrlen;
2893
2894 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2895
2896 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2897}
2898EXPORT_SYMBOL(dev_vprintk_emit);
2899
2900int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2901{
2902 va_list args;
2903 int r;
2904
2905 va_start(args, fmt);
2906
2907 r = dev_vprintk_emit(level, dev, fmt, args);
2908
2909 va_end(args);
2910
2911 return r;
2912}
2913EXPORT_SYMBOL(dev_printk_emit);
2914
Joe Perchesd1f10522014-12-25 15:07:04 -08002915static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07002916 struct va_format *vaf)
2917{
Joe Perchesd1f10522014-12-25 15:07:04 -08002918 if (dev)
2919 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2920 dev_driver_string(dev), dev_name(dev), vaf);
2921 else
2922 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002923}
Joe Perches99bcf212010-06-27 01:02:34 +00002924
Joe Perchesd1f10522014-12-25 15:07:04 -08002925void dev_printk(const char *level, const struct device *dev,
2926 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00002927{
2928 struct va_format vaf;
2929 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00002930
2931 va_start(args, fmt);
2932
2933 vaf.fmt = fmt;
2934 vaf.va = &args;
2935
Joe Perchesd1f10522014-12-25 15:07:04 -08002936 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002937
Joe Perches99bcf212010-06-27 01:02:34 +00002938 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00002939}
2940EXPORT_SYMBOL(dev_printk);
2941
2942#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08002943void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00002944{ \
2945 struct va_format vaf; \
2946 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00002947 \
2948 va_start(args, fmt); \
2949 \
2950 vaf.fmt = fmt; \
2951 vaf.va = &args; \
2952 \
Joe Perchesd1f10522014-12-25 15:07:04 -08002953 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002954 \
Joe Perches99bcf212010-06-27 01:02:34 +00002955 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00002956} \
2957EXPORT_SYMBOL(func);
2958
2959define_dev_printk_level(dev_emerg, KERN_EMERG);
2960define_dev_printk_level(dev_alert, KERN_ALERT);
2961define_dev_printk_level(dev_crit, KERN_CRIT);
2962define_dev_printk_level(dev_err, KERN_ERR);
2963define_dev_printk_level(dev_warn, KERN_WARNING);
2964define_dev_printk_level(dev_notice, KERN_NOTICE);
2965define_dev_printk_level(_dev_info, KERN_INFO);
2966
2967#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002968
2969static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2970{
2971 return fwnode && !IS_ERR(fwnode->secondary);
2972}
2973
2974/**
2975 * set_primary_fwnode - Change the primary firmware node of a given device.
2976 * @dev: Device to handle.
2977 * @fwnode: New primary firmware node of the device.
2978 *
2979 * Set the device's firmware node pointer to @fwnode, but if a secondary
2980 * firmware node of the device is present, preserve it.
2981 */
2982void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2983{
2984 if (fwnode) {
2985 struct fwnode_handle *fn = dev->fwnode;
2986
2987 if (fwnode_is_primary(fn))
2988 fn = fn->secondary;
2989
Mika Westerberg55f89a82015-11-30 17:11:39 +02002990 if (fn) {
2991 WARN_ON(fwnode->secondary);
2992 fwnode->secondary = fn;
2993 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002994 dev->fwnode = fwnode;
2995 } else {
2996 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
2997 dev->fwnode->secondary : NULL;
2998 }
2999}
3000EXPORT_SYMBOL_GPL(set_primary_fwnode);
3001
3002/**
3003 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3004 * @dev: Device to handle.
3005 * @fwnode: New secondary firmware node of the device.
3006 *
3007 * If a primary firmware node of the device is present, set its secondary
3008 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3009 * @fwnode.
3010 */
3011void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3012{
3013 if (fwnode)
3014 fwnode->secondary = ERR_PTR(-ENODEV);
3015
3016 if (fwnode_is_primary(dev->fwnode))
3017 dev->fwnode->secondary = fwnode;
3018 else
3019 dev->fwnode = fwnode;
3020}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003021
3022/**
3023 * device_set_of_node_from_dev - reuse device-tree node of another device
3024 * @dev: device whose device-tree node is being set
3025 * @dev2: device whose device-tree node is being reused
3026 *
3027 * Takes another reference to the new device-tree node after first dropping
3028 * any reference held to the old node.
3029 */
3030void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3031{
3032 of_node_put(dev->of_node);
3033 dev->of_node = of_node_get(dev2->of_node);
3034 dev->of_node_reused = true;
3035}
3036EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);