Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / isdn / hysdn / hysdn_net.c
blobcf250e48d9240ff42ab22f3a6e7f224b9ff3e3c7
1 /* $Id: hysdn_net.c,v 1.8 2000/11/13 22:51:47 kai Exp $
3 * Linux driver for HYSDN cards, net (ethernet type) handling routines.
5 * written by Werner Cornelius (werner@titro.de) for Hypercope GmbH
7 * Copyright 1999 by Werner Cornelius (werner@titro.de)
9 * This net module has been inspired by the skeleton driver from
10 * Donald Becker (becker@CESDIS.gsfc.nasa.gov)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #define __NO_VERSION__
29 #include <linux/module.h>
30 #include <linux/version.h>
31 #include <linux/signal.h>
32 #include <linux/kernel.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/inetdevice.h>
38 #include "hysdn_defs.h"
40 /* store the actual version for log reporting */
41 char *hysdn_net_revision = "$Revision: 1.8 $";
43 #define MAX_SKB_BUFFERS 20 /* number of buffers for keeping TX-data */
45 /****************************************************************************/
46 /* structure containing the complete network data. The structure is aligned */
47 /* in a way that both, the device and statistics are kept inside it. */
48 /* for proper access, the device structure MUST be the first var/struct */
49 /* inside the definition. */
50 /****************************************************************************/
51 struct net_local {
52 struct net_device netdev; /* the network device */
53 struct net_device_stats stats;
54 /* additional vars may be added here */
55 char dev_name[9]; /* our own device name */
57 /* Tx control lock. This protects the transmit buffer ring
58 * state along with the "tx full" state of the driver. This
59 * means all netif_queue flow control actions are protected
60 * by this lock as well.
62 spinlock_t lock;
63 struct sk_buff *skbs[MAX_SKB_BUFFERS]; /* pointers to tx-skbs */
64 int in_idx, out_idx; /* indexes to buffer ring */
65 int sk_count; /* number of buffers currently in ring */
67 int is_open; /* flag controlling module locking */
68 }; /* net_local */
71 /*****************************************************/
72 /* Get the current statistics for this card. */
73 /* This may be called with the card open or closed ! */
74 /*****************************************************/
75 static struct net_device_stats *
76 net_get_stats(struct net_device *dev)
78 return (&((struct net_local *) dev)->stats);
79 } /* net_device_stats */
81 /*********************************************************************/
82 /* Open/initialize the board. This is called (in the current kernel) */
83 /* sometime after booting when the 'ifconfig' program is run. */
84 /* This routine should set everything up anew at each open, even */
85 /* registers that "should" only need to be set once at boot, so that */
86 /* there is non-reboot way to recover if something goes wrong. */
87 /*********************************************************************/
88 static int
89 net_open(struct net_device *dev)
91 struct in_device *in_dev;
92 hysdn_card *card = dev->priv;
93 int i;
95 if (!((struct net_local *) dev)->is_open)
96 MOD_INC_USE_COUNT; /* increment only if interface is actually down */
97 ((struct net_local *) dev)->is_open = 1; /* device actually open */
99 netif_start_queue(dev); /* start tx-queueing */
101 /* Fill in the MAC-level header (if not already set) */
102 if (!card->mac_addr[0]) {
103 for (i = 0; i < ETH_ALEN - sizeof(ulong); i++)
104 dev->dev_addr[i] = 0xfc;
105 if ((in_dev = dev->ip_ptr) != NULL) {
106 struct in_ifaddr *ifa = in_dev->ifa_list;
107 if (ifa != NULL)
108 memcpy(dev->dev_addr + (ETH_ALEN - sizeof(ulong)), &ifa->ifa_local, sizeof(ulong));
110 } else
111 memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);
113 return (0);
114 } /* net_open */
116 /*******************************************/
117 /* flush the currently occupied tx-buffers */
118 /* must only be called when device closed */
119 /*******************************************/
120 static void
121 flush_tx_buffers(struct net_local *nl)
124 while (nl->sk_count) {
125 dev_kfree_skb(nl->skbs[nl->out_idx++]); /* free skb */
126 if (nl->out_idx >= MAX_SKB_BUFFERS)
127 nl->out_idx = 0; /* wrap around */
128 nl->sk_count--;
130 } /* flush_tx_buffers */
133 /*********************************************************************/
134 /* close/decativate the device. The device is not removed, but only */
135 /* deactivated. */
136 /*********************************************************************/
137 static int
138 net_close(struct net_device *dev)
141 netif_stop_queue(dev); /* disable queueing */
143 if (((struct net_local *) dev)->is_open)
144 MOD_DEC_USE_COUNT; /* adjust module counter */
145 ((struct net_local *) dev)->is_open = 0;
146 flush_tx_buffers((struct net_local *) dev);
148 return (0); /* success */
149 } /* net_close */
151 /************************************/
152 /* send a packet on this interface. */
153 /* new style for kernel >= 2.3.33 */
154 /************************************/
155 static int
156 net_send_packet(struct sk_buff *skb, struct net_device *dev)
158 struct net_local *lp = (struct net_local *) dev;
160 spin_lock_irq(&lp->lock);
162 lp->skbs[lp->in_idx++] = skb; /* add to buffer list */
163 if (lp->in_idx >= MAX_SKB_BUFFERS)
164 lp->in_idx = 0; /* wrap around */
165 lp->sk_count++; /* adjust counter */
166 dev->trans_start = jiffies;
168 /* If we just used up the very last entry in the
169 * TX ring on this device, tell the queueing
170 * layer to send no more.
172 if (lp->sk_count >= MAX_SKB_BUFFERS)
173 netif_stop_queue(dev);
175 /* When the TX completion hw interrupt arrives, this
176 * is when the transmit statistics are updated.
179 spin_unlock_irq(&lp->lock);
181 if (lp->sk_count <= 3) {
182 queue_task(&((hysdn_card *) dev->priv)->irq_queue, &tq_immediate);
183 mark_bh(IMMEDIATE_BH);
185 return (0); /* success */
186 } /* net_send_packet */
190 /***********************************************************************/
191 /* acknowlegde a packet send. The network layer will be informed about */
192 /* completion */
193 /***********************************************************************/
194 void
195 hysdn_tx_netack(hysdn_card * card)
197 struct net_local *lp = card->netif;
199 if (!lp)
200 return; /* non existing device */
203 if (!lp->sk_count)
204 return; /* error condition */
206 lp->stats.tx_packets++;
207 lp->stats.tx_bytes += lp->skbs[lp->out_idx]->len;
209 dev_kfree_skb(lp->skbs[lp->out_idx++]); /* free skb */
210 if (lp->out_idx >= MAX_SKB_BUFFERS)
211 lp->out_idx = 0; /* wrap around */
213 if (lp->sk_count-- == MAX_SKB_BUFFERS) /* dec usage count */
214 netif_start_queue((struct net_device *) lp);
215 } /* hysdn_tx_netack */
217 /*****************************************************/
218 /* we got a packet from the network, go and queue it */
219 /*****************************************************/
220 void
221 hysdn_rx_netpkt(hysdn_card * card, uchar * buf, word len)
223 struct net_local *lp = card->netif;
224 struct sk_buff *skb;
226 if (!lp)
227 return; /* non existing device */
229 lp->stats.rx_bytes += len;
231 skb = dev_alloc_skb(len);
232 if (skb == NULL) {
233 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
234 lp->netdev.name);
235 lp->stats.rx_dropped++;
236 return;
238 skb->dev = &lp->netdev;
240 /* copy the data */
241 memcpy(skb_put(skb, len), buf, len);
243 /* determine the used protocol */
244 skb->protocol = eth_type_trans(skb, &lp->netdev);
246 netif_rx(skb);
247 lp->stats.rx_packets++; /* adjust packet count */
249 } /* hysdn_rx_netpkt */
251 /*****************************************************/
252 /* return the pointer to a network packet to be send */
253 /*****************************************************/
254 struct sk_buff *
255 hysdn_tx_netget(hysdn_card * card)
257 struct net_local *lp = card->netif;
259 if (!lp)
260 return (NULL); /* non existing device */
262 if (!lp->sk_count)
263 return (NULL); /* nothing available */
265 return (lp->skbs[lp->out_idx]); /* next packet to send */
266 } /* hysdn_tx_netget */
269 /*******************************************/
270 /* init function called by register device */
271 /*******************************************/
272 static int
273 net_init(struct net_device *dev)
275 /* setup the function table */
276 dev->open = net_open;
277 dev->stop = net_close;
278 dev->hard_start_xmit = net_send_packet;
279 dev->get_stats = net_get_stats;
281 /* Fill in the fields of the device structure with ethernet values. */
282 ether_setup(dev);
284 return (0); /* success */
285 } /* net_init */
287 /*****************************************************************************/
288 /* hysdn_net_create creates a new net device for the given card. If a device */
289 /* already exists, it will be deleted and created a new one. The return value */
290 /* 0 announces success, else a negative error code will be returned. */
291 /*****************************************************************************/
293 hysdn_net_create(hysdn_card * card)
295 struct net_device *dev;
296 int i;
297 if(!card) {
298 printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");
299 return (-ENOMEM);
301 hysdn_net_release(card); /* release an existing net device */
302 if ((dev = kmalloc(sizeof(struct net_local), GFP_KERNEL)) == NULL) {
303 printk(KERN_WARNING "HYSDN: unable to allocate mem\n");
304 if (card->debug_flags & LOG_NET_INIT)
305 return (-ENOMEM);
307 memset(dev, 0, sizeof(struct net_local)); /* clean the structure */
309 spin_lock_init(&((struct net_local *) dev)->lock);
311 /* initialise necessary or informing fields */
312 dev->base_addr = card->iobase; /* IO address */
313 dev->irq = card->irq; /* irq */
314 dev->init = net_init; /* the init function of the device */
315 if(dev->name) {
316 strcpy(dev->name, ((struct net_local *) dev)->dev_name);
318 if ((i = register_netdev(dev))) {
319 printk(KERN_WARNING "HYSDN: unable to create network device\n");
320 kfree(dev);
321 return (i);
323 dev->priv = card; /* remember pointer to own data structure */
324 card->netif = dev; /* setup the local pointer */
326 if (card->debug_flags & LOG_NET_INIT)
327 hysdn_addlog(card, "network device created");
328 return (0); /* and return success */
329 } /* hysdn_net_create */
331 /***************************************************************************/
332 /* hysdn_net_release deletes the net device for the given card. The return */
333 /* value 0 announces success, else a negative error code will be returned. */
334 /***************************************************************************/
336 hysdn_net_release(hysdn_card * card)
338 struct net_device *dev = card->netif;
340 if (!dev)
341 return (0); /* non existing */
343 card->netif = NULL; /* clear out pointer */
344 dev->stop(dev); /* close the device */
346 flush_tx_buffers((struct net_local *) dev); /* empty buffers */
348 unregister_netdev(dev); /* release the device */
349 kfree(dev); /* release the memory allocated */
350 if (card->debug_flags & LOG_NET_INIT)
351 hysdn_addlog(card, "network device deleted");
353 return (0); /* always successfull */
354 } /* hysdn_net_release */
356 /*****************************************************************************/
357 /* hysdn_net_getname returns a pointer to the name of the network interface. */
358 /* if the interface is not existing, a "-" is returned. */
359 /*****************************************************************************/
360 char *
361 hysdn_net_getname(hysdn_card * card)
363 struct net_device *dev = card->netif;
365 if (!dev)
366 return ("-"); /* non existing */
368 return (dev->name);
369 } /* hysdn_net_getname */