isdnhdlc.c 15 KB

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