aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/isdn/i4l/isdn_v110.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/isdn/i4l/isdn_v110.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/isdn/i4l/isdn_v110.c')
-rw-r--r--drivers/isdn/i4l/isdn_v110.c617
1 files changed, 617 insertions, 0 deletions
diff --git a/drivers/isdn/i4l/isdn_v110.c b/drivers/isdn/i4l/isdn_v110.c
new file mode 100644
index 000000000000..f47f2b9846d8
--- /dev/null
+++ b/drivers/isdn/i4l/isdn_v110.c
@@ -0,0 +1,617 @@
1/* $Id: isdn_v110.c,v 1.1.2.2 2004/01/12 22:37:19 keil Exp $
2 *
3 * Linux ISDN subsystem, V.110 related functions (linklevel).
4 *
5 * Copyright by Thomas Pfeiffer (pfeiffer@pds.de)
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
9 *
10 */
11
12#include <linux/string.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/mm.h>
16#include <linux/delay.h>
17
18#include <linux/isdn.h>
19#include "isdn_v110.h"
20
21#undef ISDN_V110_DEBUG
22
23char *isdn_v110_revision = "$Revision: 1.1.2.2 $";
24
25#define V110_38400 255
26#define V110_19200 15
27#define V110_9600 3
28
29/*
30 * The following data are precoded matrices, online and offline matrix
31 * for 9600, 19200 und 38400, respectively
32 */
33static unsigned char V110_OnMatrix_9600[] =
34{0xfc, 0xfc, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff,
35 0xff, 0xfd, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xfd,
36 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff,
37 0xff, 0xfd, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xfd};
38
39static unsigned char V110_OffMatrix_9600[] =
40{0xfc, 0xfc, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
44
45static unsigned char V110_OnMatrix_19200[] =
46{0xf0, 0xf0, 0xff, 0xf7, 0xff, 0xf7, 0xff, 0xf7, 0xff, 0xf7,
47 0xfd, 0xff, 0xff, 0xf7, 0xff, 0xf7, 0xff, 0xf7, 0xff, 0xf7};
48
49static unsigned char V110_OffMatrix_19200[] =
50{0xf0, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
51 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
52
53static unsigned char V110_OnMatrix_38400[] =
54{0x00, 0x7f, 0x7f, 0x7f, 0x7f, 0xfd, 0x7f, 0x7f, 0x7f, 0x7f};
55
56static unsigned char V110_OffMatrix_38400[] =
57{0x00, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xff};
58
59/*
60 * FlipBits reorders sequences of keylen bits in one byte.
61 * E.g. source order 7654321 will be converted to 45670123 when keylen = 4,
62 * and to 67452301 when keylen = 2. This is necessary because ordering on
63 * the isdn line is the other way.
64 */
65static __inline unsigned char
66FlipBits(unsigned char c, int keylen)
67{
68 unsigned char b = c;
69 unsigned char bit = 128;
70 int i;
71 int j;
72 int hunks = (8 / keylen);
73
74 c = 0;
75 for (i = 0; i < hunks; i++) {
76 for (j = 0; j < keylen; j++) {
77 if (b & (bit >> j))
78 c |= bit >> (keylen - j - 1);
79 }
80 bit >>= keylen;
81 }
82 return c;
83}
84
85
86/* isdn_v110_open allocates and initializes private V.110 data
87 * structures and returns a pointer to these.
88 */
89static isdn_v110_stream *
90isdn_v110_open(unsigned char key, int hdrlen, int maxsize)
91{
92 int i;
93 isdn_v110_stream *v;
94
95 if ((v = kmalloc(sizeof(isdn_v110_stream), GFP_ATOMIC)) == NULL)
96 return NULL;
97 memset(v, 0, sizeof(isdn_v110_stream));
98 v->key = key;
99 v->nbits = 0;
100 for (i = 0; key & (1 << i); i++)
101 v->nbits++;
102
103 v->nbytes = 8 / v->nbits;
104 v->decodelen = 0;
105
106 switch (key) {
107 case V110_38400:
108 v->OnlineFrame = V110_OnMatrix_38400;
109 v->OfflineFrame = V110_OffMatrix_38400;
110 break;
111 case V110_19200:
112 v->OnlineFrame = V110_OnMatrix_19200;
113 v->OfflineFrame = V110_OffMatrix_19200;
114 break;
115 default:
116 v->OnlineFrame = V110_OnMatrix_9600;
117 v->OfflineFrame = V110_OffMatrix_9600;
118 break;
119 }
120 v->framelen = v->nbytes * 10;
121 v->SyncInit = 5;
122 v->introducer = 0;
123 v->dbit = 1;
124 v->b = 0;
125 v->skbres = hdrlen;
126 v->maxsize = maxsize - hdrlen;
127 if ((v->encodebuf = kmalloc(maxsize, GFP_ATOMIC)) == NULL) {
128 kfree(v);
129 return NULL;
130 }
131 return v;
132}
133
134/* isdn_v110_close frees private V.110 data structures */
135void
136isdn_v110_close(isdn_v110_stream * v)
137{
138 if (v == NULL)
139 return;
140#ifdef ISDN_V110_DEBUG
141 printk(KERN_DEBUG "v110 close\n");
142#endif
143 kfree(v->encodebuf);
144 kfree(v);
145}
146
147
148/*
149 * ValidHeaderBytes return the number of valid bytes in v->decodebuf
150 */
151static int
152ValidHeaderBytes(isdn_v110_stream * v)
153{
154 int i;
155 for (i = 0; (i < v->decodelen) && (i < v->nbytes); i++)
156 if ((v->decodebuf[i] & v->key) != 0)
157 break;
158 return i;
159}
160
161/*
162 * SyncHeader moves the decodebuf ptr to the next valid header
163 */
164static void
165SyncHeader(isdn_v110_stream * v)
166{
167 unsigned char *rbuf = v->decodebuf;
168 int len = v->decodelen;
169
170 if (len == 0)
171 return;
172 for (rbuf++, len--; len > 0; len--, rbuf++) /* such den SyncHeader in buf ! */
173 if ((*rbuf & v->key) == 0) /* erstes byte gefunden ? */
174 break; /* jupp! */
175 if (len)
176 memcpy(v->decodebuf, rbuf, len);
177
178 v->decodelen = len;
179#ifdef ISDN_V110_DEBUG
180 printk(KERN_DEBUG "isdn_v110: Header resync\n");
181#endif
182}
183
184/* DecodeMatrix takes n (n>=1) matrices (v110 frames, 10 bytes) where
185 len is the number of matrix-lines. len must be a multiple of 10, i.e.
186 only complete matices must be given.
187 From these, netto data is extracted and returned in buf. The return-value
188 is the bytecount of the decoded data.
189 */
190static int
191DecodeMatrix(isdn_v110_stream * v, unsigned char *m, int len, unsigned char *buf)
192{
193 int line = 0;
194 int buflen = 0;
195 int mbit = 64;
196 int introducer = v->introducer;
197 int dbit = v->dbit;
198 unsigned char b = v->b;
199
200 while (line < len) { /* Are we done with all lines of the matrix? */
201 if ((line % 10) == 0) { /* the 0. line of the matrix is always 0 ! */
202 if (m[line] != 0x00) { /* not 0 ? -> error! */
203#ifdef ISDN_V110_DEBUG
204 printk(KERN_DEBUG "isdn_v110: DecodeMatrix, V110 Bad Header\n");
205 /* returning now is not the right thing, though :-( */
206#endif
207 }
208 line++; /* next line of matrix */
209 continue;
210 } else if ((line % 10) == 5) { /* in line 5 there's only e-bits ! */
211 if ((m[line] & 0x70) != 0x30) { /* 011 has to be at the beginning! */
212#ifdef ISDN_V110_DEBUG
213 printk(KERN_DEBUG "isdn_v110: DecodeMatrix, V110 Bad 5th line\n");
214 /* returning now is not the right thing, though :-( */
215#endif
216 }
217 line++; /* next line */
218 continue;
219 } else if (!introducer) { /* every byte starts with 10 (stopbit, startbit) */
220 introducer = (m[line] & mbit) ? 0 : 1; /* current bit of the matrix */
221 next_byte:
222 if (mbit > 2) { /* was it the last bit in this line ? */
223 mbit >>= 1; /* no -> take next */
224 continue;
225 } /* otherwise start with leftmost bit in the next line */
226 mbit = 64;
227 line++;
228 continue;
229 } else { /* otherwise we need to set a data bit */
230 if (m[line] & mbit) /* was that bit set in the matrix ? */
231 b |= dbit; /* yes -> set it in the data byte */
232 else
233 b &= dbit - 1; /* no -> clear it in the data byte */
234 if (dbit < 128) /* is that data byte done ? */
235 dbit <<= 1; /* no, got the next bit */
236 else { /* data byte is done */
237 buf[buflen++] = b; /* copy byte into the output buffer */
238 introducer = b = 0; /* init of the intro sequence and of the data byte */
239 dbit = 1; /* next we look for the 0th bit */
240 }
241 goto next_byte; /* look for next bit in the matrix */
242 }
243 }
244 v->introducer = introducer;
245 v->dbit = dbit;
246 v->b = b;
247 return buflen; /* return number of bytes in the output buffer */
248}
249
250/*
251 * DecodeStream receives V.110 coded data from the input stream. It recovers the
252 * original frames.
253 * The input stream doesn't need to be framed
254 */
255struct sk_buff *
256isdn_v110_decode(isdn_v110_stream * v, struct sk_buff *skb)
257{
258 int i;
259 int j;
260 int len;
261 unsigned char *v110_buf;
262 unsigned char *rbuf;
263
264 if (!skb) {
265 printk(KERN_WARNING "isdn_v110_decode called with NULL skb!\n");
266 return NULL;
267 }
268 rbuf = skb->data;
269 len = skb->len;
270 if (v == NULL) {
271 /* invalid handle, no chance to proceed */
272 printk(KERN_WARNING "isdn_v110_decode called with NULL stream!\n");
273 dev_kfree_skb(skb);
274 return NULL;
275 }
276 if (v->decodelen == 0) /* cache empty? */
277 for (; len > 0; len--, rbuf++) /* scan for SyncHeader in buf */
278 if ((*rbuf & v->key) == 0)
279 break; /* found first byte */
280 if (len == 0) {
281 dev_kfree_skb(skb);
282 return NULL;
283 }
284 /* copy new data to decode-buffer */
285 memcpy(&(v->decodebuf[v->decodelen]), rbuf, len);
286 v->decodelen += len;
287 ReSync:
288 if (v->decodelen < v->nbytes) { /* got a new header ? */
289 dev_kfree_skb(skb);
290 return NULL; /* no, try later */
291 }
292 if (ValidHeaderBytes(v) != v->nbytes) { /* is that a valid header? */
293 SyncHeader(v); /* no -> look for header */
294 goto ReSync;
295 }
296 len = (v->decodelen - (v->decodelen % (10 * v->nbytes))) / v->nbytes;
297 if ((v110_buf = kmalloc(len, GFP_ATOMIC)) == NULL) {
298 printk(KERN_WARNING "isdn_v110_decode: Couldn't allocate v110_buf\n");
299 dev_kfree_skb(skb);
300 return NULL;
301 }
302 for (i = 0; i < len; i++) {
303 v110_buf[i] = 0;
304 for (j = 0; j < v->nbytes; j++)
305 v110_buf[i] |= (v->decodebuf[(i * v->nbytes) + j] & v->key) << (8 - ((j + 1) * v->nbits));
306 v110_buf[i] = FlipBits(v110_buf[i], v->nbits);
307 }
308 v->decodelen = (v->decodelen % (10 * v->nbytes));
309 memcpy(v->decodebuf, &(v->decodebuf[len * v->nbytes]), v->decodelen);
310
311 skb_trim(skb, DecodeMatrix(v, v110_buf, len, skb->data));
312 kfree(v110_buf);
313 if (skb->len)
314 return skb;
315 else {
316 kfree_skb(skb);
317 return NULL;
318 }
319}
320
321/* EncodeMatrix takes input data in buf, len is the bytecount.
322 Data is encoded into v110 frames in m. Return value is the number of
323 matrix-lines generated.
324 */
325static int
326EncodeMatrix(unsigned char *buf, int len, unsigned char *m, int mlen)
327{
328 int line = 0;
329 int i = 0;
330 int mbit = 128;
331 int dbit = 1;
332 int introducer = 3;
333 int ibit[] = {0, 1, 1};
334
335 while ((i < len) && (line < mlen)) { /* while we still have input data */
336 switch (line % 10) { /* in which line of the matrix are we? */
337 case 0:
338 m[line++] = 0x00; /* line 0 is always 0 */
339 mbit = 128; /* go on with the 7th bit */
340 break;
341 case 5:
342 m[line++] = 0xbf; /* line 5 is always 10111111 */
343 mbit = 128; /* go on with the 7th bit */
344 break;
345 }
346 if (line >= mlen) {
347 printk(KERN_WARNING "isdn_v110 (EncodeMatrix): buffer full!\n");
348 return line;
349 }
350 next_bit:
351 switch (mbit) { /* leftmost or rightmost bit ? */
352 case 1:
353 line++; /* rightmost -> go to next line */
354 if (line >= mlen) {
355 printk(KERN_WARNING "isdn_v110 (EncodeMatrix): buffer full!\n");
356 return line;
357 }
358 case 128:
359 m[line] = 128; /* leftmost -> set byte to 1000000 */
360 mbit = 64; /* current bit in the matrix line */
361 continue;
362 }
363 if (introducer) { /* set 110 sequence ? */
364 introducer--; /* set on digit less */
365 m[line] |= ibit[introducer] ? mbit : 0; /* set corresponding bit */
366 mbit >>= 1; /* bit of matrix line >> 1 */
367 goto next_bit; /* and go on there */
368 } /* else push data bits into the matrix! */
369 m[line] |= (buf[i] & dbit) ? mbit : 0; /* set data bit in matrix */
370 if (dbit == 128) { /* was it the last one? */
371 dbit = 1; /* then go on with first bit of */
372 i++; /* next byte in input buffer */
373 if (i < len) /* input buffer done ? */
374 introducer = 3; /* no, write introducer 110 */
375 else { /* input buffer done ! */
376 m[line] |= (mbit - 1) & 0xfe; /* set remaining bits in line to 1 */
377 break;
378 }
379 } else /* not the last data bit */
380 dbit <<= 1; /* then go to next data bit */
381 mbit >>= 1; /* go to next bit of matrix */
382 goto next_bit;
383
384 }
385 /* if necessary, generate remaining lines of the matrix... */
386 if ((line) && ((line + 10) < mlen))
387 switch (++line % 10) {
388 case 1:
389 m[line++] = 0xfe;
390 case 2:
391 m[line++] = 0xfe;
392 case 3:
393 m[line++] = 0xfe;
394 case 4:
395 m[line++] = 0xfe;
396 case 5:
397 m[line++] = 0xbf;
398 case 6:
399 m[line++] = 0xfe;
400 case 7:
401 m[line++] = 0xfe;
402 case 8:
403 m[line++] = 0xfe;
404 case 9:
405 m[line++] = 0xfe;
406 }
407 return line; /* that's how many lines we have */
408}
409
410/*
411 * Build a sync frame.
412 */
413static struct sk_buff *
414isdn_v110_sync(isdn_v110_stream *v)
415{
416 struct sk_buff *skb;
417
418 if (v == NULL) {
419 /* invalid handle, no chance to proceed */
420 printk(KERN_WARNING "isdn_v110_sync called with NULL stream!\n");
421 return NULL;
422 }
423 if ((skb = dev_alloc_skb(v->framelen + v->skbres))) {
424 skb_reserve(skb, v->skbres);
425 memcpy(skb_put(skb, v->framelen), v->OfflineFrame, v->framelen);
426 }
427 return skb;
428}
429
430/*
431 * Build an idle frame.
432 */
433static struct sk_buff *
434isdn_v110_idle(isdn_v110_stream *v)
435{
436 struct sk_buff *skb;
437
438 if (v == NULL) {
439 /* invalid handle, no chance to proceed */
440 printk(KERN_WARNING "isdn_v110_sync called with NULL stream!\n");
441 return NULL;
442 }
443 if ((skb = dev_alloc_skb(v->framelen + v->skbres))) {
444 skb_reserve(skb, v->skbres);
445 memcpy(skb_put(skb, v->framelen), v->OnlineFrame, v->framelen);
446 }
447 return skb;
448}
449
450struct sk_buff *
451isdn_v110_encode(isdn_v110_stream * v, struct sk_buff *skb)
452{
453 int i;
454 int j;
455 int rlen;
456 int mlen;
457 int olen;
458 int size;
459 int sval1;
460 int sval2;
461 int nframes;
462 unsigned char *v110buf;
463 unsigned char *rbuf;
464 struct sk_buff *nskb;
465
466 if (v == NULL) {
467 /* invalid handle, no chance to proceed */
468 printk(KERN_WARNING "isdn_v110_encode called with NULL stream!\n");
469 return NULL;
470 }
471 if (!skb) {
472 /* invalid skb, no chance to proceed */
473 printk(KERN_WARNING "isdn_v110_encode called with NULL skb!\n");
474 return NULL;
475 }
476 rlen = skb->len;
477 nframes = (rlen + 3) / 4;
478 v110buf = v->encodebuf;
479 if ((nframes * 40) > v->maxsize) {
480 size = v->maxsize;
481 rlen = v->maxsize / 40;
482 } else
483 size = nframes * 40;
484 if (!(nskb = dev_alloc_skb(size + v->skbres + sizeof(int)))) {
485 printk(KERN_WARNING "isdn_v110_encode: Couldn't alloc skb\n");
486 return NULL;
487 }
488 skb_reserve(nskb, v->skbres + sizeof(int));
489 if (skb->len == 0) {
490 memcpy(skb_put(nskb, v->framelen), v->OnlineFrame, v->framelen);
491 *((int *)skb_push(nskb, sizeof(int))) = 0;
492 return nskb;
493 }
494 mlen = EncodeMatrix(skb->data, rlen, v110buf, size);
495 /* now distribute 2 or 4 bits each to the output stream! */
496 rbuf = skb_put(nskb, size);
497 olen = 0;
498 sval1 = 8 - v->nbits;
499 sval2 = v->key << sval1;
500 for (i = 0; i < mlen; i++) {
501 v110buf[i] = FlipBits(v110buf[i], v->nbits);
502 for (j = 0; j < v->nbytes; j++) {
503 if (size--)
504 *rbuf++ = ~v->key | (((v110buf[i] << (j * v->nbits)) & sval2) >> sval1);
505 else {
506 printk(KERN_WARNING "isdn_v110_encode: buffers full!\n");
507 goto buffer_full;
508 }
509 olen++;
510 }
511 }
512buffer_full:
513 skb_trim(nskb, olen);
514 *((int *)skb_push(nskb, sizeof(int))) = rlen;
515 return nskb;
516}
517
518int
519isdn_v110_stat_callback(int idx, isdn_ctrl * c)
520{
521 isdn_v110_stream *v = NULL;
522 int i;
523 int ret;
524
525 if (idx < 0)
526 return 0;
527 switch (c->command) {
528 case ISDN_STAT_BSENT:
529 /* Keep the send-queue of the driver filled
530 * with frames:
531 * If number of outstanding frames < 3,
532 * send down an Idle-Frame (or an Sync-Frame, if
533 * v->SyncInit != 0).
534 */
535 if (!(v = dev->v110[idx]))
536 return 0;
537 atomic_inc(&dev->v110use[idx]);
538 for (i=0; i * v->framelen < c->parm.length; i++) {
539 if (v->skbidle > 0) {
540 v->skbidle--;
541 ret = 1;
542 } else {
543 if (v->skbuser > 0)
544 v->skbuser--;
545 ret = 0;
546 }
547 }
548 for (i = v->skbuser + v->skbidle; i < 2; i++) {
549 struct sk_buff *skb;
550 if (v->SyncInit > 0)
551 skb = isdn_v110_sync(v);
552 else
553 skb = isdn_v110_idle(v);
554 if (skb) {
555 if (dev->drv[c->driver]->interface->writebuf_skb(c->driver, c->arg, 1, skb) <= 0) {
556 dev_kfree_skb(skb);
557 break;
558 } else {
559 if (v->SyncInit)
560 v->SyncInit--;
561 v->skbidle++;
562 }
563 } else
564 break;
565 }
566 atomic_dec(&dev->v110use[idx]);
567 return ret;
568 case ISDN_STAT_DHUP:
569 case ISDN_STAT_BHUP:
570 while (1) {
571 atomic_inc(&dev->v110use[idx]);
572 if (atomic_dec_and_test(&dev->v110use[idx])) {
573 isdn_v110_close(dev->v110[idx]);
574 dev->v110[idx] = NULL;
575 break;
576 }
577 mdelay(1);
578 }
579 break;
580 case ISDN_STAT_BCONN:
581 if (dev->v110emu[idx] && (dev->v110[idx] == NULL)) {
582 int hdrlen = dev->drv[c->driver]->interface->hl_hdrlen;
583 int maxsize = dev->drv[c->driver]->interface->maxbufsize;
584 atomic_inc(&dev->v110use[idx]);
585 switch (dev->v110emu[idx]) {
586 case ISDN_PROTO_L2_V11096:
587 dev->v110[idx] = isdn_v110_open(V110_9600, hdrlen, maxsize);
588 break;
589 case ISDN_PROTO_L2_V11019:
590 dev->v110[idx] = isdn_v110_open(V110_19200, hdrlen, maxsize);
591 break;
592 case ISDN_PROTO_L2_V11038:
593 dev->v110[idx] = isdn_v110_open(V110_38400, hdrlen, maxsize);
594 break;
595 default:;
596 }
597 if ((v = dev->v110[idx])) {
598 while (v->SyncInit) {
599 struct sk_buff *skb = isdn_v110_sync(v);
600 if (dev->drv[c->driver]->interface->writebuf_skb(c->driver, c->arg, 1, skb) <= 0) {
601 dev_kfree_skb(skb);
602 /* Unable to send, try later */
603 break;
604 }
605 v->SyncInit--;
606 v->skbidle++;
607 }
608 } else
609 printk(KERN_WARNING "isdn_v110: Couldn't open stream for chan %d\n", idx);
610 atomic_dec(&dev->v110use[idx]);
611 }
612 break;
613 default:
614 return 0;
615 }
616 return 0;
617}