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