dahdi_dynamic_loc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Dynamic Span Interface for DAHDI (Local Interface)
  3. *
  4. * Written by Nicolas Bougues <nbougues@axialys.net>
  5. *
  6. * Copyright (C) 2004, Axialys Interactive
  7. *
  8. * All rights reserved.
  9. *
  10. * Note : a DAHDI timing source must exist prior to loading this driver
  11. *
  12. * Address syntax :
  13. * <key>:<id>[:<monitor id>]
  14. *
  15. * As of now, keys and ids are single digit only
  16. *
  17. * One span may have up to one "normal" peer, and one "monitor" peer
  18. *
  19. * Example :
  20. *
  21. * Say you have two spans cross connected, a third one monitoring RX on the
  22. * first one, a fourth one monitoring RX on the second one
  23. *
  24. * 1:0
  25. * 1:1
  26. * 1:2:0
  27. * 1:3:1
  28. *
  29. * Contrary to TDMoE, no frame loss can occur.
  30. *
  31. * See bug #2021 for more details
  32. *
  33. */
  34. /*
  35. * See http://www.asterisk.org for more information about
  36. * the Asterisk project. Please do not directly contact
  37. * any of the maintainers of this project for assistance;
  38. * the project provides a web site, mailing lists and IRC
  39. * channels for your use.
  40. *
  41. * This program is free software, distributed under the terms of
  42. * the GNU General Public License Version 2 as published by the
  43. * Free Software Foundation. See the LICENSE file included with
  44. * this program for more details.
  45. */
  46. #include <linux/kernel.h>
  47. #include <linux/errno.h>
  48. #include <linux/module.h>
  49. #include <linux/init.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/slab.h>
  52. #include <linux/kmod.h>
  53. #include <linux/netdevice.h>
  54. #include <linux/notifier.h>
  55. #include <dahdi/kernel.h>
  56. /**
  57. * struct dahdi_dynamic_loc - For local dynamic spans
  58. * @monitor_rx_peer: Indicates the peer span that monitors this span.
  59. * @peer: Indicates the rw peer for this span.
  60. *
  61. */
  62. struct dahdi_dynamic_local {
  63. unsigned short key;
  64. unsigned short id;
  65. struct dahdi_dynamic_local *monitor_rx_peer;
  66. struct dahdi_dynamic_local *peer;
  67. struct dahdi_span *span;
  68. struct list_head node;
  69. };
  70. static DEFINE_SPINLOCK(local_lock);
  71. static LIST_HEAD(dynamic_local_list);
  72. static void
  73. dahdi_dynamic_local_transmit(struct dahdi_dynamic *dyn, u8 *msg, size_t msglen)
  74. {
  75. struct dahdi_dynamic_local *d;
  76. unsigned long flags;
  77. spin_lock_irqsave(&local_lock, flags);
  78. d = dyn->pvt;
  79. if (d && d->peer && d->peer->span) {
  80. if (test_bit(DAHDI_FLAGBIT_REGISTERED, &d->peer->span->flags))
  81. dahdi_dynamic_receive(d->peer->span, msg, msglen);
  82. }
  83. if (d && d->monitor_rx_peer && d->monitor_rx_peer->span) {
  84. if (test_bit(DAHDI_FLAGBIT_REGISTERED,
  85. &d->monitor_rx_peer->span->flags)) {
  86. dahdi_dynamic_receive(d->monitor_rx_peer->span,
  87. msg, msglen);
  88. }
  89. }
  90. spin_unlock_irqrestore(&local_lock, flags);
  91. }
  92. static int digit2int(char d)
  93. {
  94. switch(d) {
  95. case 'F':
  96. case 'E':
  97. case 'D':
  98. case 'C':
  99. case 'B':
  100. case 'A':
  101. return d - 'A' + 10;
  102. case 'f':
  103. case 'e':
  104. case 'd':
  105. case 'c':
  106. case 'b':
  107. case 'a':
  108. return d - 'a' + 10;
  109. case '9':
  110. case '8':
  111. case '7':
  112. case '6':
  113. case '5':
  114. case '4':
  115. case '3':
  116. case '2':
  117. case '1':
  118. case '0':
  119. return d - '0';
  120. }
  121. return -1;
  122. }
  123. static void dahdi_dynamic_local_destroy(struct dahdi_dynamic *dyn)
  124. {
  125. struct dahdi_dynamic_local *d;
  126. unsigned long flags;
  127. struct dahdi_dynamic_local *cur;
  128. spin_lock_irqsave(&local_lock, flags);
  129. d = dyn->pvt;
  130. list_for_each_entry(cur, &dynamic_local_list, node) {
  131. if (cur->peer == d)
  132. cur->peer = NULL;
  133. if (cur->monitor_rx_peer == d)
  134. cur->monitor_rx_peer = NULL;
  135. }
  136. list_del(&d->node);
  137. dyn->pvt = NULL;
  138. spin_unlock_irqrestore(&local_lock, flags);
  139. printk(KERN_INFO "TDMoL: Removed interface for %s, key %d "
  140. "id %d\n", d->span->name, d->key, d->id);
  141. kfree(d);
  142. }
  143. static int dahdi_dynamic_local_create(struct dahdi_dynamic *dyn,
  144. const char *address)
  145. {
  146. struct dahdi_dynamic_local *d, *l;
  147. unsigned long flags;
  148. int key = -1, id = -1, monitor = -1;
  149. struct dahdi_span *const span = &dyn->span;
  150. if (strlen(address) >= 3) {
  151. if (address[1] != ':')
  152. goto INVALID_ADDRESS;
  153. key = digit2int(address[0]);
  154. id = digit2int(address[2]);
  155. }
  156. if (strlen (address) == 5) {
  157. if (address[3] != ':')
  158. goto INVALID_ADDRESS;
  159. monitor = digit2int(address[4]);
  160. }
  161. if (key == -1 || id == -1)
  162. goto INVALID_ADDRESS;
  163. d = kzalloc(sizeof(*d), GFP_KERNEL);
  164. if (!d)
  165. return -ENOMEM;
  166. d->key = key;
  167. d->id = id;
  168. d->span = span;
  169. spin_lock_irqsave(&local_lock, flags);
  170. /* Add this peer to any existing spans with same key
  171. And add them as peers to this one */
  172. list_for_each_entry(l, &dynamic_local_list, node) {
  173. if (l->key != d->key)
  174. continue;
  175. if (l->id == d->id) {
  176. printk(KERN_DEBUG "TDMoL: Duplicate id (%d) for key "
  177. "%d\n", d->id, d->key);
  178. goto CLEAR_AND_DEL_FROM_PEERS;
  179. }
  180. if (monitor == -1) {
  181. if (l->peer) {
  182. printk(KERN_DEBUG "TDMoL: Span with key %d and "
  183. "id %d already has a R/W peer\n",
  184. d->key, d->id);
  185. goto CLEAR_AND_DEL_FROM_PEERS;
  186. } else {
  187. l->peer = d;
  188. d->peer = l;
  189. }
  190. }
  191. if (monitor == l->id) {
  192. if (l->monitor_rx_peer) {
  193. printk(KERN_DEBUG "TDMoL: Span with key %d and "
  194. "id %d already has a monitoring peer\n",
  195. d->key, d->id);
  196. goto CLEAR_AND_DEL_FROM_PEERS;
  197. } else {
  198. l->monitor_rx_peer = d;
  199. }
  200. }
  201. }
  202. list_add(&d->node, &dynamic_local_list);
  203. dyn->pvt = d;
  204. spin_unlock_irqrestore(&local_lock, flags);
  205. printk(KERN_INFO "TDMoL: Added new interface for %s, "
  206. "key %d id %d\n", span->name, d->key, d->id);
  207. span->cannot_provide_timing = 1;
  208. return 0;
  209. CLEAR_AND_DEL_FROM_PEERS:
  210. list_for_each_entry(l, &dynamic_local_list, node) {
  211. if (l->peer == d)
  212. l->peer = NULL;
  213. if (l->monitor_rx_peer == d)
  214. l->monitor_rx_peer = NULL;
  215. }
  216. kfree(d);
  217. spin_unlock_irqrestore(&local_lock, flags);
  218. return -EINVAL;
  219. INVALID_ADDRESS:
  220. printk (KERN_NOTICE "TDMoL: Invalid address %s\n", address);
  221. return -EINVAL;
  222. }
  223. static struct dahdi_dynamic_driver dahdi_dynamic_local = {
  224. .owner = THIS_MODULE,
  225. .name = "loc",
  226. .desc = "Local",
  227. .create = dahdi_dynamic_local_create,
  228. .destroy = dahdi_dynamic_local_destroy,
  229. .transmit = dahdi_dynamic_local_transmit,
  230. };
  231. static int __init dahdi_dynamic_local_init(void)
  232. {
  233. dahdi_dynamic_register_driver(&dahdi_dynamic_local);
  234. return 0;
  235. }
  236. static void __exit dahdi_dynamic_local_exit(void)
  237. {
  238. dahdi_dynamic_unregister_driver(&dahdi_dynamic_local);
  239. }
  240. module_init(dahdi_dynamic_local_init);
  241. module_exit(dahdi_dynamic_local_exit);
  242. MODULE_LICENSE("GPL v2");