w1_family.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_family.h"
  20. #include "w1.h"
  21. DEFINE_SPINLOCK(w1_flock);
  22. static LIST_HEAD(w1_families);
  23. /**
  24. * w1_register_family() - register a device family driver
  25. * @newf: family to register
  26. */
  27. int w1_register_family(struct w1_family *newf)
  28. {
  29. struct list_head *ent, *n;
  30. struct w1_family *f;
  31. int ret = 0;
  32. spin_lock(&w1_flock);
  33. list_for_each_safe(ent, n, &w1_families) {
  34. f = list_entry(ent, struct w1_family, family_entry);
  35. if (f->fid == newf->fid) {
  36. ret = -EEXIST;
  37. break;
  38. }
  39. }
  40. if (!ret) {
  41. atomic_set(&newf->refcnt, 0);
  42. list_add_tail(&newf->family_entry, &w1_families);
  43. }
  44. spin_unlock(&w1_flock);
  45. /* check default devices against the new set of drivers */
  46. w1_reconnect_slaves(newf, 1);
  47. return ret;
  48. }
  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. /*
  76. * Should be called under w1_flock held.
  77. */
  78. struct w1_family * w1_family_registered(u8 fid)
  79. {
  80. struct list_head *ent, *n;
  81. struct w1_family *f = NULL;
  82. int ret = 0;
  83. list_for_each_safe(ent, n, &w1_families) {
  84. f = list_entry(ent, struct w1_family, family_entry);
  85. if (f->fid == fid) {
  86. ret = 1;
  87. break;
  88. }
  89. }
  90. return (ret) ? f : NULL;
  91. }
  92. static void __w1_family_put(struct w1_family *f)
  93. {
  94. atomic_dec(&f->refcnt);
  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. #if 0
  103. void w1_family_get(struct w1_family *f)
  104. {
  105. spin_lock(&w1_flock);
  106. __w1_family_get(f);
  107. spin_unlock(&w1_flock);
  108. }
  109. #endif /* 0 */
  110. void __w1_family_get(struct w1_family *f)
  111. {
  112. smp_mb__before_atomic();
  113. atomic_inc(&f->refcnt);
  114. smp_mb__after_atomic();
  115. }
  116. EXPORT_SYMBOL(w1_unregister_family);
  117. EXPORT_SYMBOL(w1_register_family);