Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * isdnhdlc.c -- General purpose ISDN HDLC decoder. |
| 3 | * |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 4 | * Copyright (C) |
| 5 | * 2002 Wolfgang Mües <wolfgang@iksw-muees.de> |
| 6 | * 2001 Frode Isaksen <fisaksen@bewan.com> |
| 7 | * 2001 Kai Germaschewski <kai.germaschewski@gmx.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/crc-ccitt.h> |
Karsten Keil | cb3824b | 2009-07-08 14:21:12 +0200 | [diff] [blame] | 27 | #include <linux/isdn/hdlc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | /*-------------------------------------------------------------------*/ |
| 30 | |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 31 | MODULE_AUTHOR("Wolfgang Mües <wolfgang@iksw-muees.de>, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | "Frode Isaksen <fisaksen@bewan.com>, " |
| 33 | "Kai Germaschewski <kai.germaschewski@gmx.de>"); |
| 34 | MODULE_DESCRIPTION("General purpose ISDN HDLC decoder"); |
| 35 | MODULE_LICENSE("GPL"); |
| 36 | |
| 37 | /*-------------------------------------------------------------------*/ |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | enum { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 40 | HDLC_FAST_IDLE, HDLC_GET_FLAG_B0, HDLC_GETFLAG_B1A6, HDLC_GETFLAG_B7, |
| 41 | HDLC_GET_DATA, HDLC_FAST_FLAG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | enum { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 45 | HDLC_SEND_DATA, HDLC_SEND_CRC1, HDLC_SEND_FAST_FLAG, |
| 46 | HDLC_SEND_FIRST_FLAG, HDLC_SEND_CRC2, HDLC_SEND_CLOSING_FLAG, |
| 47 | HDLC_SEND_IDLE1, HDLC_SEND_FAST_IDLE, HDLC_SENDFLAG_B0, |
| 48 | HDLC_SENDFLAG_B1A6, HDLC_SENDFLAG_B7, STOPPED |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 51 | void isdnhdlc_rcv_init(struct isdnhdlc_vars *hdlc, int do_adapt56) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 53 | hdlc->bit_shift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | hdlc->hdlc_bits1 = 0; |
| 55 | hdlc->data_bits = 0; |
| 56 | hdlc->ffbit_shift = 0; |
| 57 | hdlc->data_received = 0; |
| 58 | hdlc->state = HDLC_GET_DATA; |
| 59 | hdlc->do_adapt56 = do_adapt56; |
| 60 | hdlc->dchannel = 0; |
| 61 | hdlc->crc = 0; |
| 62 | hdlc->cbin = 0; |
| 63 | hdlc->shift_reg = 0; |
| 64 | hdlc->ffvalue = 0; |
| 65 | hdlc->dstpos = 0; |
| 66 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 67 | EXPORT_SYMBOL(isdnhdlc_out_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 69 | void isdnhdlc_out_init(struct isdnhdlc_vars *hdlc, int is_d_channel, |
| 70 | int do_adapt56) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 72 | hdlc->bit_shift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | hdlc->hdlc_bits1 = 0; |
| 74 | hdlc->data_bits = 0; |
| 75 | hdlc->ffbit_shift = 0; |
| 76 | hdlc->data_received = 0; |
| 77 | hdlc->do_closing = 0; |
| 78 | hdlc->ffvalue = 0; |
| 79 | if (is_d_channel) { |
| 80 | hdlc->dchannel = 1; |
| 81 | hdlc->state = HDLC_SEND_FIRST_FLAG; |
| 82 | } else { |
| 83 | hdlc->dchannel = 0; |
| 84 | hdlc->state = HDLC_SEND_FAST_FLAG; |
| 85 | hdlc->ffvalue = 0x7e; |
| 86 | } |
| 87 | hdlc->cbin = 0x7e; |
| 88 | hdlc->bit_shift = 0; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 89 | if (do_adapt56) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | hdlc->do_adapt56 = 1; |
| 91 | hdlc->data_bits = 0; |
| 92 | hdlc->state = HDLC_SENDFLAG_B0; |
| 93 | } else { |
| 94 | hdlc->do_adapt56 = 0; |
| 95 | hdlc->data_bits = 8; |
| 96 | } |
| 97 | hdlc->shift_reg = 0; |
| 98 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 99 | EXPORT_SYMBOL(isdnhdlc_rcv_init); |
| 100 | |
| 101 | static int |
| 102 | check_frame(struct isdnhdlc_vars *hdlc) |
| 103 | { |
| 104 | int status; |
| 105 | |
| 106 | if (hdlc->dstpos < 2) /* too small - framing error */ |
| 107 | status = -HDLC_FRAMING_ERROR; |
| 108 | else if (hdlc->crc != 0xf0b8) /* crc error */ |
| 109 | status = -HDLC_CRC_ERROR; |
| 110 | else { |
| 111 | /* remove CRC */ |
| 112 | hdlc->dstpos -= 2; |
| 113 | /* good frame */ |
| 114 | status = hdlc->dstpos; |
| 115 | } |
| 116 | return status; |
| 117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | isdnhdlc_decode - decodes HDLC frames from a transparent bit stream. |
| 121 | |
| 122 | The source buffer is scanned for valid HDLC frames looking for |
| 123 | flags (01111110) to indicate the start of a frame. If the start of |
| 124 | the frame is found, the bit stuffing is removed (0 after 5 1's). |
| 125 | When a new flag is found, the complete frame has been received |
| 126 | and the CRC is checked. |
| 127 | If a valid frame is found, the function returns the frame length |
| 128 | excluding the CRC with the bit HDLC_END_OF_FRAME set. |
| 129 | If the beginning of a valid frame is found, the function returns |
| 130 | the length. |
| 131 | If a framing error is found (too many 1s and not a flag) the function |
| 132 | returns the length with the bit HDLC_FRAMING_ERROR set. |
| 133 | If a CRC error is found the function returns the length with the |
| 134 | bit HDLC_CRC_ERROR set. |
| 135 | If the frame length exceeds the destination buffer size, the function |
| 136 | returns the length with the bit HDLC_LENGTH_ERROR set. |
| 137 | |
| 138 | src - source buffer |
| 139 | slen - source buffer length |
| 140 | count - number of bytes removed (decoded) from the source buffer |
| 141 | dst _ destination buffer |
| 142 | dsize - destination buffer size |
| 143 | returns - number of decoded bytes in the destination buffer and status |
| 144 | flag. |
| 145 | */ |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 146 | int isdnhdlc_decode(struct isdnhdlc_vars *hdlc, const u8 *src, int slen, |
| 147 | int *count, u8 *dst, int dsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 149 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 151 | static const unsigned char fast_flag[] = { |
| 152 | 0x00, 0x00, 0x00, 0x20, 0x30, 0x38, 0x3c, 0x3e, 0x3f |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 155 | static const unsigned char fast_flag_value[] = { |
| 156 | 0x00, 0x7e, 0xfc, 0xf9, 0xf3, 0xe7, 0xcf, 0x9f, 0x3f |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 159 | static const unsigned char fast_abort[] = { |
| 160 | 0x00, 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 163 | #define handle_fast_flag(h) \ |
| 164 | do {\ |
| 165 | if (h->cbin == fast_flag[h->bit_shift]) {\ |
| 166 | h->ffvalue = fast_flag_value[h->bit_shift];\ |
| 167 | h->state = HDLC_FAST_FLAG;\ |
| 168 | h->ffbit_shift = h->bit_shift;\ |
| 169 | h->bit_shift = 1;\ |
| 170 | } else {\ |
| 171 | h->state = HDLC_GET_DATA;\ |
| 172 | h->data_received = 0;\ |
| 173 | } \ |
| 174 | } while (0) |
| 175 | |
| 176 | #define handle_abort(h) \ |
| 177 | do {\ |
| 178 | h->shift_reg = fast_abort[h->ffbit_shift - 1];\ |
| 179 | h->hdlc_bits1 = h->ffbit_shift - 2;\ |
| 180 | if (h->hdlc_bits1 < 0)\ |
| 181 | h->hdlc_bits1 = 0;\ |
| 182 | h->data_bits = h->ffbit_shift - 1;\ |
| 183 | h->state = HDLC_GET_DATA;\ |
| 184 | h->data_received = 0;\ |
| 185 | } while (0) |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | *count = slen; |
| 188 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 189 | while (slen > 0) { |
| 190 | if (hdlc->bit_shift == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | hdlc->cbin = *src++; |
| 192 | slen--; |
| 193 | hdlc->bit_shift = 8; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 194 | if (hdlc->do_adapt56) |
| 195 | hdlc->bit_shift--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 198 | switch (hdlc->state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | case STOPPED: |
| 200 | return 0; |
| 201 | case HDLC_FAST_IDLE: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 202 | if (hdlc->cbin == 0xff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | hdlc->bit_shift = 0; |
| 204 | break; |
| 205 | } |
| 206 | hdlc->state = HDLC_GET_FLAG_B0; |
| 207 | hdlc->hdlc_bits1 = 0; |
| 208 | hdlc->bit_shift = 8; |
| 209 | break; |
| 210 | case HDLC_GET_FLAG_B0: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 211 | if (!(hdlc->cbin & 0x80)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | hdlc->state = HDLC_GETFLAG_B1A6; |
| 213 | hdlc->hdlc_bits1 = 0; |
| 214 | } else { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 215 | if ((!hdlc->do_adapt56) && |
| 216 | (++hdlc->hdlc_bits1 >= 8) && |
| 217 | (hdlc->bit_shift == 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | hdlc->state = HDLC_FAST_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 220 | hdlc->cbin <<= 1; |
| 221 | hdlc->bit_shift--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | break; |
| 223 | case HDLC_GETFLAG_B1A6: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 224 | if (hdlc->cbin & 0x80) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | hdlc->hdlc_bits1++; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 226 | if (hdlc->hdlc_bits1 == 6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | hdlc->state = HDLC_GETFLAG_B7; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 228 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | hdlc->hdlc_bits1 = 0; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 230 | hdlc->cbin <<= 1; |
| 231 | hdlc->bit_shift--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | break; |
| 233 | case HDLC_GETFLAG_B7: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 234 | if (hdlc->cbin & 0x80) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | hdlc->state = HDLC_GET_FLAG_B0; |
| 236 | } else { |
| 237 | hdlc->state = HDLC_GET_DATA; |
| 238 | hdlc->crc = 0xffff; |
| 239 | hdlc->shift_reg = 0; |
| 240 | hdlc->hdlc_bits1 = 0; |
| 241 | hdlc->data_bits = 0; |
| 242 | hdlc->data_received = 0; |
| 243 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 244 | hdlc->cbin <<= 1; |
| 245 | hdlc->bit_shift--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | break; |
| 247 | case HDLC_GET_DATA: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 248 | if (hdlc->cbin & 0x80) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | hdlc->hdlc_bits1++; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 250 | switch (hdlc->hdlc_bits1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | case 6: |
| 252 | break; |
| 253 | case 7: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 254 | if (hdlc->data_received) |
| 255 | /* bad frame */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | status = -HDLC_FRAMING_ERROR; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 257 | if (!hdlc->do_adapt56) { |
| 258 | if (hdlc->cbin == fast_abort |
| 259 | [hdlc->bit_shift + 1]) { |
| 260 | hdlc->state = |
| 261 | HDLC_FAST_IDLE; |
| 262 | hdlc->bit_shift = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | break; |
| 264 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 265 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | hdlc->state = HDLC_GET_FLAG_B0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | break; |
| 268 | default: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 269 | hdlc->shift_reg >>= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | hdlc->shift_reg |= 0x80; |
| 271 | hdlc->data_bits++; |
| 272 | break; |
| 273 | } |
| 274 | } else { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 275 | switch (hdlc->hdlc_bits1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | case 5: |
| 277 | break; |
| 278 | case 6: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 279 | if (hdlc->data_received) |
| 280 | status = check_frame(hdlc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | hdlc->crc = 0xffff; |
| 282 | hdlc->shift_reg = 0; |
| 283 | hdlc->data_bits = 0; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 284 | if (!hdlc->do_adapt56) |
| 285 | handle_fast_flag(hdlc); |
| 286 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | hdlc->state = HDLC_GET_DATA; |
| 288 | hdlc->data_received = 0; |
| 289 | } |
| 290 | break; |
| 291 | default: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 292 | hdlc->shift_reg >>= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | hdlc->data_bits++; |
| 294 | break; |
| 295 | } |
| 296 | hdlc->hdlc_bits1 = 0; |
| 297 | } |
| 298 | if (status) { |
| 299 | hdlc->dstpos = 0; |
| 300 | *count -= slen; |
| 301 | hdlc->cbin <<= 1; |
| 302 | hdlc->bit_shift--; |
| 303 | return status; |
| 304 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 305 | if (hdlc->data_bits == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | hdlc->data_bits = 0; |
| 307 | hdlc->data_received = 1; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 308 | hdlc->crc = crc_ccitt_byte(hdlc->crc, |
| 309 | hdlc->shift_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 311 | /* good byte received */ |
| 312 | if (hdlc->dstpos < dsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | dst[hdlc->dstpos++] = hdlc->shift_reg; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 314 | else { |
| 315 | /* frame too long */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | status = -HDLC_LENGTH_ERROR; |
| 317 | hdlc->dstpos = 0; |
| 318 | } |
| 319 | } |
| 320 | hdlc->cbin <<= 1; |
| 321 | hdlc->bit_shift--; |
| 322 | break; |
| 323 | case HDLC_FAST_FLAG: |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 324 | if (hdlc->cbin == hdlc->ffvalue) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | hdlc->bit_shift = 0; |
| 326 | break; |
| 327 | } else { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 328 | if (hdlc->cbin == 0xff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | hdlc->state = HDLC_FAST_IDLE; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 330 | hdlc->bit_shift = 0; |
| 331 | } else if (hdlc->ffbit_shift == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | hdlc->state = HDLC_GETFLAG_B7; |
| 333 | break; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 334 | } else |
| 335 | handle_abort(hdlc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | break; |
| 338 | default: |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | *count -= slen; |
| 343 | return 0; |
| 344 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 345 | EXPORT_SYMBOL(isdnhdlc_decode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | /* |
| 347 | isdnhdlc_encode - encodes HDLC frames to a transparent bit stream. |
| 348 | |
| 349 | The bit stream starts with a beginning flag (01111110). After |
| 350 | that each byte is added to the bit stream with bit stuffing added |
| 351 | (0 after 5 1's). |
| 352 | When the last byte has been removed from the source buffer, the |
| 353 | CRC (2 bytes is added) and the frame terminates with the ending flag. |
| 354 | For the dchannel, the idle character (all 1's) is also added at the end. |
| 355 | If this function is called with empty source buffer (slen=0), flags or |
| 356 | idle character will be generated. |
| 357 | |
| 358 | src - source buffer |
| 359 | slen - source buffer length |
| 360 | count - number of bytes removed (encoded) from source buffer |
| 361 | dst _ destination buffer |
| 362 | dsize - destination buffer size |
| 363 | returns - number of encoded bytes in the destination buffer |
| 364 | */ |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 365 | int isdnhdlc_encode(struct isdnhdlc_vars *hdlc, const u8 *src, u16 slen, |
| 366 | int *count, u8 *dst, int dsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
| 368 | static const unsigned char xfast_flag_value[] = { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 369 | 0x7e, 0x3f, 0x9f, 0xcf, 0xe7, 0xf3, 0xf9, 0xfc, 0x7e |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | int len = 0; |
| 373 | |
| 374 | *count = slen; |
| 375 | |
| 376 | while (dsize > 0) { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 377 | if (hdlc->bit_shift == 0) { |
| 378 | if (slen && !hdlc->do_closing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | hdlc->shift_reg = *src++; |
| 380 | slen--; |
| 381 | if (slen == 0) |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 382 | /* closing sequence, CRC + flag(s) */ |
| 383 | hdlc->do_closing = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | hdlc->bit_shift = 8; |
| 385 | } else { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 386 | if (hdlc->state == HDLC_SEND_DATA) { |
| 387 | if (hdlc->data_received) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | hdlc->state = HDLC_SEND_CRC1; |
| 389 | hdlc->crc ^= 0xffff; |
| 390 | hdlc->bit_shift = 8; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 391 | hdlc->shift_reg = |
| 392 | hdlc->crc & 0xff; |
| 393 | } else if (!hdlc->do_adapt56) |
| 394 | hdlc->state = |
| 395 | HDLC_SEND_FAST_FLAG; |
| 396 | else |
| 397 | hdlc->state = |
| 398 | HDLC_SENDFLAG_B0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | } |
| 402 | } |
| 403 | |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 404 | switch (hdlc->state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | case STOPPED: |
| 406 | while (dsize--) |
| 407 | *dst++ = 0xff; |
| 408 | |
| 409 | return dsize; |
| 410 | case HDLC_SEND_FAST_FLAG: |
| 411 | hdlc->do_closing = 0; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 412 | if (slen == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | *dst++ = hdlc->ffvalue; |
| 414 | len++; |
| 415 | dsize--; |
| 416 | break; |
| 417 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 418 | if (hdlc->bit_shift == 8) { |
| 419 | hdlc->cbin = hdlc->ffvalue >> |
| 420 | (8 - hdlc->data_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | hdlc->state = HDLC_SEND_DATA; |
| 422 | hdlc->crc = 0xffff; |
| 423 | hdlc->hdlc_bits1 = 0; |
| 424 | hdlc->data_received = 1; |
| 425 | } |
| 426 | break; |
| 427 | case HDLC_SENDFLAG_B0: |
| 428 | hdlc->do_closing = 0; |
| 429 | hdlc->cbin <<= 1; |
| 430 | hdlc->data_bits++; |
| 431 | hdlc->hdlc_bits1 = 0; |
| 432 | hdlc->state = HDLC_SENDFLAG_B1A6; |
| 433 | break; |
| 434 | case HDLC_SENDFLAG_B1A6: |
| 435 | hdlc->cbin <<= 1; |
| 436 | hdlc->data_bits++; |
| 437 | hdlc->cbin++; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 438 | if (++hdlc->hdlc_bits1 == 6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | hdlc->state = HDLC_SENDFLAG_B7; |
| 440 | break; |
| 441 | case HDLC_SENDFLAG_B7: |
| 442 | hdlc->cbin <<= 1; |
| 443 | hdlc->data_bits++; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 444 | if (slen == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | hdlc->state = HDLC_SENDFLAG_B0; |
| 446 | break; |
| 447 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 448 | if (hdlc->bit_shift == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | hdlc->state = HDLC_SEND_DATA; |
| 450 | hdlc->crc = 0xffff; |
| 451 | hdlc->hdlc_bits1 = 0; |
| 452 | hdlc->data_received = 1; |
| 453 | } |
| 454 | break; |
| 455 | case HDLC_SEND_FIRST_FLAG: |
| 456 | hdlc->data_received = 1; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 457 | if (hdlc->data_bits == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | hdlc->state = HDLC_SEND_DATA; |
| 459 | hdlc->crc = 0xffff; |
| 460 | hdlc->hdlc_bits1 = 0; |
| 461 | break; |
| 462 | } |
| 463 | hdlc->cbin <<= 1; |
| 464 | hdlc->data_bits++; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 465 | if (hdlc->shift_reg & 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | hdlc->cbin++; |
| 467 | hdlc->shift_reg >>= 1; |
| 468 | hdlc->bit_shift--; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 469 | if (hdlc->bit_shift == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | hdlc->state = HDLC_SEND_DATA; |
| 471 | hdlc->crc = 0xffff; |
| 472 | hdlc->hdlc_bits1 = 0; |
| 473 | } |
| 474 | break; |
| 475 | case HDLC_SEND_DATA: |
| 476 | hdlc->cbin <<= 1; |
| 477 | hdlc->data_bits++; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 478 | if (hdlc->hdlc_bits1 == 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | hdlc->hdlc_bits1 = 0; |
| 480 | break; |
| 481 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 482 | if (hdlc->bit_shift == 8) |
| 483 | hdlc->crc = crc_ccitt_byte(hdlc->crc, |
| 484 | hdlc->shift_reg); |
| 485 | if (hdlc->shift_reg & 0x01) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | hdlc->hdlc_bits1++; |
| 487 | hdlc->cbin++; |
| 488 | hdlc->shift_reg >>= 1; |
| 489 | hdlc->bit_shift--; |
| 490 | } else { |
| 491 | hdlc->hdlc_bits1 = 0; |
| 492 | hdlc->shift_reg >>= 1; |
| 493 | hdlc->bit_shift--; |
| 494 | } |
| 495 | break; |
| 496 | case HDLC_SEND_CRC1: |
| 497 | hdlc->cbin <<= 1; |
| 498 | hdlc->data_bits++; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 499 | if (hdlc->hdlc_bits1 == 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | hdlc->hdlc_bits1 = 0; |
| 501 | break; |
| 502 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 503 | if (hdlc->shift_reg & 0x01) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | hdlc->hdlc_bits1++; |
| 505 | hdlc->cbin++; |
| 506 | hdlc->shift_reg >>= 1; |
| 507 | hdlc->bit_shift--; |
| 508 | } else { |
| 509 | hdlc->hdlc_bits1 = 0; |
| 510 | hdlc->shift_reg >>= 1; |
| 511 | hdlc->bit_shift--; |
| 512 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 513 | if (hdlc->bit_shift == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | hdlc->shift_reg = (hdlc->crc >> 8); |
| 515 | hdlc->state = HDLC_SEND_CRC2; |
| 516 | hdlc->bit_shift = 8; |
| 517 | } |
| 518 | break; |
| 519 | case HDLC_SEND_CRC2: |
| 520 | hdlc->cbin <<= 1; |
| 521 | hdlc->data_bits++; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 522 | if (hdlc->hdlc_bits1 == 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | hdlc->hdlc_bits1 = 0; |
| 524 | break; |
| 525 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 526 | if (hdlc->shift_reg & 0x01) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | hdlc->hdlc_bits1++; |
| 528 | hdlc->cbin++; |
| 529 | hdlc->shift_reg >>= 1; |
| 530 | hdlc->bit_shift--; |
| 531 | } else { |
| 532 | hdlc->hdlc_bits1 = 0; |
| 533 | hdlc->shift_reg >>= 1; |
| 534 | hdlc->bit_shift--; |
| 535 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 536 | if (hdlc->bit_shift == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | hdlc->shift_reg = 0x7e; |
| 538 | hdlc->state = HDLC_SEND_CLOSING_FLAG; |
| 539 | hdlc->bit_shift = 8; |
| 540 | } |
| 541 | break; |
| 542 | case HDLC_SEND_CLOSING_FLAG: |
| 543 | hdlc->cbin <<= 1; |
| 544 | hdlc->data_bits++; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 545 | if (hdlc->hdlc_bits1 == 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | hdlc->hdlc_bits1 = 0; |
| 547 | break; |
| 548 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 549 | if (hdlc->shift_reg & 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | hdlc->cbin++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | hdlc->shift_reg >>= 1; |
| 552 | hdlc->bit_shift--; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 553 | if (hdlc->bit_shift == 0) { |
| 554 | hdlc->ffvalue = |
| 555 | xfast_flag_value[hdlc->data_bits]; |
| 556 | if (hdlc->dchannel) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | hdlc->ffvalue = 0x7e; |
| 558 | hdlc->state = HDLC_SEND_IDLE1; |
| 559 | hdlc->bit_shift = 8-hdlc->data_bits; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 560 | if (hdlc->bit_shift == 0) |
| 561 | hdlc->state = |
| 562 | HDLC_SEND_FAST_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } else { |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 564 | if (!hdlc->do_adapt56) { |
| 565 | hdlc->state = |
| 566 | HDLC_SEND_FAST_FLAG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | hdlc->data_received = 0; |
| 568 | } else { |
| 569 | hdlc->state = HDLC_SENDFLAG_B0; |
| 570 | hdlc->data_received = 0; |
| 571 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 572 | /* Finished this frame, send flags */ |
| 573 | if (dsize > 1) |
| 574 | dsize = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | break; |
| 578 | case HDLC_SEND_IDLE1: |
| 579 | hdlc->do_closing = 0; |
| 580 | hdlc->cbin <<= 1; |
| 581 | hdlc->cbin++; |
| 582 | hdlc->data_bits++; |
| 583 | hdlc->bit_shift--; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 584 | if (hdlc->bit_shift == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | hdlc->state = HDLC_SEND_FAST_IDLE; |
| 586 | hdlc->bit_shift = 0; |
| 587 | } |
| 588 | break; |
| 589 | case HDLC_SEND_FAST_IDLE: |
| 590 | hdlc->do_closing = 0; |
| 591 | hdlc->cbin = 0xff; |
| 592 | hdlc->data_bits = 8; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 593 | if (hdlc->bit_shift == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | hdlc->cbin = 0x7e; |
| 595 | hdlc->state = HDLC_SEND_FIRST_FLAG; |
| 596 | } else { |
| 597 | *dst++ = hdlc->cbin; |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 598 | hdlc->bit_shift = 0; |
| 599 | hdlc->data_bits = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | len++; |
| 601 | dsize = 0; |
| 602 | } |
| 603 | break; |
| 604 | default: |
| 605 | break; |
| 606 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 607 | if (hdlc->do_adapt56) { |
| 608 | if (hdlc->data_bits == 7) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | hdlc->cbin <<= 1; |
| 610 | hdlc->cbin++; |
| 611 | hdlc->data_bits++; |
| 612 | } |
| 613 | } |
Karsten Keil | 6bd4bcd | 2009-07-08 19:11:09 +0200 | [diff] [blame^] | 614 | if (hdlc->data_bits == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | *dst++ = hdlc->cbin; |
| 616 | hdlc->data_bits = 0; |
| 617 | len++; |
| 618 | dsize--; |
| 619 | } |
| 620 | } |
| 621 | *count -= slen; |
| 622 | |
| 623 | return len; |
| 624 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | EXPORT_SYMBOL(isdnhdlc_encode); |