1 /* isa-skeleton.c: A network driver outline for linux.
2 *
3 * Written 1993-94 by Donald Becker.
4 *
5 * Copyright 1993 United States Government as represented by the
6 * Director, National Security Agency.
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU Public License, incorporated herein by reference.
10 *
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
14 *
15 * This file is an outline for writing a network device driver for the
16 * the Linux operating system.
17 *
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.
21 *
22 */
23
24 static const char *version =
25 "isa-skeleton.c:v1.51 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
26
27 /*
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.
36 *
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.
40 */
41
42 #include <linux/module.h>
43
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>
61
62 #include <linux/netdevice.h>
63 #include <linux/etherdevice.h>
64 #include <linux/skbuff.h>
65
66 /*
67 * The name of the card. Is used for messages and in the requests for
68 * io regions, irqs and dma channels
69 */
70 static const char* cardname = "netcard";
71
72 /* First, a few definitions that the brave might change. */
73
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};
77
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;
83
84 /* The number of low I/O ports used by the ethercard. */
85 #define NETCARD_IO_EXTENT 32
86
87 #define MY_TX_TIMEOUT ((400*HZ)/1000)
88
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. */
93
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.
98 */
99 spinlock_t lock;
100 };
101
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
106
107 /* Index to functions, as function prototypes. */
108
109 extern int netcard_probe(struct net_device *dev);
110
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);
120
121
122 /* Example routines you must write ;->. */
123 #define tx_done(dev) 1
124 static void hardware_send_packet(short ioaddr, char *buf, int length);
125 static void chipset_init(struct net_device *dev, int startp);
126
127 /*
128 * Check for a network adaptor of this type, and return '' 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).
133 */
134 int __init
135 netcard_probe(struct net_device *dev)
136 {
137 int i;
138 int base_addr = dev->base_addr;
139
140 SET_MODULE_OWNER(dev);
141
142 if (base_addr > 0x1ff) /* Check a single specified location. */
143 return netcard_probe1(dev, base_addr);
144 else if (base_addr != 0) /* Don't probe at all. */
145 return -ENXIO;
146
147 for (i = 0; netcard_portlist[i]; i++) {
148 int ioaddr = netcard_portlist[i];
149 if (check_region(ioaddr, NETCARD_IO_EXTENT))
150 continue;
151 if (netcard_probe1(dev, ioaddr) == 0)
152 return 0;
153 }
154
155 return -ENODEV;
156 }
157
158 /*
159 * This is the real probe routine. Linux has a history of friendly device
160 * probes on the ISA bus. A good device probes avoids doing writes, and
161 * verifies that the correct device exists and functions.
162 */
163 static int __init netcard_probe1(struct net_device *dev, int ioaddr)
164 {
165 struct net_local *np;
166 static unsigned version_printed = 0;
167 int i;
168
169 /*
170 * For ethernet adaptors the first three octets of the station address
171 * contains the manufacturer's unique code. That might be a good probe
172 * method. Ideally you would add additional checks.
173 */
174 if (inb(ioaddr + 0) != SA_ADDR0
175 || inb(ioaddr + 1) != SA_ADDR1
176 || inb(ioaddr + 2) != SA_ADDR2) {
177 return -ENODEV;
178 }
179
180 if (net_debug && version_printed++ == 0)
181 printk(KERN_DEBUG "%s", version);
182
183 printk(KERN_INFO "%s: %s found at %#3x, ", dev->name, cardname, ioaddr);
184
185 /* Fill in the 'dev' fields. */
186 dev->base_addr = ioaddr;
187
188 /* Retrieve and print the ethernet address. */
189 for (i = 0; i < 6; i++)
190 printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
191
192 #ifdef jumpered_interrupts
193 /*
194 * If this board has jumpered interrupts, allocate the interrupt
195 * vector now. There is no point in waiting since no other device
196 * can use the interrupt, and this marks the irq as busy. Jumpered
197 * interrupts are typically not reported by the boards, and we must
198 * used autoIRQ to find them.
199 */
200
201 if (dev->irq == -1)
202 ; /* Do nothing: a user-level program will set it. */
203 else if (dev->irq < 2) { /* "Auto-IRQ" */
204 autoirq_setup(0);
205 /* Trigger an interrupt here. */
206
207 dev->irq = autoirq_report(0);
208 if (net_debug >= 2)
209 printk(" autoirq is %d", dev->irq);
210 } else if (dev->irq == 2)
211 /*
212 * Fixup for users that don't know that IRQ 2 is really
213 * IRQ9, or don't know which one to set.
214 */
215 dev->irq = 9;
216
217 {
218 int irqval = request_irq(dev->irq, &net_interrupt, 0, cardname, dev);
219 if (irqval) {
220 printk("%s: unable to get IRQ %d (irqval=%d).\n",
221 dev->name, dev->irq, irqval);
222 return -EAGAIN;
223 }
224 }
225 #endif /* jumpered interrupt */
226 #ifdef jumpered_dma
227 /*
228 * If we use a jumpered DMA channel, that should be probed for and
229 * allocated here as well. See lance.c for an example.
230 */
231 if (dev->dma == 0) {
232 if (request_dma(dev->dma, cardname)) {
233 printk("DMA %d allocation failed.\n", dev->dma);
234 return -EAGAIN;
235 } else
236 printk(", assigned DMA %d.\n", dev->dma);
237 } else {
238 short dma_status, new_dma_status;
239
240 /* Read the DMA channel status registers. */
241 dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
242 (inb(DMA2_STAT_REG) & 0xf0);
243 /* Trigger a DMA request, perhaps pause a bit. */
244 outw(0x1234, ioaddr + 8);
245 /* Re-read the DMA status registers. */
246 new_dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
247 (inb(DMA2_STAT_REG) & 0xf0);
248 /*
249 * Eliminate the old and floating requests,
250 * and DMA4 the cascade.
251 */
252 new_dma_status ^= dma_status;
253 new_dma_status &= ~0x10;
254 for (i = 7; i > 0; i--)
255 if (test_bit(i, &new_dma_status)) {
256 dev->dma = i;
257 break;
258 }
259 if (i <= 0) {
260 printk("DMA probe failed.\n");
261 return -EAGAIN;
262 }
263 if (request_dma(dev->dma, cardname)) {
264 printk("probed DMA %d allocation failed.\n", dev->dma);
265 return -EAGAIN;
266 }
267 }
268 #endif /* jumpered DMA */
269
270 /* Initialize the device structure. */
271 if (dev->priv == NULL) {
272 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
273 if (dev->priv == NULL)
274 return -ENOMEM;
275 }
276
277 memset(dev->priv, 0, sizeof(struct net_local));
278
279 np = (struct net_local *)dev->priv;
280 spin_lock_init(&np->lock);
281
282 /* Grab the region so that no one else tries to probe our ioports. */
283 request_region(ioaddr, NETCARD_IO_EXTENT, cardname);
284
285 dev->open = net_open;
286 dev->stop = net_close;
287 dev->hard_start_xmit = net_send_packet;
288 dev->get_stats = net_get_stats;
289 dev->set_multicast_list = &set_multicast_list;
290
291 dev->tx_timeout = &net_tx_timeout;
292 dev->watchdog_timeo = MY_TX_TIMEOUT;
293
294 /* Fill in the fields of the device structure with ethernet values. */
295 ether_setup(dev);
296
297 return 0;
298 }
299
300 static void net_tx_timeout(struct net_device *dev)
301 {
302 struct net_local *np = (struct net_local *)dev->priv;
303
304 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
305 tx_done(dev) ? "IRQ conflict" : "network cable problem");
306
307 /* Try to restart the adaptor. */
308 chipset_init(dev, 1);
309
310 np->stats.tx_errors++;
311
312 /* If we have space available to accept new transmit
313 * requests, wake up the queueing layer. This would
314 * be the case if the chipset_init() call above just
315 * flushes out the tx queue and empties it.
316 *
317 * If instead, the tx queue is retained then the
318 * netif_wake_queue() call should be placed in the
319 * TX completion interrupt handler of the driver instead
320 * of here.
321 */
322 if (!tx_full(dev))
323 netif_wake_queue(dev);
324 }
325
326 /*
327 * Open/initialize the board. This is called (in the current kernel)
328 * sometime after booting when the 'ifconfig' program is run.
329 *
330 * This routine should set everything up anew at each open, even
331 * registers that "should" only need to be set once at boot, so that
332 * there is non-reboot way to recover if something goes wrong.
333 */
334 static int
335 net_open(struct net_device *dev)
336 {
337 struct net_local *np = (struct net_local *)dev->priv;
338 int ioaddr = dev->base_addr;
339 /*
340 * This is used if the interrupt line can turned off (shared).
341 * See 3c503.c for an example of selecting the IRQ at config-time.
342 */
343 if (request_irq(dev->irq, &net_interrupt, 0, cardname, dev)) {
344 return -EAGAIN;
345 }
346 /*
347 * Always allocate the DMA channel after the IRQ,
348 * and clean up on failure.
349 */
350 if (request_dma(dev->dma, cardname)) {
351 free_irq(dev->irq, dev);
352 return -EAGAIN;
353 }
354
355 /* Reset the hardware here. Don't forget to set the station address. */
356 chipset_init(dev, 1);
357 outb(0x00, ioaddr);
358 np->open_time = jiffies;
359
360 /* We are now ready to accept transmit requeusts from
361 * the queueing layer of the networking.
362 */
363 netif_start_queue(dev);
364
365 return 0;
366 }
367
368 /* This will only be invoked if your driver is _not_ in XOFF state.
369 * What this means is that you need not check it, and that this
370 * invariant will hold if you make sure that the netif_*_queue()
371 * calls are done at the proper times.
372 */
373 static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
374 {
375 struct net_local *np = (struct net_local *)dev->priv;
376 int ioaddr = dev->base_addr;
377 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
378 unsigned char *buf = skb->data;
379
380 /* If some error occurs while trying to transmit this
381 * packet, you should return '1' from this function.
382 * In such a case you _may not_ do anything to the
383 * SKB, it is still owned by the network queueing
384 * layer when an error is returned. This means you
385 * may not modify any SKB fields, you may not free
386 * the SKB, etc.
387 */
388
389 #if TX_RING
390 /* This is the most common case for modern hardware.
391 * The spinlock protects this code from the TX complete
392 * hardware interrupt handler. Queue flow control is
393 * thus managed under this lock as well.
394 */
395 spin_lock_irq(&np->lock);
396
397 add_to_tx_ring(np, skb, length);
398 dev->trans_start = jiffies;
399
400 /* If we just used up the very last entry in the
401 * TX ring on this device, tell the queueing
402 * layer to send no more.
403 */
404 if (tx_full(dev))
405 netif_stop_queue(dev);
406
407 /* When the TX completion hw interrupt arrives, this
408 * is when the transmit statistics are updated.
409 */
410
411 spin_unlock_irq(&np->lock);
412 #else
413 /* This is the case for older hardware which takes
414 * a single transmit buffer at a time, and it is
415 * just written to the device via PIO.
416 *
417 * No spin locking is needed since there is no TX complete
418 * event. If by chance your card does have a TX complete
419 * hardware IRQ then you may need to utilize np->lock here.
420 */
421 hardware_send_packet(ioaddr, buf, length);
422 np->stats.tx_bytes += skb->len;
423
424 dev->trans_start = jiffies;
425
426 /* You might need to clean up and record Tx statistics here. */
427 if (inw(ioaddr) == /*RU*/81)
428 np->stats.tx_aborted_errors++;
429 dev_kfree_skb (skb);
430 #endif
431
432 return 0;
433 }
434
435 #if TX_RING
436 /* This handles TX complete events posted by the device
437 * via interrupts.
438 */
439 void net_tx(struct net_device *dev)
440 {
441 struct net_local *np = (struct net_local *)dev->priv;
442 int entry;
443
444 /* This protects us from concurrent execution of
445 * our dev->hard_start_xmit function above.
446 */
447 spin_lock(&np->lock);
448
449 entry = np->tx_old;
450 while (tx_entry_is_sent(np, entry)) {
451 struct sk_buff *skb = np->skbs[entry];
452
453 np->stats.tx_bytes += skb->len;
454 dev_kfree_skb_irq (skb);
455
456 entry = next_tx_entry(np, entry);
457 }
458 np->tx_old = entry;
459
460 /* If we had stopped the queue due to a "tx full"
461 * condition, and space has now been made available,
462 * wake up the queue.
463 */
464 if (netif_queue_stopped(dev) && ! tx_full(dev))
465 netif_wake_queue(dev);
466
467 spin_unlock(&np->lock);
468 }
469 #endif
470
471 /*
472 * The typical workload of the driver:
473 * Handle the network interface interrupts.
474 */
475 static void net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
476 {
477 struct net_device *dev = dev_id;
478 struct net_local *np;
479 int ioaddr, status;
480
481 ioaddr = dev->base_addr;
482
483 np = (struct net_local *)dev->priv;
484 status = inw(ioaddr + 0);
485
486 if (status & RX_INTR) {
487 /* Got a packet(s). */
488 net_rx(dev);
489 }
490 #if TX_RING
491 if (status & TX_INTR) {
492 /* Transmit complete. */
493 net_tx(dev);
494 np->stats.tx_packets++;
495 netif_wake_queue(dev);
496 }
497 #endif
498 if (status & COUNTERS_INTR) {
499 /* Increment the appropriate 'localstats' field. */
500 np->stats.tx_window_errors++;
501 }
502 }
503
504 /* We have a good packet(s), get it/them out of the buffers. */
505 static void
506 net_rx(struct net_device *dev)
507 {
508 struct net_local *lp = (struct net_local *)dev->priv;
509 int ioaddr = dev->base_addr;
510 int boguscount = 10;
511
512 do {
513 int status = inw(ioaddr);
514 int pkt_len = inw(ioaddr);
515
516 if (pkt_len == 0) /* Read all the frames? */
517 break; /* Done for now */
518
519 if (status & 0x40) { /* There was an error. */
520 lp->stats.rx_errors++;
521 if (status & 0x20) lp->stats.rx_frame_errors++;
522 if (status & 0x10) lp->stats.rx_over_errors++;
523 if (status & 0x08) lp->stats.rx_crc_errors++;
524 if (status & 0x04) lp->stats.rx_fifo_errors++;
525 } else {
526 /* Malloc up new buffer. */
527 struct sk_buff *skb;
528
529 lp->stats.rx_bytes+=pkt_len;
530
531 skb = dev_alloc_skb(pkt_len);
532 if (skb == NULL) {
533 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
534 dev->name);
535 lp->stats.rx_dropped++;
536 break;
537 }
538 skb->dev = dev;
539
540 /* 'skb->data' points to the start of sk_buff data area. */
541 memcpy(skb_put(skb,pkt_len), (void*)dev->rmem_start,
542 pkt_len);
543 /* or */
544 insw(ioaddr, skb->data, (pkt_len + 1) >> 1);
545
546 netif_rx(skb);
547 lp->stats.rx_packets++;
548 }
549 } while (--boguscount);
550
551 return;
552 }
553
554 /* The inverse routine to net_open(). */
555 static int
556 net_close(struct net_device *dev)
557 {
558 struct net_local *lp = (struct net_local *)dev->priv;
559 int ioaddr = dev->base_addr;
560
561 lp->open_time = 0;
562
563 netif_stop_queue(dev);
564
565 /* Flush the Tx and disable Rx here. */
566
567 disable_dma(dev->dma);
568
569 /* If not IRQ or DMA jumpered, free up the line. */
570 outw(0x00, ioaddr+0); /* Release the physical interrupt line. */
571
572 free_irq(dev->irq, dev);
573 free_dma(dev->dma);
574
575 /* Update the statistics here. */
576
577 return 0;
578
579 }
580
581 /*
582 * Get the current statistics.
583 * This may be called with the card open or closed.
584 */
585 static struct net_device_stats *net_get_stats(struct net_device *dev)
586 {
587 struct net_local *lp = (struct net_local *)dev->priv;
588 short ioaddr = dev->base_addr;
589
590 /* Update the statistics from the device registers. */
591 lp->stats.rx_missed_errors = inw(ioaddr+1);
592 return &lp->stats;
593 }
594
595 /*
596 * Set or clear the multicast filter for this adaptor.
597 * num_addrs == -1 Promiscuous mode, receive all packets
598 * num_addrs == 0 Normal mode, clear multicast list
599 * num_addrs > 0 Multicast mode, receive normal and MC packets,
600 * and do best-effort filtering.
601 */
602 static void
603 set_multicast_list(struct net_device *dev)
604 {
605 short ioaddr = dev->base_addr;
606 if (dev->flags&IFF_PROMISC)
607 {
608 /* Enable promiscuous mode */
609 outw(MULTICAST|PROMISC, ioaddr);
610 }
611 else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > HW_MAX_ADDRS)
612 {
613 /* Disable promiscuous mode, use normal mode. */
614 hardware_set_filter(NULL);
615
616 outw(MULTICAST, ioaddr);
617 }
618 else if(dev->mc_count)
619 {
620 /* Walk the address list, and load the filter */
621 hardware_set_filter(dev->mc_list);
622
623 outw(MULTICAST, ioaddr);
624 }
625 else
626 outw(0, ioaddr);
627 }
628
629 #ifdef MODULE
630
631 static struct net_device this_device;
632 static int io = 0x300;
633 static int irq;
634 static int dma;
635 static int mem;
636
637 int init_module(void)
638 {
639 int result;
640
641 if (io == 0)
642 printk(KERN_WARNING "%s: You shouldn't use auto-probing with insmod!\n",
643 cardname);
644
645 /* Copy the parameters from insmod into the device structure. */
646 this_device.base_addr = io;
647 this_device.irq = irq;
648 this_device.dma = dma;
649 this_device.mem_start = mem;
650 this_device.init = netcard_probe;
651
652 if ((result = register_netdev(&this_device)) != 0)
653 return result;
654
655 return 0;
656 }
657
658 void
659 cleanup_module(void)
660 {
661 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
662 unregister_netdev(&this_device);
663 /*
664 * If we don't do this, we can't re-insmod it later.
665 * Release irq/dma here, when you have jumpered versions and
666 * allocate them in net_probe1().
667 */
668 /*
669 free_irq(this_device.irq, dev);
670 free_dma(this_device.dma);
671 */
672 release_region(this_device.base_addr, NETCARD_IO_EXTENT);
673
674 if (this_device.priv)
675 kfree(this_device.priv);
676 }
677
678 #endif /* MODULE */
679
680 /*
681 * Local variables:
682 * compile-command:
683 * gcc -D__KERNEL__ -Wall -Wstrict-prototypes -Wwrite-strings
684 * -Wredundant-decls -O2 -m486 -c skeleton.c
685 * version-control: t
686 * kept-new-versions: 5
687 * tab-width: 4
688 * c-indent-level: 4
689 * End:
690 */
691
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.