📄 skeleton.c
字号:
*/ if (request_irq(dev->irq, &net_interrupt, 0, cardname, dev)) { return -EAGAIN; } /* * Always allocate the DMA channel after the IRQ, * and clean up on failure. */ if (request_dma(dev->dma, cardname)) { free_irq(dev->irq, dev); return -EAGAIN; } /* Reset the hardware here. Don't forget to set the station address. */ /*chipset_init(dev, 1);*/ outb(0x00, ioaddr); lp->open_time = jiffies; dev->tbusy = 0; dev->interrupt = 0; dev->start = 1; MOD_INC_USE_COUNT; return 0;}static int net_send_packet(struct sk_buff *skb, struct device *dev){ struct net_local *lp = (struct net_local *)dev->priv; int ioaddr = dev->base_addr; if (dev->tbusy) { /* * If we get here, some higher level has decided we are broken. * There should really be a "kick me" function call instead. */ int tickssofar = jiffies - dev->trans_start; if (tickssofar < 5) return 1; printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name, tx_done(dev) ? "IRQ conflict" : "network cable problem"); /* Try to restart the adaptor. */ chipset_init(dev, 1); dev->tbusy=0; dev->trans_start = jiffies; } /* * Block a timer-based transmit from overlapping. This could better be * done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */ if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) printk(KERN_WARNING "%s: Transmitter access conflict.\n", dev->name); else { short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN; unsigned char *buf = skb->data; lp->stats.tx_bytes+=skb->len; hardware_send_packet(ioaddr, buf, length); dev->trans_start = jiffies; } dev_kfree_skb (skb); /* You might need to clean up and record Tx statistics here. */ if (inw(ioaddr) == /*RU*/81) lp->stats.tx_aborted_errors++; return 0;}/* * The typical workload of the driver: * Handle the network interface interrupts. */static void net_interrupt(int irq, void *dev_id, struct pt_regs * regs){ struct device *dev = dev_id; struct net_local *lp; int ioaddr, status, boguscount = 0; if (dev == NULL) { printk(KERN_WARNING "%s: irq %d for unknown device.\n", cardname, irq); return; } dev->interrupt = 1; ioaddr = dev->base_addr; lp = (struct net_local *)dev->priv; status = inw(ioaddr + 0); do { if (status /*& RX_INTR*/) { /* Got a packet(s). */ net_rx(dev); } if (status /*& TX_INTR*/) { lp->stats.tx_packets++; dev->tbusy = 0; mark_bh(NET_BH); /* Inform upper layers. */ } if (status /*& COUNTERS_INTR*/) { /* Increment the appropriate 'localstats' field. */ lp->stats.tx_window_errors++; } } while (++boguscount < 20) ; dev->interrupt = 0; return;}/* We have a good packet(s), get it/them out of the buffers. */static voidnet_rx(struct device *dev){ struct net_local *lp = (struct net_local *)dev->priv; int ioaddr = dev->base_addr; int boguscount = 10; do { int status = inw(ioaddr); int pkt_len = inw(ioaddr); if (pkt_len == 0) /* Read all the frames? */ break; /* Done for now */ if (status & 0x40) { /* There was an error. */ lp->stats.rx_errors++; if (status & 0x20) lp->stats.rx_frame_errors++; if (status & 0x10) lp->stats.rx_over_errors++; if (status & 0x08) lp->stats.rx_crc_errors++; if (status & 0x04) lp->stats.rx_fifo_errors++; } else { /* Malloc up new buffer. */ struct sk_buff *skb; lp->stats.rx_bytes+=pkt_len; skb = dev_alloc_skb(pkt_len); if (skb == NULL) { printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name); lp->stats.rx_dropped++; break; } skb->dev = dev; /* 'skb->data' points to the start of sk_buff data area. */ memcpy(skb_put(skb,pkt_len), (void*)dev->rmem_start, pkt_len); /* or */ insw(ioaddr, skb->data, (pkt_len + 1) >> 1); netif_rx(skb); lp->stats.rx_packets++; } } while (--boguscount); /* * If any worth-while packets have been received, dev_rint() * has done a mark_bh(NET_BH) for us and will work on them * when we get to the bottom-half routine. */ return;}/* The inverse routine to net_open(). */static intnet_close(struct device *dev){ struct net_local *lp = (struct net_local *)dev->priv; int ioaddr = dev->base_addr; lp->open_time = 0; dev->tbusy = 1; dev->start = 0; /* Flush the Tx and disable Rx here. */ disable_dma(dev->dma); /* If not IRQ or DMA jumpered, free up the line. */ outw(0x00, ioaddr+0); /* Release the physical interrupt line. */ free_irq(dev->irq, dev); free_dma(dev->dma); /* Update the statistics here. */ MOD_DEC_USE_COUNT; return 0;}/* * Get the current statistics. * This may be called with the card open or closed. */static struct net_device_stats *net_get_stats(struct device *dev){ struct net_local *lp = (struct net_local *)dev->priv; short ioaddr = dev->base_addr; cli(); /* Update the statistics from the device registers. */ lp->stats.rx_missed_errors = inw(ioaddr+1); sti(); return &lp->stats;}/* * Set or clear the multicast filter for this adaptor. * num_addrs == -1 Promiscuous mode, receive all packets * num_addrs == 0 Normal mode, clear multicast list * num_addrs > 0 Multicast mode, receive normal and MC packets, * and do best-effort filtering. */static voidset_multicast_list(struct device *dev){ short ioaddr = dev->base_addr; if (dev->flags&IFF_PROMISC) { /* Enable promiscuous mode */ outw(MULTICAST|PROMISC, ioaddr); } else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > HW_MAX_ADDRS) { /* Disable promiscuous mode, use normal mode. */ hardware_set_filter(NULL); outw(MULTICAST, ioaddr); } else if(dev->mc_count) { /* Walk the address list, and load the filter */ hardware_set_filter(dev->mc_list); outw(MULTICAST, ioaddr); } else outw(0, ioaddr);}#ifdef MODULEstatic char devicename[9] = { 0, };static struct device this_device = { devicename, /* will be inserted by linux/drivers/net/net_init.c */ 0, 0, 0, 0, 0, 0, /* I/O address, IRQ */ 0, 0, 0, NULL, netcard_probe };static int io = 0x300;static int irq = 0;static int dma = 0;static int mem = 0;int init_module(void){ int result; if (io == 0) printk(KERN_WARNING "%s: You shouldn't use auto-probing with insmod!\n", cardname); /* Copy the parameters from insmod into the device structure. */ this_device.base_addr = io; this_device.irq = irq; this_device.dma = dma; this_device.mem_start = mem; if ((result = register_netdev(&this_device)) != 0) return result; return 0;}voidcleanup_module(void){ /* No need to check MOD_IN_USE, as sys_delete_module() checks. */ unregister_netdev(&this_device); /* * If we don't do this, we can't re-insmod it later. * Release irq/dma here, when you have jumpered versions and * allocate them in net_probe1(). */ /* free_irq(this_device.irq, dev); free_dma(this_device.dma); */ release_region(this_device.base_addr, NETCARD_IO_EXTENT); if (this_device.priv) kfree_s(this_device.priv, sizeof(struct net_local));}#endif /* MODULE *//* * Local variables: * compile-command: * gcc -D__KERNEL__ -Wall -Wstrict-prototypes -Wwrite-strings * -Wredundant-decls -O2 -m486 -c skeleton.c * version-control: t * kept-new-versions: 5 * tab-width: 4 * c-indent-level: 4 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -