Blame view

drivers/isdn/mISDN/hwchannel.c 8.66 KB
1b2b03f8e   Karsten Keil   Add mISDN core files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   *
   * Author	Karsten Keil <kkeil@novell.com>
   *
   * Copyright 2008  by Karsten Keil <kkeil@novell.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   */
5a0e3ad6a   Tejun Heo   include cleanup: ...
17
  #include <linux/gfp.h>
1b2b03f8e   Karsten Keil   Add mISDN core files
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include <linux/module.h>
  #include <linux/mISDNhw.h>
  
  static void
  dchannel_bh(struct work_struct *ws)
  {
  	struct dchannel	*dch  = container_of(ws, struct dchannel, workq);
  	struct sk_buff	*skb;
  	int		err;
  
  	if (test_and_clear_bit(FLG_RECVQUEUE, &dch->Flags)) {
  		while ((skb = skb_dequeue(&dch->rqueue))) {
  			if (likely(dch->dev.D.peer)) {
  				err = dch->dev.D.recv(dch->dev.D.peer, skb);
  				if (err)
  					dev_kfree_skb(skb);
  			} else
  				dev_kfree_skb(skb);
  		}
  	}
  	if (test_and_clear_bit(FLG_PHCHANGE, &dch->Flags)) {
  		if (dch->phfunc)
  			dch->phfunc(dch);
  	}
  }
  
  static void
  bchannel_bh(struct work_struct *ws)
  {
  	struct bchannel	*bch  = container_of(ws, struct bchannel, workq);
  	struct sk_buff	*skb;
  	int		err;
  
  	if (test_and_clear_bit(FLG_RECVQUEUE, &bch->Flags)) {
  		while ((skb = skb_dequeue(&bch->rqueue))) {
1b2b03f8e   Karsten Keil   Add mISDN core files
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  			bch->rcount--;
  			if (likely(bch->ch.peer)) {
  				err = bch->ch.recv(bch->ch.peer, skb);
  				if (err)
  					dev_kfree_skb(skb);
  			} else
  				dev_kfree_skb(skb);
  		}
  	}
  }
  
  int
  mISDN_initdchannel(struct dchannel *ch, int maxlen, void *phf)
  {
  	test_and_set_bit(FLG_HDLC, &ch->Flags);
  	ch->maxlen = maxlen;
  	ch->hw = NULL;
  	ch->rx_skb = NULL;
  	ch->tx_skb = NULL;
  	ch->tx_idx = 0;
  	ch->phfunc = phf;
  	skb_queue_head_init(&ch->squeue);
  	skb_queue_head_init(&ch->rqueue);
  	INIT_LIST_HEAD(&ch->dev.bchannels);
  	INIT_WORK(&ch->workq, dchannel_bh);
  	return 0;
  }
  EXPORT_SYMBOL(mISDN_initdchannel);
  
  int
  mISDN_initbchannel(struct bchannel *ch, int maxlen)
  {
  	ch->Flags = 0;
  	ch->maxlen = maxlen;
  	ch->hw = NULL;
  	ch->rx_skb = NULL;
  	ch->tx_skb = NULL;
  	ch->tx_idx = 0;
  	skb_queue_head_init(&ch->rqueue);
  	ch->rcount = 0;
  	ch->next_skb = NULL;
  	INIT_WORK(&ch->workq, bchannel_bh);
  	return 0;
  }
  EXPORT_SYMBOL(mISDN_initbchannel);
  
  int
  mISDN_freedchannel(struct dchannel *ch)
  {
  	if (ch->tx_skb) {
  		dev_kfree_skb(ch->tx_skb);
  		ch->tx_skb = NULL;
  	}
  	if (ch->rx_skb) {
  		dev_kfree_skb(ch->rx_skb);
  		ch->rx_skb = NULL;
  	}
  	skb_queue_purge(&ch->squeue);
  	skb_queue_purge(&ch->rqueue);
0d26aa704   Tejun Heo   mISDN: don't use ...
112
  	flush_work_sync(&ch->workq);
1b2b03f8e   Karsten Keil   Add mISDN core files
113
114
115
  	return 0;
  }
  EXPORT_SYMBOL(mISDN_freedchannel);
