asyncdata.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 encode 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
  35. * (mstate != MS_LOCKED && (inputstate & INS_command))
  36. * Append received bytes to the command response buffer and forward them
  37. * line by line to the response handler. Exit whenever a mode/state change
  38. * might have occurred.
  39. * Note: Received lines may be terminated by CR, LF, or CR LF, which will be
  40. * removed before passing the line to the response handler.
  41. * Return value:
  42. * number of processed bytes
  43. */
  44. static unsigned cmd_loop(unsigned numbytes, struct inbuf_t *inbuf)
  45. {
  46. unsigned char *src = inbuf->data + inbuf->head;
  47. struct cardstate *cs = inbuf->cs;
  48. unsigned cbytes = cs->cbytes;
  49. unsigned procbytes = 0;
  50. unsigned char c;
  51. while (procbytes < numbytes) {
  52. c = *src++;
  53. procbytes++;
  54. switch (c) {
  55. case '\n':
  56. if (cbytes == 0 && cs->respdata[0] == '\r') {
  57. /* collapse LF with preceding CR */
  58. cs->respdata[0] = 0;
  59. break;
  60. }
  61. /* --v-- fall through --v-- */
  62. case '\r':
  63. /* end of message line, pass to response handler */
  64. if (cbytes >= MAX_RESP_SIZE) {
  65. dev_warn(cs->dev, "response too large (%d)\n",
  66. cbytes);
  67. cbytes = MAX_RESP_SIZE;
  68. }
  69. cs->cbytes = cbytes;
  70. gigaset_dbg_buffer(DEBUG_TRANSCMD, "received response",
  71. cbytes, cs->respdata);
  72. gigaset_handle_modem_response(cs);
  73. cbytes = 0;
  74. /* store EOL byte for CRLF collapsing */
  75. cs->respdata[0] = c;
  76. /* cs->dle may have changed */
  77. if (cs->dle && !(inbuf->inputstate & INS_DLE_command))
  78. inbuf->inputstate &= ~INS_command;
  79. /* return for reevaluating state */
  80. goto exit;
  81. case DLE_FLAG:
  82. if (inbuf->inputstate & INS_DLE_char) {
  83. /* quoted DLE: clear quote flag */
  84. inbuf->inputstate &= ~INS_DLE_char;
  85. } else if (cs->dle ||
  86. (inbuf->inputstate & INS_DLE_command)) {
  87. /* DLE escape, pass up for handling */
  88. inbuf->inputstate |= INS_DLE_char;
  89. goto exit;
  90. }
  91. /* quoted or not in DLE mode: treat as regular data */
  92. /* --v-- fall through --v-- */
  93. default:
  94. /* append to line buffer if possible */
  95. if (cbytes < MAX_RESP_SIZE)
  96. cs->respdata[cbytes] = c;
  97. cbytes++;
  98. }
  99. }
  100. exit:
  101. cs->cbytes = cbytes;
  102. return procbytes;
  103. }
  104. /* process a block of received bytes in lock mode
  105. * All received bytes are passed unmodified to the tty i/f.
  106. * Return value:
  107. * number of processed bytes
  108. */
  109. static unsigned lock_loop(unsigned numbytes, struct inbuf_t *inbuf)
  110. {
  111. unsigned char *src = inbuf->data + inbuf->head;
  112. gigaset_dbg_buffer(DEBUG_LOCKCMD, "received response", numbytes, src);
  113. gigaset_if_receive(inbuf->cs, src, numbytes);
  114. return numbytes;
  115. }
  116. /* process a block of received bytes in HDLC data mode
  117. * (mstate != MS_LOCKED && !(inputstate & INS_command) && proto2 == L2_HDLC)
  118. * Collect HDLC frames, undoing byte stuffing and watching for DLE escapes.
  119. * When a frame is complete, check the FCS and pass valid frames to the LL.
  120. * If DLE is encountered, return immediately to let the caller handle it.
  121. * Return value:
  122. * number of processed bytes
  123. */
  124. static unsigned hdlc_loop(unsigned numbytes, struct inbuf_t *inbuf)
  125. {
  126. struct cardstate *cs = inbuf->cs;
  127. struct bc_state *bcs = cs->bcs;
  128. int inputstate = bcs->inputstate;
  129. __u16 fcs = bcs->rx_fcs;
  130. struct sk_buff *skb = bcs->rx_skb;
  131. unsigned char *src = inbuf->data + inbuf->head;
  132. unsigned procbytes = 0;
  133. unsigned char c;
  134. if (inputstate & INS_byte_stuff) {
  135. if (!numbytes)
  136. return 0;
  137. inputstate &= ~INS_byte_stuff;
  138. goto byte_stuff;
  139. }
  140. while (procbytes < numbytes) {
  141. c = *src++;
  142. procbytes++;
  143. if (c == DLE_FLAG) {
  144. if (inputstate & INS_DLE_char) {
  145. /* quoted DLE: clear quote flag */
  146. inputstate &= ~INS_DLE_char;
  147. } else if (cs->dle || (inputstate & INS_DLE_command)) {
  148. /* DLE escape, pass up for handling */
  149. inputstate |= INS_DLE_char;
  150. break;
  151. }
  152. }
  153. if (c == PPP_ESCAPE) {
  154. /* byte stuffing indicator: pull in next byte */
  155. if (procbytes >= numbytes) {
  156. /* end of buffer, save for later processing */
  157. inputstate |= INS_byte_stuff;
  158. break;
  159. }
  160. byte_stuff:
  161. c = *src++;
  162. procbytes++;
  163. if (c == DLE_FLAG) {
  164. if (inputstate & INS_DLE_char) {
  165. /* quoted DLE: clear quote flag */
  166. inputstate &= ~INS_DLE_char;
  167. } else if (cs->dle ||
  168. (inputstate & INS_DLE_command)) {
  169. /* DLE escape, pass up for handling */
  170. inputstate |=
  171. INS_DLE_char | INS_byte_stuff;
  172. break;
  173. }
  174. }
  175. c ^= PPP_TRANS;
  176. #ifdef CONFIG_GIGASET_DEBUG
  177. if (!muststuff(c))
  178. gig_dbg(DEBUG_HDLC, "byte stuffed: 0x%02x", c);
  179. #endif
  180. } else if (c == PPP_FLAG) {
  181. /* end of frame: process content if any */
  182. if (inputstate & INS_have_data) {
  183. gig_dbg(DEBUG_HDLC,
  184. "7e----------------------------");
  185. /* check and pass received frame */
  186. if (!skb) {
  187. /* skipped frame */
  188. gigaset_isdn_rcv_err(bcs);
  189. } else if (skb->len < 2) {
  190. /* frame too short for FCS */
  191. dev_warn(cs->dev,
  192. "short frame (%d)\n",
  193. skb->len);
  194. gigaset_isdn_rcv_err(bcs);
  195. dev_kfree_skb_any(skb);
  196. } else if (fcs != PPP_GOODFCS) {
  197. /* frame check error */
  198. dev_err(cs->dev,
  199. "Checksum failed, %u bytes corrupted!\n",
  200. skb->len);
  201. gigaset_isdn_rcv_err(bcs);
  202. dev_kfree_skb_any(skb);
  203. } else {
  204. /* good frame */
  205. __skb_trim(skb, skb->len - 2);
  206. gigaset_skb_rcvd(bcs, skb);
  207. }
  208. /* prepare reception of next frame */
  209. inputstate &= ~INS_have_data;
  210. skb = gigaset_new_rx_skb(bcs);
  211. } else {
  212. /* empty frame (7E 7E) */
  213. #ifdef CONFIG_GIGASET_DEBUG
  214. ++bcs->emptycount;
  215. #endif
  216. if (!skb) {
  217. /* skipped (?) */
  218. gigaset_isdn_rcv_err(bcs);
  219. skb = gigaset_new_rx_skb(bcs);
  220. }
  221. }
  222. fcs = PPP_INITFCS;
  223. continue;
  224. #ifdef CONFIG_GIGASET_DEBUG
  225. } else if (muststuff(c)) {
  226. /* Should not happen. Possible after ZDLE=1<CR><LF>. */
  227. gig_dbg(DEBUG_HDLC, "not byte stuffed: 0x%02x", c);
  228. #endif
  229. }
  230. /* regular data byte, append to skb */
  231. #ifdef CONFIG_GIGASET_DEBUG
  232. if (!(inputstate & INS_have_data)) {
  233. gig_dbg(DEBUG_HDLC, "7e (%d x) ================",
  234. bcs->emptycount);
  235. bcs->emptycount = 0;
  236. }
  237. #endif
  238. inputstate |= INS_have_data;
  239. if (skb) {
  240. if (skb->len >= bcs->rx_bufsize) {
  241. dev_warn(cs->dev, "received packet too long\n");
  242. dev_kfree_skb_any(skb);
  243. /* skip remainder of packet */
  244. bcs->rx_skb = skb = NULL;
  245. } else {
  246. *__skb_put(skb, 1) = c;
  247. fcs = crc_ccitt_byte(fcs, c);
  248. }
  249. }
  250. }
  251. bcs->inputstate = inputstate;
  252. bcs->rx_fcs = fcs;
  253. return procbytes;
  254. }
  255. /* process a block of received bytes in transparent data mode
  256. * (mstate != MS_LOCKED && !(inputstate & INS_command) && proto2 != L2_HDLC)
  257. * Invert bytes, undoing byte stuffing and watching for DLE escapes.
  258. * If DLE is encountered, return immediately to let the caller handle it.
  259. * Return value:
  260. * number of processed bytes
  261. */
  262. static unsigned iraw_loop(unsigned numbytes, struct inbuf_t *inbuf)
  263. {
  264. struct cardstate *cs = inbuf->cs;
  265. struct bc_state *bcs = cs->bcs;
  266. int inputstate = bcs->inputstate;
  267. struct sk_buff *skb = bcs->rx_skb;
  268. unsigned char *src = inbuf->data + inbuf->head;
  269. unsigned procbytes = 0;
  270. unsigned char c;
  271. if (!skb) {
  272. /* skip this block */
  273. gigaset_new_rx_skb(bcs);
  274. return numbytes;
  275. }
  276. while (procbytes < numbytes && skb->len < bcs->rx_bufsize) {
  277. c = *src++;
  278. procbytes++;
  279. if (c == DLE_FLAG) {
  280. if (inputstate & INS_DLE_char) {
  281. /* quoted DLE: clear quote flag */
  282. inputstate &= ~INS_DLE_char;
  283. } else if (cs->dle || (inputstate & INS_DLE_command)) {
  284. /* DLE escape, pass up for handling */
  285. inputstate |= INS_DLE_char;
  286. break;
  287. }
  288. }
  289. /* regular data byte: append to current skb */
  290. inputstate |= INS_have_data;
  291. *__skb_put(skb, 1) = bitrev8(c);
  292. }
  293. /* pass data up */
  294. if (inputstate & INS_have_data) {
  295. gigaset_skb_rcvd(bcs, skb);
  296. inputstate &= ~INS_have_data;
  297. gigaset_new_rx_skb(bcs);
  298. }
  299. bcs->inputstate = inputstate;
  300. return procbytes;
  301. }
  302. /* process DLE escapes
  303. * Called whenever a DLE sequence might be encountered in the input stream.
  304. * Either processes the entire DLE sequence or, if that isn't possible,
  305. * notes the fact that an initial DLE has been received in the INS_DLE_char
  306. * inputstate flag and resumes processing of the sequence on the next call.
  307. */
  308. static void handle_dle(struct inbuf_t *inbuf)
  309. {
  310. struct cardstate *cs = inbuf->cs;
  311. if (cs->mstate == MS_LOCKED)
  312. return; /* no DLE processing in lock mode */
  313. if (!(inbuf->inputstate & INS_DLE_char)) {
  314. /* no DLE pending */
  315. if (inbuf->data[inbuf->head] == DLE_FLAG &&
  316. (cs->dle || inbuf->inputstate & INS_DLE_command)) {
  317. /* start of DLE sequence */
  318. inbuf->head++;
  319. if (inbuf->head == inbuf->tail ||
  320. inbuf->head == RBUFSIZE) {
  321. /* end of buffer, save for later processing */
  322. inbuf->inputstate |= INS_DLE_char;
  323. return;
  324. }
  325. } else {
  326. /* regular data byte */
  327. return;
  328. }
  329. }
  330. /* consume pending DLE */
  331. inbuf->inputstate &= ~INS_DLE_char;
  332. switch (inbuf->data[inbuf->head]) {
  333. case 'X': /* begin of event message */
  334. if (inbuf->inputstate & INS_command)
  335. dev_notice(cs->dev,
  336. "received <DLE>X in command mode\n");
  337. inbuf->inputstate |= INS_command | INS_DLE_command;
  338. inbuf->head++; /* byte consumed */
  339. break;
  340. case '.': /* end of event message */
  341. if (!(inbuf->inputstate & INS_DLE_command))
  342. dev_notice(cs->dev,
  343. "received <DLE>. without <DLE>X\n");
  344. inbuf->inputstate &= ~INS_DLE_command;
  345. /* return to data mode if in DLE mode */
  346. if (cs->dle)
  347. inbuf->inputstate &= ~INS_command;
  348. inbuf->head++; /* byte consumed */
  349. break;
  350. case DLE_FLAG: /* DLE in data stream */
  351. /* mark as quoted */
  352. inbuf->inputstate |= INS_DLE_char;
  353. if (!(cs->dle || inbuf->inputstate & INS_DLE_command))
  354. dev_notice(cs->dev,
  355. "received <DLE><DLE> not in DLE mode\n");
  356. break; /* quoted byte left in buffer */
  357. default:
  358. dev_notice(cs->dev, "received <DLE><%02x>\n",
  359. inbuf->data[inbuf->head]);
  360. /* quoted byte left in buffer */
  361. }
  362. }
  363. /**
  364. * gigaset_m10x_input() - process a block of data received from the device
  365. * @inbuf: received data and device descriptor structure.
  366. *
  367. * Called by hardware module {ser,usb}_gigaset with a block of received
  368. * bytes. Separates the bytes received over the serial data channel into
  369. * user data and command replies (locked/unlocked) according to the
  370. * current state of the interface.
  371. */
  372. void gigaset_m10x_input(struct inbuf_t *inbuf)
  373. {
  374. struct cardstate *cs = inbuf->cs;
  375. unsigned numbytes, procbytes;
  376. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", inbuf->head, inbuf->tail);
  377. while (inbuf->head != inbuf->tail) {
  378. /* check for DLE escape */
  379. handle_dle(inbuf);
  380. /* process a contiguous block of bytes */
  381. numbytes = (inbuf->head > inbuf->tail ?
  382. RBUFSIZE : inbuf->tail) - inbuf->head;
  383. gig_dbg(DEBUG_INTR, "processing %u bytes", numbytes);
  384. /*
  385. * numbytes may be 0 if handle_dle() ate the last byte.
  386. * This does no harm, *_loop() will just return 0 immediately.
  387. */
  388. if (cs->mstate == MS_LOCKED)
  389. procbytes = lock_loop(numbytes, inbuf);
  390. else if (inbuf->inputstate & INS_command)
  391. procbytes = cmd_loop(numbytes, inbuf);
  392. else if (cs->bcs->proto2 == L2_HDLC)
  393. procbytes = hdlc_loop(numbytes, inbuf);
  394. else
  395. procbytes = iraw_loop(numbytes, inbuf);
  396. inbuf->head += procbytes;
  397. /* check for buffer wraparound */
  398. if (inbuf->head >= RBUFSIZE)
  399. inbuf->head = 0;
  400. gig_dbg(DEBUG_INTR, "head set to %u", inbuf->head);
  401. }
  402. }
  403. EXPORT_SYMBOL_GPL(gigaset_m10x_input);
  404. /* == data output ========================================================== */
  405. /*
  406. * Encode a data packet into an octet stuffed HDLC frame with FCS,
  407. * opening and closing flags, preserving headroom data.
  408. * parameters:
  409. * skb skb containing original packet (freed upon return)
  410. * Return value:
  411. * pointer to newly allocated skb containing the result frame
  412. * and the original link layer header, NULL on error
  413. */
  414. static struct sk_buff *HDLC_Encode(struct sk_buff *skb)
  415. {
  416. struct sk_buff *hdlc_skb;
  417. __u16 fcs;
  418. unsigned char c;
  419. unsigned char *cp;
  420. int len;
  421. unsigned int stuf_cnt;
  422. stuf_cnt = 0;
  423. fcs = PPP_INITFCS;
  424. cp = skb->data;
  425. len = skb->len;
  426. while (len--) {
  427. if (muststuff(*cp))
  428. stuf_cnt++;
  429. fcs = crc_ccitt_byte(fcs, *cp++);
  430. }
  431. fcs ^= 0xffff; /* complement */
  432. /* size of new buffer: original size + number of stuffing bytes
  433. * + 2 bytes FCS + 2 stuffing bytes for FCS (if needed) + 2 flag bytes
  434. * + room for link layer header
  435. */
  436. hdlc_skb = dev_alloc_skb(skb->len + stuf_cnt + 6 + skb->mac_len);
  437. if (!hdlc_skb) {
  438. dev_kfree_skb_any(skb);
  439. return NULL;
  440. }
  441. /* Copy link layer header into new skb */
  442. skb_reset_mac_header(hdlc_skb);
  443. skb_reserve(hdlc_skb, skb->mac_len);
  444. memcpy(skb_mac_header(hdlc_skb), skb_mac_header(skb), skb->mac_len);
  445. hdlc_skb->mac_len = skb->mac_len;
  446. /* Add flag sequence in front of everything.. */
  447. *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
  448. /* Perform byte stuffing while copying data. */
  449. while (skb->len--) {
  450. if (muststuff(*skb->data)) {
  451. *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
  452. *(skb_put(hdlc_skb, 1)) = (*skb->data++) ^ PPP_TRANS;
  453. } else
  454. *(skb_put(hdlc_skb, 1)) = *skb->data++;
  455. }
  456. /* Finally add FCS (byte stuffed) and flag sequence */
  457. c = (fcs & 0x00ff); /* least significant byte first */
  458. if (muststuff(c)) {
  459. *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
  460. c ^= PPP_TRANS;
  461. }
  462. *(skb_put(hdlc_skb, 1)) = c;
  463. c = ((fcs >> 8) & 0x00ff);
  464. if (muststuff(c)) {
  465. *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
  466. c ^= PPP_TRANS;
  467. }
  468. *(skb_put(hdlc_skb, 1)) = c;
  469. *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
  470. dev_kfree_skb_any(skb);
  471. return hdlc_skb;
  472. }
  473. /*
  474. * Encode a data packet into an octet stuffed raw bit inverted frame,
  475. * preserving headroom data.
  476. * parameters:
  477. * skb skb containing original packet (freed upon return)
  478. * Return value:
  479. * pointer to newly allocated skb containing the result frame
  480. * and the original link layer header, NULL on error
  481. */
  482. static struct sk_buff *iraw_encode(struct sk_buff *skb)
  483. {
  484. struct sk_buff *iraw_skb;
  485. unsigned char c;
  486. unsigned char *cp;
  487. int len;
  488. /* size of new buffer (worst case = every byte must be stuffed):
  489. * 2 * original size + room for link layer header
  490. */
  491. iraw_skb = dev_alloc_skb(2*skb->len + skb->mac_len);
  492. if (!iraw_skb) {
  493. dev_kfree_skb_any(skb);
  494. return NULL;
  495. }
  496. /* copy link layer header into new skb */
  497. skb_reset_mac_header(iraw_skb);
  498. skb_reserve(iraw_skb, skb->mac_len);
  499. memcpy(skb_mac_header(iraw_skb), skb_mac_header(skb), skb->mac_len);
  500. iraw_skb->mac_len = skb->mac_len;
  501. /* copy and stuff data */
  502. cp = skb->data;
  503. len = skb->len;
  504. while (len--) {
  505. c = bitrev8(*cp++);
  506. if (c == DLE_FLAG)
  507. *(skb_put(iraw_skb, 1)) = c;
  508. *(skb_put(iraw_skb, 1)) = c;
  509. }
  510. dev_kfree_skb_any(skb);
  511. return iraw_skb;
  512. }
  513. /**
  514. * gigaset_m10x_send_skb() - queue an skb for sending
  515. * @bcs: B channel descriptor structure.
  516. * @skb: data to send.
  517. *
  518. * Called by LL to encode and queue an skb for sending, and start
  519. * transmission if necessary.
  520. * Once the payload data has been transmitted completely, gigaset_skb_sent()
  521. * will be called with the skb's link layer header preserved.
  522. *
  523. * Return value:
  524. * number of bytes accepted for sending (skb->len) if ok,
  525. * error code < 0 (eg. -ENOMEM) on error
  526. */
  527. int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb)
  528. {
  529. struct cardstate *cs = bcs->cs;
  530. unsigned len = skb->len;
  531. unsigned long flags;
  532. if (bcs->proto2 == L2_HDLC)
  533. skb = HDLC_Encode(skb);
  534. else
  535. skb = iraw_encode(skb);
  536. if (!skb) {
  537. dev_err(cs->dev,
  538. "unable to allocate memory for encoding!\n");
  539. return -ENOMEM;
  540. }
  541. skb_queue_tail(&bcs->squeue, skb);
  542. spin_lock_irqsave(&cs->lock, flags);
  543. if (cs->connected)
  544. tasklet_schedule(&cs->write_tasklet);
  545. spin_unlock_irqrestore(&cs->lock, flags);
  546. return len; /* ok so far */
  547. }
  548. EXPORT_SYMBOL_GPL(gigaset_m10x_send_skb);