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