isdnhdlc.c 15 KB

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