~ubuntu-branches/ubuntu/trusty/proll/trusty

« back to all changes in this revision

Viewing changes to .pc/001_qemu.patch/src/packet.c

  • Committer: Bazaar Package Importer
  • Author(s): Guillem Jover
  • Date: 2011-06-08 04:35:59 UTC
  • Revision ID: james.westby@ubuntu.com-20110608043559-8ln88b583x0uqcox
Tags: 18-5
* Switch to debhelper compatibility level 7.
* Use dh_prep instead of “dh_clean -k”.
* Use $(filter ...) instead of $(findstring ...) to extract space separated
  options from DEB_BUILD_OPTIONS in debian/rules.
* Switch to source format “3.0 (quilt)”:
  - Remove quilt from Build-Depends.
  - Remove quilt.make include from debian/rules.
  - Remove patch and unpatch targets from debian/rules.
  - Remove now unneeded debian/README.source.
* Now using Standards-Version 3.9.2 (no changes needed).
* Remove leading ‘./’ from lintian overrides.
* Rename build target to build-indep, install to install-indep, and add
  an empty build-arch target.
* Use dpkg-buildflags to set CPPFLAGS, CFLAGS and LDFLAGS.
* Disable Sparc V9 specific unbuildable code.
* Add ${misc:Depends} to Depends field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Packet driver interface for Gero's bootstrap IP/TFTP and
 
3
 * Linux ethernet driver environment of the other side.
 
4
 * This is developed for the context of Proll.
 
5
 */
 
6
 
 
7
#include <general.h>            /* __P() */
 
8
#include <net.h>                /* ETH_ALEN for netpriv.h */
 
9
#include <romlib.h>             /* printk() */
 
10
#include "./netpriv.h"          /* Our own protos */
 
11
 
 
12
#define ETH_FRAME_MIN (ETH_DATA_MIN + ETH_HLEN) /* Min. bytes in frame w/o FCS*/
 
13
#define ETH_FRAME_MAX (ETH_DATA_MAX + ETH_HLEN) /* Max. bytes in frame w/o FCS*/
 
14
#define ETH_BUF_SIZE  (ETH_FRAME_MAX + ETH_FLEN) /* Buffer size includes FCS */
 
15
 
 
16
struct treg {
 
17
        int type;
 
18
        int (*func)();
 
19
};
 
20
 
 
21
#define MAXTYPES  3
 
22
 
 
23
static struct treg tregv[MAXTYPES];
 
24
 
 
25
#define MAXSKBS   35            /* hme: 32, then some */
 
26
#define SKBLN    (1536)         /* hme wants ETH_FRAME_LEN + 2, aligned to 64 */
 
27
 
 
28
struct skb_ent {
 
29
        char inuse;
 
30
        char queued;
 
31
        struct sk_buff skb;
 
32
};
 
33
 
 
34
static char skb_rbuf[64 + MAXSKBS*SKBLN];
 
35
static struct skb_ent skev[MAXSKBS];
 
36
 
 
37
static struct device *eth0;
 
38
 
 
39
static union {
 
40
        unsigned char s[ETH_BUF_SIZE + 4];
 
41
        int aligner;
 
42
} wbuf;
 
43
static struct sk_buff *rskb;
 
44
static int nqskb = 0;
 
45
 
 
46
 
 
47
void init_packet()
 
48
{
 
49
        int i;
 
50
 
 
51
        if (eth0 == 0) {
 
52
                printk("init_packet: no eth0\n");
 
53
                return;
 
54
        }
 
55
 
 
56
        memcpy(myhwaddr, eth0->dev_addr, ETH_ALEN);
 
57
        if ((*eth0->open)(eth0) != 0) {
 
58
                printk("init_packet: eth0 open error\n");
 
59
                return;
 
60
        }
 
61
 
 
62
        for (i = 0; i < MAXSKBS; i++) {
 
63
                skev[i].skb.allocn = i;
 
64
        }
 
65
}
 
