w1_family.c 3.0 KB

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