summaryrefslogtreecommitdiffstats
path: root/net/irda/wrapper.c
blob: 2ddf84efa649a4db973c0b3d686202537bc2c268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*********************************************************************
 *                
 * Filename:      wrapper.c
 * Version:       1.2
 * Description:   IrDA SIR async wrapper layer
 * Status:        Experimental.
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Created at:    Mon Aug  4 20:40:53 1997
 * Modified at:   Fri May 28 20:30:24 1999
 * Modified by:   Dag Brattli <dagb@cs.uit.no>
 * Modified at:   Fri May 28  3:11 CST 1999
 * Modified by:   Horst von Brand <vonbrand@sleipnir.valparaiso.cl>
 * 
 *     Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>, 
 *     All Rights Reserved.
 *     
 *     This program is free software; you can redistribute it and/or 
 *     modify it under the terms of the GNU General Public License as 
 *     published by the Free Software Foundation; either version 2 of 
 *     the License, or (at your option) any later version.
 *
 *     Neither Dag Brattli nor University of Tromsø admit liability nor
 *     provide warranty for any of this software. This material is 
 *     provided "AS-IS" and at no charge.
 *
 ********************************************************************/

#include <linux/skbuff.h>
#include <linux/string.h>
#include <asm/byteorder.h>

#include <net/irda/irda.h>
#include <net/irda/wrapper.h>
#include <net/irda/irtty.h>
#include <net/irda/crc.h>
#include <net/irda/irlap.h>
#include <net/irda/irlap_frame.h>
#include <net/irda/irda_device.h>

static inline int stuff_byte(__u8 byte, __u8 *buf);

static void state_outside_frame(struct irda_device *idev, __u8 byte);
static void state_begin_frame(struct irda_device *idev, __u8 byte);
static void state_link_escape(struct irda_device *idev, __u8 byte);
static void state_inside_frame(struct irda_device *idev, __u8 byte);

static void (*state[])(struct irda_device *idev, __u8 byte) = 
{ 
	state_outside_frame,
	state_begin_frame,
	state_link_escape,
	state_inside_frame,
};

/*
 * Function async_wrap (skb, *tx_buff)
 *
 *    Makes a new buffer with wrapping and stuffing, should check that 
 *    we don't get tx buffer overflow.
 */
int async_wrap_skb(struct sk_buff *skb, __u8 *tx_buff, int buffsize)
{
 	int i;
	int n;
	int xbofs;
	union {
		__u16 value;
		__u8 bytes[2];
	} fcs;

	/* Initialize variables */
	fcs.value = INIT_FCS;
	n = 0;

	if (skb->len > 2048) {
		DEBUG(0, __FUNCTION__ "Warning size=%d of sk_buff to big!\n", 
		      (int) skb->len);
		return 0;
	}
	
	/*
	 *  Send  XBOF's for required min. turn time and for the negotiated
	 *  additional XBOFS
	 */
	if (((struct irlap_skb_cb *)(skb->cb))->magic != LAP_MAGIC) {
		DEBUG(1, __FUNCTION__ "(), wrong magic in skb!\n");
		xbofs = 10;
	} else
		xbofs = ((struct irlap_skb_cb *)(skb->cb))->xbofs;

	memset(tx_buff+n, XBOF, xbofs);
	n += xbofs;

	/* Start of packet character BOF */
	tx_buff[n++] = BOF;

	/* Insert frame and calc CRC */
	for (i=0; i < skb->len; i++) {
		/*
		 *  Check for the possibility of tx buffer overflow. We use
		 *  bufsize-5 since the maximum number of bytes that can be 
		 *  transmitted after this point is 5.
		 */
		ASSERT(n < (buffsize-5), return n;);

		n += stuff_byte(skb->data[i], tx_buff+n);
		fcs.value = irda_fcs(fcs.value, skb->data[i]);
	}
	
	/* Insert CRC in little endian format (LSB first) */
	fcs.value = ~fcs.value;
#ifdef __LITTLE_ENDIAN
	n += stuff_byte(fcs.bytes[0], tx_buff+n);
	n += stuff_byte(fcs.bytes[1], tx_buff+n);
#else ifdef __BIG_ENDIAN
	n += stuff_byte(fcs.bytes[1], tx_buff+n);
	n += stuff_byte(fcs.bytes[0], tx_buff+n);
#endif
	tx_buff[n++] = EOF;

	return n;
}

/*
 * Function async_bump (idev)
 *
 *    Got a frame, make a copy of it, and pass it up the stack!
 *
 */
static inline void async_bump(struct irda_device *idev, __u8 *buf, int len)
{
       	struct sk_buff *skb;

	skb = dev_alloc_skb(len+1);
	if (!skb)  {
		idev->stats.rx_dropped++;
		return;
	}

	/*  Align IP header to 20 bytes */
	skb_reserve(skb, 1);
	
        /* Copy data without CRC */
	memcpy(skb_put(skb, len-2), buf, len-2); 
	
	/* 
	 *  Feed it to IrLAP layer 
	 */
	skb->dev = &idev->netdev;
	skb->mac.raw  = skb->data;
	skb->protocol = htons(ETH_P_IRDA);

	netif_rx(skb);
	idev->stats.rx_packets++;
	idev->stats.rx_bytes += skb->len;	
}

