stack.c 16 KB

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