3 * Author Karsten Keil <kkeil@novell.com>
5 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
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.
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.
18 #include <linux/mISDNif.h>
19 #include <linux/kthread.h>
25 _queue_message(struct mISDNstack
*st
, struct sk_buff
*skb
)
27 struct mISDNhead
*hh
= mISDN_HEAD_P(skb
);
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
);
40 mISDN_queue_message(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
42 _queue_message(ch
->st
, skb
);
46 static struct mISDNchannel
*
47 get_channel4id(struct mISDNstack
*st
, u_int id
)
49 struct mISDNchannel
*ch
;
51 mutex_lock(&st
->lmutex
);
52 list_for_each_entry(ch
, &st
->layer2
, list
) {
58 mutex_unlock(&st
->lmutex
);
63 send_socklist(struct mISDN_sock_list
*sl
, struct sk_buff
*skb
)
65 struct hlist_node
*node
;
67 struct sk_buff
*cskb
= NULL
;
70 sk_for_each(sk
, node
, &sl
->head
) {
71 if (sk
->sk_state
!= MISDN_BOUND
)
74 cskb
= skb_copy(skb
, GFP_KERNEL
);
76 printk(KERN_WARNING
"%s no skb\n", __func__
);
79 if (!sock_queue_rcv_skb(sk
, cskb
))
82 read_unlock(&sl
->lock
);
88 send_layer2(struct mISDNstack
*st
, struct sk_buff
*skb
)
91 struct mISDNhead
*hh
= mISDN_HEAD_P(skb
);
92 struct mISDNchannel
*ch
;
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
)) {
104 cskb
= skb_copy(skb
, GFP_KERNEL
);
107 ret
= ch
->send(ch
, cskb
);
109 if (*debug
& DEBUG_SEND_ERR
)
111 "%s ch%d prim(%x) addr(%x)"
114 hh
->prim
, ch
->addr
, ret
);
118 printk(KERN_WARNING
"%s ch%d addr %x no mem\n",
119 __func__
, ch
->nr
, ch
->addr
);
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
);
132 ret
= st
->dev
->teimgr
->ctrl(st
->dev
->teimgr
, CHECK_DATA
, skb
);
135 else if (*debug
& DEBUG_SEND_ERR
)
137 "%s ch%d mgr prim(%x) addr(%x) err %d\n",
138 __func__
, ch
->nr
, hh
->prim
, ch
->addr
, ret
);
141 mutex_unlock(&st
->lmutex
);
147 send_msg_to_layer(struct mISDNstack
*st
, struct sk_buff
*skb
)
149 struct mISDNhead
*hh
= mISDN_HEAD_P(skb
);
150 struct mISDNchannel
*ch
;
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
);
158 if (!hlist_empty(&st
->l1sock
.head
)) {
159 __net_timestamp(skb
);
160 send_socklist(&st
->l1sock
, skb
);
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
);
168 } else if (lm
== 0x4) {
169 ch
= get_channel4id(st
, hh
->id
);
171 return ch
->send(ch
, skb
);
174 "%s: dev(%s) prim(%x) id(%x) no channel\n",
175 __func__
, dev_name(&st
->dev
->dev
), hh
->prim
,
177 } else if (lm
== 0x8) {
179 ch
= get_channel4id(st
, hh
->id
);
181 return ch
->send(ch
, skb
);
184 "%s: dev(%s) prim(%x) id(%x) no channel\n",
185 __func__
, dev_name(&st
->dev
->dev
), hh
->prim
,
188 /* broadcast not handled yet */
189 printk(KERN_WARNING
"%s: dev(%s) prim %x not delivered\n",
190 __func__
, dev_name(&st
->dev
->dev
), hh
->prim
);
196 do_clear_stack(struct mISDNstack
*st
)
201 mISDNStackd(void *data
)
203 struct mISDNstack
*st
= data
;
209 sigfillset(¤t
->blocked
);
213 if (*debug
& DEBUG_MSG_THREAD
)
214 printk(KERN_DEBUG
"mISDNStackd %s started\n",
215 dev_name(&st
->dev
->dev
));
217 if (st
->notify
!= NULL
) {
218 complete(st
->notify
);
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
);
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
);
233 test_and_clear_bit(mISDN_STACK_WORK
,
235 /* test if a race happens */
236 skb
= skb_dequeue(&st
->msgq
);
239 test_and_set_bit(mISDN_STACK_WORK
,
242 #ifdef MISDN_MSG_STATS
245 err
= send_msg_to_layer(st
, skb
);
247 if (*debug
& DEBUG_SEND_ERR
)
249 "%s: %s prim(%x) id(%x) "
251 __func__
, dev_name(&st
->dev
->dev
),
252 mISDN_HEAD_PRIM(skb
),
253 mISDN_HEAD_ID(skb
), err
);
257 if (unlikely(test_bit(mISDN_STACK_STOPPED
,
259 test_and_clear_bit(mISDN_STACK_WORK
,
261 test_and_clear_bit(mISDN_STACK_RUNNING
,
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
);
270 test_and_clear_bit(mISDN_STACK_CLEARING
, &st
->status
);
271 test_and_set_bit(mISDN_STACK_RESTART
, &st
->status
);
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
,
280 if (test_bit(mISDN_STACK_ABORT
, &st
->status
))
282 if (st
->notify
!= NULL
) {
283 complete(st
->notify
);
286 #ifdef MISDN_MSG_STATS
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",
294 __func__
, dev_name(&st
->dev
->dev
), st
->status
);
295 test_and_set_bit(mISDN_STACK_ACTIVE
, &st
->status
);
297 test_and_clear_bit(mISDN_STACK_WAKEUP
, &st
->status
);
299 if (test_bit(mISDN_STACK_STOPPED
, &st
->status
)) {
300 test_and_clear_bit(mISDN_STACK_RUNNING
, &st
->status
);
301 #ifdef MISDN_MSG_STATS
306 #ifdef MISDN_MSG_STATS
307 printk(KERN_DEBUG
"mISDNStackd daemon for %s proceed %d "
308 "msg %d sleep %d stopped\n",
309 dev_name(&st
->dev
->dev
), st
->msg_cnt
, st
->sleep_cnt
,
312 "mISDNStackd daemon for %s utime(%ld) stime(%ld)\n",
313 dev_name(&st
->dev
->dev
), st
->thread
->utime
, st
->thread
->stime
);
315 "mISDNStackd daemon for %s nvcsw(%ld) nivcsw(%ld)\n",
316 dev_name(&st
->dev
->dev
), st
->thread
->nvcsw
, st
->thread
->nivcsw
);
317 printk(KERN_DEBUG
"mISDNStackd daemon for %s killed now\n",
318 dev_name(&st
->dev
->dev
));
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
);
326 if (st
->notify
!= NULL
) {
327 complete(st
->notify
);
334 l1_receive(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
338 __net_timestamp(skb
);
339 _queue_message(ch
->st
, skb
);
344 set_channel_address(struct mISDNchannel
*ch
, u_int sapi
, u_int tei
)
346 ch
->addr
= sapi
| (tei
<< 8);
350 __add_layer2(struct mISDNchannel
*ch
, struct mISDNstack
*st
)
352 list_add_tail(&ch
->list
, &st
->layer2
);
356 add_layer2(struct mISDNchannel
*ch
, struct mISDNstack
*st
)
358 mutex_lock(&st
->lmutex
);
359 __add_layer2(ch
, st
);
360 mutex_unlock(&st
->lmutex
);
364 st_own_ctrl(struct mISDNchannel
*ch
, u_int cmd
, void *arg
)
366 if (!ch
->st
|| ch
->st
->layer1
)
368 return ch
->st
->layer1
->ctrl(ch
->st
->layer1
, cmd
, arg
);
372 create_stack(struct mISDNdevice
*dev
)
374 struct mISDNstack
*newst
;
376 DECLARE_COMPLETION_ONSTACK(done
);
378 newst
= kzalloc(sizeof(struct mISDNstack
), GFP_KERNEL
);
380 printk(KERN_ERR
"kmalloc mISDN_stack failed\n");
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
);
391 err
= create_teimanager(dev
);
393 printk(KERN_ERR
"kmalloc teimanager failed\n");
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
)
408 printk(KERN_DEBUG
"%s: st(%s)\n", __func__
,
409 dev_name(&newst
->dev
->dev
));
410 newst
->notify
= &done
;
411 newst
->thread
= kthread_run(mISDNStackd
, (void *)newst
, "mISDN_%s",
412 dev_name(&newst
->dev
->dev
));
413 if (IS_ERR(newst
->thread
)) {
414 err
= PTR_ERR(newst
->thread
);
416 "mISDN:cannot create kernel thread for %s (%d)\n",
417 dev_name(&newst
->dev
->dev
), err
);
418 delete_teimanager(dev
->teimgr
);
421 wait_for_completion(&done
);
426 connect_layer1(struct mISDNdevice
*dev
, struct mISDNchannel
*ch
,
427 u_int protocol
, struct sockaddr_mISDN
*adr
)
429 struct mISDN_sock
*msk
= container_of(ch
, struct mISDN_sock
, ch
);
430 struct channel_req rq
;
434 if (*debug
& DEBUG_CORE_FUNC
)
435 printk(KERN_DEBUG
"%s: %s proto(%x) adr(%d %d %d %d)\n",
436 __func__
, dev_name(&dev
->dev
), protocol
, adr
->dev
,
437 adr
->channel
, adr
->sapi
, adr
->tei
);
443 ch
->recv
= mISDN_queue_message
;
444 ch
->peer
= &dev
->D
.st
->own
;
446 rq
.protocol
= protocol
;
447 rq
.adr
.channel
= adr
->channel
;
448 err
= dev
->D
.ctrl(&dev
->D
, OPEN_CHANNEL
, &rq
);
449 printk(KERN_DEBUG
"%s: ret %d (dev %d)\n", __func__
, 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
);
464 connect_Bstack(struct mISDNdevice
*dev
, struct mISDNchannel
*ch
,
465 u_int protocol
, struct sockaddr_mISDN
*adr
)
467 struct channel_req rq
, rq2
;
469 struct Bprotocol
*bp
;
471 if (*debug
& DEBUG_CORE_FUNC
)
472 printk(KERN_DEBUG
"%s: %s proto(%x) adr(%d %d %d %d)\n",
473 __func__
, dev_name(&dev
->dev
), protocol
,
474 adr
->dev
, adr
->channel
, adr
->sapi
,
477 pmask
= 1 << (protocol
& ISDN_P_B_MASK
);
478 if (pmask
& dev
->Bprotocols
) {
479 rq
.protocol
= protocol
;
481 err
= dev
->D
.ctrl(&dev
->D
, OPEN_CHANNEL
, &rq
);
484 ch
->recv
= rq
.ch
->send
;
486 rq
.ch
->recv
= ch
->send
;
488 rq
.ch
->st
= dev
->D
.st
;
490 bp
= get_Bprotocol4mask(pmask
);
493 rq2
.protocol
= protocol
;
496 err
= bp
->create(&rq2
);
499 ch
->recv
= rq2
.ch
->send
;
501 rq2
.ch
->st
= dev
->D
.st
;
502 rq
.protocol
= rq2
.protocol
;
504 err
= dev
->D
.ctrl(&dev
->D
, OPEN_CHANNEL
, &rq
);
506 rq2
.ch
->ctrl(rq2
.ch
, CLOSE_CHANNEL
, NULL
);
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
;
515 ch
->protocol
= protocol
;
521 create_l2entity(struct mISDNdevice
*dev
, struct mISDNchannel
*ch
,
522 u_int protocol
, struct sockaddr_mISDN
*adr
)
524 struct channel_req rq
;
527 if (*debug
& DEBUG_CORE_FUNC
)
528 printk(KERN_DEBUG
"%s: %s proto(%x) adr(%d %d %d %d)\n",
529 __func__
, dev_name(&dev
->dev
), protocol
,
530 adr
->dev
, adr
->channel
, adr
->sapi
,
532 rq
.protocol
= ISDN_P_TE_S0
;
533 if (dev
->Dprotocols
& (1 << ISDN_P_TE_E1
))
534 rq
.protocol
= ISDN_P_TE_E1
;
537 rq
.protocol
= ISDN_P_NT_S0
;
538 if (dev
->Dprotocols
& (1 << ISDN_P_NT_E1
))
539 rq
.protocol
= ISDN_P_NT_E1
;
541 ch
->recv
= mISDN_queue_message
;
542 ch
->peer
= &dev
->D
.st
->own
;
545 err
= dev
->D
.ctrl(&dev
->D
, OPEN_CHANNEL
, &rq
);
546 printk(KERN_DEBUG
"%s: ret 1 %d\n", __func__
, err
);
549 rq
.protocol
= protocol
;
552 err
= dev
->teimgr
->ctrl(dev
->teimgr
, OPEN_CHANNEL
, &rq
);
553 printk(KERN_DEBUG
"%s: ret 2 %d\n", __func__
, err
);
555 if ((protocol
== ISDN_P_LAPD_NT
) && !rq
.ch
)
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 */
564 err
= -EPROTONOSUPPORT
;
570 delete_channel(struct mISDNchannel
*ch
)
572 struct mISDN_sock
*msk
= container_of(ch
, struct mISDN_sock
, ch
);
573 struct mISDNchannel
*pch
;
576 printk(KERN_WARNING
"%s: no stack\n", __func__
);
579 if (*debug
& DEBUG_CORE_FUNC
)
580 printk(KERN_DEBUG
"%s: st(%s) protocol(%x)\n", __func__
,
581 dev_name(&ch
->st
->dev
->dev
), ch
->protocol
);
582 if (ch
->protocol
>= ISDN_P_B_START
) {
584 ch
->peer
->ctrl(ch
->peer
, CLOSE_CHANNEL
, NULL
);
589 switch (ch
->protocol
) {
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
);
600 pch
= get_channel4id(ch
->st
, ch
->nr
);
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
);
609 printk(KERN_WARNING
"%s: no l2 channel\n",
613 pch
= ch
->st
->dev
->teimgr
;
615 pch
->ctrl(pch
, CLOSE_CHANNEL
, NULL
);
617 printk(KERN_WARNING
"%s: no l2 channel\n",
627 delete_stack(struct mISDNdevice
*dev
)
629 struct mISDNstack
*st
= dev
->D
.st
;
630 DECLARE_COMPLETION_ONSTACK(done
);
632 if (*debug
& DEBUG_CORE_FUNC
)
633 printk(KERN_DEBUG
"%s: st(%s)\n", __func__
,
634 dev_name(&st
->dev
->dev
));
636 delete_teimanager(dev
->teimgr
);
639 printk(KERN_WARNING
"%s: notifier in use\n",
641 complete(st
->notify
);
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
);
649 if (!list_empty(&st
->layer2
))
650 printk(KERN_WARNING
"%s: layer2 list not empty\n",
652 if (!hlist_empty(&st
->l1sock
.head
))
653 printk(KERN_WARNING
"%s: layer1 list not empty\n",
659 mISDN_initstack(u_int
*dp
)