66
 
 
67
unsigned char *reg_type(int ptype, int (*func)())
 
68
{
 
69
        int n;
 
70
        struct treg *tp;
 
71
 
 
72
        tp = tregv;
 
73
        for (n = 0; ; n++) {
 
74
                if (n >= MAXTYPES) return 0;
 
75
                if (tp->type == ptype) break;
 
76
                if (tp->func == 0) break;
 
77
                tp++;
 
78
        }
 
79
        tp->type = ptype;
 
80
        tp->func = func;
 
81
        return wbuf.s;
 
82
}
 
83
 
 
84
int write_packet(int leng, int type, unsigned char *dst)
 
85
{
 
86
        struct sk_buff *skb;
 
87
        unsigned char *s;
 
88
#if 0
 
89
        struct treg *tp;
 
90
#endif
 
91
 
 
92
#if 0
 
93
        tp = tregv;
 
94
        for (n = 0; ; n++) {
 
95
                if (n >= MAXTYPES) return -1;
 
96
                if (tp->type == ptype) break;
 
97
                tp++;
 
98
        }
 
99
#endif
 
100
 
 
101
        if ((skb = dev_alloc_skb(14+leng)) == 0) {
 
102
                printk("write_packet: no skb %d\n", leng);
 
103
                return -1;
 
104
        }
 
105
        s = (unsigned char *)skb->data;
 
106
 
 
107
        bcopy(dst, s+0, ETH_ALEN);
 
108
        bcopy(myhwaddr, s+6, ETH_ALEN);
 
109
        s[12] = type >> 8;
 
110
        s[13] = type;
 
111
        bcopy(wbuf.s, s+14, leng);
 
112
        skb->len = 14 + leng;
 
113
 
 
114
        if ((*eth0->hard_start_xmit)(skb, eth0) != 0) {
 
115
                printk("write_packet: tranmission error\n");
 
116
                return -1;
 
117
        }
 
118
 
 
119
        return 0;
 
120
}
 
121
 
 
122
/*
 
123
 */
 
124
void empty_buf()
 
125
{
 
126
 
 
127
        if (rskb != 0) {
 
128
                dev_kfree_skb(rskb);
 
129
                rskb = 0;
 
130
        } else {
 
131
/* P3 */ printk("empty_buf: already free\n");
 
132
        }
 
133
}
 
134
 
 
135
/*
 
136
 * Hardware driver registers with us.
 
137
 */
 
138
void ether_setup(struct device *dev) {
 
139
 
 
140
        if (eth0 != 0) {
 
141
                printk("Duplicate ether_setup\n");
 
142
        }
 
143
        eth0 = dev;
 
144
}
 
145
 
 
146
/*
 
147
 */
 
148
struct sk_buff *dev_alloc_skb(unsigned int size)
 
149
{
 
150
        int i;
 
151
        struct skb_ent *skep;
 
152
 
 
153
        if (size > SKBLN) {
 
154
                printk("dev_alloc_skb: too long (%d)\n", size);
 
155
                return 0;
 
156
        }
 
157
 
 
158
        skep = skev;
 
159
        for (i = 0; i < MAXSKBS; i++) {
 
160
                if (!skep->inuse) {
 
161
                        skep->inuse = 1;
 
162
                        skep->skb.data = (char *)
 
163
                            (((unsigned)skb_rbuf + 63) & (~63)) + i*SKBLN;
 
164
                        skep->skb.len = 0;
 
165
                        /* skep->skb.truesize = size; */
 
166
                        skep->skb.dev = 0;
 
167
                        skep->skb.protocol = 0;
 
168
                        return &skep->skb;
 
169
                }
 
170
                skep++;
 
171
        }
 
172
 
 
173
        return 0;
 
174
}
 
