1 /* $Id: hysdn_net.c,v 1.1.4.1 2001/11/20 14:19:37 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 #define __NO_VERSION__
17 #include <linux/module.h>
18 #include <linux/version.h>
19 #include <linux/signal.h>
20 #include <linux/kernel.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/skbuff.h>
24 #include <linux/inetdevice.h>
26 #include "hysdn_defs.h"
28 unsigned int hynet_enable = 0xffffffff;
29 MODULE_PARM(hynet_enable, "i");
31 /* store the actual version for log reporting */
32 char *hysdn_net_revision = "$Revision: 1.1.4.1 $";
34 #define MAX_SKB_BUFFERS 20 /* number of buffers for keeping TX-data */
36 /****************************************************************************/
37 /* structure containing the complete network data. The structure is aligned */
38 /* in a way that both, the device and statistics are kept inside it. */
39 /* for proper access, the device structure MUST be the first var/struct */
40 /* inside the definition. */
41 /****************************************************************************/
43 struct net_device netdev; /* the network device */
44 struct net_device_stats stats;
45 /* additional vars may be added here */
46 char dev_name[9]; /* our own device name */
48 /* Tx control lock. This protects the transmit buffer ring
49 * state along with the "tx full" state of the driver. This
50 * means all netif_queue flow control actions are protected
51 * by this lock as well.
54 struct sk_buff *skbs[MAX_SKB_BUFFERS]; /* pointers to tx-skbs */
55 int in_idx, out_idx; /* indexes to buffer ring */
56 int sk_count; /* number of buffers currently in ring */
58 int is_open; /* flag controlling module locking */
62 /*****************************************************/
63 /* Get the current statistics for this card. */
64 /* This may be called with the card open or closed ! */
65 /*****************************************************/
66 static struct net_device_stats *
67 net_get_stats(struct net_device *dev)
69 return (&((struct net_local *) dev)->stats);
70 } /* net_device_stats */
72 /*********************************************************************/
73 /* Open/initialize the board. This is called (in the current kernel) */
74 /* sometime after booting when the 'ifconfig' program is run. */
75 /* This routine should set everything up anew at each open, even */
76 /* registers that "should" only need to be set once at boot, so that */
77 /* there is non-reboot way to recover if something goes wrong. */
78 /*********************************************************************/
80 net_open(struct net_device *dev)
82 struct in_device *in_dev;
83 hysdn_card *card = dev->priv;
86 if (!((struct net_local *) dev)->is_open)
87 MOD_INC_USE_COUNT; /* increment only if interface is actually down */
88 ((struct net_local *) dev)->is_open = 1; /* device actually open */
90 netif_start_queue(dev); /* start tx-queueing */
92 /* Fill in the MAC-level header (if not already set) */
93 if (!card->mac_addr[0]) {
94 for (i = 0; i < ETH_ALEN - sizeof(ulong); i++)
95 dev->dev_addr[i] = 0xfc;
96 if ((in_dev = dev->ip_ptr) != NULL) {
97 struct in_ifaddr *ifa = in_dev->ifa_list;
99 memcpy(dev->dev_addr + (ETH_ALEN - sizeof(ulong)), &ifa->ifa_local, sizeof(ulong));
102 memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);
107 /*******************************************/
108 /* flush the currently occupied tx-buffers */
109 /* must only be called when device closed */
110 /*******************************************/
112 flush_tx_buffers(struct net_local *nl)
115 while (nl->sk_count) {
116 dev_kfree_skb(nl->skbs[nl->out_idx++]); /* free skb */
117 if (nl->out_idx >= MAX_SKB_BUFFERS)
118 nl->out_idx = 0; /* wrap around */
121 } /* flush_tx_buffers */
124 /*********************************************************************/
125 /* close/decativate the device. The device is not removed, but only */
127 /*********************************************************************/
129 net_close(struct net_device *dev)
132 netif_stop_queue(dev); /* disable queueing */
134 if (((struct net_local *) dev)->is_open)
135 MOD_DEC_USE_COUNT; /* adjust module counter */
136 ((struct net_local *) dev)->is_open = 0;
137 flush_tx_buffers((struct net_local *) dev);
139 return (0); /* success */
142 /************************************/
143 /* send a packet on this interface. */
144 /* new style for kernel >= 2.3.33 */
145 /************************************/
147 net_send_packet(struct sk_buff *skb, struct net_device *dev)
149 struct net_local *lp = (struct net_local *) dev;
151 spin_lock_irq(&lp->lock);
153 lp->skbs[lp->in_idx++] = skb; /* add to buffer list */
154 if (lp->in_idx >= MAX_SKB_BUFFERS)
155 lp->in_idx = 0; /* wrap around */
156 lp->sk_count++; /* adjust counter */
157 dev->trans_start = jiffies;
159 /* If we just used up the very last entry in the
160 * TX ring on this device, tell the queueing
161 * layer to send no more.
163 if (lp->sk_count >= MAX_SKB_BUFFERS)
164 netif_stop_queue(dev);
166 /* When the TX completion hw interrupt arrives, this
167 * is when the transmit statistics are updated.
170 spin_unlock_irq(&lp->lock);
172 if (lp->sk_count <= 3) {
173 queue_task(&((hysdn_card *) dev->priv)->irq_queue, &tq_immediate);
174 mark_bh(IMMEDIATE_BH);
176 return (0); /* success */
177 } /* net_send_packet */
181 /***********************************************************************/
182 /* acknowlegde a packet send. The network layer will be informed about */
184 /***********************************************************************/
186 hysdn_tx_netack(hysdn_card * card)
188 struct net_local *lp = card->netif;
191 return; /* non existing device */
195 return; /* error condition */
197 lp->stats.tx_packets++;
198 lp->stats.tx_bytes += lp->skbs[lp->out_idx]->len;
200 dev_kfree_skb(lp->skbs[lp->out_idx++]); /* free skb */
201 if (lp->out_idx >= MAX_SKB_BUFFERS)
202 lp->out_idx = 0; /* wrap around */
204 if (lp->sk_count-- == MAX_SKB_BUFFERS) /* dec usage count */
205 netif_start_queue((struct net_device *) lp);
206 } /* hysdn_tx_netack */
208 /*****************************************************/
209 /* we got a packet from the network, go and queue it */
210 /*****************************************************/
212 hysdn_rx_netpkt(hysdn_card * card, uchar * buf, word len)
214 struct net_local *lp = card->netif;
218 return; /* non existing device */
220 lp->stats.rx_bytes += len;
222 skb = dev_alloc_skb(len);
224 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
226 lp->stats.rx_dropped++;
229 skb->dev = &lp->netdev;
232 memcpy(skb_put(skb, len), buf, len);
234 /* determine the used protocol */
235 skb->protocol = eth_type_trans(skb, &lp->netdev);
238 lp->stats.rx_packets++; /* adjust packet count */
240 } /* hysdn_rx_netpkt */
242 /*****************************************************/
243 /* return the pointer to a network packet to be send */
244 /*****************************************************/
246 hysdn_tx_netget(hysdn_card * card)
248 struct net_local *lp = card->netif;
251 return (NULL); /* non existing device */
254 return (NULL); /* nothing available */
256 return (lp->skbs[lp->out_idx]); /* next packet to send */
257 } /* hysdn_tx_netget */
260 /*******************************************/
261 /* init function called by register device */
262 /*******************************************/
264 net_init(struct net_device *dev)
266 /* setup the function table */
267 dev->open = net_open;
268 dev->stop = net_close;
269 dev->hard_start_xmit = net_send_packet;
270 dev->get_stats = net_get_stats;
272 /* Fill in the fields of the device structure with ethernet values. */
275 return (0); /* success */
278 /*****************************************************************************/
279 /* hysdn_net_create creates a new net device for the given card. If a device */
280 /* already exists, it will be deleted and created a new one. The return value */
281 /* 0 announces success, else a negative error code will be returned. */
282 /*****************************************************************************/
284 hysdn_net_create(hysdn_card * card)
286 struct net_device *dev;
289 printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");
292 hysdn_net_release(card); /* release an existing net device */
293 if ((dev = kmalloc(sizeof(struct net_local), GFP_KERNEL)) == NULL) {
294 printk(KERN_WARNING "HYSDN: unable to allocate mem\n");
297 memset(dev, 0, sizeof(struct net_local)); /* clean the structure */
299 spin_lock_init(&((struct net_local *) dev)->lock);
301 /* initialise necessary or informing fields */
302 dev->base_addr = card->iobase; /* IO address */
303 dev->irq = card->irq; /* irq */
304 dev->init = net_init; /* the init function of the device */
306 strcpy(dev->name, ((struct net_local *) dev)->dev_name);
308 if ((i = register_netdev(dev))) {
309 printk(KERN_WARNING "HYSDN: unable to create network device\n");
313 dev->priv = card; /* remember pointer to own data structure */
314 card->netif = dev; /* setup the local pointer */
316 if (card->debug_flags & LOG_NET_INIT)
317 hysdn_addlog(card, "network device created");
318 return (0); /* and return success */
319 } /* hysdn_net_create */
321 /***************************************************************************/
322 /* hysdn_net_release deletes the net device for the given card. The return */
323 /* value 0 announces success, else a negative error code will be returned. */
324 /***************************************************************************/
326 hysdn_net_release(hysdn_card * card)
328 struct net_device *dev = card->netif;
331 return (0); /* non existing */
333 card->netif = NULL; /* clear out pointer */
334 dev->stop(dev); /* close the device */
336 flush_tx_buffers((struct net_local *) dev); /* empty buffers */
338 unregister_netdev(dev); /* release the device */
339 kfree(dev); /* release the memory allocated */
340 if (card->debug_flags & LOG_NET_INIT)
341 hysdn_addlog(card, "network device deleted");
343 return (0); /* always successful */
344 } /* hysdn_net_release */
346 /*****************************************************************************/
347 /* hysdn_net_getname returns a pointer to the name of the network interface. */
348 /* if the interface is not existing, a "-" is returned. */
349 /*****************************************************************************/
351 hysdn_net_getname(hysdn_card * card)
353 struct net_device *dev = card->netif;
356 return ("-"); /* non existing */
359 } /* hysdn_net_getname */