Debian Codegraph @

drivers/dahdi/dahdi_dynamic_loc.c

raw symbols SCIP
1 /*
2 * Dynamic Span Interface for DAHDI (Local Interface)
3 *
4 * Written by Nicolas Bougues <[email protected]>
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 /*
36 * See http://www.asterisk.org for more information about
37 * the Asterisk project. Please do not directly contact
38 * any of the maintainers of this project for assistance;
39 * the project provides a web site, mailing lists and IRC
40 * channels for your use.
41 *
42 * This program is free software, distributed under the terms of
43 * the GNU General Public License Version 2 as published by the
44 * Free Software Foundation. See the LICENSE file included with
45 * this program for more details.
46 */
47
48 #include <linux/kernel.h>
49 #include <linux/errno.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/spinlock.h>
53 #include <linux/slab.h>
54 #include <linux/kmod.h>
55 #include <linux/netdevice.h>
56 #include <linux/notifier.h>
57
58 #include <dahdi/kernel.h>
59
60 /**
61 * struct dahdi_dynamic_loc - For local dynamic spans
62 * @monitor_rx_peer: Indicates the peer span that monitors this span.
63 * @peer: Indicates the rw peer for this span.
64 *
65 */
66 struct dahdi_dynamic_local {
67 unsigned short key;
68 unsigned short id;
69 struct dahdi_dynamic_local *monitor_rx_peer;
70 struct dahdi_dynamic_local *peer;
71 struct dahdi_span *span;
72 struct list_head node;
73 };
74
75 static DEFINE_SPINLOCK(local_lock);
76 static LIST_HEAD(dynamic_local_list);
77
78 static void
79 dahdi_dynamic_local_transmit(struct dahdi_dynamic *dyn, u8 *msg, size_t msglen)
80 {
81 struct dahdi_dynamic_local *d;
82 unsigned long flags;
83
84 spin_lock_irqsave(&local_lock, flags);
85 d = dyn->pvt;
86 if (d && d->peer && d->peer->span) {
87 if (test_bit(DAHDI_FLAGBIT_REGISTERED, &d->peer->span->flags))
88 dahdi_dynamic_receive(d->peer->span, msg, msglen);
89 }
90 if (d && d->monitor_rx_peer && d->monitor_rx_peer->span) {
91 if (test_bit(DAHDI_FLAGBIT_REGISTERED,
92 &d->monitor_rx_peer->span->flags)) {
93 dahdi_dynamic_receive(d->monitor_rx_peer->span,
94 msg, msglen);
95 }
96 }
97 spin_unlock_irqrestore(&local_lock, flags);
98 }
99
100 static int digit2int(char d)
101 {
102 switch(d) {
103 case 'F':
104 case 'E':
105 case 'D':
106 case 'C':
107 case 'B':
108 case 'A':
109 return d - 'A' + 10;
110 case 'f':
111 case 'e':
112 case 'd':
113 case 'c':
114 case 'b':
115 case 'a':
116 return d - 'a' + 10;
117 case '9':
118 case '8':
119 case '7':
120 case '6':
121 case '5':
122 case '4':
123 case '3':
124 case '2':
125 case '1':
126 case '0':
127 return d - '0';
128 }
129 return -1;
130 }
131
132 static void dahdi_dynamic_local_destroy(struct dahdi_dynamic *dyn)
133 {
134 struct dahdi_dynamic_local *d;
135 unsigned long flags;
136 struct dahdi_dynamic_local *cur;
137
138 spin_lock_irqsave(&local_lock, flags);
139 d = dyn->pvt;
140 list_for_each_entry(cur, &dynamic_local_list, node) {
141 if (cur->peer == d)
142 cur->peer = NULL;
143 if (cur->monitor_rx_peer == d)
144 cur->monitor_rx_peer = NULL;
145 }
146 list_del(&d->node);
147 dyn->pvt = NULL;
148 spin_unlock_irqrestore(&local_lock, flags);
149
150 printk(KERN_INFO "TDMoL: Removed interface for %s, key %d "
151 "id %d\n", d->span->name, d->key, d->id);
152 kfree(d);
153 }
154
155 static int dahdi_dynamic_local_create(struct dahdi_dynamic *dyn,
156 const char *address)
157 {
158 struct dahdi_dynamic_local *d, *l;
159 unsigned long flags;
160 int key = -1, id = -1, monitor = -1;
161 struct dahdi_span *const span = &dyn->span;
162
163 if (strlen(address) >= 3) {
164 if (address[1] != ':')
165 goto INVALID_ADDRESS;
166 key = digit2int(address[0]);
167 id = digit2int(address[2]);
168 }
169 if (strlen (address) == 5) {
170 if (address[3] != ':')
171 goto INVALID_ADDRESS;
172 monitor = digit2int(address[4]);
173 }
174
175 if (key == -1 || id == -1)
176 goto INVALID_ADDRESS;
177
178 d = kzalloc(sizeof(*d), GFP_KERNEL);
179 if (!d)
180 return -ENOMEM;
181
182 d->key = key;
183 d->id = id;
184 d->span = span;
185
186 spin_lock_irqsave(&local_lock, flags);
187 /* Add this peer to any existing spans with same key
188 And add them as peers to this one */
189 list_for_each_entry(l, &dynamic_local_list, node) {
190 if (l->key != d->key)
191 continue;
192
193 if (l->id == d->id) {
194 printk(KERN_DEBUG "TDMoL: Duplicate id (%d) for key "
195 "%d\n", d->id, d->key);
196 goto CLEAR_AND_DEL_FROM_PEERS;
197 }
198 if (monitor == -1) {
199 if (l->peer) {
200 printk(KERN_DEBUG "TDMoL: Span with key %d and "
201 "id %d already has a R/W peer\n",
202 d->key, d->id);
203 goto CLEAR_AND_DEL_FROM_PEERS;
204 } else {
205 l->peer = d;
206 d->peer = l;
207 }
208 }
209 if (monitor == l->id) {
210 if (l->monitor_rx_peer) {
211 printk(KERN_DEBUG "TDMoL: Span with key %d and "
212 "id %d already has a monitoring peer\n",
213 d->key, d->id);
214 goto CLEAR_AND_DEL_FROM_PEERS;
215 } else {
216 l->monitor_rx_peer = d;
217 }
218 }
219 }
220 list_add(&d->node, &dynamic_local_list);
221 dyn->pvt = d;
222 spin_unlock_irqrestore(&local_lock, flags);
223
224 printk(KERN_INFO "TDMoL: Added new interface for %s, "
225 "key %d id %d\n", span->name, d->key, d->id);
226
227 span->cannot_provide_timing = 1;
228 return 0;
229
230 CLEAR_AND_DEL_FROM_PEERS:
231 list_for_each_entry(l, &dynamic_local_list, node) {
232 if (l->peer == d)
233 l->peer = NULL;
234 if (l->monitor_rx_peer == d)
235 l->monitor_rx_peer = NULL;
236 }
237 kfree(d);
238 spin_unlock_irqrestore(&local_lock, flags);
239 return -EINVAL;
240
241 INVALID_ADDRESS:
242 printk (KERN_NOTICE "TDMoL: Invalid address %s\n", address);
243 return -EINVAL;
244 }
245
246 static struct dahdi_dynamic_driver dahdi_dynamic_local = {
247 .owner = THIS_MODULE,
248 .name = "loc",
249 .desc = "Local",
250 .create = dahdi_dynamic_local_create,
251 .destroy = dahdi_dynamic_local_destroy,
252 .transmit = dahdi_dynamic_local_transmit,
253 };
254
255 static int __init dahdi_dynamic_local_init(void)
256 {
257 dahdi_dynamic_register_driver(&dahdi_dynamic_local);
258 return 0;
259 }
260
261 static void __exit dahdi_dynamic_local_exit(void)
262 {
263 dahdi_dynamic_unregister_driver(&dahdi_dynamic_local);
264 }
265
266 module_init(dahdi_dynamic_local_init);
267 module_exit(dahdi_dynamic_local_exit);
268
269 MODULE_LICENSE("GPL v2");