]> Git Repo - linux.git/blame - drivers/net/arcnet/capmode.c
arcnet: Wrap some long lines
[linux.git] / drivers / net / arcnet / capmode.c
CommitLineData
1da177e4
LT
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
05a24b23
JP
29#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
30
1da177e4 31#include <linux/module.h>
5a0e3ad6 32#include <linux/gfp.h>
1da177e4
LT
33#include <linux/init.h>
34#include <linux/if_arp.h>
35#include <net/arp.h>
36#include <linux/netdevice.h>
37#include <linux/skbuff.h>
38#include <linux/arcdevice.h>
39
1da177e4
LT
40/* packet receiver */
41static void rx(struct net_device *dev, int bufnum,
42 struct archdr *pkthdr, int length)
43{
454d7c9b 44 struct arcnet_local *lp = netdev_priv(dev);
1da177e4
LT
45 struct sk_buff *skb;
46 struct archdr *pkt = pkthdr;
47 char *pktbuf, *pkthdrbuf;
48 int ofs;
49
a34c0932
JP
50 arc_printk(D_DURING, dev, "it's a raw(cap) packet (length=%d)\n",
51 length);
1da177e4
LT
52
53 if (length >= MinTU)
54 ofs = 512 - length;
55 else
56 ofs = 256 - length;
57
58 skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC);
d8101e7b 59 if (!skb) {
5803c512 60 dev->stats.rx_dropped++;
1da177e4
LT
61 return;
62 }
63 skb_put(skb, length + ARC_HDR_SIZE + sizeof(int));
64 skb->dev = dev;
459a98ed 65 skb_reset_mac_header(skb);
98e399f8 66 pkt = (struct archdr *)skb_mac_header(skb);
1da177e4
LT
67 skb_pull(skb, ARC_HDR_SIZE);
68
f2f0a16b
JP
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 */
cb334648
JP
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));
1da177e4
LT
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
72aeea48
JP
87 if (BUGLVL(D_SKB))
88 arcnet_dump_skb(dev, skb, "rx");
1da177e4 89
09640e63 90 skb->protocol = cpu_to_be16(ETH_P_ARCNET);
1da177e4 91 netif_rx(skb);
1da177e4
LT
92}
93
f2f0a16b 94/* Create the ARCnet hard/soft headers for cap mode.
1da177e4
LT
95 * There aren't any soft headers in cap mode - not even the protocol id.
96 */
97static int build_header(struct sk_buff *skb,
98 struct net_device *dev,
99 unsigned short type,
100 uint8_t daddr)
101{
102 int hdr_size = ARC_HDR_SIZE;
cb334648 103 struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
1da177e4 104
a34c0932
JP
105 arc_printk(D_PROTO, dev, "Preparing header for cap packet %x.\n",
106 *((int *)&pkt->soft.cap.cookie[0]));
f2f0a16b
JP
107
108 /* Set the source hardware address.
1da177e4
LT
109 *
110 * This is pretty pointless for most purposes, but it can help in
111 * debugging. ARCnet does not allow us to change the source address in
112 * the actual packet sent)
113 */
114 pkt->hard.source = *dev->dev_addr;
115
116 /* see linux/net/ethernet/eth.c to see where I got the following */
117
118 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
f2f0a16b
JP
119 /* FIXME: fill in the last byte of the dest ipaddr here to
120 * better comply with RFC1051 in "noarp" mode.
1da177e4
LT
121 */
122 pkt->hard.dest = 0;
123 return hdr_size;
124 }
125 /* otherwise, just fill it in and go! */
126 pkt->hard.dest = daddr;
127
128 return hdr_size; /* success */
129}
130
1da177e4
LT
131static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
132 int bufnum)
133{
454d7c9b 134 struct arcnet_local *lp = netdev_priv(dev);
1da177e4
LT
135 struct arc_hardware *hard = &pkt->hard;
136 int ofs;
137
1da177e4
LT
138 /* hard header is not included in packet length */
139 length -= ARC_HDR_SIZE;
140 /* And neither is the cookie field */
141 length -= sizeof(int);
142
a34c0932
JP
143 arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
144 lp->next_tx, lp->cur_tx, bufnum);
1da177e4 145
a34c0932
JP
146 arc_printk(D_PROTO, dev, "Sending for cap packet %x.\n",
147 *((int *)&pkt->soft.cap.cookie[0]));
1da177e4
LT
148
149 if (length > XMTU) {
150 /* should never happen! other people already check for this. */
a34c0932
JP
151 arc_printk(D_NORMAL, dev, "Bug! prepare_tx with size %d (> %d)\n",
152 length, XMTU);
1da177e4
LT
153 length = XMTU;
154 }
155 if (length > MinTU) {
156 hard->offset[0] = 0;
157 hard->offset[1] = ofs = 512 - length;
158 } else if (length > MTU) {
159 hard->offset[0] = 0;
160 hard->offset[1] = ofs = 512 - length - 3;
7f5e760c 161 } else {
1da177e4 162 hard->offset[0] = ofs = 256 - length;
7f5e760c 163 }
1da177e4 164
a34c0932
JP
165 arc_printk(D_DURING, dev, "prepare_tx: length=%d ofs=%d\n",
166 length, ofs);
1da177e4 167
0da529a7 168 /* Copy the arcnet-header + the protocol byte down: */
1da177e4
LT
169 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
170 lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
171 sizeof(pkt->soft.cap.proto));
172
0da529a7 173 /* Skip the extra integer we have written into it as a cookie
f2f0a16b
JP
174 * but write the rest of the message:
175 */
cb334648
JP
176 lp->hw.copy_to_card(dev, bufnum, ofs + 1,
177 ((unsigned char *)&pkt->soft.cap.mes), length - 1);
1da177e4
LT
178
179 lp->lastload_dest = hard->dest;
180
0da529a7 181 return 1; /* done */
1da177e4
LT
182}
183
1da177e4
LT
184static int ack_tx(struct net_device *dev, int acked)
185{
0da529a7
DM
186 struct arcnet_local *lp = netdev_priv(dev);
187 struct sk_buff *ackskb;
188 struct archdr *ackpkt;
cb334648 189 int length = sizeof(struct arc_cap);
1da177e4 190
a34c0932
JP
191 arc_printk(D_DURING, dev, "capmode: ack_tx: protocol: %x: result: %d\n",
192 lp->outgoing.skb->protocol, acked);
1da177e4 193
72aeea48
JP
194 if (BUGLVL(D_SKB))
195 arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
1da177e4 196
0da529a7 197 /* Now alloc a skb to send back up through the layers: */
cb334648 198 ackskb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
d8101e7b 199 if (!ackskb)
0da529a7 200 goto free_outskb;
0da529a7 201
cb334648 202 skb_put(ackskb, length + ARC_HDR_SIZE);
0da529a7
DM
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); */
1da177e4 208
0da529a7
DM
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 */
cb334648 212 ackpkt->soft.cap.mes.ack = acked;
1da177e4 213
a34c0932
JP
214 arc_printk(D_PROTO, dev, "Ackknowledge for cap packet %x.\n",
215 *((int *)&ackpkt->soft.cap.cookie[0]));
1da177e4 216
0da529a7 217 ackskb->protocol = cpu_to_be16(ETH_P_ARCNET);
1da177e4 218
72aeea48
JP
219 if (BUGLVL(D_SKB))
220 arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
0da529a7 221 netif_rx(ackskb);
1da177e4 222
0da529a7
DM
223free_outskb:
224 dev_kfree_skb_irq(lp->outgoing.skb);
d6d7d3ed
JP
225 lp->outgoing.proto = NULL;
226 /* We are always finished when in this protocol */
1da177e4 227
0da529a7
DM
228 return 0;
229}
1da177e4 230
7f5e760c 231static struct ArcProto capmode_proto = {
0da529a7
DM
232 'r',
233 XMTU,
234 0,
235 rx,
236 build_header,
237 prepare_tx,
238 NULL,
239 ack_tx
240};
1da177e4 241
0da529a7
DM
242static void arcnet_cap_init(void)
243{
244 int count;
1da177e4 245
0da529a7
DM
246 for (count = 1; count <= 8; count++)
247 if (arc_proto_map[count] == arc_proto_default)
248 arc_proto_map[count] = &capmode_proto;
249
250 /* for cap mode, we only set the bcast proto if there's no better one */
251 if (arc_bcast_proto == arc_proto_default)
252 arc_bcast_proto = &capmode_proto;
253
254 arc_proto_default = &capmode_proto;
255 arc_raw_proto = &capmode_proto;
1da177e4 256}
0da529a7
DM
257
258static int __init capmode_module_init(void)
259{
05a24b23 260 pr_info("%s\n", "cap mode (`c') encapsulation support loaded");
0da529a7
DM
261 arcnet_cap_init();
262 return 0;
263}
264
265static void __exit capmode_module_exit(void)
266{
267 arcnet_unregister_proto(&capmode_proto);
268}
269module_init(capmode_module_init);
270module_exit(capmode_module_exit);
271
272MODULE_LICENSE("GPL");
This page took 1.009125 seconds and 4 git commands to generate.