w1_family.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
  4. */
  5. #include <linux/spinlock.h>
  6. #include <linux/list.h>
  7. #include <linux/sched/signal.h>
  8. #include <linux/delay.h>
  9. #include <linux/export.h>
  10. #include "w1_internal.h"
  11. DEFINE_SPINLOCK(w1_flock);
  12. static LIST_HEAD(w1_families);
  13. /**
  14. * w1_register_family() - register a device family driver
  15. * @newf: family to register
  16. */
  17. int w1_register_family(struct w1_family *newf)
  18. {
  19. struct list_head *ent, *n;
  20. struct w1_family *f;
  21. int ret = 0;
  22. spin_lock(&w1_flock);
  23. list_for_each_safe(ent, n, &w1_families) {
  24. f = list_entry(ent, struct w1_family, family_entry);
  25. if (f->fid == newf->fid) {
  26. ret = -EEXIST;
  27. break;
  28. }
  29. }
  30. if (!ret) {
  31. atomic_set(&newf->refcnt, 0);
  32. list_add_tail(&newf->family_entry, &w1_families);
  33. }
  34. spin_unlock(&w1_flock);
  35. /* check default devices against the new set of drivers */
  36. w1_reconnect_slaves(newf, 1);
  37. return ret;
  38. }
  39. EXPORT_SYMBOL(w1_register_family);
  40. /**
  41. * w1_unregister_family() - unregister a device family driver
  42. * @fent: family to unregister
  43. */
  44. void w1_unregister_family(struct w1_family *fent)
  45. {
  46. struct list_head *ent, *n;
  47. struct w1_family *f;
  48. spin_lock(&w1_flock);
  49. list_for_each_safe(ent, n, &w1_families) {
  50. f = list_entry(ent, struct w1_family, family_entry);
  51. if (f->fid == fent->fid) {
  52. list_del(&fent->family_entry);
  53. break;
  54. }
  55. }
  56. spin_unlock(&w1_flock);
  57. /* deatch devices using this family code */
  58. w1_reconnect_slaves(fent, 0);
  59. while (atomic_read(&fent->refcnt)) {
  60. pr_info("Waiting for family %u to become free: refcnt=%d.\n",
  61. fent->fid, atomic_read(&fent->refcnt));
  62. if (msleep_interruptible(1000))
  63. flush_signals(current);
  64. }
  65. }
  66. EXPORT_SYMBOL(w1_unregister_family);
  67. /*
  68. * Should be called under w1_flock held.
  69. */
  70. struct w1_family * w1_family_registered(u8 fid)
  71. {
  72. struct list_head *ent, *n;
  73. struct w1_family *f = NULL;
  74. int ret = 0;
  75. list_for_each_safe(ent, n, &w1_families) {
  76. f = list_entry(ent, struct w1_family, family_entry);
  77. if (f->fid == fid) {
  78. ret = 1;
  79. break;
  80. }
  81. }
  82. return (ret) ? f : NULL;
  83. }
  84. static void __w1_family_put(struct w1_family *f)
  85. {
  86. atomic_dec(&f->refcnt);
  87. }
  88. void w1_family_put(struct w1_family *f)
  89. {
  90. spin_lock(&w1_flock);
  91. __w1_family_put(f);
  92. spin_unlock(&w1_flock);
  93. }
  94. #if 0
  95. void w1_family_get(struct w1_family *f)
  96. {
  97. spin_lock(&w1_flock);
  98. __w1_family_get(f);
  99. spin_unlock(&w1_flock);
  100. }
  101. #endif /* 0 */
  102. void __w1_family_get(struct w1_family *f)
  103. {
  104. smp_mb__before_atomic();
  105. atomic_inc(&f->refcnt);
  106. smp_mb__after_atomic();
  107. }