w1_family.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * w1_family.c
  3. *
  4. * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/spinlock.h>
  22. #include <linux/list.h>
  23. #include <linux/sched.h> /* schedule_timeout() */
  24. #include <linux/delay.h>
  25. #include <linux/export.h>
  26. #include "w1_family.h"
  27. #include "w1.h"
  28. DEFINE_SPINLOCK(w1_flock);
  29. static LIST_HEAD(w1_families);
  30. /**
  31. * w1_register_family() - register a device family driver
  32. * @newf: family to register
  33. */
  34. int w1_register_family(struct w1_family *newf)
  35. {
  36. struct list_head *ent, *n;
  37. struct w1_family *f;
  38. int ret = 0;
  39. spin_lock(&w1_flock);
  40. list_for_each_safe(ent, n, &w1_families) {
  41. f = list_entry(ent, struct w1_family, family_entry);
  42. if (f->fid == newf->fid) {
  43. ret = -EEXIST;
  44. break;
  45. }
  46. }
  47. if (!ret) {
  48. atomic_set(&newf->refcnt, 0);
  49. list_add_tail(&newf->family_entry, &w1_families);
  50. }
  51. spin_unlock(&w1_flock);
  52. /* check default devices against the new set of drivers */
  53. w1_reconnect_slaves(newf, 1);
  54. return ret;
  55. }
  56. /**
  57. * w1_unregister_family() - unregister a device family driver
  58. * @fent: family to unregister
  59. */
  60. void w1_unregister_family(struct w1_family *fent)
  61. {
  62. struct list_head *ent, *n;
  63. struct w1_family *f;
  64. spin_lock(&w1_flock);
  65. list_for_each_safe(ent, n, &w1_families) {
  66. f = list_entry(ent, struct w1_family, family_entry);
  67. if (f->fid == fent->fid) {
  68. list_del(&fent->family_entry);
  69. break;
  70. }
  71. }
  72. spin_unlock(&w1_flock);
  73. /* deatch devices using this family code */
  74. w1_reconnect_slaves(fent, 0);
  75. while (atomic_read(&fent->refcnt)) {
  76. pr_info("Waiting for family %u to become free: refcnt=%d.\n",
  77. fent->fid, atomic_read(&fent->refcnt));
  78. if (msleep_interruptible(1000))
  79. flush_signals(current);
  80. }
  81. }
  82. /*
  83. * Should be called under w1_flock held.
  84. */
  85. struct w1_family * w1_family_registered(u8 fid)
  86. {
  87. struct list_head *ent, *n;
  88. struct w1_family *f = NULL;
  89. int ret = 0;
  90. list_for_each_safe(ent, n, &w1_families) {
  91. f = list_entry(ent, struct w1_family, family_entry);
  92. if (f->fid == fid) {
  93. ret = 1;
  94. break;
  95. }
  96. }
  97. return (ret) ? f : NULL;
  98. }
  99. static void __w1_family_put(struct w1_family *f)
  100. {
  101. atomic_dec(&f->refcnt);
  102. }
  103. void w1_family_put(struct w1_family *f)
  104. {
  105. spin_lock(&w1_flock);
  106. __w1_family_put(f);
  107. spin_unlock(&w1_flock);
  108. }
  109. #if 0
  110. void w1_family_get(struct w1_family *f)
  111. {
  112. spin_lock(&w1_flock);
  113. __w1_family_get(f);
  114. spin_unlock(&w1_flock);
  115. }
  116. #endif /* 0 */
  117. void __w1_family_get(struct w1_family *f)
  118. {
  119. smp_mb__before_atomic();
  120. atomic_inc(&f->refcnt);
  121. smp_mb__after_atomic();
  122. }
  123. EXPORT_SYMBOL(w1_unregister_family);
  124. EXPORT_SYMBOL(w1_register_family);