ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / isdn / hysdn / hysdn_net.c
1 /* $Id: hysdn_net.c,v 1.8.6.4 2001/09/23 22:24:54 kai Exp $
2  *
3  * Linux driver for HYSDN cards, net (ethernet type) handling routines.
4  *
5  * Author    Werner Cornelius (werner@titro.de) for Hypercope GmbH
6  * Copyright 1999 by Werner Cornelius (werner@titro.de)
7  *
8  * This software may be used and distributed according to the terms
9  * of the GNU General Public License, incorporated herein by reference.
10  *
11  * This net module has been inspired by the skeleton driver from
12  * Donald Becker (becker@CESDIS.gsfc.nasa.gov)
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/version.h>
18 #include <linux/signal.h>
19 #include <linux/kernel.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/inetdevice.h>
24
25 #include "hysdn_defs.h"
26
27 unsigned int hynet_enable = 0xffffffff; 
28 MODULE_PARM(hynet_enable, "i");
29
30 /* store the actual version for log reporting */
31 char *hysdn_net_revision = "$Revision: 1.8.6.4 $";
32
33 #define MAX_SKB_BUFFERS 20      /* number of buffers for keeping TX-data */
34
35 /****************************************************************************/
36 /* structure containing the complete network data. The structure is aligned */
37 /* in a way that both, the device and statistics are kept inside it.        */
38 /* for proper access, the device structure MUST be the first var/struct     */
39 /* inside the definition.                                                   */
40 /****************************************************************************/
41 struct net_local {
42         struct net_device netdev;       /* the network device */
43         struct net_device_stats stats;
44         /* additional vars may be added here */
45         char dev_name[9];       /* our own device name */
46
47         /* Tx control lock.  This protects the transmit buffer ring
48          * state along with the "tx full" state of the driver.  This
49          * means all netif_queue flow control actions are protected
50          * by this lock as well.
51          */
52         spinlock_t lock;
53         struct sk_buff *skbs[MAX_SKB_BUFFERS];  /* pointers to tx-skbs */
54         int in_idx, out_idx;    /* indexes to buffer ring */
55         int sk_count;           /* number of buffers currently in ring */
56 };                              /* net_local */
57
58
59 /*****************************************************/
60 /* Get the current statistics for this card.         */
61 /* This may be called with the card open or closed ! */
62 /*****************************************************/
63 static struct net_device_stats *
64 net_get_stats(struct net_device *dev)
65 {
66         return (&((struct net_local *) dev)->stats);
67 }                               /* net_device_stats */
68
69 /*********************************************************************/
70 /* Open/initialize the board. This is called (in the current kernel) */
71 /* sometime after booting when the 'ifconfig' program is run.        */
72 /* This routine should set everything up anew at each open, even     */
73 /* registers that "should" only need to be set once at boot, so that */
74 /* there is non-reboot way to recover if something goes wrong.       */
75 /*********************************************************************/
76 static int
77 net_open(struct net_device *dev)
78 {
79         struct in_device *in_dev;
80         hysdn_card *card = dev->priv;
81         int i;
82
83         netif_start_queue(dev); /* start tx-queueing */
84
85         /* Fill in the MAC-level header (if not already set) */
86         if (!card->mac_addr[0]) {
87                 for (i = 0; i < ETH_ALEN - sizeof(ulong); i++)
88                         dev->dev_addr[i] = 0xfc;
89                 if ((in_dev = dev->ip_ptr) != NULL) {
90                         struct in_ifaddr *ifa = in_dev->ifa_list;
91                         if (ifa != NULL)
92                                 memcpy(dev->dev_addr + (ETH_ALEN - sizeof(ulong)), &ifa->ifa_local, sizeof(ulong));
93                 }
94         } else
95                 memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);
96
97         return (0);
98 }                               /* net_open */
99
100 /*******************************************/
101 /* flush the currently occupied tx-buffers */
102 /* must only be called when device closed  */
103 /*******************************************/
104 static void
105 flush_tx_buffers(struct net_local *nl)
106 {
107
108         while (nl->sk_count) {
109                 dev_kfree_skb(nl->skbs[nl->out_idx++]);         /* free skb */
110                 if (nl->out_idx >= MAX_SKB_BUFFERS)
111                         nl->out_idx = 0;        /* wrap around */
112                 nl->sk_count--;
113         }
114 }                               /* flush_tx_buffers */
115
116
117 /*********************************************************************/
118 /* close/decativate the device. The device is not removed, but only  */
119 /* deactivated.                                                      */
120 /*********************************************************************/
121 static int
122 net_close(struct net_device *dev)
123 {
124
125         netif_stop_queue(dev);  /* disable queueing */
126
127         flush_tx_buffers((struct net_local *) dev);
128
129         return (0);             /* success */
130 }                               /* net_close */
131
132 /************************************/
133 /* send a packet on this interface. */
134 /* new style for kernel >= 2.3.33   */
135 /************************************/
136 static int
137 net_send_packet(struct sk_buff *skb, struct net_device *dev)
138 {
139         struct net_local *lp = (struct net_local *) dev;
140
141         spin_lock_irq(&lp->lock);
142
143         lp->skbs[lp->in_idx++] = skb;   /* add to buffer list */
144         if (lp->in_idx >= MAX_SKB_BUFFERS)
145                 lp->in_idx = 0; /* wrap around */
146         lp->sk_count++;         /* adjust counter */
147         dev->trans_start = jiffies;
148
149         /* If we just used up the very last entry in the
150          * TX ring on this device, tell the queueing
151          * layer to send no more.
152          */
153         if (lp->sk_count >= MAX_SKB_BUFFERS)
154                 netif_stop_queue(dev);
155
156         /* When the TX completion hw interrupt arrives, this
157          * is when the transmit statistics are updated.
158          */
159
160         spin_unlock_irq(&lp->lock);
161
162         if (lp->sk_count <= 3) {
163                 schedule_work(&((hysdn_card *) dev->priv)->irq_queue);
164         }
165         return (0);             /* success */
166 }                               /* net_send_packet */
167
168
169
170 /***********************************************************************/
171 /* acknowlegde a packet send. The network layer will be informed about */
172 /* completion                                                          */
173 /***********************************************************************/
174 void
175 hysdn_tx_netack(hysdn_card * card)
176 {
177         struct net_local *lp = card->netif;
178
179         if (!lp)
180                 return;         /* non existing device */
181
182
183         if (!lp->sk_count)
184                 return;         /* error condition */
185
186         lp->stats.tx_packets++;
187         lp->stats.tx_bytes += lp->skbs[lp->out_idx]->len;
188
189         dev_kfree_skb(lp->skbs[lp->out_idx++]);         /* free skb */
190         if (lp->out_idx >= MAX_SKB_BUFFERS)
191                 lp->out_idx = 0;        /* wrap around */
192
193         if (lp->sk_count-- == MAX_SKB_BUFFERS)  /* dec usage count */
194                 netif_start_queue((struct net_device *) lp);
195 }                               /* hysdn_tx_netack */
196
197 /*****************************************************/
198 /* we got a packet from the network, go and queue it */
199 /*****************************************************/
200 void
201 hysdn_rx_netpkt(hysdn_card * card, uchar * buf, word len)
202 {
203         struct net_local *lp = card->netif;
204         struct sk_buff *skb;
205
206         if (!lp)
207                 return;         /* non existing device */
208
209         lp->stats.rx_bytes += len;
210
211         skb = dev_alloc_skb(len);
212         if (skb == NULL) {
213                 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
214                        lp->netdev.name);
215                 lp->stats.rx_dropped++;
216                 return;
217         }
218         skb->dev = &lp->netdev;
219
220         /* copy the data */
221         memcpy(skb_put(skb, len), buf, len);
222
223         /* determine the used protocol */
224         skb->protocol = eth_type_trans(skb, &lp->netdev);
225
226         netif_rx(skb);
227         lp->stats.rx_packets++; /* adjust packet count */
228
229 }                               /* hysdn_rx_netpkt */
230
231 /*****************************************************/
232 /* return the pointer to a network packet to be send */
233 /*****************************************************/
234 struct sk_buff *
235 hysdn_tx_netget(hysdn_card * card)
236 {
237         struct net_local *lp = card->netif;
238
239         if (!lp)
240                 return (NULL);  /* non existing device */
241
242         if (!lp->sk_count)
243                 return (NULL);  /* nothing available */
244
245         return (lp->skbs[lp->out_idx]);         /* next packet to send */
246 }                               /* hysdn_tx_netget */
247
248
249 /*******************************************/
250 /* init function called by register device */
251 /*******************************************/
252 static int
253 net_init(struct net_device *dev)
254 {
255         /* setup the function table */
256         dev->open = net_open;
257         dev->stop = net_close;
258         dev->hard_start_xmit = net_send_packet;
259         dev->get_stats = net_get_stats;
260
261         /* Fill in the fields of the device structure with ethernet values. */
262         ether_setup(dev);
263
264         return (0);             /* success */
265 }                               /* net_init */
266
267 /*****************************************************************************/
268 /* hysdn_net_create creates a new net device for the given card. If a device */
269 /* already exists, it will be deleted and created a new one. The return value */
270 /* 0 announces success, else a negative error code will be returned.         */
271 /*****************************************************************************/
272 int
273 hysdn_net_create(hysdn_card * card)
274 {
275         struct net_device *dev;
276         int i;
277         if(!card) {
278                 printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");
279                 return (-ENOMEM);
280         }
281         hysdn_net_release(card);        /* release an existing net device */
282         if ((dev = kmalloc(sizeof(struct net_local), GFP_KERNEL)) == NULL) {
283                 printk(KERN_WARNING "HYSDN: unable to allocate mem\n");
284                 return (-ENOMEM);
285         }
286         memset(dev, 0, sizeof(struct net_local));       /* clean the structure */
287
288         spin_lock_init(&((struct net_local *) dev)->lock);
289
290         /* initialise necessary or informing fields */
291         dev->base_addr = card->iobase;  /* IO address */
292         dev->irq = card->irq;   /* irq */
293         dev->init = net_init;   /* the init function of the device */
294         if(dev->name) {
295                 strcpy(dev->name, ((struct net_local *) dev)->dev_name);
296         } 
297         if ((i = register_netdev(dev))) {
298                 printk(KERN_WARNING "HYSDN: unable to create network device\n");
299                 kfree(dev);
300                 return (i);
301         }
302         dev->priv = card;       /* remember pointer to own data structure */
303         card->netif = dev;      /* setup the local pointer */
304
305         if (card->debug_flags & LOG_NET_INIT)
306                 hysdn_addlog(card, "network device created");
307         return (0);             /* and return success */
308 }                               /* hysdn_net_create */
309
310 /***************************************************************************/
311 /* hysdn_net_release deletes the net device for the given card. The return */
312 /* value 0 announces success, else a negative error code will be returned. */
313 /***************************************************************************/
314 int
315 hysdn_net_release(hysdn_card * card)
316 {
317         struct net_device *dev = card->netif;
318
319         if (!dev)
320                 return (0);     /* non existing */
321
322         card->netif = NULL;     /* clear out pointer */
323         dev->stop(dev);         /* close the device */
324
325         flush_tx_buffers((struct net_local *) dev);     /* empty buffers */
326
327         unregister_netdev(dev); /* release the device */
328         free_netdev(dev);       /* release the memory allocated */
329         if (card->debug_flags & LOG_NET_INIT)
330                 hysdn_addlog(card, "network device deleted");
331
332         return (0);             /* always successful */
333 }                               /* hysdn_net_release */
334
335 /*****************************************************************************/
336 /* hysdn_net_getname returns a pointer to the name of the network interface. */
337 /* if the interface is not existing, a "-" is returned.                      */
338 /*****************************************************************************/
339 char *
340 hysdn_net_getname(hysdn_card * card)
341 {
342         struct net_device *dev = card->netif;
343
344         if (!dev)
345                 return ("-");   /* non existing */
346
347         return (dev->name);
348 }                               /* hysdn_net_getname */