]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/base/core.c
usb: Don't disable Latency tolerance Messaging (LTM) before port reset
[mirror_ubuntu-bionic-kernel.git] / drivers / base / core.c
CommitLineData
1da177e4
LT
1/*
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
64bb5d2c
GKH
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
1da177e4
LT
8 *
9 * This file is released under the GPLv2
10 *
11 */
12
1da177e4
LT
13#include <linux/device.h>
14#include <linux/err.h>
97badf87 15#include <linux/fwnode.h>
1da177e4
LT
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/string.h>
23681e47 20#include <linux/kdev_t.h>
116af378 21#include <linux/notifier.h>
07d57a32
GL
22#include <linux/of.h>
23#include <linux/of_device.h>
da231fd5 24#include <linux/genhd.h>
815d2d50 25#include <linux/kallsyms.h>
f75b1c60 26#include <linux/mutex.h>
af8db150 27#include <linux/pm_runtime.h>
c4e00daa 28#include <linux/netdevice.h>
174cd4b1 29#include <linux/sched/signal.h>
63967685 30#include <linux/sysfs.h>
1da177e4
LT
31
32#include "base.h"
33#include "power/power.h"
34
e52eec13
AK
35#ifdef CONFIG_SYSFS_DEPRECATED
36#ifdef CONFIG_SYSFS_DEPRECATED_V2
37long sysfs_deprecated = 1;
38#else
39long sysfs_deprecated = 0;
40#endif
3454bf96 41static int __init sysfs_deprecated_setup(char *arg)
e52eec13 42{
34da5e67 43 return kstrtol(arg, 10, &sysfs_deprecated);
e52eec13
AK
44}
45early_param("sysfs.deprecated", sysfs_deprecated_setup);
46#endif
47
9ed98953
RW
48/* 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 *
21d5c57b
RW
156 * 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 *
9ed98953
RW
164 * 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
64df1148 176 * not be registered, however.
9ed98953
RW
177 */
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
21d5c57b 205 link = kzalloc(sizeof(*link), GFP_KERNEL);
9ed98953
RW
206 if (!link)
207 goto out;
208
baa8809f
RW
209 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;
21d5c57b 218 }
baa8809f 219 pm_runtime_new_link(consumer);
21d5c57b 220 }
9ed98953
RW
221 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
64df1148 229 /* Determine the initial link state. */
9ed98953
RW
230 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:
21d5c57b
RW
237 /*
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
9ed98953
RW
245 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
baa8809f
RW
303 if (link->flags & DL_FLAG_PM_RUNTIME)
304 pm_runtime_drop_link(link->consumer);
305
9ed98953
RW
306 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
03d0bf0f
LW
316 if (link->flags & DL_FLAG_PM_RUNTIME)
317 pm_runtime_drop_link(link->consumer);
318
9ed98953
RW
319 list_del(&link->s_node);
320 list_del(&link->c_node);
321 device_link_free(link);
322}
323#endif /* !CONFIG_SRCU */
324
325/**
326 * device_link_del - Delete a link between two devices.
327 * @link: Device link to delete.
328 *
329 * The caller must ensure proper synchronization of this function with runtime
330 * PM.
331 */
332void device_link_del(struct device_link *link)
333{
334 device_links_write_lock();
335 device_pm_lock();
336 __device_link_del(link);
337 device_pm_unlock();
338 device_links_write_unlock();
339}
340EXPORT_SYMBOL_GPL(device_link_del);
341
342static void device_links_missing_supplier(struct device *dev)
343{
344 struct device_link *link;
345
346 list_for_each_entry(link, &dev->links.suppliers, c_node)
347 if (link->status == DL_STATE_CONSUMER_PROBE)
348 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
349}
350
351/**
352 * device_links_check_suppliers - Check presence of supplier drivers.
353 * @dev: Consumer device.
354 *
355 * Check links from this device to any suppliers. Walk the list of the device's
356 * links to suppliers and see if all of them are available. If not, simply
357 * return -EPROBE_DEFER.
358 *
359 * We need to guarantee that the supplier will not go away after the check has
360 * been positive here. It only can go away in __device_release_driver() and
361 * that function checks the device's links to consumers. This means we need to
362 * mark the link as "consumer probe in progress" to make the supplier removal
363 * wait for us to complete (or bad things may happen).
364 *
365 * Links with the DL_FLAG_STATELESS flag set are ignored.
366 */
367int device_links_check_suppliers(struct device *dev)
368{
369 struct device_link *link;
370 int ret = 0;
371
372 device_links_write_lock();
373
374 list_for_each_entry(link, &dev->links.suppliers, c_node) {
375 if (link->flags & DL_FLAG_STATELESS)
376 continue;
377
378 if (link->status != DL_STATE_AVAILABLE) {
379 device_links_missing_supplier(dev);
380 ret = -EPROBE_DEFER;
381 break;
382 }
383 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
384 }
385 dev->links.status = DL_DEV_PROBING;
386
387 device_links_write_unlock();
388 return ret;
389}
390
391/**
392 * device_links_driver_bound - Update device links after probing its driver.
393 * @dev: Device to update the links for.
394 *
395 * The probe has been successful, so update links from this device to any
396 * consumers by changing their status to "available".
397 *
398 * Also change the status of @dev's links to suppliers to "active".
399 *
400 * Links with the DL_FLAG_STATELESS flag set are ignored.
401 */
402void device_links_driver_bound(struct device *dev)
403{
404 struct device_link *link;
405
406 device_links_write_lock();
407
408 list_for_each_entry(link, &dev->links.consumers, s_node) {
409 if (link->flags & DL_FLAG_STATELESS)
410 continue;
411
412 WARN_ON(link->status != DL_STATE_DORMANT);
413 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
414 }
415
416 list_for_each_entry(link, &dev->links.suppliers, c_node) {
417 if (link->flags & DL_FLAG_STATELESS)
418 continue;
419
420 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
421 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
422 }
423
424 dev->links.status = DL_DEV_DRIVER_BOUND;
425
426 device_links_write_unlock();
427}
428
429/**
430 * __device_links_no_driver - Update links of a device without a driver.
431 * @dev: Device without a drvier.
432 *
433 * Delete all non-persistent links from this device to any suppliers.
434 *
435 * Persistent links stay around, but their status is changed to "available",
436 * unless they already are in the "supplier unbind in progress" state in which
437 * case they need not be updated.
438 *
439 * Links with the DL_FLAG_STATELESS flag set are ignored.
440 */
441static void __device_links_no_driver(struct device *dev)
442{
443 struct device_link *link, *ln;
444
445 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
446 if (link->flags & DL_FLAG_STATELESS)
447 continue;
448
449 if (link->flags & DL_FLAG_AUTOREMOVE)
450 __device_link_del(link);
451 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
452 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
453 }
454
455 dev->links.status = DL_DEV_NO_DRIVER;
456}
457
458void device_links_no_driver(struct device *dev)
459{
460 device_links_write_lock();
461 __device_links_no_driver(dev);
462 device_links_write_unlock();
463}
464
465/**
466 * device_links_driver_cleanup - Update links after driver removal.
467 * @dev: Device whose driver has just gone away.
468 *
469 * Update links to consumers for @dev by changing their status to "dormant" and
470 * invoke %__device_links_no_driver() to update links to suppliers for it as
471 * appropriate.
472 *
473 * Links with the DL_FLAG_STATELESS flag set are ignored.
474 */
475void device_links_driver_cleanup(struct device *dev)
476{
477 struct device_link *link;
478
479 device_links_write_lock();
480
481 list_for_each_entry(link, &dev->links.consumers, s_node) {
482 if (link->flags & DL_FLAG_STATELESS)
483 continue;
484
485 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
486 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
487 WRITE_ONCE(link->status, DL_STATE_DORMANT);
488 }
489
490 __device_links_no_driver(dev);
491
492 device_links_write_unlock();
493}
494
495/**
496 * device_links_busy - Check if there are any busy links to consumers.
497 * @dev: Device to check.
498 *
499 * Check each consumer of the device and return 'true' if its link's status
500 * is one of "consumer probe" or "active" (meaning that the given consumer is
501 * probing right now or its driver is present). Otherwise, change the link
502 * state to "supplier unbind" to prevent the consumer from being probed
503 * successfully going forward.
504 *
505 * Return 'false' if there are no probing or active consumers.
506 *
507 * Links with the DL_FLAG_STATELESS flag set are ignored.
508 */
509bool device_links_busy(struct device *dev)
510{
511 struct device_link *link;
512 bool ret = false;
513
514 device_links_write_lock();
515
516 list_for_each_entry(link, &dev->links.consumers, s_node) {
517 if (link->flags & DL_FLAG_STATELESS)
518 continue;
519
520 if (link->status == DL_STATE_CONSUMER_PROBE
521 || link->status == DL_STATE_ACTIVE) {
522 ret = true;
523 break;
524 }
525 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
526 }
527
528 dev->links.status = DL_DEV_UNBINDING;
529
530 device_links_write_unlock();
531 return ret;
532}
533
534/**
535 * device_links_unbind_consumers - Force unbind consumers of the given device.
536 * @dev: Device to unbind the consumers of.
537 *
538 * Walk the list of links to consumers for @dev and if any of them is in the
539 * "consumer probe" state, wait for all device probes in progress to complete
540 * and start over.
541 *
542 * If that's not the case, change the status of the link to "supplier unbind"
543 * and check if the link was in the "active" state. If so, force the consumer
544 * driver to unbind and start over (the consumer will not re-probe as we have
545 * changed the state of the link already).
546 *
547 * Links with the DL_FLAG_STATELESS flag set are ignored.
548 */
549void device_links_unbind_consumers(struct device *dev)
550{
551 struct device_link *link;
552
553 start:
554 device_links_write_lock();
555
556 list_for_each_entry(link, &dev->links.consumers, s_node) {
557 enum device_link_state status;
558
559 if (link->flags & DL_FLAG_STATELESS)
560 continue;
561
562 status = link->status;
563 if (status == DL_STATE_CONSUMER_PROBE) {
564 device_links_write_unlock();
565
566 wait_for_device_probe();
567 goto start;
568 }
569 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
570 if (status == DL_STATE_ACTIVE) {
571 struct device *consumer = link->consumer;
572
573 get_device(consumer);
574
575 device_links_write_unlock();
576
577 device_release_driver_internal(consumer, NULL,
578 consumer->parent);
579 put_device(consumer);
580 goto start;
581 }
582 }
583
584 device_links_write_unlock();
585}
586
587/**
588 * device_links_purge - Delete existing links to other devices.
589 * @dev: Target device.
590 */
591static void device_links_purge(struct device *dev)
592{
593 struct device_link *link, *ln;
594
595 /*
596 * Delete all of the remaining links from this device to any other
597 * devices (either consumers or suppliers).
598 */
599 device_links_write_lock();
600
601 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
602 WARN_ON(link->status == DL_STATE_ACTIVE);
603 __device_link_del(link);
604 }
605
606 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
607 WARN_ON(link->status != DL_STATE_DORMANT &&
608 link->status != DL_STATE_NONE);
609 __device_link_del(link);
610 }
611
612 device_links_write_unlock();
613}
614
615/* Device links support end. */
616
4a3ad20c
GKH
617int (*platform_notify)(struct device *dev) = NULL;
618int (*platform_notify_remove)(struct device *dev) = NULL;
e105b8bf
DW
619static struct kobject *dev_kobj;
620struct kobject *sysfs_dev_char_kobj;
621struct kobject *sysfs_dev_block_kobj;
1da177e4 622
5e33bc41
RW
623static DEFINE_MUTEX(device_hotplug_lock);
624
625void lock_device_hotplug(void)
626{
627 mutex_lock(&device_hotplug_lock);
628}
629
630void unlock_device_hotplug(void)
631{
632 mutex_unlock(&device_hotplug_lock);
633}
634
635int lock_device_hotplug_sysfs(void)
636{
637 if (mutex_trylock(&device_hotplug_lock))
638 return 0;
639
640 /* Avoid busy looping (5 ms of sleep should do). */
641 msleep(5);
642 return restart_syscall();
643}
644
4e886c29
GKH
645#ifdef CONFIG_BLOCK
646static inline int device_is_not_partition(struct device *dev)
647{
648 return !(dev->type == &part_type);
649}
650#else
651static inline int device_is_not_partition(struct device *dev)
652{
653 return 1;
654}
655#endif
1da177e4 656
3e95637a
AS
657/**
658 * dev_driver_string - Return a device's driver name, if at all possible
659 * @dev: struct device to get the name of
660 *
661 * Will return the device's driver's name if it is bound to a device. If
9169c012 662 * the device is not bound to a driver, it will return the name of the bus
3e95637a
AS
663 * it is attached to. If it is not attached to a bus either, an empty
664 * string will be returned.
665 */
bf9ca69f 666const char *dev_driver_string(const struct device *dev)
3e95637a 667{
3589972e
AS
668 struct device_driver *drv;
669
670 /* dev->driver can change to NULL underneath us because of unbinding,
671 * so be careful about accessing it. dev->bus and dev->class should
672 * never change once they are set, so they don't need special care.
673 */
6aa7de05 674 drv = READ_ONCE(dev->driver);
3589972e 675 return drv ? drv->name :
a456b702
JD
676 (dev->bus ? dev->bus->name :
677 (dev->class ? dev->class->name : ""));
3e95637a 678}
310a922d 679EXPORT_SYMBOL(dev_driver_string);
3e95637a 680
1da177e4
LT
681#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
682
4a3ad20c
GKH
683static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
684 char *buf)
1da177e4 685{
4a3ad20c 686 struct device_attribute *dev_attr = to_dev_attr(attr);
b0d1f807 687 struct device *dev = kobj_to_dev(kobj);
4a0c20bf 688 ssize_t ret = -EIO;
1da177e4
LT
689
690 if (dev_attr->show)
54b6f35c 691 ret = dev_attr->show(dev, dev_attr, buf);
815d2d50 692 if (ret >= (ssize_t)PAGE_SIZE) {
53a9c87e
GKH
693 print_symbol("dev_attr_show: %s returned bad count\n",
694 (unsigned long)dev_attr->show);
815d2d50 695 }
1da177e4
LT
696 return ret;
697}
698
4a3ad20c
GKH
699static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
700 const char *buf, size_t count)
1da177e4 701{
4a3ad20c 702 struct device_attribute *dev_attr = to_dev_attr(attr);
b0d1f807 703 struct device *dev = kobj_to_dev(kobj);
4a0c20bf 704 ssize_t ret = -EIO;
1da177e4
LT
705
706 if (dev_attr->store)
54b6f35c 707 ret = dev_attr->store(dev, dev_attr, buf, count);
1da177e4
LT
708 return ret;
709}
710
52cf25d0 711static const struct sysfs_ops dev_sysfs_ops = {
1da177e4
LT
712 .show = dev_attr_show,
713 .store = dev_attr_store,
714};
715
ca22e56d
KS
716#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
717
718ssize_t device_store_ulong(struct device *dev,
719 struct device_attribute *attr,
720 const char *buf, size_t size)
721{
722 struct dev_ext_attribute *ea = to_ext_attr(attr);
723 char *end;
724 unsigned long new = simple_strtoul(buf, &end, 0);
725 if (end == buf)
726 return -EINVAL;
727 *(unsigned long *)(ea->var) = new;
728 /* Always return full write size even if we didn't consume all */
729 return size;
730}
731EXPORT_SYMBOL_GPL(device_store_ulong);
732
733ssize_t device_show_ulong(struct device *dev,
734 struct device_attribute *attr,
735 char *buf)
736{
737 struct dev_ext_attribute *ea = to_ext_attr(attr);
738 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
739}
740EXPORT_SYMBOL_GPL(device_show_ulong);
741
742ssize_t device_store_int(struct device *dev,
743 struct device_attribute *attr,
744 const char *buf, size_t size)
745{
746 struct dev_ext_attribute *ea = to_ext_attr(attr);
747 char *end;
748 long new = simple_strtol(buf, &end, 0);
749 if (end == buf || new > INT_MAX || new < INT_MIN)
750 return -EINVAL;
751 *(int *)(ea->var) = new;
752 /* Always return full write size even if we didn't consume all */
753 return size;
754}
755EXPORT_SYMBOL_GPL(device_store_int);
756
757ssize_t device_show_int(struct device *dev,
758 struct device_attribute *attr,
759 char *buf)
760{
761 struct dev_ext_attribute *ea = to_ext_attr(attr);
762
763 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
764}
765EXPORT_SYMBOL_GPL(device_show_int);
1da177e4 766
91872392
BP
767ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
768 const char *buf, size_t size)
769{
770 struct dev_ext_attribute *ea = to_ext_attr(attr);
771
772 if (strtobool(buf, ea->var) < 0)
773 return -EINVAL;
774
775 return size;
776}
777EXPORT_SYMBOL_GPL(device_store_bool);
778
779ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
780 char *buf)
781{
782 struct dev_ext_attribute *ea = to_ext_attr(attr);
783
784 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
785}
786EXPORT_SYMBOL_GPL(device_show_bool);
787
1da177e4 788/**
f8878dcb
RD
789 * device_release - free device structure.
790 * @kobj: device's kobject.
1da177e4 791 *
f8878dcb
RD
792 * This is called once the reference count for the object
793 * reaches 0. We forward the call to the device's release
794 * method, which should handle actually freeing the structure.
1da177e4 795 */
4a3ad20c 796static void device_release(struct kobject *kobj)
1da177e4 797{
b0d1f807 798 struct device *dev = kobj_to_dev(kobj);
fb069a5d 799 struct device_private *p = dev->p;
1da177e4 800
a525a3dd
ML
801 /*
802 * Some platform devices are driven without driver attached
803 * and managed resources may have been acquired. Make sure
804 * all resources are released.
805 *
806 * Drivers still can add resources into device after device
807 * is deleted but alive, so release devres here to avoid
808 * possible memory leak.
809 */
810 devres_release_all(dev);
811
1da177e4
LT
812 if (dev->release)
813 dev->release(dev);
f9f852df
KS
814 else if (dev->type && dev->type->release)
815 dev->type->release(dev);
2620efef
GKH
816 else if (dev->class && dev->class->dev_release)
817 dev->class->dev_release(dev);
f810a5cf
AV
818 else
819 WARN(1, KERN_ERR "Device '%s' does not have a release() "
4a3ad20c 820 "function, it is broken and must be fixed.\n",
1e0b2cf9 821 dev_name(dev));
fb069a5d 822 kfree(p);
1da177e4
LT
823}
824
bc451f20
EB
825static const void *device_namespace(struct kobject *kobj)
826{
b0d1f807 827 struct device *dev = kobj_to_dev(kobj);
bc451f20
EB
828 const void *ns = NULL;
829
830 if (dev->class && dev->class->ns_type)
831 ns = dev->class->namespace(dev);
832
833 return ns;
834}
835
8f4afc41 836static struct kobj_type device_ktype = {
1da177e4
LT
837 .release = device_release,
838 .sysfs_ops = &dev_sysfs_ops,
bc451f20 839 .namespace = device_namespace,
1da177e4
LT
840};
841
842
312c004d 843static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1da177e4
LT
844{
845 struct kobj_type *ktype = get_ktype(kobj);
846
8f4afc41 847 if (ktype == &device_ktype) {
b0d1f807 848 struct device *dev = kobj_to_dev(kobj);
1da177e4
LT
849 if (dev->bus)
850 return 1;
23681e47
GKH
851 if (dev->class)
852 return 1;
1da177e4
LT
853 }
854 return 0;
855}
856
312c004d 857static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1da177e4 858{
b0d1f807 859 struct device *dev = kobj_to_dev(kobj);
1da177e4 860
23681e47
GKH
861 if (dev->bus)
862 return dev->bus->name;
863 if (dev->class)
864 return dev->class->name;
865 return NULL;
1da177e4
LT
866}
867
7eff2e7a
KS
868static int dev_uevent(struct kset *kset, struct kobject *kobj,
869 struct kobj_uevent_env *env)
1da177e4 870{
b0d1f807 871 struct device *dev = kobj_to_dev(kobj);
1da177e4
LT
872 int retval = 0;
873
6fcf53ac 874 /* add device node properties if present */
23681e47 875 if (MAJOR(dev->devt)) {
6fcf53ac
KS
876 const char *tmp;
877 const char *name;
2c9ede55 878 umode_t mode = 0;
4e4098a3
GKH
879 kuid_t uid = GLOBAL_ROOT_UID;
880 kgid_t gid = GLOBAL_ROOT_GID;
6fcf53ac 881
7eff2e7a
KS
882 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
883 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
3c2670e6 884 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
6fcf53ac
KS
885 if (name) {
886 add_uevent_var(env, "DEVNAME=%s", name);
e454cea2
KS
887 if (mode)
888 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
4e4098a3
GKH
889 if (!uid_eq(uid, GLOBAL_ROOT_UID))
890 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
891 if (!gid_eq(gid, GLOBAL_ROOT_GID))
892 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
3c2670e6 893 kfree(tmp);
6fcf53ac 894 }
23681e47
GKH
895 }
896
414264f9 897 if (dev->type && dev->type->name)
7eff2e7a 898 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
414264f9 899
239378f1 900 if (dev->driver)
7eff2e7a 901 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
239378f1 902
07d57a32
GL
903 /* Add common DT information about the device */
904 of_device_uevent(dev, env);
905
7eff2e7a 906 /* have the bus specific function add its stuff */
312c004d 907 if (dev->bus && dev->bus->uevent) {
7eff2e7a 908 retval = dev->bus->uevent(dev, env);
f9f852df 909 if (retval)
7dc72b28 910 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1e0b2cf9 911 dev_name(dev), __func__, retval);
1da177e4
LT
912 }
913
7eff2e7a 914 /* have the class specific function add its stuff */
2620efef 915 if (dev->class && dev->class->dev_uevent) {
7eff2e7a 916 retval = dev->class->dev_uevent(dev, env);
f9f852df 917 if (retval)
7dc72b28 918 pr_debug("device: '%s': %s: class uevent() "
1e0b2cf9 919 "returned %d\n", dev_name(dev),
2b3a302a 920 __func__, retval);
f9f852df
KS
921 }
922
eef35c2d 923 /* have the device type specific function add its stuff */
f9f852df 924 if (dev->type && dev->type->uevent) {
7eff2e7a 925 retval = dev->type->uevent(dev, env);
f9f852df 926 if (retval)
7dc72b28 927 pr_debug("device: '%s': %s: dev_type uevent() "
1e0b2cf9 928 "returned %d\n", dev_name(dev),
2b3a302a 929 __func__, retval);
2620efef
GKH
930 }
931
1da177e4
LT
932 return retval;
933}
934
9cd43611 935static const struct kset_uevent_ops device_uevent_ops = {
312c004d
KS
936 .filter = dev_uevent_filter,
937 .name = dev_uevent_name,
938 .uevent = dev_uevent,
1da177e4
LT
939};
940
c5e064a6 941static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
16574dcc
KS
942 char *buf)
943{
944 struct kobject *top_kobj;
945 struct kset *kset;
7eff2e7a 946 struct kobj_uevent_env *env = NULL;
16574dcc
KS
947 int i;
948 size_t count = 0;
949 int retval;
950
951 /* search the kset, the device belongs to */
952 top_kobj = &dev->kobj;
5c5daf65
KS
953 while (!top_kobj->kset && top_kobj->parent)
954 top_kobj = top_kobj->parent;
16574dcc
KS
955 if (!top_kobj->kset)
956 goto out;
5c5daf65 957
16574dcc
KS
958 kset = top_kobj->kset;
959 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
960 goto out;
961
962 /* respect filter */
963 if (kset->uevent_ops && kset->uevent_ops->filter)
964 if (!kset->uevent_ops->filter(kset, &dev->kobj))
965 goto out;
966
7eff2e7a
KS
967 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
968 if (!env)
c7308c81
GKH
969 return -ENOMEM;
970
16574dcc 971 /* let the kset specific function add its keys */
7eff2e7a 972 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
16574dcc
KS
973 if (retval)
974 goto out;
975
976 /* copy keys to file */
7eff2e7a
KS
977 for (i = 0; i < env->envp_idx; i++)
978 count += sprintf(&buf[count], "%s\n", env->envp[i]);
16574dcc 979out:
7eff2e7a 980 kfree(env);
16574dcc
KS
981 return count;
982}
983
c5e064a6 984static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
a7fd6706
KS
985 const char *buf, size_t count)
986{
f36776fa
PR
987 if (kobject_synth_uevent(&dev->kobj, buf, count))
988 dev_err(dev, "uevent: failed to send synthetic uevent\n");
60a96a59 989
a7fd6706
KS
990 return count;
991}
c5e064a6 992static DEVICE_ATTR_RW(uevent);
a7fd6706 993
c5e064a6 994static ssize_t online_show(struct device *dev, struct device_attribute *attr,
4f3549d7
RW
995 char *buf)
996{
997 bool val;
998
5e33bc41 999 device_lock(dev);
4f3549d7 1000 val = !dev->offline;
5e33bc41 1001 device_unlock(dev);
4f3549d7
RW
1002 return sprintf(buf, "%u\n", val);
1003}
1004
c5e064a6 1005static ssize_t online_store(struct device *dev, struct device_attribute *attr,
4f3549d7
RW
1006 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
5e33bc41
RW
1015 ret = lock_device_hotplug_sysfs();
1016 if (ret)
1017 return ret;
1018
4f3549d7
RW
1019 ret = val ? device_online(dev) : device_offline(dev);
1020 unlock_device_hotplug();
1021 return ret < 0 ? ret : count;
1022}
c5e064a6 1023static DEVICE_ATTR_RW(online);
4f3549d7 1024
fa6fdb33 1025int device_add_groups(struct device *dev, const struct attribute_group **groups)
621a1672 1026{
3e9b2bae 1027 return sysfs_create_groups(&dev->kobj, groups);
de0ff00d 1028}
a7670d42 1029EXPORT_SYMBOL_GPL(device_add_groups);
de0ff00d 1030
fa6fdb33
GKH
1031void device_remove_groups(struct device *dev,
1032 const struct attribute_group **groups)
de0ff00d 1033{
3e9b2bae 1034 sysfs_remove_groups(&dev->kobj, groups);
de0ff00d 1035}
a7670d42 1036EXPORT_SYMBOL_GPL(device_remove_groups);
de0ff00d 1037
57b8ff07
DT
1038union device_attr_group_devres {
1039 const struct attribute_group *group;
1040 const struct attribute_group **groups;
1041};
1042
1043static int devm_attr_group_match(struct device *dev, void *res, void *data)
1044{
1045 return ((union device_attr_group_devres *)res)->group == data;
1046}
1047
1048static void devm_attr_group_remove(struct device *dev, void *res)
1049{
1050 union device_attr_group_devres *devres = res;
1051 const struct attribute_group *group = devres->group;
1052
1053 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1054 sysfs_remove_group(&dev->kobj, group);
1055}
1056
1057static void devm_attr_groups_remove(struct device *dev, void *res)
1058{
1059 union device_attr_group_devres *devres = res;
1060 const struct attribute_group **groups = devres->groups;
1061
1062 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1063 sysfs_remove_groups(&dev->kobj, groups);
1064}
1065
1066/**
1067 * devm_device_add_group - given a device, create a managed attribute group
1068 * @dev: The device to create the group for
1069 * @grp: The attribute group to create
1070 *
1071 * This function creates a group for the first time. It will explicitly
1072 * warn and error if any of the attribute files being created already exist.
1073 *
1074 * Returns 0 on success or error code on failure.
1075 */
1076int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1077{
1078 union device_attr_group_devres *devres;
1079 int error;
1080
1081 devres = devres_alloc(devm_attr_group_remove,
1082 sizeof(*devres), GFP_KERNEL);
1083 if (!devres)
1084 return -ENOMEM;
1085
1086 error = sysfs_create_group(&dev->kobj, grp);
1087 if (error) {
1088 devres_free(devres);
1089 return error;
1090 }
1091
1092 devres->group = grp;
1093 devres_add(dev, devres);
1094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(devm_device_add_group);
1097
1098/**
1099 * devm_device_remove_group: remove a managed group from a device
1100 * @dev: device to remove the group from
1101 * @grp: group to remove
1102 *
1103 * This function removes a group of attributes from a device. The attributes
1104 * previously have to have been created for this group, otherwise it will fail.
1105 */
1106void devm_device_remove_group(struct device *dev,
1107 const struct attribute_group *grp)
1108{
1109 WARN_ON(devres_release(dev, devm_attr_group_remove,
1110 devm_attr_group_match,
1111 /* cast away const */ (void *)grp));
1112}
1113EXPORT_SYMBOL_GPL(devm_device_remove_group);
1114
1115/**
1116 * devm_device_add_groups - create a bunch of managed attribute groups
1117 * @dev: The device to create the group for
1118 * @groups: The attribute groups to create, NULL terminated
1119 *
1120 * This function creates a bunch of managed attribute groups. If an error
1121 * occurs when creating a group, all previously created groups will be
1122 * removed, unwinding everything back to the original state when this
1123 * function was called. It will explicitly warn and error if any of the
1124 * attribute files being created already exist.
1125 *
1126 * Returns 0 on success or error code from sysfs_create_group on failure.
1127 */
1128int devm_device_add_groups(struct device *dev,
1129 const struct attribute_group **groups)
1130{
1131 union device_attr_group_devres *devres;
1132 int error;
1133
1134 devres = devres_alloc(devm_attr_groups_remove,
1135 sizeof(*devres), GFP_KERNEL);
1136 if (!devres)
1137 return -ENOMEM;
1138
1139 error = sysfs_create_groups(&dev->kobj, groups);
1140 if (error) {
1141 devres_free(devres);
1142 return error;
1143 }
1144
1145 devres->groups = groups;
1146 devres_add(dev, devres);
1147 return 0;
1148}
1149EXPORT_SYMBOL_GPL(devm_device_add_groups);
1150
1151/**
1152 * devm_device_remove_groups - remove a list of managed groups
1153 *
1154 * @dev: The device for the groups to be removed from
1155 * @groups: NULL terminated list of groups to be removed
1156 *
1157 * If groups is not NULL, remove the specified groups from the device.
1158 */
1159void devm_device_remove_groups(struct device *dev,
1160 const struct attribute_group **groups)
1161{
1162 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1163 devm_attr_group_match,
1164 /* cast away const */ (void *)groups));
1165}
1166EXPORT_SYMBOL_GPL(devm_device_remove_groups);
de0ff00d 1167
2620efef
GKH
1168static int device_add_attrs(struct device *dev)
1169{
1170 struct class *class = dev->class;
aed65af1 1171 const struct device_type *type = dev->type;
621a1672 1172 int error;
2620efef 1173
621a1672 1174 if (class) {
d05a6f96 1175 error = device_add_groups(dev, class->dev_groups);
f9f852df 1176 if (error)
621a1672 1177 return error;
2620efef 1178 }
f9f852df 1179
621a1672
DT
1180 if (type) {
1181 error = device_add_groups(dev, type->groups);
f9f852df 1182 if (error)
a6b01ded 1183 goto err_remove_class_groups;
f9f852df
KS
1184 }
1185
621a1672
DT
1186 error = device_add_groups(dev, dev->groups);
1187 if (error)
1188 goto err_remove_type_groups;
1189
4f3549d7 1190 if (device_supports_offline(dev) && !dev->offline_disabled) {
c5e064a6 1191 error = device_create_file(dev, &dev_attr_online);
4f3549d7 1192 if (error)
ecfbf6fd 1193 goto err_remove_dev_groups;
4f3549d7
RW
1194 }
1195
621a1672
DT
1196 return 0;
1197
ecfbf6fd
RW
1198 err_remove_dev_groups:
1199 device_remove_groups(dev, dev->groups);
621a1672
DT
1200 err_remove_type_groups:
1201 if (type)
1202 device_remove_groups(dev, type->groups);
d05a6f96
GKH
1203 err_remove_class_groups:
1204 if (class)
1205 device_remove_groups(dev, class->dev_groups);
621a1672 1206
2620efef
GKH
1207 return error;
1208}
1209
1210static void device_remove_attrs(struct device *dev)
1211{
1212 struct class *class = dev->class;
aed65af1 1213 const struct device_type *type = dev->type;
2620efef 1214
c5e064a6 1215 device_remove_file(dev, &dev_attr_online);
621a1672 1216 device_remove_groups(dev, dev->groups);
f9f852df 1217
621a1672
DT
1218 if (type)
1219 device_remove_groups(dev, type->groups);
1220
a6b01ded 1221 if (class)
d05a6f96 1222 device_remove_groups(dev, class->dev_groups);
2620efef
GKH
1223}
1224
c5e064a6 1225static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
23681e47
GKH
1226 char *buf)
1227{
1228 return print_dev_t(buf, dev->devt);
1229}
c5e064a6 1230static DEVICE_ATTR_RO(dev);
ad6a1e1c 1231
ca22e56d 1232/* /sys/devices/ */
881c6cfd 1233struct kset *devices_kset;
1da177e4 1234
52cdbdd4
GS
1235/**
1236 * devices_kset_move_before - Move device in the devices_kset's list.
1237 * @deva: Device to move.
1238 * @devb: Device @deva should come before.
1239 */
1240static void devices_kset_move_before(struct device *deva, struct device *devb)
1241{
1242 if (!devices_kset)
1243 return;
1244 pr_debug("devices_kset: Moving %s before %s\n",
1245 dev_name(deva), dev_name(devb));
1246 spin_lock(&devices_kset->list_lock);
1247 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1248 spin_unlock(&devices_kset->list_lock);
1249}
1250
1251/**
1252 * devices_kset_move_after - Move device in the devices_kset's list.
1253 * @deva: Device to move
1254 * @devb: Device @deva should come after.
1255 */
1256static void devices_kset_move_after(struct device *deva, struct device *devb)
1257{
1258 if (!devices_kset)
1259 return;
1260 pr_debug("devices_kset: Moving %s after %s\n",
1261 dev_name(deva), dev_name(devb));
1262 spin_lock(&devices_kset->list_lock);
1263 list_move(&deva->kobj.entry, &devb->kobj.entry);
1264 spin_unlock(&devices_kset->list_lock);
1265}
1266
1267/**
1268 * devices_kset_move_last - move the device to the end of devices_kset's list.
1269 * @dev: device to move
1270 */
1271void devices_kset_move_last(struct device *dev)
1272{
1273 if (!devices_kset)
1274 return;
1275 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1276 spin_lock(&devices_kset->list_lock);
1277 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1278 spin_unlock(&devices_kset->list_lock);
1279}
1280
1da177e4 1281/**
4a3ad20c
GKH
1282 * device_create_file - create sysfs attribute file for device.
1283 * @dev: device.
1284 * @attr: device attribute descriptor.
1da177e4 1285 */
26579ab7
PC
1286int device_create_file(struct device *dev,
1287 const struct device_attribute *attr)
1da177e4
LT
1288{
1289 int error = 0;
8f46baaa
FB
1290
1291 if (dev) {
1292 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
97521978 1293 "Attribute %s: write permission without 'store'\n",
1294 attr->attr.name);
8f46baaa 1295 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
97521978 1296 "Attribute %s: read permission without 'show'\n",
1297 attr->attr.name);
1da177e4 1298 error = sysfs_create_file(&dev->kobj, &attr->attr);
8f46baaa
FB
1299 }
1300
1da177e4
LT
1301 return error;
1302}
86df2687 1303EXPORT_SYMBOL_GPL(device_create_file);
1da177e4
LT
1304
1305/**
4a3ad20c
GKH
1306 * device_remove_file - remove sysfs attribute file.
1307 * @dev: device.
1308 * @attr: device attribute descriptor.
1da177e4 1309 */
26579ab7
PC
1310void device_remove_file(struct device *dev,
1311 const struct device_attribute *attr)
1da177e4 1312{
0c98b19f 1313 if (dev)
1da177e4 1314 sysfs_remove_file(&dev->kobj, &attr->attr);
1da177e4 1315}
86df2687 1316EXPORT_SYMBOL_GPL(device_remove_file);
1da177e4 1317
6b0afc2a
TH
1318/**
1319 * device_remove_file_self - remove sysfs attribute file from its own method.
1320 * @dev: device.
1321 * @attr: device attribute descriptor.
1322 *
1323 * See kernfs_remove_self() for details.
1324 */
1325bool device_remove_file_self(struct device *dev,
1326 const struct device_attribute *attr)
1327{
1328 if (dev)
1329 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1330 else
1331 return false;
1332}
1333EXPORT_SYMBOL_GPL(device_remove_file_self);
1334
2589f188
GKH
1335/**
1336 * device_create_bin_file - create sysfs binary attribute file for device.
1337 * @dev: device.
1338 * @attr: device binary attribute descriptor.
1339 */
66ecb92b
PC
1340int device_create_bin_file(struct device *dev,
1341 const struct bin_attribute *attr)
2589f188
GKH
1342{
1343 int error = -EINVAL;
1344 if (dev)
1345 error = sysfs_create_bin_file(&dev->kobj, attr);
1346 return error;
1347}
1348EXPORT_SYMBOL_GPL(device_create_bin_file);
1349
1350/**
1351 * device_remove_bin_file - remove sysfs binary attribute file
1352 * @dev: device.
1353 * @attr: device binary attribute descriptor.
1354 */
66ecb92b
PC
1355void device_remove_bin_file(struct device *dev,
1356 const struct bin_attribute *attr)
2589f188
GKH
1357{
1358 if (dev)
1359 sysfs_remove_bin_file(&dev->kobj, attr);
1360}
1361EXPORT_SYMBOL_GPL(device_remove_bin_file);
1362
34bb61f9
JB
1363static void klist_children_get(struct klist_node *n)
1364{
f791b8c8
GKH
1365 struct device_private *p = to_device_private_parent(n);
1366 struct device *dev = p->device;
34bb61f9
JB
1367
1368 get_device(dev);
1369}
1370
1371static void klist_children_put(struct klist_node *n)
1372{
f791b8c8
GKH
1373 struct device_private *p = to_device_private_parent(n);
1374 struct device *dev = p->device;
34bb61f9
JB
1375
1376 put_device(dev);
1377}
1378
1da177e4 1379/**
4a3ad20c
GKH
1380 * device_initialize - init device structure.
1381 * @dev: device.
1da177e4 1382 *
5739411a
CH
1383 * This prepares the device for use by other layers by initializing
1384 * its fields.
4a3ad20c 1385 * It is the first half of device_register(), if called by
5739411a
CH
1386 * that function, though it can also be called separately, so one
1387 * may use @dev's fields. In particular, get_device()/put_device()
1388 * may be used for reference counting of @dev after calling this
1389 * function.
1390 *
b10d5efd
AS
1391 * All fields in @dev must be initialized by the caller to 0, except
1392 * for those explicitly set to some other value. The simplest
1393 * approach is to use kzalloc() to allocate the structure containing
1394 * @dev.
1395 *
5739411a
CH
1396 * NOTE: Use put_device() to give up your reference instead of freeing
1397 * @dev directly once you have called this function.
1da177e4 1398 */
1da177e4
LT
1399void device_initialize(struct device *dev)
1400{
881c6cfd 1401 dev->kobj.kset = devices_kset;
f9cb074b 1402 kobject_init(&dev->kobj, &device_ktype);
1da177e4 1403 INIT_LIST_HEAD(&dev->dma_pools);
3142788b 1404 mutex_init(&dev->mutex);
1704f47b 1405 lockdep_set_novalidate_class(&dev->mutex);
9ac7849e
TH
1406 spin_lock_init(&dev->devres_lock);
1407 INIT_LIST_HEAD(&dev->devres_head);
3b98aeaf 1408 device_pm_init(dev);
87348136 1409 set_dev_node(dev, -1);
4a7cc831
JL
1410#ifdef CONFIG_GENERIC_MSI_IRQ
1411 INIT_LIST_HEAD(&dev->msi_list);
1412#endif
9ed98953
RW
1413 INIT_LIST_HEAD(&dev->links.consumers);
1414 INIT_LIST_HEAD(&dev->links.suppliers);
1415 dev->links.status = DL_DEV_NO_DRIVER;
1da177e4 1416}
86df2687 1417EXPORT_SYMBOL_GPL(device_initialize);
1da177e4 1418
d73ce004 1419struct kobject *virtual_device_parent(struct device *dev)
f0ee61a6 1420{
86406245 1421 static struct kobject *virtual_dir = NULL;
f0ee61a6 1422
86406245 1423 if (!virtual_dir)
4ff6abff 1424 virtual_dir = kobject_create_and_add("virtual",
881c6cfd 1425 &devices_kset->kobj);
f0ee61a6 1426
86406245 1427 return virtual_dir;
f0ee61a6
GKH
1428}
1429
bc451f20
EB
1430struct class_dir {
1431 struct kobject kobj;
1432 struct class *class;
1433};
1434
1435#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1436
1437static void class_dir_release(struct kobject *kobj)
1438{
1439 struct class_dir *dir = to_class_dir(kobj);
1440 kfree(dir);
1441}
1442
1443static const
1444struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
40fa5422 1445{
bc451f20
EB
1446 struct class_dir *dir = to_class_dir(kobj);
1447 return dir->class->ns_type;
1448}
1449
1450static struct kobj_type class_dir_ktype = {
1451 .release = class_dir_release,
1452 .sysfs_ops = &kobj_sysfs_ops,
1453 .child_ns_type = class_dir_child_ns_type
1454};
1455
1456static struct kobject *
1457class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1458{
1459 struct class_dir *dir;
43968d2f
GKH
1460 int retval;
1461
bc451f20
EB
1462 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1463 if (!dir)
1464 return NULL;
1465
1466 dir->class = class;
1467 kobject_init(&dir->kobj, &class_dir_ktype);
1468
6b6e39a6 1469 dir->kobj.kset = &class->p->glue_dirs;
bc451f20
EB
1470
1471 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1472 if (retval < 0) {
1473 kobject_put(&dir->kobj);
1474 return NULL;
1475 }
1476 return &dir->kobj;
1477}
1478
e4a60d13 1479static DEFINE_MUTEX(gdp_mutex);
bc451f20
EB
1480
1481static struct kobject *get_device_parent(struct device *dev,
1482 struct device *parent)
1483{
86406245
KS
1484 if (dev->class) {
1485 struct kobject *kobj = NULL;
1486 struct kobject *parent_kobj;
1487 struct kobject *k;
1488
ead454fe 1489#ifdef CONFIG_BLOCK
39aba963 1490 /* block disks show up in /sys/block */
e52eec13 1491 if (sysfs_deprecated && dev->class == &block_class) {
39aba963
KS
1492 if (parent && parent->class == &block_class)
1493 return &parent->kobj;
6b6e39a6 1494 return &block_class.p->subsys.kobj;
39aba963 1495 }
ead454fe 1496#endif
e52eec13 1497
86406245
KS
1498 /*
1499 * If we have no parent, we live in "virtual".
0f4dafc0
KS
1500 * Class-devices with a non class-device as parent, live
1501 * in a "glue" directory to prevent namespace collisions.
86406245
KS
1502 */
1503 if (parent == NULL)
1504 parent_kobj = virtual_device_parent(dev);
24b1442d 1505 else if (parent->class && !dev->class->ns_type)
86406245
KS
1506 return &parent->kobj;
1507 else
1508 parent_kobj = &parent->kobj;
1509
77d3d7c1
TH
1510 mutex_lock(&gdp_mutex);
1511
86406245 1512 /* find our class-directory at the parent and reference it */
6b6e39a6
KS
1513 spin_lock(&dev->class->p->glue_dirs.list_lock);
1514 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
86406245
KS
1515 if (k->parent == parent_kobj) {
1516 kobj = kobject_get(k);
1517 break;
1518 }
6b6e39a6 1519 spin_unlock(&dev->class->p->glue_dirs.list_lock);
77d3d7c1
TH
1520 if (kobj) {
1521 mutex_unlock(&gdp_mutex);
86406245 1522 return kobj;
77d3d7c1 1523 }
86406245
KS
1524
1525 /* or create a new class-directory at the parent device */
bc451f20 1526 k = class_dir_create_and_add(dev->class, parent_kobj);
0f4dafc0 1527 /* do not emit an uevent for this simple "glue" directory */
77d3d7c1 1528 mutex_unlock(&gdp_mutex);
43968d2f 1529 return k;
86406245
KS
1530 }
1531
ca22e56d
KS
1532 /* subsystems can specify a default root directory for their devices */
1533 if (!parent && dev->bus && dev->bus->dev_root)
1534 return &dev->bus->dev_root->kobj;
1535
86406245 1536 if (parent)
c744aeae
CH
1537 return &parent->kobj;
1538 return NULL;
1539}
da231fd5 1540
cebf8fd1
ML
1541static inline bool live_in_glue_dir(struct kobject *kobj,
1542 struct device *dev)
1543{
1544 if (!kobj || !dev->class ||
1545 kobj->kset != &dev->class->p->glue_dirs)
1546 return false;
1547 return true;
1548}
1549
1550static inline struct kobject *get_glue_dir(struct device *dev)
1551{
1552 return dev->kobj.parent;
1553}
1554
1555/*
1556 * make sure cleaning up dir as the last step, we need to make
1557 * sure .release handler of kobject is run with holding the
1558 * global lock
1559 */
63b6971a 1560static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
da231fd5 1561{
0f4dafc0 1562 /* see if we live in a "glue" directory */
cebf8fd1 1563 if (!live_in_glue_dir(glue_dir, dev))
da231fd5
KS
1564 return;
1565
e4a60d13 1566 mutex_lock(&gdp_mutex);
0f4dafc0 1567 kobject_put(glue_dir);
e4a60d13 1568 mutex_unlock(&gdp_mutex);
da231fd5 1569}
63b6971a 1570
2ee97caf
CH
1571static int device_add_class_symlinks(struct device *dev)
1572{
5590f319 1573 struct device_node *of_node = dev_of_node(dev);
2ee97caf
CH
1574 int error;
1575
5590f319 1576 if (of_node) {
0c3c234b 1577 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
5590f319
BH
1578 if (error)
1579 dev_warn(dev, "Error %d creating of_node link\n",error);
1580 /* An error here doesn't warrant bringing down the device */
1581 }
1582
2ee97caf
CH
1583 if (!dev->class)
1584 return 0;
da231fd5 1585
1fbfee6c 1586 error = sysfs_create_link(&dev->kobj,
6b6e39a6 1587 &dev->class->p->subsys.kobj,
2ee97caf
CH
1588 "subsystem");
1589 if (error)
5590f319 1590 goto out_devnode;
da231fd5 1591
4e886c29 1592 if (dev->parent && device_is_not_partition(dev)) {
39aba963 1593 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
4f01a757
DT
1594 "device");
1595 if (error)
39aba963 1596 goto out_subsys;
2ee97caf 1597 }
2ee97caf 1598
ead454fe 1599#ifdef CONFIG_BLOCK
39aba963 1600 /* /sys/block has directories and does not need symlinks */
e52eec13 1601 if (sysfs_deprecated && dev->class == &block_class)
39aba963 1602 return 0;
ead454fe 1603#endif
39aba963 1604
da231fd5 1605 /* link in the class directory pointing to the device */
6b6e39a6 1606 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1e0b2cf9 1607 &dev->kobj, dev_name(dev));
da231fd5 1608 if (error)
39aba963 1609 goto out_device;
da231fd5 1610
da231fd5
KS
1611 return 0;
1612
39aba963
KS
1613out_device:
1614 sysfs_remove_link(&dev->kobj, "device");
da231fd5 1615
2ee97caf
CH
1616out_subsys:
1617 sysfs_remove_link(&dev->kobj, "subsystem");
5590f319
BH
1618out_devnode:
1619 sysfs_remove_link(&dev->kobj, "of_node");
2ee97caf
CH
1620 return error;
1621}
1622
1623static void device_remove_class_symlinks(struct device *dev)
1624{
5590f319
BH
1625 if (dev_of_node(dev))
1626 sysfs_remove_link(&dev->kobj, "of_node");
1627
2ee97caf
CH
1628 if (!dev->class)
1629 return;
da231fd5 1630
4e886c29 1631 if (dev->parent && device_is_not_partition(dev))
da231fd5 1632 sysfs_remove_link(&dev->kobj, "device");
2ee97caf 1633 sysfs_remove_link(&dev->kobj, "subsystem");
ead454fe 1634#ifdef CONFIG_BLOCK
e52eec13 1635 if (sysfs_deprecated && dev->class == &block_class)
39aba963 1636 return;
ead454fe 1637#endif
6b6e39a6 1638 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2ee97caf
CH
1639}
1640
413c239f
SR
1641/**
1642 * dev_set_name - set a device name
1643 * @dev: device
46232366 1644 * @fmt: format string for the device's name
413c239f
SR
1645 */
1646int dev_set_name(struct device *dev, const char *fmt, ...)
1647{
1648 va_list vargs;
1fa5ae85 1649 int err;
413c239f
SR
1650
1651 va_start(vargs, fmt);
1fa5ae85 1652 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
413c239f 1653 va_end(vargs);
1fa5ae85 1654 return err;
413c239f
SR
1655}
1656EXPORT_SYMBOL_GPL(dev_set_name);
1657
e105b8bf
DW
1658/**
1659 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1660 * @dev: device
1661 *
1662 * By default we select char/ for new entries. Setting class->dev_obj
1663 * to NULL prevents an entry from being created. class->dev_kobj must
1664 * be set (or cleared) before any devices are registered to the class
1665 * otherwise device_create_sys_dev_entry() and
0d4e293c
PK
1666 * device_remove_sys_dev_entry() will disagree about the presence of
1667 * the link.
e105b8bf
DW
1668 */
1669static struct kobject *device_to_dev_kobj(struct device *dev)
1670{
1671 struct kobject *kobj;
1672
1673 if (dev->class)
1674 kobj = dev->class->dev_kobj;
1675 else
1676 kobj = sysfs_dev_char_kobj;
1677
1678 return kobj;
1679}
1680
1681static int device_create_sys_dev_entry(struct device *dev)
1682{
1683 struct kobject *kobj = device_to_dev_kobj(dev);
1684 int error = 0;
1685 char devt_str[15];
1686
1687 if (kobj) {
1688 format_dev_t(devt_str, dev->devt);
1689 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1690 }
1691
1692 return error;
1693}
1694
1695static void device_remove_sys_dev_entry(struct device *dev)
1696{
1697 struct kobject *kobj = device_to_dev_kobj(dev);
1698 char devt_str[15];
1699
1700 if (kobj) {
1701 format_dev_t(devt_str, dev->devt);
1702 sysfs_remove_link(kobj, devt_str);
1703 }
1704}
1705
b4028437
GKH
1706int device_private_init(struct device *dev)
1707{
1708 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1709 if (!dev->p)
1710 return -ENOMEM;
1711 dev->p->device = dev;
1712 klist_init(&dev->p->klist_children, klist_children_get,
1713 klist_children_put);
ef8a3fd6 1714 INIT_LIST_HEAD(&dev->p->deferred_probe);
b4028437
GKH
1715 return 0;
1716}
1717
1da177e4 1718/**
4a3ad20c
GKH
1719 * device_add - add device to device hierarchy.
1720 * @dev: device.
1da177e4 1721 *
4a3ad20c
GKH
1722 * This is part 2 of device_register(), though may be called
1723 * separately _iff_ device_initialize() has been called separately.
1da177e4 1724 *
5739411a 1725 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
4a3ad20c
GKH
1726 * to the global and sibling lists for the device, then
1727 * adds it to the other relevant subsystems of the driver model.
5739411a 1728 *
b10d5efd
AS
1729 * Do not call this routine or device_register() more than once for
1730 * any device structure. The driver model core is not designed to work
1731 * with devices that get unregistered and then spring back to life.
1732 * (Among other things, it's very hard to guarantee that all references
1733 * to the previous incarnation of @dev have been dropped.) Allocate
1734 * and register a fresh new struct device instead.
1735 *
5739411a
CH
1736 * NOTE: _Never_ directly free @dev after calling this function, even
1737 * if it returned an error! Always use put_device() to give up your
1738 * reference instead.
1da177e4
LT
1739 */
1740int device_add(struct device *dev)
1741{
35dbf4ef 1742 struct device *parent;
ca22e56d 1743 struct kobject *kobj;
c47ed219 1744 struct class_interface *class_intf;
c906a48a 1745 int error = -EINVAL;
cebf8fd1 1746 struct kobject *glue_dir = NULL;
775b64d2 1747
1da177e4 1748 dev = get_device(dev);
c906a48a
GKH
1749 if (!dev)
1750 goto done;
1751
fb069a5d 1752 if (!dev->p) {
b4028437
GKH
1753 error = device_private_init(dev);
1754 if (error)
1755 goto done;
fb069a5d 1756 }
fb069a5d 1757
1fa5ae85
KS
1758 /*
1759 * for statically allocated devices, which should all be converted
1760 * some day, we need to initialize the name. We prevent reading back
1761 * the name, and force the use of dev_name()
1762 */
1763 if (dev->init_name) {
acc0e90f 1764 dev_set_name(dev, "%s", dev->init_name);
1fa5ae85
KS
1765 dev->init_name = NULL;
1766 }
c906a48a 1767
ca22e56d
KS
1768 /* subsystems can specify simple device enumeration */
1769 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1770 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1771
e6309e75
TG
1772 if (!dev_name(dev)) {
1773 error = -EINVAL;
5c8563d7 1774 goto name_error;
e6309e75 1775 }
1da177e4 1776
1e0b2cf9 1777 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
c205ef48 1778
1da177e4 1779 parent = get_device(dev->parent);
ca22e56d
KS
1780 kobj = get_device_parent(dev, parent);
1781 if (kobj)
1782 dev->kobj.parent = kobj;
1da177e4 1783
0d358f22 1784 /* use parent numa_node */
56f2de81 1785 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
0d358f22
YL
1786 set_dev_node(dev, dev_to_node(parent));
1787
1da177e4 1788 /* first, register with generic layer. */
8a577ffc
KS
1789 /* we require the name to be set before, and pass NULL */
1790 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
cebf8fd1
ML
1791 if (error) {
1792 glue_dir = get_glue_dir(dev);
1da177e4 1793 goto Error;
cebf8fd1 1794 }
a7fd6706 1795
37022644
BW
1796 /* notify platform of device entry */
1797 if (platform_notify)
1798 platform_notify(dev);
1799
c5e064a6 1800 error = device_create_file(dev, &dev_attr_uevent);
a306eea4
CH
1801 if (error)
1802 goto attrError;
a7fd6706 1803
2ee97caf
CH
1804 error = device_add_class_symlinks(dev);
1805 if (error)
1806 goto SymlinkError;
dc0afa83
CH
1807 error = device_add_attrs(dev);
1808 if (error)
2620efef 1809 goto AttrsError;
dc0afa83
CH
1810 error = bus_add_device(dev);
1811 if (error)
1da177e4 1812 goto BusError;
3b98aeaf 1813 error = dpm_sysfs_add(dev);
57eee3d2 1814 if (error)
3b98aeaf
AS
1815 goto DPMError;
1816 device_pm_add(dev);
ec0676ee 1817
0cd75047
SK
1818 if (MAJOR(dev->devt)) {
1819 error = device_create_file(dev, &dev_attr_dev);
1820 if (error)
1821 goto DevAttrError;
1822
1823 error = device_create_sys_dev_entry(dev);
1824 if (error)
1825 goto SysEntryError;
1826
1827 devtmpfs_create_node(dev);
1828 }
1829
ec0676ee 1830 /* Notify clients of device addition. This call must come
268863f4 1831 * after dpm_sysfs_add() and before kobject_uevent().
ec0676ee
AS
1832 */
1833 if (dev->bus)
1834 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1835 BUS_NOTIFY_ADD_DEVICE, dev);
1836
83b5fb4c 1837 kobject_uevent(&dev->kobj, KOBJ_ADD);
2023c610 1838 bus_probe_device(dev);
1da177e4 1839 if (parent)
f791b8c8
GKH
1840 klist_add_tail(&dev->p->knode_parent,
1841 &parent->p->klist_children);
1da177e4 1842
5d9fd169 1843 if (dev->class) {
ca22e56d 1844 mutex_lock(&dev->class->p->mutex);
c47ed219 1845 /* tie the class to the device */
5a3ceb86 1846 klist_add_tail(&dev->knode_class,
6b6e39a6 1847 &dev->class->p->klist_devices);
c47ed219
GKH
1848
1849 /* notify any interfaces that the device is here */
184f1f77 1850 list_for_each_entry(class_intf,
ca22e56d 1851 &dev->class->p->interfaces, node)
c47ed219
GKH
1852 if (class_intf->add_dev)
1853 class_intf->add_dev(dev, class_intf);
ca22e56d 1854 mutex_unlock(&dev->class->p->mutex);
5d9fd169 1855 }
c906a48a 1856done:
1da177e4
LT
1857 put_device(dev);
1858 return error;
0cd75047
SK
1859 SysEntryError:
1860 if (MAJOR(dev->devt))
1861 device_remove_file(dev, &dev_attr_dev);
1862 DevAttrError:
1863 device_pm_remove(dev);
1864 dpm_sysfs_remove(dev);
3b98aeaf 1865 DPMError:
57eee3d2
RW
1866 bus_remove_device(dev);
1867 BusError:
82f0cf9b 1868 device_remove_attrs(dev);
2620efef 1869 AttrsError:
2ee97caf
CH
1870 device_remove_class_symlinks(dev);
1871 SymlinkError:
c5e064a6 1872 device_remove_file(dev, &dev_attr_uevent);
23681e47 1873 attrError:
312c004d 1874 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
cebf8fd1 1875 glue_dir = get_glue_dir(dev);
1da177e4
LT
1876 kobject_del(&dev->kobj);
1877 Error:
cebf8fd1 1878 cleanup_glue_dir(dev, glue_dir);
5f0163a5 1879 put_device(parent);
5c8563d7
KS
1880name_error:
1881 kfree(dev->p);
1882 dev->p = NULL;
c906a48a 1883 goto done;
1da177e4 1884}
86df2687 1885EXPORT_SYMBOL_GPL(device_add);
1da177e4 1886
1da177e4 1887/**
4a3ad20c
GKH
1888 * device_register - register a device with the system.
1889 * @dev: pointer to the device structure
1da177e4 1890 *
4a3ad20c
GKH
1891 * This happens in two clean steps - initialize the device
1892 * and add it to the system. The two steps can be called
1893 * separately, but this is the easiest and most common.
1894 * I.e. you should only call the two helpers separately if
1895 * have a clearly defined need to use and refcount the device
1896 * before it is added to the hierarchy.
5739411a 1897 *
b10d5efd
AS
1898 * For more information, see the kerneldoc for device_initialize()
1899 * and device_add().
1900 *
5739411a
CH
1901 * NOTE: _Never_ directly free @dev after calling this function, even
1902 * if it returned an error! Always use put_device() to give up the
1903 * reference initialized in this function instead.
1da177e4 1904 */
1da177e4
LT
1905int device_register(struct device *dev)
1906{
1907 device_initialize(dev);
1908 return device_add(dev);
1909}
86df2687 1910EXPORT_SYMBOL_GPL(device_register);
1da177e4 1911
1da177e4 1912/**
4a3ad20c
GKH
1913 * get_device - increment reference count for device.
1914 * @dev: device.
1da177e4 1915 *
4a3ad20c
GKH
1916 * This simply forwards the call to kobject_get(), though
1917 * we do take care to provide for the case that we get a NULL
1918 * pointer passed in.
1da177e4 1919 */
4a3ad20c 1920struct device *get_device(struct device *dev)
1da177e4 1921{
b0d1f807 1922 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1da177e4 1923}
86df2687 1924EXPORT_SYMBOL_GPL(get_device);
1da177e4 1925
1da177e4 1926/**
4a3ad20c
GKH
1927 * put_device - decrement reference count.
1928 * @dev: device in question.
1da177e4 1929 */
4a3ad20c 1930void put_device(struct device *dev)
1da177e4 1931{
edfaa7c3 1932 /* might_sleep(); */
1da177e4
LT
1933 if (dev)
1934 kobject_put(&dev->kobj);
1935}
86df2687 1936EXPORT_SYMBOL_GPL(put_device);
1da177e4 1937
1da177e4 1938/**
4a3ad20c
GKH
1939 * device_del - delete device from system.
1940 * @dev: device.
1da177e4 1941 *
4a3ad20c
GKH
1942 * This is the first part of the device unregistration
1943 * sequence. This removes the device from the lists we control
1944 * from here, has it removed from the other driver model
1945 * subsystems it was added to in device_add(), and removes it
1946 * from the kobject hierarchy.
1da177e4 1947 *
4a3ad20c
GKH
1948 * NOTE: this should be called manually _iff_ device_add() was
1949 * also called manually.
1da177e4 1950 */
4a3ad20c 1951void device_del(struct device *dev)
1da177e4 1952{
4a3ad20c 1953 struct device *parent = dev->parent;
cebf8fd1 1954 struct kobject *glue_dir = NULL;
c47ed219 1955 struct class_interface *class_intf;
1da177e4 1956
ec0676ee
AS
1957 /* Notify clients of device removal. This call must come
1958 * before dpm_sysfs_remove().
1959 */
1960 if (dev->bus)
1961 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1962 BUS_NOTIFY_DEL_DEVICE, dev);
9ed98953 1963
3b98aeaf 1964 dpm_sysfs_remove(dev);
1da177e4 1965 if (parent)
f791b8c8 1966 klist_del(&dev->p->knode_parent);
e105b8bf 1967 if (MAJOR(dev->devt)) {
2b2af54a 1968 devtmpfs_delete_node(dev);
e105b8bf 1969 device_remove_sys_dev_entry(dev);
c5e064a6 1970 device_remove_file(dev, &dev_attr_dev);
e105b8bf 1971 }
b9d9c82b 1972 if (dev->class) {
da231fd5 1973 device_remove_class_symlinks(dev);
99ef3ef8 1974
ca22e56d 1975 mutex_lock(&dev->class->p->mutex);
c47ed219 1976 /* notify any interfaces that the device is now gone */
184f1f77 1977 list_for_each_entry(class_intf,
ca22e56d 1978 &dev->class->p->interfaces, node)
c47ed219
GKH
1979 if (class_intf->remove_dev)
1980 class_intf->remove_dev(dev, class_intf);
1981 /* remove the device from the class list */
5a3ceb86 1982 klist_del(&dev->knode_class);
ca22e56d 1983 mutex_unlock(&dev->class->p->mutex);
b9d9c82b 1984 }
c5e064a6 1985 device_remove_file(dev, &dev_attr_uevent);
2620efef 1986 device_remove_attrs(dev);
28953533 1987 bus_remove_device(dev);
4b6d1f12 1988 device_pm_remove(dev);
d1c3414c 1989 driver_deferred_probe_del(dev);
478573c9 1990 device_remove_properties(dev);
2ec16150 1991 device_links_purge(dev);
1da177e4
LT
1992
1993 /* Notify the platform of the removal, in case they
1994 * need to do anything...
1995 */
1996 if (platform_notify_remove)
1997 platform_notify_remove(dev);
599bad38
JR
1998 if (dev->bus)
1999 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2000 BUS_NOTIFY_REMOVED_DEVICE, dev);
312c004d 2001 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
cebf8fd1 2002 glue_dir = get_glue_dir(dev);
1da177e4 2003 kobject_del(&dev->kobj);
cebf8fd1 2004 cleanup_glue_dir(dev, glue_dir);
da231fd5 2005 put_device(parent);
1da177e4 2006}
86df2687 2007EXPORT_SYMBOL_GPL(device_del);
1da177e4
LT
2008
2009/**
4a3ad20c
GKH
2010 * device_unregister - unregister device from system.
2011 * @dev: device going away.
1da177e4 2012 *
4a3ad20c
GKH
2013 * We do this in two parts, like we do device_register(). First,
2014 * we remove it from all the subsystems with device_del(), then
2015 * we decrement the reference count via put_device(). If that
2016 * is the final reference count, the device will be cleaned up
2017 * via device_release() above. Otherwise, the structure will
2018 * stick around until the final reference to the device is dropped.
1da177e4 2019 */
4a3ad20c 2020void device_unregister(struct device *dev)
1da177e4 2021{
1e0b2cf9 2022 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1da177e4
LT
2023 device_del(dev);
2024 put_device(dev);
2025}
86df2687 2026EXPORT_SYMBOL_GPL(device_unregister);
1da177e4 2027
3d060aeb
AS
2028static struct device *prev_device(struct klist_iter *i)
2029{
2030 struct klist_node *n = klist_prev(i);
2031 struct device *dev = NULL;
2032 struct device_private *p;
2033
2034 if (n) {
2035 p = to_device_private_parent(n);
2036 dev = p->device;
2037 }
2038 return dev;
2039}
2040
4a3ad20c 2041static struct device *next_device(struct klist_iter *i)
36239577 2042{
4a3ad20c 2043 struct klist_node *n = klist_next(i);
f791b8c8
GKH
2044 struct device *dev = NULL;
2045 struct device_private *p;
2046
2047 if (n) {
2048 p = to_device_private_parent(n);
2049 dev = p->device;
2050 }
2051 return dev;
36239577
PM
2052}
2053
6fcf53ac 2054/**
e454cea2 2055 * device_get_devnode - path of device node file
6fcf53ac 2056 * @dev: device
e454cea2 2057 * @mode: returned file access mode
3c2670e6
KS
2058 * @uid: returned file owner
2059 * @gid: returned file group
6fcf53ac
KS
2060 * @tmp: possibly allocated string
2061 *
2062 * Return the relative path of a possible device node.
2063 * Non-default names may need to allocate a memory to compose
2064 * a name. This memory is returned in tmp and needs to be
2065 * freed by the caller.
2066 */
e454cea2 2067const char *device_get_devnode(struct device *dev,
4e4098a3 2068 umode_t *mode, kuid_t *uid, kgid_t *gid,
3c2670e6 2069 const char **tmp)
6fcf53ac
KS
2070{
2071 char *s;
2072
2073 *tmp = NULL;
2074
2075 /* the device type may provide a specific name */
e454cea2 2076 if (dev->type && dev->type->devnode)
3c2670e6 2077 *tmp = dev->type->devnode(dev, mode, uid, gid);
6fcf53ac
KS
2078 if (*tmp)
2079 return *tmp;
2080
2081 /* the class may provide a specific name */
e454cea2
KS
2082 if (dev->class && dev->class->devnode)
2083 *tmp = dev->class->devnode(dev, mode);
6fcf53ac
KS
2084 if (*tmp)
2085 return *tmp;
2086
2087 /* return name without allocation, tmp == NULL */
2088 if (strchr(dev_name(dev), '!') == NULL)
2089 return dev_name(dev);
2090
2091 /* replace '!' in the name with '/' */
a29fd614
RV
2092 s = kstrdup(dev_name(dev), GFP_KERNEL);
2093 if (!s)
6fcf53ac 2094 return NULL;
a29fd614
RV
2095 strreplace(s, '!', '/');
2096 return *tmp = s;
6fcf53ac
KS
2097}
2098
1da177e4 2099/**
4a3ad20c
GKH
2100 * device_for_each_child - device child iterator.
2101 * @parent: parent struct device.
4a3ad20c 2102 * @fn: function to be called for each device.
f8878dcb 2103 * @data: data for the callback.
1da177e4 2104 *
4a3ad20c
GKH
2105 * Iterate over @parent's child devices, and call @fn for each,
2106 * passing it @data.
1da177e4 2107 *
4a3ad20c
GKH
2108 * We check the return of @fn each time. If it returns anything
2109 * other than 0, we break out and return that value.
1da177e4 2110 */
4a3ad20c
GKH
2111int device_for_each_child(struct device *parent, void *data,
2112 int (*fn)(struct device *dev, void *data))
1da177e4 2113{
36239577 2114 struct klist_iter i;
4a3ad20c 2115 struct device *child;
1da177e4
LT
2116 int error = 0;
2117
014c90db
GKH
2118 if (!parent->p)
2119 return 0;
2120
f791b8c8 2121 klist_iter_init(&parent->p->klist_children, &i);
36239577
PM
2122 while ((child = next_device(&i)) && !error)
2123 error = fn(child, data);
2124 klist_iter_exit(&i);
1da177e4
LT
2125 return error;
2126}
86df2687 2127EXPORT_SYMBOL_GPL(device_for_each_child);
1da177e4 2128
3d060aeb
AS
2129/**
2130 * device_for_each_child_reverse - device child iterator in reversed order.
2131 * @parent: parent struct device.
2132 * @fn: function to be called for each device.
2133 * @data: data for the callback.
2134 *
2135 * Iterate over @parent's child devices, and call @fn for each,
2136 * passing it @data.
2137 *
2138 * We check the return of @fn each time. If it returns anything
2139 * other than 0, we break out and return that value.
2140 */
2141int device_for_each_child_reverse(struct device *parent, void *data,
2142 int (*fn)(struct device *dev, void *data))
2143{
2144 struct klist_iter i;
2145 struct device *child;
2146 int error = 0;
2147
2148 if (!parent->p)
2149 return 0;
2150
2151 klist_iter_init(&parent->p->klist_children, &i);
2152 while ((child = prev_device(&i)) && !error)
2153 error = fn(child, data);
2154 klist_iter_exit(&i);
2155 return error;
2156}
2157EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2158
5ab69981
CH
2159/**
2160 * device_find_child - device iterator for locating a particular device.
2161 * @parent: parent struct device
5ab69981 2162 * @match: Callback function to check device
f8878dcb 2163 * @data: Data to pass to match function
5ab69981
CH
2164 *
2165 * This is similar to the device_for_each_child() function above, but it
2166 * returns a reference to a device that is 'found' for later use, as
2167 * determined by the @match callback.
2168 *
2169 * The callback should return 0 if the device doesn't match and non-zero
2170 * if it does. If the callback returns non-zero and a reference to the
2171 * current device can be obtained, this function will return to the caller
2172 * and not iterate over any more devices.
a4e2400a
FV
2173 *
2174 * NOTE: you will need to drop the reference with put_device() after use.
5ab69981 2175 */
4a3ad20c
GKH
2176struct device *device_find_child(struct device *parent, void *data,
2177 int (*match)(struct device *dev, void *data))
5ab69981
CH
2178{
2179 struct klist_iter i;
2180 struct device *child;
2181
2182 if (!parent)
2183 return NULL;
2184
f791b8c8 2185 klist_iter_init(&parent->p->klist_children, &i);
5ab69981
CH
2186 while ((child = next_device(&i)))
2187 if (match(child, data) && get_device(child))
2188 break;
2189 klist_iter_exit(&i);
2190 return child;
2191}
86df2687 2192EXPORT_SYMBOL_GPL(device_find_child);
5ab69981 2193
1da177e4
LT
2194int __init devices_init(void)
2195{
881c6cfd
GKH
2196 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2197 if (!devices_kset)
2198 return -ENOMEM;
e105b8bf
DW
2199 dev_kobj = kobject_create_and_add("dev", NULL);
2200 if (!dev_kobj)
2201 goto dev_kobj_err;
2202 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2203 if (!sysfs_dev_block_kobj)
2204 goto block_kobj_err;
2205 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2206 if (!sysfs_dev_char_kobj)
2207 goto char_kobj_err;
2208
881c6cfd 2209 return 0;
e105b8bf
DW
2210
2211 char_kobj_err:
2212 kobject_put(sysfs_dev_block_kobj);
2213 block_kobj_err:
2214 kobject_put(dev_kobj);
2215 dev_kobj_err:
2216 kset_unregister(devices_kset);
2217 return -ENOMEM;
1da177e4
LT
2218}
2219
4f3549d7
RW
2220static int device_check_offline(struct device *dev, void *not_used)
2221{
2222 int ret;
2223
2224 ret = device_for_each_child(dev, NULL, device_check_offline);
2225 if (ret)
2226 return ret;
2227
2228 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2229}
2230
2231/**
2232 * device_offline - Prepare the device for hot-removal.
2233 * @dev: Device to be put offline.
2234 *
2235 * Execute the device bus type's .offline() callback, if present, to prepare
2236 * the device for a subsequent hot-removal. If that succeeds, the device must
2237 * not be used until either it is removed or its bus type's .online() callback
2238 * is executed.
2239 *
2240 * Call under device_hotplug_lock.
2241 */
2242int device_offline(struct device *dev)
2243{
2244 int ret;
2245
2246 if (dev->offline_disabled)
2247 return -EPERM;
2248
2249 ret = device_for_each_child(dev, NULL, device_check_offline);
2250 if (ret)
2251 return ret;
2252
2253 device_lock(dev);
2254 if (device_supports_offline(dev)) {
2255 if (dev->offline) {
2256 ret = 1;
2257 } else {
2258 ret = dev->bus->offline(dev);
2259 if (!ret) {
2260 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2261 dev->offline = true;
2262 }
2263 }
2264 }
2265 device_unlock(dev);
2266
2267 return ret;
2268}
2269
2270/**
2271 * device_online - Put the device back online after successful device_offline().
2272 * @dev: Device to be put back online.
2273 *
2274 * If device_offline() has been successfully executed for @dev, but the device
2275 * has not been removed subsequently, execute its bus type's .online() callback
2276 * to indicate that the device can be used again.
2277 *
2278 * Call under device_hotplug_lock.
2279 */
2280int device_online(struct device *dev)
2281{
2282 int ret = 0;
2283
2284 device_lock(dev);
2285 if (device_supports_offline(dev)) {
2286 if (dev->offline) {
2287 ret = dev->bus->online(dev);
2288 if (!ret) {
2289 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2290 dev->offline = false;
2291 }
2292 } else {
2293 ret = 1;
2294 }
2295 }
2296 device_unlock(dev);
2297
2298 return ret;
2299}
2300
7f100d15 2301struct root_device {
0aa0dc41
MM
2302 struct device dev;
2303 struct module *owner;
2304};
2305
93058424 2306static inline struct root_device *to_root_device(struct device *d)
481e2079
FW
2307{
2308 return container_of(d, struct root_device, dev);
2309}
0aa0dc41
MM
2310
2311static void root_device_release(struct device *dev)
2312{
2313 kfree(to_root_device(dev));
2314}
2315
2316/**
2317 * __root_device_register - allocate and register a root device
2318 * @name: root device name
2319 * @owner: owner module of the root device, usually THIS_MODULE
2320 *
2321 * This function allocates a root device and registers it
2322 * using device_register(). In order to free the returned
2323 * device, use root_device_unregister().
2324 *
2325 * Root devices are dummy devices which allow other devices
2326 * to be grouped under /sys/devices. Use this function to
2327 * allocate a root device and then use it as the parent of
2328 * any device which should appear under /sys/devices/{name}
2329 *
2330 * The /sys/devices/{name} directory will also contain a
2331 * 'module' symlink which points to the @owner directory
2332 * in sysfs.
2333 *
f0eae0ed
JN
2334 * Returns &struct device pointer on success, or ERR_PTR() on error.
2335 *
0aa0dc41
MM
2336 * Note: You probably want to use root_device_register().
2337 */
2338struct device *__root_device_register(const char *name, struct module *owner)
2339{
2340 struct root_device *root;
2341 int err = -ENOMEM;
2342
2343 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2344 if (!root)
2345 return ERR_PTR(err);
2346
acc0e90f 2347 err = dev_set_name(&root->dev, "%s", name);
0aa0dc41
MM
2348 if (err) {
2349 kfree(root);
2350 return ERR_PTR(err);
2351 }
2352
2353 root->dev.release = root_device_release;
2354
2355 err = device_register(&root->dev);
2356 if (err) {
2357 put_device(&root->dev);
2358 return ERR_PTR(err);
2359 }
2360
1d9e882b 2361#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
0aa0dc41
MM
2362 if (owner) {
2363 struct module_kobject *mk = &owner->mkobj;
2364
2365 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2366 if (err) {
2367 device_unregister(&root->dev);
2368 return ERR_PTR(err);
2369 }
2370 root->owner = owner;
2371 }
2372#endif
2373
2374 return &root->dev;
2375}
2376EXPORT_SYMBOL_GPL(__root_device_register);
2377
2378/**
2379 * root_device_unregister - unregister and free a root device
7cbcf225 2380 * @dev: device going away
0aa0dc41
MM
2381 *
2382 * This function unregisters and cleans up a device that was created by
2383 * root_device_register().
2384 */
2385void root_device_unregister(struct device *dev)
2386{
2387 struct root_device *root = to_root_device(dev);
2388
2389 if (root->owner)
2390 sysfs_remove_link(&root->dev.kobj, "module");
2391
2392 device_unregister(dev);
2393}
2394EXPORT_SYMBOL_GPL(root_device_unregister);
2395
23681e47
GKH
2396
2397static void device_create_release(struct device *dev)
2398{
1e0b2cf9 2399 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
23681e47
GKH
2400 kfree(dev);
2401}
2402
39ef3112
GR
2403static struct device *
2404device_create_groups_vargs(struct class *class, struct device *parent,
2405 dev_t devt, void *drvdata,
2406 const struct attribute_group **groups,
2407 const char *fmt, va_list args)
23681e47 2408{
23681e47
GKH
2409 struct device *dev = NULL;
2410 int retval = -ENODEV;
2411
2412 if (class == NULL || IS_ERR(class))
2413 goto error;
23681e47
GKH
2414
2415 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2416 if (!dev) {
2417 retval = -ENOMEM;
2418 goto error;
2419 }
2420
bbc780f8 2421 device_initialize(dev);
23681e47
GKH
2422 dev->devt = devt;
2423 dev->class = class;
2424 dev->parent = parent;
39ef3112 2425 dev->groups = groups;
23681e47 2426 dev->release = device_create_release;
8882b394 2427 dev_set_drvdata(dev, drvdata);
23681e47 2428
1fa5ae85
KS
2429 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2430 if (retval)
2431 goto error;
2432
bbc780f8 2433 retval = device_add(dev);
23681e47
GKH
2434 if (retval)
2435 goto error;
2436
23681e47
GKH
2437 return dev;
2438
2439error:
286661b3 2440 put_device(dev);
23681e47
GKH
2441 return ERR_PTR(retval);
2442}
39ef3112
GR
2443
2444/**
2445 * device_create_vargs - creates a device and registers it with sysfs
2446 * @class: pointer to the struct class that this device should be registered to
2447 * @parent: pointer to the parent struct device of this new device, if any
2448 * @devt: the dev_t for the char device to be added
2449 * @drvdata: the data to be added to the device for callbacks
2450 * @fmt: string for the device's name
2451 * @args: va_list for the device's name
2452 *
2453 * This function can be used by char device classes. A struct device
2454 * will be created in sysfs, registered to the specified class.
2455 *
2456 * A "dev" file will be created, showing the dev_t for the device, if
2457 * the dev_t is not 0,0.
2458 * If a pointer to a parent struct device is passed in, the newly created
2459 * struct device will be a child of that device in sysfs.
2460 * The pointer to the struct device will be returned from the call.
2461 * Any further sysfs files that might be required can be created using this
2462 * pointer.
2463 *
2464 * Returns &struct device pointer on success, or ERR_PTR() on error.
2465 *
2466 * Note: the struct class passed to this function must have previously
2467 * been created with a call to class_create().
2468 */
2469struct device *device_create_vargs(struct class *class, struct device *parent,
2470 dev_t devt, void *drvdata, const char *fmt,
2471 va_list args)
2472{
2473 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2474 fmt, args);
2475}
8882b394
GKH
2476EXPORT_SYMBOL_GPL(device_create_vargs);
2477
2478/**
4e106739 2479 * device_create - creates a device and registers it with sysfs
8882b394
GKH
2480 * @class: pointer to the struct class that this device should be registered to
2481 * @parent: pointer to the parent struct device of this new device, if any
2482 * @devt: the dev_t for the char device to be added
2483 * @drvdata: the data to be added to the device for callbacks
2484 * @fmt: string for the device's name
2485 *
2486 * This function can be used by char device classes. A struct device
2487 * will be created in sysfs, registered to the specified class.
2488 *
2489 * A "dev" file will be created, showing the dev_t for the device, if
2490 * the dev_t is not 0,0.
2491 * If a pointer to a parent struct device is passed in, the newly created
2492 * struct device will be a child of that device in sysfs.
2493 * The pointer to the struct device will be returned from the call.
2494 * Any further sysfs files that might be required can be created using this
2495 * pointer.
2496 *
f0eae0ed
JN
2497 * Returns &struct device pointer on success, or ERR_PTR() on error.
2498 *
8882b394
GKH
2499 * Note: the struct class passed to this function must have previously
2500 * been created with a call to class_create().
2501 */
4e106739
GKH
2502struct device *device_create(struct class *class, struct device *parent,
2503 dev_t devt, void *drvdata, const char *fmt, ...)
8882b394
GKH
2504{
2505 va_list vargs;
2506 struct device *dev;
2507
2508 va_start(vargs, fmt);
2509 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2510 va_end(vargs);
2511 return dev;
2512}
4e106739 2513EXPORT_SYMBOL_GPL(device_create);
8882b394 2514
39ef3112
GR
2515/**
2516 * device_create_with_groups - creates a device and registers it with sysfs
2517 * @class: pointer to the struct class that this device should be registered to
2518 * @parent: pointer to the parent struct device of this new device, if any
2519 * @devt: the dev_t for the char device to be added
2520 * @drvdata: the data to be added to the device for callbacks
2521 * @groups: NULL-terminated list of attribute groups to be created
2522 * @fmt: string for the device's name
2523 *
2524 * This function can be used by char device classes. A struct device
2525 * will be created in sysfs, registered to the specified class.
2526 * Additional attributes specified in the groups parameter will also
2527 * be created automatically.
2528 *
2529 * A "dev" file will be created, showing the dev_t for the device, if
2530 * the dev_t is not 0,0.
2531 * If a pointer to a parent struct device is passed in, the newly created
2532 * struct device will be a child of that device in sysfs.
2533 * The pointer to the struct device will be returned from the call.
2534 * Any further sysfs files that might be required can be created using this
2535 * pointer.
2536 *
2537 * Returns &struct device pointer on success, or ERR_PTR() on error.
2538 *
2539 * Note: the struct class passed to this function must have previously
2540 * been created with a call to class_create().
2541 */
2542struct device *device_create_with_groups(struct class *class,
2543 struct device *parent, dev_t devt,
2544 void *drvdata,
2545 const struct attribute_group **groups,
2546 const char *fmt, ...)
2547{
2548 va_list vargs;
2549 struct device *dev;
2550
2551 va_start(vargs, fmt);
2552 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2553 fmt, vargs);
2554 va_end(vargs);
2555 return dev;
2556}
2557EXPORT_SYMBOL_GPL(device_create_with_groups);
2558
9f3b795a 2559static int __match_devt(struct device *dev, const void *data)
23681e47 2560{
9f3b795a 2561 const dev_t *devt = data;
23681e47 2562
cd35449b 2563 return dev->devt == *devt;
775b64d2
RW
2564}
2565
2566/**
2567 * device_destroy - removes a device that was created with device_create()
2568 * @class: pointer to the struct class that this device was registered with
2569 * @devt: the dev_t of the device that was previously registered
2570 *
2571 * This call unregisters and cleans up a device that was created with a
2572 * call to device_create().
2573 */
2574void device_destroy(struct class *class, dev_t devt)
2575{
2576 struct device *dev;
23681e47 2577
695794ae 2578 dev = class_find_device(class, NULL, &devt, __match_devt);
cd35449b
DY
2579 if (dev) {
2580 put_device(dev);
23681e47 2581 device_unregister(dev);
cd35449b 2582 }
23681e47
GKH
2583}
2584EXPORT_SYMBOL_GPL(device_destroy);
a2de48ca
GKH
2585
2586/**
2587 * device_rename - renames a device
2588 * @dev: the pointer to the struct device to be renamed
2589 * @new_name: the new name of the device
030c1d2b
EB
2590 *
2591 * It is the responsibility of the caller to provide mutual
2592 * exclusion between two different calls of device_rename
2593 * on the same device to ensure that new_name is valid and
2594 * won't conflict with other devices.
c6c0ac66 2595 *
a5462516
TT
2596 * Note: Don't call this function. Currently, the networking layer calls this
2597 * function, but that will change. The following text from Kay Sievers offers
2598 * some insight:
2599 *
2600 * Renaming devices is racy at many levels, symlinks and other stuff are not
2601 * replaced atomically, and you get a "move" uevent, but it's not easy to
2602 * connect the event to the old and new device. Device nodes are not renamed at
2603 * all, there isn't even support for that in the kernel now.
2604 *
2605 * In the meantime, during renaming, your target name might be taken by another
2606 * driver, creating conflicts. Or the old name is taken directly after you
2607 * renamed it -- then you get events for the same DEVPATH, before you even see
2608 * the "move" event. It's just a mess, and nothing new should ever rely on
2609 * kernel device renaming. Besides that, it's not even implemented now for
2610 * other things than (driver-core wise very simple) network devices.
2611 *
2612 * We are currently about to change network renaming in udev to completely
2613 * disallow renaming of devices in the same namespace as the kernel uses,
2614 * because we can't solve the problems properly, that arise with swapping names
2615 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2616 * be allowed to some other name than eth[0-9]*, for the aforementioned
2617 * reasons.
2618 *
2619 * Make up a "real" name in the driver before you register anything, or add
2620 * some other attributes for userspace to find the device, or use udev to add
2621 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2622 * don't even want to get into that and try to implement the missing pieces in
2623 * the core. We really have other pieces to fix in the driver core mess. :)
a2de48ca 2624 */
6937e8f8 2625int device_rename(struct device *dev, const char *new_name)
a2de48ca 2626{
4b30ee58 2627 struct kobject *kobj = &dev->kobj;
2ee97caf 2628 char *old_device_name = NULL;
a2de48ca
GKH
2629 int error;
2630
2631 dev = get_device(dev);
2632 if (!dev)
2633 return -EINVAL;
2634
69df7533 2635 dev_dbg(dev, "renaming to %s\n", new_name);
a2de48ca 2636
1fa5ae85 2637 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2ee97caf
CH
2638 if (!old_device_name) {
2639 error = -ENOMEM;
2640 goto out;
a2de48ca 2641 }
a2de48ca 2642
f349cf34 2643 if (dev->class) {
4b30ee58
TH
2644 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2645 kobj, old_device_name,
2646 new_name, kobject_namespace(kobj));
f349cf34
EB
2647 if (error)
2648 goto out;
2649 }
39aba963 2650
4b30ee58 2651 error = kobject_rename(kobj, new_name);
1fa5ae85 2652 if (error)
2ee97caf 2653 goto out;
a2de48ca 2654
2ee97caf 2655out:
a2de48ca
GKH
2656 put_device(dev);
2657
2ee97caf 2658 kfree(old_device_name);
a2de48ca
GKH
2659
2660 return error;
2661}
a2807dbc 2662EXPORT_SYMBOL_GPL(device_rename);
8a82472f
CH
2663
2664static int device_move_class_links(struct device *dev,
2665 struct device *old_parent,
2666 struct device *new_parent)
2667{
f7f3461d 2668 int error = 0;
8a82472f 2669
f7f3461d
GKH
2670 if (old_parent)
2671 sysfs_remove_link(&dev->kobj, "device");
2672 if (new_parent)
2673 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2674 "device");
2675 return error;
8a82472f
CH
2676}
2677
2678/**
2679 * device_move - moves a device to a new parent
2680 * @dev: the pointer to the struct device to be moved
c744aeae 2681 * @new_parent: the new parent of the device (can by NULL)
ffa6a705 2682 * @dpm_order: how to reorder the dpm_list
8a82472f 2683 */
ffa6a705
CH
2684int device_move(struct device *dev, struct device *new_parent,
2685 enum dpm_order dpm_order)
8a82472f
CH
2686{
2687 int error;
2688 struct device *old_parent;
c744aeae 2689 struct kobject *new_parent_kobj;
8a82472f
CH
2690
2691 dev = get_device(dev);
2692 if (!dev)
2693 return -EINVAL;
2694
ffa6a705 2695 device_pm_lock();
8a82472f 2696 new_parent = get_device(new_parent);
4a3ad20c 2697 new_parent_kobj = get_device_parent(dev, new_parent);
63b6971a 2698
1e0b2cf9
KS
2699 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2700 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
c744aeae 2701 error = kobject_move(&dev->kobj, new_parent_kobj);
8a82472f 2702 if (error) {
63b6971a 2703 cleanup_glue_dir(dev, new_parent_kobj);
8a82472f
CH
2704 put_device(new_parent);
2705 goto out;
2706 }
2707 old_parent = dev->parent;
2708 dev->parent = new_parent;
2709 if (old_parent)
f791b8c8 2710 klist_remove(&dev->p->knode_parent);
0d358f22 2711 if (new_parent) {
f791b8c8
GKH
2712 klist_add_tail(&dev->p->knode_parent,
2713 &new_parent->p->klist_children);
0d358f22
YL
2714 set_dev_node(dev, dev_to_node(new_parent));
2715 }
2716
bdd4034d
RV
2717 if (dev->class) {
2718 error = device_move_class_links(dev, old_parent, new_parent);
2719 if (error) {
2720 /* We ignore errors on cleanup since we're hosed anyway... */
2721 device_move_class_links(dev, new_parent, old_parent);
2722 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2723 if (new_parent)
2724 klist_remove(&dev->p->knode_parent);
2725 dev->parent = old_parent;
2726 if (old_parent) {
2727 klist_add_tail(&dev->p->knode_parent,
2728 &old_parent->p->klist_children);
2729 set_dev_node(dev, dev_to_node(old_parent));
2730 }
0d358f22 2731 }
bdd4034d
RV
2732 cleanup_glue_dir(dev, new_parent_kobj);
2733 put_device(new_parent);
2734 goto out;
8a82472f 2735 }
8a82472f 2736 }
ffa6a705
CH
2737 switch (dpm_order) {
2738 case DPM_ORDER_NONE:
2739 break;
2740 case DPM_ORDER_DEV_AFTER_PARENT:
2741 device_pm_move_after(dev, new_parent);
52cdbdd4 2742 devices_kset_move_after(dev, new_parent);
ffa6a705
CH
2743 break;
2744 case DPM_ORDER_PARENT_BEFORE_DEV:
2745 device_pm_move_before(new_parent, dev);
52cdbdd4 2746 devices_kset_move_before(new_parent, dev);
ffa6a705
CH
2747 break;
2748 case DPM_ORDER_DEV_LAST:
2749 device_pm_move_last(dev);
52cdbdd4 2750 devices_kset_move_last(dev);
ffa6a705
CH
2751 break;
2752 }
bdd4034d 2753
8a82472f
CH
2754 put_device(old_parent);
2755out:
ffa6a705 2756 device_pm_unlock();
8a82472f
CH
2757 put_device(dev);
2758 return error;
2759}
8a82472f 2760EXPORT_SYMBOL_GPL(device_move);
37b0c020
GKH
2761
2762/**
2763 * device_shutdown - call ->shutdown() on each device to shutdown.
2764 */
2765void device_shutdown(void)
2766{
f123db8e 2767 struct device *dev, *parent;
6245838f
HD
2768
2769 spin_lock(&devices_kset->list_lock);
2770 /*
2771 * Walk the devices list backward, shutting down each in turn.
2772 * Beware that device unplug events may also start pulling
2773 * devices offline, even as the system is shutting down.
2774 */
2775 while (!list_empty(&devices_kset->list)) {
2776 dev = list_entry(devices_kset->list.prev, struct device,
2777 kobj.entry);
d1c6c030
ML
2778
2779 /*
2780 * hold reference count of device's parent to
2781 * prevent it from being freed because parent's
2782 * lock is to be held
2783 */
f123db8e 2784 parent = get_device(dev->parent);
6245838f
HD
2785 get_device(dev);
2786 /*
2787 * Make sure the device is off the kset list, in the
2788 * event that dev->*->shutdown() doesn't remove it.
2789 */
2790 list_del_init(&dev->kobj.entry);
2791 spin_unlock(&devices_kset->list_lock);
fe6b91f4 2792
d1c6c030 2793 /* hold lock to avoid race with probe/release */
f123db8e
BL
2794 if (parent)
2795 device_lock(parent);
d1c6c030
ML
2796 device_lock(dev);
2797
fe6b91f4
AS
2798 /* Don't allow any more runtime suspends */
2799 pm_runtime_get_noresume(dev);
2800 pm_runtime_barrier(dev);
37b0c020 2801
7521621e 2802 if (dev->class && dev->class->shutdown_pre) {
f77af151 2803 if (initcall_debug)
7521621e
MS
2804 dev_info(dev, "shutdown_pre\n");
2805 dev->class->shutdown_pre(dev);
2806 }
2807 if (dev->bus && dev->bus->shutdown) {
0246c4fa
SL
2808 if (initcall_debug)
2809 dev_info(dev, "shutdown\n");
37b0c020
GKH
2810 dev->bus->shutdown(dev);
2811 } else if (dev->driver && dev->driver->shutdown) {
0246c4fa
SL
2812 if (initcall_debug)
2813 dev_info(dev, "shutdown\n");
37b0c020
GKH
2814 dev->driver->shutdown(dev);
2815 }
d1c6c030
ML
2816
2817 device_unlock(dev);
f123db8e
BL
2818 if (parent)
2819 device_unlock(parent);
d1c6c030 2820
6245838f 2821 put_device(dev);
f123db8e 2822 put_device(parent);
6245838f
HD
2823
2824 spin_lock(&devices_kset->list_lock);
37b0c020 2825 }
6245838f 2826 spin_unlock(&devices_kset->list_lock);
37b0c020 2827}
99bcf217
JP
2828
2829/*
2830 * Device logging functions
2831 */
2832
2833#ifdef CONFIG_PRINTK
666f355f
JP
2834static int
2835create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
99bcf217 2836{
c4e00daa 2837 const char *subsys;
798efc60 2838 size_t pos = 0;
99bcf217 2839
c4e00daa
KS
2840 if (dev->class)
2841 subsys = dev->class->name;
2842 else if (dev->bus)
2843 subsys = dev->bus->name;
2844 else
798efc60 2845 return 0;
c4e00daa 2846
798efc60 2847 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
655e5b7c
BH
2848 if (pos >= hdrlen)
2849 goto overflow;
c4e00daa
KS
2850
2851 /*
2852 * Add device identifier DEVICE=:
2853 * b12:8 block dev_t
2854 * c127:3 char dev_t
2855 * n8 netdev ifindex
2856 * +sound:card0 subsystem:devname
2857 */
2858 if (MAJOR(dev->devt)) {
2859 char c;
2860
2861 if (strcmp(subsys, "block") == 0)
2862 c = 'b';
2863 else
2864 c = 'c';
798efc60
JP
2865 pos++;
2866 pos += snprintf(hdr + pos, hdrlen - pos,
2867 "DEVICE=%c%u:%u",
2868 c, MAJOR(dev->devt), MINOR(dev->devt));
c4e00daa
KS
2869 } else if (strcmp(subsys, "net") == 0) {
2870 struct net_device *net = to_net_dev(dev);
2871
798efc60
JP
2872 pos++;
2873 pos += snprintf(hdr + pos, hdrlen - pos,
2874 "DEVICE=n%u", net->ifindex);
c4e00daa 2875 } else {
798efc60
JP
2876 pos++;
2877 pos += snprintf(hdr + pos, hdrlen - pos,
2878 "DEVICE=+%s:%s", subsys, dev_name(dev));
c4e00daa 2879 }
af7f2158 2880
655e5b7c
BH
2881 if (pos >= hdrlen)
2882 goto overflow;
2883
798efc60 2884 return pos;
655e5b7c
BH
2885
2886overflow:
2887 dev_WARN(dev, "device/subsystem name too long");
2888 return 0;
798efc60 2889}
798efc60 2890
05e4e5b8
JP
2891int dev_vprintk_emit(int level, const struct device *dev,
2892 const char *fmt, va_list args)
2893{
2894 char hdr[128];
2895 size_t hdrlen;
2896
2897 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2898
2899 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2900}
2901EXPORT_SYMBOL(dev_vprintk_emit);
2902
2903int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2904{
2905 va_list args;
2906 int r;
2907
2908 va_start(args, fmt);
2909
2910 r = dev_vprintk_emit(level, dev, fmt, args);
2911
2912 va_end(args);
2913
2914 return r;
2915}
2916EXPORT_SYMBOL(dev_printk_emit);
2917
d1f1052c 2918static void __dev_printk(const char *level, const struct device *dev,
798efc60
JP
2919 struct va_format *vaf)
2920{
d1f1052c
JP
2921 if (dev)
2922 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2923 dev_driver_string(dev), dev_name(dev), vaf);
2924 else
2925 printk("%s(NULL device *): %pV", level, vaf);
99bcf217
JP
2926}
2927
d1f1052c
JP
2928void dev_printk(const char *level, const struct device *dev,
2929 const char *fmt, ...)
99bcf217
JP
2930{
2931 struct va_format vaf;
2932 va_list args;
99bcf217
JP
2933
2934 va_start(args, fmt);
2935
2936 vaf.fmt = fmt;
2937 vaf.va = &args;
2938
d1f1052c 2939 __dev_printk(level, dev, &vaf);
798efc60 2940
99bcf217 2941 va_end(args);
99bcf217
JP
2942}
2943EXPORT_SYMBOL(dev_printk);
2944
2945#define define_dev_printk_level(func, kern_level) \
d1f1052c 2946void func(const struct device *dev, const char *fmt, ...) \
99bcf217
JP
2947{ \
2948 struct va_format vaf; \
2949 va_list args; \
99bcf217
JP
2950 \
2951 va_start(args, fmt); \
2952 \
2953 vaf.fmt = fmt; \
2954 vaf.va = &args; \
2955 \
d1f1052c 2956 __dev_printk(kern_level, dev, &vaf); \
798efc60 2957 \
99bcf217 2958 va_end(args); \
99bcf217
JP
2959} \
2960EXPORT_SYMBOL(func);
2961
2962define_dev_printk_level(dev_emerg, KERN_EMERG);
2963define_dev_printk_level(dev_alert, KERN_ALERT);
2964define_dev_printk_level(dev_crit, KERN_CRIT);
2965define_dev_printk_level(dev_err, KERN_ERR);
2966define_dev_printk_level(dev_warn, KERN_WARNING);
2967define_dev_printk_level(dev_notice, KERN_NOTICE);
2968define_dev_printk_level(_dev_info, KERN_INFO);
2969
2970#endif
97badf87
RW
2971
2972static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2973{
2974 return fwnode && !IS_ERR(fwnode->secondary);
2975}
2976
2977/**
2978 * set_primary_fwnode - Change the primary firmware node of a given device.
2979 * @dev: Device to handle.
2980 * @fwnode: New primary firmware node of the device.
2981 *
2982 * Set the device's firmware node pointer to @fwnode, but if a secondary
2983 * firmware node of the device is present, preserve it.
2984 */
2985void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2986{
2987 if (fwnode) {
2988 struct fwnode_handle *fn = dev->fwnode;
2989
2990 if (fwnode_is_primary(fn))
2991 fn = fn->secondary;
2992
55f89a8a
MW
2993 if (fn) {
2994 WARN_ON(fwnode->secondary);
2995 fwnode->secondary = fn;
2996 }
97badf87
RW
2997 dev->fwnode = fwnode;
2998 } else {
2999 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3000 dev->fwnode->secondary : NULL;
3001 }
3002}
3003EXPORT_SYMBOL_GPL(set_primary_fwnode);
3004
3005/**
3006 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3007 * @dev: Device to handle.
3008 * @fwnode: New secondary firmware node of the device.
3009 *
3010 * If a primary firmware node of the device is present, set its secondary
3011 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3012 * @fwnode.
3013 */
3014void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3015{
3016 if (fwnode)
3017 fwnode->secondary = ERR_PTR(-ENODEV);
3018
3019 if (fwnode_is_primary(dev->fwnode))
3020 dev->fwnode->secondary = fwnode;
3021 else
3022 dev->fwnode = fwnode;
3023}
4e75e1d7
JH
3024
3025/**
3026 * device_set_of_node_from_dev - reuse device-tree node of another device
3027 * @dev: device whose device-tree node is being set
3028 * @dev2: device whose device-tree node is being reused
3029 *
3030 * Takes another reference to the new device-tree node after first dropping
3031 * any reference held to the old node.
3032 */
3033void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3034{
3035 of_node_put(dev->of_node);
3036 dev->of_node = of_node_get(dev2->of_node);
3037 dev->of_node_reused = true;
3038}
3039EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);