8391324f684bbdd5b83dbf5e701c11fa1d68c816
[cascardo/linux.git] / drivers / net / arcnet / capmode.c
1 /*
2  * Linux ARCnet driver - "cap mode" packet encapsulation.
3  * It adds sequence numbers to packets for communicating between a user space
4  * application and the driver. After a transmit it sends a packet with protocol
5  * byte 0 back up to the userspace containing the sequence number of the packet
6  * plus the transmit-status on the ArcNet.
7  *
8  * Written 2002-4 by Esben Nielsen, Vestas Wind Systems A/S
9  * Derived from arc-rawmode.c by Avery Pennarun.
10  * arc-rawmode was in turned based on skeleton.c, see below.
11  *
12  * **********************
13  *
14  * The original copyright of skeleton.c was as follows:
15  *
16  * skeleton.c Written 1993 by Donald Becker.
17  * Copyright 1993 United States Government as represented by the
18  * Director, National Security Agency.  This software may only be used
19  * and distributed according to the terms of the GNU General Public License as
20  * modified by SRC, incorporated herein by reference.
21  *
22  * **********************
23  *
24  * For more details, see drivers/net/arcnet.c
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 /* packet receiver */
41 static 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         /* up to sizeof(pkt->soft) has already been copied from the card
70          * squeeze in an int for the cap encapsulation
71          * use these variables to be sure we count in bytes, not in
72          * sizeof(struct archdr)
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 /* Create the ARCnet hard/soft headers for cap mode.
94  * There aren't any soft headers in cap mode - not even the protocol id.
95  */
96 static int build_header(struct sk_buff *skb,
97                         struct net_device *dev,
98                         unsigned short type,
99                         uint8_t daddr)
100 {
101         int hdr_size = ARC_HDR_SIZE;
102         struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
103
104         BUGMSG(D_PROTO, "Preparing header for cap packet %x.\n",
105                *((int *)&pkt->soft.cap.cookie[0]));
106
107         /* Set the source hardware address.
108          *
109          * This is pretty pointless for most purposes, but it can help in
110          * debugging.  ARCnet does not allow us to change the source address in
111          * the actual packet sent)
112          */
113         pkt->hard.source = *dev->dev_addr;
114
115         /* see linux/net/ethernet/eth.c to see where I got the following */
116
117         if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
118                 /* FIXME: fill in the last byte of the dest ipaddr here to
119                  * better comply with RFC1051 in "noarp" mode.
120                  */
121                 pkt->hard.dest = 0;
122                 return hdr_size;
123         }
124         /* otherwise, just fill it in and go! */
125         pkt->hard.dest = daddr;
126
127         return hdr_size;        /* success */
128 }
129
130 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
131                       int bufnum)
132 {
133         struct arcnet_local *lp = netdev_priv(dev);
134         struct arc_hardware *hard = &pkt->hard;
135         int ofs;
136
137         /* hard header is not included in packet length */
138         length -= ARC_HDR_SIZE;
139         /* And neither is the cookie field */
140         length -= sizeof(int);
141
142         BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
143                lp->next_tx, lp->cur_tx, bufnum);
144
145         BUGMSG(D_PROTO, "Sending for cap packet %x.\n",
146                *((int *)&pkt->soft.cap.cookie[0]));
147
148         if (length > XMTU) {
149                 /* should never happen! other people already check for this. */
150                 BUGMSG(D_NORMAL, "Bug!  prepare_tx with size %d (> %d)\n",
151                        length, XMTU);
152                 length = XMTU;
153         }
154         if (length > MinTU) {
155                 hard->offset[0] = 0;
156                 hard->offset[1] = ofs = 512 - length;
157         } else if (length > MTU) {
158                 hard->offset[0] = 0;
159                 hard->offset[1] = ofs = 512 - length - 3;
160         } else {
161                 hard->offset[0] = ofs = 256 - length;
162         }
163
164         BUGMSG(D_DURING, "prepare_tx: length=%d ofs=%d\n",
165                length, ofs);
166
167         /* Copy the arcnet-header + the protocol byte down: */
168         lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
169         lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
170                             sizeof(pkt->soft.cap.proto));
171
172         /* Skip the extra integer we have written into it as a cookie
173          * but write the rest of the message:
174          */
175         lp->hw.copy_to_card(dev, bufnum, ofs + 1,
176                             ((unsigned char *)&pkt->soft.cap.mes), length - 1);
177
178         lp->lastload_dest = hard->dest;
179
180         return 1;       /* done */
181 }
182
183 static int ack_tx(struct net_device *dev, int acked)
184 {
185         struct arcnet_local *lp = netdev_priv(dev);
186         struct sk_buff *ackskb;
187         struct archdr *ackpkt;
188         int length = sizeof(struct arc_cap);
189
190         BUGMSG(D_DURING, "capmode: ack_tx: protocol: %x: result: %d\n",
191                lp->outgoing.skb->protocol, acked);
192
193         BUGLVL(D_SKB) arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
194
195         /* Now alloc a skb to send back up through the layers: */
196         ackskb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
197         if (ackskb == NULL) {
198                 BUGMSG(D_NORMAL, "Memory squeeze, can't acknowledge.\n");
199                 goto free_outskb;
200         }
201
202         skb_put(ackskb, length + ARC_HDR_SIZE);
203         ackskb->dev = dev;
204
205         skb_reset_mac_header(ackskb);
206         ackpkt = (struct archdr *)skb_mac_header(ackskb);
207         /* skb_pull(ackskb, ARC_HDR_SIZE); */
208
209         skb_copy_from_linear_data(lp->outgoing.skb, ackpkt,
210                                   ARC_HDR_SIZE + sizeof(struct arc_cap));
211         ackpkt->soft.cap.proto = 0; /* using protocol 0 for acknowledge */
212         ackpkt->soft.cap.mes.ack = acked;
213
214         BUGMSG(D_PROTO, "Ackknowledge for cap packet %x.\n",
215                *((int *)&ackpkt->soft.cap.cookie[0]));
216
217         ackskb->protocol = cpu_to_be16(ETH_P_ARCNET);
218
219         BUGLVL(D_SKB) arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
220         netif_rx(ackskb);
221
222 free_outskb:
223         dev_kfree_skb_irq(lp->outgoing.skb);
224         lp->outgoing.proto = NULL; /* We are always finished when in this protocol */
225
226         return 0;
227 }
228
229 static struct ArcProto capmode_proto = {
230         'r',
231         XMTU,
232         0,
233         rx,
234         build_header,
235         prepare_tx,
236         NULL,
237         ack_tx
238 };
239
240 static void arcnet_cap_init(void)
241 {
242         int count;
243
244         for (count = 1; count <= 8; count++)
245                 if (arc_proto_map[count] == arc_proto_default)
246                         arc_proto_map[count] = &capmode_proto;
247
248         /* for cap mode, we only set the bcast proto if there's no better one */
249         if (arc_bcast_proto == arc_proto_default)
250                 arc_bcast_proto = &capmode_proto;
251
252         arc_proto_default = &capmode_proto;
253         arc_raw_proto = &capmode_proto;
254 }
255
256 static int __init capmode_module_init(void)
257 {
258         printk(VERSION);
259         arcnet_cap_init();
260         return 0;
261 }
262
263 static void __exit capmode_module_exit(void)
264 {
265         arcnet_unregister_proto(&capmode_proto);
266 }
267 module_init(capmode_module_init);
268 module_exit(capmode_module_exit);
269
270 MODULE_LICENSE("GPL");