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