isdnhdlc.c 15 KB

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