hwchannel.c 8.7 KB

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