/*
 * Function stuff_byte (byte, buf)
 *
 *    Byte stuff one single byte and put the result in buffer pointed to by
 *    buf. The buffer must at all times be able to have two bytes inserted.
 * 
 */
static inline int stuff_byte(__u8 byte, __u8 *buf) 
{
	switch (byte) {
	case BOF: /* FALLTHROUGH */
	case EOF: /* FALLTHROUGH */
	case CE:
		/* Insert transparently coded */
		buf[0] = CE;               /* Send link escape */
		buf[1] = byte^IRDA_TRANS;    /* Complement bit 5 */
		return 2;
		/* break; */
	default:
		 /* Non-special value, no transparency required */
		buf[0] = byte;
		return 1;
		/* break; */
	}
}

/*
 * Function async_unwrap (skb)
 *
 *    Parse and de-stuff frame received from the IrDA-port
 *
 */
inline void async_unwrap_char(struct irda_device *idev, __u8 byte)
{
	(*state[idev->rx_buff.state]) (idev, byte);
}
	 
/*
 * Function state_outside_frame (idev, byte)
 *
 *    
 *
 */
static void state_outside_frame(struct irda_device *idev, __u8 byte)
{
	switch (byte) {
	case BOF:
		idev->rx_buff.state = BEGIN_FRAME;
		idev->rx_buff.in_frame = TRUE;
		break;
	case XBOF:
		/* idev->xbofs++; */
		break;
	case EOF:
		irda_device_set_media_busy( idev, TRUE);
		break;
	default:
		break;
	}
}

/*
 * Function state_begin_frame (idev, byte)
 *
 *    Begin of frame detected
 *
 */
static void state_begin_frame(struct irda_device *idev, __u8 byte)
{
	switch (byte) {
	case BOF:
		/* Continue */
		break;
	case CE:
		/* Stuffed byte */
		idev->rx_buff.state = LINK_ESCAPE;

		/* Time to initialize receive buffer */
		idev->rx_buff.data = idev->rx_buff.head;
		idev->rx_buff.len = 0;
		break;
	case EOF:
		/* Abort frame */
		idev->rx_buff.state = OUTSIDE_FRAME;
		
		idev->stats.rx_errors++;
		idev->stats.rx_frame_errors++;
		break;
	default:	
		/* Time to initialize receive buffer */
		idev->rx_buff.data = idev->rx_buff.head;
		idev->rx_buff.len = 0;

		idev->rx_buff.data[idev->rx_buff.len++] = byte;
		
		idev->rx_buff.fcs = irda_fcs(INIT_FCS, byte);
		idev->rx_buff.state = INSIDE_FRAME;
		break;
	}
}

/*
 * Function state_link_escape (idev, byte)
 *
 *    
 *
 */
static void state_link_escape(struct irda_device *idev, __u8 byte)
{
	switch (byte) {
	case BOF: /* New frame? */
		idev->rx_buff.state = BEGIN_FRAME;
		irda_device_set_media_busy(idev, TRUE);
		break;
	case CE:
		DEBUG(4, "WARNING: State not defined\n");
		break;
	case EOF: /* Abort frame */
		idev->rx_buff.state = OUTSIDE_FRAME;
		break;
	default:
		/* 
		 *  Stuffed char, complement bit 5 of byte 
		 *  following CE, IrLAP p.114 
		 */
		byte ^= IRDA_TRANS;
		if (idev->rx_buff.len < idev->rx_buff.truesize)  {
			idev->rx_buff.data[idev->rx_buff.len++] = byte;
			idev->rx_buff.fcs = irda_fcs(idev->rx_buff.fcs, byte);
			idev->rx_buff.state = INSIDE_FRAME;
		} else {
			DEBUG(1, __FUNCTION__ 
			      "(), Rx buffer overflow, aborting\n");
			idev->rx_buff.state = OUTSIDE_FRAME;
		}
		break;
	}
}

/*
 * Function state_inside_frame (idev, byte)
 *
 *    Handle bytes received within a frame
 *
 */
static void state_inside_frame(struct irda_device *idev, __u8 byte)
{
	switch (byte) {
	case BOF: /* New frame? */
		idev->rx_buff.state = BEGIN_FRAME;
		irda_device_set_media_busy(idev, TRUE);
		break;
	case CE: /* Stuffed char */
		idev->rx_buff.state = LINK_ESCAPE;
		break;
	case EOF: /* End of frame */
		idev->rx_buff.state = OUTSIDE_FRAME;
		idev->rx_buff.in_frame = FALSE;
		
		/* Test FCS and deliver frame if it's good */
		if (idev->rx_buff.fcs == GOOD_FCS) {
			async_bump(idev, idev->rx_buff.data, 
				   idev->rx_buff.len);
		} else {
			/* Wrong CRC, discard frame!  */
			irda_device_set_media_busy(idev, TRUE); 
			
			idev->stats.rx_errors++;
			idev->stats.rx_crc_errors++;
		}			
		break;
	default: /* Must be the next byte of the frame */
		if (idev->rx_buff.len < idev->rx_buff.truesize)  {
			idev->rx_buff.data[idev->rx_buff.len++] = byte;
			idev->rx_buff.fcs = irda_fcs(idev->rx_buff.fcs, byte);
		} else {
			DEBUG(1, __FUNCTION__ 
			      "(), Rx buffer overflow, aborting\n");
			idev->rx_buff.state = OUTSIDE_FRAME;
		}
		break;
	}
}