hwchannel.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/module.h>
  18. #include <linux/mISDNhw.h>
  19. static void
  20. dchannel_bh(struct work_struct *ws)
  21. {
  22. struct dchannel *dch = container_of(ws, struct dchannel, workq);
  23. struct sk_buff *skb;
  24. int err;
  25. if (test_and_clear_bit(FLG_RECVQUEUE, &dch->Flags)) {
  26. while ((skb = skb_dequeue(&dch->rqueue))) {
  27. if (likely(dch->dev.D.peer)) {
  28. err = dch->dev.D.recv(dch->dev.D.peer, skb);
  29. if (err)
  30. dev_kfree_skb(skb);
  31. } else
  32. dev_kfree_skb(skb);
  33. }
  34. }
  35. if (test_and_clear_bit(FLG_PHCHANGE, &dch->Flags)) {
  36. if (dch->phfunc)
  37. dch->phfunc(dch);
  38. }
  39. }
  40. static void
  41. bchannel_bh(struct work_struct *ws)
  42. {
  43. struct bchannel *bch = container_of(ws, struct bchannel, workq);
  44. struct sk_buff *skb;
  45. int err;
  46. if (test_and_clear_bit(FLG_RECVQUEUE, &bch->Flags)) {
  47. while ((skb = skb_dequeue(&bch->rqueue))) {
  48. if (bch->rcount >= 64)
  49. printk(KERN_WARNING "B-channel %p receive "
  50. "queue if full, but empties...\n", bch);
  51. bch->rcount--;
  52. if (likely(bch->ch.peer)) {
  53. err = bch->ch.recv(bch->ch.peer, skb);
  54. if (err)
  55. dev_kfree_skb(skb);
  56. } else
  57. dev_kfree_skb(skb);
  58. }
  59. }
  60. }
  61. int
  62. mISDN_initdchannel(struct dchannel *ch, int maxlen, void *phf)
  63. {
  64. test_and_set_bit(FLG_HDLC, &ch->Flags);
  65. ch->maxlen = maxlen;
  66. ch->hw = NULL;
  67. ch->rx_skb = NULL;
  68. ch->tx_skb = NULL;
  69. ch->tx_idx = 0;
  70. ch->phfunc = phf;
  71. skb_queue_head_init(&ch->squeue);
  72. skb_queue_head_init(&ch->rqueue);
  73. INIT_LIST_HEAD(&ch->dev.bchannels);
  74. INIT_WORK(&ch->workq, dchannel_bh);
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(mISDN_initdchannel);
  78. int
  79. mISDN_initbchannel(struct bchannel *ch, int maxlen)
  80. {
  81. ch->Flags = 0;
  82. ch->maxlen = maxlen;
  83. ch->hw = NULL;
  84. ch->rx_skb = NULL;
  85. ch->tx_skb = NULL;
  86. ch->tx_idx = 0;
  87. skb_queue_head_init(&ch->rqueue);
  88. ch->rcount = 0;
  89. ch->next_skb = NULL;
  90. INIT_WORK(&ch->workq, bchannel_bh);
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(mISDN_initbchannel);
  94. int
  95. mISDN_freedchannel(struct dchannel *ch)
  96. {
  97. if (ch->tx_skb) {
  98. dev_kfree_skb(ch->tx_skb);
  99. ch->tx_skb = NULL;
  100. }
  101. if (ch->rx_skb) {
  102. dev_kfree_skb(ch->rx_skb);
  103. ch->rx_skb = NULL;
  104. }
  105. skb_queue_purge(&ch->squeue);
  106. skb_queue_purge(&ch->rqueue);
  107. flush_scheduled_work();
  108. return 0;
  109. }
  110. EXPORT_SYMBOL(mISDN_freedchannel);
  111. int
  112. mISDN_freebchannel(struct bchannel *ch)
  113. {
  114. if (ch->tx_skb) {
  115. dev_kfree_skb(ch->tx_skb);
  116. ch->tx_skb = NULL;
  117. }
  118. if (ch->rx_skb) {
  119. dev_kfree_skb(ch->rx_skb);
  120. ch->rx_skb = NULL;
  121. }
  122. if (ch->next_skb) {
  123. dev_kfree_skb(ch->next_skb);
  124. ch->next_skb = NULL;
  125. }
  126. skb_queue_purge(&ch->rqueue);
  127. ch->rcount = 0;
  128. flush_scheduled_work();
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(mISDN_freebchannel);
  132. static inline u_int
  133. get_sapi_tei(u_char *p)
  134. {
  135. u_int sapi, tei;
  136. sapi = *p >> 2;
  137. tei = p[1] >> 1;
  138. return sapi | (tei << 8);
  139. }
  140. void
  141. recv_Dchannel(struct dchannel *dch)
  142. {
  143. struct mISDNhead *hh;
  144. if (dch->rx_skb->len < 2) { /* at least 2 for sapi / tei */
  145. dev_kfree_skb(dch->rx_skb);
  146. dch->rx_skb = NULL;
  147. return;
  148. }
  149. hh = mISDN_HEAD_P(dch->rx_skb);
  150. hh->prim = PH_DATA_IND;
  151. hh->id = get_sapi_tei(dch->rx_skb->data);
  152. skb_queue_tail(&dch->rqueue, dch->rx_skb);
  153. dch->rx_skb = NULL;
  154. schedule_event(dch, FLG_RECVQUEUE);
  155. }
  156. EXPORT_SYMBOL(recv_Dchannel);
  157. void
  158. recv_Bchannel(struct bchannel *bch)
  159. {
  160. struct mISDNhead *hh;
  161. hh = mISDN_HEAD_P(bch->rx_skb);
  162. hh->prim = PH_DATA_IND;
  163. hh->id = MISDN_ID_ANY;
  164. if (bch->rcount >= 64) {
  165. dev_kfree_skb(bch->rx_skb);
  166. bch->rx_skb = NULL;
  167. return;
  168. }
  169. bch->rcount++;
  170. skb_queue_tail(&bch->rqueue, bch->rx_skb);
  171. bch->rx_skb = NULL;
  172. schedule_event(bch, FLG_RECVQUEUE);
  173. }
  174. EXPORT_SYMBOL(recv_Bchannel);
  175. void
  176. recv_Dchannel_skb(struct dchannel *dch, struct sk_buff *skb)
  177. {
  178. skb_queue_tail(&dch->rqueue, skb);
  179. schedule_event(dch, FLG_RECVQUEUE);
  180. }
  181. EXPORT_SYMBOL(recv_Dchannel_skb);
  182. void
  183. recv_Bchannel_skb(struct bchannel *bch, struct sk_buff *skb)
  184. {
  185. if (bch->rcount >= 64) {
  186. dev_kfree_skb(skb);
  187. return;
  188. }
  189. bch->rcount++;
  190. skb_queue_tail(&bch->rqueue, skb);
  191. schedule_event(bch, FLG_RECVQUEUE);
  192. }
  193. EXPORT_SYMBOL(recv_Bchannel_skb);
  194. static void
  195. confirm_Dsend(struct dchannel *dch)
  196. {
  197. struct sk_buff *skb;
  198. skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(dch->tx_skb),
  199. 0, NULL, GFP_ATOMIC);
  200. if (!skb) {
  201. printk(KERN_ERR "%s: no skb id %x\n", __func__,
  202. mISDN_HEAD_ID(dch->tx_skb));
  203. return;
  204. }
  205. skb_queue_tail(&dch->rqueue, skb);
  206. schedule_event(dch, FLG_RECVQUEUE);
  207. }
  208. int
  209. get_next_dframe(struct dchannel *dch)
  210. {
  211. dch->tx_idx = 0;
  212. dch->tx_skb = skb_dequeue(&dch->squeue);
  213. if (dch->tx_skb) {
  214. confirm_Dsend(dch);
  215. return 1;
  216. }
  217. dch->tx_skb = NULL;
  218. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(get_next_dframe);
  222. void
  223. confirm_Bsend(struct bchannel *bch)
  224. {
  225. struct sk_buff *skb;
  226. if (bch->rcount >= 64)
  227. return;
  228. skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(bch->tx_skb),
  229. 0, NULL, GFP_ATOMIC);
  230. if (!skb) {
  231. printk(KERN_ERR "%s: no skb id %x\n", __func__,
  232. mISDN_HEAD_ID(bch->tx_skb));
  233. return;
  234. }
  235. bch->rcount++;
  236. skb_queue_tail(&bch->rqueue, skb);
  237. schedule_event(bch, FLG_RECVQUEUE);
  238. }
  239. EXPORT_SYMBOL(confirm_Bsend);
  240. int
  241. get_next_bframe(struct bchannel *bch)
  242. {
  243. bch->tx_idx = 0;
  244. if (test_bit(FLG_TX_NEXT, &bch->Flags)) {
  245. bch->tx_skb = bch->next_skb;
  246. if (bch->tx_skb) {
  247. bch->next_skb = NULL;
  248. test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  249. if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
  250. confirm_Bsend(bch); /* not for transparent */
  251. return 1;
  252. } else {
  253. test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  254. printk(KERN_WARNING "B TX_NEXT without skb\n");
  255. }
  256. }
  257. bch->tx_skb = NULL;
  258. test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
  259. return 0;
  260. }
  261. EXPORT_SYMBOL(get_next_bframe);
  262. void
  263. queue_ch_frame(struct mISDNchannel *ch, u_int pr, int id, struct sk_buff *skb)
  264. {
  265. struct mISDNhead *hh;
  266. if (!skb) {
  267. _queue_data(ch, pr, id, 0, NULL, GFP_ATOMIC);
  268. } else {
  269. if (ch->peer) {
  270. hh = mISDN_HEAD_P(skb);
  271. hh->prim = pr;
  272. hh->id = id;
  273. if (!ch->recv(ch->peer, skb))
  274. return;
  275. }
  276. dev_kfree_skb(skb);
  277. }
  278. }
  279. EXPORT_SYMBOL(queue_ch_frame);
  280. int
  281. dchannel_senddata(struct dchannel *ch, struct sk_buff *skb)
  282. {
  283. /* check oversize */
  284. if (skb->len <= 0) {
  285. printk(KERN_WARNING "%s: skb too small\n", __func__);
  286. return -EINVAL;
  287. }
  288. if (skb->len > ch->maxlen) {
  289. printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
  290. __func__, skb->len, ch->maxlen);
  291. return -EINVAL;
  292. }
  293. /* HW lock must be obtained */
  294. if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  295. skb_queue_tail(&ch->squeue, skb);
  296. return 0;
  297. } else {
  298. /* write to fifo */
  299. ch->tx_skb = skb;
  300. ch->tx_idx = 0;
  301. return 1;
  302. }
  303. }
  304. EXPORT_SYMBOL(dchannel_senddata);
  305. int
  306. bchannel_senddata(struct bchannel *ch, struct sk_buff *skb)
  307. {
  308. /* check oversize */
  309. if (skb->len <= 0) {
  310. printk(KERN_WARNING "%s: skb too small\n", __func__);
  311. return -EINVAL;
  312. }
  313. if (skb->len > ch->maxlen) {
  314. printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
  315. __func__, skb->len, ch->maxlen);
  316. return -EINVAL;
  317. }
  318. /* HW lock must be obtained */
  319. /* check for pending next_skb */
  320. if (ch->next_skb) {
  321. printk(KERN_WARNING
  322. "%s: next_skb exist ERROR (skb->len=%d next_skb->len=%d)\n",
  323. __func__, skb->len, ch->next_skb->len);
  324. return -EBUSY;
  325. }
  326. if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  327. test_and_set_bit(FLG_TX_NEXT, &ch->Flags);
  328. ch->next_skb = skb;
  329. return 0;
  330. } else {
  331. /* write to fifo */
  332. ch->tx_skb = skb;
  333. ch->tx_idx = 0;
  334. return 1;
  335. }
  336. }
  337. EXPORT_SYMBOL(bchannel_senddata);