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
24 static const char *version
=
25 "skeleton.c:v1.51 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
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
37 * Finally, keep in mind that the Linux kernel is has an API, not
38 * ABI. Proprietary object-code-only distributions are not permitted
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>
52 #include <linux/malloc.h>
53 #include <linux/string.h>
54 #include <asm/system.h>
55 #include <asm/bitops.h>
58 #include <linux/errno.h>
59 #include <linux/init.h>
61 #include <linux/netdevice.h>
62 #include <linux/etherdevice.h>
63 #include <linux/skbuff.h>
66 * The name of the card. Is used for messages and in the requests for
67 * io regions, irqs and dma channels
69 static const char* cardname
= "netcard";
71 /* First, a few definitions that the brave might change. */
73 /* A zero-terminated list of I/O addresses to be probed. */
74 static unsigned int netcard_portlist
[] __initdata
=
75 { 0x200, 0x240, 0x280, 0x2C0, 0x300, 0x320, 0x340, 0};
77 /* use 0 for production, 1 for verification, >2 for debug */
81 static unsigned int net_debug
= NET_DEBUG
;
83 /* The number of low I/O ports used by the ethercard. */
84 #define NETCARD_IO_EXTENT 32
86 /* Information that need to be kept for each board. */
88 struct net_device_stats stats
;
89 long open_time
; /* Useless example local info. */
92 /* The station (ethernet) address prefix, used for IDing the board. */
97 /* Index to functions, as function prototypes. */
99 extern int netcard_probe(struct device
*dev
);
101 static int netcard_probe1(struct device
*dev
, int ioaddr
);
102 static int net_open(struct device
*dev
);
103 static int net_send_packet(struct sk_buff
*skb
, struct device
*dev
);
104 static void net_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
);
105 static void net_rx(struct device
*dev
);
106 static int net_close(struct device
*dev
);
107 static struct net_device_stats
*net_get_stats(struct device
*dev
);
108 static void set_multicast_list(struct device
*dev
);
110 /* Example routines you must write ;->. */
111 #define tx_done(dev) 1
112 extern void hardware_send_packet(short ioaddr
, char *buf
, int length
);
113 extern void chipset_init(struct device
*dev
, int startp
);
116 * Check for a network adaptor of this type, and return '0' iff one exists.
117 * If dev->base_addr == 0, probe all likely locations.
118 * If dev->base_addr == 1, always return failure.
119 * If dev->base_addr == 2, allocate space for the device and return success
120 * (detachable devices only).
124 * Support for an alternate probe manager,
125 * which will eliminate the boilerplate below.
127 struct netdev_entry netcard_drv
=
128 {cardname
, netcard_probe1
, NETCARD_IO_EXTENT
, netcard_portlist
};
131 netcard_probe(struct device
*dev
))
134 int base_addr
= dev
? dev
->base_addr
: 0;
136 if (base_addr
> 0x1ff) /* Check a single specified location. */
137 return netcard_probe1(dev
, base_addr
);
138 else if (base_addr
!= 0) /* Don't probe at all. */
141 for (i
= 0; netcard_portlist
[i
]; i
++) {
142 int ioaddr
= netcard_portlist
[i
];
143 if (check_region(ioaddr
, NETCARD_IO_EXTENT
))
145 if (netcard_probe1(dev
, ioaddr
) == 0)
154 * This is the real probe routine. Linux has a history of friendly device
155 * probes on the ISA bus. A good device probes avoids doing writes, and
156 * verifies that the correct device exists and functions.
158 __initfunc(static int netcard_probe1(struct device
*dev
, int ioaddr
))
160 static unsigned version_printed
= 0;
164 * For ethernet adaptors the first three octets of the station address
165 * contains the manufacturer's unique code. That might be a good probe
166 * method. Ideally you would add additional checks.
168 if (inb(ioaddr
+ 0) != SA_ADDR0
169 || inb(ioaddr
+ 1) != SA_ADDR1
170 || inb(ioaddr
+ 2) != SA_ADDR2
) {
174 /* Allocate a new 'dev' if needed. */
177 * Don't allocate the private data here, it is done later
178 * This makes it easier to free the memory when this driver
179 * is used as a module.
181 dev
= init_etherdev(0, 0);
186 if (net_debug
&& version_printed
++ == 0)
187 printk(KERN_DEBUG
"%s", version
);
189 printk(KERN_INFO
"%s: %s found at %#3x, ", dev
->name
, cardname
, ioaddr
);
191 /* Fill in the 'dev' fields. */
192 dev
->base_addr
= ioaddr
;
194 /* Retrieve and print the ethernet address. */
195 for (i
= 0; i
< 6; i
++)
196 printk(" %2.2x", dev
->dev_addr
[i
] = inb(ioaddr
+ i
));
198 #ifdef jumpered_interrupts
200 * If this board has jumpered interrupts, allocate the interrupt
201 * vector now. There is no point in waiting since no other device
202 * can use the interrupt, and this marks the irq as busy. Jumpered
203 * interrupts are typically not reported by the boards, and we must
204 * used autoIRQ to find them.
208 ; /* Do nothing: a user-level program will set it. */
209 else if (dev
->irq
< 2) { /* "Auto-IRQ" */
211 /* Trigger an interrupt here. */
213 dev
->irq
= autoirq_report(0);
215 printk(" autoirq is %d", dev
->irq
);
216 } else if (dev
->irq
== 2)
218 * Fixup for users that don't know that IRQ 2 is really
219 * IRQ9, or don't know which one to set.
224 int irqval
= request_irq(dev
->irq
, &net_interrupt
, 0, cardname
, dev
);
226 printk("%s: unable to get IRQ %d (irqval=%d).\n",
227 dev
->name
, dev
->irq
, irqval
);
231 #endif /* jumpered interrupt */
234 * If we use a jumpered DMA channel, that should be probed for and
235 * allocated here as well. See lance.c for an example.
238 if (request_dma(dev
->dma
, cardname
)) {
239 printk("DMA %d allocation failed.\n", dev
->dma
);
242 printk(", assigned DMA %d.\n", dev
->dma
);
244 short dma_status
, new_dma_status
;
246 /* Read the DMA channel status registers. */
247 dma_status
= ((inb(DMA1_STAT_REG
) >> 4) & 0x0f) |
248 (inb(DMA2_STAT_REG
) & 0xf0);
249 /* Trigger a DMA request, perhaps pause a bit. */
250 outw(0x1234, ioaddr
+ 8);
251 /* Re-read the DMA status registers. */
252 new_dma_status
= ((inb(DMA1_STAT_REG
) >> 4) & 0x0f) |
253 (inb(DMA2_STAT_REG
) & 0xf0);
255 * Eliminate the old and floating requests,
256 * and DMA4 the cascade.
258 new_dma_status
^= dma_status
;
259 new_dma_status
&= ~0x10;
260 for (i
= 7; i
> 0; i
--)
261 if (test_bit(i
, &new_dma_status
)) {
266 printk("DMA probe failed.\n");
269 if (request_dma(dev
->dma
, cardname
)) {
270 printk("probed DMA %d allocation failed.\n", dev
->dma
);
274 #endif /* jumpered DMA */
276 /* Initialize the device structure. */
277 if (dev
->priv
== NULL
) {
278 dev
->priv
= kmalloc(sizeof(struct net_local
), GFP_KERNEL
);
279 if (dev
->priv
== NULL
)
283 memset(dev
->priv
, 0, sizeof(struct net_local
));
285 /* Grab the region so that no one else tries to probe our ioports. */
286 request_region(ioaddr
, NETCARD_IO_EXTENT
, cardname
);
288 dev
->open
= net_open
;
289 dev
->stop
= net_close
;
290 dev
->hard_start_xmit
= net_send_packet
;
291 dev
->get_stats
= net_get_stats
;
292 dev
->set_multicast_list
= &set_multicast_list
;
294 /* Fill in the fields of the device structure with ethernet values. */
301 * Open/initialize the board. This is called (in the current kernel)
302 * sometime after booting when the 'ifconfig' program is run.
304 * This routine should set everything up anew at each open, even
305 * registers that "should" only need to be set once at boot, so that
306 * there is non-reboot way to recover if something goes wrong.
309 net_open(struct device
*dev
)
311 struct net_local
*lp
= (struct net_local
*)dev
->priv
;
312 int ioaddr
= dev
->base_addr
;
314 * This is used if the interrupt line can turned off (shared).
315 * See 3c503.c for an example of selecting the IRQ at config-time.
317 if (request_irq(dev
->irq
, &net_interrupt
, 0, cardname
, dev
)) {
321 * Always allocate the DMA channel after the IRQ,
322 * and clean up on failure.
324 if (request_dma(dev
->dma
, cardname
)) {
325 free_irq(dev
->irq
, dev
);
329 /* Reset the hardware here. Don't forget to set the station address. */
330 /*chipset_init(dev, 1);*/
332 lp
->open_time
= jiffies
;
343 static int net_send_packet(struct sk_buff
*skb
, struct device
*dev
)
345 struct net_local
*lp
= (struct net_local
*)dev
->priv
;
346 int ioaddr
= dev
->base_addr
;
350 * If we get here, some higher level has decided we are broken.
351 * There should really be a "kick me" function call instead.
353 int tickssofar
= jiffies
- dev
->trans_start
;
356 printk(KERN_WARNING
"%s: transmit timed out, %s?\n", dev
->name
,
357 tx_done(dev
) ? "IRQ conflict" : "network cable problem");
358 /* Try to restart the adaptor. */
359 chipset_init(dev
, 1);
361 dev
->trans_start
= jiffies
;
365 * Block a timer-based transmit from overlapping. This could better be
366 * done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
368 if (test_and_set_bit(0, (void*)&dev
->tbusy
) != 0)
369 printk(KERN_WARNING
"%s: Transmitter access conflict.\n", dev
->name
);
371 short length
= ETH_ZLEN
< skb
->len
? skb
->len
: ETH_ZLEN
;
372 unsigned char *buf
= skb
->data
;
373 lp
->stats
.tx_bytes
+=skb
->len
;
374 hardware_send_packet(ioaddr
, buf
, length
);
375 dev
->trans_start
= jiffies
;
379 /* You might need to clean up and record Tx statistics here. */
380 if (inw(ioaddr
) == /*RU*/81)
381 lp
->stats
.tx_aborted_errors
++;
387 * The typical workload of the driver:
388 * Handle the network interface interrupts.
390 static void net_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
392 struct device
*dev
= dev_id
;
393 struct net_local
*lp
;
394 int ioaddr
, status
, boguscount
= 0;
397 printk(KERN_WARNING
"%s: irq %d for unknown device.\n", cardname
, irq
);
402 ioaddr
= dev
->base_addr
;
403 lp
= (struct net_local
*)dev
->priv
;
404 status
= inw(ioaddr
+ 0);
407 if (status
/*& RX_INTR*/) {
408 /* Got a packet(s). */
411 if (status
/*& TX_INTR*/) {
412 lp
->stats
.tx_packets
++;
414 mark_bh(NET_BH
); /* Inform upper layers. */
416 if (status
/*& COUNTERS_INTR*/) {
417 /* Increment the appropriate 'localstats' field. */
418 lp
->stats
.tx_window_errors
++;
420 } while (++boguscount
< 20) ;
426 /* We have a good packet(s), get it/them out of the buffers. */
428 net_rx(struct device
*dev
)
430 struct net_local
*lp
= (struct net_local
*)dev
->priv
;
431 int ioaddr
= dev
->base_addr
;
435 int status
= inw(ioaddr
);
436 int pkt_len
= inw(ioaddr
);
438 if (pkt_len
== 0) /* Read all the frames? */
439 break; /* Done for now */
441 if (status
& 0x40) { /* There was an error. */
442 lp
->stats
.rx_errors
++;
443 if (status
& 0x20) lp
->stats
.rx_frame_errors
++;
444 if (status
& 0x10) lp
->stats
.rx_over_errors
++;
445 if (status
& 0x08) lp
->stats
.rx_crc_errors
++;
446 if (status
& 0x04) lp
->stats
.rx_fifo_errors
++;
448 /* Malloc up new buffer. */
451 lp
->stats
.rx_bytes
+=pkt_len
;
453 skb
= dev_alloc_skb(pkt_len
);
455 printk(KERN_NOTICE
"%s: Memory squeeze, dropping packet.\n",
457 lp
->stats
.rx_dropped
++;
462 /* 'skb->data' points to the start of sk_buff data area. */
463 memcpy(skb_put(skb
,pkt_len
), (void*)dev
->rmem_start
,
466 insw(ioaddr
, skb
->data
, (pkt_len
+ 1) >> 1);
469 lp
->stats
.rx_packets
++;
471 } while (--boguscount
);
474 * If any worth-while packets have been received, dev_rint()
475 * has done a mark_bh(NET_BH) for us and will work on them
476 * when we get to the bottom-half routine.
481 /* The inverse routine to net_open(). */
483 net_close(struct device
*dev
)
485 struct net_local
*lp
= (struct net_local
*)dev
->priv
;
486 int ioaddr
= dev
->base_addr
;
493 /* Flush the Tx and disable Rx here. */
495 disable_dma(dev
->dma
);
497 /* If not IRQ or DMA jumpered, free up the line. */
498 outw(0x00, ioaddr
+0); /* Release the physical interrupt line. */
500 free_irq(dev
->irq
, dev
);
503 /* Update the statistics here. */
512 * Get the current statistics.
513 * This may be called with the card open or closed.
515 static struct net_device_stats
*net_get_stats(struct device
*dev
)
517 struct net_local
*lp
= (struct net_local
*)dev
->priv
;
518 short ioaddr
= dev
->base_addr
;
521 /* Update the statistics from the device registers. */
522 lp
->stats
.rx_missed_errors
= inw(ioaddr
+1);
529 * Set or clear the multicast filter for this adaptor.
530 * num_addrs == -1 Promiscuous mode, receive all packets
531 * num_addrs == 0 Normal mode, clear multicast list
532 * num_addrs > 0 Multicast mode, receive normal and MC packets,
533 * and do best-effort filtering.
536 set_multicast_list(struct device
*dev
)
538 short ioaddr
= dev
->base_addr
;
539 if (dev
->flags
&IFF_PROMISC
)
541 /* Enable promiscuous mode */
542 outw(MULTICAST
|PROMISC
, ioaddr
);
544 else if((dev
->flags
&IFF_ALLMULTI
) || dev
->mc_count
> HW_MAX_ADDRS
)
546 /* Disable promiscuous mode, use normal mode. */
547 hardware_set_filter(NULL
);
549 outw(MULTICAST
, ioaddr
);
551 else if(dev
->mc_count
)
553 /* Walk the address list, and load the filter */
554 hardware_set_filter(dev
->mc_list
);
556 outw(MULTICAST
, ioaddr
);
564 static char devicename
[9] = { 0, };
565 static struct device this_device
= {
566 devicename
, /* will be inserted by linux/drivers/net/net_init.c */
568 0, 0, /* I/O address, IRQ */
569 0, 0, 0, NULL
, netcard_probe
};
571 static int io
= 0x300;
576 int init_module(void)
581 printk(KERN_WARNING
"%s: You shouldn't use auto-probing with insmod!\n",
584 /* Copy the parameters from insmod into the device structure. */
585 this_device
.base_addr
= io
;
586 this_device
.irq
= irq
;
587 this_device
.dma
= dma
;
588 this_device
.mem_start
= mem
;
590 if ((result
= register_netdev(&this_device
)) != 0)
599 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
600 unregister_netdev(&this_device
);
602 * If we don't do this, we can't re-insmod it later.
603 * Release irq/dma here, when you have jumpered versions and
604 * allocate them in net_probe1().
607 free_irq(this_device.irq, dev);
608 free_dma(this_device.dma);
610 release_region(this_device
.base_addr
, NETCARD_IO_EXTENT
);
612 if (this_device
.priv
)
613 kfree_s(this_device
.priv
, sizeof(struct net_local
));
621 * gcc -D__KERNEL__ -Wall -Wstrict-prototypes -Wwrite-strings
622 * -Wredundant-decls -O2 -m486 -c skeleton.c
624 * kept-new-versions: 5