fb286f047   Karsten Keil   mISDN: Make clear...
116
117
  void
  mISDN_clear_bchannel(struct bchannel *ch)
1b2b03f8e   Karsten Keil   Add mISDN core files
118
119
120
121
122
  {
  	if (ch->tx_skb) {
  		dev_kfree_skb(ch->tx_skb);
  		ch->tx_skb = NULL;
  	}
fb286f047   Karsten Keil   mISDN: Make clear...
123
  	ch->tx_idx = 0;
1b2b03f8e   Karsten Keil   Add mISDN core files
124
125
126
127
128
129
130
131
  	if (ch->rx_skb) {
  		dev_kfree_skb(ch->rx_skb);
  		ch->rx_skb = NULL;
  	}
  	if (ch->next_skb) {
  		dev_kfree_skb(ch->next_skb);
  		ch->next_skb = NULL;
  	}
fb286f047   Karsten Keil   mISDN: Make clear...
132
133
134
135
136
137
138
139
140
141
  	test_and_clear_bit(FLG_TX_BUSY, &ch->Flags);
  	test_and_clear_bit(FLG_TX_NEXT, &ch->Flags);
  	test_and_clear_bit(FLG_ACTIVE, &ch->Flags);
  }
  EXPORT_SYMBOL(mISDN_clear_bchannel);
  
  int
  mISDN_freebchannel(struct bchannel *ch)
  {
  	mISDN_clear_bchannel(ch);
1b2b03f8e   Karsten Keil   Add mISDN core files
142
143
  	skb_queue_purge(&ch->rqueue);
  	ch->rcount = 0;
0d26aa704   Tejun Heo   mISDN: don't use ...
144
  	flush_work_sync(&ch->workq);
1b2b03f8e   Karsten Keil   Add mISDN core files
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  	return 0;
  }
  EXPORT_SYMBOL(mISDN_freebchannel);
  
  static inline u_int
  get_sapi_tei(u_char *p)
  {
  	u_int	sapi, tei;
  
  	sapi = *p >> 2;
  	tei = p[1] >> 1;
  	return sapi | (tei << 8);
  }
  
  void
  recv_Dchannel(struct dchannel *dch)
  {
  	struct mISDNhead *hh;
  
  	if (dch->rx_skb->len < 2) { /* at least 2 for sapi / tei */
  		dev_kfree_skb(dch->rx_skb);
  		dch->rx_skb = NULL;
  		return;
  	}
  	hh = mISDN_HEAD_P(dch->rx_skb);
  	hh->prim = PH_DATA_IND;
  	hh->id = get_sapi_tei(dch->rx_skb->data);
  	skb_queue_tail(&dch->rqueue, dch->rx_skb);
  	dch->rx_skb = NULL;
  	schedule_event(dch, FLG_RECVQUEUE);
  }
  EXPORT_SYMBOL(recv_Dchannel);
  
  void
1f28fa19d   Martin Bachem   mISDN: Add E-Chan...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  recv_Echannel(struct dchannel *ech, struct dchannel *dch)
  {
  	struct mISDNhead *hh;
  
  	if (ech->rx_skb->len < 2) { /* at least 2 for sapi / tei */
  		dev_kfree_skb(ech->rx_skb);
  		ech->rx_skb = NULL;
  		return;
  	}
  	hh = mISDN_HEAD_P(ech->rx_skb);
  	hh->prim = PH_DATA_E_IND;
  	hh->id = get_sapi_tei(ech->rx_skb->data);
  	skb_queue_tail(&dch->rqueue, ech->rx_skb);
  	ech->rx_skb = NULL;
  	schedule_event(dch, FLG_RECVQUEUE);
  }
  EXPORT_SYMBOL(recv_Echannel);
  
  void
