1 /* isa-skeleton.c: A network driver outline for linux.
3 * Written 1993-94 by Donald Becker.
5 * Copyright 1993 United States Government as represented by the
6 * Director, National Security Agency.
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
11 * The author may be reached as becker@scyld.com, or C/O
12 * Scyld Computing Corporation
13 * 410 Severn Ave., Suite 210
16 * This file is an outline for writing a network device driver for the
17 * the Linux operating system.
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
25 static const char *version =
26 "isa-skeleton.c:v1.51 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
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
38 * Finally, keep in mind that the Linux kernel is has an API, not
39 * ABI. Proprietary object-code-only distributions are not permitted
43 #include <linux/module.h>
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>
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>
60 #include <linux/errno.h>
61 #include <linux/init.h>
63 #include <linux/netdevice.h>
64 #include <linux/etherdevice.h>
65 #include <linux/skbuff.h>
68 * The name of the card. Is used for messages and in the requests for
69 * io regions, irqs and dma channels
71 static const char* cardname = "netcard";
73 /* First, a few definitions that the brave might change. */
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};
79 /* use 0 for production, 1 for verification, >2 for debug */
83 static unsigned int net_debug = NET_DEBUG;
85 /* The number of low I/O ports used by the ethercard. */
86 #define NETCARD_IO_EXTENT 32
88 #define MY_TX_TIMEOUT ((400*HZ)/1000)
90 /* Information that need to be kept for each board. */
92 struct net_device_stats stats;
93 long open_time; /* Useless example local info. */
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.
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
108 /* Index to functions, as function prototypes. */
110 extern int netcard_probe(struct net_device *dev);
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);
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);
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).
136 netcard_probe(struct net_device *dev)
139 int base_addr = dev->base_addr;
141 SET_MODULE_OWNER(dev);
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. */
148 for (i = 0; netcard_portlist[i]; i++) {
149 int ioaddr = netcard_portlist[i];
150 if (check_region(ioaddr, NETCARD_IO_EXTENT))
152 if (netcard_probe1(dev, ioaddr) == 0)
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.
164 static int __init netcard_probe1(struct net_device *dev, int ioaddr)
166 struct net_local *np;
167 static unsigned version_printed;
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.
175 if (inb(ioaddr + 0) != SA_ADDR0
176 || inb(ioaddr + 1) != SA_ADDR1
177 || inb(ioaddr + 2) != SA_ADDR2) {
181 if (net_debug && version_printed++ == 0)
182 printk(KERN_DEBUG "%s", version);
184 printk(KERN_INFO "%s: %s found at %#3x, ", dev->name, cardname, ioaddr);
186 /* Fill in the 'dev' fields. */
187 dev->base_addr = ioaddr;
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));
193 #ifdef jumpered_interrupts
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.
203 ; /* Do nothing: a user-level program will set it. */
204 else if (dev->irq < 2) { /* "Auto-IRQ" */
206 /* Trigger an interrupt here. */
208 dev->irq = autoirq_report(0);
210 printk(" autoirq is %d", dev->irq);
211 } else if (dev->irq == 2)
213 * Fixup for users that don't know that IRQ 2 is really
214 * IRQ9, or don't know which one to set.
219 int irqval = request_irq(dev->irq, &net_interrupt, 0, cardname, dev);
221 printk("%s: unable to get IRQ %d (irqval=%d).\n",
222 dev->name, dev->irq, irqval);
226 #endif /* jumpered interrupt */
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.
233 if (request_dma(dev->dma, cardname)) {
234 printk("DMA %d allocation failed.\n", dev->dma);
237 printk(", assigned DMA %d.\n", dev->dma);
239 short dma_status, new_dma_status;
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);
250 * Eliminate the old and floating requests,
251 * and DMA4 the cascade.
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)) {
261 printk("DMA probe failed.\n");
264 if (request_dma(dev->dma, cardname)) {
265 printk("probed DMA %d allocation failed.\n", dev->dma);
269 #endif /* jumpered DMA */
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)
278 memset(dev->priv, 0, sizeof(struct net_local));
280 np = (struct net_local *)dev->priv;
281 spin_lock_init(&np->lock);
283 /* Grab the region so that no one else tries to probe our ioports. */
284 request_region(ioaddr, NETCARD_IO_EXTENT, cardname);
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;
292 dev->tx_timeout = &net_tx_timeout;
293 dev->watchdog_timeo = MY_TX_TIMEOUT;
295 /* Fill in the fields of the device structure with ethernet values. */
301 static void net_tx_timeout(struct net_device *dev)
303 struct net_local *np = (struct net_local *)dev->priv;
305 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
306 tx_done(dev) ? "IRQ conflict" : "network cable problem");
308 /* Try to restart the adaptor. */
309 chipset_init(dev, 1);
311 np->stats.tx_errors++;
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.
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
324 netif_wake_queue(dev);
328 * Open/initialize the board. This is called (in the current kernel)
329 * sometime after booting when the 'ifconfig' program is run.
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.
336 net_open(struct net_device *dev)
338 struct net_local *np = (struct net_local *)dev->priv;
339 int ioaddr = dev->base_addr;
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.
344 if (request_irq(dev->irq, &net_interrupt, 0, cardname, dev)) {
348 * Always allocate the DMA channel after the IRQ,
349 * and clean up on failure.
351 if (request_dma(dev->dma, cardname)) {
352 free_irq(dev->irq, dev);
356 /* Reset the hardware here. Don't forget to set the station address. */
357 chipset_init(dev, 1);
359 np->open_time = jiffies;
361 /* We are now ready to accept transmit requeusts from
362 * the queueing layer of the networking.
364 netif_start_queue(dev);
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.
374 static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
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;
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
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.
396 spin_lock_irq(&np->lock);
398 add_to_tx_ring(np, skb, length);
399 dev->trans_start = jiffies;
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.
406 netif_stop_queue(dev);
408 /* When the TX completion hw interrupt arrives, this
409 * is when the transmit statistics are updated.
412 spin_unlock_irq(&np->lock);
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.
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.
422 hardware_send_packet(ioaddr, buf, length);
423 np->stats.tx_bytes += skb->len;
425 dev->trans_start = jiffies;
427 /* You might need to clean up and record Tx statistics here. */
428 if (inw(ioaddr) == /*RU*/81)
429 np->stats.tx_aborted_errors++;
437 /* This handles TX complete events posted by the device
440 void net_tx(struct net_device *dev)
442 struct net_local *np = (struct net_local *)dev->priv;
445 /* This protects us from concurrent execution of
446 * our dev->hard_start_xmit function above.
448 spin_lock(&np->lock);
451 while (tx_entry_is_sent(np, entry)) {
452 struct sk_buff *skb = np->skbs[entry];
454 np->stats.tx_bytes += skb->len;
455 dev_kfree_skb_irq (skb);
457 entry = next_tx_entry(np, entry);
461 /* If we had stopped the queue due to a "tx full"
462 * condition, and space has now been made available,
465 if (netif_queue_stopped(dev) && ! tx_full(dev))
466 netif_wake_queue(dev);
468 spin_unlock(&np->lock);
473 * The typical workload of the driver:
474 * Handle the network interface interrupts.
476 static void net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
478 struct net_device *dev = dev_id;
479 struct net_local *np;
482 ioaddr = dev->base_addr;
484 np = (struct net_local *)dev->priv;
485 status = inw(ioaddr + 0);
487 if (status & RX_INTR) {
488 /* Got a packet(s). */
492 if (status & TX_INTR) {
493 /* Transmit complete. */
495 np->stats.tx_packets++;
496 netif_wake_queue(dev);
499 if (status & COUNTERS_INTR) {
500 /* Increment the appropriate 'localstats' field. */
501 np->stats.tx_window_errors++;
505 /* We have a good packet(s), get it/them out of the buffers. */
507 net_rx(struct net_device *dev)
509 struct net_local *lp = (struct net_local *)dev->priv;
510 int ioaddr = dev->base_addr;
514 int status = inw(ioaddr);
515 int pkt_len = inw(ioaddr);
517 if (pkt_len == 0) /* Read all the frames? */
518 break; /* Done for now */
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++;
527 /* Malloc up new buffer. */
530 lp->stats.rx_bytes+=pkt_len;
532 skb = dev_alloc_skb(pkt_len);
534 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
536 lp->stats.rx_dropped++;
541 /* 'skb->data' points to the start of sk_buff data area. */
542 memcpy(skb_put(skb,pkt_len), (void*)dev->rmem_start,
545 insw(ioaddr, skb->data, (pkt_len + 1) >> 1);
548 dev->last_rx = jiffies;
549 lp->stats.rx_packets++;
550 lp->stats.rx_bytes += pkt_len;
552 } while (--boguscount);
557 /* The inverse routine to net_open(). */
559 net_close(struct net_device *dev)
561 struct net_local *lp = (struct net_local *)dev->priv;
562 int ioaddr = dev->base_addr;
566 netif_stop_queue(dev);
568 /* Flush the Tx and disable Rx here. */
570 disable_dma(dev->dma);
572 /* If not IRQ or DMA jumpered, free up the line. */
573 outw(0x00, ioaddr+0); /* Release the physical interrupt line. */
575 free_irq(dev->irq, dev);
578 /* Update the statistics here. */
585 * Get the current statistics.
586 * This may be called with the card open or closed.
588 static struct net_device_stats *net_get_stats(struct net_device *dev)
590 struct net_local *lp = (struct net_local *)dev->priv;
591 short ioaddr = dev->base_addr;
593 /* Update the statistics from the device registers. */
594 lp->stats.rx_missed_errors = inw(ioaddr+1);
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.
606 set_multicast_list(struct net_device *dev)
608 short ioaddr = dev->base_addr;
609 if (dev->flags&IFF_PROMISC)
611 /* Enable promiscuous mode */
612 outw(MULTICAST|PROMISC, ioaddr);
614 else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > HW_MAX_ADDRS)
616 /* Disable promiscuous mode, use normal mode. */
617 hardware_set_filter(NULL);
619 outw(MULTICAST, ioaddr);
621 else if(dev->mc_count)
623 /* Walk the address list, and load the filter */
624 hardware_set_filter(dev->mc_list);
626 outw(MULTICAST, ioaddr);
634 static struct net_device this_device;
635 static int io = 0x300;
639 MODULE_LICENSE("GPL");
641 int init_module(void)
646 printk(KERN_WARNING "%s: You shouldn't use auto-probing with insmod!\n",
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;
656 if ((result = register_netdev(&this_device)) != 0)
665 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
666 unregister_netdev(&this_device);
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().
673 free_irq(this_device.irq, dev);
674 free_dma(this_device.dma);
676 release_region(this_device.base_addr, NETCARD_IO_EXTENT);
678 if (this_device.priv)
679 kfree(this_device.priv);
687 * gcc -D__KERNEL__ -Wall -Wstrict-prototypes -Wwrite-strings
688 * -Wredundant-decls -O2 -m486 -c skeleton.c
690 * kept-new-versions: 5