1 /* $Id: hysdn_net.c,v 1.8.6.4 2001/09/23 22:24:54 kai Exp $
3 * Linux driver for HYSDN cards, net (ethernet type) handling routines.
5 * Author Werner Cornelius (werner@titro.de) for Hypercope GmbH
6 * Copyright 1999 by Werner Cornelius (werner@titro.de)
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
11 * This net module has been inspired by the skeleton driver from
12 * Donald Becker (becker@CESDIS.gsfc.nasa.gov)
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>
24 #include "hysdn_defs.h"
26 unsigned int hynet_enable = 0xffffffff;
27 module_param(hynet_enable, uint, 0);
29 /* store the actual version for log reporting */
30 char *hysdn_net_revision = "$Revision: 1.8.6.4 $";
32 #define MAX_SKB_BUFFERS 20 /* number of buffers for keeping TX-data */
34 /****************************************************************************/
35 /* structure containing the complete network data. The structure is aligned */
36 /* in a way that both, the device and statistics are kept inside it. */
37 /* for proper access, the device structure MUST be the first var/struct */
38 /* inside the definition. */
39 /****************************************************************************/
41 struct net_device netdev; /* the network device */
42 struct net_device_stats stats;
43 /* additional vars may be added here */
44 char dev_name[9]; /* our own device name */
46 /* Tx control lock. This protects the transmit buffer ring
47 * state along with the "tx full" state of the driver. This
48 * means all netif_queue flow control actions are protected
49 * by this lock as well.
52 struct sk_buff *skbs[MAX_SKB_BUFFERS]; /* pointers to tx-skbs */
53 int in_idx, out_idx; /* indexes to buffer ring */
54 int sk_count; /* number of buffers currently in ring */
58 /*****************************************************/
59 /* Get the current statistics for this card. */
60 /* This may be called with the card open or closed ! */
61 /*****************************************************/
62 static struct net_device_stats *
63 net_get_stats(struct net_device *dev)
65 return (&((struct net_local *) dev)->stats);
66 } /* net_device_stats */
68 /*********************************************************************/
69 /* Open/initialize the board. This is called (in the current kernel) */
70 /* sometime after booting when the 'ifconfig' program is run. */
71 /* This routine should set everything up anew at each open, even */
72 /* registers that "should" only need to be set once at boot, so that */
73 /* there is non-reboot way to recover if something goes wrong. */
74 /*********************************************************************/
76 net_open(struct net_device *dev)
78 struct in_device *in_dev;
79 hysdn_card *card = dev->priv;
82 netif_start_queue(dev); /* start tx-queueing */
84 /* Fill in the MAC-level header (if not already set) */
85 if (!card->mac_addr[0]) {
86 for (i = 0; i < ETH_ALEN - sizeof(unsigned long); i++)
87 dev->dev_addr[i] = 0xfc;
88 if ((in_dev = dev->ip_ptr) != NULL) {
89 struct in_ifaddr *ifa = in_dev->ifa_list;
91 memcpy(dev->dev_addr + (ETH_ALEN - sizeof(unsigned long)), &ifa->ifa_local, sizeof(unsigned long));
94 memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);
99 /*******************************************/
100 /* flush the currently occupied tx-buffers */
101 /* must only be called when device closed */
102 /*******************************************/
104 flush_tx_buffers(struct net_local *nl)
107 while (nl->sk_count) {
108 dev_kfree_skb(nl->skbs[nl->out_idx++]); /* free skb */
109 if (nl->out_idx >= MAX_SKB_BUFFERS)
110 nl->out_idx = 0; /* wrap around */
113 } /* flush_tx_buffers */
116 /*********************************************************************/
117 /* close/decativate the device. The device is not removed, but only */
119 /*********************************************************************/
121 net_close(struct net_device *dev)
124 netif_stop_queue(dev); /* disable queueing */
126 flush_tx_buffers((struct net_local *) dev);
128 return (0); /* success */
131 /************************************/
132 /* send a packet on this interface. */
133 /* new style for kernel >= 2.3.33 */
134 /************************************/
136 net_send_packet(struct sk_buff *skb, struct net_device *dev)
138 struct net_local *lp = (struct net_local *) dev;
140 spin_lock_irq(&lp->lock);
142 lp->skbs[lp->in_idx++] = skb; /* add to buffer list */
143 if (lp->in_idx >= MAX_SKB_BUFFERS)
144 lp->in_idx = 0; /* wrap around */
145 lp->sk_count++; /* adjust counter */
146 dev->trans_start = jiffies;
148 /* If we just used up the very last entry in the
149 * TX ring on this device, tell the queueing
150 * layer to send no more.
152 if (lp->sk_count >= MAX_SKB_BUFFERS)
153 netif_stop_queue(dev);
155 /* When the TX completion hw interrupt arrives, this
156 * is when the transmit statistics are updated.
159 spin_unlock_irq(&lp->lock);
161 if (lp->sk_count <= 3) {
162 schedule_work(&((hysdn_card *) dev->priv)->irq_queue);
164 return (0); /* success */
165 } /* net_send_packet */
169 /***********************************************************************/
170 /* acknowlegde a packet send. The network layer will be informed about */
172 /***********************************************************************/
174 hysdn_tx_netack(hysdn_card * card)
176 struct net_local *lp = card->netif;
179 return; /* non existing device */
183 return; /* error condition */
185 lp->stats.tx_packets++;
186 lp->stats.tx_bytes += lp->skbs[lp->out_idx]->len;
188 dev_kfree_skb(lp->skbs[lp->out_idx++]); /* free skb */
189 if (lp->out_idx >= MAX_SKB_BUFFERS)
190 lp->out_idx = 0; /* wrap around */
192 if (lp->sk_count-- == MAX_SKB_BUFFERS) /* dec usage count */
193 netif_start_queue((struct net_device *) lp);
194 } /* hysdn_tx_netack */
196 /*****************************************************/
197 /* we got a packet from the network, go and queue it */
198 /*****************************************************/
200 hysdn_rx_netpkt(hysdn_card * card, unsigned char *buf, unsigned short len)
202 struct net_local *lp = card->netif;
206 return; /* non existing device */
208 lp->stats.rx_bytes += len;
210 skb = dev_alloc_skb(len);
212 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
214 lp->stats.rx_dropped++;
217 skb->dev = &lp->netdev;
220 memcpy(skb_put(skb, len), buf, len);
222 /* determine the used protocol */
223 skb->protocol = eth_type_trans(skb, &lp->netdev);
226 lp->stats.rx_packets++; /* adjust packet count */
228 } /* hysdn_rx_netpkt */
230 /*****************************************************/
231 /* return the pointer to a network packet to be send */
232 /*****************************************************/
234 hysdn_tx_netget(hysdn_card * card)
236 struct net_local *lp = card->netif;
239 return (NULL); /* non existing device */
242 return (NULL); /* nothing available */
244 return (lp->skbs[lp->out_idx]); /* next packet to send */
245 } /* hysdn_tx_netget */
248 /*******************************************/
249 /* init function called by register device */
250 /*******************************************/
252 net_init(struct net_device *dev)
254 /* setup the function table */
255 dev->open = net_open;
256 dev->stop = net_close;
257 dev->hard_start_xmit = net_send_packet;
258 dev->get_stats = net_get_stats;
260 /* Fill in the fields of the device structure with ethernet values. */
263 return (0); /* success */
266 /*****************************************************************************/
267 /* hysdn_net_create creates a new net device for the given card. If a device */
268 /* already exists, it will be deleted and created a new one. The return value */
269 /* 0 announces success, else a negative error code will be returned. */
270 /*****************************************************************************/
272 hysdn_net_create(hysdn_card * card)
274 struct net_device *dev;
277 printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");
280 hysdn_net_release(card); /* release an existing net device */
281 if ((dev = kzalloc(sizeof(struct net_local), GFP_KERNEL)) == NULL) {
282 printk(KERN_WARNING "HYSDN: unable to allocate mem\n");
286 spin_lock_init(&((struct net_local *) dev)->lock);
288 /* initialise necessary or informing fields */
289 dev->base_addr = card->iobase; /* IO address */
290 dev->irq = card->irq; /* irq */
291 dev->init = net_init; /* the init function of the device */
293 strcpy(dev->name, ((struct net_local *) dev)->dev_name);
295 if ((i = register_netdev(dev))) {
296 printk(KERN_WARNING "HYSDN: unable to create network device\n");
300 dev->priv = card; /* remember pointer to own data structure */
301 card->netif = dev; /* setup the local pointer */
303 if (card->debug_flags & LOG_NET_INIT)
304 hysdn_addlog(card, "network device created");
305 return (0); /* and return success */
306 } /* hysdn_net_create */
308 /***************************************************************************/
309 /* hysdn_net_release deletes the net device for the given card. The return */
310 /* value 0 announces success, else a negative error code will be returned. */
311 /***************************************************************************/
313 hysdn_net_release(hysdn_card * card)
315 struct net_device *dev = card->netif;
318 return (0); /* non existing */
320 card->netif = NULL; /* clear out pointer */
321 dev->stop(dev); /* close the device */
323 flush_tx_buffers((struct net_local *) dev); /* empty buffers */
325 unregister_netdev(dev); /* release the device */
326 free_netdev(dev); /* release the memory allocated */
327 if (card->debug_flags & LOG_NET_INIT)
328 hysdn_addlog(card, "network device deleted");
330 return (0); /* always successful */
331 } /* hysdn_net_release */
333 /*****************************************************************************/
334 /* hysdn_net_getname returns a pointer to the name of the network interface. */
335 /* if the interface is not existing, a "-" is returned. */
336 /*****************************************************************************/
338 hysdn_net_getname(hysdn_card * card)
340 struct net_device *dev = card->netif;
343 return ("-"); /* non existing */
346 } /* hysdn_net_getname */