175
 
 
176
void dev_kfree_skb(struct sk_buff *skb) {
 
177
        int x = skb->allocn;
 
178
        skev[x].inuse = 0;
 
179
        if (skev[x].queued) {
 
180
/* P3 */ printk("kfree_skb: %d was queued\n", x);
 
181
                skev[x].queued = 0;
 
182
        }
 
183
}
 
184
 
 
185
void skb_reserve(struct sk_buff *skb, unsigned int len) {
 
186
        skb->data += len;
 
187
}
 
188
 
 
189
/*
 
190
 * Add data to an sk_buff
 
191
 */
 
192
unsigned char *skb_put(struct sk_buff *skb, unsigned int len) {
 
193
        char *p = skb->data + skb->len;
 
194
        if ((skb->len += len) > SKBLN) {
 
195
                printk("skb_put: too long (+%d %d)\n", len, skb->len);
 
196
                return 0;
 
197
        }
 
198
        return p;
 
199
}
 
200
 
 
201
/*
 
202
 */
 
203
void skb_trim(struct sk_buff *skb, unsigned int len) {
 
204
        if (skb->len > len) {
 
205
                skb->len = len;
 
206
        }
 
207
}
 
208
 
 
209
/*
 
210
 */
 
211
void
 
212
eth_copy_and_sum(struct sk_buff *dest, unsigned char *src, int len, int base)
 
213
{
 
214
        bcopy(src, dest->data, len);
 
215
}
 
216
 
 
217
unsigned short eth_type_trans(struct sk_buff *skb, struct device *dev)
 
218
{
 
219
        unsigned char *s = skb->data + 12;
 
220
        return s[0] << 8 | s[1];                /* Network order word */
 
221
}
 
222
 
 
223
void netif_rx(struct sk_buff *skb)
 
224
{
 
225
        int i, n;
 
226
        struct treg *tp;
 
227
        struct skb_ent *skep;
 
228
 
 
229
        /*
 
230
         * Queue it
 
231
         */
 
232
        skev[skb->allocn].queued = 1;
 
233
        nqskb++;
 
234
 
 
235
        /*
 
236
         * Process them
 
237
         */
 
238
        for (i = 0, skep = skev; i < MAXSKBS; i++, skep++) {
 
239
                if (nqskb == 0 || rskb != 0)    /* Optimize || deferred */
 
240
                        break;
 
241
 
 
242
                if (!skep->queued)
 
243
                        continue;
 
244
                /*
 
245
                 * Dequeue
 
246
                 */
 
247
                --nqskb;
 
248
                skep->queued = 0;
 
249
                skb = &skep->skb;
 
250
 
 
251
                for (n = 0, tp = tregv; n < MAXTYPES; n++, tp++) {
 
252
                        if (tp->type == skb->protocol && tp->func != 0)
 
253
                                break;
 
254
                }
 
255
                if (n >= MAXTYPES) {
 
256
#if 1   /* Useful. Comment it out for busy networks. */
 
257
                        printk("netif_rx: dropping type %x, no func\n",
 
258
                            skb->protocol);
 
259
#endif
 
260
                        dev_kfree_skb(skb);
 
261
                        continue;
 
262
                }
 
263
 
 
264
                rskb = skb;
 
265
                if ((*tp->func)(skb->data + ETH_HLEN,
 
266
                   skb->len - ETH_HLEN, skb->data+6) == 0) {
 
267
                        dev_kfree_skb(skb);
 
268
                        rskb = 0;
 
269
                }
 
270
        }
 
271
 
 
272
        return;
 
273
}
 
274
 
 
275
/*
 
276
 */
 
277
unsigned int ld4(void *ptr) {
 
278
        unsigned char *s = ptr;
 
279
        return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
 
280
}
 
281
 
 
282
void st4(void *ptr, unsigned int n) {
 
283
        unsigned char *s = ptr;
 
284
        s[0] = n >> 24;  s[1] = n >> 16;  s[2] = n >> 8;  s[3] = n;
 
285
}