make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / net / isa-skeleton.c
1 /* isa-skeleton.c: A network driver outline for linux.
2  *
3  *      Written 1993-94 by Donald Becker.
4  *
5  *      Copyright 1993 United States Government as represented by the
6  *      Director, National Security Agency.
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  *      The author may be reached as becker@scyld.com, or C/O
12  *      Scyld Computing Corporation
13  *      410 Severn Ave., Suite 210
14  *      Annapolis MD 21403
15  *
16  *      This file is an outline for writing a network device driver for the
17  *      the Linux operating system.
18  *
19  *      To write (or understand) a driver, have a look at the "loopback.c" file to
20  *      get a feel of what is going on, and then use the code below as a skeleton
21  *      for the new driver.
22  *
23  */
24
25 static const char *version =
26         "isa-skeleton.c:v1.51 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
27
28 /*
29  *  Sources:
30  *      List your sources of programming information to document that
31  *      the driver is your own creation, and give due credit to others
32  *      that contributed to the work. Remember that GNU project code
33  *      cannot use proprietary or trade secret information. Interface
34  *      definitions are generally considered non-copyrightable to the
35  *      extent that the same names and structures must be used to be
36  *      compatible.
37  *
38  *      Finally, keep in mind that the Linux kernel is has an API, not
39  *      ABI. Proprietary object-code-only distributions are not permitted
40  *      under the GPL.
41  */
42
43 #include <linux/module.h>
44
45 #include <linux/kernel.h>
46 #include <linux/sched.h>
47 #include <linux/types.h>
48 #include <linux/fcntl.h>
49 #include <linux/interrupt.h>
50 #include <linux/ptrace.h>
51 #include <linux/ioport.h>
52 #include <linux/in.h>
53 #include <linux/slab.h>
54 #include <linux/string.h>
55 #include <asm/system.h>
56 #include <asm/bitops.h>
57 #include <linux/spinlock.h>
58 #include <asm/io.h>
59 #include <asm/dma.h>
60 #include <linux/errno.h>
61 #include <linux/init.h>
62
63 #include <linux/netdevice.h>
64 #include <linux/etherdevice.h>
65 #include <linux/skbuff.h>
66
67 /*
68  * The name of the card. Is used for messages and in the requests for
69  * io regions, irqs and dma channels
70  */
71 static const char* cardname = "netcard";
72
73 /* First, a few definitions that the brave might change. */
74
75 /* A zero-terminated list of I/O addresses to be probed. */
76 static unsigned int netcard_portlist[] __initdata =
77    { 0x200, 0x240, 0x280, 0x2C0, 0x300, 0x320, 0x340, 0};
78
79 /* use 0 for production, 1 for verification, >2 for debug */
80 #ifndef NET_DEBUG
81 #define NET_DEBUG 2
82 #endif
83 static unsigned int net_debug = NET_DEBUG;
84
85 /* The number of low I/O ports used by the ethercard. */
86 #define NETCARD_IO_EXTENT       32
87
88 #define MY_TX_TIMEOUT  ((400*HZ)/1000)
89
90 /* Information that need to be kept for each board. */
91 struct net_local {
92         struct net_device_stats stats;
93         long open_time;                 /* Useless example local info. */
94
95         /* Tx control lock.  This protects the transmit buffer ring
96          * state along with the "tx full" state of the driver.  This
97          * means all netif_queue flow control actions are protected
98          * by this lock as well.
99          */
100         spinlock_t lock;
101 };
102
103 /* The station (ethernet) address prefix, used for IDing the board. */
104 #define SA_ADDR0 0x00
105 #define SA_ADDR1 0x42
106 #define SA_ADDR2 0x65
107
108 /* Index to functions, as function prototypes. */
109
110 extern int netcard_probe(struct net_device *dev);
111
112 static int      netcard_probe1(struct net_device *dev, int ioaddr);
113 static int      net_open(struct net_device *dev);
114 static int      net_send_packet(struct sk_buff *skb, struct net_device *dev);
115 static void     net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
116 static void     net_rx(struct net_device *dev);
117 static int      net_close(struct net_device *dev);
118 static struct   net_device_stats *net_get_stats(struct net_device *dev);
119 static void     set_multicast_list(struct net_device *dev);
120 static void     net_tx_timeout(struct net_device *dev);
121
122
123 /* Example routines you must write ;->. */
124 #define tx_done(dev) 1
125 static void     hardware_send_packet(short ioaddr, char *buf, int length);
126 static void     chipset_init(struct net_device *dev, int startp);
127
128 /*
129  * Check for a network adaptor of this type, and return '0' iff one exists.
130  * If dev->base_addr == 0, probe all likely locations.
131  * If dev->base_addr == 1, always return failure.
132  * If dev->base_addr == 2, allocate space for the device and return success
133  * (detachable devices only).
134  */
135 int __init 
136 netcard_probe(struct net_device *dev)
137 {
138         int i;
139         int base_addr = dev->base_addr;
140
141         SET_MODULE_OWNER(dev);
142
143         if (base_addr > 0x1ff)    /* Check a single specified location. */
144                 return netcard_probe1(dev, base_addr);
145         else if (base_addr != 0)  /* Don't probe at all. */
146                 return -ENXIO;
147
148         for (i = 0; netcard_portlist[i]; i++) {
149                 int ioaddr = netcard_portlist[i];
150                 if (check_region(ioaddr, NETCARD_IO_EXTENT))
151                         continue;
152                 if (netcard_probe1(dev, ioaddr) == 0)
153                         return 0;
154         }
155
156         return -ENODEV;
157 }
158
159 /*
160  * This is the real probe routine. Linux has a history of friendly device
161  * probes on the ISA bus. A good device probes avoids doing writes, and
162  * verifies that the correct device exists and functions.
163  */
164 static int __init netcard_probe1(struct net_device *dev, int ioaddr)
165 {
166         struct net_local *np;
167         static unsigned version_printed;
168         int i;
169
170         /*
171          * For ethernet adaptors the first three octets of the station address 
172          * contains the manufacturer's unique code. That might be a good probe
173          * method. Ideally you would add additional checks.
174          */ 
175         if (inb(ioaddr + 0) != SA_ADDR0
176                 ||       inb(ioaddr + 1) != SA_ADDR1
177                 ||       inb(ioaddr + 2) != SA_ADDR2) {
178                 return -ENODEV;
179         }
180
181         if (net_debug  &&  version_printed++ == 0)
182                 printk(KERN_DEBUG "%s", version);
183
184         printk(KERN_INFO "%s: %s found at %#3x, ", dev->name, cardname, ioaddr);
185
186         /* Fill in the 'dev' fields. */
187         dev->base_addr = ioaddr;
188
189         /* Retrieve and print the ethernet address. */
190         for (i = 0; i < 6; i++)
191                 printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
192
193 #ifdef jumpered_interrupts
194         /*
195          * If this board has jumpered interrupts, allocate the interrupt
196          * vector now. There is no point in waiting since no other device
197          * can use the interrupt, and this marks the irq as busy. Jumpered
198          * interrupts are typically not reported by the boards, and we must
199          * used autoIRQ to find them.
200          */
201
202         if (dev->irq == -1)
203                 ;       /* Do nothing: a user-level program will set it. */
204         else if (dev->irq < 2) {        /* "Auto-IRQ" */
205                 autoirq_setup(0);
206                 /* Trigger an interrupt here. */
207
208                 dev->irq = autoirq_report(0);
209                 if (net_debug >= 2)
210                         printk(" autoirq is %d", dev->irq);
211         } else if (dev->irq == 2)
212                 /*
213                  * Fixup for users that don't know that IRQ 2 is really
214                  * IRQ9, or don't know which one to set.
215                  */
216                 dev->irq = 9;
217
218         {
219                 int irqval = request_irq(dev->irq, &net_interrupt, 0, cardname, dev);
220                 if (irqval) {
221                         printk("%s: unable to get IRQ %d (irqval=%d).\n",
222                                    dev->name, dev->irq, irqval);
223                         return -EAGAIN;
224                 }
225         }
226 #endif  /* jumpered interrupt */
227 #ifdef jumpered_dma
228         /*
229          * If we use a jumpered DMA channel, that should be probed for and
230          * allocated here as well. See lance.c for an example.
231          */
232         if (dev->dma == 0) {
233                 if (request_dma(dev->dma, cardname)) {
234                         printk("DMA %d allocation failed.\n", dev->dma);
235                         return -EAGAIN;
236                 } else
237                         printk(", assigned DMA %d.\n", dev->dma);
238         } else {
239                 short dma_status, new_dma_status;
240
241                 /* Read the DMA channel status registers. */
242                 dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
243                         (inb(DMA2_STAT_REG) & 0xf0);
244                 /* Trigger a DMA request, perhaps pause a bit. */
245                 outw(0x1234, ioaddr + 8);
246                 /* Re-read the DMA status registers. */
247                 new_dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
248                         (inb(DMA2_STAT_REG) & 0xf0);
249                 /*
250                  * Eliminate the old and floating requests,
251                  * and DMA4 the cascade.
252                  */
253                 new_dma_status ^= dma_status;
254                 new_dma_status &= ~0x10;
255                 for (i = 7; i > 0; i--)
256                         if (test_bit(i, &new_dma_status)) {
257                                 dev->dma = i;
258                                 break;
259                         }
260                 if (i <= 0) {
261                         printk("DMA probe failed.\n");
262                         return -EAGAIN;
263                 } 
264                 if (request_dma(dev->dma, cardname)) {
265                         printk("probed DMA %d allocation failed.\n", dev->dma);
266                         return -EAGAIN;
267                 }
268         }
269 #endif  /* jumpered DMA */
270
271         /* Initialize the device structure. */
272         if (dev->priv == NULL) {
273                 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
274                 if (dev->priv == NULL)
275                         return -ENOMEM;
276         }
277
278         memset(dev->priv, 0, sizeof(struct net_local));
279
280         np = (struct net_local *)dev->priv;
281         spin_lock_init(&np->lock);
282
283         /* Grab the region so that no one else tries to probe our ioports. */
284         request_region(ioaddr, NETCARD_IO_EXTENT, cardname);
285
286         dev->open               = net_open;
287         dev->stop               = net_close;
288         dev->hard_start_xmit    = net_send_packet;
289         dev->get_stats          = net_get_stats;
290         dev->set_multicast_list = &set_multicast_list;
291
292         dev->tx_timeout         = &net_tx_timeout;
293         dev->watchdog_timeo     = MY_TX_TIMEOUT; 
294
295         /* Fill in the fields of the device structure with ethernet values. */
296         ether_setup(dev);
297
298         return 0;
299 }
300
301 static void net_tx_timeout(struct net_device *dev)
302 {
303         struct net_local *np = (struct net_local *)dev->priv;
304
305         printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
306                tx_done(dev) ? "IRQ conflict" : "network cable problem");
307
308         /* Try to restart the adaptor. */
309         chipset_init(dev, 1);
310
311         np->stats.tx_errors++;
312
313         /* If we have space available to accept new transmit
314          * requests, wake up the queueing layer.  This would
315          * be the case if the chipset_init() call above just
316          * flushes out the tx queue and empties it.
317          *
318          * If instead, the tx queue is retained then the
319          * netif_wake_queue() call should be placed in the
320          * TX completion interrupt handler of the driver instead
321          * of here.
322          */
323         if (!tx_full(dev))
324                 netif_wake_queue(dev);
325 }
326
327 /*
328  * Open/initialize the board. This is called (in the current kernel)
329  * sometime after booting when the 'ifconfig' program is run.
330  *
331  * This routine should set everything up anew at each open, even
332  * registers that "should" only need to be set once at boot, so that
333  * there is non-reboot way to recover if something goes wrong.
334  */
335 static int
336 net_open(struct net_device *dev)
337 {
338         struct net_local *np = (struct net_local *)dev->priv;
339         int ioaddr = dev->base_addr;
340         /*
341          * This is used if the interrupt line can turned off (shared).
342          * See 3c503.c for an example of selecting the IRQ at config-time.
343          */
344         if (request_irq(dev->irq, &net_interrupt, 0, cardname, dev)) {
345                 return -EAGAIN;
346         }
347         /*
348          * Always allocate the DMA channel after the IRQ,
349          * and clean up on failure.
350          */
351         if (request_dma(dev->dma, cardname)) {
352                 free_irq(dev->irq, dev);
353                 return -EAGAIN;
354         }
355
356         /* Reset the hardware here. Don't forget to set the station address. */
357         chipset_init(dev, 1);
358         outb(0x00, ioaddr);
359         np->open_time = jiffies;
360
361         /* We are now ready to accept transmit requeusts from
362          * the queueing layer of the networking.
363          */
364         netif_start_queue(dev);
365
366         return 0;
367 }
368
369 /* This will only be invoked if your driver is _not_ in XOFF state.
370  * What this means is that you need not check it, and that this
371  * invariant will hold if you make sure that the netif_*_queue()
372  * calls are done at the proper times.
373  */
374 static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
375 {
376         struct net_local *np = (struct net_local *)dev->priv;
377         int ioaddr = dev->base_addr;
378         short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
379         unsigned char *buf = skb->data;
380
381         /* If some error occurs while trying to transmit this
382          * packet, you should return '1' from this function.
383          * In such a case you _may not_ do anything to the
384          * SKB, it is still owned by the network queueing
385          * layer when an error is returned.  This means you
386          * may not modify any SKB fields, you may not free
387          * the SKB, etc.
388          */
389
390 #if TX_RING
391         /* This is the most common case for modern hardware.
392          * The spinlock protects this code from the TX complete
393          * hardware interrupt handler.  Queue flow control is
394          * thus managed under this lock as well.
395          */
396         spin_lock_irq(&np->lock);
397
398         add_to_tx_ring(np, skb, length);
399         dev->trans_start = jiffies;
400
401         /* If we just used up the very last entry in the
402          * TX ring on this device, tell the queueing
403          * layer to send no more.
404          */
405         if (tx_full(dev))
406                 netif_stop_queue(dev);
407
408         /* When the TX completion hw interrupt arrives, this
409          * is when the transmit statistics are updated.
410          */
411
412         spin_unlock_irq(&np->lock);
413 #else
414         /* This is the case for older hardware which takes
415          * a single transmit buffer at a time, and it is
416          * just written to the device via PIO.
417          *
418          * No spin locking is needed since there is no TX complete
419          * event.  If by chance your card does have a TX complete
420          * hardware IRQ then you may need to utilize np->lock here.
421          */
422         hardware_send_packet(ioaddr, buf, length);
423         np->stats.tx_bytes += skb->len;
424
425         dev->trans_start = jiffies;
426
427         /* You might need to clean up and record Tx statistics here. */
428         if (inw(ioaddr) == /*RU*/81)
429                 np->stats.tx_aborted_errors++;
430         dev_kfree_skb (skb);
431 #endif
432
433         return 0;
434 }
435
436 #if TX_RING
437 /* This handles TX complete events posted by the device
438  * via interrupts.
439  */
440 void net_tx(struct net_device *dev)
441 {
442         struct net_local *np = (struct net_local *)dev->priv;
443         int entry;
444
445         /* This protects us from concurrent execution of
446          * our dev->hard_start_xmit function above.
447          */
448         spin_lock(&np->lock);
449
450         entry = np->tx_old;
451         while (tx_entry_is_sent(np, entry)) {
452                 struct sk_buff *skb = np->skbs[entry];
453
454                 np->stats.tx_bytes += skb->len;
455                 dev_kfree_skb_irq (skb);
456
457                 entry = next_tx_entry(np, entry);
458         }
459         np->tx_old = entry;
460
461         /* If we had stopped the queue due to a "tx full"
462          * condition, and space has now been made available,
463          * wake up the queue.
464          */
465         if (netif_queue_stopped(dev) && ! tx_full(dev))
466                 netif_wake_queue(dev);
467
468         spin_unlock(&np->lock);
469 }
470 #endif
471
472 /*
473  * The typical workload of the driver:
474  * Handle the network interface interrupts.
475  */
476 static void net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
477 {
478         struct net_device *dev = dev_id;
479         struct net_local *np;
480         int ioaddr, status;
481
482         ioaddr = dev->base_addr;
483
484         np = (struct net_local *)dev->priv;
485         status = inw(ioaddr + 0);
486
487         if (status & RX_INTR) {
488                 /* Got a packet(s). */
489                 net_rx(dev);
490         }
491 #if TX_RING
492         if (status & TX_INTR) {
493                 /* Transmit complete. */
494                 net_tx(dev);
495                 np->stats.tx_packets++;
496                 netif_wake_queue(dev);
497         }
498 #endif
499         if (status & COUNTERS_INTR) {
500                 /* Increment the appropriate 'localstats' field. */
501                 np->stats.tx_window_errors++;
502         }
503 }
504
505 /* We have a good packet(s), get it/them out of the buffers. */
506 static void
507 net_rx(struct net_device *dev)
508 {
509         struct net_local *lp = (struct net_local *)dev->priv;
510         int ioaddr = dev->base_addr;
511         int boguscount = 10;
512
513         do {
514                 int status = inw(ioaddr);
515                 int pkt_len = inw(ioaddr);
516           
517                 if (pkt_len == 0)               /* Read all the frames? */
518                         break;                  /* Done for now */
519
520                 if (status & 0x40) {    /* There was an error. */
521                         lp->stats.rx_errors++;
522                         if (status & 0x20) lp->stats.rx_frame_errors++;
523                         if (status & 0x10) lp->stats.rx_over_errors++;
524                         if (status & 0x08) lp->stats.rx_crc_errors++;
525                         if (status & 0x04) lp->stats.rx_fifo_errors++;
526                 } else {
527                         /* Malloc up new buffer. */
528                         struct sk_buff *skb;
529
530                         lp->stats.rx_bytes+=pkt_len;
531                         
532                         skb = dev_alloc_skb(pkt_len);
533                         if (skb == NULL) {
534                                 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
535                                            dev->name);
536                                 lp->stats.rx_dropped++;
537                                 break;
538                         }
539                         skb->dev = dev;
540
541                         /* 'skb->data' points to the start of sk_buff data area. */
542                         memcpy(skb_put(skb,pkt_len), (void*)dev->rmem_start,
543                                    pkt_len);
544                         /* or */
545                         insw(ioaddr, skb->data, (pkt_len + 1) >> 1);
546
547                         netif_rx(skb);
548                         dev->last_rx = jiffies;
549                         lp->stats.rx_packets++;
550                         lp->stats.rx_bytes += pkt_len;
551                 }
552         } while (--boguscount);
553
554         return;
555 }
556
557 /* The inverse routine to net_open(). */
558 static int
559 net_close(struct net_device *dev)
560 {
561         struct net_local *lp = (struct net_local *)dev->priv;
562         int ioaddr = dev->base_addr;
563
564         lp->open_time = 0;
565
566         netif_stop_queue(dev);
567
568         /* Flush the Tx and disable Rx here. */
569
570         disable_dma(dev->dma);
571
572         /* If not IRQ or DMA jumpered, free up the line. */
573         outw(0x00, ioaddr+0);   /* Release the physical interrupt line. */
574
575         free_irq(dev->irq, dev);
576         free_dma(dev->dma);
577
578         /* Update the statistics here. */
579
580         return 0;
581
582 }
583
584 /*
585  * Get the current statistics.
586  * This may be called with the card open or closed.
587  */
588 static struct net_device_stats *net_get_stats(struct net_device *dev)
589 {
590         struct net_local *lp = (struct net_local *)dev->priv;
591         short ioaddr = dev->base_addr;
592
593         /* Update the statistics from the device registers. */
594         lp->stats.rx_missed_errors = inw(ioaddr+1);
595         return &lp->stats;
596 }
597
598 /*
599  * Set or clear the multicast filter for this adaptor.
600  * num_addrs == -1      Promiscuous mode, receive all packets
601  * num_addrs == 0       Normal mode, clear multicast list
602  * num_addrs > 0        Multicast mode, receive normal and MC packets,
603  *                      and do best-effort filtering.
604  */
605 static void
606 set_multicast_list(struct net_device *dev)
607 {
608         short ioaddr = dev->base_addr;
609         if (dev->flags&IFF_PROMISC)
610         {
611                 /* Enable promiscuous mode */
612                 outw(MULTICAST|PROMISC, ioaddr);
613         }
614         else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > HW_MAX_ADDRS)
615         {
616                 /* Disable promiscuous mode, use normal mode. */
617                 hardware_set_filter(NULL);
618
619                 outw(MULTICAST, ioaddr);
620         }
621         else if(dev->mc_count)
622         {
623                 /* Walk the address list, and load the filter */
624                 hardware_set_filter(dev->mc_list);
625
626                 outw(MULTICAST, ioaddr);
627         }
628         else 
629                 outw(0, ioaddr);
630 }
631
632 #ifdef MODULE
633
634 static struct net_device this_device;
635 static int io = 0x300;
636 static int irq;
637 static int dma;
638 static int mem;
639 MODULE_LICENSE("GPL");
640
641 int init_module(void)
642 {
643         int result;
644
645         if (io == 0)
646                 printk(KERN_WARNING "%s: You shouldn't use auto-probing with insmod!\n",
647                            cardname);
648
649         /* Copy the parameters from insmod into the device structure. */
650         this_device.base_addr = io;
651         this_device.irq       = irq;
652         this_device.dma       = dma;
653         this_device.mem_start = mem;
654         this_device.init      = netcard_probe;
655
656         if ((result = register_netdev(&this_device)) != 0)
657                 return result;
658
659         return 0;
660 }
661
662 void
663 cleanup_module(void)
664 {
665         /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
666         unregister_netdev(&this_device);
667         /*
668          * If we don't do this, we can't re-insmod it later.
669          * Release irq/dma here, when you have jumpered versions and
670          * allocate them in net_probe1().
671          */
672         /*
673            free_irq(this_device.irq, dev);
674            free_dma(this_device.dma);
675         */
676         release_region(this_device.base_addr, NETCARD_IO_EXTENT);
677
678         if (this_device.priv)
679                 kfree(this_device.priv);
680 }
681
682 #endif /* MODULE */
683
684 /*
685  * Local variables:
686  *  compile-command:
687  *      gcc -D__KERNEL__ -Wall -Wstrict-prototypes -Wwrite-strings
688  *      -Wredundant-decls -O2 -m486 -c skeleton.c
689  *  version-control: t
690  *  kept-new-versions: 5
691  *  tab-width: 4
692  *  c-indent-level: 4
693  * End:
694  */