Debian Codegraph @

include/dahdi/fasthdlc.h

raw
1 /*
2 * Mark's Mythical Table-based raw HDLC implementation
3 *
4 * This is designed to be a very fast, but memory efficient
5 * implementation of standard HDLC protocol.
6 *
7 * This table based HDLC technology is PATENT PENDING, but will always be
8 * remain freely distributable under the terms of the GPL version 2.
9 *
10 * For non-GPL licensing, please contact Mark Spencer at
11 * the below e-mail address.
12 *
13 * Copyright (C) 2001-2008, Digium, Inc.
14 *
15 * Written by Mark Spencer <[email protected]>
16 *
17 */
18
19 /*
20 * See http://www.asterisk.org for more information about
21 * the Asterisk project. Please do not directly contact
22 * any of the maintainers of this project for assistance;
23 * the project provides a web site, mailing lists and IRC
24 * channels for your use.
25 *
26 * This program is free software, distributed under the terms of
27 * the GNU General Public License Version 2 as published by the
28 * Free Software Foundation. See the LICENSE file included with
29 * this program for more details.
30 */
31
32 #ifndef _FASTHDLC_H
33 #define _FASTHDLC_H
34
35 enum fasthdlc_mode {
36 FASTHDLC_MODE_64 = 0,
37 FASTHDLC_MODE_56,
38 FASTHDLC_MODE_16,
39 };
40
41 struct fasthdlc_state {
42 int state; /* What state we are in */
43 unsigned int data; /* Our current data queue */
44 int bits; /* Number of bits in our data queue */
45 int ones; /* Number of ones */
46 enum fasthdlc_mode mode;
47 unsigned int minbits;
48 };
49
50 #ifdef FAST_HDLC_NEED_TABLES
51 #define RETURN_COMPLETE_FLAG (0x1000)
52 #define RETURN_DISCARD_FLAG (0x2000)
53 #define RETURN_EMPTY_FLAG (0x4000)
54
55 /* Unlike most HDLC implementations, we define only two states,
56 when we are in a valid frame, and when we are searching for
57 a frame header */
58
59 #define FRAME_SEARCH 0
60 #define PROCESS_FRAME 1
61
62 /*
63
64 HDLC Search State table -- Look for a frame header. The return value
65 of this table is as follows:
66
67 |---8---|---7---|---6---|---5---|---4---|---3---|---2---|---1---|
68 | Z E R O E S | Next | Bits Consumed |
69 |-------|-------|-------|-------|-------|-------|-------|-------|
70
71 The indexes for this table are the state (0 or 1) and the next 8
72 bits of the stream.
73
74 Note that this table is only used for state 0 and 1.
75
76 The user should discard the top "bits consumed" bits of data before
77 the next call. "Next state" represents the actual next state for
78 decoding.
79
80 */
81 static unsigned char hdlc_search[256];
82
83 /*
84 HDLC Data Table
85
86 The indexes to this table are the number of one's we've seen so far (0-5) and
87 the next 10 bits of input (which is enough to guarantee us that we
88 will retrieve at least one byte of data (or frame or whatever).
89
90 The format for the return value is:
91
92 Bits 15: Status (1=Valid Data, 0=Control Frame (see bits 7-0 for type))
93 Bits 14-12: Number of ones in a row, so far
94 Bits 11-8: The number of bits consumed (0-10)
95 Bits 7-0: The return data (if appropriate)
96
97 The next state is simply bit #15
98
99 */
100
101 #define CONTROL_COMPLETE 1
102 #define CONTROL_ABORT 2
103
104 #define STATUS_MASK (1 << 15)
105 #define STATUS_VALID (1 << 15)
106 #define STATUS_CONTROL (0 << 15)
107 #define STATE_MASK (1 << 15)
108 #define ONES_MASK (7 << 12)
109 #define DATA_MASK (0xff)
110
111 static unsigned short hdlc_frame[6][1024];
112
113 static unsigned int minbits[2] = { 8, 10 };
114
115 /*
116 Last, but not least, we have the encoder table. It takes
117 as its indices the number of ones so far and a byte of data
118 and returns an int composed of the following fields:
119
120 Bots 31-22: Actual Data
121 Bits 21-16: Unused
122 Bits 15-8: Number of ones
123 Bits 3-0: Number of bits of output (13-4) to use
124
125 Of course we could optimize by reducing to two tables, but I don't
126 really think it's worth the trouble at this point.
127 */
128
129 static unsigned int hdlc_encode[6][256];
130
131 static inline char hdlc_search_precalc(unsigned char c)
132 {
133 int x, p=0;
134 /* Look for a flag. If this isn't a flag,
135 line us up for the next possible shot at
136 a flag */
137
138 /* If it's a flag, we go to state 1, and have
139 consumed 8 bits */
140 if (c == 0x7e)
141 return 0x10 | 8;
142
143 /* If it's an abort, we stay in the same state
144 and have consumed 8 bits */
145 if (c == 0x7f)
146 return 0x00 | 8;
147
148 /* If it's all 1's, we state in the same state and
149 have consumed 8 bits */
150 if (c == 0xff)
151 return 0x00 | 8;
152
153 /* If we get here, we must have at least one zero in us
154 but we're not the flag. So, start at the end (LSB) and
155 work our way to the top (MSB) looking for a zero. The
156 position of that 0 is most optimistic start of a real
157 frame header */
158 x=1;
159 p=7;
160 while(p && (c & x)) {
161 x <<= 1;
162 p--;
163 }
164 return p;
165 }
166
167 #ifdef DEBUG_PRECALC
168 static inline void hdlc_search_print(char c, char r)
169 {
170 int x=0x80;
171 while(x) {
172 printf("%s", c & x ? "1" : "0");
173 x >>= 1;
174 }
175 printf(" => State %d, Consume %d\n", (r & 0x10) >> 4, r & 0xf);
176 }
177 #endif
178
179 #define HFP(status, ones, bits, data) \
180 ((status) | ((ones) << 12) | ((bits) << 8) | (data))
181
182 static inline unsigned int hdlc_frame_precalc(unsigned char x, unsigned short c)
183 {
184 /* Assume we have seen 'x' one's so far, and have read the
185 bottom 10 bytes of c (MSB first). Now, we HAVE to have
186 a byte of data or a frame or something. We are assumed
187 to be at the beginning of a byte of data or something */
188 unsigned char ones = x;
189 unsigned char data=0;
190 int bits=0;
191 int consumed=0;
192 while(bits < 8) {
193 data >>=1;
194 consumed++;
195 if (ones == 5) {
196 /* We've seen five ones */
197 if (c & 0x0200) {
198 /* Another one -- Some sort of signal frame */
199 if ((!(c & 0x0100)) && (bits == 6)) {
200 /* This is a frame terminator (10) */
201 return HFP(0,
202 0, 8, CONTROL_COMPLETE);
203 } else {
204 /* Yuck! It's something else...
205 Abort this entire frame, and
206 start looking for a good frame */
207 return HFP(0,
208 0, consumed+1, CONTROL_ABORT);
209 }
210 } else {
211 /* It's an inserted zero, just skip it */
212 ones = 0;
213 data <<= 1;
214 }
215 } else {
216 /* Add it to our bit list, LSB to
217 MSB */
218 if (c & 0x0200) {
219 data |= 0x80;
220 ones++;
221 } else
222 ones=0;
223 bits++;
224 }
225 c <<= 1;
226 }
227 /* Consume the extra 0 now rather than later. */
228 if (ones == 5) {
229 ones = 0;
230 consumed++;
231 }
232 return HFP(STATUS_VALID, ones, consumed, data);
233 }
234
235 #ifdef DEBUG_PRECALC
236
237 static inline void hdlc_frame_print(unsigned char x, unsigned short c, unsigned int res)
238 {
239 int z=0x0200;
240 char *status[] = {
241 "Control",
242 "Valid",
243 };
244 printf("%d one's then ", x);
245 while(z) {
246 printf("%s", c & z ? "1" : "0");
247 z >>= 1;
248 }
249 printf(" => Status %s, ", res & STATUS_MASK ? "1" : "0");
250 printf("Consumed: %d, ", (res & 0x0f00) >> 8);
251 printf("Status: %s, ", status[(res & STATUS_MASK) >> 15]);
252 printf("Ones: %d, ", (res & ONES_MASK) >> 12);
253 printf("Data: %02x\n", res & 0xff);
254
255 }
256
257 #endif
258
259 static inline unsigned int hdlc_encode_precalc(int x, unsigned char y)
260 {
261 int bits=0;
262 int ones=x;
263 unsigned short data=0;
264 int z;
265 for (z=0;z<8;z++) {
266 /* Zero-stuff if needed */
267 if (ones == 5) {
268 /* Stuff a zero */
269 data <<= 1;
270 ones=0;
271 bits++;
272 }
273 if (y & 0x01) {
274 /* There's a one */
275 data <<= 1;
276 data |= 0x1;
277 ones++;
278 bits++;
279 } else {
280 data <<= 1;
281 ones = 0;
282 bits++;
283 }
284 y >>= 1;
285 }
286 /* Special case -- Stuff the zero at the end if appropriate */
287 if (ones == 5) {
288 /* Stuff a zero */
289 data <<= 1;
290 ones=0;
291 bits++;
292 }
293 data <<= (10-bits);
294 return (data << 22) | (ones << 8) | (bits);
295 }
296
297 #ifdef DEBUG_PRECALC
298 static inline void hdlc_encode_print(int x, unsigned char y, unsigned int val)
299 {
300 unsigned int z;
301 unsigned short c;
302 printf("%d ones, %02x (", x, y);
303 z = 0x80;
304 while(z) {
305 printf("%s", y & z ? "1" : "0");
306 z >>= 1;
307 }
308 printf(") encoded as ");
309 z = 1 << 31;
310 for (x=0;x<(val & 0xf);x++) {
311 printf("%s", val & z ? "1" : "0");
312 z >>= 1;
313 }
314 printf(" with %d ones now, %d bits in len\n", (val & 0xf00) >> 8, val & 0xf);
315
316
317 }
318 #endif
319
320 static inline void fasthdlc_precalc(void)
321 {
322 int x;
323 int y;
324 /* First the easy part -- the searching */
325 for (x=0;x<256;x++) {
326 hdlc_search[x] = hdlc_search_precalc(x);
327 #ifdef DEBUG_PRECALC
328 hdlc_search_print(x, hdlc_search[x]);
329 #endif
330 }
331 /* Now the hard part -- the frame tables */
332 for (x=0;x<6;x++) {
333 /* Given the # of preceeding ones, process the next
334 byte of input (up to 10 actual bits) */
335 for (y=0;y<1024;y++) {
336 hdlc_frame[x][y] = hdlc_frame_precalc(x, y);
337 #ifdef DEBUG_PRECALC
338 hdlc_frame_print(x, y, hdlc_frame[x][y]);
339 #endif
340 }
341 }
342 /* Now another not-so-hard part, the encoding table */
343 for (x=0;x<6;x++) {
344 for (y=0;y<256;y++) {
345 hdlc_encode[x][y] = hdlc_encode_precalc(x,y);
346 #ifdef DEBUG_PRECALC
347 hdlc_encode_print(x,y,hdlc_encode[x][y]);
348 #endif
349 }
350 }
351 }
352
353
354 static inline void fasthdlc_init(struct fasthdlc_state *h, enum fasthdlc_mode mode)
355 {
356 /* Initializes all states appropriately */
357 h->mode = mode;
358 h->state = 0;
359 h->bits = 0;
360 h->data = 0;
361 h->ones = 0;
362
363 switch (mode) {
364 case FASTHDLC_MODE_64:
365 h->minbits = 8;
366 break;
367 case FASTHDLC_MODE_56:
368 h->minbits = 7;
369 break;
370 case FASTHDLC_MODE_16:
371 h->minbits = 2;
372 break;
373 }
374
375 }
376
377 static inline int fasthdlc_tx_load_nocheck(struct fasthdlc_state *h, unsigned char c)
378 {
379 unsigned int res;
380 res = hdlc_encode[h->ones][c];
381 h->ones = (res & 0xf00) >> 8;
382 h->data |= (res & 0xffc00000) >> h->bits;
383 h->bits += (res & 0xf);
384 return 0;
385 }
386
387 static inline int fasthdlc_tx_load(struct fasthdlc_state *h, unsigned char c)
388 {
389 /* Gotta have at least 10 bits left */
390 if (h->bits > 22)
391 return -1;
392 return fasthdlc_tx_load_nocheck(h, c);
393 }
394
395 static inline int fasthdlc_tx_frame_nocheck(struct fasthdlc_state *h)
396 {
397 h->ones = 0;
398 h->data |= ( 0x7e000000 >> h->bits);
399 h->bits += 8;
400 return 0;
401 }
402
403 static inline int fasthdlc_tx_frame(struct fasthdlc_state *h)
404 {
405 if (h->bits > 24)
406 return -1;
407 return fasthdlc_tx_frame_nocheck(h);
408 }
409
410 static inline int fasthdlc_tx_need_data(struct fasthdlc_state *h)
411 {
412 if (h->mode == FASTHDLC_MODE_56) {
413 if (h->bits < 7)
414 return 1;
415 } else if (h->mode == FASTHDLC_MODE_16) {
416 if (h->bits < 2)
417 return 1;
418 } else {
419 if (h->bits < 8)
420 return 1;
421 }
422
423 return 0;
424 }
425
426 static inline int fasthdlc_tx_run_nocheck(struct fasthdlc_state *h)
427 {
428 unsigned char b;
429 if (h->mode == FASTHDLC_MODE_16) {
430 b = h->data >> 30;
431 h->bits -= 2;
432 h->data <<= 2;
433
434 return (b & 3) << 6;
435 } else if (h->mode == FASTHDLC_MODE_56) {
436 b = h->data >> 25;
437 h->bits -= 7;
438 h->data <<= 7;
439
440 return ((b & 0x7f) << 1) | 1;
441 } else {
442 b = h->data >> 24;
443 h->bits -= 8;
444 h->data <<= 8;
445
446 return b;
447 }
448
449 }
450
451 static inline int fasthdlc_tx_run(struct fasthdlc_state *h)
452 {
453 if (h->bits < h->minbits)
454 return -1;
455 return fasthdlc_tx_run_nocheck(h);
456 }
457
458 static inline int fasthdlc_rx_load_nocheck(struct fasthdlc_state *h, unsigned char b)
459 {
460 if (h->mode == FASTHDLC_MODE_16) {
461 h->data |= (b >> 6) << (30-h->bits);
462 h->bits += 2;
463 } else if (h->mode == FASTHDLC_MODE_56) {
464 h->data |= (b >> 1) << (25-h->bits);
465 h->bits += 7;
466 } else {
467 /* Put the new byte in the data stream */
468 h->data |= b << (24-h->bits);
469 h->bits += 8;
470 }
471 return 0;
472 }
473
474 static inline int fasthdlc_rx_load(struct fasthdlc_state *h, unsigned char b)
475 {
476 /* Make sure we have enough space */
477 if (h->bits > 24)
478 return -1;
479 return fasthdlc_rx_load_nocheck(h, b);
480 }
481
482 /*
483 Returns a data character if available, logical OR'd with
484 zero or more of RETURN_COMPLETE_FLAG, RETURN_DISCARD_FLAG,
485 and RETURN_EMPTY_FLAG, signifying a complete frame, a
486 discarded frame, or there is nothing to return.
487 */
488
489 static inline int fasthdlc_rx_run(struct fasthdlc_state *h)
490 {
491 unsigned short next;
492 int retval=RETURN_EMPTY_FLAG;
493 while ((h->bits >= minbits[h->state]) && (retval == RETURN_EMPTY_FLAG)) {
494 /* Run until we can no longer be assured that we will
495 have enough bits to continue */
496 switch(h->state) {
497 case FRAME_SEARCH:
498 /* Look for an HDLC frame, keying from
499 the top byte. */
500 next = hdlc_search[h->data >> 24];
501 h->bits -= next & 0x0f;
502 h->data <<= next & 0x0f;
503 h->state = next >> 4;
504 h->ones = 0;
505 break;
506 case PROCESS_FRAME:
507 /* Process as much as the next ten bits */
508 next = hdlc_frame[h->ones][h->data >> 22];
509 h->bits -= ((next & 0x0f00) >> 8);
510 h->data <<= ((next & 0x0f00) >> 8);
511 h->state = (next & STATE_MASK) >> 15;
512 h->ones = (next & ONES_MASK) >> 12;
513 switch(next & STATUS_MASK) {
514 case STATUS_CONTROL:
515 if (next & CONTROL_COMPLETE) {
516 /* A complete, valid frame received */
517 retval = (RETURN_COMPLETE_FLAG);
518 /* Stay in this state */
519 h->state = 1;
520 } else {
521 /* An abort (either out of sync of explicit) */
522 retval = (RETURN_DISCARD_FLAG);
523 }
524 break;
525 case STATUS_VALID:
526 retval = (next & DATA_MASK);
527 }
528 }
529 }
530 return retval;
531 }
532 #endif /* FAST_HDLC_NEED_TABLES */
533 #endif