stack.c 16 KB

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