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>
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>
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>
59 #include <asm/system.h>
60 #include <asm/bitops.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 */
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. */
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.
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 extern int netcard_probe(struct net_device
*dev
);
109 static int netcard_probe1(struct net_device
*dev
, int ioaddr
);
110 static int net_open(struct net_device
*dev
);
111 static int net_send_packet(struct sk_buff
*skb
, struct net_device
*dev
);
112 static irqreturn_t
net_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
);
113 static void net_rx(struct net_device
*dev
);
114 static int net_close(struct net_device
*dev
);
115 static struct net_device_stats
*net_get_stats(struct net_device
*dev
);
116 static void set_multicast_list(struct net_device
*dev
);
117 static void net_tx_timeout(struct net_device
*dev
);
120 /* Example routines you must write ;->. */
121 #define tx_done(dev) 1
122 static void hardware_send_packet(short ioaddr
, char *buf
, int length
);
123 static void chipset_init(struct net_device
*dev
, int startp
);
126 * Check for a network adaptor of this type, and return '0' iff one exists.
127 * If dev->base_addr == 0, probe all likely locations.
128 * If dev->base_addr == 1, always return failure.
129 * If dev->base_addr == 2, allocate space for the device and return success
130 * (detachable devices only).
133 netcard_probe(struct net_device
*dev
)
136 int base_addr
= dev
->base_addr
;
138 SET_MODULE_OWNER(dev
);
140 if (base_addr
> 0x1ff) /* Check a single specified location. */
141 return netcard_probe1(dev
, base_addr
);
142 else if (base_addr
!= 0) /* Don't probe at all. */
145 for (i
= 0; netcard_portlist
[i
]; i
++) {
146 int ioaddr
= netcard_portlist
[i
];
147 if (check_region(ioaddr
, NETCARD_IO_EXTENT
))
149 if (netcard_probe1(dev
, ioaddr
) == 0)
157 * This is the real probe routine. Linux has a history of friendly device
158 * probes on the ISA bus. A good device probes avoids doing writes, and
159 * verifies that the correct device exists and functions.
161 static int __init
netcard_probe1(struct net_device
*dev
, int ioaddr
)
163 struct net_local
*np
;
164 static unsigned version_printed
;
168 * For ethernet adaptors the first three octets of the station address
169 * contains the manufacturer's unique code. That might be a good probe
170 * method. Ideally you would add additional checks.
172 if (inb(ioaddr
+ 0) != SA_ADDR0
173 || inb(ioaddr
+ 1) != SA_ADDR1
174 || inb(ioaddr
+ 2) != SA_ADDR2
) {
178 if (net_debug
&& version_printed
++ == 0)
179 printk(KERN_DEBUG
"%s", version
);
181 printk(KERN_INFO
"%s: %s found at %#3x, ", dev
->name
, cardname
, ioaddr
);
183 /* Fill in the 'dev' fields. */
184 dev
->base_addr
= ioaddr
;
186 /* Retrieve and print the ethernet address. */
187 for (i
= 0; i
< 6; i
++)
188 printk(" %2.2x", dev
->dev_addr
[i
] = inb(ioaddr
+ i
));
190 #ifdef jumpered_interrupts
192 * If this board has jumpered interrupts, allocate the interrupt
193 * vector now. There is no point in waiting since no other device
194 * can use the interrupt, and this marks the irq as busy. Jumpered
195 * interrupts are typically not reported by the boards, and we must
196 * used autoIRQ to find them.
200 ; /* Do nothing: a user-level program will set it. */
201 else if (dev
->irq
< 2) { /* "Auto-IRQ" */
202 unsigned long irq_mask
= probe_irq_on();
203 /* Trigger an interrupt here. */
205 dev
->irq
= probe_irq_off(irq_mask
);
207 printk(" autoirq is %d", dev
->irq
);
208 } else if (dev
->irq
== 2)
210 * Fixup for users that don't know that IRQ 2 is really
211 * IRQ9, or don't know which one to set.
216 int irqval
= request_irq(dev
->irq
, &net_interrupt
, 0, cardname
, dev
);
218 printk("%s: unable to get IRQ %d (irqval=%d).\n",
219 dev
->name
, dev
->irq
, irqval
);
223 #endif /* jumpered interrupt */
226 * If we use a jumpered DMA channel, that should be probed for and
227 * allocated here as well. See lance.c for an example.
230 if (request_dma(dev
->dma
, cardname
)) {
231 printk("DMA %d allocation failed.\n", dev
->dma
);
234 printk(", assigned DMA %d.\n", dev
->dma
);
236 short dma_status
, new_dma_status
;
238 /* Read the DMA channel status registers. */
239 dma_status
= ((inb(DMA1_STAT_REG
) >> 4) & 0x0f) |
240 (inb(DMA2_STAT_REG
) & 0xf0);
241 /* Trigger a DMA request, perhaps pause a bit. */
242 outw(0x1234, ioaddr
+ 8);
243 /* Re-read the DMA status registers. */
244 new_dma_status
= ((inb(DMA1_STAT_REG
) >> 4) & 0x0f) |
245 (inb(DMA2_STAT_REG
) & 0xf0);
247 * Eliminate the old and floating requests,
248 * and DMA4 the cascade.
250 new_dma_status
^= dma_status
;
251 new_dma_status
&= ~0x10;
252 for (i
= 7; i
> 0; i
--)
253 if (test_bit(i
, &new_dma_status
)) {
258 printk("DMA probe failed.\n");
261 if (request_dma(dev
->dma
, cardname
)) {
262 printk("probed DMA %d allocation failed.\n", dev
->dma
);
266 #endif /* jumpered DMA */
268 /* Initialize the device structure. */
269 if (dev
->priv
== NULL
) {
270 dev
->priv
= kmalloc(sizeof(struct net_local
), GFP_KERNEL
);
271 if (dev
->priv
== NULL
)
275 memset(dev
->priv
, 0, sizeof(struct net_local
));
277 np
= (struct net_local
*)dev
->priv
;
278 spin_lock_init(&np
->lock
);
280 /* Grab the region so that no one else tries to probe our ioports. */
281 request_region(ioaddr
, NETCARD_IO_EXTENT
, cardname
);
283 dev
->open
= net_open
;
284 dev
->stop
= net_close
;
285 dev
->hard_start_xmit
= net_send_packet
;
286 dev
->get_stats
= net_get_stats
;
287 dev
->set_multicast_list
= &set_multicast_list
;
289 dev
->tx_timeout
= &net_tx_timeout
;
290 dev
->watchdog_timeo
= MY_TX_TIMEOUT
;
292 /* Fill in the fields of the device structure with ethernet values. */
298 static void net_tx_timeout(struct net_device
*dev
)
300 struct net_local
*np
= (struct net_local
*)dev
->priv
;
302 printk(KERN_WARNING
"%s: transmit timed out, %s?\n", dev
->name
,
303 tx_done(dev
) ? "IRQ conflict" : "network cable problem");
305 /* Try to restart the adaptor. */
306 chipset_init(dev
, 1);
308 np
->stats
.tx_errors
++;
310 /* If we have space available to accept new transmit
311 * requests, wake up the queueing layer. This would
312 * be the case if the chipset_init() call above just
313 * flushes out the tx queue and empties it.
315 * If instead, the tx queue is retained then the
316 * netif_wake_queue() call should be placed in the
317 * TX completion interrupt handler of the driver instead
321 netif_wake_queue(dev
);
325 * Open/initialize the board. This is called (in the current kernel)
326 * sometime after booting when the 'ifconfig' program is run.
328 * This routine should set everything up anew at each open, even
329 * registers that "should" only need to be set once at boot, so that
330 * there is non-reboot way to recover if something goes wrong.
333 net_open(struct net_device
*dev
)
335 struct net_local
*np
= (struct net_local
*)dev
->priv
;
336 int ioaddr
= dev
->base_addr
;
338 * This is used if the interrupt line can turned off (shared).
339 * See 3c503.c for an example of selecting the IRQ at config-time.
341 if (request_irq(dev
->irq
, &net_interrupt
, 0, cardname
, dev
)) {
345 * Always allocate the DMA channel after the IRQ,
346 * and clean up on failure.
348 if (request_dma(dev
->dma
, cardname
)) {
349 free_irq(dev
->irq
, dev
);
353 /* Reset the hardware here. Don't forget to set the station address. */
354 chipset_init(dev
, 1);
356 np
->open_time
= jiffies
;
358 /* We are now ready to accept transmit requeusts from
359 * the queueing layer of the networking.
361 netif_start_queue(dev
);
366 /* This will only be invoked if your driver is _not_ in XOFF state.
367 * What this means is that you need not check it, and that this
368 * invariant will hold if you make sure that the netif_*_queue()
369 * calls are done at the proper times.
371 static int net_send_packet(struct sk_buff
*skb
, struct net_device
*dev
)
373 struct net_local
*np
= (struct net_local
*)dev
->priv
;
374 int ioaddr
= dev
->base_addr
;
375 short length
= ETH_ZLEN
< skb
->len
? skb
->len
: ETH_ZLEN
;
376 unsigned char *buf
= skb
->data
;
378 /* If some error occurs while trying to transmit this
379 * packet, you should return '1' from this function.
380 * In such a case you _may not_ do anything to the
381 * SKB, it is still owned by the network queueing
382 * layer when an error is returned. This means you
383 * may not modify any SKB fields, you may not free
388 /* This is the most common case for modern hardware.
389 * The spinlock protects this code from the TX complete
390 * hardware interrupt handler. Queue flow control is
391 * thus managed under this lock as well.
393 spin_lock_irq(&np
->lock
);
395 add_to_tx_ring(np
, skb
, length
);
396 dev
->trans_start
= jiffies
;
398 /* If we just used up the very last entry in the
399 * TX ring on this device, tell the queueing
400 * layer to send no more.
403 netif_stop_queue(dev
);
405 /* When the TX completion hw interrupt arrives, this
406 * is when the transmit statistics are updated.
409 spin_unlock_irq(&np
->lock
);
411 /* This is the case for older hardware which takes
412 * a single transmit buffer at a time, and it is
413 * just written to the device via PIO.
415 * No spin locking is needed since there is no TX complete
416 * event. If by chance your card does have a TX complete
417 * hardware IRQ then you may need to utilize np->lock here.
419 hardware_send_packet(ioaddr
, buf
, length
);
420 np
->stats
.tx_bytes
+= skb
->len
;
422 dev
->trans_start
= jiffies
;
424 /* You might need to clean up and record Tx statistics here. */
425 if (inw(ioaddr
) == /*RU*/81)
426 np
->stats
.tx_aborted_errors
++;
434 /* This handles TX complete events posted by the device
437 void net_tx(struct net_device
*dev
)
439 struct net_local
*np
= (struct net_local
*)dev
->priv
;
442 /* This protects us from concurrent execution of
443 * our dev->hard_start_xmit function above.
445 spin_lock(&np
->lock
);
448 while (tx_entry_is_sent(np
, entry
)) {
449 struct sk_buff
*skb
= np
->skbs
[entry
];
451 np
->stats
.tx_bytes
+= skb
->len
;
452 dev_kfree_skb_irq (skb
);
454 entry
= next_tx_entry(np
, entry
);
458 /* If we had stopped the queue due to a "tx full"
459 * condition, and space has now been made available,
462 if (netif_queue_stopped(dev
) && ! tx_full(dev
))
463 netif_wake_queue(dev
);
465 spin_unlock(&np
->lock
);
470 * The typical workload of the driver:
471 * Handle the network interface interrupts.
473 static irqreturn_t
net_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
475 struct net_device
*dev
= dev_id
;
476 struct net_local
*np
;
480 ioaddr
= dev
->base_addr
;
482 np
= (struct net_local
*)dev
->priv
;
483 status
= inw(ioaddr
+ 0);
489 if (status
& RX_INTR
) {
490 /* Got a packet(s). */
494 if (status
& TX_INTR
) {
495 /* Transmit complete. */
497 np
->stats
.tx_packets
++;
498 netif_wake_queue(dev
);
501 if (status
& COUNTERS_INTR
) {
502 /* Increment the appropriate 'localstats' field. */
503 np
->stats
.tx_window_errors
++;
506 return IRQ_RETVAL(handled
);
509 /* We have a good packet(s), get it/them out of the buffers. */
511 net_rx(struct net_device
*dev
)
513 struct net_local
*lp
= (struct net_local
*)dev
->priv
;
514 int ioaddr
= dev
->base_addr
;
518 int status
= inw(ioaddr
);
519 int pkt_len
= inw(ioaddr
);
521 if (pkt_len
== 0) /* Read all the frames? */
522 break; /* Done for now */
524 if (status
& 0x40) { /* There was an error. */
525 lp
->stats
.rx_errors
++;
526 if (status
& 0x20) lp
->stats
.rx_frame_errors
++;
527 if (status
& 0x10) lp
->stats
.rx_over_errors
++;
528 if (status
& 0x08) lp
->stats
.rx_crc_errors
++;
529 if (status
& 0x04) lp
->stats
.rx_fifo_errors
++;
531 /* Malloc up new buffer. */
534 lp
->stats
.rx_bytes
+=pkt_len
;
536 skb
= dev_alloc_skb(pkt_len
);
538 printk(KERN_NOTICE
"%s: Memory squeeze, dropping packet.\n",
540 lp
->stats
.rx_dropped
++;
545 /* 'skb->data' points to the start of sk_buff data area. */
546 memcpy(skb_put(skb
,pkt_len
), (void*)dev
->rmem_start
,
549 insw(ioaddr
, skb
->data
, (pkt_len
+ 1) >> 1);
552 dev
->last_rx
= jiffies
;
553 lp
->stats
.rx_packets
++;
554 lp
->stats
.rx_bytes
+= pkt_len
;
556 } while (--boguscount
);
561 /* The inverse routine to net_open(). */
563 net_close(struct net_device
*dev
)
565 struct net_local
*lp
= (struct net_local
*)dev
->priv
;
566 int ioaddr
= dev
->base_addr
;
570 netif_stop_queue(dev
);
572 /* Flush the Tx and disable Rx here. */
574 disable_dma(dev
->dma
);
576 /* If not IRQ or DMA jumpered, free up the line. */
577 outw(0x00, ioaddr
+0); /* Release the physical interrupt line. */
579 free_irq(dev
->irq
, dev
);
582 /* Update the statistics here. */
589 * Get the current statistics.
590 * This may be called with the card open or closed.
592 static struct net_device_stats
*net_get_stats(struct net_device
*dev
)
594 struct net_local
*lp
= (struct net_local
*)dev
->priv
;
595 short ioaddr
= dev
->base_addr
;
597 /* Update the statistics from the device registers. */
598 lp
->stats
.rx_missed_errors
= inw(ioaddr
+1);
603 * Set or clear the multicast filter for this adaptor.
604 * num_addrs == -1 Promiscuous mode, receive all packets
605 * num_addrs == 0 Normal mode, clear multicast list
606 * num_addrs > 0 Multicast mode, receive normal and MC packets,
607 * and do best-effort filtering.
610 set_multicast_list(struct net_device
*dev
)
612 short ioaddr
= dev
->base_addr
;
613 if (dev
->flags
&IFF_PROMISC
)
615 /* Enable promiscuous mode */
616 outw(MULTICAST
|PROMISC
, ioaddr
);
618 else if((dev
->flags
&IFF_ALLMULTI
) || dev
->mc_count
> HW_MAX_ADDRS
)
620 /* Disable promiscuous mode, use normal mode. */
621 hardware_set_filter(NULL
);
623 outw(MULTICAST
, ioaddr
);
625 else if(dev
->mc_count
)
627 /* Walk the address list, and load the filter */
628 hardware_set_filter(dev
->mc_list
);
630 outw(MULTICAST
, ioaddr
);
638 static struct net_device this_device
;
639 static int io
= 0x300;
643 MODULE_LICENSE("GPL");
645 int init_module(void)
650 printk(KERN_WARNING
"%s: You shouldn't use auto-probing with insmod!\n",
653 /* Copy the parameters from insmod into the device structure. */
654 this_device
.base_addr
= io
;
655 this_device
.irq
= irq
;
656 this_device
.dma
= dma
;
657 this_device
.mem_start
= mem
;
658 this_device
.init
= netcard_probe
;
660 if ((result
= register_netdev(&this_device
)) != 0)
669 unregister_netdev(&this_device
);
671 * If we don't do this, we can't re-insmod it later.
672 * Release irq/dma here, when you have jumpered versions and
673 * allocate them in net_probe1().
676 free_irq(this_device.irq, dev);
677 free_dma(this_device.dma);
679 release_region(this_device
.base_addr
, NETCARD_IO_EXTENT
);
681 if (this_device
.priv
)
682 kfree(this_device
.priv
);
690 * gcc -D__KERNEL__ -Wall -Wstrict-prototypes -Wwrite-strings
691 * -Wredundant-decls -O2 -m486 -c skeleton.c
693 * kept-new-versions: 5