7cfa153dd   Andreas Eversberg   mISDN: Echo cance...
198
  recv_Bchannel(struct bchannel *bch, unsigned int id)
1b2b03f8e   Karsten Keil   Add mISDN core files
199
200
201
202
203
  {
  	struct mISDNhead *hh;
  
  	hh = mISDN_HEAD_P(bch->rx_skb);
  	hh->prim = PH_DATA_IND;
7cfa153dd   Andreas Eversberg   mISDN: Echo cance...
204
  	hh->id = id;
1b2b03f8e   Karsten Keil   Add mISDN core files
205
  	if (bch->rcount >= 64) {
11618496d   Andreas Eversberg   mISDN: Fix queue ...
206
  		printk(KERN_WARNING "B-channel %p receive queue overflow, "
1752a373c   Paul Bolle   mISDN: fix printk...
207
208
  			"flushing!
  ", bch);
11618496d   Andreas Eversberg   mISDN: Fix queue ...
209
210
  		skb_queue_purge(&bch->rqueue);
  		bch->rcount = 0;
1b2b03f8e   Karsten Keil   Add mISDN core files
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  		return;
  	}
  	bch->rcount++;
  	skb_queue_tail(&bch->rqueue, bch->rx_skb);
  	bch->rx_skb = NULL;
  	schedule_event(bch, FLG_RECVQUEUE);
  }
  EXPORT_SYMBOL(recv_Bchannel);
  
  void
  recv_Dchannel_skb(struct dchannel *dch, struct sk_buff *skb)
  {
  	skb_queue_tail(&dch->rqueue, skb);
  	schedule_event(dch, FLG_RECVQUEUE);
  }
  EXPORT_SYMBOL(recv_Dchannel_skb);
  
  void
  recv_Bchannel_skb(struct bchannel *bch, struct sk_buff *skb)
  {
  	if (bch->rcount >= 64) {
11618496d   Andreas Eversberg   mISDN: Fix queue ...
232
  		printk(KERN_WARNING "B-channel %p receive queue overflow, "
1752a373c   Paul Bolle   mISDN: fix printk...
233
234
  			"flushing!
  ", bch);
11618496d   Andreas Eversberg   mISDN: Fix queue ...
235
236
  		skb_queue_purge(&bch->rqueue);
  		bch->rcount = 0;
1b2b03f8e   Karsten Keil   Add mISDN core files
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  	}
  	bch->rcount++;
  	skb_queue_tail(&bch->rqueue, skb);
  	schedule_event(bch, FLG_RECVQUEUE);
  }
  EXPORT_SYMBOL(recv_Bchannel_skb);
  
  static void
  confirm_Dsend(struct dchannel *dch)
  {
  	struct sk_buff	*skb;
  
  	skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(dch->tx_skb),
  	    0, NULL, GFP_ATOMIC);
  	if (!skb) {
  		printk(KERN_ERR "%s: no skb id %x
  ", __func__,
  		    mISDN_HEAD_ID(dch->tx_skb));
  		return;
  	}
  	skb_queue_tail(&dch->rqueue, skb);
  	schedule_event(dch, FLG_RECVQUEUE);
  }
  
  int
  get_next_dframe(struct dchannel *dch)
  {
  	dch->tx_idx = 0;
  	dch->tx_skb = skb_dequeue(&dch->squeue);
  	if (dch->tx_skb) {
  		confirm_Dsend(dch);
  		return 1;
  	}
  	dch->tx_skb = NULL;
  	test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  	return 0;
  }
  EXPORT_SYMBOL(get_next_dframe);
  
  void
  confirm_Bsend(struct bchannel *bch)
  {
  	struct sk_buff	*skb;
11618496d   Andreas Eversberg   mISDN: Fix queue ...
280
281
  	if (bch->rcount >= 64) {
  		printk(KERN_WARNING "B-channel %p receive queue overflow, "
1752a373c   Paul Bolle   mISDN: fix printk...
282
283
  			"flushing!
  ", bch);
11618496d   Andreas Eversberg   mISDN: Fix queue ...
284
285
286
  		skb_queue_purge(&bch->rqueue);
  		bch->rcount = 0;
  	}
1b2b03f8e   Karsten Keil   Add mISDN core files
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  	skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(bch->tx_skb),
  	    0, NULL, GFP_ATOMIC);
  	if (!skb) {
  		printk(KERN_ERR "%s: no skb id %x
  ", __func__,
  		    mISDN_HEAD_ID(bch->tx_skb));
  		return;
  	}
  	bch->rcount++;
  	skb_queue_tail(&bch->rqueue, skb);
  	schedule_event(bch, FLG_RECVQUEUE);
  }
  EXPORT_SYMBOL(confirm_Bsend);
  
  int
  get_next_bframe(struct bchannel *bch)
  {
  	bch->tx_idx = 0;
  	if (test_bit(FLG_TX_NEXT, &bch->Flags)) {
  		bch->tx_skb = bch->next_skb;
  		if (bch->tx_skb) {
  			bch->next_skb = NULL;
  			test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  			if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
  				confirm_Bsend(bch); /* not for transparent */
  			return 1;
  		} else {
  			test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  			printk(KERN_WARNING "B TX_NEXT without skb
  ");
  		}
  	}
  	bch->tx_skb = NULL;
  	test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
  	return 0;
  }
  EXPORT_SYMBOL(get_next_bframe);
  
  void
  queue_ch_frame(struct mISDNchannel *ch, u_int pr, int id, struct sk_buff *skb)
  {
  	struct mISDNhead *hh;
  
  	if (!skb) {
  		_queue_data(ch, pr, id, 0, NULL, GFP_ATOMIC);
  	} else {
  		if (ch->peer) {
  			hh = mISDN_HEAD_P(skb);
  			hh->prim = pr;
  			hh->id = id;
  			if (!ch->recv(ch->peer, skb))
  				return;
  		}
  		dev_kfree_skb(skb);
  	}
  }
  EXPORT_SYMBOL(queue_ch_frame);
  
  int
  dchannel_senddata(struct dchannel *ch, struct sk_buff *skb)
  {
  	/* check oversize */
  	if (skb->len <= 0) {
  		printk(KERN_WARNING "%s: skb too small
  ", __func__);
  		return -EINVAL;
  	}
  	if (skb->len > ch->maxlen) {
  		printk(KERN_WARNING "%s: skb too large(%d/%d)
  ",
  			__func__, skb->len, ch->maxlen);
  		return -EINVAL;
  	}
  	/* HW lock must be obtained */
  	if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  		skb_queue_tail(&ch->squeue, skb);
  		return 0;
  	} else {
  		/* write to fifo */
  		ch->tx_skb = skb;
  		ch->tx_idx = 0;
  		return 1;
  	}
  }
  EXPORT_SYMBOL(dchannel_senddata);
  
  int
  bchannel_senddata(struct bchannel *ch, struct sk_buff *skb)
  {
  
  	/* check oversize */
  	if (skb->len <= 0) {
  		printk(KERN_WARNING "%s: skb too small
  ", __func__);
  		return -EINVAL;
  	}
  	if (skb->len > ch->maxlen) {
  		printk(KERN_WARNING "%s: skb too large(%d/%d)
  ",
  			__func__, skb->len, ch->maxlen);
  		return -EINVAL;
  	}
  	/* HW lock must be obtained */
  	/* check for pending next_skb */
  	if (ch->next_skb) {
  		printk(KERN_WARNING
  		    "%s: next_skb exist ERROR (skb->len=%d next_skb->len=%d)
  ",
  		    __func__, skb->len, ch->next_skb->len);
  		return -EBUSY;
  	}
  	if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  		test_and_set_bit(FLG_TX_NEXT, &ch->Flags);
  		ch->next_skb = skb;
  		return 0;
  	} else {
  		/* write to fifo */
  		ch->tx_skb = skb;
  		ch->tx_idx = 0;
  		return 1;
  	}
  }
  EXPORT_SYMBOL(bchannel_senddata);