stack.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  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. #include <linux/slab.h>
  18. #include <linux/mISDNif.h>
  19. #include <linux/kthread.h>
  20. #include "core.h"
  21. static u_int *debug;
  22. static inline void
  23. _queue_message(struct mISDNstack *st, struct sk_buff *skb)
  24. {
  25. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  26. if (*debug & DEBUG_QUEUE_FUNC)
  27. printk(KERN_DEBUG "%s prim(%x) id(%x) %p\n",
  28. __func__, hh->prim, hh->id, skb);
  29. skb_queue_tail(&st->msgq, skb);
  30. if (likely(!test_bit(mISDN_STACK_STOPPED, &st->status))) {
  31. test_and_set_bit(mISDN_STACK_WORK, &st->status);
  32. wake_up_interruptible(&st->workq);
  33. }
  34. }
  35. static int
  36. mISDN_queue_message(struct mISDNchannel *ch, struct sk_buff *skb)
  37. {
  38. _queue_message(ch->st, skb);
  39. return 0;
  40. }
  41. static struct mISDNchannel *
  42. get_channel4id(struct mISDNstack *st, u_int id)
  43. {
  44. struct mISDNchannel *ch;
  45. mutex_lock(&st->lmutex);
  46. list_for_each_entry(ch, &st->layer2, list) {
  47. if (id == ch->nr)
  48. goto unlock;
  49. }
  50. ch = NULL;
  51. unlock:
  52. mutex_unlock(&st->lmutex);
  53. return ch;
  54. }
  55. static void
  56. send_socklist(struct mISDN_sock_list *sl, struct sk_buff *skb)
  57. {
  58. struct hlist_node *node;
  59. struct sock *sk;
  60. struct sk_buff *cskb = NULL;
  61. read_lock(&sl->lock);
  62. sk_for_each(sk, node, &sl->head) {
  63. if (sk->sk_state != MISDN_BOUND)
  64. continue;
  65. if (!cskb)
  66. cskb = skb_copy(skb, GFP_KERNEL);
  67. if (!cskb) {
  68. printk(KERN_WARNING "%s no skb\n", __func__);
  69. break;
  70. }
  71. if (!sock_queue_rcv_skb(sk, cskb))
  72. cskb = NULL;
  73. }
  74. read_unlock(&sl->lock);
  75. if (cskb)
  76. dev_kfree_skb(cskb);
  77. }
  78. static void
  79. send_layer2(struct mISDNstack *st, struct sk_buff *skb)
  80. {
  81. struct sk_buff *cskb;
  82. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  83. struct mISDNchannel *ch;
  84. int ret;
  85. if (!st)
  86. return;
  87. mutex_lock(&st->lmutex);
  88. if ((hh->id & MISDN_ID_ADDR_MASK) == MISDN_ID_ANY) { /* L2 for all */
  89. list_for_each_entry(ch, &st->layer2, list) {
  90. if (list_is_last(&ch->list, &st->layer2)) {
  91. cskb = skb;
  92. skb = NULL;
  93. } else {
  94. cskb = skb_copy(skb, GFP_KERNEL);
  95. }
  96. if (cskb) {
  97. ret = ch->send(ch, cskb);
  98. if (ret) {
  99. if (*debug & DEBUG_SEND_ERR)
  100. printk(KERN_DEBUG
  101. "%s ch%d prim(%x) addr(%x)"
  102. " err %d\n",
  103. __func__, ch->nr,
  104. hh->prim, ch->addr, ret);
  105. dev_kfree_skb(cskb);
  106. }
  107. } else {
  108. printk(KERN_WARNING "%s ch%d addr %x no mem\n",
  109. __func__, ch->nr, ch->addr);
  110. goto out;
  111. }
  112. }
  113. } else {
  114. list_for_each_entry(ch, &st->layer2, list) {
  115. if ((hh->id & MISDN_ID_ADDR_MASK) == ch->addr) {
  116. ret = ch->send(ch, skb);
  117. if (!ret)
  118. skb = NULL;
  119. goto out;
  120. }
  121. }
  122. ret = st->dev->teimgr->ctrl(st->dev->teimgr, CHECK_DATA, skb);
  123. if (!ret)
  124. skb = NULL;
  125. else if (*debug & DEBUG_SEND_ERR)
  126. printk(KERN_DEBUG
  127. "%s ch%d mgr prim(%x) addr(%x) err %d\n",
  128. __func__, ch->nr, hh->prim, ch->addr, ret);
  129. }
  130. out:
  131. mutex_unlock(&st->lmutex);
  132. if (skb)
  133. dev_kfree_skb(skb);
  134. }
  135. static inline int
  136. send_msg_to_layer(struct mISDNstack *st, struct sk_buff *skb)
  137. {
  138. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  139. struct mISDNchannel *ch;
  140. int lm;
  141. lm = hh->prim & MISDN_LAYERMASK;
  142. if (*debug & DEBUG_QUEUE_FUNC)
  143. printk(KERN_DEBUG "%s prim(%x) id(%x) %p\n",
  144. __func__, hh->prim, hh->id, skb);
  145. if (lm == 0x1) {
  146. if (!hlist_empty(&st->l1sock.head)) {
  147. __net_timestamp(skb);
  148. send_socklist(&st->l1sock, skb);
  149. }
  150. return st->layer1->send(st->layer1, skb);
  151. } else if (lm == 0x2) {
  152. if (!hlist_empty(&st->l1sock.head))
  153. send_socklist(&st->l1sock, skb);
  154. send_layer2(st, skb);
  155. return 0;
  156. } else if (lm == 0x4) {
  157. ch = get_channel4id(st, hh->id);
  158. if (ch)
  159. return ch->send(ch, skb);
  160. else
  161. printk(KERN_WARNING
  162. "%s: dev(%s) prim(%x) id(%x) no channel\n",
  163. __func__, dev_name(&st->dev->dev), hh->prim,
  164. hh->id);
  165. } else if (lm == 0x8) {
  166. WARN_ON(lm == 0x8);
  167. ch = get_channel4id(st, hh->id);
  168. if (ch)
  169. return ch->send(ch, skb);
  170. else
  171. printk(KERN_WARNING
  172. "%s: dev(%s) prim(%x) id(%x) no channel\n",
  173. __func__, dev_name(&st->dev->dev), hh->prim,
  174. hh->id);
  175. } else {
  176. /* broadcast not handled yet */
  177. printk(KERN_WARNING "%s: dev(%s) prim %x not delivered\n",
  178. __func__, dev_name(&st->dev->dev), hh->prim);
  179. }
  180. return -ESRCH;
  181. }
  182. static void
  183. do_clear_stack(struct mISDNstack *st)
  184. {
  185. }
  186. static int
  187. mISDNStackd(void *data)
  188. {
  189. struct mISDNstack *st = data;
  190. int err = 0;
  191. sigfillset(&current->blocked);
  192. if (*debug & DEBUG_MSG_THREAD)
  193. printk(KERN_DEBUG "mISDNStackd %s started\n",
  194. dev_name(&st->dev->dev));
  195. if (st->notify != NULL) {
  196. complete(st->notify);
  197. st->notify = NULL;
  198. }
  199. for (;;) {
  200. struct sk_buff *skb;
  201. if (unlikely(test_bit(mISDN_STACK_STOPPED, &st->status))) {
  202. test_and_clear_bit(mISDN_STACK_WORK, &st->status);
  203. test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
  204. } else
  205. test_and_set_bit(mISDN_STACK_RUNNING, &st->status);
  206. while (test_bit(mISDN_STACK_WORK, &st->status)) {
  207. skb = skb_dequeue(&st->msgq);
  208. if (!skb) {
  209. test_and_clear_bit(mISDN_STACK_WORK,
  210. &st->status);
  211. /* test if a race happens */
  212. skb = skb_dequeue(&st->msgq);
  213. if (!skb)
  214. continue;
  215. test_and_set_bit(mISDN_STACK_WORK,
  216. &st->status);
  217. }
  218. #ifdef MISDN_MSG_STATS
  219. st->msg_cnt++;
  220. #endif
  221. err = send_msg_to_layer(st, skb);
  222. if (unlikely(err)) {
  223. if (*debug & DEBUG_SEND_ERR)
  224. printk(KERN_DEBUG
  225. "%s: %s prim(%x) id(%x) "
  226. "send call(%d)\n",
  227. __func__, dev_name(&st->dev->dev),
  228. mISDN_HEAD_PRIM(skb),
  229. mISDN_HEAD_ID(skb), err);
  230. dev_kfree_skb(skb);
  231. continue;
  232. }
  233. if (unlikely(test_bit(mISDN_STACK_STOPPED,
  234. &st->status))) {
  235. test_and_clear_bit(mISDN_STACK_WORK,
  236. &st->status);
  237. test_and_clear_bit(mISDN_STACK_RUNNING,
  238. &st->status);
  239. break;
  240. }
  241. }
  242. if (test_bit(mISDN_STACK_CLEARING, &st->status)) {
  243. test_and_set_bit(mISDN_STACK_STOPPED, &st->status);
  244. test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
  245. do_clear_stack(st);
  246. test_and_clear_bit(mISDN_STACK_CLEARING, &st->status);
  247. test_and_set_bit(mISDN_STACK_RESTART, &st->status);
  248. }
  249. if (test_and_clear_bit(mISDN_STACK_RESTART, &st->status)) {
  250. test_and_clear_bit(mISDN_STACK_STOPPED, &st->status);
  251. test_and_set_bit(mISDN_STACK_RUNNING, &st->status);
  252. if (!skb_queue_empty(&st->msgq))
  253. test_and_set_bit(mISDN_STACK_WORK,
  254. &st->status);
  255. }
  256. if (test_bit(mISDN_STACK_ABORT, &st->status))
  257. break;
  258. if (st->notify != NULL) {
  259. complete(st->notify);
  260. st->notify = NULL;
  261. }
  262. #ifdef MISDN_MSG_STATS
  263. st->sleep_cnt++;
  264. #endif
  265. test_and_clear_bit(mISDN_STACK_ACTIVE, &st->status);
  266. wait_event_interruptible(st->workq, (st->status &
  267. mISDN_STACK_ACTION_MASK));
  268. if (*debug & DEBUG_MSG_THREAD)
  269. printk(KERN_DEBUG "%s: %s wake status %08lx\n",
  270. __func__, dev_name(&st->dev->dev), st->status);
  271. test_and_set_bit(mISDN_STACK_ACTIVE, &st->status);
  272. test_and_clear_bit(mISDN_STACK_WAKEUP, &st->status);
  273. if (test_bit(mISDN_STACK_STOPPED, &st->status)) {
  274. test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
  275. #ifdef MISDN_MSG_STATS
  276. st->stopped_cnt++;
  277. #endif
  278. }
  279. }
  280. #ifdef MISDN_MSG_STATS
  281. printk(KERN_DEBUG "mISDNStackd daemon for %s proceed %d "
  282. "msg %d sleep %d stopped\n",
  283. dev_name(&st->dev->dev), st->msg_cnt, st->sleep_cnt,
  284. st->stopped_cnt);
  285. printk(KERN_DEBUG
  286. "mISDNStackd daemon for %s utime(%ld) stime(%ld)\n",
  287. dev_name(&st->dev->dev), st->thread->utime, st->thread->stime);
  288. printk(KERN_DEBUG
  289. "mISDNStackd daemon for %s nvcsw(%ld) nivcsw(%ld)\n",
  290. dev_name(&st->dev->dev), st->thread->nvcsw, st->thread->nivcsw);
  291. printk(KERN_DEBUG "mISDNStackd daemon for %s killed now\n",
  292. dev_name(&st->dev->dev));
  293. #endif
  294. test_and_set_bit(mISDN_STACK_KILLED, &st->status);
  295. test_and_clear_bit(mISDN_STACK_RUNNING, &st->status);
  296. test_and_clear_bit(mISDN_STACK_ACTIVE, &st->status);
  297. test_and_clear_bit(mISDN_STACK_ABORT, &st->status);
  298. skb_queue_purge(&st->msgq);
  299. st->thread = NULL;
  300. if (st->notify != NULL) {
  301. complete(st->notify);
  302. st->notify = NULL;
  303. }
  304. return 0;
  305. }
  306. static int
  307. l1_receive(struct mISDNchannel *ch, struct sk_buff *skb)
  308. {
  309. if (!ch->st)
  310. return -ENODEV;
  311. __net_timestamp(skb);
  312. _queue_message(ch->st, skb);
  313. return 0;
  314. }
  315. void
  316. set_channel_address(struct mISDNchannel *ch, u_int sapi, u_int tei)
  317. {
  318. ch->addr = sapi | (tei << 8);
  319. }
  320. void
  321. __add_layer2(struct mISDNchannel *ch, struct mISDNstack *st)
  322. {
  323. list_add_tail(&ch->list, &st->layer2);
  324. }
  325. void
  326. add_layer2(struct mISDNchannel *ch, struct mISDNstack *st)
  327. {
  328. mutex_lock(&st->lmutex);
  329. __add_layer2(ch, st);
  330. mutex_unlock(&st->lmutex);
  331. }
  332. static int
  333. st_own_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  334. {
  335. if (!ch->st || !ch->st->layer1)
  336. return -EINVAL;
  337. return ch->st->layer1->ctrl(ch->st->layer1, cmd, arg);
  338. }
  339. int
  340. create_stack(struct mISDNdevice *dev)
  341. {
  342. struct mISDNstack *newst;
  343. int err;
  344. DECLARE_COMPLETION_ONSTACK(done);
  345. newst = kzalloc(sizeof(struct mISDNstack), GFP_KERNEL);
  346. if (!newst) {
  347. printk(KERN_ERR "kmalloc mISDN_stack failed\n");
  348. return -ENOMEM;
  349. }
  350. newst->dev = dev;
  351. INIT_LIST_HEAD(&newst->layer2);
  352. INIT_HLIST_HEAD(&newst->l1sock.head);
  353. rwlock_init(&newst->l1sock.lock);
  354. init_waitqueue_head(&newst->workq);
  355. skb_queue_head_init(&newst->msgq);
  356. mutex_init(&newst->lmutex);
  357. dev->D.st = newst;
  358. err = create_teimanager(dev);
  359. if (err) {
  360. printk(KERN_ERR "kmalloc teimanager failed\n");
  361. kfree(newst);
  362. return err;
  363. }
  364. dev->teimgr->peer = &newst->own;
  365. dev->teimgr->recv = mISDN_queue_message;
  366. dev->teimgr->st = newst;
  367. newst->layer1 = &dev->D;
  368. dev->D.recv = l1_receive;
  369. dev->D.peer = &newst->own;
  370. newst->own.st = newst;
  371. newst->own.ctrl = st_own_ctrl;
  372. newst->own.send = mISDN_queue_message;
  373. newst->own.recv = mISDN_queue_message;
  374. if (*debug & DEBUG_CORE_FUNC)
  375. printk(KERN_DEBUG "%s: st(%s)\n", __func__,
  376. dev_name(&newst->dev->dev));
  377. newst->notify = &done;
  378. newst->thread = kthread_run(mISDNStackd, (void *)newst, "mISDN_%s",
  379. dev_name(&newst->dev->dev));
  380. if (IS_ERR(newst->thread)) {
  381. err = PTR_ERR(newst->thread);
  382. printk(KERN_ERR
  383. "mISDN:cannot create kernel thread for %s (%d)\n",
  384. dev_name(&newst->dev->dev), err);
  385. delete_teimanager(dev->teimgr);
  386. kfree(newst);
  387. } else
  388. wait_for_completion(&done);
  389. return err;
  390. }
  391. int
  392. connect_layer1(struct mISDNdevice *dev, struct mISDNchannel *ch,
  393. u_int protocol, struct sockaddr_mISDN *adr)
  394. {
  395. struct mISDN_sock *msk = container_of(ch, struct mISDN_sock, ch);
  396. struct channel_req rq;
  397. int err;
  398. if (*debug & DEBUG_CORE_FUNC)
  399. printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
  400. __func__, dev_name(&dev->dev), protocol, adr->dev,
  401. adr->channel, adr->sapi, adr->tei);
  402. switch (protocol) {
  403. case ISDN_P_NT_S0:
  404. case ISDN_P_NT_E1:
  405. case ISDN_P_TE_S0:
  406. case ISDN_P_TE_E1:
  407. ch->recv = mISDN_queue_message;
  408. ch->peer = &dev->D.st->own;
  409. ch->st = dev->D.st;
  410. rq.protocol = protocol;
  411. rq.adr.channel = adr->channel;
  412. err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
  413. printk(KERN_DEBUG "%s: ret %d (dev %d)\n", __func__, err,
  414. dev->id);
  415. if (err)
  416. return err;
  417. write_lock_bh(&dev->D.st->l1sock.lock);
  418. sk_add_node(&msk->sk, &dev->D.st->l1sock.head);
  419. write_unlock_bh(&dev->D.st->l1sock.lock);
  420. break;
  421. default:
  422. return -ENOPROTOOPT;
  423. }
  424. return 0;
  425. }
  426. int
  427. connect_Bstack(struct mISDNdevice *dev, struct mISDNchannel *ch,
  428. u_int protocol, struct sockaddr_mISDN *adr)
  429. {
  430. struct channel_req rq, rq2;
  431. int pmask, err;
  432. struct Bprotocol *bp;
  433. if (*debug & DEBUG_CORE_FUNC)
  434. printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
  435. __func__, dev_name(&dev->dev), protocol,
  436. adr->dev, adr->channel, adr->sapi,
  437. adr->tei);
  438. ch->st = dev->D.st;
  439. pmask = 1 << (protocol & ISDN_P_B_MASK);
  440. if (pmask & dev->Bprotocols) {
  441. rq.protocol = protocol;
  442. rq.adr = *adr;
  443. err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
  444. if (err)
  445. return err;
  446. ch->recv = rq.ch->send;
  447. ch->peer = rq.ch;
  448. rq.ch->recv = ch->send;
  449. rq.ch->peer = ch;
  450. rq.ch->st = dev->D.st;
  451. } else {
  452. bp = get_Bprotocol4mask(pmask);
  453. if (!bp)
  454. return -ENOPROTOOPT;
  455. rq2.protocol = protocol;
  456. rq2.adr = *adr;
  457. rq2.ch = ch;
  458. err = bp->create(&rq2);
  459. if (err)
  460. return err;
  461. ch->recv = rq2.ch->send;
  462. ch->peer = rq2.ch;
  463. rq2.ch->st = dev->D.st;
  464. rq.protocol = rq2.protocol;
  465. rq.adr = *adr;
  466. err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
  467. if (err) {
  468. rq2.ch->ctrl(rq2.ch, CLOSE_CHANNEL, NULL);
  469. return err;
  470. }
  471. rq2.ch->recv = rq.ch->send;
  472. rq2.ch->peer = rq.ch;
  473. rq.ch->recv = rq2.ch->send;
  474. rq.ch->peer = rq2.ch;
  475. rq.ch->st = dev->D.st;
  476. }
  477. ch->protocol = protocol;
  478. ch->nr = rq.ch->nr;
  479. return 0;
  480. }
  481. int
  482. create_l2entity(struct mISDNdevice *dev, struct mISDNchannel *ch,
  483. u_int protocol, struct sockaddr_mISDN *adr)
  484. {
  485. struct channel_req rq;
  486. int err;
  487. if (*debug & DEBUG_CORE_FUNC)
  488. printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
  489. __func__, dev_name(&dev->dev), protocol,
  490. adr->dev, adr->channel, adr->sapi,
  491. adr->tei);
  492. rq.protocol = ISDN_P_TE_S0;
  493. if (dev->Dprotocols & (1 << ISDN_P_TE_E1))
  494. rq.protocol = ISDN_P_TE_E1;
  495. switch (protocol) {
  496. case ISDN_P_LAPD_NT:
  497. rq.protocol = ISDN_P_NT_S0;
  498. if (dev->Dprotocols & (1 << ISDN_P_NT_E1))
  499. rq.protocol = ISDN_P_NT_E1;
  500. case ISDN_P_LAPD_TE:
  501. ch->recv = mISDN_queue_message;
  502. ch->peer = &dev->D.st->own;
  503. ch->st = dev->D.st;
  504. rq.adr.channel = 0;
  505. err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq);
  506. printk(KERN_DEBUG "%s: ret 1 %d\n", __func__, err);
  507. if (err)
  508. break;
  509. rq.protocol = protocol;
  510. rq.adr = *adr;
  511. rq.ch = ch;
  512. err = dev->teimgr->ctrl(dev->teimgr, OPEN_CHANNEL, &rq);
  513. printk(KERN_DEBUG "%s: ret 2 %d\n", __func__, err);
  514. if (!err) {
  515. if ((protocol == ISDN_P_LAPD_NT) && !rq.ch)
  516. break;
  517. add_layer2(rq.ch, dev->D.st);
  518. rq.ch->recv = mISDN_queue_message;
  519. rq.ch->peer = &dev->D.st->own;
  520. rq.ch->ctrl(rq.ch, OPEN_CHANNEL, NULL); /* can't fail */
  521. }
  522. break;
  523. default:
  524. err = -EPROTONOSUPPORT;
  525. }
  526. return err;
  527. }
  528. void
  529. delete_channel(struct mISDNchannel *ch)
  530. {
  531. struct mISDN_sock *msk = container_of(ch, struct mISDN_sock, ch);
  532. struct mISDNchannel *pch;
  533. if (!ch->st) {
  534. printk(KERN_WARNING "%s: no stack\n", __func__);
  535. return;
  536. }
  537. if (*debug & DEBUG_CORE_FUNC)
  538. printk(KERN_DEBUG "%s: st(%s) protocol(%x)\n", __func__,
  539. dev_name(&ch->st->dev->dev), ch->protocol);
  540. if (ch->protocol >= ISDN_P_B_START) {
  541. if (ch->peer) {
  542. ch->peer->ctrl(ch->peer, CLOSE_CHANNEL, NULL);
  543. ch->peer = NULL;
  544. }
  545. return;
  546. }
  547. switch (ch->protocol) {
  548. case ISDN_P_NT_S0:
  549. case ISDN_P_TE_S0:
  550. case ISDN_P_NT_E1:
  551. case ISDN_P_TE_E1:
  552. write_lock_bh(&ch->st->l1sock.lock);
  553. sk_del_node_init(&msk->sk);
  554. write_unlock_bh(&ch->st->l1sock.lock);
  555. ch->st->dev->D.ctrl(&ch->st->dev->D, CLOSE_CHANNEL, NULL);
  556. break;
  557. case ISDN_P_LAPD_TE:
  558. pch = get_channel4id(ch->st, ch->nr);
  559. if (pch) {
  560. mutex_lock(&ch->st->lmutex);
  561. list_del(&pch->list);
  562. mutex_unlock(&ch->st->lmutex);
  563. pch->ctrl(pch, CLOSE_CHANNEL, NULL);
  564. pch = ch->st->dev->teimgr;
  565. pch->ctrl(pch, CLOSE_CHANNEL, NULL);
  566. } else
  567. printk(KERN_WARNING "%s: no l2 channel\n",
  568. __func__);
  569. break;
  570. case ISDN_P_LAPD_NT:
  571. pch = ch->st->dev->teimgr;
  572. if (pch) {
  573. pch->ctrl(pch, CLOSE_CHANNEL, NULL);
  574. } else
  575. printk(KERN_WARNING "%s: no l2 channel\n",
  576. __func__);
  577. break;
  578. default:
  579. break;
  580. }
  581. return;
  582. }
  583. void
  584. delete_stack(struct mISDNdevice *dev)
  585. {
  586. struct mISDNstack *st = dev->D.st;
  587. DECLARE_COMPLETION_ONSTACK(done);
  588. if (*debug & DEBUG_CORE_FUNC)
  589. printk(KERN_DEBUG "%s: st(%s)\n", __func__,
  590. dev_name(&st->dev->dev));
  591. if (dev->teimgr)
  592. delete_teimanager(dev->teimgr);
  593. if (st->thread) {
  594. if (st->notify) {
  595. printk(KERN_WARNING "%s: notifier in use\n",
  596. __func__);
  597. complete(st->notify);
  598. }
  599. st->notify = &done;
  600. test_and_set_bit(mISDN_STACK_ABORT, &st->status);
  601. test_and_set_bit(mISDN_STACK_WAKEUP, &st->status);
  602. wake_up_interruptible(&st->workq);
  603. wait_for_completion(&done);
  604. }
  605. if (!list_empty(&st->layer2))
  606. printk(KERN_WARNING "%s: layer2 list not empty\n",
  607. __func__);
  608. if (!hlist_empty(&st->l1sock.head))
  609. printk(KERN_WARNING "%s: layer1 list not empty\n",
  610. __func__);
  611. kfree(st);
  612. }
  613. void
  614. mISDN_initstack(u_int *dp)
  615. {
  616. debug = dp;
  617. }