hwchannel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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, unsigned short maxlen,
  78. unsigned short minlen)
  79. {
  80. ch->Flags = 0;
  81. ch->minlen = minlen;
  82. ch->next_minlen = minlen;
  83. ch->init_minlen = minlen;
  84. ch->maxlen = maxlen;
  85. ch->next_maxlen = maxlen;
  86. ch->init_maxlen = maxlen;
  87. ch->hw = NULL;
  88. ch->rx_skb = NULL;
  89. ch->tx_skb = NULL;
  90. ch->tx_idx = 0;
  91. skb_queue_head_init(&ch->rqueue);
  92. ch->rcount = 0;
  93. ch->next_skb = NULL;
  94. INIT_WORK(&ch->workq, bchannel_bh);
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(mISDN_initbchannel);
  98. int
  99. mISDN_freedchannel(struct dchannel *ch)
  100. {
  101. if (ch->tx_skb) {
  102. dev_kfree_skb(ch->tx_skb);
  103. ch->tx_skb = NULL;
  104. }
  105. if (ch->rx_skb) {
  106. dev_kfree_skb(ch->rx_skb);
  107. ch->rx_skb = NULL;
  108. }
  109. skb_queue_purge(&ch->squeue);
  110. skb_queue_purge(&ch->rqueue);
  111. flush_work_sync(&ch->workq);
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(mISDN_freedchannel);
  115. void
  116. mISDN_clear_bchannel(struct bchannel *ch)
  117. {
  118. if (ch->tx_skb) {
  119. dev_kfree_skb(ch->tx_skb);
  120. ch->tx_skb = NULL;
  121. }
  122. ch->tx_idx = 0;
  123. if (ch->rx_skb) {
  124. dev_kfree_skb(ch->rx_skb);
  125. ch->rx_skb = NULL;
  126. }
  127. if (ch->next_skb) {
  128. dev_kfree_skb(ch->next_skb);
  129. ch->next_skb = NULL;
  130. }
  131. test_and_clear_bit(FLG_TX_BUSY, &ch->Flags);
  132. test_and_clear_bit(FLG_TX_NEXT, &ch->Flags);
  133. test_and_clear_bit(FLG_ACTIVE, &ch->Flags);
  134. ch->minlen = ch->init_minlen;
  135. ch->next_minlen = ch->init_minlen;
  136. ch->maxlen = ch->init_maxlen;
  137. ch->next_maxlen = ch->init_maxlen;
  138. }
  139. EXPORT_SYMBOL(mISDN_clear_bchannel);
  140. int
  141. mISDN_freebchannel(struct bchannel *ch)
  142. {
  143. mISDN_clear_bchannel(ch);
  144. skb_queue_purge(&ch->rqueue);
  145. ch->rcount = 0;
  146. flush_work_sync(&ch->workq);
  147. return 0;
  148. }
  149. EXPORT_SYMBOL(mISDN_freebchannel);
  150. int
  151. mISDN_ctrl_bchannel(struct bchannel *bch, struct mISDN_ctrl_req *cq)
  152. {
  153. int ret = 0;
  154. switch (cq->op) {
  155. case MISDN_CTRL_GETOP:
  156. cq->op = MISDN_CTRL_RX_BUFFER;
  157. break;
  158. case MISDN_CTRL_RX_BUFFER:
  159. if (cq->p2 > MISDN_CTRL_RX_SIZE_IGNORE)
  160. bch->next_maxlen = cq->p2;
  161. if (cq->p1 > MISDN_CTRL_RX_SIZE_IGNORE)
  162. bch->next_minlen = cq->p1;
  163. /* we return the old values */
  164. cq->p1 = bch->minlen;
  165. cq->p2 = bch->maxlen;
  166. break;
  167. default:
  168. pr_info("mISDN unhandled control %x operation\n", cq->op);
  169. ret = -EINVAL;
  170. break;
  171. }
  172. return ret;
  173. }
  174. EXPORT_SYMBOL(mISDN_ctrl_bchannel);
  175. static inline u_int
  176. get_sapi_tei(u_char *p)
  177. {
  178. u_int sapi, tei;
  179. sapi = *p >> 2;
  180. tei = p[1] >> 1;
  181. return sapi | (tei << 8);
  182. }
  183. void
  184. recv_Dchannel(struct dchannel *dch)
  185. {
  186. struct mISDNhead *hh;
  187. if (dch->rx_skb->len < 2) { /* at least 2 for sapi / tei */
  188. dev_kfree_skb(dch->rx_skb);
  189. dch->rx_skb = NULL;
  190. return;
  191. }
  192. hh = mISDN_HEAD_P(dch->rx_skb);
  193. hh->prim = PH_DATA_IND;
  194. hh->id = get_sapi_tei(dch->rx_skb->data);
  195. skb_queue_tail(&dch->rqueue, dch->rx_skb);
  196. dch->rx_skb = NULL;
  197. schedule_event(dch, FLG_RECVQUEUE);
  198. }
  199. EXPORT_SYMBOL(recv_Dchannel);
  200. void
  201. recv_Echannel(struct dchannel *ech, struct dchannel *dch)
  202. {
  203. struct mISDNhead *hh;
  204. if (ech->rx_skb->len < 2) { /* at least 2 for sapi / tei */
  205. dev_kfree_skb(ech->rx_skb);
  206. ech->rx_skb = NULL;
  207. return;
  208. }
  209. hh = mISDN_HEAD_P(ech->rx_skb);
  210. hh->prim = PH_DATA_E_IND;
  211. hh->id = get_sapi_tei(ech->rx_skb->data);
  212. skb_queue_tail(&dch->rqueue, ech->rx_skb);
  213. ech->rx_skb = NULL;
  214. schedule_event(dch, FLG_RECVQUEUE);
  215. }
  216. EXPORT_SYMBOL(recv_Echannel);
  217. void
  218. recv_Bchannel(struct bchannel *bch, unsigned int id, bool force)
  219. {
  220. struct mISDNhead *hh;
  221. /* if allocation did fail upper functions still may call us */
  222. if (unlikely(!bch->rx_skb))
  223. return;
  224. if (unlikely(!bch->rx_skb->len)) {
  225. /* we have no data to send - this may happen after recovery
  226. * from overflow or too small allocation.
  227. * We need to free the buffer here */
  228. dev_kfree_skb(bch->rx_skb);
  229. bch->rx_skb = NULL;
  230. } else {
  231. if (test_bit(FLG_TRANSPARENT, &bch->Flags) &&
  232. (bch->rx_skb->len < bch->minlen) && !force)
  233. return;
  234. hh = mISDN_HEAD_P(bch->rx_skb);
  235. hh->prim = PH_DATA_IND;
  236. hh->id = id;
  237. if (bch->rcount >= 64) {
  238. printk(KERN_WARNING
  239. "B%d receive queue overflow - flushing!\n",
  240. bch->nr);
  241. skb_queue_purge(&bch->rqueue);
  242. }
  243. bch->rcount++;
  244. skb_queue_tail(&bch->rqueue, bch->rx_skb);
  245. bch->rx_skb = NULL;
  246. schedule_event(bch, FLG_RECVQUEUE);
  247. }
  248. }
  249. EXPORT_SYMBOL(recv_Bchannel);
  250. void
  251. recv_Dchannel_skb(struct dchannel *dch, struct sk_buff *skb)
  252. {
  253. skb_queue_tail(&dch->rqueue, skb);
  254. schedule_event(dch, FLG_RECVQUEUE);
  255. }
  256. EXPORT_SYMBOL(recv_Dchannel_skb);
  257. void
  258. recv_Bchannel_skb(struct bchannel *bch, struct sk_buff *skb)
  259. {
  260. if (bch->rcount >= 64) {
  261. printk(KERN_WARNING "B-channel %p receive queue overflow, "
  262. "flushing!\n", bch);
  263. skb_queue_purge(&bch->rqueue);
  264. bch->rcount = 0;
  265. }
  266. bch->rcount++;
  267. skb_queue_tail(&bch->rqueue, skb);
  268. schedule_event(bch, FLG_RECVQUEUE);
  269. }
  270. EXPORT_SYMBOL(recv_Bchannel_skb);
  271. static void
  272. confirm_Dsend(struct dchannel *dch)
  273. {
  274. struct sk_buff *skb;
  275. skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(dch->tx_skb),
  276. 0, NULL, GFP_ATOMIC);
  277. if (!skb) {
  278. printk(KERN_ERR "%s: no skb id %x\n", __func__,
  279. mISDN_HEAD_ID(dch->tx_skb));
  280. return;
  281. }
  282. skb_queue_tail(&dch->rqueue, skb);
  283. schedule_event(dch, FLG_RECVQUEUE);
  284. }
  285. int
  286. get_next_dframe(struct dchannel *dch)
  287. {
  288. dch->tx_idx = 0;
  289. dch->tx_skb = skb_dequeue(&dch->squeue);
  290. if (dch->tx_skb) {
  291. confirm_Dsend(dch);
  292. return 1;
  293. }
  294. dch->tx_skb = NULL;
  295. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(get_next_dframe);
  299. static void
  300. confirm_Bsend(struct bchannel *bch)
  301. {
  302. struct sk_buff *skb;
  303. if (bch->rcount >= 64) {
  304. printk(KERN_WARNING "B-channel %p receive queue overflow, "
  305. "flushing!\n", bch);
  306. skb_queue_purge(&bch->rqueue);
  307. bch->rcount = 0;
  308. }
  309. skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(bch->tx_skb),
  310. 0, NULL, GFP_ATOMIC);
  311. if (!skb) {
  312. printk(KERN_ERR "%s: no skb id %x\n", __func__,
  313. mISDN_HEAD_ID(bch->tx_skb));
  314. return;
  315. }
  316. bch->rcount++;
  317. skb_queue_tail(&bch->rqueue, skb);
  318. schedule_event(bch, FLG_RECVQUEUE);
  319. }
  320. int
  321. get_next_bframe(struct bchannel *bch)
  322. {
  323. bch->tx_idx = 0;
  324. if (test_bit(FLG_TX_NEXT, &bch->Flags)) {
  325. bch->tx_skb = bch->next_skb;
  326. if (bch->tx_skb) {
  327. bch->next_skb = NULL;
  328. test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  329. /* confirm imediately to allow next data */
  330. confirm_Bsend(bch);
  331. return 1;
  332. } else {
  333. test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  334. printk(KERN_WARNING "B TX_NEXT without skb\n");
  335. }
  336. }
  337. bch->tx_skb = NULL;
  338. test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
  339. return 0;
  340. }
  341. EXPORT_SYMBOL(get_next_bframe);
  342. void
  343. queue_ch_frame(struct mISDNchannel *ch, u_int pr, int id, struct sk_buff *skb)
  344. {
  345. struct mISDNhead *hh;
  346. if (!skb) {
  347. _queue_data(ch, pr, id, 0, NULL, GFP_ATOMIC);
  348. } else {
  349. if (ch->peer) {
  350. hh = mISDN_HEAD_P(skb);
  351. hh->prim = pr;
  352. hh->id = id;
  353. if (!ch->recv(ch->peer, skb))
  354. return;
  355. }
  356. dev_kfree_skb(skb);
  357. }
  358. }
  359. EXPORT_SYMBOL(queue_ch_frame);
  360. int
  361. dchannel_senddata(struct dchannel *ch, struct sk_buff *skb)
  362. {
  363. /* check oversize */
  364. if (skb->len <= 0) {
  365. printk(KERN_WARNING "%s: skb too small\n", __func__);
  366. return -EINVAL;
  367. }
  368. if (skb->len > ch->maxlen) {
  369. printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
  370. __func__, skb->len, ch->maxlen);
  371. return -EINVAL;
  372. }
  373. /* HW lock must be obtained */
  374. if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  375. skb_queue_tail(&ch->squeue, skb);
  376. return 0;
  377. } else {
  378. /* write to fifo */
  379. ch->tx_skb = skb;
  380. ch->tx_idx = 0;
  381. return 1;
  382. }
  383. }
  384. EXPORT_SYMBOL(dchannel_senddata);
  385. int
  386. bchannel_senddata(struct bchannel *ch, struct sk_buff *skb)
  387. {
  388. /* check oversize */
  389. if (skb->len <= 0) {
  390. printk(KERN_WARNING "%s: skb too small\n", __func__);
  391. return -EINVAL;
  392. }
  393. if (skb->len > ch->maxlen) {
  394. printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
  395. __func__, skb->len, ch->maxlen);
  396. return -EINVAL;
  397. }
  398. /* HW lock must be obtained */
  399. /* check for pending next_skb */
  400. if (ch->next_skb) {
  401. printk(KERN_WARNING
  402. "%s: next_skb exist ERROR (skb->len=%d next_skb->len=%d)\n",
  403. __func__, skb->len, ch->next_skb->len);
  404. return -EBUSY;
  405. }
  406. if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  407. test_and_set_bit(FLG_TX_NEXT, &ch->Flags);
  408. ch->next_skb = skb;
  409. return 0;
  410. } else {
  411. /* write to fifo */
  412. ch->tx_skb = skb;
  413. ch->tx_idx = 0;
  414. confirm_Bsend(ch);
  415. return 1;
  416. }
  417. }
  418. EXPORT_SYMBOL(bchannel_senddata);
  419. /* The function allocates a new receive skb on demand with a size for the
  420. * requirements of the current protocol. It returns the tailroom of the
  421. * receive skb or an error.
  422. */
  423. int
  424. bchannel_get_rxbuf(struct bchannel *bch, int reqlen)
  425. {
  426. int len;
  427. if (bch->rx_skb) {
  428. len = skb_tailroom(bch->rx_skb);
  429. if (len < reqlen) {
  430. pr_warning("B%d no space for %d (only %d) bytes\n",
  431. bch->nr, reqlen, len);
  432. if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  433. /* send what we have now and try a new buffer */
  434. recv_Bchannel(bch, 0, true);
  435. } else {
  436. /* on HDLC we have to drop too big frames */
  437. return -EMSGSIZE;
  438. }
  439. } else {
  440. return len;
  441. }
  442. }
  443. /* update current min/max length first */
  444. if (unlikely(bch->maxlen != bch->next_maxlen))
  445. bch->maxlen = bch->next_maxlen;
  446. if (unlikely(bch->minlen != bch->next_minlen))
  447. bch->minlen = bch->next_minlen;
  448. if (unlikely(reqlen > bch->maxlen))
  449. return -EMSGSIZE;
  450. if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  451. if (reqlen >= bch->minlen) {
  452. len = reqlen;
  453. } else {
  454. len = 2 * bch->minlen;
  455. if (len > bch->maxlen)
  456. len = bch->maxlen;
  457. }
  458. } else {
  459. /* with HDLC we do not know the length yet */
  460. len = bch->maxlen;
  461. }
  462. bch->rx_skb = mI_alloc_skb(len, GFP_ATOMIC);
  463. if (!bch->rx_skb) {
  464. pr_warning("B%d receive no memory for %d bytes\n",
  465. bch->nr, len);
  466. len = -ENOMEM;
  467. }
  468. return len;
  469. }
  470. EXPORT_SYMBOL(bchannel_get_rxbuf);