| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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; |
| 43 |
unsigned int data; |
| 44 |
int bits; |
| 45 |
int 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
#define FRAME_SEARCH 0 |
| 60 |
#define PROCESS_FRAME 1 |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static unsigned char hdlc_search[256]; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if (c == 0x7e) |
| 141 |
return 0x10 | 8; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
if (c == 0x7f) |
| 146 |
return 0x00 | 8; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if (c == 0xff) |
| 151 |
return 0x00 | 8; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 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 |
|
| 197 |
if (c & 0x0200) { |
| 198 |
|
| 199 |
if ((!(c & 0x0100)) && (bits == 6)) { |
| 200 |
|
| 201 |
return HFP(0, |
| 202 |
0, 8, CONTROL_COMPLETE); |
| 203 |
} else { |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
return HFP(0, |
| 208 |
0, consumed+1, CONTROL_ABORT); |
| 209 |
} |
| 210 |
} else { |
| 211 |
|
| 212 |
ones = 0; |
| 213 |
data <<= 1; |
| 214 |
} |
| 215 |
} else { |
| 216 |
|
| 217 |
|
| 218 |
if (c & 0x0200) { |
| 219 |
data |= 0x80; |
| 220 |
ones++; |
| 221 |
} else |
| 222 |
ones=0; |
| 223 |
bits++; |
| 224 |
} |
| 225 |
c <<= 1; |
| 226 |
} |
| 227 |
|
| 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 |
|
| 267 |
if (ones == 5) { |
| 268 |
|
| 269 |
data <<= 1; |
| 270 |
ones=0; |
| 271 |
bits++; |
| 272 |
} |
| 273 |
if (y & 0x01) { |
| 274 |
|
| 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 |
|
| 287 |
if (ones == 5) { |
| 288 |
|
| 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 |
|
| 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 |
|
| 332 |
for (x=0;x<6;x++) { |
| 333 |
|
| 334 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 477 |
if (h->bits > 24) |
| 478 |
return -1; |
| 479 |
return fasthdlc_rx_load_nocheck(h, b); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 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 |
|
| 495 |
|
| 496 |
switch(h->state) { |
| 497 |
case FRAME_SEARCH: |
| 498 |
|
| 499 |
|
| 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 |
|
| 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 |
|
| 517 |
retval = (RETURN_COMPLETE_FLAG); |
| 518 |
|
| 519 |
h->state = 1; |
| 520 |
} else { |
| 521 |
|
| 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 |
| 533 |
#endif |