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