blob: 6bb60fb6a30b7b9b4fd42e2872261317b38c22b5 [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{
Kay Sievers60a96a52007-07-08 22:29:26 +0200984 enum kobject_action action;
985
Kay Sievers3f5468c2010-01-14 22:54:37 +0100986 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200987 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100988 else
989 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200990 return count;
991}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700992static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200993
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700994static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200995 char *buf)
996{
997 bool val;
998
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200999 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001000 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001001 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001002 return sprintf(buf, "%u\n", val);
1003}
1004
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001005static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001006 const char *buf, size_t count)
1007{
1008 bool val;
1009 int ret;
1010
1011 ret = strtobool(buf, &val);
1012 if (ret < 0)
1013 return ret;
1014
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001015 ret = lock_device_hotplug_sysfs();
1016 if (ret)
1017 return ret;
1018
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001019 ret = val ? device_online(dev) : device_offline(dev);
1020 unlock_device_hotplug();
1021 return ret < 0 ? ret : count;
1022}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001023static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001024
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001025int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001026{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001027 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001028}
1029
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001030void device_remove_groups(struct device *dev,
1031 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001032{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001033 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001034}
1035
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001036static int device_add_attrs(struct device *dev)
1037{
1038 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001039 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001040 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001041
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001042 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001043 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001044 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001045 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001046 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001047
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001048 if (type) {
1049 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001050 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001051 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001052 }
1053
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001054 error = device_add_groups(dev, dev->groups);
1055 if (error)
1056 goto err_remove_type_groups;
1057
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001058 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001059 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001060 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001061 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001062 }
1063
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001064 return 0;
1065
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001066 err_remove_dev_groups:
1067 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001068 err_remove_type_groups:
1069 if (type)
1070 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001071 err_remove_class_groups:
1072 if (class)
1073 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001074
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001075 return error;
1076}
1077
1078static void device_remove_attrs(struct device *dev)
1079{
1080 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001081 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001082
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001083 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001084 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001085
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001086 if (type)
1087 device_remove_groups(dev, type->groups);
1088
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001089 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001090 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001091}
1092
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001093static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001094 char *buf)
1095{
1096 return print_dev_t(buf, dev->devt);
1097}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001098static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001099
Kay Sieversca22e562011-12-14 14:29:38 -08001100/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001101struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001104 * devices_kset_move_before - Move device in the devices_kset's list.
1105 * @deva: Device to move.
1106 * @devb: Device @deva should come before.
1107 */
1108static void devices_kset_move_before(struct device *deva, struct device *devb)
1109{
1110 if (!devices_kset)
1111 return;
1112 pr_debug("devices_kset: Moving %s before %s\n",
1113 dev_name(deva), dev_name(devb));
1114 spin_lock(&devices_kset->list_lock);
1115 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1116 spin_unlock(&devices_kset->list_lock);
1117}
1118
1119/**
1120 * devices_kset_move_after - Move device in the devices_kset's list.
1121 * @deva: Device to move
1122 * @devb: Device @deva should come after.
1123 */
1124static void devices_kset_move_after(struct device *deva, struct device *devb)
1125{
1126 if (!devices_kset)
1127 return;
1128 pr_debug("devices_kset: Moving %s after %s\n",
1129 dev_name(deva), dev_name(devb));
1130 spin_lock(&devices_kset->list_lock);
1131 list_move(&deva->kobj.entry, &devb->kobj.entry);
1132 spin_unlock(&devices_kset->list_lock);
1133}
1134
1135/**
1136 * devices_kset_move_last - move the device to the end of devices_kset's list.
1137 * @dev: device to move
1138 */
1139void devices_kset_move_last(struct device *dev)
1140{
1141 if (!devices_kset)
1142 return;
1143 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1144 spin_lock(&devices_kset->list_lock);
1145 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1146 spin_unlock(&devices_kset->list_lock);
1147}
1148
1149/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001150 * device_create_file - create sysfs attribute file for device.
1151 * @dev: device.
1152 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001154int device_create_file(struct device *dev,
1155 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001158
1159 if (dev) {
1160 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001161 "Attribute %s: write permission without 'store'\n",
1162 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001163 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001164 "Attribute %s: read permission without 'show'\n",
1165 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001167 }
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return error;
1170}
David Graham White86df2682013-07-21 20:41:14 -04001171EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001174 * device_remove_file - remove sysfs attribute file.
1175 * @dev: device.
1176 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001178void device_remove_file(struct device *dev,
1179 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001181 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
David Graham White86df2682013-07-21 20:41:14 -04001184EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001186/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001187 * device_remove_file_self - remove sysfs attribute file from its own method.
1188 * @dev: device.
1189 * @attr: device attribute descriptor.
1190 *
1191 * See kernfs_remove_self() for details.
1192 */
1193bool device_remove_file_self(struct device *dev,
1194 const struct device_attribute *attr)
1195{
1196 if (dev)
1197 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1198 else
1199 return false;
1200}
1201EXPORT_SYMBOL_GPL(device_remove_file_self);
1202
1203/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001204 * device_create_bin_file - create sysfs binary attribute file for device.
1205 * @dev: device.
1206 * @attr: device binary attribute descriptor.
1207 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001208int device_create_bin_file(struct device *dev,
1209 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001210{
1211 int error = -EINVAL;
1212 if (dev)
1213 error = sysfs_create_bin_file(&dev->kobj, attr);
1214 return error;
1215}
1216EXPORT_SYMBOL_GPL(device_create_bin_file);
1217
1218/**
1219 * device_remove_bin_file - remove sysfs binary attribute file
1220 * @dev: device.
1221 * @attr: device binary attribute descriptor.
1222 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001223void device_remove_bin_file(struct device *dev,
1224 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001225{
1226 if (dev)
1227 sysfs_remove_bin_file(&dev->kobj, attr);
1228}
1229EXPORT_SYMBOL_GPL(device_remove_bin_file);
1230
James Bottomley34bb61f2005-09-06 16:56:51 -07001231static void klist_children_get(struct klist_node *n)
1232{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001233 struct device_private *p = to_device_private_parent(n);
1234 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001235
1236 get_device(dev);
1237}
1238
1239static void klist_children_put(struct klist_node *n)
1240{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001241 struct device_private *p = to_device_private_parent(n);
1242 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001243
1244 put_device(dev);
1245}
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001248 * device_initialize - init device structure.
1249 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 *
Cornelia Huck57394112008-09-03 18:26:40 +02001251 * This prepares the device for use by other layers by initializing
1252 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001253 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001254 * that function, though it can also be called separately, so one
1255 * may use @dev's fields. In particular, get_device()/put_device()
1256 * may be used for reference counting of @dev after calling this
1257 * function.
1258 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001259 * All fields in @dev must be initialized by the caller to 0, except
1260 * for those explicitly set to some other value. The simplest
1261 * approach is to use kzalloc() to allocate the structure containing
1262 * @dev.
1263 *
Cornelia Huck57394112008-09-03 18:26:40 +02001264 * NOTE: Use put_device() to give up your reference instead of freeing
1265 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267void device_initialize(struct device *dev)
1268{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001269 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001270 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001272 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001273 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001274 spin_lock_init(&dev->devres_lock);
1275 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001276 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001277 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001278#ifdef CONFIG_GENERIC_MSI_IRQ
1279 INIT_LIST_HEAD(&dev->msi_list);
1280#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001281 INIT_LIST_HEAD(&dev->links.consumers);
1282 INIT_LIST_HEAD(&dev->links.suppliers);
1283 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
David Graham White86df2682013-07-21 20:41:14 -04001285EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Tejun Heod73ce002013-03-12 11:30:05 -07001287struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001288{
Kay Sievers86406242007-03-14 03:25:56 +01001289 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001290
Kay Sievers86406242007-03-14 03:25:56 +01001291 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001292 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001293 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001294
Kay Sievers86406242007-03-14 03:25:56 +01001295 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001296}
1297
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001298struct class_dir {
1299 struct kobject kobj;
1300 struct class *class;
1301};
1302
1303#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1304
1305static void class_dir_release(struct kobject *kobj)
1306{
1307 struct class_dir *dir = to_class_dir(kobj);
1308 kfree(dir);
1309}
1310
1311static const
1312struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1313{
1314 struct class_dir *dir = to_class_dir(kobj);
1315 return dir->class->ns_type;
1316}
1317
1318static struct kobj_type class_dir_ktype = {
1319 .release = class_dir_release,
1320 .sysfs_ops = &kobj_sysfs_ops,
1321 .child_ns_type = class_dir_child_ns_type
1322};
1323
1324static struct kobject *
1325class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1326{
1327 struct class_dir *dir;
1328 int retval;
1329
1330 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1331 if (!dir)
1332 return NULL;
1333
1334 dir->class = class;
1335 kobject_init(&dir->kobj, &class_dir_ktype);
1336
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001337 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001338
1339 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1340 if (retval < 0) {
1341 kobject_put(&dir->kobj);
1342 return NULL;
1343 }
1344 return &dir->kobj;
1345}
1346
Yijing Wange4a60d12014-11-07 12:05:49 +08001347static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001348
Kay Sieversda231fd2007-11-21 17:29:15 +01001349static struct kobject *get_device_parent(struct device *dev,
1350 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001351{
Kay Sievers86406242007-03-14 03:25:56 +01001352 if (dev->class) {
1353 struct kobject *kobj = NULL;
1354 struct kobject *parent_kobj;
1355 struct kobject *k;
1356
Randy Dunlapead454f2010-09-24 14:36:49 -07001357#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001358 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001359 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001360 if (parent && parent->class == &block_class)
1361 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001362 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001363 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001364#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001365
Kay Sievers86406242007-03-14 03:25:56 +01001366 /*
1367 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001368 * Class-devices with a non class-device as parent, live
1369 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001370 */
1371 if (parent == NULL)
1372 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001373 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001374 return &parent->kobj;
1375 else
1376 parent_kobj = &parent->kobj;
1377
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001378 mutex_lock(&gdp_mutex);
1379
Kay Sievers86406242007-03-14 03:25:56 +01001380 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001381 spin_lock(&dev->class->p->glue_dirs.list_lock);
1382 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001383 if (k->parent == parent_kobj) {
1384 kobj = kobject_get(k);
1385 break;
1386 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001387 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001388 if (kobj) {
1389 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001390 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001391 }
Kay Sievers86406242007-03-14 03:25:56 +01001392
1393 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001394 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001395 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001396 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001397 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001398 }
1399
Kay Sieversca22e562011-12-14 14:29:38 -08001400 /* subsystems can specify a default root directory for their devices */
1401 if (!parent && dev->bus && dev->bus->dev_root)
1402 return &dev->bus->dev_root->kobj;
1403
Kay Sievers86406242007-03-14 03:25:56 +01001404 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001405 return &parent->kobj;
1406 return NULL;
1407}
Kay Sieversda231fd2007-11-21 17:29:15 +01001408
Ming Leicebf8fd2016-07-10 19:27:36 +08001409static inline bool live_in_glue_dir(struct kobject *kobj,
1410 struct device *dev)
1411{
1412 if (!kobj || !dev->class ||
1413 kobj->kset != &dev->class->p->glue_dirs)
1414 return false;
1415 return true;
1416}
1417
1418static inline struct kobject *get_glue_dir(struct device *dev)
1419{
1420 return dev->kobj.parent;
1421}
1422
1423/*
1424 * make sure cleaning up dir as the last step, we need to make
1425 * sure .release handler of kobject is run with holding the
1426 * global lock
1427 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001428static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001429{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001430 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001431 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001432 return;
1433
Yijing Wange4a60d12014-11-07 12:05:49 +08001434 mutex_lock(&gdp_mutex);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001435 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001436 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001437}
Cornelia Huck63b69712008-01-21 16:09:44 +01001438
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001439static int device_add_class_symlinks(struct device *dev)
1440{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001441 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001442 int error;
1443
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001444 if (of_node) {
1445 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
1446 if (error)
1447 dev_warn(dev, "Error %d creating of_node link\n",error);
1448 /* An error here doesn't warrant bringing down the device */
1449 }
1450
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001451 if (!dev->class)
1452 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001453
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001454 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001455 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001456 "subsystem");
1457 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001458 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001459
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001460 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001461 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1462 "device");
1463 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001464 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001465 }
Kay Sievers39aba962010-09-04 22:33:14 -07001466
Randy Dunlapead454f2010-09-24 14:36:49 -07001467#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001468 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001469 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001470 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001471#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001472
1473 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001474 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001475 &dev->kobj, dev_name(dev));
1476 if (error)
1477 goto out_device;
1478
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001479 return 0;
1480
Kay Sievers39aba962010-09-04 22:33:14 -07001481out_device:
1482 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001483
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001484out_subsys:
1485 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001486out_devnode:
1487 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001488 return error;
1489}
1490
1491static void device_remove_class_symlinks(struct device *dev)
1492{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001493 if (dev_of_node(dev))
1494 sysfs_remove_link(&dev->kobj, "of_node");
1495
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001496 if (!dev->class)
1497 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001498
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001499 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001500 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001501 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001502#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001503 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001504 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001505#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001506 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001510 * dev_set_name - set a device name
1511 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001512 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001513 */
1514int dev_set_name(struct device *dev, const char *fmt, ...)
1515{
1516 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001517 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001518
1519 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001520 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001521 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001522 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001523}
1524EXPORT_SYMBOL_GPL(dev_set_name);
1525
1526/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001527 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1528 * @dev: device
1529 *
1530 * By default we select char/ for new entries. Setting class->dev_obj
1531 * to NULL prevents an entry from being created. class->dev_kobj must
1532 * be set (or cleared) before any devices are registered to the class
1533 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001534 * device_remove_sys_dev_entry() will disagree about the presence of
1535 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001536 */
1537static struct kobject *device_to_dev_kobj(struct device *dev)
1538{
1539 struct kobject *kobj;
1540
1541 if (dev->class)
1542 kobj = dev->class->dev_kobj;
1543 else
1544 kobj = sysfs_dev_char_kobj;
1545
1546 return kobj;
1547}
1548
1549static int device_create_sys_dev_entry(struct device *dev)
1550{
1551 struct kobject *kobj = device_to_dev_kobj(dev);
1552 int error = 0;
1553 char devt_str[15];
1554
1555 if (kobj) {
1556 format_dev_t(devt_str, dev->devt);
1557 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1558 }
1559
1560 return error;
1561}
1562
1563static void device_remove_sys_dev_entry(struct device *dev)
1564{
1565 struct kobject *kobj = device_to_dev_kobj(dev);
1566 char devt_str[15];
1567
1568 if (kobj) {
1569 format_dev_t(devt_str, dev->devt);
1570 sysfs_remove_link(kobj, devt_str);
1571 }
1572}
1573
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001574int device_private_init(struct device *dev)
1575{
1576 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1577 if (!dev->p)
1578 return -ENOMEM;
1579 dev->p->device = dev;
1580 klist_init(&dev->p->klist_children, klist_children_get,
1581 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001582 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001583 return 0;
1584}
1585
Dan Williamse105b8b2008-04-21 10:51:07 -07001586/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001587 * device_add - add device to device hierarchy.
1588 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001590 * This is part 2 of device_register(), though may be called
1591 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 *
Cornelia Huck57394112008-09-03 18:26:40 +02001593 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001594 * to the global and sibling lists for the device, then
1595 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001596 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001597 * Do not call this routine or device_register() more than once for
1598 * any device structure. The driver model core is not designed to work
1599 * with devices that get unregistered and then spring back to life.
1600 * (Among other things, it's very hard to guarantee that all references
1601 * to the previous incarnation of @dev have been dropped.) Allocate
1602 * and register a fresh new struct device instead.
1603 *
Cornelia Huck57394112008-09-03 18:26:40 +02001604 * NOTE: _Never_ directly free @dev after calling this function, even
1605 * if it returned an error! Always use put_device() to give up your
1606 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
1608int device_add(struct device *dev)
1609{
1610 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -08001611 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001612 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001613 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001614 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001617 if (!dev)
1618 goto done;
1619
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001620 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001621 error = device_private_init(dev);
1622 if (error)
1623 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001624 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001625
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001626 /*
1627 * for statically allocated devices, which should all be converted
1628 * some day, we need to initialize the name. We prevent reading back
1629 * the name, and force the use of dev_name()
1630 */
1631 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001632 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001633 dev->init_name = NULL;
1634 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001635
Kay Sieversca22e562011-12-14 14:29:38 -08001636 /* subsystems can specify simple device enumeration */
1637 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1638 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1639
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001640 if (!dev_name(dev)) {
1641 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001642 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001645 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001648 kobj = get_device_parent(dev, parent);
1649 if (kobj)
1650 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Yinghai Lu0d358f22008-02-19 03:20:41 -08001652 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001653 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001654 set_dev_node(dev, dev_to_node(parent));
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001657 /* we require the name to be set before, and pass NULL */
1658 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001659 if (error) {
1660 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001662 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001663
Brian Walsh37022642006-08-14 22:43:19 -07001664 /* notify platform of device entry */
1665 if (platform_notify)
1666 platform_notify(dev);
1667
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001668 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001669 if (error)
1670 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001671
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001672 error = device_add_class_symlinks(dev);
1673 if (error)
1674 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001675 error = device_add_attrs(dev);
1676 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001677 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001678 error = bus_add_device(dev);
1679 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001681 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001682 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001683 goto DPMError;
1684 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001685
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001686 if (MAJOR(dev->devt)) {
1687 error = device_create_file(dev, &dev_attr_dev);
1688 if (error)
1689 goto DevAttrError;
1690
1691 error = device_create_sys_dev_entry(dev);
1692 if (error)
1693 goto SysEntryError;
1694
1695 devtmpfs_create_node(dev);
1696 }
1697
Alan Sternec0676ee2008-12-05 14:10:31 -05001698 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001699 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001700 */
1701 if (dev->bus)
1702 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1703 BUS_NOTIFY_ADD_DEVICE, dev);
1704
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001705 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001706 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001708 klist_add_tail(&dev->p->knode_parent,
1709 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001711 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001712 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001713 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001714 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001715 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001716
1717 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001718 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001719 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001720 if (class_intf->add_dev)
1721 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001722 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001723 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001724done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 put_device(dev);
1726 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001727 SysEntryError:
1728 if (MAJOR(dev->devt))
1729 device_remove_file(dev, &dev_attr_dev);
1730 DevAttrError:
1731 device_pm_remove(dev);
1732 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001733 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001734 bus_remove_device(dev);
1735 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001736 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001737 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001738 device_remove_class_symlinks(dev);
1739 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001740 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001741 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001742 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001743 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 kobject_del(&dev->kobj);
1745 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001746 cleanup_glue_dir(dev, glue_dir);
Markus Elfring5f0163a2015-02-05 11:48:26 +01001747 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001748name_error:
1749 kfree(dev->p);
1750 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001751 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
David Graham White86df2682013-07-21 20:41:14 -04001753EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001756 * device_register - register a device with the system.
1757 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001759 * This happens in two clean steps - initialize the device
1760 * and add it to the system. The two steps can be called
1761 * separately, but this is the easiest and most common.
1762 * I.e. you should only call the two helpers separately if
1763 * have a clearly defined need to use and refcount the device
1764 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001765 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001766 * For more information, see the kerneldoc for device_initialize()
1767 * and device_add().
1768 *
Cornelia Huck57394112008-09-03 18:26:40 +02001769 * NOTE: _Never_ directly free @dev after calling this function, even
1770 * if it returned an error! Always use put_device() to give up the
1771 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773int device_register(struct device *dev)
1774{
1775 device_initialize(dev);
1776 return device_add(dev);
1777}
David Graham White86df2682013-07-21 20:41:14 -04001778EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001781 * get_device - increment reference count for device.
1782 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001784 * This simply forwards the call to kobject_get(), though
1785 * we do take care to provide for the case that we get a NULL
1786 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001788struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001790 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
David Graham White86df2682013-07-21 20:41:14 -04001792EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001795 * put_device - decrement reference count.
1796 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001798void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001800 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (dev)
1802 kobject_put(&dev->kobj);
1803}
David Graham White86df2682013-07-21 20:41:14 -04001804EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001807 * device_del - delete device from system.
1808 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001810 * This is the first part of the device unregistration
1811 * sequence. This removes the device from the lists we control
1812 * from here, has it removed from the other driver model
1813 * subsystems it was added to in device_add(), and removes it
1814 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001816 * NOTE: this should be called manually _iff_ device_add() was
1817 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001819void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001821 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08001822 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001823 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Alan Sternec0676ee2008-12-05 14:10:31 -05001825 /* Notify clients of device removal. This call must come
1826 * before dpm_sysfs_remove().
1827 */
1828 if (dev->bus)
1829 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1830 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001831
1832 device_links_purge(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001833 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001835 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001836 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001837 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001838 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001839 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001840 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001841 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001842 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001843
Kay Sieversca22e562011-12-14 14:29:38 -08001844 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001845 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001846 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001847 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001848 if (class_intf->remove_dev)
1849 class_intf->remove_dev(dev, class_intf);
1850 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001851 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001852 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001853 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001854 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001855 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001856 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001857 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001858 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02001859 device_remove_properties(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /* Notify the platform of the removal, in case they
1862 * need to do anything...
1863 */
1864 if (platform_notify_remove)
1865 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02001866 if (dev->bus)
1867 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1868 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001869 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001870 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08001872 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01001873 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
David Graham White86df2682013-07-21 20:41:14 -04001875EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001878 * device_unregister - unregister device from system.
1879 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001881 * We do this in two parts, like we do device_register(). First,
1882 * we remove it from all the subsystems with device_del(), then
1883 * we decrement the reference count via put_device(). If that
1884 * is the final reference count, the device will be cleaned up
1885 * via device_release() above. Otherwise, the structure will
1886 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001888void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001890 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 device_del(dev);
1892 put_device(dev);
1893}
David Graham White86df2682013-07-21 20:41:14 -04001894EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03001896static struct device *prev_device(struct klist_iter *i)
1897{
1898 struct klist_node *n = klist_prev(i);
1899 struct device *dev = NULL;
1900 struct device_private *p;
1901
1902 if (n) {
1903 p = to_device_private_parent(n);
1904 dev = p->device;
1905 }
1906 return dev;
1907}
1908
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001909static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001910{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001911 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001912 struct device *dev = NULL;
1913 struct device_private *p;
1914
1915 if (n) {
1916 p = to_device_private_parent(n);
1917 dev = p->device;
1918 }
1919 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001920}
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001923 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001924 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001925 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07001926 * @uid: returned file owner
1927 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001928 * @tmp: possibly allocated string
1929 *
1930 * Return the relative path of a possible device node.
1931 * Non-default names may need to allocate a memory to compose
1932 * a name. This memory is returned in tmp and needs to be
1933 * freed by the caller.
1934 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001935const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001936 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07001937 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001938{
1939 char *s;
1940
1941 *tmp = NULL;
1942
1943 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001944 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07001945 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001946 if (*tmp)
1947 return *tmp;
1948
1949 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001950 if (dev->class && dev->class->devnode)
1951 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001952 if (*tmp)
1953 return *tmp;
1954
1955 /* return name without allocation, tmp == NULL */
1956 if (strchr(dev_name(dev), '!') == NULL)
1957 return dev_name(dev);
1958
1959 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07001960 s = kstrdup(dev_name(dev), GFP_KERNEL);
1961 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001962 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07001963 strreplace(s, '!', '/');
1964 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001965}
1966
1967/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001968 * device_for_each_child - device child iterator.
1969 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001970 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001971 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001973 * Iterate over @parent's child devices, and call @fn for each,
1974 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001976 * We check the return of @fn each time. If it returns anything
1977 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001979int device_for_each_child(struct device *parent, void *data,
1980 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001982 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001983 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 int error = 0;
1985
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001986 if (!parent->p)
1987 return 0;
1988
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001989 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001990 while ((child = next_device(&i)) && !error)
1991 error = fn(child, data);
1992 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return error;
1994}
David Graham White86df2682013-07-21 20:41:14 -04001995EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Cornelia Huck5ab69982006-11-16 15:42:07 +01001997/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03001998 * device_for_each_child_reverse - device child iterator in reversed order.
1999 * @parent: parent struct device.
2000 * @fn: function to be called for each device.
2001 * @data: data for the callback.
2002 *
2003 * Iterate over @parent's child devices, and call @fn for each,
2004 * passing it @data.
2005 *
2006 * We check the return of @fn each time. If it returns anything
2007 * other than 0, we break out and return that value.
2008 */
2009int device_for_each_child_reverse(struct device *parent, void *data,
2010 int (*fn)(struct device *dev, void *data))
2011{
2012 struct klist_iter i;
2013 struct device *child;
2014 int error = 0;
2015
2016 if (!parent->p)
2017 return 0;
2018
2019 klist_iter_init(&parent->p->klist_children, &i);
2020 while ((child = prev_device(&i)) && !error)
2021 error = fn(child, data);
2022 klist_iter_exit(&i);
2023 return error;
2024}
2025EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2026
2027/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002028 * device_find_child - device iterator for locating a particular device.
2029 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002030 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002031 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002032 *
2033 * This is similar to the device_for_each_child() function above, but it
2034 * returns a reference to a device that is 'found' for later use, as
2035 * determined by the @match callback.
2036 *
2037 * The callback should return 0 if the device doesn't match and non-zero
2038 * if it does. If the callback returns non-zero and a reference to the
2039 * current device can be obtained, this function will return to the caller
2040 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002041 *
2042 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002043 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002044struct device *device_find_child(struct device *parent, void *data,
2045 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002046{
2047 struct klist_iter i;
2048 struct device *child;
2049
2050 if (!parent)
2051 return NULL;
2052
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002053 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002054 while ((child = next_device(&i)))
2055 if (match(child, data) && get_device(child))
2056 break;
2057 klist_iter_exit(&i);
2058 return child;
2059}
David Graham White86df2682013-07-21 20:41:14 -04002060EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062int __init devices_init(void)
2063{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002064 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2065 if (!devices_kset)
2066 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002067 dev_kobj = kobject_create_and_add("dev", NULL);
2068 if (!dev_kobj)
2069 goto dev_kobj_err;
2070 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2071 if (!sysfs_dev_block_kobj)
2072 goto block_kobj_err;
2073 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2074 if (!sysfs_dev_char_kobj)
2075 goto char_kobj_err;
2076
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002077 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002078
2079 char_kobj_err:
2080 kobject_put(sysfs_dev_block_kobj);
2081 block_kobj_err:
2082 kobject_put(dev_kobj);
2083 dev_kobj_err:
2084 kset_unregister(devices_kset);
2085 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002088static int device_check_offline(struct device *dev, void *not_used)
2089{
2090 int ret;
2091
2092 ret = device_for_each_child(dev, NULL, device_check_offline);
2093 if (ret)
2094 return ret;
2095
2096 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2097}
2098
2099/**
2100 * device_offline - Prepare the device for hot-removal.
2101 * @dev: Device to be put offline.
2102 *
2103 * Execute the device bus type's .offline() callback, if present, to prepare
2104 * the device for a subsequent hot-removal. If that succeeds, the device must
2105 * not be used until either it is removed or its bus type's .online() callback
2106 * is executed.
2107 *
2108 * Call under device_hotplug_lock.
2109 */
2110int device_offline(struct device *dev)
2111{
2112 int ret;
2113
2114 if (dev->offline_disabled)
2115 return -EPERM;
2116
2117 ret = device_for_each_child(dev, NULL, device_check_offline);
2118 if (ret)
2119 return ret;
2120
2121 device_lock(dev);
2122 if (device_supports_offline(dev)) {
2123 if (dev->offline) {
2124 ret = 1;
2125 } else {
2126 ret = dev->bus->offline(dev);
2127 if (!ret) {
2128 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2129 dev->offline = true;
2130 }
2131 }
2132 }
2133 device_unlock(dev);
2134
2135 return ret;
2136}
2137
2138/**
2139 * device_online - Put the device back online after successful device_offline().
2140 * @dev: Device to be put back online.
2141 *
2142 * If device_offline() has been successfully executed for @dev, but the device
2143 * has not been removed subsequently, execute its bus type's .online() callback
2144 * to indicate that the device can be used again.
2145 *
2146 * Call under device_hotplug_lock.
2147 */
2148int device_online(struct device *dev)
2149{
2150 int ret = 0;
2151
2152 device_lock(dev);
2153 if (device_supports_offline(dev)) {
2154 if (dev->offline) {
2155 ret = dev->bus->online(dev);
2156 if (!ret) {
2157 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2158 dev->offline = false;
2159 }
2160 } else {
2161 ret = 1;
2162 }
2163 }
2164 device_unlock(dev);
2165
2166 return ret;
2167}
2168
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002169struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002170 struct device dev;
2171 struct module *owner;
2172};
2173
Josh Triplett93058422012-11-18 21:27:55 -08002174static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002175{
2176 return container_of(d, struct root_device, dev);
2177}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002178
2179static void root_device_release(struct device *dev)
2180{
2181 kfree(to_root_device(dev));
2182}
2183
2184/**
2185 * __root_device_register - allocate and register a root device
2186 * @name: root device name
2187 * @owner: owner module of the root device, usually THIS_MODULE
2188 *
2189 * This function allocates a root device and registers it
2190 * using device_register(). In order to free the returned
2191 * device, use root_device_unregister().
2192 *
2193 * Root devices are dummy devices which allow other devices
2194 * to be grouped under /sys/devices. Use this function to
2195 * allocate a root device and then use it as the parent of
2196 * any device which should appear under /sys/devices/{name}
2197 *
2198 * The /sys/devices/{name} directory will also contain a
2199 * 'module' symlink which points to the @owner directory
2200 * in sysfs.
2201 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002202 * Returns &struct device pointer on success, or ERR_PTR() on error.
2203 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002204 * Note: You probably want to use root_device_register().
2205 */
2206struct device *__root_device_register(const char *name, struct module *owner)
2207{
2208 struct root_device *root;
2209 int err = -ENOMEM;
2210
2211 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2212 if (!root)
2213 return ERR_PTR(err);
2214
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002215 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002216 if (err) {
2217 kfree(root);
2218 return ERR_PTR(err);
2219 }
2220
2221 root->dev.release = root_device_release;
2222
2223 err = device_register(&root->dev);
2224 if (err) {
2225 put_device(&root->dev);
2226 return ERR_PTR(err);
2227 }
2228
Christoph Egger1d9e8822010-05-17 16:57:58 +02002229#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002230 if (owner) {
2231 struct module_kobject *mk = &owner->mkobj;
2232
2233 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2234 if (err) {
2235 device_unregister(&root->dev);
2236 return ERR_PTR(err);
2237 }
2238 root->owner = owner;
2239 }
2240#endif
2241
2242 return &root->dev;
2243}
2244EXPORT_SYMBOL_GPL(__root_device_register);
2245
2246/**
2247 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002248 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002249 *
2250 * This function unregisters and cleans up a device that was created by
2251 * root_device_register().
2252 */
2253void root_device_unregister(struct device *dev)
2254{
2255 struct root_device *root = to_root_device(dev);
2256
2257 if (root->owner)
2258 sysfs_remove_link(&root->dev.kobj, "module");
2259
2260 device_unregister(dev);
2261}
2262EXPORT_SYMBOL_GPL(root_device_unregister);
2263
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002264
2265static void device_create_release(struct device *dev)
2266{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002267 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002268 kfree(dev);
2269}
2270
Guenter Roeck39ef3112013-07-14 16:05:57 -07002271static struct device *
2272device_create_groups_vargs(struct class *class, struct device *parent,
2273 dev_t devt, void *drvdata,
2274 const struct attribute_group **groups,
2275 const char *fmt, va_list args)
2276{
2277 struct device *dev = NULL;
2278 int retval = -ENODEV;
2279
2280 if (class == NULL || IS_ERR(class))
2281 goto error;
2282
2283 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2284 if (!dev) {
2285 retval = -ENOMEM;
2286 goto error;
2287 }
2288
David Herrmannbbc780f2013-11-21 20:15:48 +01002289 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002290 dev->devt = devt;
2291 dev->class = class;
2292 dev->parent = parent;
2293 dev->groups = groups;
2294 dev->release = device_create_release;
2295 dev_set_drvdata(dev, drvdata);
2296
2297 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2298 if (retval)
2299 goto error;
2300
David Herrmannbbc780f2013-11-21 20:15:48 +01002301 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002302 if (retval)
2303 goto error;
2304
2305 return dev;
2306
2307error:
2308 put_device(dev);
2309 return ERR_PTR(retval);
2310}
2311
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002312/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002313 * device_create_vargs - creates a device and registers it with sysfs
2314 * @class: pointer to the struct class that this device should be registered to
2315 * @parent: pointer to the parent struct device of this new device, if any
2316 * @devt: the dev_t for the char device to be added
2317 * @drvdata: the data to be added to the device for callbacks
2318 * @fmt: string for the device's name
2319 * @args: va_list for the device's name
2320 *
2321 * This function can be used by char device classes. A struct device
2322 * will be created in sysfs, registered to the specified class.
2323 *
2324 * A "dev" file will be created, showing the dev_t for the device, if
2325 * the dev_t is not 0,0.
2326 * If a pointer to a parent struct device is passed in, the newly created
2327 * struct device will be a child of that device in sysfs.
2328 * The pointer to the struct device will be returned from the call.
2329 * Any further sysfs files that might be required can be created using this
2330 * pointer.
2331 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002332 * Returns &struct device pointer on success, or ERR_PTR() on error.
2333 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002334 * Note: the struct class passed to this function must have previously
2335 * been created with a call to class_create().
2336 */
2337struct device *device_create_vargs(struct class *class, struct device *parent,
2338 dev_t devt, void *drvdata, const char *fmt,
2339 va_list args)
2340{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002341 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2342 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002343}
2344EXPORT_SYMBOL_GPL(device_create_vargs);
2345
2346/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002347 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002348 * @class: pointer to the struct class that this device should be registered to
2349 * @parent: pointer to the parent struct device of this new device, if any
2350 * @devt: the dev_t for the char device to be added
2351 * @drvdata: the data to be added to the device for callbacks
2352 * @fmt: string for the device's name
2353 *
2354 * This function can be used by char device classes. A struct device
2355 * will be created in sysfs, registered to the specified class.
2356 *
2357 * A "dev" file will be created, showing the dev_t for the device, if
2358 * the dev_t is not 0,0.
2359 * If a pointer to a parent struct device is passed in, the newly created
2360 * struct device will be a child of that device in sysfs.
2361 * The pointer to the struct device will be returned from the call.
2362 * Any further sysfs files that might be required can be created using this
2363 * pointer.
2364 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002365 * Returns &struct device pointer on success, or ERR_PTR() on error.
2366 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002367 * Note: the struct class passed to this function must have previously
2368 * been created with a call to class_create().
2369 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002370struct device *device_create(struct class *class, struct device *parent,
2371 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002372{
2373 va_list vargs;
2374 struct device *dev;
2375
2376 va_start(vargs, fmt);
2377 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2378 va_end(vargs);
2379 return dev;
2380}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002381EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002382
Guenter Roeck39ef3112013-07-14 16:05:57 -07002383/**
2384 * device_create_with_groups - creates a device and registers it with sysfs
2385 * @class: pointer to the struct class that this device should be registered to
2386 * @parent: pointer to the parent struct device of this new device, if any
2387 * @devt: the dev_t for the char device to be added
2388 * @drvdata: the data to be added to the device for callbacks
2389 * @groups: NULL-terminated list of attribute groups to be created
2390 * @fmt: string for the device's name
2391 *
2392 * This function can be used by char device classes. A struct device
2393 * will be created in sysfs, registered to the specified class.
2394 * Additional attributes specified in the groups parameter will also
2395 * be created automatically.
2396 *
2397 * A "dev" file will be created, showing the dev_t for the device, if
2398 * the dev_t is not 0,0.
2399 * If a pointer to a parent struct device is passed in, the newly created
2400 * struct device will be a child of that device in sysfs.
2401 * The pointer to the struct device will be returned from the call.
2402 * Any further sysfs files that might be required can be created using this
2403 * pointer.
2404 *
2405 * Returns &struct device pointer on success, or ERR_PTR() on error.
2406 *
2407 * Note: the struct class passed to this function must have previously
2408 * been created with a call to class_create().
2409 */
2410struct device *device_create_with_groups(struct class *class,
2411 struct device *parent, dev_t devt,
2412 void *drvdata,
2413 const struct attribute_group **groups,
2414 const char *fmt, ...)
2415{
2416 va_list vargs;
2417 struct device *dev;
2418
2419 va_start(vargs, fmt);
2420 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2421 fmt, vargs);
2422 va_end(vargs);
2423 return dev;
2424}
2425EXPORT_SYMBOL_GPL(device_create_with_groups);
2426
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002427static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002428{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002429 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002430
Dave Youngcd354492008-01-28 16:56:11 +08002431 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002432}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002433
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002434/**
2435 * device_destroy - removes a device that was created with device_create()
2436 * @class: pointer to the struct class that this device was registered with
2437 * @devt: the dev_t of the device that was previously registered
2438 *
2439 * This call unregisters and cleans up a device that was created with a
2440 * call to device_create().
2441 */
2442void device_destroy(struct class *class, dev_t devt)
2443{
2444 struct device *dev;
2445
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002446 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002447 if (dev) {
2448 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002449 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002450 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002451}
2452EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002453
2454/**
2455 * device_rename - renames a device
2456 * @dev: the pointer to the struct device to be renamed
2457 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002458 *
2459 * It is the responsibility of the caller to provide mutual
2460 * exclusion between two different calls of device_rename
2461 * on the same device to ensure that new_name is valid and
2462 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002463 *
Timur Tabia5462512010-12-13 14:08:52 -06002464 * Note: Don't call this function. Currently, the networking layer calls this
2465 * function, but that will change. The following text from Kay Sievers offers
2466 * some insight:
2467 *
2468 * Renaming devices is racy at many levels, symlinks and other stuff are not
2469 * replaced atomically, and you get a "move" uevent, but it's not easy to
2470 * connect the event to the old and new device. Device nodes are not renamed at
2471 * all, there isn't even support for that in the kernel now.
2472 *
2473 * In the meantime, during renaming, your target name might be taken by another
2474 * driver, creating conflicts. Or the old name is taken directly after you
2475 * renamed it -- then you get events for the same DEVPATH, before you even see
2476 * the "move" event. It's just a mess, and nothing new should ever rely on
2477 * kernel device renaming. Besides that, it's not even implemented now for
2478 * other things than (driver-core wise very simple) network devices.
2479 *
2480 * We are currently about to change network renaming in udev to completely
2481 * disallow renaming of devices in the same namespace as the kernel uses,
2482 * because we can't solve the problems properly, that arise with swapping names
2483 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2484 * be allowed to some other name than eth[0-9]*, for the aforementioned
2485 * reasons.
2486 *
2487 * Make up a "real" name in the driver before you register anything, or add
2488 * some other attributes for userspace to find the device, or use udev to add
2489 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2490 * don't even want to get into that and try to implement the missing pieces in
2491 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002492 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002493int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002494{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002495 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002496 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002497 int error;
2498
2499 dev = get_device(dev);
2500 if (!dev)
2501 return -EINVAL;
2502
ethan.zhao69df7532013-10-13 22:12:35 +08002503 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002504
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002505 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002506 if (!old_device_name) {
2507 error = -ENOMEM;
2508 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002509 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002510
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002511 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002512 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2513 kobj, old_device_name,
2514 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002515 if (error)
2516 goto out;
2517 }
Kay Sievers39aba962010-09-04 22:33:14 -07002518
Tejun Heo4b30ee52013-09-11 22:29:06 -04002519 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002520 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002521 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002522
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002523out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002524 put_device(dev);
2525
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002526 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002527
2528 return error;
2529}
Johannes Berga2807db2007-02-28 12:38:31 +01002530EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002531
2532static int device_move_class_links(struct device *dev,
2533 struct device *old_parent,
2534 struct device *new_parent)
2535{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002536 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002537
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002538 if (old_parent)
2539 sysfs_remove_link(&dev->kobj, "device");
2540 if (new_parent)
2541 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2542 "device");
2543 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002544}
2545
2546/**
2547 * device_move - moves a device to a new parent
2548 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002549 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002550 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002551 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002552int device_move(struct device *dev, struct device *new_parent,
2553 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002554{
2555 int error;
2556 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002557 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002558
2559 dev = get_device(dev);
2560 if (!dev)
2561 return -EINVAL;
2562
Cornelia Huckffa6a702009-03-04 12:44:00 +01002563 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002564 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002565 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01002566
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002567 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2568 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002569 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002570 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002571 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002572 put_device(new_parent);
2573 goto out;
2574 }
2575 old_parent = dev->parent;
2576 dev->parent = new_parent;
2577 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002578 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002579 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002580 klist_add_tail(&dev->p->knode_parent,
2581 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002582 set_dev_node(dev, dev_to_node(new_parent));
2583 }
2584
Rabin Vincentbdd40342012-04-23 09:16:36 +02002585 if (dev->class) {
2586 error = device_move_class_links(dev, old_parent, new_parent);
2587 if (error) {
2588 /* We ignore errors on cleanup since we're hosed anyway... */
2589 device_move_class_links(dev, new_parent, old_parent);
2590 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2591 if (new_parent)
2592 klist_remove(&dev->p->knode_parent);
2593 dev->parent = old_parent;
2594 if (old_parent) {
2595 klist_add_tail(&dev->p->knode_parent,
2596 &old_parent->p->klist_children);
2597 set_dev_node(dev, dev_to_node(old_parent));
2598 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002599 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002600 cleanup_glue_dir(dev, new_parent_kobj);
2601 put_device(new_parent);
2602 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002603 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002604 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002605 switch (dpm_order) {
2606 case DPM_ORDER_NONE:
2607 break;
2608 case DPM_ORDER_DEV_AFTER_PARENT:
2609 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002610 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002611 break;
2612 case DPM_ORDER_PARENT_BEFORE_DEV:
2613 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002614 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002615 break;
2616 case DPM_ORDER_DEV_LAST:
2617 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002618 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002619 break;
2620 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002621
Cornelia Huck8a824722006-11-20 17:07:51 +01002622 put_device(old_parent);
2623out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002624 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002625 put_device(dev);
2626 return error;
2627}
Cornelia Huck8a824722006-11-20 17:07:51 +01002628EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002629
2630/**
2631 * device_shutdown - call ->shutdown() on each device to shutdown.
2632 */
2633void device_shutdown(void)
2634{
Benson Leungf123db82013-09-24 20:05:11 -07002635 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002636
Hugh Daschbach62458382010-03-22 10:36:37 -07002637 spin_lock(&devices_kset->list_lock);
2638 /*
2639 * Walk the devices list backward, shutting down each in turn.
2640 * Beware that device unplug events may also start pulling
2641 * devices offline, even as the system is shutting down.
2642 */
2643 while (!list_empty(&devices_kset->list)) {
2644 dev = list_entry(devices_kset->list.prev, struct device,
2645 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002646
2647 /*
2648 * hold reference count of device's parent to
2649 * prevent it from being freed because parent's
2650 * lock is to be held
2651 */
Benson Leungf123db82013-09-24 20:05:11 -07002652 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002653 get_device(dev);
2654 /*
2655 * Make sure the device is off the kset list, in the
2656 * event that dev->*->shutdown() doesn't remove it.
2657 */
2658 list_del_init(&dev->kobj.entry);
2659 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002660
Ming Leid1c6c032012-06-22 18:01:40 +08002661 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002662 if (parent)
2663 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002664 device_lock(dev);
2665
Alan Sternfe6b91f2011-12-06 23:24:52 +01002666 /* Don't allow any more runtime suspends */
2667 pm_runtime_get_noresume(dev);
2668 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002669
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002670 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002671 if (initcall_debug)
2672 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002673 dev->bus->shutdown(dev);
2674 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002675 if (initcall_debug)
2676 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002677 dev->driver->shutdown(dev);
2678 }
Ming Leid1c6c032012-06-22 18:01:40 +08002679
2680 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002681 if (parent)
2682 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002683
Hugh Daschbach62458382010-03-22 10:36:37 -07002684 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002685 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002686
2687 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002688 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002689 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002690}
Joe Perches99bcf212010-06-27 01:02:34 +00002691
2692/*
2693 * Device logging functions
2694 */
2695
2696#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002697static int
2698create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002699{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002700 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002701 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002702
Kay Sieversc4e00da2012-05-03 02:29:59 +02002703 if (dev->class)
2704 subsys = dev->class->name;
2705 else if (dev->bus)
2706 subsys = dev->bus->name;
2707 else
Joe Perches798efc62012-09-12 20:11:29 -07002708 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002709
Joe Perches798efc62012-09-12 20:11:29 -07002710 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002711 if (pos >= hdrlen)
2712 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002713
2714 /*
2715 * Add device identifier DEVICE=:
2716 * b12:8 block dev_t
2717 * c127:3 char dev_t
2718 * n8 netdev ifindex
2719 * +sound:card0 subsystem:devname
2720 */
2721 if (MAJOR(dev->devt)) {
2722 char c;
2723
2724 if (strcmp(subsys, "block") == 0)
2725 c = 'b';
2726 else
2727 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002728 pos++;
2729 pos += snprintf(hdr + pos, hdrlen - pos,
2730 "DEVICE=%c%u:%u",
2731 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002732 } else if (strcmp(subsys, "net") == 0) {
2733 struct net_device *net = to_net_dev(dev);
2734
Joe Perches798efc62012-09-12 20:11:29 -07002735 pos++;
2736 pos += snprintf(hdr + pos, hdrlen - pos,
2737 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002738 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002739 pos++;
2740 pos += snprintf(hdr + pos, hdrlen - pos,
2741 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002742 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002743
Ben Hutchings655e5b72014-08-26 00:34:44 -07002744 if (pos >= hdrlen)
2745 goto overflow;
2746
Joe Perches798efc62012-09-12 20:11:29 -07002747 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002748
2749overflow:
2750 dev_WARN(dev, "device/subsystem name too long");
2751 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002752}
Joe Perches798efc62012-09-12 20:11:29 -07002753
Joe Perches05e4e5b2012-09-12 20:13:37 -07002754int dev_vprintk_emit(int level, const struct device *dev,
2755 const char *fmt, va_list args)
2756{
2757 char hdr[128];
2758 size_t hdrlen;
2759
2760 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2761
2762 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2763}
2764EXPORT_SYMBOL(dev_vprintk_emit);
2765
2766int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2767{
2768 va_list args;
2769 int r;
2770
2771 va_start(args, fmt);
2772
2773 r = dev_vprintk_emit(level, dev, fmt, args);
2774
2775 va_end(args);
2776
2777 return r;
2778}
2779EXPORT_SYMBOL(dev_printk_emit);
2780
Joe Perchesd1f10522014-12-25 15:07:04 -08002781static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07002782 struct va_format *vaf)
2783{
Joe Perchesd1f10522014-12-25 15:07:04 -08002784 if (dev)
2785 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2786 dev_driver_string(dev), dev_name(dev), vaf);
2787 else
2788 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002789}
Joe Perches99bcf212010-06-27 01:02:34 +00002790
Joe Perchesd1f10522014-12-25 15:07:04 -08002791void dev_printk(const char *level, const struct device *dev,
2792 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00002793{
2794 struct va_format vaf;
2795 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00002796
2797 va_start(args, fmt);
2798
2799 vaf.fmt = fmt;
2800 vaf.va = &args;
2801
Joe Perchesd1f10522014-12-25 15:07:04 -08002802 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002803
Joe Perches99bcf212010-06-27 01:02:34 +00002804 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00002805}
2806EXPORT_SYMBOL(dev_printk);
2807
2808#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08002809void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00002810{ \
2811 struct va_format vaf; \
2812 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00002813 \
2814 va_start(args, fmt); \
2815 \
2816 vaf.fmt = fmt; \
2817 vaf.va = &args; \
2818 \
Joe Perchesd1f10522014-12-25 15:07:04 -08002819 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002820 \
Joe Perches99bcf212010-06-27 01:02:34 +00002821 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00002822} \
2823EXPORT_SYMBOL(func);
2824
2825define_dev_printk_level(dev_emerg, KERN_EMERG);
2826define_dev_printk_level(dev_alert, KERN_ALERT);
2827define_dev_printk_level(dev_crit, KERN_CRIT);
2828define_dev_printk_level(dev_err, KERN_ERR);
2829define_dev_printk_level(dev_warn, KERN_WARNING);
2830define_dev_printk_level(dev_notice, KERN_NOTICE);
2831define_dev_printk_level(_dev_info, KERN_INFO);
2832
2833#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002834
2835static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2836{
2837 return fwnode && !IS_ERR(fwnode->secondary);
2838}
2839
2840/**
2841 * set_primary_fwnode - Change the primary firmware node of a given device.
2842 * @dev: Device to handle.
2843 * @fwnode: New primary firmware node of the device.
2844 *
2845 * Set the device's firmware node pointer to @fwnode, but if a secondary
2846 * firmware node of the device is present, preserve it.
2847 */
2848void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2849{
2850 if (fwnode) {
2851 struct fwnode_handle *fn = dev->fwnode;
2852
2853 if (fwnode_is_primary(fn))
2854 fn = fn->secondary;
2855
Mika Westerberg55f89a82015-11-30 17:11:39 +02002856 if (fn) {
2857 WARN_ON(fwnode->secondary);
2858 fwnode->secondary = fn;
2859 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002860 dev->fwnode = fwnode;
2861 } else {
2862 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
2863 dev->fwnode->secondary : NULL;
2864 }
2865}
2866EXPORT_SYMBOL_GPL(set_primary_fwnode);
2867
2868/**
2869 * set_secondary_fwnode - Change the secondary firmware node of a given device.
2870 * @dev: Device to handle.
2871 * @fwnode: New secondary firmware node of the device.
2872 *
2873 * If a primary firmware node of the device is present, set its secondary
2874 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
2875 * @fwnode.
2876 */
2877void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2878{
2879 if (fwnode)
2880 fwnode->secondary = ERR_PTR(-ENODEV);
2881
2882 if (fwnode_is_primary(dev->fwnode))
2883 dev->fwnode->secondary = fwnode;
2884 else
2885 dev->fwnode = fwnode;
2886}