[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / drivers / net / isa-skeleton.c
blob50bebb55e9ee4c2293fe10423d8639fe3f65b6a6
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
14 * Annapolis MD 21403
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
21 * for the new driver.
25 static const char *version =
26 "isa-skeleton.c:v1.51 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
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.
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.
43 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/types.h>
46 #include <linux/fcntl.h>
47 #include <linux/interrupt.h>
48 #include <linux/ioport.h>
49 #include <linux/in.h>
50 #include <linux/slab.h>
51 #include <linux/string.h>
52 #include <linux/spinlock.h>
53 #include <linux/errno.h>
54 #include <linux/init.h>
55 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/skbuff.h>
58 #include <linux/bitops.h>
60 #include <asm/system.h>
61 #include <asm/io.h>
62 #include <asm/dma.h>
65 * The name of the card. Is used for messages and in the requests for
66 * io regions, irqs and dma channels
68 static const char* cardname = "netcard";
70 /* First, a few definitions that the brave might change. */
72 /* A zero-terminated list of I/O addresses to be probed. */
73 static unsigned int netcard_portlist[] __initdata =
74 { 0x200, 0x240, 0x280, 0x2C0, 0x300, 0x320, 0x340, 0};
76 /* use 0 for production, 1 for verification, >2 for debug */
77 #ifndef NET_DEBUG
78 #define NET_DEBUG 2
79 #endif
80 static unsigned int net_debug = NET_DEBUG;
82 /* The number of low I/O ports used by the ethercard. */
83 #define NETCARD_IO_EXTENT 32
85 #define MY_TX_TIMEOUT ((400*HZ)/1000)
87 /* Information that need to be kept for each board. */
88 struct net_local {
89 struct net_device_stats stats;
90 long open_time; /* Useless example local info. */
92 /* Tx control lock. This protects the transmit buffer ring
93 * state along with the "tx full" state of the driver. This
94 * means all netif_queue flow control actions are protected
95 * by this lock as well.
97 spinlock_t lock;
100 /* The station (ethernet) address prefix, used for IDing the board. */
101 #define SA_ADDR0 0x00
102 #define SA_ADDR1 0x42
103 #define SA_ADDR2 0x65
105 /* Index to functions, as function prototypes. */
107 static int netcard_probe1(struct net_device *dev, int ioaddr);
108 static int net_open(struct net_device *dev);
109 static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
110 static irqreturn_t net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
111 static void net_rx(struct net_device *dev);
112 static int net_close(struct net_device *dev);
113 static struct net_device_stats *net_get_stats(struct net_device *dev);
114 static void set_multicast_list(struct net_device *dev);
115 static void net_tx_timeout(struct net_device *dev);
118 /* Example routines you must write ;->. */
119 #define tx_done(dev) 1
120 static void hardware_send_packet(short ioaddr, char *buf, int length);
121 static void chipset_init(struct net_device *dev, int startp);
124 * Check for a network adaptor of this type, and return '0' iff one exists.
125 * If dev->base_addr == 0, probe all likely locations.
126 * If dev->base_addr == 1, always return failure.
127 * If dev->base_addr == 2, allocate space for the device and return success
128 * (detachable devices only).
130 static int __init do_netcard_probe(struct net_device *dev)
132 int i;
133 int base_addr = dev->base_addr;
134 int irq = dev->irq;
136 SET_MODULE_OWNER(dev);
138 if (base_addr > 0x1ff) /* Check a single specified location. */
139 return netcard_probe1(dev, base_addr);
140 else if (base_addr != 0) /* Don't probe at all. */
141 return -ENXIO;
143 for (i = 0; netcard_portlist[i]; i++) {
144 int ioaddr = netcard_portlist[i];
145 if (netcard_probe1(dev, ioaddr) == 0)
146 return 0;
147 dev->irq = irq;
150 return -ENODEV;
153 static void cleanup_card(struct net_device *dev)
155 #ifdef jumpered_dma
156 free_dma(dev->dma);
157 #endif
158 #ifdef jumpered_interrupts
159 free_irq(dev->irq, dev);
160 #endif
161 release_region(dev->base_addr, NETCARD_IO_EXTENT);
164 #ifndef MODULE
165 struct net_device * __init netcard_probe(int unit)
167 struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
168 int err;
170 if (!dev)
171 return ERR_PTR(-ENOMEM);
173 sprintf(dev->name, "eth%d", unit);
174 netdev_boot_setup_check(dev);
176 err = do_netcard_probe(dev);
177 if (err)
178 goto out;
179 err = register_netdev(dev);
180 if (err)
181 goto out1;
182 return dev;
183 out1:
184 cleanup_card(dev);
185 out:
186 free_netdev(dev);
187 return ERR_PTR(err);
189 #endif
192 * This is the real probe routine. Linux has a history of friendly device
193 * probes on the ISA bus. A good device probes avoids doing writes, and
194 * verifies that the correct device exists and functions.
196 static int __init netcard_probe1(struct net_device *dev, int ioaddr)
198 struct net_local *np;
199 static unsigned version_printed;
200 int i;
201 int err = -ENODEV;
203 /* Grab the region so that no one else tries to probe our ioports. */
204 if (!request_region(ioaddr, NETCARD_IO_EXTENT, cardname))
205 return -EBUSY;
208 * For ethernet adaptors the first three octets of the station address
209 * contains the manufacturer's unique code. That might be a good probe
210 * method. Ideally you would add additional checks.
212 if (inb(ioaddr + 0) != SA_ADDR0
213 || inb(ioaddr + 1) != SA_ADDR1
214 || inb(ioaddr + 2) != SA_ADDR2)
215 goto out;
217 if (net_debug && version_printed++ == 0)
218 printk(KERN_DEBUG "%s", version);
220 printk(KERN_INFO "%s: %s found at %#3x, ", dev->name, cardname, ioaddr);
222 /* Fill in the 'dev' fields. */
223 dev->base_addr = ioaddr;
225 /* Retrieve and print the ethernet address. */
226 for (i = 0; i < 6; i++)
227 printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
229 err = -EAGAIN;
230 #ifdef jumpered_interrupts
232 * If this board has jumpered interrupts, allocate the interrupt
233 * vector now. There is no point in waiting since no other device
234 * can use the interrupt, and this marks the irq as busy. Jumpered
235 * interrupts are typically not reported by the boards, and we must
236 * used autoIRQ to find them.
239 if (dev->irq == -1)
240 ; /* Do nothing: a user-level program will set it. */
241 else if (dev->irq < 2) { /* "Auto-IRQ" */
242 unsigned long irq_mask = probe_irq_on();
243 /* Trigger an interrupt here. */
245 dev->irq = probe_irq_off(irq_mask);
246 if (net_debug >= 2)
247 printk(" autoirq is %d", dev->irq);
248 } else if (dev->irq == 2)
250 * Fixup for users that don't know that IRQ 2 is really
251 * IRQ9, or don't know which one to set.
253 dev->irq = 9;
256 int irqval = request_irq(dev->irq, &net_interrupt, 0, cardname, dev);
257 if (irqval) {
258 printk("%s: unable to get IRQ %d (irqval=%d).\n",
259 dev->name, dev->irq, irqval);
260 goto out;
263 #endif /* jumpered interrupt */
264 #ifdef jumpered_dma
266 * If we use a jumpered DMA channel, that should be probed for and
267 * allocated here as well. See lance.c for an example.
269 if (dev->dma == 0) {
270 if (request_dma(dev->dma, cardname)) {
271 printk("DMA %d allocation failed.\n", dev->dma);
272 goto out1;
273 } else
274 printk(", assigned DMA %d.\n", dev->dma);
275 } else {
276 short dma_status, new_dma_status;
278 /* Read the DMA channel status registers. */
279 dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
280 (inb(DMA2_STAT_REG) & 0xf0);
281 /* Trigger a DMA request, perhaps pause a bit. */
282 outw(0x1234, ioaddr + 8);
283 /* Re-read the DMA status registers. */
284 new_dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
285 (inb(DMA2_STAT_REG) & 0xf0);
287 * Eliminate the old and floating requests,
288 * and DMA4 the cascade.
290 new_dma_status ^= dma_status;
291 new_dma_status &= ~0x10;
292 for (i = 7; i > 0; i--)
293 if (test_bit(i, &new_dma_status)) {
294 dev->dma = i;
295 break;
297 if (i <= 0) {
298 printk("DMA probe failed.\n");
299 goto out1;
301 if (request_dma(dev->dma, cardname)) {
302 printk("probed DMA %d allocation failed.\n", dev->dma);
303 goto out1;
306 #endif /* jumpered DMA */
308 np = netdev_priv(dev);
309 spin_lock_init(&np->lock);
311 dev->open = net_open;
312 dev->stop = net_close;
313 dev->hard_start_xmit = net_send_packet;
314 dev->get_stats = net_get_stats;
315 dev->set_multicast_list = &set_multicast_list;
317 dev->tx_timeout = &net_tx_timeout;
318 dev->watchdog_timeo = MY_TX_TIMEOUT;
319 return 0;
320 out1:
321 #ifdef jumpered_interrupts
322 free_irq(dev->irq, dev);
323 #endif
324 out:
325 release_region(base_addr, NETCARD_IO_EXTENT);
326 return err;
329 static void net_tx_timeout(struct net_device *dev)
331 struct net_local *np = netdev_priv(dev);
333 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
334 tx_done(dev) ? "IRQ conflict" : "network cable problem");
336 /* Try to restart the adaptor. */
337 chipset_init(dev, 1);
339 np->stats.tx_errors++;
341 /* If we have space available to accept new transmit
342 * requests, wake up the queueing layer. This would
343 * be the case if the chipset_init() call above just
344 * flushes out the tx queue and empties it.
346 * If instead, the tx queue is retained then the
347 * netif_wake_queue() call should be placed in the
348 * TX completion interrupt handler of the driver instead
349 * of here.
351 if (!tx_full(dev))
352 netif_wake_queue(dev);
356 * Open/initialize the board. This is called (in the current kernel)
357 * sometime after booting when the 'ifconfig' program is run.
359 * This routine should set everything up anew at each open, even
360 * registers that "should" only need to be set once at boot, so that
361 * there is non-reboot way to recover if something goes wrong.
363 static int
364 net_open(struct net_device *dev)
366 struct net_local *np = netdev_priv(dev);
367 int ioaddr = dev->base_addr;
369 * This is used if the interrupt line can turned off (shared).
370 * See 3c503.c for an example of selecting the IRQ at config-time.
372 if (request_irq(dev->irq, &net_interrupt, 0, cardname, dev)) {
373 return -EAGAIN;
376 * Always allocate the DMA channel after the IRQ,
377 * and clean up on failure.
379 if (request_dma(dev->dma, cardname)) {
380 free_irq(dev->irq, dev);
381 return -EAGAIN;
384 /* Reset the hardware here. Don't forget to set the station address. */
385 chipset_init(dev, 1);
386 outb(0x00, ioaddr);
387 np->open_time = jiffies;
389 /* We are now ready to accept transmit requeusts from
390 * the queueing layer of the networking.
392 netif_start_queue(dev);
394 return 0;
397 /* This will only be invoked if your driver is _not_ in XOFF state.
398 * What this means is that you need not check it, and that this
399 * invariant will hold if you make sure that the netif_*_queue()
400 * calls are done at the proper times.
402 static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
404 struct net_local *np = netdev_priv(dev);
405 int ioaddr = dev->base_addr;
406 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
407 unsigned char *buf = skb->data;
409 /* If some error occurs while trying to transmit this
410 * packet, you should return '1' from this function.
411 * In such a case you _may not_ do anything to the
412 * SKB, it is still owned by the network queueing
413 * layer when an error is returned. This means you
414 * may not modify any SKB fields, you may not free
415 * the SKB, etc.
418 #if TX_RING
419 /* This is the most common case for modern hardware.
420 * The spinlock protects this code from the TX complete
421 * hardware interrupt handler. Queue flow control is
422 * thus managed under this lock as well.
424 spin_lock_irq(&np->lock);
426 add_to_tx_ring(np, skb, length);
427 dev->trans_start = jiffies;
429 /* If we just used up the very last entry in the
430 * TX ring on this device, tell the queueing
431 * layer to send no more.
433 if (tx_full(dev))
434 netif_stop_queue(dev);
436 /* When the TX completion hw interrupt arrives, this
437 * is when the transmit statistics are updated.
440 spin_unlock_irq(&np->lock);
441 #else
442 /* This is the case for older hardware which takes
443 * a single transmit buffer at a time, and it is
444 * just written to the device via PIO.
446 * No spin locking is needed since there is no TX complete
447 * event. If by chance your card does have a TX complete
448 * hardware IRQ then you may need to utilize np->lock here.
450 hardware_send_packet(ioaddr, buf, length);
451 np->stats.tx_bytes += skb->len;
453 dev->trans_start = jiffies;
455 /* You might need to clean up and record Tx statistics here. */
456 if (inw(ioaddr) == /*RU*/81)
457 np->stats.tx_aborted_errors++;
458 dev_kfree_skb (skb);
459 #endif
461 return 0;
464 #if TX_RING
465 /* This handles TX complete events posted by the device
466 * via interrupts.
468 void net_tx(struct net_device *dev)
470 struct net_local *np = netdev_priv(dev);
471 int entry;
473 /* This protects us from concurrent execution of
474 * our dev->hard_start_xmit function above.
476 spin_lock(&np->lock);
478 entry = np->tx_old;
479 while (tx_entry_is_sent(np, entry)) {
480 struct sk_buff *skb = np->skbs[entry];
482 np->stats.tx_bytes += skb->len;
483 dev_kfree_skb_irq (skb);
485 entry = next_tx_entry(np, entry);
487 np->tx_old = entry;
489 /* If we had stopped the queue due to a "tx full"
490 * condition, and space has now been made available,
491 * wake up the queue.
493 if (netif_queue_stopped(dev) && ! tx_full(dev))
494 netif_wake_queue(dev);
496 spin_unlock(&np->lock);
498 #endif
501 * The typical workload of the driver:
502 * Handle the network interface interrupts.
504 static irqreturn_t net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
506 struct net_device *dev = dev_id;
507 struct net_local *np;
508 int ioaddr, status;
509 int handled = 0;
511 ioaddr = dev->base_addr;
513 np = netdev_priv(dev);
514 status = inw(ioaddr + 0);
516 if (status == 0)
517 goto out;
518 handled = 1;
520 if (status & RX_INTR) {
521 /* Got a packet(s). */
522 net_rx(dev);
524 #if TX_RING
525 if (status & TX_INTR) {
526 /* Transmit complete. */
527 net_tx(dev);
528 np->stats.tx_packets++;
529 netif_wake_queue(dev);
531 #endif
532 if (status & COUNTERS_INTR) {
533 /* Increment the appropriate 'localstats' field. */
534 np->stats.tx_window_errors++;
536 out:
537 return IRQ_RETVAL(handled);
540 /* We have a good packet(s), get it/them out of the buffers. */
541 static void
542 net_rx(struct net_device *dev)
544 struct net_local *lp = netdev_priv(dev);
545 int ioaddr = dev->base_addr;
546 int boguscount = 10;
548 do {
549 int status = inw(ioaddr);
550 int pkt_len = inw(ioaddr);
552 if (pkt_len == 0) /* Read all the frames? */
553 break; /* Done for now */
555 if (status & 0x40) { /* There was an error. */
556 lp->stats.rx_errors++;
557 if (status & 0x20) lp->stats.rx_frame_errors++;
558 if (status & 0x10) lp->stats.rx_over_errors++;
559 if (status & 0x08) lp->stats.rx_crc_errors++;
560 if (status & 0x04) lp->stats.rx_fifo_errors++;
561 } else {
562 /* Malloc up new buffer. */
563 struct sk_buff *skb;
565 lp->stats.rx_bytes+=pkt_len;
567 skb = dev_alloc_skb(pkt_len);
568 if (skb == NULL) {
569 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
570 dev->name);
571 lp->stats.rx_dropped++;
572 break;
574 skb->dev = dev;
576 /* 'skb->data' points to the start of sk_buff data area. */
577 memcpy(skb_put(skb,pkt_len), (void*)dev->rmem_start,
578 pkt_len);
579 /* or */
580 insw(ioaddr, skb->data, (pkt_len + 1) >> 1);
582 netif_rx(skb);
583 dev->last_rx = jiffies;
584 lp->stats.rx_packets++;
585 lp->stats.rx_bytes += pkt_len;
587 } while (--boguscount);
589 return;
592 /* The inverse routine to net_open(). */
593 static int
594 net_close(struct net_device *dev)
596 struct net_local *lp = netdev_priv(dev);
597 int ioaddr = dev->base_addr;
599 lp->open_time = 0;
601 netif_stop_queue(dev);
603 /* Flush the Tx and disable Rx here. */
605 disable_dma(dev->dma);
607 /* If not IRQ or DMA jumpered, free up the line. */
608 outw(0x00, ioaddr+0); /* Release the physical interrupt line. */
610 free_irq(dev->irq, dev);
611 free_dma(dev->dma);
613 /* Update the statistics here. */
615 return 0;
620 * Get the current statistics.
621 * This may be called with the card open or closed.
623 static struct net_device_stats *net_get_stats(struct net_device *dev)
625 struct net_local *lp = netdev_priv(dev);
626 short ioaddr = dev->base_addr;
628 /* Update the statistics from the device registers. */
629 lp->stats.rx_missed_errors = inw(ioaddr+1);
630 return &lp->stats;
634 * Set or clear the multicast filter for this adaptor.
635 * num_addrs == -1 Promiscuous mode, receive all packets
636 * num_addrs == 0 Normal mode, clear multicast list
637 * num_addrs > 0 Multicast mode, receive normal and MC packets,
638 * and do best-effort filtering.
640 static void
641 set_multicast_list(struct net_device *dev)
643 short ioaddr = dev->base_addr;
644 if (dev->flags&IFF_PROMISC)
646 /* Enable promiscuous mode */
647 outw(MULTICAST|PROMISC, ioaddr);
649 else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > HW_MAX_ADDRS)
651 /* Disable promiscuous mode, use normal mode. */
652 hardware_set_filter(NULL);
654 outw(MULTICAST, ioaddr);
656 else if(dev->mc_count)
658 /* Walk the address list, and load the filter */
659 hardware_set_filter(dev->mc_list);
661 outw(MULTICAST, ioaddr);
663 else
664 outw(0, ioaddr);
667 #ifdef MODULE
669 static struct net_device *this_device;
670 static int io = 0x300;
671 static int irq;
672 static int dma;
673 static int mem;
674 MODULE_LICENSE("GPL");
676 int init_module(void)
678 struct net_device *dev;
679 int result;
681 if (io == 0)
682 printk(KERN_WARNING "%s: You shouldn't use auto-probing with insmod!\n",
683 cardname);
684 dev = alloc_etherdev(sizeof(struct net_local));
685 if (!dev)
686 return -ENOMEM;
688 /* Copy the parameters from insmod into the device structure. */
689 dev->base_addr = io;
690 dev->irq = irq;
691 dev->dma = dma;
692 dev->mem_start = mem;
693 if (do_netcard_probe(dev) == 0) {
694 if (register_netdev(dev) == 0)
695 this_device = dev;
696 return 0;
698 cleanup_card(dev);
700 free_netdev(dev);
701 return -ENXIO;
704 void
705 cleanup_module(void)
707 unregister_netdev(this_device);
708 cleanup_card(this_device);
709 free_netdev(this_device);
712 #endif /* MODULE */
715 * Local variables:
716 * compile-command:
717 * gcc -D__KERNEL__ -Wall -Wstrict-prototypes -Wwrite-strings
718 * -Wredundant-decls -O2 -m486 -c skeleton.c
719 * version-control: t
720 * kept-new-versions: 5
721 * tab-width: 4
722 * c-indent-level: 4
723 * End: