blob: 8cff570bb8df58bf2a8f929a5ea900a06c009789 [file] [log] [blame]
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001/*
2 *
3 * Author Karsten Keil <kkeil@novell.com>
4 *
5 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
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 version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/mISDNif.h>
19#include <linux/kthread.h>
20#include "core.h"
21
22static u_int *debug;
23
24static inline void
25_queue_message(struct mISDNstack *st, struct sk_buff *skb)
26{
27 struct mISDNhead *hh = mISDN_HEAD_P(skb);
28
29 if (*debug & DEBUG_QUEUE_FUNC)
30 printk(KERN_DEBUG "%s prim(%x) id(%x) %p\n",
31 __func__, hh->prim, hh->id, skb);
32 skb_queue_tail(&st->msgq, skb);
33 if (likely(!test_bit(mISDN_STACK_STOPPED, &st->status))) {
34 test_and_set_bit(mISDN_STACK_WORK, &st->status);
35 wake_up_interruptible(&st->workq);
36 }
37}
38
Hannes Eder5b834352008-12-12 21:15:17 -080039static int
Karsten Keil1b2b03f2008-07-27 01:54:58 +020040mISDN_queue_message(struct mISDNchannel *ch, struct sk_buff *skb)
41{
42 _queue_message(ch->st, skb);
43 return 0;
44}
45
46static struct mISDNchannel *
47get_channel4id(struct mISDNstack *st, u_int id)
48{
49 struct mISDNchannel *ch;
50
51 mutex_lock(&st->lmutex);
52 list_for_each_entry(ch, &st->layer2, list) {
53 if (id == ch->nr)
54 goto unlock;
55 }
56 ch = NULL;
57unlock:
58 mutex_unlock(&st->lmutex);
59 return ch;
60}
61
62static void
63send_socklist(struct mISDN_sock_list *sl, struct sk_buff *skb)
64{
65 struct hlist_node *node;
66 struct sock *sk;
67 struct sk_buff *cskb = NULL;
68
69 read_lock(&sl->lock);
70 sk_for_each(sk, node, &sl->head) {
71 if (sk->sk_state != MISDN_BOUND)
72 continue;
73 if (!cskb)
74 cskb = skb_copy(skb, GFP_KERNEL);
75 if (!cskb) {
76 printk(KERN_WARNING "%s no skb\n", __func__);
77 break;
78 }
79 if (!sock_queue_rcv_skb(sk, cskb))
80 cskb = NULL;
81 }
82 read_unlock(&sl->lock);
83 if (cskb)
84 dev_kfree_skb(cskb);
85}
86
87static void
88send_layer2(struct mISDNstack *st, struct sk_buff *skb)
89{
90 struct sk_buff *cskb;
91 struct mISDNhead *hh = mISDN_HEAD_P(skb);
92 struct mISDNchannel *ch;
93 int ret;
94
95 if (!st)
96 return;
97 mutex_lock(&st->lmutex);
98 if ((hh->id & MISDN_ID_ADDR_MASK) == MISDN_ID_ANY) { /* L2 for all */
99 list_for_each_entry(ch, &st->layer2, list) {
100 if (list_is_last(&ch->list, &st->layer2)) {
101 cskb = skb;
102 skb = NULL;
103 } else {
104 cskb = skb_copy(skb, GFP_KERNEL);
105 }
106 if (cskb) {
107 ret = ch->send(ch, cskb);
108 if (ret) {
109 if (*debug & DEBUG_SEND_ERR)
110 printk(KERN_DEBUG
111 "%s ch%d prim(%x) addr(%x)"
112 " err %d\n",
113 __func__, ch->nr,
114 hh->prim, ch->addr, ret);
115 dev_kfree_skb(cskb);
116 }
117 } else {
118 printk(KERN_WARNING "%s ch%d addr %x no mem\n",
119 __func__, ch->nr, ch->addr);
120 goto out;
121 }
122 }
123 } else {
124 list_for_each_entry(ch, &st->layer2, list) {
125 if ((hh->id & MISDN_ID_ADDR_MASK) == ch->addr) {
126 ret = ch->send(ch, skb);
127 if (!ret)
128 skb = NULL;
129 goto out;
130 }
131 }
132 ret = st->dev->teimgr->ctrl(st->dev->teimgr, CHECK_DATA, skb);
133 if (!ret)
134 skb = NULL;
135 else if (*debug & DEBUG_SEND_ERR)
136 printk(KERN_DEBUG
137 "%s ch%d mgr prim(%x) addr(%x) err %d\n",
138 __func__, ch->nr, hh->prim, ch->addr, ret);
139 }
140out:
141 mutex_unlock(&st->lmutex);
142 if (skb)
143 dev_kfree_skb(skb);
144}
145
146static inline int
147send_msg_to_layer(struct mISDNstack *st, struct sk_buff *skb)
148{
149 struct mISDNhead *hh = mISDN_HEAD_P(skb);
150 struct mISDNchannel *ch;
151 int lm;
152
153 lm = hh->prim & MISDN_LAYERMASK;
154 if (*debug & DEBUG_QUEUE_FUNC)
155 printk(KERN_DEBUG "%s prim(%x) id(%x) %p\n",
156 __func__, hh->prim, hh->id, skb);
157 if (lm == 0x1) {
158 if (!hlist_empty(&st->l1sock.head)) {
159 __net_timestamp(skb);
160 send_socklist(&st->l1sock, skb);
161 }
162 return st->layer1->send(st->layer1, skb);
163 } else if (lm == 0x2) {
164 if (!hlist_empty(&st->l1sock.head))
165 send_socklist(&st->l1sock, skb);
166 send_layer2(st, skb);
167 return 0;
168 } else if (lm == 0x4) {
169 ch = get_channel4id(st, hh->id);
170 if (ch)
171 return ch->send(ch, skb);
172 else
173 printk(KERN_WARNING
174 "%s: dev(%s) prim(%x) id(%x) no channel\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200175 __func__, dev_name(&st->dev->dev), hh->prim,
176 hh->id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200177 } else if (lm == 0x8) {
178 WARN_ON(lm == 0x8);
179 ch = get_channel4id(st, hh->id);
180 if (ch)
181 return ch->send(ch, skb);
182 else
183 printk(KERN_WARNING
184 "%s: dev(%s) prim(%x) id(%x) no channel\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200185 __func__, dev_name(&st->dev->dev), hh->prim,
186 hh->id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200187 } else {
188 /* broadcast not handled yet */
189 printk(KERN_WARNING "%s: dev(%s) prim %x not delivered\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200190 __func__, dev_name(&st->dev->dev), hh->prim);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200191 }
192 return -ESRCH;
193}
194
195static void
196do_clear_stack(struct mISDNstack *st)
197{
198}
199
200static int
201mISDNStackd(void *data)
202{
203 struct mISDNstack *st = data;
204 int err = 0;
205
206#ifdef CONFIG_SMP
207 lock_kernel();
208#endif
209 sigfillset(&current->blocked);
210#ifdef CONFIG_SMP
211 unlock_kernel();
212#endif
213 if (*debug & DEBUG_MSG_THREAD)
Matthias Urlichs837468d2008-08-16 00:04:33 +0200214 printk(KERN_DEBUG "mISDNStackd %s started\n",
215 dev_name(&st->dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200216
217 if (st->notify != NULL) {
218 complete(st->notify);
219 st->notify = NULL;
220 }
221
222 for (;;) {
223 struct sk_buff *skb;
224
225 if (unlikely(test_bit(mISDN_STACK_STOPPED, &st->status))) {
226 test_and_clear_bit(mISDN_STACK_WORK, &st->status);
227 test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
228 } else
229 test_and_set_bit(mISDN_STACK_RUNNING, &st->status);
230 while (test_bit(mISDN_STACK_WORK, &st->status)) {
231 skb = skb_dequeue(&st->msgq);
232 if (!skb) {
233 test_and_clear_bit(mISDN_STACK_WORK,
234 &st->status);
235 /* test if a race happens */
236 skb = skb_dequeue(&st->msgq);
237 if (!skb)
238 continue;
239 test_and_set_bit(mISDN_STACK_WORK,
240 &st->status);
241 }
242#ifdef MISDN_MSG_STATS
243 st->msg_cnt++;
244#endif
245 err = send_msg_to_layer(st, skb);
246 if (unlikely(err)) {
247 if (*debug & DEBUG_SEND_ERR)
248 printk(KERN_DEBUG
249 "%s: %s prim(%x) id(%x) "
250 "send call(%d)\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200251 __func__, dev_name(&st->dev->dev),
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200252 mISDN_HEAD_PRIM(skb),
253 mISDN_HEAD_ID(skb), err);
254 dev_kfree_skb(skb);
255 continue;
256 }
257 if (unlikely(test_bit(mISDN_STACK_STOPPED,
258 &st->status))) {
259 test_and_clear_bit(mISDN_STACK_WORK,
260 &st->status);
261 test_and_clear_bit(mISDN_STACK_RUNNING,
262 &st->status);
263 break;
264 }
265 }
266 if (test_bit(mISDN_STACK_CLEARING, &st->status)) {
267 test_and_set_bit(mISDN_STACK_STOPPED, &st->status);
268 test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
269 do_clear_stack(st);
270 test_and_clear_bit(mISDN_STACK_CLEARING, &st->status);
271 test_and_set_bit(mISDN_STACK_RESTART, &st->status);
272 }
273 if (test_and_clear_bit(mISDN_STACK_RESTART, &st->status)) {
274 test_and_clear_bit(mISDN_STACK_STOPPED, &st->status);
275 test_and_set_bit(mISDN_STACK_RUNNING, &st->status);
276 if (!skb_queue_empty(&st->msgq))
277 test_and_set_bit(mISDN_STACK_WORK,
278 &st->status);
279 }
280 if (test_bit(mISDN_STACK_ABORT, &st->status))
281 break;
282 if (st->notify != NULL) {
283 complete(st->notify);
284 st->notify = NULL;
285 }
286#ifdef MISDN_MSG_STATS
287 st->sleep_cnt++;
288#endif
289 test_and_clear_bit(mISDN_STACK_ACTIVE, &st->status);
290 wait_event_interruptible(st->workq, (st->status &
291 mISDN_STACK_ACTION_MASK));
292 if (*debug & DEBUG_MSG_THREAD)
293 printk(KERN_DEBUG "%s: %s wake status %08lx\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200294 __func__, dev_name(&st->dev->dev), st->status);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200295 test_and_set_bit(mISDN_STACK_ACTIVE, &st->status);
296
297 test_and_clear_bit(mISDN_STACK_WAKEUP, &st->status);
298
299 if (test_bit(mISDN_STACK_STOPPED, &st->status)) {
300 test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
301#ifdef MISDN_MSG_STATS
302 st->stopped_cnt++;
303#endif
304 }
305 }
306#ifdef MISDN_MSG_STATS
307 printk(KERN_DEBUG "mISDNStackd daemon for %s proceed %d "
308 "msg %d sleep %d stopped\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200309 dev_name(&st->dev->dev), st->msg_cnt, st->sleep_cnt,
310 st->stopped_cnt);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200311 printk(KERN_DEBUG
312 "mISDNStackd daemon for %s utime(%ld) stime(%ld)\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200313 dev_name(&st->dev->dev), st->thread->utime, st->thread->stime);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200314 printk(KERN_DEBUG
315 "mISDNStackd daemon for %s nvcsw(%ld) nivcsw(%ld)\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200316 dev_name(&st->dev->dev), st->thread->nvcsw, st->thread->nivcsw);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200317 printk(KERN_DEBUG "mISDNStackd daemon for %s killed now\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200318 dev_name(&st->dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200319#endif
320 test_and_set_bit(mISDN_STACK_KILLED, &st->status);
321 test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
322 test_and_clear_bit(mISDN_STACK_ACTIVE, &st->status);
323 test_and_clear_bit(mISDN_STACK_ABORT, &st->status);
324 skb_queue_purge(&st->msgq);
325 st->thread = NULL;
326 if (st->notify != NULL) {
327 complete(st->notify);
328 st->notify = NULL;
329 }
330 return 0;
331}
332
333static int
334l1_receive(struct mISDNchannel *ch, struct sk_buff *skb)
335{
336 if (!ch->st)
337 return -ENODEV;
338 __net_timestamp(skb);
339 _queue_message(ch->st, skb);
340 return 0;
341}
342
343void
344set_channel_address(struct mISDNchannel *ch, u_int sapi, u_int tei)
345{
346 ch->addr = sapi | (tei << 8);
347}
348
349void
350__add_layer2(struct mISDNchannel *ch, struct mISDNstack *st)
351{
352 list_add_tail(&ch->list, &st->layer2);
353}
354
355void
356add_layer2(struct mISDNchannel *ch, struct mISDNstack *st)
357{
358 mutex_lock(&st->lmutex);
359 __add_layer2(ch, st);
360 mutex_unlock(&st->lmutex);
361}
362
363static int
364st_own_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
365{
366 if (!ch->st || ch->st->layer1)
367 return -EINVAL;
368 return ch->st->layer1->ctrl(ch->st->layer1, cmd, arg);
369}
370
371int
372create_stack(struct mISDNdevice *dev)
373{
374 struct mISDNstack *newst;
375 int err;
376 DECLARE_COMPLETION_ONSTACK(done);
377
378 newst = kzalloc(sizeof(struct mISDNstack), GFP_KERNEL);
379 if (!newst) {
380 printk(KERN_ERR "kmalloc mISDN_stack failed\n");
381 return -ENOMEM;
382 }
383 newst->dev = dev;
384 INIT_LIST_HEAD(&newst->layer2);
385 INIT_HLIST_HEAD(&newst->l1sock.head);
386 rwlock_init(&newst->l1sock.lock);
387 init_waitqueue_head(&newst->workq);
388 skb_queue_head_init(&newst->msgq);
389 mutex_init(&newst->lmutex);
390 dev->D.st = newst;
391 err = create_teimanager(dev);
392 if (err) {
393 printk(KERN_ERR "kmalloc teimanager failed\n");
394 kfree(newst);
395 return err;
396 }
397 dev->teimgr->peer = &newst->own;
398 dev->teimgr->recv = mISDN_queue_message;
399 dev->teimgr->st = newst;
400 newst->layer1 = &dev->D;
401 dev->D.recv = l1_receive;
402 dev->D.peer = &newst->own;
403 newst->own.st = newst;
404 newst->own.ctrl = st_own_ctrl;
405 newst->own.send = mISDN_queue_message;
406 newst->own.recv = mISDN_queue_message;
407 if (*debug & DEBUG_CORE_FUNC)
Matthias Urlichs837468d2008-08-16 00:04:33 +0200408 printk(KERN_DEBUG "%s: st(%s)\n", __func__,
409 dev_name(&newst->dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200410 newst->notify = &done;
411 newst->thread = kthread_run(mISDNStackd, (void *)newst, "mISDN_%s",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200412 dev_name(&newst->dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200413 if (IS_ERR(newst->thread)) {
414 err = PTR_ERR(newst->thread);
415 printk(KERN_ERR
416 "mISDN:cannot create kernel thread for %s (%d)\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200417 dev_name(&newst->dev->dev), err);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200418 delete_teimanager(dev->teimgr);
419 kfree(newst);
420 } else
421 wait_for_completion(&done);
422 return err;
423}
424
425int
426connect_layer1(struct mISDNdevice *dev, struct mISDNchannel *ch,
427 u_int protocol, struct sockaddr_mISDN *adr)
428{
429 struct mISDN_sock *msk = container_of(ch, struct mISDN_sock, ch);
430 struct channel_req rq;
431 int err;
432
433
434 if (*debug & DEBUG_CORE_FUNC)
435 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200436 __func__, dev_name(&dev->dev), protocol, adr->dev,
437 adr->channel, adr->sapi, adr->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200438 switch (protocol) {
439 case ISDN_P_NT_S0:
440 case ISDN_P_NT_E1:
441 case ISDN_P_TE_S0:
442 case ISDN_P_TE_E1:
443#ifdef PROTOCOL_CHECK
444 /* this should be enhanced */
445 if (!list_empty(&dev->D.st->layer2)
446 && dev->D.protocol != protocol)
447 return -EBUSY;
448 if (!hlist_empty(&dev->D.st->l1sock.head)
449 && dev->D.protocol != protocol)
450 return -EBUSY;
451#endif
452 ch->recv = mISDN_queue_message;
453 ch->peer = &dev->D.st->own;
454 ch->st = dev->D.st;
455 rq.protocol = protocol;
Martin Bachem1f28fa192008-09-03 15:17:45 +0200456 rq.adr.channel = adr->channel;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200457 err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
458 printk(KERN_DEBUG "%s: ret 1 %d\n", __func__, err);
459 if (err)
460 return err;
461 write_lock_bh(&dev->D.st->l1sock.lock);
462 sk_add_node(&msk->sk, &dev->D.st->l1sock.head);
463 write_unlock_bh(&dev->D.st->l1sock.lock);
464 break;
465 default:
466 return -ENOPROTOOPT;
467 }
468 return 0;
469}
470
471int
472connect_Bstack(struct mISDNdevice *dev, struct mISDNchannel *ch,
473 u_int protocol, struct sockaddr_mISDN *adr)
474{
475 struct channel_req rq, rq2;
476 int pmask, err;
477 struct Bprotocol *bp;
478
479 if (*debug & DEBUG_CORE_FUNC)
480 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200481 __func__, dev_name(&dev->dev), protocol,
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200482 adr->dev, adr->channel, adr->sapi,
483 adr->tei);
484 ch->st = dev->D.st;
485 pmask = 1 << (protocol & ISDN_P_B_MASK);
486 if (pmask & dev->Bprotocols) {
487 rq.protocol = protocol;
488 rq.adr = *adr;
489 err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
490 if (err)
491 return err;
492 ch->recv = rq.ch->send;
493 ch->peer = rq.ch;
494 rq.ch->recv = ch->send;
495 rq.ch->peer = ch;
496 rq.ch->st = dev->D.st;
497 } else {
498 bp = get_Bprotocol4mask(pmask);
499 if (!bp)
500 return -ENOPROTOOPT;
501 rq2.protocol = protocol;
502 rq2.adr = *adr;
503 rq2.ch = ch;
504 err = bp->create(&rq2);
505 if (err)
506 return err;
507 ch->recv = rq2.ch->send;
508 ch->peer = rq2.ch;
509 rq2.ch->st = dev->D.st;
510 rq.protocol = rq2.protocol;
511 rq.adr = *adr;
512 err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
513 if (err) {
514 rq2.ch->ctrl(rq2.ch, CLOSE_CHANNEL, NULL);
515 return err;
516 }
517 rq2.ch->recv = rq.ch->send;
518 rq2.ch->peer = rq.ch;
519 rq.ch->recv = rq2.ch->send;
520 rq.ch->peer = rq2.ch;
521 rq.ch->st = dev->D.st;
522 }
523 ch->protocol = protocol;
524 ch->nr = rq.ch->nr;
525 return 0;
526}
527
528int
529create_l2entity(struct mISDNdevice *dev, struct mISDNchannel *ch,
530 u_int protocol, struct sockaddr_mISDN *adr)
531{
532 struct channel_req rq;
533 int err;
534
535 if (*debug & DEBUG_CORE_FUNC)
536 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200537 __func__, dev_name(&dev->dev), protocol,
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200538 adr->dev, adr->channel, adr->sapi,
539 adr->tei);
540 rq.protocol = ISDN_P_TE_S0;
541 if (dev->Dprotocols & (1 << ISDN_P_TE_E1))
542 rq.protocol = ISDN_P_TE_E1;
543 switch (protocol) {
544 case ISDN_P_LAPD_NT:
545 rq.protocol = ISDN_P_NT_S0;
546 if (dev->Dprotocols & (1 << ISDN_P_NT_E1))
547 rq.protocol = ISDN_P_NT_E1;
548 case ISDN_P_LAPD_TE:
549#ifdef PROTOCOL_CHECK
550 /* this should be enhanced */
551 if (!list_empty(&dev->D.st->layer2)
552 && dev->D.protocol != protocol)
553 return -EBUSY;
554 if (!hlist_empty(&dev->D.st->l1sock.head)
555 && dev->D.protocol != protocol)
556 return -EBUSY;
557#endif
558 ch->recv = mISDN_queue_message;
559 ch->peer = &dev->D.st->own;
560 ch->st = dev->D.st;
561 rq.adr.channel = 0;
562 err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
563 printk(KERN_DEBUG "%s: ret 1 %d\n", __func__, err);
564 if (err)
565 break;
566 rq.protocol = protocol;
567 rq.adr = *adr;
568 rq.ch = ch;
569 err = dev->teimgr->ctrl(dev->teimgr, OPEN_CHANNEL, &rq);
570 printk(KERN_DEBUG "%s: ret 2 %d\n", __func__, err);
571 if (!err) {
572 if ((protocol == ISDN_P_LAPD_NT) && !rq.ch)
573 break;
574 add_layer2(rq.ch, dev->D.st);
575 rq.ch->recv = mISDN_queue_message;
576 rq.ch->peer = &dev->D.st->own;
577 rq.ch->ctrl(rq.ch, OPEN_CHANNEL, NULL); /* can't fail */
578 }
579 break;
580 default:
581 err = -EPROTONOSUPPORT;
582 }
583 return err;
584}
585
586void
587delete_channel(struct mISDNchannel *ch)
588{
589 struct mISDN_sock *msk = container_of(ch, struct mISDN_sock, ch);
590 struct mISDNchannel *pch;
591
592 if (!ch->st) {
593 printk(KERN_WARNING "%s: no stack\n", __func__);
594 return;
595 }
596 if (*debug & DEBUG_CORE_FUNC)
597 printk(KERN_DEBUG "%s: st(%s) protocol(%x)\n", __func__,
Matthias Urlichs837468d2008-08-16 00:04:33 +0200598 dev_name(&ch->st->dev->dev), ch->protocol);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200599 if (ch->protocol >= ISDN_P_B_START) {
600 if (ch->peer) {
601 ch->peer->ctrl(ch->peer, CLOSE_CHANNEL, NULL);
602 ch->peer = NULL;
603 }
604 return;
605 }
606 switch (ch->protocol) {
607 case ISDN_P_NT_S0:
608 case ISDN_P_TE_S0:
609 case ISDN_P_NT_E1:
610 case ISDN_P_TE_E1:
611 write_lock_bh(&ch->st->l1sock.lock);
612 sk_del_node_init(&msk->sk);
613 write_unlock_bh(&ch->st->l1sock.lock);
614 ch->st->dev->D.ctrl(&ch->st->dev->D, CLOSE_CHANNEL, NULL);
615 break;
616 case ISDN_P_LAPD_TE:
617 pch = get_channel4id(ch->st, ch->nr);
618 if (pch) {
619 mutex_lock(&ch->st->lmutex);
620 list_del(&pch->list);
621 mutex_unlock(&ch->st->lmutex);
622 pch->ctrl(pch, CLOSE_CHANNEL, NULL);
623 pch = ch->st->dev->teimgr;
624 pch->ctrl(pch, CLOSE_CHANNEL, NULL);
625 } else
626 printk(KERN_WARNING "%s: no l2 channel\n",
627 __func__);
628 break;
629 case ISDN_P_LAPD_NT:
630 pch = ch->st->dev->teimgr;
631 if (pch) {
632 pch->ctrl(pch, CLOSE_CHANNEL, NULL);
633 } else
634 printk(KERN_WARNING "%s: no l2 channel\n",
635 __func__);
636 break;
637 default:
638 break;
639 }
640 return;
641}
642
643void
644delete_stack(struct mISDNdevice *dev)
645{
646 struct mISDNstack *st = dev->D.st;
647 DECLARE_COMPLETION_ONSTACK(done);
648
649 if (*debug & DEBUG_CORE_FUNC)
650 printk(KERN_DEBUG "%s: st(%s)\n", __func__,
Matthias Urlichs837468d2008-08-16 00:04:33 +0200651 dev_name(&st->dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200652 if (dev->teimgr)
653 delete_teimanager(dev->teimgr);
654 if (st->thread) {
655 if (st->notify) {
656 printk(KERN_WARNING "%s: notifier in use\n",
657 __func__);
658 complete(st->notify);
659 }
660 st->notify = &done;
661 test_and_set_bit(mISDN_STACK_ABORT, &st->status);
662 test_and_set_bit(mISDN_STACK_WAKEUP, &st->status);
663 wake_up_interruptible(&st->workq);
664 wait_for_completion(&done);
665 }
666 if (!list_empty(&st->layer2))
667 printk(KERN_WARNING "%s: layer2 list not empty\n",
668 __func__);
669 if (!hlist_empty(&st->l1sock.head))
670 printk(KERN_WARNING "%s: layer1 list not empty\n",
671 __func__);
672 kfree(st);
673}
674
675void
676mISDN_initstack(u_int *dp)
677{
678 debug = dp;
679}