w1_family.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * w1_family.c
  3. *
  4. * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  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/delay.h>
  24. #include "w1_family.h"
  25. DEFINE_SPINLOCK(w1_flock);
  26. static LIST_HEAD(w1_families);
  27. extern void w1_reconnect_slaves(struct w1_family *f);
  28. static int w1_check_family(struct w1_family *f)
  29. {
  30. if (!f->fops->rname || !f->fops->rbin)
  31. return -EINVAL;
  32. return 0;
  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. if (w1_check_family(newf))
  40. return -EINVAL;
  41. spin_lock(&w1_flock);
  42. list_for_each_safe(ent, n, &w1_families) {
  43. f = list_entry(ent, struct w1_family, family_entry);
  44. if (f->fid == newf->fid) {
  45. ret = -EEXIST;
  46. break;
  47. }
  48. }
  49. if (!ret) {
  50. atomic_set(&newf->refcnt, 0);
  51. newf->need_exit = 0;
  52. list_add_tail(&newf->family_entry, &w1_families);
  53. }
  54. spin_unlock(&w1_flock);
  55. w1_reconnect_slaves(newf);
  56. return ret;
  57. }
  58. void w1_unregister_family(struct w1_family *fent)
  59. {
  60. struct list_head *ent, *n;
  61. struct w1_family *f;
  62. spin_lock(&w1_flock);
  63. list_for_each_safe(ent, n, &w1_families) {
  64. f = list_entry(ent, struct w1_family, family_entry);
  65. if (f->fid == fent->fid) {
  66. list_del(&fent->family_entry);
  67. break;
  68. }
  69. }
  70. fent->need_exit = 1;
  71. spin_unlock(&w1_flock);
  72. while (atomic_read(&fent->refcnt)) {
  73. printk(KERN_INFO "Waiting for family %u to become free: refcnt=%d.\n",
  74. fent->fid, atomic_read(&fent->refcnt));
  75. if (msleep_interruptible(1000))
  76. flush_signals(current);
  77. }
  78. }
  79. /*
  80. * Should be called under w1_flock held.
  81. */
  82. struct w1_family * w1_family_registered(u8 fid)
  83. {
  84. struct list_head *ent, *n;
  85. struct w1_family *f = NULL;
  86. int ret = 0;
  87. list_for_each_safe(ent, n, &w1_families) {
  88. f = list_entry(ent, struct w1_family, family_entry);
  89. if (f->fid == fid) {
  90. ret = 1;
  91. break;
  92. }
  93. }
  94. return (ret) ? f : NULL;
  95. }
  96. void w1_family_put(struct w1_family *f)
  97. {
  98. spin_lock(&w1_flock);
  99. __w1_family_put(f);
  100. spin_unlock(&w1_flock);
  101. }
  102. void __w1_family_put(struct w1_family *f)
  103. {
  104. if (atomic_dec_and_test(&f->refcnt))
  105. f->need_exit = 1;
  106. }
  107. void w1_family_get(struct w1_family *f)
  108. {
  109. spin_lock(&w1_flock);
  110. __w1_family_get(f);
  111. spin_unlock(&w1_flock);
  112. }
  113. void __w1_family_get(struct w1_family *f)
  114. {
  115. smp_mb__before_atomic_inc();
  116. atomic_inc(&f->refcnt);
  117. smp_mb__after_atomic_inc();
  118. }
  119. EXPORT_SYMBOL(w1_family_get);
  120. EXPORT_SYMBOL(w1_family_put);
  121. EXPORT_SYMBOL(w1_family_registered);
  122. EXPORT_SYMBOL(w1_unregister_family);
  123. EXPORT_SYMBOL(w1_register_family);