00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <linux/skbuff.h>
00029 #include <linux/string.h>
00030 #include <asm/byteorder.h>
00031
00032 #include <net/irda/irda.h>
00033 #include <net/irda/wrapper.h>
00034 #include <net/irda/irtty.h>
00035 #include <net/irda/crc.h>
00036 #include <net/irda/irlap.h>
00037 #include <net/irda/irlap_frame.h>
00038 #include <net/irda/irda_device.h>
00039
00040 static inline int stuff_byte(__u8 byte, __u8 *buf);
00041
00042 static void state_outside_frame(struct device *dev,
00043 struct net_device_stats *stats,
00044 iobuff_t *rx_buff, __u8 byte);
00045 static void state_begin_frame(struct device *dev,
00046 struct net_device_stats *stats,
00047 iobuff_t *rx_buff, __u8 byte);
00048 static void state_link_escape(struct device *dev,
00049 struct net_device_stats *stats,
00050 iobuff_t *rx_buff, __u8 byte);
00051 static void state_inside_frame(struct device *dev,
00052 struct net_device_stats *stats,
00053 iobuff_t *rx_buff, __u8 byte);
00054
00055 static void (*state[])(struct device *dev, struct net_device_stats *stats,
00056 iobuff_t *rx_buff, __u8 byte) =
00057 {
00058 state_outside_frame,
00059 state_begin_frame,
00060 state_link_escape,
00061 state_inside_frame,
00062 };
00063
00064
00065
00066
00067
00068
00069
00070 int async_wrap_skb(struct sk_buff *skb, __u8 *tx_buff, int buffsize)
00071 {
00072 int xbofs;
00073 int i;
00074 int n;
00075 union {
00076 __u16 value;
00077 __u8 bytes[2];
00078 } fcs;
00079
00080
00081 fcs.value = INIT_FCS;
00082 n = 0;
00083
00084
00085
00086
00087
00088 if (((struct irda_skb_cb *)(skb->cb))->magic != LAP_MAGIC) {
00089
00090
00091
00092
00093
00094 IRDA_DEBUG(1, __FUNCTION__ "(), wrong magic in skb!\n");
00095 xbofs = 10;
00096 } else
00097 xbofs = ((struct irda_skb_cb *)(skb->cb))->xbofs;
00098
00099 IRDA_DEBUG(4, __FUNCTION__ "(), xbofs=%d\n", xbofs);
00100
00101
00102 if (xbofs > 163) {
00103 IRDA_DEBUG(0, __FUNCTION__ "(), too many xbofs (%d)\n", xbofs);
00104 xbofs = 163;
00105 }
00106
00107 memset(tx_buff+n, XBOF, xbofs);
00108 n += xbofs;
00109
00110
00111 tx_buff[n++] = BOF;
00112
00113
00114 for (i=0; i < skb->len; i++) {
00115
00116
00117
00118
00119
00120 ASSERT(n < (buffsize-5), return n;);
00121
00122 n += stuff_byte(skb->data[i], tx_buff+n);
00123 fcs.value = irda_fcs(fcs.value, skb->data[i]);
00124 }
00125
00126
00127 fcs.value = ~fcs.value;
00128 #ifdef __LITTLE_ENDIAN
00129 n += stuff_byte(fcs.bytes[0], tx_buff+n);
00130 n += stuff_byte(fcs.bytes[1], tx_buff+n);
00131 #else ifdef __BIG_ENDIAN
00132 n += stuff_byte(fcs.bytes[1], tx_buff+n);
00133 n += stuff_byte(fcs.bytes[0], tx_buff+n);
00134 #endif
00135 tx_buff[n++] = EOF;
00136
00137 return n;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147 static inline int stuff_byte(__u8 byte, __u8 *buf)
00148 {
00149 switch (byte) {
00150 case BOF:
00151 case EOF:
00152 case CE:
00153
00154 buf[0] = CE;
00155 buf[1] = byte^IRDA_TRANS;
00156 return 2;
00157
00158 default:
00159
00160 buf[0] = byte;
00161 return 1;
00162
00163 }
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 inline void async_bump(struct device *dev, struct net_device_stats *stats,
00173 __u8 *buf, int len)
00174 {
00175 struct sk_buff *skb;
00176
00177 skb = dev_alloc_skb(len+1);
00178 if (!skb) {
00179 stats->rx_dropped++;
00180 return;
00181 }
00182
00183
00184 skb_reserve(skb, 1);
00185
00186
00187 memcpy(skb_put(skb, len-2), buf, len-2);
00188
00189
00190 skb->dev = dev;
00191 skb->mac.raw = skb->data;
00192 skb->protocol = htons(ETH_P_IRDA);
00193
00194 netif_rx(skb);
00195
00196 stats->rx_packets++;
00197 stats->rx_bytes += len;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206 inline void async_unwrap_char(struct device *dev,
00207 struct net_device_stats *stats,
00208 iobuff_t *rx_buff, __u8 byte)
00209 {
00210 (*state[rx_buff->state])(dev, stats, rx_buff, byte);
00211 }
00212
00213
00214
00215
00216
00217
00218
00219 static void state_outside_frame(struct device *dev,
00220 struct net_device_stats *stats,
00221 iobuff_t *rx_buff, __u8 byte)
00222 {
00223 switch (byte) {
00224 case BOF:
00225 rx_buff->state = BEGIN_FRAME;
00226 rx_buff->in_frame = TRUE;
00227 break;
00228 case XBOF:
00229
00230 break;
00231 case EOF:
00232 irda_device_set_media_busy(dev, TRUE);
00233 break;
00234 default:
00235 irda_device_set_media_busy(dev, TRUE);
00236 break;
00237 }
00238 }
00239
00240
00241
00242
00243
00244
00245
00246 static void state_begin_frame(struct device *dev,
00247 struct net_device_stats *stats,
00248 iobuff_t *rx_buff, __u8 byte)
00249 {
00250
00251 rx_buff->data = rx_buff->head;
00252 rx_buff->len = 0;
00253 rx_buff->fcs = INIT_FCS;
00254
00255 switch (byte) {
00256 case BOF:
00257
00258 break;
00259 case CE:
00260
00261 rx_buff->state = LINK_ESCAPE;
00262 break;
00263 case EOF:
00264
00265 rx_buff->state = OUTSIDE_FRAME;
00266 IRDA_DEBUG(1, __FUNCTION__ "(), abort frame\n");
00267 stats->rx_errors++;
00268 stats->rx_frame_errors++;
00269 break;
00270 default:
00271 rx_buff->data[rx_buff->len++] = byte;
00272 rx_buff->fcs = irda_fcs(rx_buff->fcs, byte);
00273 rx_buff->state = INSIDE_FRAME;
00274 break;
00275 }
00276 }
00277
00278
00279
00280
00281
00282
00283
00284 static void state_link_escape(struct device *dev,
00285 struct net_device_stats *stats,
00286 iobuff_t *rx_buff, __u8 byte)
00287 {
00288 switch (byte) {
00289 case BOF:
00290 rx_buff->state = BEGIN_FRAME;
00291 irda_device_set_media_busy(dev, TRUE);
00292 break;
00293 case CE:
00294 WARNING(__FUNCTION__ "(), state not defined\n");
00295 break;
00296 case EOF:
00297 rx_buff->state = OUTSIDE_FRAME;
00298 break;
00299 default:
00300
00301
00302
00303
00304 byte ^= IRDA_TRANS;
00305 if (rx_buff->len < rx_buff->truesize) {
00306 rx_buff->data[rx_buff->len++] = byte;
00307 rx_buff->fcs = irda_fcs(rx_buff->fcs, byte);
00308 rx_buff->state = INSIDE_FRAME;
00309 } else {
00310 IRDA_DEBUG(1, __FUNCTION__ "(), rx buffer overflow\n");
00311 rx_buff->state = OUTSIDE_FRAME;
00312 }
00313 break;
00314 }
00315 }
00316
00317
00318
00319
00320
00321
00322
00323 static void state_inside_frame(struct device *dev,
00324 struct net_device_stats *stats,
00325 iobuff_t *rx_buff, __u8 byte)
00326 {
00327 int ret = 0;
00328
00329 switch (byte) {
00330 case BOF:
00331 rx_buff->state = BEGIN_FRAME;
00332 irda_device_set_media_busy(dev, TRUE);
00333 break;
00334 case CE:
00335 rx_buff->state = LINK_ESCAPE;
00336 break;
00337 case EOF:
00338 rx_buff->state = OUTSIDE_FRAME;
00339 rx_buff->in_frame = FALSE;
00340
00341
00342 if (rx_buff->fcs == GOOD_FCS) {
00343
00344 async_bump(dev, stats, rx_buff->data, rx_buff->len);
00345 ret = TRUE;
00346 break;
00347 } else {
00348
00349 irda_device_set_media_busy(dev, TRUE);
00350
00351 IRDA_DEBUG(1, __FUNCTION__ "(), crc error\n");
00352 stats->rx_errors++;
00353 stats->rx_crc_errors++;
00354 }
00355 break;
00356 default:
00357 if (rx_buff->len < rx_buff->truesize) {
00358 rx_buff->data[rx_buff->len++] = byte;
00359 rx_buff->fcs = irda_fcs(rx_buff->fcs, byte);
00360 } else {
00361 IRDA_DEBUG(1, __FUNCTION__
00362 "(), Rx buffer overflow, aborting\n");
00363 rx_buff->state = OUTSIDE_FRAME;
00364 }
00365 break;
00366 }
00367 }
00368
00369