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