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