asyncdata.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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.
  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. #include <linux/bitrev.h>
  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);
  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 = bcs->inputstate;
  107. __u16 fcs = bcs->fcs;
  108. struct sk_buff *skb = bcs->skb;
  109. unsigned char error;
  110. struct sk_buff *compskb;
  111. int startbytes = numbytes;
  112. int l;
  113. if (unlikely(inputstate & INS_byte_stuff)) {
  114. inputstate &= ~INS_byte_stuff;
  115. goto byte_stuff;
  116. }
  117. for (;;) {
  118. if (unlikely(c == PPP_ESCAPE)) {
  119. if (unlikely(!numbytes)) {
  120. inputstate |= INS_byte_stuff;
  121. break;
  122. }
  123. c = *src++;
  124. --numbytes;
  125. if (unlikely(c == DLE_FLAG &&
  126. (cs->dle ||
  127. inbuf->inputstate & INS_DLE_command))) {
  128. inbuf->inputstate |= INS_DLE_char;
  129. inputstate |= INS_byte_stuff;
  130. break;
  131. }
  132. byte_stuff:
  133. c ^= PPP_TRANS;
  134. if (unlikely(!muststuff(c)))
  135. gig_dbg(DEBUG_HDLC, "byte stuffed: 0x%02x", c);
  136. } else if (unlikely(c == PPP_FLAG)) {
  137. if (unlikely(inputstate & INS_skip_frame)) {
  138. #ifdef CONFIG_GIGASET_DEBUG
  139. if (!(inputstate & INS_have_data)) { /* 7E 7E */
  140. ++bcs->emptycount;
  141. } else
  142. gig_dbg(DEBUG_HDLC,
  143. "7e----------------------------");
  144. #endif
  145. /* end of frame */
  146. error = 1;
  147. gigaset_rcv_error(NULL, cs, bcs);
  148. } else if (!(inputstate & INS_have_data)) { /* 7E 7E */
  149. #ifdef CONFIG_GIGASET_DEBUG
  150. ++bcs->emptycount;
  151. #endif
  152. break;
  153. } else {
  154. gig_dbg(DEBUG_HDLC,
  155. "7e----------------------------");
  156. /* end of frame */
  157. error = 0;
  158. if (unlikely(fcs != PPP_GOODFCS)) {
  159. dev_err(cs->dev,
  160. "Packet checksum at %lu failed, "
  161. "packet is corrupted (%u bytes)!\n",
  162. bcs->rcvbytes, skb->len);
  163. compskb = NULL;
  164. gigaset_rcv_error(compskb, cs, bcs);
  165. error = 1;
  166. } else {
  167. if (likely((l = skb->len) > 2)) {
  168. skb->tail -= 2;
  169. skb->len -= 2;
  170. } else {
  171. dev_kfree_skb(skb);
  172. skb = NULL;
  173. inputstate |= INS_skip_frame;
  174. if (l == 1) {
  175. dev_err(cs->dev,
  176. "invalid packet size (1)!\n");
  177. error = 1;
  178. gigaset_rcv_error(NULL,
  179. cs, bcs);
  180. }
  181. }
  182. if (likely(!(error ||
  183. (inputstate &
  184. INS_skip_frame)))) {
  185. gigaset_rcv_skb(skb, cs, bcs);
  186. }
  187. }
  188. }
  189. if (unlikely(error))
  190. if (skb)
  191. dev_kfree_skb(skb);
  192. fcs = PPP_INITFCS;
  193. inputstate &= ~(INS_have_data | INS_skip_frame);
  194. if (unlikely(bcs->ignore)) {
  195. inputstate |= INS_skip_frame;
  196. skb = NULL;
  197. } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)) {
  198. skb_reserve(skb, HW_HDR_LEN);
  199. } else {
  200. dev_warn(cs->dev,
  201. "could not allocate new skb\n");
  202. inputstate |= INS_skip_frame;
  203. }
  204. break;
  205. } else if (unlikely(muststuff(c))) {
  206. /* Should not happen. Possible after ZDLE=1<CR><LF>. */
  207. gig_dbg(DEBUG_HDLC, "not byte stuffed: 0x%02x", c);
  208. }
  209. /* add character */
  210. #ifdef CONFIG_GIGASET_DEBUG
  211. if (unlikely(!(inputstate & INS_have_data))) {
  212. gig_dbg(DEBUG_HDLC, "7e (%d x) ================",
  213. bcs->emptycount);
  214. bcs->emptycount = 0;
  215. }
  216. #endif
  217. inputstate |= INS_have_data;
  218. if (likely(!(inputstate & INS_skip_frame))) {
  219. if (unlikely(skb->len == SBUFSIZE)) {
  220. dev_warn(cs->dev, "received packet too long\n");
  221. dev_kfree_skb_any(skb);
  222. skb = NULL;
  223. inputstate |= INS_skip_frame;
  224. break;
  225. }
  226. *__skb_put(skb, 1) = c;
  227. fcs = crc_ccitt_byte(fcs, c);
  228. }
  229. if (unlikely(!numbytes))
  230. break;
  231. c = *src++;
  232. --numbytes;
  233. if (unlikely(c == DLE_FLAG &&
  234. (cs->dle ||
  235. inbuf->inputstate & INS_DLE_command))) {
  236. inbuf->inputstate |= INS_DLE_char;
  237. break;
  238. }
  239. }
  240. bcs->inputstate = inputstate;
  241. bcs->fcs = fcs;
  242. bcs->skb = skb;
  243. return startbytes - numbytes;
  244. }
  245. /* process a block of received bytes in transparent data mode
  246. * Invert bytes, undoing byte stuffing and watching for DLE escapes.
  247. * If DLE is encountered, return immediately to let the caller handle it.
  248. * Return value:
  249. * number of processed bytes
  250. * numbytes (all bytes processed) on error --FIXME
  251. */
  252. static inline int iraw_loop(unsigned char c, unsigned char *src, int numbytes,
  253. struct inbuf_t *inbuf)
  254. {
  255. struct cardstate *cs = inbuf->cs;
  256. struct bc_state *bcs = inbuf->bcs;
  257. int inputstate = bcs->inputstate;
  258. struct sk_buff *skb = bcs->skb;
  259. int startbytes = numbytes;
  260. for (;;) {
  261. /* add character */
  262. inputstate |= INS_have_data;
  263. if (likely(!(inputstate & INS_skip_frame))) {
  264. if (unlikely(skb->len == SBUFSIZE)) {
  265. //FIXME just pass skb up and allocate a new one
  266. dev_warn(cs->dev, "received packet too long\n");
  267. dev_kfree_skb_any(skb);
  268. skb = NULL;
  269. inputstate |= INS_skip_frame;
  270. break;
  271. }
  272. *__skb_put(skb, 1) = bitrev8(c);
  273. }
  274. if (unlikely(!numbytes))
  275. break;
  276. c = *src++;
  277. --numbytes;
  278. if (unlikely(c == DLE_FLAG &&
  279. (cs->dle ||
  280. inbuf->inputstate & INS_DLE_command))) {
  281. inbuf->inputstate |= INS_DLE_char;
  282. break;
  283. }
  284. }
  285. /* pass data up */
  286. if (likely(inputstate & INS_have_data)) {
  287. if (likely(!(inputstate & INS_skip_frame))) {
  288. gigaset_rcv_skb(skb, cs, bcs);
  289. }
  290. inputstate &= ~(INS_have_data | INS_skip_frame);
  291. if (unlikely(bcs->ignore)) {
  292. inputstate |= INS_skip_frame;
  293. skb = NULL;
  294. } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN))
  295. != NULL)) {
  296. skb_reserve(skb, HW_HDR_LEN);
  297. } else {
  298. dev_warn(cs->dev, "could not allocate new skb\n");
  299. inputstate |= INS_skip_frame;
  300. }
  301. }
  302. bcs->inputstate = inputstate;
  303. bcs->skb = skb;
  304. return startbytes - numbytes;
  305. }
  306. /* process a block of data received from the device
  307. */
  308. void gigaset_m10x_input(struct inbuf_t *inbuf)
  309. {
  310. struct cardstate *cs;
  311. unsigned tail, head, numbytes;
  312. unsigned char *src, c;
  313. int procbytes;
  314. head = inbuf->head;
  315. tail = inbuf->tail;
  316. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  317. if (head != tail) {
  318. cs = inbuf->cs;
  319. src = inbuf->data + head;
  320. numbytes = (head > tail ? RBUFSIZE : tail) - head;
  321. gig_dbg(DEBUG_INTR, "processing %u bytes", numbytes);
  322. while (numbytes) {
  323. if (cs->mstate == MS_LOCKED) {
  324. procbytes = lock_loop(src, numbytes, inbuf);
  325. src += procbytes;
  326. numbytes -= procbytes;
  327. } else {
  328. c = *src++;
  329. --numbytes;
  330. if (c == DLE_FLAG && (cs->dle ||
  331. inbuf->inputstate & INS_DLE_command)) {
  332. if (!(inbuf->inputstate & INS_DLE_char)) {
  333. inbuf->inputstate |= INS_DLE_char;
  334. goto nextbyte;
  335. }
  336. /* <DLE> <DLE> => <DLE> in data stream */
  337. inbuf->inputstate &= ~INS_DLE_char;
  338. }
  339. if (!(inbuf->inputstate & INS_DLE_char)) {
  340. /* FIXME use function pointers? */
  341. if (inbuf->inputstate & INS_command)
  342. procbytes = cmd_loop(c, src, numbytes, inbuf);
  343. else if (inbuf->bcs->proto2 == ISDN_PROTO_L2_HDLC)
  344. procbytes = hdlc_loop(c, src, numbytes, inbuf);
  345. else
  346. procbytes = iraw_loop(c, src, numbytes, inbuf);
  347. src += procbytes;
  348. numbytes -= procbytes;
  349. } else { /* DLE char */
  350. inbuf->inputstate &= ~INS_DLE_char;
  351. switch (c) {
  352. case 'X': /*begin of command*/
  353. if (inbuf->inputstate & INS_command)
  354. dev_warn(cs->dev,
  355. "received <DLE> 'X' in command mode\n");
  356. inbuf->inputstate |=
  357. INS_command | INS_DLE_command;
  358. break;
  359. case '.': /*end of command*/
  360. if (!(inbuf->inputstate & INS_command))
  361. dev_warn(cs->dev,
  362. "received <DLE> '.' in hdlc mode\n");
  363. inbuf->inputstate &= cs->dle ?
  364. ~(INS_DLE_command|INS_command)
  365. : ~INS_DLE_command;
  366. break;
  367. //case DLE_FLAG: /*DLE_FLAG in data stream*/ /* schon oben behandelt! */
  368. default:
  369. dev_err(cs->dev,
  370. "received 0x10 0x%02x!\n",
  371. (int) c);
  372. /* FIXME: reset driver?? */
  373. }
  374. }
  375. }
  376. nextbyte:
  377. if (!numbytes) {
  378. /* end of buffer, check for wrap */
  379. if (head > tail) {
  380. head = 0;
  381. src = inbuf->data;
  382. numbytes = tail;
  383. } else {
  384. head = tail;
  385. break;
  386. }
  387. }
  388. }
  389. gig_dbg(DEBUG_INTR, "setting head to %u", head);
  390. inbuf->head = head;
  391. }
  392. }
  393. EXPORT_SYMBOL_GPL(gigaset_m10x_input);
  394. /* == data output ========================================================== */
  395. /* Encoding of a PPP packet into an octet stuffed HDLC frame
  396. * with FCS, opening and closing flags.
  397. * parameters:
  398. * skb skb containing original packet (freed upon return)
  399. * head number of headroom bytes to allocate in result skb
  400. * tail number of tailroom bytes to allocate in result skb
  401. * Return value:
  402. * pointer to newly allocated skb containing the result frame
  403. */
  404. static struct sk_buff *HDLC_Encode(struct sk_buff *skb, int head, int tail)
  405. {
  406. struct sk_buff *hdlc_skb;
  407. __u16 fcs;
  408. unsigned char c;
  409. unsigned char *cp;
  410. int len;
  411. unsigned int stuf_cnt;
  412. stuf_cnt = 0;
  413. fcs = PPP_INITFCS;
  414. cp = skb->data;
  415. len = skb->len;
  416. while (len--) {
  417. if (muststuff(*cp))
  418. stuf_cnt++;
  419. fcs = crc_ccitt_byte(fcs, *cp++);
  420. }
  421. fcs ^= 0xffff; /* complement */
  422. /* size of new buffer: original size + number of stuffing bytes
  423. * + 2 bytes FCS + 2 stuffing bytes for FCS (if needed) + 2 flag bytes
  424. */
  425. hdlc_skb = dev_alloc_skb(skb->len + stuf_cnt + 6 + tail + head);
  426. if (!hdlc_skb) {
  427. dev_kfree_skb(skb);
  428. return NULL;
  429. }
  430. skb_reserve(hdlc_skb, head);
  431. /* Copy acknowledge request into new skb */
  432. memcpy(hdlc_skb->head, skb->head, 2);
  433. /* Add flag sequence in front of everything.. */
  434. *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
  435. /* Perform byte stuffing while copying data. */
  436. while (skb->len--) {
  437. if (muststuff(*skb->data)) {
  438. *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
  439. *(skb_put(hdlc_skb, 1)) = (*skb->data++) ^ PPP_TRANS;
  440. } else
  441. *(skb_put(hdlc_skb, 1)) = *skb->data++;
  442. }
  443. /* Finally add FCS (byte stuffed) and flag sequence */
  444. c = (fcs & 0x00ff); /* least significant byte first */
  445. if (muststuff(c)) {
  446. *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
  447. c ^= PPP_TRANS;
  448. }
  449. *(skb_put(hdlc_skb, 1)) = c;
  450. c = ((fcs >> 8) & 0x00ff);
  451. if (muststuff(c)) {
  452. *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
  453. c ^= PPP_TRANS;
  454. }
  455. *(skb_put(hdlc_skb, 1)) = c;
  456. *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
  457. dev_kfree_skb(skb);
  458. return hdlc_skb;
  459. }
  460. /* Encoding of a raw packet into an octet stuffed bit inverted frame
  461. * parameters:
  462. * skb skb containing original packet (freed upon return)
  463. * head number of headroom bytes to allocate in result skb
  464. * tail number of tailroom bytes to allocate in result skb
  465. * Return value:
  466. * pointer to newly allocated skb containing the result frame
  467. */
  468. static struct sk_buff *iraw_encode(struct sk_buff *skb, int head, int tail)
  469. {
  470. struct sk_buff *iraw_skb;
  471. unsigned char c;
  472. unsigned char *cp;
  473. int len;
  474. /* worst case: every byte must be stuffed */
  475. iraw_skb = dev_alloc_skb(2*skb->len + tail + head);
  476. if (!iraw_skb) {
  477. dev_kfree_skb(skb);
  478. return NULL;
  479. }
  480. skb_reserve(iraw_skb, head);
  481. cp = skb->data;
  482. len = skb->len;
  483. while (len--) {
  484. c = bitrev8(*cp++);
  485. if (c == DLE_FLAG)
  486. *(skb_put(iraw_skb, 1)) = c;
  487. *(skb_put(iraw_skb, 1)) = c;
  488. }
  489. dev_kfree_skb(skb);
  490. return iraw_skb;
  491. }
  492. /* gigaset_send_skb
  493. * called by common.c to queue an skb for sending
  494. * and start transmission if necessary
  495. * parameters:
  496. * B Channel control structure
  497. * skb
  498. * Return value:
  499. * number of bytes accepted for sending
  500. * (skb->len if ok, 0 if out of buffer space)
  501. * or error code (< 0, eg. -EINVAL)
  502. */
  503. int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb)
  504. {
  505. unsigned len = skb->len;
  506. unsigned long flags;
  507. if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
  508. skb = HDLC_Encode(skb, HW_HDR_LEN, 0);
  509. else
  510. skb = iraw_encode(skb, HW_HDR_LEN, 0);
  511. if (!skb) {
  512. dev_err(bcs->cs->dev,
  513. "unable to allocate memory for encoding!\n");
  514. return -ENOMEM;
  515. }
  516. skb_queue_tail(&bcs->squeue, skb);
  517. spin_lock_irqsave(&bcs->cs->lock, flags);
  518. if (bcs->cs->connected)
  519. tasklet_schedule(&bcs->cs->write_tasklet);
  520. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  521. return len; /* ok so far */
  522. }
  523. EXPORT_SYMBOL_GPL(gigaset_m10x_send_skb);