1 /*********************************************************************
5 * Description: IrDA SIR async wrapper layer
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Mon Aug 4 20:40:53 1997
9 * Modified at: Mon Oct 18 15:51:58 1999
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-1999 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 int state_outside_frame(struct net_device
*dev
,
43 struct iobuff_t
*rx_buff
, __u8 byte
);
44 static int state_begin_frame(struct net_device
*dev
,
45 struct iobuff_t
*rx_buff
, __u8 byte
);
46 static int state_link_escape(struct net_device
*dev
,
47 struct iobuff_t
*rx_buff
, __u8 byte
);
48 static int state_inside_frame(struct net_device
*dev
,
49 struct iobuff_t
*rx_buff
, __u8 byte
);
51 static int (*state
[])(struct net_device
*dev
, struct iobuff_t
*rx_buff
,
61 * Function async_wrap (skb, *tx_buff, buffsize)
63 * Makes a new buffer with wrapping and stuffing, should check that
64 * we don't get tx buffer overflow.
66 int async_wrap_skb(struct sk_buff
*skb
, __u8
*tx_buff
, int buffsize
)
76 /* Initialize variables */
81 * Send XBOF's for required min. turn time and for the negotiated
84 if (((struct irda_skb_cb
*)(skb
->cb
))->magic
!= LAP_MAGIC
) {
85 IRDA_DEBUG(1, __FUNCTION__
"(), wrong magic in skb!\n");
88 xbofs
= ((struct irda_skb_cb
*)(skb
->cb
))->xbofs
;
90 memset(tx_buff
+n
, XBOF
, xbofs
);
93 /* Start of packet character BOF */
96 /* Insert frame and calc CRC */
97 for (i
=0; i
< skb
->len
; i
++) {
99 * Check for the possibility of tx buffer overflow. We use
100 * bufsize-5 since the maximum number of bytes that can be
101 * transmitted after this point is 5.
103 ASSERT(n
< (buffsize
-5), return n
;);
105 n
+= stuff_byte(skb
->data
[i
], tx_buff
+n
);
106 fcs
.value
= irda_fcs(fcs
.value
, skb
->data
[i
]);
109 /* Insert CRC in little endian format (LSB first) */
110 fcs
.value
= ~fcs
.value
;
111 #ifdef __LITTLE_ENDIAN
112 n
+= stuff_byte(fcs
.bytes
[0], tx_buff
+n
);
113 n
+= stuff_byte(fcs
.bytes
[1], tx_buff
+n
);
114 #else ifdef __BIG_ENDIAN
115 n
+= stuff_byte(fcs
.bytes
[1], tx_buff
+n
);
116 n
+= stuff_byte(fcs
.bytes
[0], tx_buff
+n
);
124 * Function stuff_byte (byte, buf)
126 * Byte stuff one single byte and put the result in buffer pointed to by
127 * buf. The buffer must at all times be able to have two bytes inserted.
130 static inline int stuff_byte(__u8 byte
, __u8
*buf
)
133 case BOF
: /* FALLTHROUGH */
134 case EOF
: /* FALLTHROUGH */
136 /* Insert transparently coded */
137 buf
[0] = CE
; /* Send link escape */
138 buf
[1] = byte
^IRDA_TRANS
; /* Complement bit 5 */
142 /* Non-special value, no transparency required */
150 * Function async_bump (buf, len, stats)
152 * Got a frame, make a copy of it, and pass it up the stack!
155 int async_bump(struct net_device
*dev
, __u8
*buf
, int len
)
157 struct net_device_stats
*stats
= dev
->get_stats(dev
);
160 skb
= dev_alloc_skb(len
+1);
166 /* Align IP header to 20 bytes */
169 /* Copy data without CRC */
170 memcpy(skb_put(skb
, len
-2), buf
, len
-2);
172 /* Feed it to IrLAP layer */
174 skb
->mac
.raw
= skb
->data
;
175 skb
->protocol
= htons(ETH_P_IRDA
);
180 stats
->rx_bytes
+= len
;
186 * Function async_unwrap_char (dev, rx_buff, byte)
188 * Parse and de-stuff frame received from the IrDA-port
191 inline int async_unwrap_char(struct net_device
*dev
, struct iobuff_t
*rx_buff
,
194 return (*state
[rx_buff
->state
])(dev
, rx_buff
, byte
);
198 * Function state_outside_frame (dev, rx_buff, byte)
203 static int state_outside_frame(struct net_device
*dev
,
204 struct iobuff_t
*rx_buff
, __u8 byte
)
208 rx_buff
->state
= BEGIN_FRAME
;
209 rx_buff
->in_frame
= TRUE
;
215 irda_device_set_media_busy(dev
, TRUE
);
224 * Function state_begin_frame (idev, byte)
226 * Begin of frame detected
229 static int state_begin_frame(struct net_device
*dev
,
230 struct iobuff_t
*rx_buff
, __u8 byte
)
232 struct net_device_stats
*stats
= dev
->get_stats(dev
);
234 /* Time to initialize receive buffer */
235 rx_buff
->data
= rx_buff
->head
;
237 rx_buff
->fcs
= INIT_FCS
;
245 rx_buff
->state
= LINK_ESCAPE
;
249 rx_buff
->state
= OUTSIDE_FRAME
;
252 stats
->rx_frame_errors
++;
255 rx_buff
->data
[rx_buff
->len
++] = byte
;
257 rx_buff
->fcs
= irda_fcs(rx_buff
->fcs
, byte
);
258 rx_buff
->state
= INSIDE_FRAME
;
265 * Function state_link_escape (idev, byte)
270 static int state_link_escape(struct net_device
*dev
,
271 struct iobuff_t
*rx_buff
, __u8 byte
)
274 case BOF
: /* New frame? */
275 rx_buff
->state
= BEGIN_FRAME
;
276 irda_device_set_media_busy(dev
, TRUE
);
279 IRDA_DEBUG(4, "WARNING: State not defined\n");
281 case EOF
: /* Abort frame */
282 rx_buff
->state
= OUTSIDE_FRAME
;
286 * Stuffed char, complement bit 5 of byte
287 * following CE, IrLAP p.114
290 if (rx_buff
->len
< rx_buff
->truesize
) {
291 rx_buff
->data
[rx_buff
->len
++] = byte
;
292 rx_buff
->fcs
= irda_fcs(rx_buff
->fcs
, byte
);
293 rx_buff
->state
= INSIDE_FRAME
;
295 IRDA_DEBUG(1, __FUNCTION__
296 "(), Rx buffer overflow, aborting\n");
297 rx_buff
->state
= OUTSIDE_FRAME
;
305 * Function state_inside_frame (idev, byte)
307 * Handle bytes received within a frame
310 static int state_inside_frame(struct net_device
*dev
,
311 struct iobuff_t
*rx_buff
, __u8 byte
)
313 struct net_device_stats
*stats
= dev
->get_stats(dev
);
317 case BOF
: /* New frame? */
318 rx_buff
->state
= BEGIN_FRAME
;
319 irda_device_set_media_busy(dev
, TRUE
);
321 case CE
: /* Stuffed char */
322 rx_buff
->state
= LINK_ESCAPE
;
324 case EOF
: /* End of frame */
325 rx_buff
->state
= OUTSIDE_FRAME
;
326 rx_buff
->in_frame
= FALSE
;
328 /* Test FCS and signal success if the frame is good */
329 if (rx_buff
->fcs
== GOOD_FCS
) {
331 async_bump(dev
, rx_buff
->data
, rx_buff
->len
);
335 /* Wrong CRC, discard frame! */
336 irda_device_set_media_busy(dev
, TRUE
);
339 stats
->rx_crc_errors
++;
342 default: /* Must be the next byte of the frame */
343 if (rx_buff
->len
< rx_buff
->truesize
) {
344 rx_buff
->data
[rx_buff
->len
++] = byte
;
345 rx_buff
->fcs
= irda_fcs(rx_buff
->fcs
, byte
);
347 IRDA_DEBUG(1, __FUNCTION__
348 "(), Rx buffer overflow, aborting\n");
349 rx_buff
->state
= OUTSIDE_FRAME
;