1 /*********************************************************************
5 * Description: IrDA SIR async wrapper layer
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>
14 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
15 * All Rights Reserved.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * Neither Dag Brattli nor University of Tromsø admit liability nor
23 * provide warranty for any of this software. This material is
24 * provided "AS-IS" and at no charge.
26 ********************************************************************/
28 #include <linux/skbuff.h>
29 #include <linux/string.h>
30 #include <asm/byteorder.h>
32 #include <net/irda/irda.h>
33 #include <net/irda/wrapper.h>
34 #include <net/irda/irtty.h>
35 #include <net/irda/crc.h>
36 #include <net/irda/irlap.h>
37 #include <net/irda/irlap_frame.h>
38 #include <net/irda/irda_device.h>
40 static inline int stuff_byte(__u8 byte, __u8 *buf);
42 static void state_outside_frame(struct net_device *dev,
43 struct net_device_stats *stats,
44 iobuff_t *rx_buff, __u8 byte);
45 static void state_begin_frame(struct net_device *dev,
46 struct net_device_stats *stats,
47 iobuff_t *rx_buff, __u8 byte);
48 static void state_link_escape(struct net_device *dev,
49 struct net_device_stats *stats,
50 iobuff_t *rx_buff, __u8 byte);
51 static void state_inside_frame(struct net_device *dev,
52 struct net_device_stats *stats,
53 iobuff_t *rx_buff, __u8 byte);
55 static void (*state[])(struct net_device *dev, struct net_device_stats *stats,
56 iobuff_t *rx_buff, __u8 byte) =
65 * Function async_wrap (skb, *tx_buff, buffsize)
67 * Makes a new buffer with wrapping and stuffing, should check that
68 * we don't get tx buffer overflow.
70 int async_wrap_skb(struct sk_buff *skb, __u8 *tx_buff, int buffsize)
72 struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
81 /* Initialize variables */
86 * Send XBOF's for required min. turn time and for the negotiated
90 if (cb->magic != LAP_MAGIC) {
92 * This will happen for all frames sent from user-space.
93 * Nothing to worry about, but we set the default number of
96 IRDA_DEBUG(1, __FUNCTION__ "(), wrong magic in skb!\n");
99 xbofs = cb->xbofs + cb->xbofs_delay;
101 IRDA_DEBUG(4, __FUNCTION__ "(), xbofs=%d\n", xbofs);
103 /* Check that we never use more than 115 + 48 xbofs */
105 IRDA_DEBUG(0, __FUNCTION__ "(), too many xbofs (%d)\n", xbofs);
109 memset(tx_buff+n, XBOF, xbofs);
112 /* Start of packet character BOF */
115 /* Insert frame and calc CRC */
116 for (i=0; i < skb->len; i++) {
118 * Check for the possibility of tx buffer overflow. We use
119 * bufsize-5 since the maximum number of bytes that can be
120 * transmitted after this point is 5.
122 ASSERT(n < (buffsize-5), return n;);
124 n += stuff_byte(skb->data[i], tx_buff+n);
125 fcs.value = irda_fcs(fcs.value, skb->data[i]);
128 /* Insert CRC in little endian format (LSB first) */
129 fcs.value = ~fcs.value;
130 #ifdef __LITTLE_ENDIAN
131 n += stuff_byte(fcs.bytes[0], tx_buff+n);
132 n += stuff_byte(fcs.bytes[1], tx_buff+n);
133 #else /* ifdef __BIG_ENDIAN */
134 n += stuff_byte(fcs.bytes[1], tx_buff+n);
135 n += stuff_byte(fcs.bytes[0], tx_buff+n);
143 * Function stuff_byte (byte, buf)
145 * Byte stuff one single byte and put the result in buffer pointed to by
146 * buf. The buffer must at all times be able to have two bytes inserted.
149 static inline int stuff_byte(__u8 byte, __u8 *buf)
152 case BOF: /* FALLTHROUGH */
153 case EOF: /* FALLTHROUGH */
155 /* Insert transparently coded */
156 buf[0] = CE; /* Send link escape */
157 buf[1] = byte^IRDA_TRANS; /* Complement bit 5 */
161 /* Non-special value, no transparency required */
169 * Function async_bump (buf, len, stats)
171 * Got a frame, make a copy of it, and pass it up the stack! We can try
172 * to inline it since it's only called from state_inside_frame
174 inline void async_bump(struct net_device *dev, struct net_device_stats *stats,
179 skb = dev_alloc_skb(len+1);
185 /* Align IP header to 20 bytes */
188 /* Copy data without CRC */
189 memcpy(skb_put(skb, len-2), buf, len-2);
191 /* Feed it to IrLAP layer */
193 skb->mac.raw = skb->data;
194 skb->protocol = htons(ETH_P_IRDA);
199 stats->rx_bytes += len;
203 * Function async_unwrap_char (dev, rx_buff, byte)
205 * Parse and de-stuff frame received from the IrDA-port
208 inline void async_unwrap_char(struct net_device *dev,
209 struct net_device_stats *stats,
210 iobuff_t *rx_buff, __u8 byte)
212 (*state[rx_buff->state])(dev, stats, rx_buff, byte);
216 * Function state_outside_frame (dev, rx_buff, byte)
218 * Not receiving any frame (or just bogus data)
221 static void state_outside_frame(struct net_device *dev,
222 struct net_device_stats *stats,
223 iobuff_t *rx_buff, __u8 byte)
227 rx_buff->state = BEGIN_FRAME;
228 rx_buff->in_frame = TRUE;
234 irda_device_set_media_busy(dev, TRUE);
237 irda_device_set_media_busy(dev, TRUE);
243 * Function state_begin_frame (idev, byte)
245 * Begin of frame detected
248 static void state_begin_frame(struct net_device *dev,
249 struct net_device_stats *stats,
250 iobuff_t *rx_buff, __u8 byte)
252 /* Time to initialize receive buffer */
253 rx_buff->data = rx_buff->head;
255 rx_buff->fcs = INIT_FCS;
263 rx_buff->state = LINK_ESCAPE;
267 rx_buff->state = OUTSIDE_FRAME;
268 IRDA_DEBUG(1, __FUNCTION__ "(), abort frame\n");
270 stats->rx_frame_errors++;
273 rx_buff->data[rx_buff->len++] = byte;
274 rx_buff->fcs = irda_fcs(rx_buff->fcs, byte);
275 rx_buff->state = INSIDE_FRAME;
281 * Function state_link_escape (dev, byte)
283 * Found link escape character
286 static void state_link_escape(struct net_device *dev,
287 struct net_device_stats *stats,
288 iobuff_t *rx_buff, __u8 byte)
291 case BOF: /* New frame? */
292 IRDA_DEBUG(1, __FUNCTION__
293 "(), Discarding incomplete frame\n");
294 rx_buff->state = BEGIN_FRAME;
295 irda_device_set_media_busy(dev, TRUE);
298 WARNING(__FUNCTION__ "(), state not defined\n");
300 case EOF: /* Abort frame */
301 rx_buff->state = OUTSIDE_FRAME;
305 * Stuffed char, complement bit 5 of byte
306 * following CE, IrLAP p.114
309 if (rx_buff->len < rx_buff->truesize) {
310 rx_buff->data[rx_buff->len++] = byte;
311 rx_buff->fcs = irda_fcs(rx_buff->fcs, byte);
312 rx_buff->state = INSIDE_FRAME;
314 IRDA_DEBUG(1, __FUNCTION__ "(), rx buffer overflow\n");
315 rx_buff->state = OUTSIDE_FRAME;
322 * Function state_inside_frame (dev, byte)
324 * Handle bytes received within a frame
327 static void state_inside_frame(struct net_device *dev,
328 struct net_device_stats *stats,
329 iobuff_t *rx_buff, __u8 byte)
334 case BOF: /* New frame? */
335 IRDA_DEBUG(1, __FUNCTION__
336 "(), Discarding incomplete frame\n");
337 rx_buff->state = BEGIN_FRAME;
338 irda_device_set_media_busy(dev, TRUE);
340 case CE: /* Stuffed char */
341 rx_buff->state = LINK_ESCAPE;
343 case EOF: /* End of frame */
344 rx_buff->state = OUTSIDE_FRAME;
345 rx_buff->in_frame = FALSE;
347 /* Test FCS and signal success if the frame is good */
348 if (rx_buff->fcs == GOOD_FCS) {
350 async_bump(dev, stats, rx_buff->data, rx_buff->len);
354 /* Wrong CRC, discard frame! */
355 irda_device_set_media_busy(dev, TRUE);
357 IRDA_DEBUG(1, __FUNCTION__ "(), crc error\n");
359 stats->rx_crc_errors++;
362 default: /* Must be the next byte of the frame */
363 if (rx_buff->len < rx_buff->truesize) {
364 rx_buff->data[rx_buff->len++] = byte;
365 rx_buff->fcs = irda_fcs(rx_buff->fcs, byte);
367 IRDA_DEBUG(1, __FUNCTION__
368 "(), Rx buffer overflow, aborting\n");
369 rx_buff->state = OUTSIDE_FRAME;