3c509.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,663 行 · 第 1/3 页
C
1,663 行
dev->irq = irq; dev->if_port = if_port; lp = netdev_priv(dev);#if defined(__ISAPNP__) lp->dev = &idev->dev;#endif err = el3_common_init(dev); if (err) goto out1;#ifdef CONFIG_PM /* register power management */ lp->pmdev = pm_register(PM_ISA_DEV, card_idx, el3_pm_callback); if (lp->pmdev) { struct pm_dev *p; p = lp->pmdev; p->data = (struct net_device *)dev; }#endif el3_cards++; lp->next_dev = el3_root_dev; el3_root_dev = dev; return 0;out1:#if defined(__ISAPNP__) if (idev) pnp_device_detach(idev);#endifout: free_netdev(dev); return err;}#ifdef CONFIG_MCAstatic int __init el3_mca_probe(struct device *device){ /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch, * heavily modified by Chris Beauregard * (cpbeaure@csclub.uwaterloo.ca) to support standard MCA * probing. * * redone for multi-card detection by ZP Gu (zpg@castle.net) * now works as a module */ struct el3_private *lp; short i; int ioaddr, irq, if_port; u16 phys_addr[3]; struct net_device *dev = NULL; u_char pos4, pos5; struct mca_device *mdev = to_mca_device(device); int slot = mdev->slot; int err; pos4 = mca_device_read_stored_pos(mdev, 4); pos5 = mca_device_read_stored_pos(mdev, 5); ioaddr = ((short)((pos4&0xfc)|0x02)) << 8; irq = pos5 & 0x0f; printk("3c529: found %s at slot %d\n", el3_mca_adapter_names[mdev->index], slot + 1); /* claim the slot */ strncpy(mdev->name, el3_mca_adapter_names[mdev->index], sizeof(mdev->name)); mca_device_set_claim(mdev, 1); if_port = pos4 & 0x03; irq = mca_device_transform_irq(mdev, irq); ioaddr = mca_device_transform_ioport(mdev, ioaddr); if (el3_debug > 2) { printk("3c529: irq %d ioaddr 0x%x ifport %d\n", irq, ioaddr, if_port); } EL3WINDOW(0); for (i = 0; i < 3; i++) { phys_addr[i] = htons(read_eeprom(ioaddr, i)); } dev = alloc_etherdev(sizeof (struct el3_private)); if (dev == NULL) { release_region(ioaddr, EL3_IO_EXTENT); return -ENOMEM; } SET_MODULE_OWNER(dev); netdev_boot_setup_check(dev); memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr)); dev->base_addr = ioaddr; dev->irq = irq; dev->if_port = if_port; lp = netdev_priv(dev); lp->dev = device; lp->type = EL3_MCA; device->driver_data = dev; err = el3_common_init(dev); if (err) { device->driver_data = NULL; free_netdev(dev); return -ENOMEM; } el3_cards++; return 0;} #endif /* CONFIG_MCA */#ifdef CONFIG_EISAstatic int __init el3_eisa_probe (struct device *device){ struct el3_private *lp; short i; int ioaddr, irq, if_port; u16 phys_addr[3]; struct net_device *dev = NULL; struct eisa_device *edev; int err; /* Yeepee, The driver framework is calling us ! */ edev = to_eisa_device (device); ioaddr = edev->base_addr; if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509")) return -EBUSY; /* Change the register set to the configuration window 0. */ outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD); irq = inw(ioaddr + WN0_IRQ) >> 12; if_port = inw(ioaddr + 6)>>14; for (i = 0; i < 3; i++) phys_addr[i] = htons(read_eeprom(ioaddr, i)); /* Restore the "Product ID" to the EEPROM read register. */ read_eeprom(ioaddr, 3); dev = alloc_etherdev(sizeof (struct el3_private)); if (dev == NULL) { release_region(ioaddr, EL3_IO_EXTENT); return -ENOMEM; } SET_MODULE_OWNER(dev); netdev_boot_setup_check(dev); memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr)); dev->base_addr = ioaddr; dev->irq = irq; dev->if_port = if_port; lp = netdev_priv(dev); lp->dev = device; lp->type = EL3_EISA; eisa_set_drvdata (edev, dev); err = el3_common_init(dev); if (err) { eisa_set_drvdata (edev, NULL); free_netdev(dev); return err; } el3_cards++; return 0;}#endif#if defined(CONFIG_EISA) || defined(CONFIG_MCA)/* This remove works for all device types. * * The net dev must be stored in the driver_data field */static int __devexit el3_device_remove (struct device *device){ struct net_device *dev; dev = device->driver_data; el3_common_remove (dev); return 0;}#endif/* Read a word from the EEPROM using the regular EEPROM access register. Assume that we are in register window zero. */static ushort read_eeprom(int ioaddr, int index){ outw(EEPROM_READ + index, ioaddr + 10); /* Pause for at least 162 us. for the read to take place. Some chips seem to require much longer */ mdelay(2); return inw(ioaddr + 12);}/* Read a word from the EEPROM when in the ISA ID probe state. */static ushort __init id_read_eeprom(int index){ int bit, word = 0; /* Issue read command, and pause for at least 162 us. for it to complete. Assume extra-fast 16Mhz bus. */ outb(EEPROM_READ + index, id_port); /* Pause for at least 162 us. for the read to take place. */ /* Some chips seem to require much longer */ mdelay(4); for (bit = 15; bit >= 0; bit--) word = (word << 1) + (inb(id_port) & 0x01); if (el3_debug > 3) printk(" 3c509 EEPROM word %d %#4.4x.\n", index, word); return word;}static intel3_open(struct net_device *dev){ int ioaddr = dev->base_addr; int i; outw(TxReset, ioaddr + EL3_CMD); outw(RxReset, ioaddr + EL3_CMD); outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD); i = request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev); if (i) return i; EL3WINDOW(0); if (el3_debug > 3) printk("%s: Opening, IRQ %d status@%x %4.4x.\n", dev->name, dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS)); el3_up(dev); if (el3_debug > 3) printk("%s: Opened 3c509 IRQ %d status %4.4x.\n", dev->name, dev->irq, inw(ioaddr + EL3_STATUS)); return 0;}static voidel3_tx_timeout (struct net_device *dev){ struct el3_private *lp = netdev_priv(dev); int ioaddr = dev->base_addr; /* Transmitter timeout, serious problems. */ printk("%s: transmit timed out, Tx_status %2.2x status %4.4x " "Tx FIFO room %d.\n", dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS), inw(ioaddr + TX_FREE)); lp->stats.tx_errors++; dev->trans_start = jiffies; /* Issue TX_RESET and TX_START commands. */ outw(TxReset, ioaddr + EL3_CMD); outw(TxEnable, ioaddr + EL3_CMD); netif_wake_queue(dev);}static intel3_start_xmit(struct sk_buff *skb, struct net_device *dev){ struct el3_private *lp = netdev_priv(dev); int ioaddr = dev->base_addr; unsigned long flags; netif_stop_queue (dev); lp->stats.tx_bytes += skb->len; if (el3_debug > 4) { printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n", dev->name, skb->len, inw(ioaddr + EL3_STATUS)); }#if 0#ifndef final_version { /* Error-checking code, delete someday. */ ushort status = inw(ioaddr + EL3_STATUS); if (status & 0x0001 /* IRQ line active, missed one. */ && inw(ioaddr + EL3_STATUS) & 1) { /* Make sure. */ printk("%s: Missed interrupt, status then %04x now %04x" " Tx %2.2x Rx %4.4x.\n", dev->name, status, inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS), inw(ioaddr + RX_STATUS)); /* Fake interrupt trigger by masking, acknowledge interrupts. */ outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD); outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq, ioaddr + EL3_CMD); outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD); } }#endif#endif /* * We lock the driver against other processors. Note * we don't need to lock versus the IRQ as we suspended * that. This means that we lose the ability to take * an RX during a TX upload. That sucks a bit with SMP * on an original 3c509 (2K buffer) * * Using disable_irq stops us crapping on other * time sensitive devices. */ spin_lock_irqsave(&lp->lock, flags); /* Put out the doubleword header... */ outw(skb->len, ioaddr + TX_FIFO); outw(0x00, ioaddr + TX_FIFO); /* ... and the packet rounded to a doubleword. */#ifdef __powerpc__ outsl_ns(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);#else outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);#endif dev->trans_start = jiffies; if (inw(ioaddr + TX_FREE) > 1536) netif_start_queue(dev); else /* Interrupt us when the FIFO has room for max-sized packet. */ outw(SetTxThreshold + 1536, ioaddr + EL3_CMD); spin_unlock_irqrestore(&lp->lock, flags); dev_kfree_skb (skb); /* Clear the Tx status stack. */ { short tx_status; int i = 4; while (--i > 0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) { if (tx_status & 0x38) lp->stats.tx_aborted_errors++; if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD); if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD); outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */ } } return 0;}/* The EL3 interrupt handler. */static irqreturn_tel3_interrupt(int irq, void *dev_id, struct pt_regs *regs){ struct net_device *dev = (struct net_device *)dev_id; struct el3_private *lp; int ioaddr, status; int i = max_interrupt_work; if (dev == NULL) { printk ("el3_interrupt(): irq %d for unknown device.\n", irq); return IRQ_NONE; } lp = netdev_priv(dev); spin_lock(&lp->lock); ioaddr = dev->base_addr; if (el3_debug > 4) { status = inw(ioaddr + EL3_STATUS); printk("%s: interrupt, status %4.4x.\n", dev->name, status); } while ((status = inw(ioaddr + EL3_STATUS)) & (IntLatch | RxComplete | StatsFull)) { if (status & RxComplete) el3_rx(dev); if (status & TxAvailable) { if (el3_debug > 5) printk(" TX room bit was handled.\n"); /* There's room in the FIFO for a full-sized packet. */ outw(AckIntr | TxAvailable, ioaddr + EL3_CMD); netif_wake_queue (dev); } if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) { /* Handle all uncommon interrupts. */ if (status & StatsFull) /* Empty statistics. */ update_stats(dev); if (status & RxEarly) { /* Rx early is unused. */ el3_rx(dev); outw(AckIntr | RxEarly, ioaddr + EL3_CMD); } if (status & TxComplete) { /* Really Tx error. */ struct el3_private *lp = netdev_priv(dev); short tx_status; int i = 4; while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) { if (tx_status & 0x38) lp->stats.tx_aborted_errors++; if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD); if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD); outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */ } } if (status & AdapterFailure) { /* Adapter failure requires Rx reset and reinit. */ outw(RxReset, ioaddr + EL3_CMD); /* Set the Rx filter to the current state. */ outw(SetRxFilter | RxStation | RxBroadcast | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0) | (dev->flags & IFF_PROMISC ? RxProm : 0), ioaddr + EL3_CMD); outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */ outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD); } } if (--i < 0) { printk("%s: Infinite loop in interrupt, status %4.4x.\n", dev->name, status); /* Clear all interrupts. */ outw(AckIntr | 0xFF, ioaddr + EL3_CMD); break; } /* Acknowledge the IRQ. */ outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */ } if (el3_debug > 4) { printk("%s: exiting interrupt, status %4.4x.\n", dev->name, inw(ioaddr + EL3_STATUS)); } spin_unlock(&lp->lock); return IRQ_HANDLED;}static struct net_device_stats *el3_get_stats(struct net_device *dev){ struct el3_private *lp = netdev_priv(dev); unsigned long flags; /* * This is fast enough not to bother with disable IRQ * stuff. */ spin_lock_irqsave(&lp->lock, flags); update_stats(dev); spin_unlock_irqrestore(&lp->lock, flags); return &lp->stats;}/* Update statistics. We change to register window 6, so this should be run single-threaded if the device is active. This is expected to be a rare operation, and it's simpler for the rest of the driver to assume that window 1 is always valid rather than use a special window-state variable. */static void update_stats(struct net_device *dev){ struct el3_private *lp = netdev_priv(dev); int ioaddr = dev->base_addr; if (el3_debug > 5) printk(" Updating the statistics.\n"); /* Turn off statistics updates while reading. */ outw(StatsDisable, ioaddr + EL3_CMD); /* Switch to the stats window, and read everything. */ EL3WINDOW(6); lp->stats.tx_carrier_errors += inb(ioaddr + 0); lp->stats.tx_heartbeat_errors += inb(ioaddr + 1); /* Multiple collisions. */ inb(ioaddr + 2); lp->stats.collisions += inb(ioaddr + 3); lp->stats.tx_window_errors += inb(ioaddr + 4); lp->stats.rx_fifo_errors += inb(ioaddr + 5); lp->stats.tx_packets += inb(ioaddr + 6); /* Rx packets */ inb(ioaddr + 7); /* Tx deferrals */ inb(ioaddr + 8); inw(ioaddr + 10); /* Total Rx and Tx octets. */ inw(ioaddr + 12); /* Back to window 1, and turn statistics back on. */ EL3WINDOW(1); outw(StatsEnable, ioaddr + EL3_CMD); return;}static intel3_rx(struct net_device *dev){ struct el3_private *lp = netdev_priv(dev); int ioaddr = dev->base_addr; short rx_status; if (el3_debug > 5) printk(" In rx_packet(), status %4.4x, rx_status %4.4x.\n", inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS)); while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) { if (rx_status & 0x4000) { /* Error, update stats. */ short error = rx_status & 0x3800; outw(RxDiscard, ioaddr + EL3_CMD); lp->stats.rx_errors++; switch (error) { case 0x0000: lp->stats.rx_over_errors++; break; case 0x0800: lp->stats.rx_length_errors++; break; case 0x1000: lp->stats.rx_frame_errors++; break; case 0x1800: lp->stats.rx_length_errors++; break; case 0x2000: lp->stats.rx_frame_errors++; break; case 0x2800: lp->stats.rx_crc_errors++; break; } } else { short pkt_len = rx_status & 0x7ff; struct sk_buff *skb; skb = dev_alloc_skb(pkt_len+5); lp->stats.rx_bytes += pkt_len; if (el3_debug > 4) printk("Receiving packet size %d status %4.4x.\n", pkt_len, rx_status); if (skb != NULL) { skb->dev = dev; skb_reserve(skb, 2); /* Align IP on 16 byte */ /* 'skb->data' points to the start of sk_buff data area. */#ifdef __powerpc__ insl_ns(ioaddr+RX_FIFO, skb_put(skb,pkt_len), (pkt_len + 3) >> 2);#else insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len), (pkt_len + 3) >> 2);#endif outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */ skb->protocol = eth_type_trans(skb,dev); netif_rx(skb); dev->last_rx = jiffies; lp->stats.rx_packets++; continue; } outw(RxDiscard, ioaddr + EL3_CMD); lp->stats.rx_dropped++; if (el3_debug) printk("%s: Couldn't allocate a sk_buff of size %d.\n", dev->name, pkt_len);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?