w1_family.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. int w1_register_family(struct w1_family *newf)
  31. {
  32. struct list_head *ent, *n;
  33. struct w1_family *f;
  34. int ret = 0;
  35. spin_lock(&w1_flock);
  36. list_for_each_safe(ent, n, &w1_families) {
  37. f = list_entry(ent, struct w1_family, family_entry);
  38. if (f->fid == newf->fid) {
  39. ret = -EEXIST;
  40. break;
  41. }
  42. }
  43. if (!ret) {
  44. atomic_set(&newf->refcnt, 0);
  45. list_add_tail(&newf->family_entry, &w1_families);
  46. }
  47. spin_unlock(&w1_flock);
  48. /* check default devices against the new set of drivers */
  49. w1_reconnect_slaves(newf, 1);
  50. return ret;
  51. }
  52. void w1_unregister_family(struct w1_family *fent)
  53. {
  54. struct list_head *ent, *n;
  55. struct w1_family *f;
  56. spin_lock(&w1_flock);
  57. list_for_each_safe(ent, n, &w1_families) {
  58. f = list_entry(ent, struct w1_family, family_entry);
  59. if (f->fid == fent->fid) {
  60. list_del(&fent->family_entry);
  61. break;
  62. }
  63. }
  64. spin_unlock(&w1_flock);
  65. /* deatch devices using this family code */
  66. w1_reconnect_slaves(fent, 0);
  67. while (atomic_read(&fent->refcnt)) {
  68. printk(KERN_INFO "Waiting for family %u to become free: refcnt=%d.\n",
  69. fent->fid, atomic_read(&fent->refcnt));
  70. if (msleep_interruptible(1000))
  71. flush_signals(current);
  72. }
  73. }
  74. /*
  75. * Should be called under w1_flock held.
  76. */
  77. struct w1_family * w1_family_registered(u8 fid)
  78. {
  79. struct list_head *ent, *n;
  80. struct w1_family *f = NULL;
  81. int ret = 0;
  82. list_for_each_safe(ent, n, &w1_families) {
  83. f = list_entry(ent, struct w1_family, family_entry);
  84. if (f->fid == fid) {
  85. ret = 1;
  86. break;
  87. }
  88. }
  89. return (ret) ? f : NULL;
  90. }
  91. static void __w1_family_put(struct w1_family *f)
  92. {
  93. atomic_dec(&f->refcnt);
  94. }
  95. void w1_family_put(struct w1_family *f)
  96. {
  97. spin_lock(&w1_flock);
  98. __w1_family_put(f);
  99. spin_unlock(&w1_flock);
  100. }
  101. #if 0
  102. void w1_family_get(struct w1_family *f)
  103. {
  104. spin_lock(&w1_flock);
  105. __w1_family_get(f);
  106. spin_unlock(&w1_flock);
  107. }
  108. #endif /* 0 */
  109. void __w1_family_get(struct w1_family *f)
  110. {
  111. smp_mb__before_atomic_inc();
  112. atomic_inc(&f->refcnt);
  113. smp_mb__after_atomic_inc();
  114. }
  115. EXPORT_SYMBOL(w1_unregister_family);
  116. EXPORT_SYMBOL(w1_register_family);