ALSA: usb-audio: support partially write-protected UAC2 controls
[safe/jmp/linux-2.6] / drivers / net / declance.c
index 4ae0fed..1d973db 100644 (file)
@@ -5,7 +5,7 @@
  *
  *      adopted from sunlance.c by Richard van den Berg
  *
- *      Copyright (C) 2002, 2003, 2005  Maciej W. Rozycki
+ *      Copyright (C) 2002, 2003, 2005, 2006  Maciej W. Rozycki
  *
  *      additional sources:
  *      - PMAD-AA TURBOchannel Ethernet Module Functional Specification,
@@ -44,6 +44,8 @@
  *      v0.010: Fixes for the PMAD mapping of the LANCE buffer and for the
  *              PMAX requirement to only use halfword accesses to the
  *              buffer. macro
+ *
+ *      v0.011: Converted the PMAD to the driver model. macro
  */
 
 #include <linux/crc32.h>
@@ -58,6 +60,7 @@
 #include <linux/spinlock.h>
 #include <linux/stddef.h>
 #include <linux/string.h>
+#include <linux/tc.h>
 #include <linux/types.h>
 
 #include <asm/addrspace.h>
 #include <asm/dec/kn01.h>
 #include <asm/dec/machtype.h>
 #include <asm/dec/system.h>
-#include <asm/dec/tc.h>
 
 static char version[] __devinitdata =
-"declance.c: v0.010 by Linux MIPS DECstation task force\n";
+"declance.c: v0.011 by Linux MIPS DECstation task force\n";
 
 MODULE_AUTHOR("Linux MIPS DECstation task force");
 MODULE_DESCRIPTION("DEC LANCE (DECstation onboard, PMAD-xx) driver");
 MODULE_LICENSE("GPL");
 
+#define __unused __attribute__ ((unused))
+
 /*
  * card types
  */
