wrapper.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*********************************************************************
  2. *
  3. * Filename: wrapper.c
  4. * Version: 1.2
  5. * Description: IrDA SIR async wrapper layer
  6. * Status: Stable
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Mon Aug 4 20:40:53 1997
  9. * Modified at: Fri Jan 28 13:21:09 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. * Modified at: Fri May 28 3:11 CST 1999
  12. * Modified by: Horst von Brand <vonbrand@sleipnir.valparaiso.cl>
  13. *
  14. * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
  15. * All Rights Reserved.
  16. * Copyright (c) 2000-2002 Jean Tourrilhes <jt@hpl.hp.com>
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of
  21. * the License, or (at your option) any later version.
  22. *
  23. * Neither Dag Brattli nor University of Tromsø admit liability nor
  24. * provide warranty for any of this software. This material is
  25. * provided "AS-IS" and at no charge.
  26. *
  27. ********************************************************************/
  28. #include <linux/skbuff.h>
  29. #include <linux/string.h>
  30. #include <linux/module.h>
  31. #include <asm/byteorder.h>
  32. #include <net/irda/irda.h>
  33. #include <net/irda/wrapper.h>
  34. #include <net/irda/crc.h>
  35. #include <net/irda/irlap.h>
  36. #include <net/irda/irlap_frame.h>
  37. #include <net/irda/irda_device.h>
  38. /************************** FRAME WRAPPING **************************/
  39. /*
  40. * Unwrap and unstuff SIR frames
  41. *
  42. * Note : at FIR and MIR, HDLC framing is used and usually handled
  43. * by the controller, so we come here only for SIR... Jean II
  44. */
  45. /*
  46. * Function stuff_byte (byte, buf)
  47. *
  48. * Byte stuff one single byte and put the result in buffer pointed to by
  49. * buf. The buffer must at all times be able to have two bytes inserted.
  50. *
  51. * This is in a tight loop, better inline it, so need to be prior to callers.
  52. * (2000 bytes on P6 200MHz, non-inlined ~370us, inline ~170us) - Jean II
  53. */
  54. static inline int stuff_byte(__u8 byte, __u8 *buf)
  55. {
  56. switch (byte) {
  57. case BOF: /* FALLTHROUGH */
  58. case EOF: /* FALLTHROUGH */
  59. case CE:
  60. /* Insert transparently coded */
  61. buf[0] = CE; /* Send link escape */
  62. buf[1] = byte^IRDA_TRANS; /* Complement bit 5 */
  63. return 2;
  64. /* break; */
  65. default:
  66. /* Non-special value, no transparency required */
  67. buf[0] = byte;
  68. return 1;
  69. /* break; */
  70. }
  71. }
  72. /*
  73. * Function async_wrap (skb, *tx_buff, buffsize)
  74. *
  75. * Makes a new buffer with wrapping and stuffing, should check that
  76. * we don't get tx buffer overflow.
  77. */
  78. int async_wrap_skb(struct sk_buff *skb, __u8 *tx_buff, int buffsize)
  79. {
  80. struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
  81. int xbofs;
  82. int i;
  83. int n;
  84. union {
  85. __u16 value;
  86. __u8 bytes[2];
  87. } fcs;
  88. /* Initialize variables */
  89. fcs.value = INIT_FCS;
  90. n = 0;
  91. /*
  92. * Send XBOF's for required min. turn time and for the negotiated
  93. * additional XBOFS
  94. */
  95. if (cb->magic != LAP_MAGIC) {
  96. /*
  97. * This will happen for all frames sent from user-space.
  98. * Nothing to worry about, but we set the default number of
  99. * BOF's
  100. */
  101. IRDA_DEBUG(1, "%s(), wrong magic in skb!\n", __FUNCTION__);
  102. xbofs = 10;
  103. } else
  104. xbofs = cb->xbofs + cb->xbofs_delay;
  105. IRDA_DEBUG(4, "%s(), xbofs=%d\n", __FUNCTION__, xbofs);
  106. /* Check that we never use more than 115 + 48 xbofs */
  107. if (xbofs > 163) {
  108. IRDA_DEBUG(0, "%s(), too many xbofs (%d)\n", __FUNCTION__,
  109. xbofs);
  110. xbofs = 163;
  111. }
  112. memset(tx_buff + n, XBOF, xbofs);
  113. n += xbofs;
  114. /* Start of packet character BOF */
  115. tx_buff[n++] = BOF;
  116. /* Insert frame and calc CRC */
  117. for (i=0; i < skb->len; i++) {
  118. /*
  119. * Check for the possibility of tx buffer overflow. We use
  120. * bufsize-5 since the maximum number of bytes that can be
  121. * transmitted after this point is 5.
  122. */
  123. if(n >= (buffsize-5)) {
  124. IRDA_ERROR("%s(), tx buffer overflow (n=%d)\n",
  125. __FUNCTION__, n);
  126. return n;
  127. }
  128. n += stuff_byte(skb->data[i], tx_buff+n);
  129. fcs.value = irda_fcs(fcs.value, skb->data[i]);
  130. }
  131. /* Insert CRC in little endian format (LSB first) */
  132. fcs.value = ~fcs.value;
  133. #ifdef __LITTLE_ENDIAN
  134. n += stuff_byte(fcs.bytes[0], tx_buff+n);
  135. n += stuff_byte(fcs.bytes[1], tx_buff+n);
  136. #else /* ifdef __BIG_ENDIAN */
  137. n += stuff_byte(fcs.bytes[1], tx_buff+n);
  138. n += stuff_byte(fcs.bytes[0], tx_buff+n);
  139. #endif
  140. tx_buff[n++] = EOF;
  141. return n;
  142. }
  143. EXPORT_SYMBOL(async_wrap_skb);
  144. /************************* FRAME UNWRAPPING *************************/
  145. /*
  146. * Unwrap and unstuff SIR frames
  147. *
  148. * Complete rewrite by Jean II :
  149. * More inline, faster, more compact, more logical. Jean II
  150. * (16 bytes on P6 200MHz, old 5 to 7 us, new 4 to 6 us)
  151. * (24 bytes on P6 200MHz, old 9 to 10 us, new 7 to 8 us)
  152. * (for reference, 115200 b/s is 1 byte every 69 us)
  153. * And reduce wrapper.o by ~900B in the process ;-)
  154. *
  155. * Then, we have the addition of ZeroCopy, which is optional
  156. * (i.e. the driver must initiate it) and improve final processing.
  157. * (2005 B frame + EOF on P6 200MHz, without 30 to 50 us, with 10 to 25 us)
  158. *
  159. * Note : at FIR and MIR, HDLC framing is used and usually handled
  160. * by the controller, so we come here only for SIR... Jean II
  161. */
  162. /*
  163. * We can also choose where we want to do the CRC calculation. We can
  164. * do it "inline", as we receive the bytes, or "postponed", when
  165. * receiving the End-Of-Frame.
  166. * (16 bytes on P6 200MHz, inlined 4 to 6 us, postponed 4 to 5 us)
  167. * (24 bytes on P6 200MHz, inlined 7 to 8 us, postponed 5 to 7 us)
  168. * With ZeroCopy :
  169. * (2005 B frame on P6 200MHz, inlined 10 to 25 us, postponed 140 to 180 us)
  170. * Without ZeroCopy :
  171. * (2005 B frame on P6 200MHz, inlined 30 to 50 us, postponed 150 to 180 us)
  172. * (Note : numbers taken with irq disabled)
  173. *
  174. * From those numbers, it's not clear which is the best strategy, because
  175. * we end up running through a lot of data one way or another (i.e. cache
  176. * misses). I personally prefer to avoid the huge latency spike of the
  177. * "postponed" solution, because it come just at the time when we have
  178. * lot's of protocol processing to do and it will hurt our ability to
  179. * reach low link turnaround times... Jean II
  180. */
  181. //#define POSTPONE_RX_CRC
  182. /*
  183. * Function async_bump (buf, len, stats)
  184. *
  185. * Got a frame, make a copy of it, and pass it up the stack! We can try
  186. * to inline it since it's only called from state_inside_frame
  187. */
  188. static inline void
  189. async_bump(struct net_device *dev,
  190. struct net_device_stats *stats,
  191. iobuff_t *rx_buff)
  192. {
  193. struct sk_buff *newskb;
  194. struct sk_buff *dataskb;
  195. int docopy;
  196. /* Check if we need to copy the data to a new skb or not.
  197. * If the driver doesn't use ZeroCopy Rx, we have to do it.
  198. * With ZeroCopy Rx, the rx_buff already point to a valid
  199. * skb. But, if the frame is small, it is more efficient to
  200. * copy it to save memory (copy will be fast anyway - that's
  201. * called Rx-copy-break). Jean II */
  202. docopy = ((rx_buff->skb == NULL) ||
  203. (rx_buff->len < IRDA_RX_COPY_THRESHOLD));
  204. /* Allocate a new skb */
  205. newskb = dev_alloc_skb(docopy ? rx_buff->len + 1 : rx_buff->truesize);
  206. if (!newskb) {
  207. stats->rx_dropped++;
  208. /* We could deliver the current skb if doing ZeroCopy Rx,
  209. * but this would stall the Rx path. Better drop the
  210. * packet... Jean II */
  211. return;
  212. }
  213. /* Align IP header to 20 bytes (i.e. increase skb->data)
  214. * Note this is only useful with IrLAN, as PPP has a variable
  215. * header size (2 or 1 bytes) - Jean II */
  216. skb_reserve(newskb, 1);
  217. if(docopy) {
  218. /* Copy data without CRC (lenght already checked) */
  219. memcpy(newskb->data, rx_buff->data, rx_buff->len - 2);
  220. /* Deliver this skb */
  221. dataskb = newskb;
  222. } else {
  223. /* We are using ZeroCopy. Deliver old skb */
  224. dataskb = rx_buff->skb;
  225. /* And hook the new skb to the rx_buff */
  226. rx_buff->skb = newskb;
  227. rx_buff->head = newskb->data; /* NOT newskb->head */
  228. //printk(KERN_DEBUG "ZeroCopy : len = %d, dataskb = %p, newskb = %p\n", rx_buff->len, dataskb, newskb);
  229. }
  230. /* Set proper length on skb (without CRC) */
  231. skb_put(dataskb, rx_buff->len - 2);
  232. /* Feed it to IrLAP layer */
  233. dataskb->dev = dev;
  234. dataskb->mac.raw = dataskb->data;
  235. dataskb->protocol = htons(ETH_P_IRDA);
  236. netif_rx(dataskb);
  237. stats->rx_packets++;
  238. stats->rx_bytes += rx_buff->len;
  239. /* Clean up rx_buff (redundant with async_unwrap_bof() ???) */
  240. rx_buff->data = rx_buff->head;
  241. rx_buff->len = 0;
  242. }
  243. /*
  244. * Function async_unwrap_bof(dev, byte)
  245. *
  246. * Handle Beginning Of Frame character received within a frame
  247. *
  248. */
  249. static inline void
  250. async_unwrap_bof(struct net_device *dev,
  251. struct net_device_stats *stats,
  252. iobuff_t *rx_buff, __u8 byte)
  253. {
  254. switch(rx_buff->state) {
  255. case LINK_ESCAPE:
  256. case INSIDE_FRAME:
  257. /* Not supposed to happen, the previous frame is not
  258. * finished - Jean II */
  259. IRDA_DEBUG(1, "%s(), Discarding incomplete frame\n",
  260. __FUNCTION__);
  261. stats->rx_errors++;
  262. stats->rx_missed_errors++;
  263. irda_device_set_media_busy(dev, TRUE);
  264. break;
  265. case OUTSIDE_FRAME:
  266. case BEGIN_FRAME:
  267. default:
  268. /* We may receive multiple BOF at the start of frame */
  269. break;
  270. }
  271. /* Now receiving frame */
  272. rx_buff->state = BEGIN_FRAME;
  273. rx_buff->in_frame = TRUE;
  274. /* Time to initialize receive buffer */
  275. rx_buff->data = rx_buff->head;
  276. rx_buff->len = 0;
  277. rx_buff->fcs = INIT_FCS;
  278. }
  279. /*
  280. * Function async_unwrap_eof(dev, byte)
  281. *
  282. * Handle End Of Frame character received within a frame
  283. *
  284. */
  285. static inline void
  286. async_unwrap_eof(struct net_device *dev,
  287. struct net_device_stats *stats,
  288. iobuff_t *rx_buff, __u8 byte)
  289. {
  290. #ifdef POSTPONE_RX_CRC
  291. int i;
  292. #endif
  293. switch(rx_buff->state) {
  294. case OUTSIDE_FRAME:
  295. /* Probably missed the BOF */
  296. stats->rx_errors++;
  297. stats->rx_missed_errors++;
  298. irda_device_set_media_busy(dev, TRUE);
  299. break;
  300. case BEGIN_FRAME:
  301. case LINK_ESCAPE:
  302. case INSIDE_FRAME:
  303. default:
  304. /* Note : in the case of BEGIN_FRAME and LINK_ESCAPE,
  305. * the fcs will most likely not match and generate an
  306. * error, as expected - Jean II */
  307. rx_buff->state = OUTSIDE_FRAME;
  308. rx_buff->in_frame = FALSE;
  309. #ifdef POSTPONE_RX_CRC
  310. /* If we haven't done the CRC as we receive bytes, we
  311. * must do it now... Jean II */
  312. for(i = 0; i < rx_buff->len; i++)
  313. rx_buff->fcs = irda_fcs(rx_buff->fcs,
  314. rx_buff->data[i]);
  315. #endif
  316. /* Test FCS and signal success if the frame is good */
  317. if (rx_buff->fcs == GOOD_FCS) {
  318. /* Deliver frame */
  319. async_bump(dev, stats, rx_buff);
  320. break;
  321. } else {
  322. /* Wrong CRC, discard frame! */
  323. irda_device_set_media_busy(dev, TRUE);
  324. IRDA_DEBUG(1, "%s(), crc error\n", __FUNCTION__);
  325. stats->rx_errors++;
  326. stats->rx_crc_errors++;
  327. }
  328. break;
  329. }
  330. }
  331. /*
  332. * Function async_unwrap_ce(dev, byte)
  333. *
  334. * Handle Character Escape character received within a frame
  335. *
  336. */
  337. static inline void
  338. async_unwrap_ce(struct net_device *dev,
  339. struct net_device_stats *stats,
  340. iobuff_t *rx_buff, __u8 byte)
  341. {
  342. switch(rx_buff->state) {
  343. case OUTSIDE_FRAME:
  344. /* Activate carrier sense */
  345. irda_device_set_media_busy(dev, TRUE);
  346. break;
  347. case LINK_ESCAPE:
  348. IRDA_WARNING("%s: state not defined\n", __FUNCTION__);
  349. break;
  350. case BEGIN_FRAME:
  351. case INSIDE_FRAME:
  352. default:
  353. /* Stuffed byte coming */
  354. rx_buff->state = LINK_ESCAPE;
  355. break;
  356. }
  357. }
  358. /*
  359. * Function async_unwrap_other(dev, byte)
  360. *
  361. * Handle other characters received within a frame
  362. *
  363. */
  364. static inline void
  365. async_unwrap_other(struct net_device *dev,
  366. struct net_device_stats *stats,
  367. iobuff_t *rx_buff, __u8 byte)
  368. {
  369. switch(rx_buff->state) {
  370. /* This is on the critical path, case are ordered by
  371. * probability (most frequent first) - Jean II */
  372. case INSIDE_FRAME:
  373. /* Must be the next byte of the frame */
  374. if (rx_buff->len < rx_buff->truesize) {
  375. rx_buff->data[rx_buff->len++] = byte;
  376. #ifndef POSTPONE_RX_CRC
  377. rx_buff->fcs = irda_fcs(rx_buff->fcs, byte);
  378. #endif
  379. } else {
  380. IRDA_DEBUG(1, "%s(), Rx buffer overflow, aborting\n",
  381. __FUNCTION__);
  382. rx_buff->state = OUTSIDE_FRAME;
  383. }
  384. break;
  385. case LINK_ESCAPE:
  386. /*
  387. * Stuffed char, complement bit 5 of byte
  388. * following CE, IrLAP p.114
  389. */
  390. byte ^= IRDA_TRANS;
  391. if (rx_buff->len < rx_buff->truesize) {
  392. rx_buff->data[rx_buff->len++] = byte;
  393. #ifndef POSTPONE_RX_CRC
  394. rx_buff->fcs = irda_fcs(rx_buff->fcs, byte);
  395. #endif
  396. rx_buff->state = INSIDE_FRAME;
  397. } else {
  398. IRDA_DEBUG(1, "%s(), Rx buffer overflow, aborting\n",
  399. __FUNCTION__);
  400. rx_buff->state = OUTSIDE_FRAME;
  401. }
  402. break;
  403. case OUTSIDE_FRAME:
  404. /* Activate carrier sense */
  405. if(byte != XBOF)
  406. irda_device_set_media_busy(dev, TRUE);
  407. break;
  408. case BEGIN_FRAME:
  409. default:
  410. rx_buff->data[rx_buff->len++] = byte;
  411. #ifndef POSTPONE_RX_CRC
  412. rx_buff->fcs = irda_fcs(rx_buff->fcs, byte);
  413. #endif
  414. rx_buff->state = INSIDE_FRAME;
  415. break;
  416. }
  417. }
  418. /*
  419. * Function async_unwrap_char (dev, rx_buff, byte)
  420. *
  421. * Parse and de-stuff frame received from the IrDA-port
  422. *
  423. * This is the main entry point for SIR drivers.
  424. */
  425. void async_unwrap_char(struct net_device *dev,
  426. struct net_device_stats *stats,
  427. iobuff_t *rx_buff, __u8 byte)
  428. {
  429. switch(byte) {
  430. case CE:
  431. async_unwrap_ce(dev, stats, rx_buff, byte);
  432. break;
  433. case BOF:
  434. async_unwrap_bof(dev, stats, rx_buff, byte);
  435. break;
  436. case EOF:
  437. async_unwrap_eof(dev, stats, rx_buff, byte);
  438. break;
  439. default:
  440. async_unwrap_other(dev, stats, rx_buff, byte);
  441. break;
  442. }
  443. }
  444. EXPORT_SYMBOL(async_unwrap_char);