1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/module.h>
17#include <linux/signal.h>
18#include <linux/kernel.h>
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
21#include <linux/skbuff.h>
22#include <linux/inetdevice.h>
23
24#include "hysdn_defs.h"
25
26unsigned int hynet_enable = 0xffffffff;
27module_param(hynet_enable, uint, 0);
28
29#define MAX_SKB_BUFFERS 20
30
31
32
33
34
35
36
37struct net_local {
38
39
40
41
42
43 struct net_device *dev;
44 spinlock_t lock;
45 struct sk_buff *skbs[MAX_SKB_BUFFERS];
46 int in_idx, out_idx;
47 int sk_count;
48};
49
50
51
52
53
54
55
56
57
58
59static int
60net_open(struct net_device *dev)
61{
62 struct in_device *in_dev;
63 hysdn_card *card = dev->ml_priv;
64 int i;
65
66 netif_start_queue(dev);
67
68
69 if (!card->mac_addr[0]) {
70 for (i = 0; i < ETH_ALEN; i++)
71 dev->dev_addr[i] = 0xfc;
72 if ((in_dev = dev->ip_ptr) != NULL) {
73 struct in_ifaddr *ifa = in_dev->ifa_list;
74 if (ifa != NULL)
75 memcpy(dev->dev_addr + (ETH_ALEN - sizeof(ifa->ifa_local)), &ifa->ifa_local, sizeof(ifa->ifa_local));
76 }
77 } else
78 memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);
79
80 return (0);
81}
82
83
84
85
86
87static void
88flush_tx_buffers(struct net_local *nl)
89{
90
91 while (nl->sk_count) {
92 dev_kfree_skb(nl->skbs[nl->out_idx++]);
93 if (nl->out_idx >= MAX_SKB_BUFFERS)
94 nl->out_idx = 0;
95 nl->sk_count--;
96 }
97}
98
99
100
101
102
103
104static int
105net_close(struct net_device *dev)
106{
107
108 netif_stop_queue(dev);
109
110 flush_tx_buffers((struct net_local *) dev);
111
112 return (0);
113}
114
115
116
117
118
119static netdev_tx_t
120net_send_packet(struct sk_buff *skb, struct net_device *dev)
121{
122 struct net_local *lp = (struct net_local *) dev;
123
124 spin_lock_irq(&lp->lock);
125
126 lp->skbs[lp->in_idx++] = skb;
127 if (lp->in_idx >= MAX_SKB_BUFFERS)
128 lp->in_idx = 0;
129 lp->sk_count++;
130 dev->trans_start = jiffies;
131
132
133
134
135
136 if (lp->sk_count >= MAX_SKB_BUFFERS)
137 netif_stop_queue(dev);
138
139
140
141
142
143 spin_unlock_irq(&lp->lock);
144
145 if (lp->sk_count <= 3) {
146 schedule_work(&((hysdn_card *) dev->ml_priv)->irq_queue);
147 }
148 return NETDEV_TX_OK;
149}
150
151
152
153
154
155
156
157void
158hysdn_tx_netack(hysdn_card *card)
159{
160 struct net_local *lp = card->netif;
161
162 if (!lp)
163 return;
164
165
166 if (!lp->sk_count)
167 return;
168
169 lp->dev->stats.tx_packets++;
170 lp->dev->stats.tx_bytes += lp->skbs[lp->out_idx]->len;
171
172 dev_kfree_skb(lp->skbs[lp->out_idx++]);
173 if (lp->out_idx >= MAX_SKB_BUFFERS)
174 lp->out_idx = 0;
175
176 if (lp->sk_count-- == MAX_SKB_BUFFERS)
177 netif_start_queue((struct net_device *) lp);
178}
179
180
181
182
183void
184hysdn_rx_netpkt(hysdn_card *card, unsigned char *buf, unsigned short len)
185{
186 struct net_local *lp = card->netif;
187 struct net_device *dev;
188 struct sk_buff *skb;
189
190 if (!lp)
191 return;
192
193 dev = lp->dev;
194 dev->stats.rx_bytes += len;
195
196 skb = dev_alloc_skb(len);
197 if (skb == NULL) {
198 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
199 dev->name);
200 dev->stats.rx_dropped++;
201 return;
202 }
203
204 memcpy(skb_put(skb, len), buf, len);
205
206
207 skb->protocol = eth_type_trans(skb, dev);
208
209 dev->stats.rx_packets++;
210
211 netif_rx(skb);
212}
213
214
215
216
217struct sk_buff *
218hysdn_tx_netget(hysdn_card *card)
219{
220 struct net_local *lp = card->netif;
221
222 if (!lp)
223 return (NULL);
224
225 if (!lp->sk_count)
226 return (NULL);
227
228 return (lp->skbs[lp->out_idx]);
229}
230
231static const struct net_device_ops hysdn_netdev_ops = {
232 .ndo_open = net_open,
233 .ndo_stop = net_close,
234 .ndo_start_xmit = net_send_packet,
235 .ndo_change_mtu = eth_change_mtu,
236 .ndo_set_mac_address = eth_mac_addr,
237 .ndo_validate_addr = eth_validate_addr,
238};
239
240
241
242
243
244
245
246int
247hysdn_net_create(hysdn_card *card)
248{
249 struct net_device *dev;
250 int i;
251 struct net_local *lp;
252
253 if (!card) {
254 printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");
255 return (-ENOMEM);
256 }
257 hysdn_net_release(card);
258
259 dev = alloc_etherdev(sizeof(struct net_local));
260 if (!dev) {
261 printk(KERN_WARNING "HYSDN: unable to allocate mem\n");
262 return (-ENOMEM);
263 }
264
265 lp = netdev_priv(dev);
266 lp->dev = dev;
267
268 dev->netdev_ops = &hysdn_netdev_ops;
269 spin_lock_init(&((struct net_local *) dev)->lock);
270
271
272 dev->base_addr = card->iobase;
273 dev->irq = card->irq;
274
275 dev->netdev_ops = &hysdn_netdev_ops;
276 if ((i = register_netdev(dev))) {
277 printk(KERN_WARNING "HYSDN: unable to create network device\n");
278 free_netdev(dev);
279 return (i);
280 }
281 dev->ml_priv = card;
282 card->netif = dev;
283
284 if (card->debug_flags & LOG_NET_INIT)
285 hysdn_addlog(card, "network device created");
286 return (0);
287}
288
289
290
291
292
293int
294hysdn_net_release(hysdn_card *card)
295{
296 struct net_device *dev = card->netif;
297
298 if (!dev)
299 return (0);
300
301 card->netif = NULL;
302 net_close(dev);
303
304 flush_tx_buffers((struct net_local *) dev);
305
306 unregister_netdev(dev);
307 free_netdev(dev);
308 if (card->debug_flags & LOG_NET_INIT)
309 hysdn_addlog(card, "network device deleted");
310
311 return (0);
312}
313
314
315
316
317
318char *
319hysdn_net_getname(hysdn_card *card)
320{
321 struct net_device *dev = card->netif;
322
323 if (!dev)
324 return ("-");
325
326 return (dev->name);
327}
328