blob: deda591f70b9fab4be605442fb1f6afe9bd9f7be [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Karsten Keil1b2b03f2008-07-27 01:54:58 +020019#include <linux/mISDNif.h>
20#include <linux/kthread.h>
Frederic Weisbecker6fac4822012-11-13 14:20:55 +010021#include <linux/sched.h>
Karsten Keil1b2b03f2008-07-27 01:54:58 +020022#include "core.h"
23
24static u_int *debug;
25
26static inline void
27_queue_message(struct mISDNstack *st, struct sk_buff *skb)
28{
29 struct mISDNhead *hh = mISDN_HEAD_P(skb);
30
31 if (*debug & DEBUG_QUEUE_FUNC)
32 printk(KERN_DEBUG "%s prim(%x) id(%x) %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -080033 __func__, hh->prim, hh->id, skb);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020034 skb_queue_tail(&st->msgq, skb);
35 if (likely(!test_bit(mISDN_STACK_STOPPED, &st->status))) {
36 test_and_set_bit(mISDN_STACK_WORK, &st->status);
37 wake_up_interruptible(&st->workq);
38 }
39}
40
Hannes Eder5b834352008-12-12 21:15:17 -080041static int
Karsten Keil1b2b03f2008-07-27 01:54:58 +020042mISDN_queue_message(struct mISDNchannel *ch, struct sk_buff *skb)
43{
44 _queue_message(ch->st, skb);
45 return 0;
46}
47
48static struct mISDNchannel *
49get_channel4id(struct mISDNstack *st, u_int id)
50{
51 struct mISDNchannel *ch;
52
53 mutex_lock(&st->lmutex);
54 list_for_each_entry(ch, &st->layer2, list) {
55 if (id == ch->nr)
56 goto unlock;
57 }
58 ch = NULL;
59unlock:
60 mutex_unlock(&st->lmutex);
61 return ch;
62}
63
64static void
65send_socklist(struct mISDN_sock_list *sl, struct sk_buff *skb)
66{
67 struct hlist_node *node;
68 struct sock *sk;
69 struct sk_buff *cskb = NULL;
70
71 read_lock(&sl->lock);
72 sk_for_each(sk, node, &sl->head) {
73 if (sk->sk_state != MISDN_BOUND)
74 continue;
75 if (!cskb)
76 cskb = skb_copy(skb, GFP_KERNEL);
77 if (!cskb) {
78 printk(KERN_WARNING "%s no skb\n", __func__);
79 break;
80 }
81 if (!sock_queue_rcv_skb(sk, cskb))
82 cskb = NULL;
83 }
84 read_unlock(&sl->lock);
85 if (cskb)
86 dev_kfree_skb(cskb);
87}
88
89static void
90send_layer2(struct mISDNstack *st, struct sk_buff *skb)
91{
92 struct sk_buff *cskb;
93 struct mISDNhead *hh = mISDN_HEAD_P(skb);
94 struct mISDNchannel *ch;
95 int ret;
96
97 if (!st)
98 return;
99 mutex_lock(&st->lmutex);
100 if ((hh->id & MISDN_ID_ADDR_MASK) == MISDN_ID_ANY) { /* L2 for all */
101 list_for_each_entry(ch, &st->layer2, list) {
102 if (list_is_last(&ch->list, &st->layer2)) {
103 cskb = skb;
104 skb = NULL;
105 } else {
106 cskb = skb_copy(skb, GFP_KERNEL);
107 }
108 if (cskb) {
109 ret = ch->send(ch, cskb);
110 if (ret) {
111 if (*debug & DEBUG_SEND_ERR)
112 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800113 "%s ch%d prim(%x) addr(%x)"
114 " err %d\n",
115 __func__, ch->nr,
116 hh->prim, ch->addr, ret);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200117 dev_kfree_skb(cskb);
118 }
119 } else {
120 printk(KERN_WARNING "%s ch%d addr %x no mem\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800121 __func__, ch->nr, ch->addr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200122 goto out;
123 }
124 }
125 } else {
126 list_for_each_entry(ch, &st->layer2, list) {
127 if ((hh->id & MISDN_ID_ADDR_MASK) == ch->addr) {
128 ret = ch->send(ch, skb);
129 if (!ret)
130 skb = NULL;
131 goto out;
132 }
133 }
134 ret = st->dev->teimgr->ctrl(st->dev->teimgr, CHECK_DATA, skb);
135 if (!ret)
136 skb = NULL;
137 else if (*debug & DEBUG_SEND_ERR)
138 printk(KERN_DEBUG
Julia Lawall1b9faf52012-07-08 01:37:38 +0000139 "%s mgr prim(%x) err %d\n",
140 __func__, hh->prim, ret);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200141 }
142out:
143 mutex_unlock(&st->lmutex);
144 if (skb)
145 dev_kfree_skb(skb);
146}
147
148static inline int
149send_msg_to_layer(struct mISDNstack *st, struct sk_buff *skb)
150{
151 struct mISDNhead *hh = mISDN_HEAD_P(skb);
152 struct mISDNchannel *ch;
153 int lm;
154
155 lm = hh->prim & MISDN_LAYERMASK;
156 if (*debug & DEBUG_QUEUE_FUNC)
157 printk(KERN_DEBUG "%s prim(%x) id(%x) %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800158 __func__, hh->prim, hh->id, skb);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200159 if (lm == 0x1) {
160 if (!hlist_empty(&st->l1sock.head)) {
161 __net_timestamp(skb);
162 send_socklist(&st->l1sock, skb);
163 }
164 return st->layer1->send(st->layer1, skb);
165 } else if (lm == 0x2) {
166 if (!hlist_empty(&st->l1sock.head))
167 send_socklist(&st->l1sock, skb);
168 send_layer2(st, skb);
169 return 0;
170 } else if (lm == 0x4) {
171 ch = get_channel4id(st, hh->id);
172 if (ch)
173 return ch->send(ch, skb);
174 else
175 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -0800176 "%s: dev(%s) prim(%x) id(%x) no channel\n",
177 __func__, dev_name(&st->dev->dev), hh->prim,
178 hh->id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200179 } else if (lm == 0x8) {
180 WARN_ON(lm == 0x8);
181 ch = get_channel4id(st, hh->id);
182 if (ch)
183 return ch->send(ch, skb);
184 else
185 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -0800186 "%s: dev(%s) prim(%x) id(%x) no channel\n",
187 __func__, dev_name(&st->dev->dev), hh->prim,
188 hh->id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200189 } else {
190 /* broadcast not handled yet */
191 printk(KERN_WARNING "%s: dev(%s) prim %x not delivered\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800192 __func__, dev_name(&st->dev->dev), hh->prim);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200193 }
194 return -ESRCH;
195}
196
197static void
198do_clear_stack(struct mISDNstack *st)
199{
200}
201
202static int
203mISDNStackd(void *data)
204{
205 struct mISDNstack *st = data;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100206#ifdef MISDN_MSG_STATS
207 cputime_t utime, stime;
208#endif
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200209 int err = 0;
210
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200211 sigfillset(&current->blocked);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200212 if (*debug & DEBUG_MSG_THREAD)
Matthias Urlichs837468d2008-08-16 00:04:33 +0200213 printk(KERN_DEBUG "mISDNStackd %s started\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800214 dev_name(&st->dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200215
216 if (st->notify != NULL) {
217 complete(st->notify);
218 st->notify = NULL;
219 }
220
221 for (;;) {
222 struct sk_buff *skb;
223
224 if (unlikely(test_bit(mISDN_STACK_STOPPED, &st->status))) {
225 test_and_clear_bit(mISDN_STACK_WORK, &st->status);
226 test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
227 } else
228 test_and_set_bit(mISDN_STACK_RUNNING, &st->status);
229 while (test_bit(mISDN_STACK_WORK, &st->status)) {
230 skb = skb_dequeue(&st->msgq);
231 if (!skb) {
232 test_and_clear_bit(mISDN_STACK_WORK,
Joe Perches475be4d2012-02-19 19:52:38 -0800233 &st->status);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200234 /* test if a race happens */
235 skb = skb_dequeue(&st->msgq);
236 if (!skb)
237 continue;
238 test_and_set_bit(mISDN_STACK_WORK,
Joe Perches475be4d2012-02-19 19:52:38 -0800239 &st->status);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200240 }
241#ifdef MISDN_MSG_STATS
242 st->msg_cnt++;
243#endif
244 err = send_msg_to_layer(st, skb);
245 if (unlikely(err)) {
246 if (*debug & DEBUG_SEND_ERR)
247 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800248 "%s: %s prim(%x) id(%x) "
249 "send call(%d)\n",
250 __func__, dev_name(&st->dev->dev),
251 mISDN_HEAD_PRIM(skb),
252 mISDN_HEAD_ID(skb), err);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200253 dev_kfree_skb(skb);
254 continue;
255 }
256 if (unlikely(test_bit(mISDN_STACK_STOPPED,
Joe Perches475be4d2012-02-19 19:52:38 -0800257 &st->status))) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200258 test_and_clear_bit(mISDN_STACK_WORK,
Joe Perches475be4d2012-02-19 19:52:38 -0800259 &st->status);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200260 test_and_clear_bit(mISDN_STACK_RUNNING,
Joe Perches475be4d2012-02-19 19:52:38 -0800261 &st->status);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200262 break;
263 }
264 }
265 if (test_bit(mISDN_STACK_CLEARING, &st->status)) {
266 test_and_set_bit(mISDN_STACK_STOPPED, &st->status);
267 test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
268 do_clear_stack(st);
269 test_and_clear_bit(mISDN_STACK_CLEARING, &st->status);
270 test_and_set_bit(mISDN_STACK_RESTART, &st->status);
271 }
272 if (test_and_clear_bit(mISDN_STACK_RESTART, &st->status)) {
273 test_and_clear_bit(mISDN_STACK_STOPPED, &st->status);
274 test_and_set_bit(mISDN_STACK_RUNNING, &st->status);
275 if (!skb_queue_empty(&st->msgq))
276 test_and_set_bit(mISDN_STACK_WORK,
Joe Perches475be4d2012-02-19 19:52:38 -0800277 &st->status);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200278 }
279 if (test_bit(mISDN_STACK_ABORT, &st->status))
280 break;
281 if (st->notify != NULL) {
282 complete(st->notify);
283 st->notify = NULL;
284 }
285#ifdef MISDN_MSG_STATS
286 st->sleep_cnt++;
287#endif
288 test_and_clear_bit(mISDN_STACK_ACTIVE, &st->status);
289 wait_event_interruptible(st->workq, (st->status &
Joe Perches475be4d2012-02-19 19:52:38 -0800290 mISDN_STACK_ACTION_MASK));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200291 if (*debug & DEBUG_MSG_THREAD)
292 printk(KERN_DEBUG "%s: %s wake status %08lx\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800293 __func__, dev_name(&st->dev->dev), st->status);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200294 test_and_set_bit(mISDN_STACK_ACTIVE, &st->status);
295
296 test_and_clear_bit(mISDN_STACK_WAKEUP, &st->status);
297
298 if (test_bit(mISDN_STACK_STOPPED, &st->status)) {
299 test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
300#ifdef MISDN_MSG_STATS
301 st->stopped_cnt++;
302#endif
303 }
304 }
305#ifdef MISDN_MSG_STATS
306 printk(KERN_DEBUG "mISDNStackd daemon for %s proceed %d "
Joe Perches475be4d2012-02-19 19:52:38 -0800307 "msg %d sleep %d stopped\n",
308 dev_name(&st->dev->dev), st->msg_cnt, st->sleep_cnt,
309 st->stopped_cnt);
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100310 task_cputime(st->thread, &utime, &stime);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200311 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800312 "mISDNStackd daemon for %s utime(%ld) stime(%ld)\n",
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100313 dev_name(&st->dev->dev), utime, stime);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200314 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800315 "mISDNStackd daemon for %s nvcsw(%ld) nivcsw(%ld)\n",
316 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",
Joe Perches475be4d2012-02-19 19:52:38 -0800318 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{
Dan Carpenter08cb3f62009-11-04 08:27:09 -0800366 if (!ch->st || !ch->st->layer1)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200367 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__,
Joe Perches475be4d2012-02-19 19:52:38 -0800409 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",
Joe Perches475be4d2012-02-19 19:52:38 -0800412 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
Joe Perches475be4d2012-02-19 19:52:38 -0800416 "mISDN:cannot create kernel thread for %s (%d)\n",
417 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,
Joe Perches475be4d2012-02-19 19:52:38 -0800427 u_int protocol, struct sockaddr_mISDN *adr)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200428{
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",
Joe Perches475be4d2012-02-19 19:52:38 -0800436 __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:
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200443 ch->recv = mISDN_queue_message;
444 ch->peer = &dev->D.st->own;
445 ch->st = dev->D.st;
446 rq.protocol = protocol;
Martin Bachem1f28fa192008-09-03 15:17:45 +0200447 rq.adr.channel = adr->channel;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200448 err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
Andreas Eversberg9a812552008-09-14 14:42:18 +0200449 printk(KERN_DEBUG "%s: ret %d (dev %d)\n", __func__, err,
Joe Perches475be4d2012-02-19 19:52:38 -0800450 dev->id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200451 if (err)
452 return err;
453 write_lock_bh(&dev->D.st->l1sock.lock);
454 sk_add_node(&msk->sk, &dev->D.st->l1sock.head);
455 write_unlock_bh(&dev->D.st->l1sock.lock);
456 break;
457 default:
458 return -ENOPROTOOPT;
459 }
460 return 0;
461}
462
463int
464connect_Bstack(struct mISDNdevice *dev, struct mISDNchannel *ch,
Joe Perches475be4d2012-02-19 19:52:38 -0800465 u_int protocol, struct sockaddr_mISDN *adr)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200466{
467 struct channel_req rq, rq2;
468 int pmask, err;
469 struct Bprotocol *bp;
470
471 if (*debug & DEBUG_CORE_FUNC)
472 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800473 __func__, dev_name(&dev->dev), protocol,
474 adr->dev, adr->channel, adr->sapi,
475 adr->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200476 ch->st = dev->D.st;
477 pmask = 1 << (protocol & ISDN_P_B_MASK);
478 if (pmask & dev->Bprotocols) {
479 rq.protocol = protocol;
480 rq.adr = *adr;
481 err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
482 if (err)
483 return err;
484 ch->recv = rq.ch->send;
485 ch->peer = rq.ch;
486 rq.ch->recv = ch->send;
487 rq.ch->peer = ch;
488 rq.ch->st = dev->D.st;
489 } else {
490 bp = get_Bprotocol4mask(pmask);
491 if (!bp)
492 return -ENOPROTOOPT;
493 rq2.protocol = protocol;
494 rq2.adr = *adr;
495 rq2.ch = ch;
496 err = bp->create(&rq2);
497 if (err)
498 return err;
499 ch->recv = rq2.ch->send;
500 ch->peer = rq2.ch;
501 rq2.ch->st = dev->D.st;
502 rq.protocol = rq2.protocol;
503 rq.adr = *adr;
504 err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
505 if (err) {
506 rq2.ch->ctrl(rq2.ch, CLOSE_CHANNEL, NULL);
507 return err;
508 }
509 rq2.ch->recv = rq.ch->send;
510 rq2.ch->peer = rq.ch;
511 rq.ch->recv = rq2.ch->send;
512 rq.ch->peer = rq2.ch;
513 rq.ch->st = dev->D.st;
514 }
515 ch->protocol = protocol;
516 ch->nr = rq.ch->nr;
517 return 0;
518}
519
520int
521create_l2entity(struct mISDNdevice *dev, struct mISDNchannel *ch,
Joe Perches475be4d2012-02-19 19:52:38 -0800522 u_int protocol, struct sockaddr_mISDN *adr)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200523{
524 struct channel_req rq;
525 int err;
526
527 if (*debug & DEBUG_CORE_FUNC)
528 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800529 __func__, dev_name(&dev->dev), protocol,
530 adr->dev, adr->channel, adr->sapi,
531 adr->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200532 rq.protocol = ISDN_P_TE_S0;
533 if (dev->Dprotocols & (1 << ISDN_P_TE_E1))
534 rq.protocol = ISDN_P_TE_E1;
535 switch (protocol) {
536 case ISDN_P_LAPD_NT:
537 rq.protocol = ISDN_P_NT_S0;
538 if (dev->Dprotocols & (1 << ISDN_P_NT_E1))
539 rq.protocol = ISDN_P_NT_E1;
540 case ISDN_P_LAPD_TE:
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200541 ch->recv = mISDN_queue_message;
542 ch->peer = &dev->D.st->own;
543 ch->st = dev->D.st;
544 rq.adr.channel = 0;
545 err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
546 printk(KERN_DEBUG "%s: ret 1 %d\n", __func__, err);
547 if (err)
548 break;
549 rq.protocol = protocol;
550 rq.adr = *adr;
551 rq.ch = ch;
552 err = dev->teimgr->ctrl(dev->teimgr, OPEN_CHANNEL, &rq);
553 printk(KERN_DEBUG "%s: ret 2 %d\n", __func__, err);
554 if (!err) {
555 if ((protocol == ISDN_P_LAPD_NT) && !rq.ch)
556 break;
557 add_layer2(rq.ch, dev->D.st);
558 rq.ch->recv = mISDN_queue_message;
559 rq.ch->peer = &dev->D.st->own;
560 rq.ch->ctrl(rq.ch, OPEN_CHANNEL, NULL); /* can't fail */
561 }
562 break;
563 default:
564 err = -EPROTONOSUPPORT;
565 }
566 return err;
567}
568
569void
570delete_channel(struct mISDNchannel *ch)
571{
572 struct mISDN_sock *msk = container_of(ch, struct mISDN_sock, ch);
573 struct mISDNchannel *pch;
574
575 if (!ch->st) {
576 printk(KERN_WARNING "%s: no stack\n", __func__);
577 return;
578 }
579 if (*debug & DEBUG_CORE_FUNC)
580 printk(KERN_DEBUG "%s: st(%s) protocol(%x)\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800581 dev_name(&ch->st->dev->dev), ch->protocol);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200582 if (ch->protocol >= ISDN_P_B_START) {
583 if (ch->peer) {
584 ch->peer->ctrl(ch->peer, CLOSE_CHANNEL, NULL);
585 ch->peer = NULL;
586 }
587 return;
588 }
589 switch (ch->protocol) {
590 case ISDN_P_NT_S0:
591 case ISDN_P_TE_S0:
592 case ISDN_P_NT_E1:
593 case ISDN_P_TE_E1:
594 write_lock_bh(&ch->st->l1sock.lock);
595 sk_del_node_init(&msk->sk);
596 write_unlock_bh(&ch->st->l1sock.lock);
597 ch->st->dev->D.ctrl(&ch->st->dev->D, CLOSE_CHANNEL, NULL);
598 break;
599 case ISDN_P_LAPD_TE:
600 pch = get_channel4id(ch->st, ch->nr);
601 if (pch) {
602 mutex_lock(&ch->st->lmutex);
603 list_del(&pch->list);
604 mutex_unlock(&ch->st->lmutex);
605 pch->ctrl(pch, CLOSE_CHANNEL, NULL);
606 pch = ch->st->dev->teimgr;
607 pch->ctrl(pch, CLOSE_CHANNEL, NULL);
608 } else
609 printk(KERN_WARNING "%s: no l2 channel\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800610 __func__);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200611 break;
612 case ISDN_P_LAPD_NT:
613 pch = ch->st->dev->teimgr;
614 if (pch) {
615 pch->ctrl(pch, CLOSE_CHANNEL, NULL);
616 } else
617 printk(KERN_WARNING "%s: no l2 channel\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800618 __func__);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200619 break;
620 default:
621 break;
622 }
623 return;
624}
625
626void
627delete_stack(struct mISDNdevice *dev)
628{
629 struct mISDNstack *st = dev->D.st;
630 DECLARE_COMPLETION_ONSTACK(done);
631
632 if (*debug & DEBUG_CORE_FUNC)
633 printk(KERN_DEBUG "%s: st(%s)\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800634 dev_name(&st->dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200635 if (dev->teimgr)
636 delete_teimanager(dev->teimgr);
637 if (st->thread) {
638 if (st->notify) {
639 printk(KERN_WARNING "%s: notifier in use\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800640 __func__);
641 complete(st->notify);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200642 }
643 st->notify = &done;
644 test_and_set_bit(mISDN_STACK_ABORT, &st->status);
645 test_and_set_bit(mISDN_STACK_WAKEUP, &st->status);
646 wake_up_interruptible(&st->workq);
647 wait_for_completion(&done);
648 }
649 if (!list_empty(&st->layer2))
650 printk(KERN_WARNING "%s: layer2 list not empty\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800651 __func__);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200652 if (!hlist_empty(&st->l1sock.head))
653 printk(KERN_WARNING "%s: layer1 list not empty\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800654 __func__);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200655 kfree(st);
656}
657
658void
659mISDN_initstack(u_int *dp)
660{
661 debug = dp;
662}