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#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/if_arp.h>
32#include <net/arp.h>
33#include <linux/netdevice.h>
34#include <linux/skbuff.h>
35#include <linux/arcdevice.h>
36
37#define VERSION "arcnet: cap mode (`c') encapsulation support loaded.\n"
38
39
40static void rx(struct net_device *dev, int bufnum,
41 struct archdr *pkthdr, int length);
42static int build_header(struct sk_buff *skb,
43 struct net_device *dev,
44 unsigned short type,
45 uint8_t daddr);
46static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
47 int bufnum);
48static int ack_tx(struct net_device *dev, int acked);
49
50
51static struct ArcProto capmode_proto =
52{
53 'r',
54 XMTU,
55 0,
56 rx,
57 build_header,
58 prepare_tx,
59 NULL,
60 ack_tx
61};
62
63
64static void arcnet_cap_init(void)
65{
66 int count;
67
68 for (count = 1; count <= 8; count++)
69 if (arc_proto_map[count] == arc_proto_default)
70 arc_proto_map[count] = &capmode_proto;
71
72
73 if (arc_bcast_proto == arc_proto_default)
74 arc_bcast_proto = &capmode_proto;
75
76 arc_proto_default = &capmode_proto;
77 arc_raw_proto = &capmode_proto;
78}
79
80
81#ifdef MODULE
82
83static int __init capmode_module_init(void)
84{
85 printk(VERSION);
86 arcnet_cap_init();
87 return 0;
88}
89
90static void __exit capmode_module_exit(void)
91{
92 arcnet_unregister_proto(&capmode_proto);
93}
94module_init(capmode_module_init);
95module_exit(capmode_module_exit);
96
97MODULE_LICENSE("GPL");
98#endif
99
100
101
102
103static void rx(struct net_device *dev, int bufnum,
104 struct archdr *pkthdr, int length)
105{
106 struct arcnet_local *lp = netdev_priv(dev);
107 struct sk_buff *skb;
108 struct archdr *pkt = pkthdr;
109 char *pktbuf, *pkthdrbuf;
110 int ofs;
111
112 BUGMSG(D_DURING, "it's a raw(cap) packet (length=%d)\n", length);
113
114 if (length >= MinTU)
115 ofs = 512 - length;
116 else
117 ofs = 256 - length;
118
119 skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC);
120 if (skb == NULL) {
121 BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
122 dev->stats.rx_dropped++;
123 return;
124 }
125 skb_put(skb, length + ARC_HDR_SIZE + sizeof(int));
126 skb->dev = dev;
127 skb_reset_mac_header(skb);
128 pkt = (struct archdr *)skb_mac_header(skb);
129 skb_pull(skb, ARC_HDR_SIZE);
130
131
132
133
134
135
136 pktbuf=(char*)pkt;
137 pkthdrbuf=(char*)pkthdr;
138 memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto));
139 memcpy(pktbuf+ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto)+sizeof(int),
140 pkthdrbuf+ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto),
141 sizeof(struct archdr)-ARC_HDR_SIZE-sizeof(pkt->soft.cap.proto));
142
143 if (length > sizeof(pkt->soft))
144 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
145 pkt->soft.raw + sizeof(pkt->soft)
146 + sizeof(int),
147 length - sizeof(pkt->soft));
148
149 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
150
151 skb->protocol = cpu_to_be16(ETH_P_ARCNET);
152 netif_rx(skb);
153}
154
155
156
157
158
159
160static int build_header(struct sk_buff *skb,
161 struct net_device *dev,
162 unsigned short type,
163 uint8_t daddr)
164{
165 int hdr_size = ARC_HDR_SIZE;
166 struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
167
168 BUGMSG(D_PROTO, "Preparing header for cap packet %x.\n",
169 *((int*)&pkt->soft.cap.cookie[0]));
170
171
172
173
174
175
176
177 pkt->hard.source = *dev->dev_addr;
178
179
180
181 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
182
183
184
185
186 pkt->hard.dest = 0;
187 return hdr_size;
188 }
189
190 pkt->hard.dest = daddr;
191
192 return hdr_size;
193}
194
195
196static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
197 int bufnum)
198{
199 struct arcnet_local *lp = netdev_priv(dev);
200 struct arc_hardware *hard = &pkt->hard;
201 int ofs;
202
203
204
205 length -= ARC_HDR_SIZE;
206
207 length -= sizeof(int);
208
209 BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
210 lp->next_tx, lp->cur_tx, bufnum);
211
212 BUGMSG(D_PROTO, "Sending for cap packet %x.\n",
213 *((int*)&pkt->soft.cap.cookie[0]));
214
215 if (length > XMTU) {
216
217 BUGMSG(D_NORMAL, "Bug! prepare_tx with size %d (> %d)\n",
218 length, XMTU);
219 length = XMTU;
220 }
221 if (length > MinTU) {
222 hard->offset[0] = 0;
223 hard->offset[1] = ofs = 512 - length;
224 } else if (length > MTU) {
225 hard->offset[0] = 0;
226 hard->offset[1] = ofs = 512 - length - 3;
227 } else
228 hard->offset[0] = ofs = 256 - length;
229
230 BUGMSG(D_DURING, "prepare_tx: length=%d ofs=%d\n",
231 length,ofs);
232
233
234 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
235 lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
236 sizeof(pkt->soft.cap.proto));
237
238
239
240 lp->hw.copy_to_card(dev, bufnum, ofs+1,
241 ((unsigned char*)&pkt->soft.cap.mes),length-1);
242
243 lp->lastload_dest = hard->dest;
244
245 return 1;
246}
247
248
249static int ack_tx(struct net_device *dev, int acked)
250{
251 struct arcnet_local *lp = netdev_priv(dev);
252 struct sk_buff *ackskb;
253 struct archdr *ackpkt;
254 int length=sizeof(struct arc_cap);
255
256 BUGMSG(D_DURING, "capmode: ack_tx: protocol: %x: result: %d\n",
257 lp->outgoing.skb->protocol, acked);
258
259 BUGLVL(D_SKB) arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
260
261
262 ackskb = alloc_skb(length + ARC_HDR_SIZE , GFP_ATOMIC);
263 if (ackskb == NULL) {
264 BUGMSG(D_NORMAL, "Memory squeeze, can't acknowledge.\n");
265 goto free_outskb;
266 }
267
268 skb_put(ackskb, length + ARC_HDR_SIZE );
269 ackskb->dev = dev;
270
271 skb_reset_mac_header(ackskb);
272 ackpkt = (struct archdr *)skb_mac_header(ackskb);
273
274
275
276 skb_copy_from_linear_data(lp->outgoing.skb, ackpkt,
277 ARC_HDR_SIZE + sizeof(struct arc_cap));
278 ackpkt->soft.cap.proto=0;
279 ackpkt->soft.cap.mes.ack=acked;
280
281 BUGMSG(D_PROTO, "Ackknowledge for cap packet %x.\n",
282 *((int*)&ackpkt->soft.cap.cookie[0]));
283
284 ackskb->protocol = cpu_to_be16(ETH_P_ARCNET);
285
286 BUGLVL(D_SKB) arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
287 netif_rx(ackskb);
288
289 free_outskb:
290 dev_kfree_skb_irq(lp->outgoing.skb);
291 lp->outgoing.proto = NULL;
292
293 return 0;
294}
295