asyncdata.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Common data handling layer for ser_gigaset and usb_gigaset
  3. *
  4. * Copyright (c) 2005 by Tilman Schmidt <tilman@imap.cc>,
  5. * Hansjoerg Lipp <hjlipp@web.de>,
  6. * Stefan Eilers <Eilers.Stefan@epost.de>.
  7. *
  8. * =====================================================================
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. * =====================================================================
  14. */
  15. #include "gigaset.h"
  16. #include <linux/crc-ccitt.h>
  17. //#define GIG_M10x_STUFF_VOICE_DATA
  18. /* check if byte must be stuffed/escaped
  19. * I'm not sure which data should be encoded.
  20. * Therefore I will go the hard way and decode every value
  21. * less than 0x20, the flag sequence and the control escape char.
  22. */
  23. static inline int muststuff(unsigned char c)
  24. {
  25. if (c < PPP_TRANS) return 1;
  26. if (c == PPP_FLAG) return 1;
  27. if (c == PPP_ESCAPE) return 1;
  28. /* other possible candidates: */
  29. /* 0x91: XON with parity set */
  30. /* 0x93: XOFF with parity set */
  31. return 0;
  32. }
  33. /* == data input =========================================================== */
  34. /* process a block of received bytes in command mode (modem response)
  35. * Return value:
  36. * number of processed bytes
  37. */
  38. static inline int cmd_loop(unsigned char c, unsigned char *src, int numbytes,
  39. struct inbuf_t *inbuf)
  40. {
  41. struct cardstate *cs = inbuf->cs;
  42. unsigned cbytes = cs->cbytes;
  43. int inputstate = inbuf->inputstate;
  44. int startbytes = numbytes;
  45. for (;;) {
  46. cs->respdata[cbytes] = c;
  47. if (c == 10 || c == 13) {
  48. gig_dbg(DEBUG_TRANSCMD, "%s: End of Command (%d Bytes)",
  49. __func__, cbytes);
  50. cs->cbytes = cbytes;
  51. gigaset_handle_modem_response(cs); /* can change
  52. cs->dle */
  53. cbytes = 0;
  54. if (cs->dle &&
  55. !(inputstate & INS_DLE_command)) {
  56. inputstate &= ~INS_command;
  57. break;
  58. }
  59. } else {
  60. /* advance in line buffer, checking for overflow */
  61. if (cbytes < MAX_RESP_SIZE - 1)
  62. cbytes++;
  63. else
  64. dev_warn(cs->dev, "response too large\n");
  65. }
  66. if (!numbytes)
  67. break;
  68. c = *src++;
  69. --numbytes;
  70. if (c == DLE_FLAG &&
  71. (cs->dle || inputstate & INS_DLE_command)) {
  72. inputstate |= INS_DLE_char;
  73. break;
  74. }
  75. }
  76. cs->cbytes = cbytes;
  77. inbuf->inputstate = inputstate;
  78. return startbytes - numbytes;
  79. }
  80. /* process a block of received bytes in lock mode (tty i/f)
  81. * Return value:
  82. * number of processed bytes
  83. */
  84. static inline int lock_loop(unsigned char *src, int numbytes,
  85. struct inbuf_t *inbuf)
  86. {
  87. struct cardstate *cs = inbuf->cs;
  88. gigaset_dbg_buffer(DEBUG_LOCKCMD, "received response",
  89. numbytes, src, 0);
  90. gigaset_if_receive(cs, src, numbytes);
  91. return numbytes;
  92. }
  93. /* process a block of received bytes in HDLC data mode
  94. * Collect HDLC frames, undoing byte stuffing and watching for DLE escapes.
  95. * When a frame is complete, check the FCS and pass valid frames to the LL.
  96. * If DLE is encountered, return immediately to let the caller handle it.
  97. * Return value:
  98. * number of processed bytes
  99. * numbytes (all bytes processed) on error --FIXME
  100. */
  101. static inline int hdlc_loop(unsigned char c, unsigned char *src, int numbytes,
  102. struct inbuf_t *inbuf)
  103. {
  104. struct cardstate *cs = inbuf->cs;
  105. struct bc_state *bcs = inbuf->bcs;
  106. int inputstate;
  107. __u16 fcs;
  108. struct sk_buff *skb;
  109. unsigned char error;
  110. struct sk_buff *compskb;
  111. int startbytes = numbytes;
  112. int l;
  113. IFNULLRETVAL(bcs, numbytes);
  114. inputstate = bcs->inputstate;
  115. fcs = bcs->fcs;
  116. skb = bcs->skb;
  117. IFNULLRETVAL(skb, numbytes);
  118. if (unlikely(inputstate & INS_byte_stuff)) {
  119. inputstate &= ~INS_byte_stuff;
  120. goto byte_stuff;
  121. }
  122. for (;;) {
  123. if (unlikely(c == PPP_ESCAPE)) {
  124. if (unlikely(!numbytes)) {
  125. inputstate |= INS_byte_stuff;
  126. break;
  127. }
  128. c = *src++;
  129. --numbytes;
  130. if (unlikely(c == DLE_FLAG &&
  131. (cs->dle ||
  132. inbuf->inputstate & INS_DLE_command))) {
  133. inbuf->inputstate |= INS_DLE_char;
  134. inputstate |= INS_byte_stuff;
  135. break;
  136. }
  137. byte_stuff:
  138. c ^= PPP_TRANS;
  139. #ifdef CONFIG_GIGASET_DEBUG
  140. if (unlikely(!muststuff(c)))
  141. gig_dbg(DEBUG_HDLC, "byte stuffed: 0x%02x", c);
  142. #endif
  143. } else if (unlikely(c == PPP_FLAG)) {
  144. if (unlikely(inputstate & INS_skip_frame)) {
  145. if (!(inputstate & INS_have_data)) { /* 7E 7E */
  146. #ifdef CONFIG_GIGASET_DEBUG
  147. ++bcs->emptycount;
  148. #endif
  149. } else
  150. gig_dbg(DEBUG_HDLC,
  151. "7e----------------------------");
  152. /* end of frame */
  153. error = 1;
  154. gigaset_rcv_error(NULL, cs, bcs);
  155. } else if (!(inputstate & INS_have_data)) { /* 7E 7E */
  156. #ifdef CONFIG_GIGASET_DEBUG
  157. ++bcs->emptycount;
  158. #endif
  159. break;
  160. } else {
  161. gig_dbg(DEBUG_HDLC,
  162. "7e----------------------------");
  163. /* end of frame */
  164. error = 0;
  165. if (unlikely(fcs != PPP_GOODFCS)) {
  166. dev_err(cs->dev,
  167. "Packet checksum at %lu failed, "
  168. "packet is corrupted (%u bytes)!\n",
  169. bcs->rcvbytes, skb->len);
  170. compskb = NULL;
  171. gigaset_rcv_error(compskb, cs, bcs);
  172. error = 1;
  173. } else {
  174. if (likely((l = skb->len) > 2)) {
  175. skb->tail -= 2;
  176. skb->len -= 2;
  177. } else {
  178. dev_kfree_skb(skb);
  179. skb = NULL;
  180. inputstate |= INS_skip_frame;
  181. if (l == 1) {
  182. dev_err(cs->dev,
  183. "invalid packet size (1)!\n");
  184. error = 1;
  185. gigaset_rcv_error(NULL,
  186. cs, bcs);
  187. }
  188. }
  189. if (likely(!(error ||
  190. (inputstate &
  191. INS_skip_frame)))) {
  192. gigaset_rcv_skb(skb, cs, bcs);
  193. }
  194. }
  195. }
  196. if (unlikely(error))
  197. if (skb)
  198. dev_kfree_skb(skb);
  199. fcs = PPP_INITFCS;
  200. inputstate &= ~(INS_have_data | INS_skip_frame);
  201. if (unlikely(bcs->ignore)) {
  202. inputstate |= INS_skip_frame;
  203. skb = NULL;
  204. } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)) {
  205. skb_reserve(skb, HW_HDR_LEN);
  206. } else {
  207. dev_warn(cs->dev,
  208. "could not allocate new skb\n");
  209. inputstate |= INS_skip_frame;
  210. }
  211. break;
  212. #ifdef CONFIG_GIGASET_DEBUG
  213. } else if (unlikely(muststuff(c))) {
  214. /* Should not happen. Possible after ZDLE=1<CR><LF>. */
  215. gig_dbg(DEBUG_HDLC, "not byte stuffed: 0x%02x", c);
  216. #endif
  217. }
  218. /* add character */
  219. #ifdef CONFIG_GIGASET_DEBUG
  220. if (unlikely(!(inputstate & INS_have_data))) {
  221. gig_dbg(DEBUG_HDLC, "7e (%d x) ================",
  222. bcs->emptycount);
  223. bcs->emptycount = 0;
  224. }
  225. #endif
  226. inputstate |= INS_have_data;
  227. if (likely(!(inputstate & INS_skip_frame))) {
  228. if (unlikely(skb->len == SBUFSIZE)) {
  229. dev_warn(cs->dev, "received packet too long\n");
  230. dev_kfree_skb_any(skb);
  231. skb = NULL;
  232. inputstate |= INS_skip_frame;
  233. break;
  234. }
  235. *gigaset_skb_put_quick(skb, 1) = c;
  236. /* *__skb_put (skb, 1) = c; */
  237. fcs = crc_ccitt_byte(fcs, c);
  238. }
  239. if (unlikely(!numbytes))
  240. break;
  241. c = *src++;
  242. --numbytes;
  243. if (unlikely(c == DLE_FLAG &&
  244. (cs->dle ||
  245. inbuf->inputstate & INS_DLE_command))) {
  246. inbuf->inputstate |= INS_DLE_char;
  247. break;
  248. }
  249. }
  250. bcs->inputstate = inputstate;
  251. bcs->fcs = fcs;
  252. bcs->skb = skb;
  253. return startbytes - numbytes;
  254. }
  255. /* process a block of received bytes in transparent data mode
  256. * Invert bytes, undoing byte stuffing and watching for DLE escapes.
  257. * If DLE is encountered, return immediately to let the caller handle it.
  258. * Return value:
  259. * number of processed bytes
  260. * numbytes (all bytes processed) on error --FIXME
  261. */
  262. static inline int iraw_loop(unsigned char c, unsigned char *src, int numbytes,
  263. struct inbuf_t *inbuf)
  264. {
  265. struct cardstate *cs = inbuf->cs;
  266. struct bc_state *bcs = inbuf->bcs;
  267. int inputstate;
  268. struct sk_buff *skb;
  269. int startbytes = numbytes;
  270. IFNULLRETVAL(bcs, numbytes);
  271. inputstate = bcs->inputstate;
  272. skb = bcs->skb;
  273. IFNULLRETVAL(skb, numbytes);
  274. for (;;) {
  275. /* add character */
  276. inputstate |= INS_have_data;
  277. if (likely(!(inputstate & INS_skip_frame))) {
  278. if (unlikely(skb->len == SBUFSIZE)) {
  279. //FIXME just pass skb up and allocate a new one
  280. dev_warn(cs->dev, "received packet too long\n");
  281. dev_kfree_skb_any(skb);
  282. skb = NULL;
  283. inputstate |= INS_skip_frame;
  284. break;
  285. }
  286. *gigaset_skb_put_quick(skb, 1) = gigaset_invtab[c];
  287. }
  288. if (unlikely(!numbytes))
  289. break;
  290. c = *src++;
  291. --numbytes;
  292. if (unlikely(c == DLE_FLAG &&
  293. (cs->dle ||
  294. inbuf->inputstate & INS_DLE_command))) {
  295. inbuf->inputstate |= INS_DLE_char;
  296. break;
  297. }
  298. }
  299. /* pass data up */
  300. if (likely(inputstate & INS_have_data)) {
  301. if (likely(!(inputstate & INS_skip_frame))) {
  302. gigaset_rcv_skb(skb, cs, bcs);
  303. }
  304. inputstate &= ~(INS_have_data | INS_skip_frame);
  305. if (unlikely(bcs->ignore)) {
  306. inputstate |= INS_skip_frame;
  307. skb = NULL;
  308. } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN))
  309. != NULL)) {
  310. skb_reserve(skb, HW_HDR_LEN);
  311. } else {
  312. dev_warn(cs->dev, "could not allocate new skb\n");
  313. inputstate |= INS_skip_frame;
  314. }
  315. }
  316. bcs->inputstate = inputstate;
  317. bcs->skb = skb;
  318. return startbytes - numbytes;
  319. }
  320. /* process a block of data received from the device
  321. */
  322. void gigaset_m10x_input(struct inbuf_t *inbuf)
  323. {
  324. struct cardstate *cs;
  325. unsigned tail, head, numbytes;
  326. unsigned char *src, c;
  327. int procbytes;
  328. head = atomic_read(&inbuf->head);
  329. tail = atomic_read(&inbuf->tail);
  330. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  331. if (head != tail) {
  332. cs = inbuf->cs;
  333. src = inbuf->data + head;
  334. numbytes = (head > tail ? RBUFSIZE : tail) - head;
  335. gig_dbg(DEBUG_INTR, "processing %u bytes", numbytes);
  336. while (numbytes) {
  337. if (atomic_read(&cs->mstate) == MS_LOCKED) {
  338. procbytes = lock_loop(src, numbytes, inbuf);
  339. src += procbytes;
  340. numbytes -= procbytes;
  341. } else {
  342. c = *src++;
  343. --numbytes;
  344. if (c == DLE_FLAG && (cs->dle ||
  345. inbuf->inputstate & INS_DLE_command)) {
  346. if (!(inbuf->inputstate & INS_DLE_char)) {
  347. inbuf->inputstate |= INS_DLE_char;
  348. goto nextbyte;
  349. }
  350. /* <DLE> <DLE> => <DLE> in data stream */
  351. inbuf->inputstate &= ~INS_DLE_char;
  352. }
  353. if (!(inbuf->inputstate & INS_DLE_char)) {
  354. /* FIXME use function pointers? */
  355. if (inbuf->inputstate & INS_command)
  356. procbytes = cmd_loop(c, src, numbytes, inbuf);
  357. else if (inbuf->bcs->proto2 == ISDN_PROTO_L2_HDLC)
  358. procbytes = hdlc_loop(c, src, numbytes, inbuf);
  359. else
  360. procbytes = iraw_loop(c, src, numbytes, inbuf);
  361. src += procbytes;
  362. numbytes -= procbytes;
  363. } else { /* DLE char */
  364. inbuf->inputstate &= ~INS_DLE_char;
  365. switch (c) {
  366. case 'X': /*begin of command*/
  367. #ifdef CONFIG_GIGASET_DEBUG
  368. if (inbuf->inputstate & INS_command)
  369. dev_err(cs->dev,
  370. "received <DLE> 'X' in command mode\n");
  371. #endif
  372. inbuf->inputstate |=
  373. INS_command | INS_DLE_command;
  374. break;
  375. case '.': /*end of command*/
  376. #ifdef CONFIG_GIGASET_DEBUG
  377. if (!(inbuf->inputstate & INS_command))
  378. dev_err(cs->dev,
  379. "received <DLE> '.' in hdlc mode\n");
  380. #endif
  381. inbuf->inputstate &= cs->dle ?
  382. ~(INS_DLE_command|INS_command)
  383. : ~INS_DLE_command;
  384. break;
  385. //case DLE_FLAG: /*DLE_FLAG in data stream*/ /* schon oben behandelt! */
  386. default:
  387. dev_err(cs->dev,
  388. "received 0x10 0x%02x!\n",
  389. (int) c);
  390. /* FIXME: reset driver?? */
  391. }
  392. }
  393. }
  394. nextbyte:
  395. if (!numbytes) {
  396. /* end of buffer, check for wrap */
  397. if (head > tail) {
  398. head = 0;
  399. src = inbuf->data;
  400. numbytes = tail;
  401. } else {
  402. head = tail;
  403. break;
  404. }
  405. }
  406. }
  407. gig_dbg(DEBUG_INTR, "setting head to %u", head);
  408. atomic_set(&inbuf->head, head);
  409. }
  410. }
  411. /* == data output ========================================================== */
  412. /* Encoding of a PPP packet into an octet stuffed HDLC frame
  413. * with FCS, opening and closing flags.
  414. * parameters:
  415. * skb skb containing original packet (freed upon return)
  416. * head number of headroom bytes to allocate in result skb
  417. * tail number of tailroom bytes to allocate in result skb
  418. * Return value:
  419. * pointer to newly allocated skb containing the result frame
  420. */
  421. static struct sk_buff *HDLC_Encode(struct sk_buff *skb, int head, int tail)
  422. {
  423. struct sk_buff *hdlc_skb;
  424. __u16 fcs;
  425. unsigned char c;
  426. unsigned char *cp;
  427. int len;
  428. unsigned int stuf_cnt;
  429. stuf_cnt = 0;
  430. fcs = PPP_INITFCS;
  431. cp = skb->data;
  432. len = skb->len;
  433. while (len--) {
  434. if (muststuff(*cp))
  435. stuf_cnt++;
  436. fcs = crc_ccitt_byte(fcs, *cp++);
  437. }
  438. fcs ^= 0xffff; /* complement */
  439. /* size of new buffer: original size + number of stuffing bytes
  440. * + 2 bytes FCS + 2 stuffing bytes for FCS (if needed) + 2 flag bytes
  441. */
  442. hdlc_skb = dev_alloc_skb(skb->len + stuf_cnt + 6 + tail + head);
  443. if (!hdlc_skb) {
  444. dev_kfree_skb(skb);
  445. return NULL;
  446. }
  447. skb_reserve(hdlc_skb, head);
  448. /* Copy acknowledge request into new skb */
  449. memcpy(hdlc_skb->head, skb->head, 2);
  450. /* Add flag sequence in front of everything.. */
  451. *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
  452. /* Perform byte stuffing while copying data. */
  453. while (skb->len--) {
  454. if (muststuff(*skb->data)) {
  455. *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
  456. *(skb_put(hdlc_skb, 1)) = (*skb->data++) ^ PPP_TRANS;
  457. } else
  458. *(skb_put(hdlc_skb, 1)) = *skb->data++;
  459. }
  460. /* Finally add FCS (byte stuffed) and flag sequence */
  461. c = (fcs & 0x00ff); /* least significant byte first */
  462. if (muststuff(c)) {
  463. *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
  464. c ^= PPP_TRANS;
  465. }
  466. *(skb_put(hdlc_skb, 1)) = c;
  467. c = ((fcs >> 8) & 0x00ff);
  468. if (muststuff(c)) {
  469. *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
  470. c ^= PPP_TRANS;
  471. }
  472. *(skb_put(hdlc_skb, 1)) = c;
  473. *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
  474. dev_kfree_skb(skb);
  475. return hdlc_skb;
  476. }
  477. /* Encoding of a raw packet into an octet stuffed bit inverted frame
  478. * parameters:
  479. * skb skb containing original packet (freed upon return)
  480. * head number of headroom bytes to allocate in result skb
  481. * tail number of tailroom bytes to allocate in result skb
  482. * Return value:
  483. * pointer to newly allocated skb containing the result frame
  484. */
  485. static struct sk_buff *iraw_encode(struct sk_buff *skb, int head, int tail)
  486. {
  487. struct sk_buff *iraw_skb;
  488. unsigned char c;
  489. unsigned char *cp;
  490. int len;
  491. /* worst case: every byte must be stuffed */
  492. iraw_skb = dev_alloc_skb(2*skb->len + tail + head);
  493. if (!iraw_skb) {
  494. dev_kfree_skb(skb);
  495. return NULL;
  496. }
  497. skb_reserve(iraw_skb, head);
  498. cp = skb->data;
  499. len = skb->len;
  500. while (len--) {
  501. c = gigaset_invtab[*cp++];
  502. if (c == DLE_FLAG)
  503. *(skb_put(iraw_skb, 1)) = c;
  504. *(skb_put(iraw_skb, 1)) = c;
  505. }
  506. dev_kfree_skb(skb);
  507. return iraw_skb;
  508. }
  509. /* gigaset_send_skb
  510. * called by common.c to queue an skb for sending
  511. * and start transmission if necessary
  512. * parameters:
  513. * B Channel control structure
  514. * skb
  515. * Return value:
  516. * number of bytes accepted for sending
  517. * (skb->len if ok, 0 if out of buffer space)
  518. * or error code (< 0, eg. -EINVAL)
  519. */
  520. int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb)
  521. {
  522. unsigned len;
  523. IFNULLRETVAL(bcs, -EFAULT);
  524. IFNULLRETVAL(skb, -EFAULT);
  525. len = skb->len;
  526. if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
  527. skb = HDLC_Encode(skb, HW_HDR_LEN, 0);
  528. else
  529. skb = iraw_encode(skb, HW_HDR_LEN, 0);
  530. if (!skb) {
  531. dev_err(bcs->cs->dev,
  532. "unable to allocate memory for encoding!\n");
  533. return -ENOMEM;
  534. }
  535. skb_queue_tail(&bcs->squeue, skb);
  536. tasklet_schedule(&bcs->cs->write_tasklet);
  537. return len; /* ok so far */
  538. }