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