2 * Driver for ST5481 USB ISDN modem
5 * Copyright 2001 by Frode Isaksen <fisaksen@bewan.com>
6 * 2001 by Kai Germaschewski <kai.germaschewski@gmx.de>
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
13 #include <linux/crc-ccitt.h>
14 #include "st5481_hdlc.h"
18 HDLC_FAST_IDLE
,HDLC_GET_FLAG_B0
,HDLC_GETFLAG_B1A6
,HDLC_GETFLAG_B7
,
19 HDLC_GET_DATA
,HDLC_FAST_FLAG
23 HDLC_SEND_DATA
,HDLC_SEND_CRC1
,HDLC_SEND_FAST_FLAG
,
24 HDLC_SEND_FIRST_FLAG
,HDLC_SEND_CRC2
,HDLC_SEND_CLOSING_FLAG
,
25 HDLC_SEND_IDLE1
,HDLC_SEND_FAST_IDLE
,HDLC_SENDFLAG_B0
,
26 HDLC_SENDFLAG_B1A6
,HDLC_SENDFLAG_B7
,STOPPED
30 hdlc_rcv_init(struct hdlc_vars
*hdlc
, int do_adapt56
)
35 hdlc
->ffbit_shift
= 0;
36 hdlc
->data_received
= 0;
37 hdlc
->state
= HDLC_GET_DATA
;
38 hdlc
->do_adapt56
= do_adapt56
;
48 hdlc_out_init(struct hdlc_vars
*hdlc
, int is_d_channel
, int do_adapt56
)
53 hdlc
->ffbit_shift
= 0;
54 hdlc
->data_received
= 0;
59 hdlc
->state
= HDLC_SEND_FIRST_FLAG
;
62 hdlc
->state
= HDLC_SEND_FAST_FLAG
;
70 hdlc
->state
= HDLC_SENDFLAG_B0
;
79 hdlc_decode - decodes HDLC frames from a transparent bit stream.
81 The source buffer is scanned for valid HDLC frames looking for
82 flags (01111110) to indicate the start of a frame. If the start of
83 the frame is found, the bit stuffing is removed (0 after 5 1's).
84 When a new flag is found, the complete frame has been received
85 and the CRC is checked.
86 If a valid frame is found, the function returns the frame length
87 excluding the CRC with the bit HDLC_END_OF_FRAME set.
88 If the beginning of a valid frame is found, the function returns
90 If a framing error is found (too many 1s and not a flag) the function
91 returns the length with the bit HDLC_FRAMING_ERROR set.
92 If a CRC error is found the function returns the length with the
93 bit HDLC_CRC_ERROR set.
94 If the frame length exceeds the destination buffer size, the function
95 returns the length with the bit HDLC_LENGTH_ERROR set.
98 slen - source buffer length
99 count - number of bytes removed (decoded) from the source buffer
100 dst _ destination buffer
101 dsize - destination buffer size
102 returns - number of decoded bytes in the destination buffer and status
105 int hdlc_decode(struct hdlc_vars
*hdlc
, const unsigned char *src
,
106 int slen
, int *count
, unsigned char *dst
, int dsize
)
110 static const unsigned char fast_flag
[]={
111 0x00,0x00,0x00,0x20,0x30,0x38,0x3c,0x3e,0x3f
114 static const unsigned char fast_flag_value
[]={
115 0x00,0x7e,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f
118 static const unsigned char fast_abort
[]={
119 0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff
125 if(hdlc
->bit_shift
==0){
129 if(hdlc
->do_adapt56
){
138 if(hdlc
->cbin
== 0xff){
142 hdlc
->state
= HDLC_GET_FLAG_B0
;
143 hdlc
->hdlc_bits1
= 0;
146 case HDLC_GET_FLAG_B0
:
147 if(!(hdlc
->cbin
& 0x80)) {
148 hdlc
->state
= HDLC_GETFLAG_B1A6
;
149 hdlc
->hdlc_bits1
= 0;
151 if(!hdlc
->do_adapt56
){
152 if(++hdlc
->hdlc_bits1
>=8 ) if(hdlc
->bit_shift
==1)
153 hdlc
->state
= HDLC_FAST_IDLE
;
159 case HDLC_GETFLAG_B1A6
:
160 if(hdlc
->cbin
& 0x80){
162 if(hdlc
->hdlc_bits1
==6){
163 hdlc
->state
= HDLC_GETFLAG_B7
;
166 hdlc
->hdlc_bits1
= 0;
171 case HDLC_GETFLAG_B7
:
172 if(hdlc
->cbin
& 0x80) {
173 hdlc
->state
= HDLC_GET_FLAG_B0
;
175 hdlc
->state
= HDLC_GET_DATA
;
178 hdlc
->hdlc_bits1
= 0;
180 hdlc
->data_received
= 0;
186 if(hdlc
->cbin
& 0x80){
188 switch(hdlc
->hdlc_bits1
){
192 if(hdlc
->data_received
) {
194 status
= -HDLC_FRAMING_ERROR
;
196 if(!hdlc
->do_adapt56
){
197 if(hdlc
->cbin
==fast_abort
[hdlc
->bit_shift
+1]){
198 hdlc
->state
= HDLC_FAST_IDLE
;
203 hdlc
->state
= HDLC_GET_FLAG_B0
;
208 hdlc
->shift_reg
|= 0x80;
213 switch(hdlc
->hdlc_bits1
){
217 if(hdlc
->data_received
){
218 if (hdlc
->dstpos
< 2) {
219 status
= -HDLC_FRAMING_ERROR
;
220 } else if (hdlc
->crc
!= 0xf0b8){
222 status
= -HDLC_CRC_ERROR
;
227 status
= hdlc
->dstpos
;
233 if(!hdlc
->do_adapt56
){
234 if(hdlc
->cbin
==fast_flag
[hdlc
->bit_shift
]){
235 hdlc
->ffvalue
= fast_flag_value
[hdlc
->bit_shift
];
236 hdlc
->state
= HDLC_FAST_FLAG
;
237 hdlc
->ffbit_shift
= hdlc
->bit_shift
;
240 hdlc
->state
= HDLC_GET_DATA
;
241 hdlc
->data_received
= 0;
244 hdlc
->state
= HDLC_GET_DATA
;
245 hdlc
->data_received
= 0;
253 hdlc
->hdlc_bits1
= 0;
262 if(hdlc
->data_bits
==8){
264 hdlc
->data_received
= 1;
265 hdlc
->crc
= crc_ccitt_byte(hdlc
->crc
, hdlc
->shift_reg
);
267 // good byte received
269 dst
[hdlc
->dstpos
++] = hdlc
->shift_reg
;
272 status
= -HDLC_LENGTH_ERROR
;
280 if(hdlc
->cbin
==hdlc
->ffvalue
){
284 if(hdlc
->cbin
== 0xff){
285 hdlc
->state
= HDLC_FAST_IDLE
;
287 } else if(hdlc
->ffbit_shift
==8){
288 hdlc
->state
= HDLC_GETFLAG_B7
;
291 hdlc
->shift_reg
= fast_abort
[hdlc
->ffbit_shift
-1];
292 hdlc
->hdlc_bits1
= hdlc
->ffbit_shift
-2;
293 if(hdlc
->hdlc_bits1
<0)hdlc
->hdlc_bits1
= 0;
294 hdlc
->data_bits
= hdlc
->ffbit_shift
-1;
295 hdlc
->state
= HDLC_GET_DATA
;
296 hdlc
->data_received
= 0;
309 hdlc_encode - encodes HDLC frames to a transparent bit stream.
311 The bit stream starts with a beginning flag (01111110). After
312 that each byte is added to the bit stream with bit stuffing added
314 When the last byte has been removed from the source buffer, the
315 CRC (2 bytes is added) and the frame terminates with the ending flag.
316 For the dchannel, the idle character (all 1's) is also added at the end.
317 If this function is called with empty source buffer (slen=0), flags or
318 idle character will be generated.
321 slen - source buffer length
322 count - number of bytes removed (encoded) from source buffer
323 dst _ destination buffer
324 dsize - destination buffer size
325 returns - number of encoded bytes in the destination buffer
327 int hdlc_encode(struct hdlc_vars
*hdlc
, const unsigned char *src
,
328 unsigned short slen
, int *count
,
329 unsigned char *dst
, int dsize
)
331 static const unsigned char xfast_flag_value
[] = {
332 0x7e,0x3f,0x9f,0xcf,0xe7,0xf3,0xf9,0xfc,0x7e
340 if(hdlc
->bit_shift
==0){
341 if(slen
&& !hdlc
->do_closing
){
342 hdlc
->shift_reg
= *src
++;
345 hdlc
->do_closing
= 1; /* closing sequence, CRC + flag(s) */
348 if(hdlc
->state
== HDLC_SEND_DATA
){
349 if(hdlc
->data_received
){
350 hdlc
->state
= HDLC_SEND_CRC1
;
353 hdlc
->shift_reg
= hdlc
->crc
& 0xff;
354 } else if(!hdlc
->do_adapt56
){
355 hdlc
->state
= HDLC_SEND_FAST_FLAG
;
357 hdlc
->state
= HDLC_SENDFLAG_B0
;
370 case HDLC_SEND_FAST_FLAG
:
371 hdlc
->do_closing
= 0;
373 *dst
++ = hdlc
->ffvalue
;
378 if(hdlc
->bit_shift
==8){
379 hdlc
->cbin
= hdlc
->ffvalue
>>(8-hdlc
->data_bits
);
380 hdlc
->state
= HDLC_SEND_DATA
;
382 hdlc
->hdlc_bits1
= 0;
383 hdlc
->data_received
= 1;
386 case HDLC_SENDFLAG_B0
:
387 hdlc
->do_closing
= 0;
390 hdlc
->hdlc_bits1
= 0;
391 hdlc
->state
= HDLC_SENDFLAG_B1A6
;
393 case HDLC_SENDFLAG_B1A6
:
397 if(++hdlc
->hdlc_bits1
== 6)
398 hdlc
->state
= HDLC_SENDFLAG_B7
;
400 case HDLC_SENDFLAG_B7
:
404 hdlc
->state
= HDLC_SENDFLAG_B0
;
407 if(hdlc
->bit_shift
==8){
408 hdlc
->state
= HDLC_SEND_DATA
;
410 hdlc
->hdlc_bits1
= 0;
411 hdlc
->data_received
= 1;
414 case HDLC_SEND_FIRST_FLAG
:
415 hdlc
->data_received
= 1;
416 if(hdlc
->data_bits
==8){
417 hdlc
->state
= HDLC_SEND_DATA
;
419 hdlc
->hdlc_bits1
= 0;
424 if(hdlc
->shift_reg
& 0x01)
426 hdlc
->shift_reg
>>= 1;
428 if(hdlc
->bit_shift
==0){
429 hdlc
->state
= HDLC_SEND_DATA
;
431 hdlc
->hdlc_bits1
= 0;
437 if(hdlc
->hdlc_bits1
== 5){
438 hdlc
->hdlc_bits1
= 0;
441 if(hdlc
->bit_shift
==8){
442 hdlc
->crc
= crc_ccitt_byte(hdlc
->crc
, hdlc
->shift_reg
);
444 if(hdlc
->shift_reg
& 0x01){
447 hdlc
->shift_reg
>>= 1;
450 hdlc
->hdlc_bits1
= 0;
451 hdlc
->shift_reg
>>= 1;
458 if(hdlc
->hdlc_bits1
== 5){
459 hdlc
->hdlc_bits1
= 0;
462 if(hdlc
->shift_reg
& 0x01){
465 hdlc
->shift_reg
>>= 1;
468 hdlc
->hdlc_bits1
= 0;
469 hdlc
->shift_reg
>>= 1;
472 if(hdlc
->bit_shift
==0){
473 hdlc
->shift_reg
= (hdlc
->crc
>> 8);
474 hdlc
->state
= HDLC_SEND_CRC2
;
481 if(hdlc
->hdlc_bits1
== 5){
482 hdlc
->hdlc_bits1
= 0;
485 if(hdlc
->shift_reg
& 0x01){
488 hdlc
->shift_reg
>>= 1;
491 hdlc
->hdlc_bits1
= 0;
492 hdlc
->shift_reg
>>= 1;
495 if(hdlc
->bit_shift
==0){
496 hdlc
->shift_reg
= 0x7e;
497 hdlc
->state
= HDLC_SEND_CLOSING_FLAG
;
501 case HDLC_SEND_CLOSING_FLAG
:
504 if(hdlc
->hdlc_bits1
== 5){
505 hdlc
->hdlc_bits1
= 0;
508 if(hdlc
->shift_reg
& 0x01){
511 hdlc
->shift_reg
>>= 1;
513 if(hdlc
->bit_shift
==0){
514 hdlc
->ffvalue
= xfast_flag_value
[hdlc
->data_bits
];
516 hdlc
->ffvalue
= 0x7e;
517 hdlc
->state
= HDLC_SEND_IDLE1
;
518 hdlc
->bit_shift
= 8-hdlc
->data_bits
;
519 if(hdlc
->bit_shift
==0)
520 hdlc
->state
= HDLC_SEND_FAST_IDLE
;
522 if(!hdlc
->do_adapt56
){
523 hdlc
->state
= HDLC_SEND_FAST_FLAG
;
524 hdlc
->data_received
= 0;
526 hdlc
->state
= HDLC_SENDFLAG_B0
;
527 hdlc
->data_received
= 0;
529 // Finished with this frame, send flags
530 if (dsize
> 1) dsize
= 1;
534 case HDLC_SEND_IDLE1
:
535 hdlc
->do_closing
= 0;
540 if(hdlc
->bit_shift
==0){
541 hdlc
->state
= HDLC_SEND_FAST_IDLE
;
545 case HDLC_SEND_FAST_IDLE
:
546 hdlc
->do_closing
= 0;
549 if(hdlc
->bit_shift
== 8){
551 hdlc
->state
= HDLC_SEND_FIRST_FLAG
;
554 hdlc
->bit_shift
= hdlc
->data_bits
= 0;
562 if(hdlc
->do_adapt56
){
563 if(hdlc
->data_bits
==7){
569 if(hdlc
->data_bits
==8){