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