@@ -246,7 +250,6 @@ struct lance_init_block {
 struct lance_private {
        struct net_device *next;
        int type;
-       int slot;
        int dma_irq;
        volatile struct lance_regs *ll;
 
@@ -255,8 +258,6 @@ struct lance_private {
        int rx_new, tx_new;
        int rx_old, tx_old;
 
-       struct net_device_stats stats;
-
        unsigned short busmaster_regval;
 
        struct timer_list       multicast_timer;
@@ -288,6 +289,7 @@ struct lance_regs {
 
 int dec_lance_debug = 2;
 
+static struct tc_driver dec_lance_tc_driver;
 static struct net_device *root_lance_dev;
 
 static inline void writereg(volatile unsigned short *regptr, short value)
@@ -579,22 +581,22 @@ static int lance_rx(struct net_device *dev)
 
                /* We got an incomplete frame? */
                if ((bits & LE_R1_POK) != LE_R1_POK) {
-                       lp->stats.rx_over_errors++;
-                       lp->stats.rx_errors++;
+                       dev->stats.rx_over_errors++;
+                       dev->stats.rx_errors++;
                } else if (bits & LE_R1_ERR) {
                        /* Count only the end frame as a rx error,
                         * not the beginning
                         */
                        if (bits & LE_R1_BUF)
-                               lp->stats.rx_fifo_errors++;
+                               dev->stats.rx_fifo_errors++;
                        if (bits & LE_R1_CRC)
-                               lp->stats.rx_crc_errors++;
+                               dev->stats.rx_crc_errors++;
                        if (bits & LE_R1_OFL)
-                               lp->stats.rx_over_errors++;
+                               dev->stats.rx_over_errors++;
                        if (bits & LE_R1_FRA)
-                               lp->stats.rx_frame_errors++;
+                               dev->stats.rx_frame_errors++;
                        if (bits & LE_R1_EOP)
-                               lp->stats.rx_errors++;
+                               dev->stats.rx_errors++;
                } else {
                        len = (*rds_ptr(rd, mblength, lp->type) & 0xfff) - 4;
                        skb = dev_alloc_skb(len + 2);
@@ -602,7 +604,7 @@ static int lance_rx(struct net_device *dev)
                        if (skb == 0) {
                                printk("%s: Memory squeeze, deferring packet.\n",
                                       dev->name);
-                               lp->stats.rx_dropped++;
+                               dev->stats.rx_dropped++;
                                *rds_ptr(rd, mblength, lp->type) = 0;
                                *rds_ptr(rd, rmd1, lp->type) =
                                        ((lp->rx_buf_ptr_lnc[entry] >> 16) &
@@ -610,9 +612,8 @@ static int lance_rx(struct net_device *dev)
                                lp->rx_new = (entry + 1) & RX_RING_MOD_MASK;
                                return 0;
                        }
-                       lp->stats.rx_bytes += len;
+                       dev->stats.rx_bytes += len;
 
-                       skb->dev = dev;
                        skb_reserve(skb, 2);    /* 16 byte align */
                        skb_put(skb, len);      /* make room */
 
@@ -621,8 +622,7 @@ static int lance_rx(struct net_device *dev)
 
                        skb->protocol = eth_type_trans(skb, dev);
                        netif_rx(skb);
-                       dev->last_rx = jiffies;
-                       lp->stats.rx_packets++;
+                       dev->stats.rx_packets++;
                }
 
                /* Return the packet to the pool */
@@ -657,14 +657,14 @@ static void lance_tx(struct net_device *dev)
                if (*tds_ptr(td, tmd1, lp->type) & LE_T1_ERR) {
                        status = *tds_ptr(td, misc, lp->type);
 
-                       lp->stats.tx_errors++;
+                       dev->stats.tx_errors++;
                        if (status & LE_T3_RTY)
-                               lp->stats.tx_aborted_errors++;
+                               dev->stats.tx_aborted_errors++;
                        if (status & LE_T3_LCOL)
-                               lp->stats.tx_window_errors++;
+                               dev->stats.tx_window_errors++;
 
                        if (status & LE_T3_CLOS) {
-                               lp->stats.tx_carrier_errors++;
+                               dev->stats.tx_carrier_errors++;
                                printk("%s: Carrier Lost\n", dev->name);
                                /* Stop the lance */
                                writereg(&ll->rap, LE_CSR0);
@@ -678,7 +678,7 @@ static void lance_tx(struct net_device *dev)
                         * transmitter, restart the adapter.
                         */
                        if (status & (LE_T3_BUF | LE_T3_UFL)) {
-                               lp->stats.tx_fifo_errors++;
+                               dev->stats.tx_fifo_errors++;
 
                                printk("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
                                       dev->name);
@@ -699,13 +699,13 @@ static void lance_tx(struct net_device *dev)
 
                        /* One collision before packet was sent. */
                        if (*tds_ptr(td, tmd1, lp->type) & LE_T1_EONE)
-                               lp->stats.collisions++;
+                               dev->stats.collisions++;
 
                        /* More than one collision, be optimistic. */
                        if (*tds_ptr(td, tmd1, lp->type) & LE_T1_EMORE)
-                               lp->stats.collisions += 2;
+                               dev->stats.collisions += 2;
 
-                       lp->stats.tx_packets++;
+                       dev->stats.tx_packets++;
                }
                j = (j + 1) & TX_RING_MOD_MASK;
        }
@@ -718,15 +718,15 @@ out:
        spin_unlock(&lp->lock);
 }
 
-static irqreturn_t lance_dma_merr_int(const int irq, void *dev_id)
+static irqreturn_t lance_dma_merr_int(int irq, void *dev_id)
 {
        struct net_device *dev = dev_id;
 
-       printk("%s: DMA error\n", dev->name);
+       printk(KERN_ERR "%s: DMA error\n", dev->name);
        return IRQ_HANDLED;
 }
 
-static irqreturn_t lance_interrupt(const int irq, void *dev_id)
+static irqreturn_t lance_interrupt(int irq, void *dev_id)
 {
        struct net_device *dev = dev_id;
        struct lance_private *lp = netdev_priv(dev);
@@ -751,10 +751,10 @@ static irqreturn_t lance_interrupt(const int irq, void *dev_id)
                lance_tx(dev);
 
        if (csr0 & LE_C0_BABL)
-               lp->stats.tx_errors++;
+               dev->stats.tx_errors++;
 
        if (csr0 & LE_C0_MISS)
-               lp->stats.rx_errors++;
+               dev->stats.rx_errors++;
 
        if (csr0 & LE_C0_MERR) {
                printk("%s: Memory error, status %04x\n", dev->name, csr0);
@@ -772,8 +772,6 @@ static irqreturn_t lance_interrupt(const int irq, void *dev_id)
        return IRQ_HANDLED;
 }
 
-struct net_device *last_dev = 0;
-
 static int lance_open(struct net_device *dev)
 {
        volatile u16 *ib = (volatile u16 *)dev->mem_start;
@@ -781,8 +779,6 @@ static int lance_open(struct net_device *dev)
        volatile struct lance_regs *ll = lp->ll;
        int status = 0;
 
-       last_dev = dev;
-
        /* Stop the Lance */
        writereg(&ll->rap, LE_CSR0);
        writereg(&ll->rdp, LE_C0_STOP);
@@ -805,14 +801,14 @@ static int lance_open(struct net_device *dev)
        netif_start_queue(dev);
 
        /* Associate IRQ with lance_interrupt */
-       if (request_irq(dev->irq, &lance_interrupt, 0, "lance", dev)) {
+       if (request_irq(dev->irq, lance_interrupt, 0, "lance", dev)) {
                printk("%s: Can't get IRQ %d\n", dev->name, dev->irq);
                return -EAGAIN;
        }
        if (lp->dma_irq >= 0) {
                unsigned long flags;
 
-               if (request_irq(lp->dma_irq, &lance_dma_merr_int, 0,
+               if (request_irq(lp->dma_irq, lance_dma_merr_int, 0,
                                "lance error", dev)) {
                        free_irq(dev->irq, dev);
                        printk("%s: Can't get DMA IRQ %d\n", dev->name,
@@ -878,7 +874,7 @@ static inline int lance_reset(struct net_device *dev)
 
        lance_init_ring(dev);
        load_csrs(lp);
-       dev->trans_start = jiffies;
+       dev->trans_start = jiffies; /* prevent tx timeout */
        status = init_restart_lance(lp);
        return status;
 }
@@ -899,17 +895,20 @@ static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
        struct lance_private *lp = netdev_priv(dev);
        volatile struct lance_regs *ll = lp->ll;
        volatile u16 *ib = (volatile u16 *)dev->mem_start;
+       unsigned long flags;
        int entry, len;
 
        len = skb->len;
 
        if (len < ETH_ZLEN) {
                if (skb_padto(skb, ETH_ZLEN))
-                       return 0;
+                       return NETDEV_TX_OK;
                len = ETH_ZLEN;
        }
 
-       lp->stats.tx_bytes += len;
+       dev->stats.tx_bytes += len;
+
+       spin_lock_irqsave(&lp->lock, flags);
 
        entry = lp->tx_new;
        *lib_ptr(ib, btx_ring[entry].length, lp->type) = (-len);
@@ -929,28 +928,19 @@ static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
        /* Kick the lance: transmit now */
        writereg(&ll->rdp, LE_C0_INEA | LE_C0_TDMD);
 
-       spin_unlock_irq(&lp->lock);
+       spin_unlock_irqrestore(&lp->lock, flags);
 
-       dev->trans_start = jiffies;
        dev_kfree_skb(skb);
 
-       return 0;
-}
-
-static struct net_device_stats *lance_get_stats(struct net_device *dev)
-{
-       struct lance_private *lp = netdev_priv(dev);
-
-       return &lp->stats;
+       return NETDEV_TX_OK;
 }
 
 static void lance_load_multicast(struct net_device *dev)
 {
        struct lance_private *lp = netdev_priv(dev);
        volatile u16 *ib = (volatile u16 *)dev->mem_start;
-       struct dev_mc_list *dmi = dev->mc_list;
+       struct netdev_hw_addr *ha;
        char *addrs;
-       int i;
        u32 crc;
 
        /* set all multicast bits */
@@ -968,9 +958,8 @@ static void lance_load_multicast(struct net_device *dev)
        *lib_ptr(ib, filter[3], lp->type) = 0;
 
        /* Add addresses */
-       for (i = 0; i < dev->mc_count; i++) {
-               addrs = dmi->dmi_addr;
-               dmi = dmi->next;
+       netdev_for_each_mc_addr(ha, dev) {
+               addrs = ha->addr;
 
                /* multicast address? */
                if (!(*addrs & 1))
@@ -980,7 +969,6 @@ static void lance_load_multicast(struct net_device *dev)
                crc = crc >> 26;
                *lib_ptr(ib, filter[crc >> 4], lp->type) |= 1 << (crc & 0xf);
        }
-       return;
 }
 
 static void lance_set_multicast(struct net_device *dev)
@@ -1023,7 +1011,18 @@ static void lance_set_multicast_retry(unsigned long _opaque)
        lance_set_multicast(dev);
 }
 
-static int __init dec_lance_init(const int type, const int slot)
+static const struct net_device_ops lance_netdev_ops = {
+       .ndo_open               = lance_open,
+       .ndo_stop               = lance_close,
+       .ndo_start_xmit         = lance_start_xmit,
+       .ndo_tx_timeout         = lance_tx_timeout,
+       .ndo_set_multicast_list = lance_set_multicast,
+       .ndo_change_mtu         = eth_change_mtu,
+       .ndo_validate_addr      = eth_validate_addr,
+       .ndo_set_mac_address    = eth_mac_addr,
+};
+
+static int __init dec_lance_probe(struct device *bdev, const int type)
 {
        static unsigned version_printed;
        static const char fmt[] = "declance%d";
@@ -1031,6 +1030,7 @@ static int __init dec_lance_init(const int type, const int slot)
        struct net_device *dev;
        struct lance_private *lp;
        volatile struct lance_regs *ll;
+       resource_size_t start = 0, len = 0;
        int i, ret;
        unsigned long esar_base;
        unsigned char *esar;
@@ -1038,14 +1038,18 @@ static int __init dec_lance_init(const int type, const int slot)
        if (dec_lance_debug && version_printed++ == 0)
                printk(version);
 
-       i = 0;
-       dev = root_lance_dev;
-       while (dev) {
-               i++;
-               lp = (struct lance_private *)dev->priv;
-               dev = lp->next;
+       if (bdev)
+               snprintf(name, sizeof(name), "%s", dev_name(bdev));
+       else {
+               i = 0;
+               dev = root_lance_dev;
+               while (dev) {
+                       i++;
+                       lp = netdev_priv(dev);
+                       dev = lp->next;
+               }
+               snprintf(name, sizeof(name), fmt, i);
        }
-       snprintf(name, sizeof(name), fmt, i);
 
        dev = alloc_etherdev(sizeof(struct lance_private));
        if (!dev) {
@@ -1063,7 +1067,6 @@ static int __init dec_lance_init(const int type, const int slot)
        spin_lock_init(&lp->lock);
 
        lp->type = type;
-       lp->slot = slot;
        switch (type) {
        case ASIC_LANCE:
                dev->base_addr = CKSEG1ADDR(dec_kn_slot_base + IOASIC_LANCE);
@@ -1110,12 +1113,22 @@ static int __init dec_lance_init(const int type, const int slot)
                break;
 #ifdef CONFIG_TC
        case PMAD_LANCE:
-               claim_tc_card(slot);
+               dev_set_drvdata(bdev, dev);
+
+               start = to_tc_dev(bdev)->resource.start;
+               len = to_tc_dev(bdev)->resource.end - start + 1;
+               if (!request_mem_region(start, len, dev_name(bdev))) {
+                       printk(KERN_ERR
+                              "%s: Unable to reserve MMIO resource\n",
+                              dev_name(bdev));
+                       ret = -EBUSY;
+                       goto err_out_dev;
+               }
 
-               dev->mem_start = CKSEG1ADDR(get_tc_base_addr(slot));
+               dev->mem_start = CKSEG1ADDR(start);
                dev->mem_end = dev->mem_start + 0x100000;
                dev->base_addr = dev->mem_start + 0x100000;
-               dev->irq = get_tc_irq_nr(slot);
+               dev->irq = to_tc_dev(bdev)->interrupt;
                esar_base = dev->mem_start + 0x1c0002;
                lp->dma_irq = -1;
 
@@ -1174,7 +1187,7 @@ static int __init dec_lance_init(const int type, const int slot)
                printk(KERN_ERR "%s: declance_init called with unknown type\n",
                        name);
                ret = -ENODEV;
-               goto err_out_free_dev;
+               goto err_out_dev;
        }
 
        ll = (struct lance_regs *) dev->base_addr;
@@ -1188,7 +1201,7 @@ static int __init dec_lance_init(const int type, const int slot)
                        "%s: Ethernet station address prom not found!\n",
                        name);
                ret = -ENODEV;
-               goto err_out_free_dev;
+               goto err_out_resource;
        }
        /* Check the prom contents */
        for (i = 0; i < 8; i++) {
@@ -1198,7 +1211,7 @@ static int __init dec_lance_init(const int type, const int slot)
                        printk(KERN_ERR "%s: Something is wrong with the "
                                "ethernet station address prom!\n", name);
                        ret = -ENODEV;
-                       goto err_out_free_dev;
+                       goto err_out_resource;
                }
        }
 
@@ -1208,29 +1221,22 @@ static int __init dec_lance_init(const int type, const int slot)
         */
        switch (type) {
        case ASIC_LANCE:
-               printk("%s: IOASIC onboard LANCE, addr = ", name);
+               printk("%s: IOASIC onboard LANCE", name);
                break;
        case PMAD_LANCE:
-               printk("%s: PMAD-AA, addr = ", name);
+               printk("%s: PMAD-AA", name);
                break;
        case PMAX_LANCE:
-               printk("%s: PMAX onboard LANCE, addr = ", name);
+               printk("%s: PMAX onboard LANCE", name);
                break;
        }
-       for (i = 0; i < 6; i++) {
+       for (i = 0; i < 6; i++)
                dev->dev_addr[i] = esar[i * 4];
-               printk("%2.2x%c", dev->dev_addr[i], i == 5 ? ',' : ':');
-       }
 
-       printk(" irq = %d\n", dev->irq);
+       printk(", addr = %pM, irq = %d\n", dev->dev_addr, dev->irq);
 
-       dev->open = &lance_open;
-       dev->stop = &lance_close;
-       dev->hard_start_xmit = &lance_start_xmit;
-       dev->tx_timeout = &lance_tx_timeout;
+       dev->netdev_ops = &lance_netdev_ops;
        dev->watchdog_timeo = 5*HZ;
-       dev->get_stats = &lance_get_stats;
-       dev->set_multicast_list = &lance_set_multicast;
 
        /* lp->ll is the location of the registers for lance card */
        lp->ll = ll;
@@ -1255,48 +1261,51 @@ static int __init dec_lance_init(const int type, const int slot)
        if (ret) {
                printk(KERN_ERR
                        "%s: Unable to register netdev, aborting.\n", name);
-               goto err_out_free_dev;
+               goto err_out_resource;
        }
 
-       lp->next = root_lance_dev;
-       root_lance_dev = dev;
+       if (!bdev) {
+               lp->next = root_lance_dev;
+               root_lance_dev = dev;
+       }
 
        printk("%s: registered as %s.\n", name, dev->name);
        return 0;
 
-err_out_free_dev:
+err_out_resource:
+       if (bdev)
+               release_mem_region(start, len);
+
+err_out_dev:
        free_netdev(dev);
 
 err_out:
        return ret;
 }
 
+static void __exit dec_lance_remove(struct device *bdev)
+{
+       struct net_device *dev = dev_get_drvdata(bdev);
+       resource_size_t start, len;
+
+       unregister_netdev(dev);
+       start = to_tc_dev(bdev)->resource.start;
+       len = to_tc_dev(bdev)->resource.end - start + 1;
+       release_mem_region(start, len);
+       free_netdev(dev);
+}
 
 /* Find all the lance cards on the system and initialize them */
-static int __init dec_lance_probe(void)
+static int __init dec_lance_platform_probe(void)
 {
        int count = 0;
 
-       /* Scan slots for PMAD-AA cards first. */
-#ifdef CONFIG_TC
-       if (TURBOCHANNEL) {
-               int slot;
-
-               while ((slot = search_tc_card("PMAD-AA")) >= 0) {
-                       if (dec_lance_init(PMAD_LANCE, slot) < 0)
-                               break;
-                       count++;
-               }
-       }
-#endif
-
-       /* Then handle onboard devices. */
        if (dec_interrupt[DEC_IRQ_LANCE] >= 0) {
                if (dec_interrupt[DEC_IRQ_LANCE_MERR] >= 0) {
-                       if (dec_lance_init(ASIC_LANCE, -1) >= 0)
+                       if (dec_lance_probe(NULL, ASIC_LANCE) >= 0)
                                count++;
                } else if (!TURBOCHANNEL) {
-                       if (dec_lance_init(PMAX_LANCE, -1) >= 0)
+                       if (dec_lance_probe(NULL, PMAX_LANCE) >= 0)
                                count++;
                }
        }
@@ -1304,21 +1313,70 @@ static int __init dec_lance_probe(void)
        return (count > 0) ? 0 : -ENODEV;
 }
 
-static void __exit dec_lance_cleanup(void)
+static void __exit dec_lance_platform_remove(void)
 {
        while (root_lance_dev) {
                struct net_device *dev = root_lance_dev;
                struct lance_private *lp = netdev_priv(dev);
 
                unregister_netdev(dev);
-#ifdef CONFIG_TC
-               if (lp->slot >= 0)
-                       release_tc_card(lp->slot);
-#endif
                root_lance_dev = lp->next;
                free_netdev(dev);
        }
 }
 
-module_init(dec_lance_probe);
-module_exit(dec_lance_cleanup);
+#ifdef CONFIG_TC
+static int __init dec_lance_tc_probe(struct device *dev);
+static int __exit dec_lance_tc_remove(struct device *dev);
+
+static const struct tc_device_id dec_lance_tc_table[] = {
+       { "DEC     ", "PMAD-AA " },
+       { }
+};
+MODULE_DEVICE_TABLE(tc, dec_lance_tc_table);
+
+static struct tc_driver dec_lance_tc_driver = {
+       .id_table       = dec_lance_tc_table,
+       .driver         = {
+               .name   = "declance",
+               .bus    = &tc_bus_type,
+               .probe  = dec_lance_tc_probe,
+               .remove = __exit_p(dec_lance_tc_remove),
+       },
+};
+
+static int __init dec_lance_tc_probe(struct device *dev)
+{
+        int status = dec_lance_probe(dev, PMAD_LANCE);
+        if (!status)
+                get_device(dev);
+        return status;
+}
+
+static int __exit dec_lance_tc_remove(struct device *dev)
+{
+        put_device(dev);
+        dec_lance_remove(dev);
+        return 0;
+}
+#endif
+
+static int __init dec_lance_init(void)
+{
+       int status;
+
+       status = tc_register_driver(&dec_lance_tc_driver);
+       if (!status)
+               dec_lance_platform_probe();
+       return status;
+}
+
+static void __exit dec_lance_exit(void)
+{
+       dec_lance_platform_remove();
+       tc_unregister_driver(&dec_lance_tc_driver);
+}
+
+
+module_init(dec_lance_init);
+module_exit(dec_lance_exit);