Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * CoreChip-sz SR9700 one chip USB 1.1 Ethernet Devices
0003  *
0004  * Author : Liu Junliang <liujunliang_ljl@163.com>
0005  *
0006  * Based on dm9601.c
0007  *
0008  * This file is licensed under the terms of the GNU General Public License
0009  * version 2.  This program is licensed "as is" without any warranty of any
0010  * kind, whether express or implied.
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/sched.h>
0015 #include <linux/stddef.h>
0016 #include <linux/netdevice.h>
0017 #include <linux/etherdevice.h>
0018 #include <linux/ethtool.h>
0019 #include <linux/mii.h>
0020 #include <linux/usb.h>
0021 #include <linux/crc32.h>
0022 #include <linux/usb/usbnet.h>
0023 
0024 #include "sr9700.h"
0025 
0026 static int sr_read(struct usbnet *dev, u8 reg, u16 length, void *data)
0027 {
0028     int err;
0029 
0030     err = usbnet_read_cmd(dev, SR_RD_REGS, SR_REQ_RD_REG, 0, reg, data,
0031                   length);
0032     if ((err != length) && (err >= 0))
0033         err = -EINVAL;
0034     return err;
0035 }
0036 
0037 static int sr_write(struct usbnet *dev, u8 reg, u16 length, void *data)
0038 {
0039     int err;
0040 
0041     err = usbnet_write_cmd(dev, SR_WR_REGS, SR_REQ_WR_REG, 0, reg, data,
0042                    length);
0043     if ((err >= 0) && (err < length))
0044         err = -EINVAL;
0045     return err;
0046 }
0047 
0048 static int sr_read_reg(struct usbnet *dev, u8 reg, u8 *value)
0049 {
0050     return sr_read(dev, reg, 1, value);
0051 }
0052 
0053 static int sr_write_reg(struct usbnet *dev, u8 reg, u8 value)
0054 {
0055     return usbnet_write_cmd(dev, SR_WR_REGS, SR_REQ_WR_REG,
0056                 value, reg, NULL, 0);
0057 }
0058 
0059 static void sr_write_async(struct usbnet *dev, u8 reg, u16 length,
0060                const void *data)
0061 {
0062     usbnet_write_cmd_async(dev, SR_WR_REGS, SR_REQ_WR_REG,
0063                    0, reg, data, length);
0064 }
0065 
0066 static void sr_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
0067 {
0068     usbnet_write_cmd_async(dev, SR_WR_REGS, SR_REQ_WR_REG,
0069                    value, reg, NULL, 0);
0070 }
0071 
0072 static int wait_phy_eeprom_ready(struct usbnet *dev, int phy)
0073 {
0074     int i;
0075 
0076     for (i = 0; i < SR_SHARE_TIMEOUT; i++) {
0077         u8 tmp = 0;
0078         int ret;
0079 
0080         udelay(1);
0081         ret = sr_read_reg(dev, SR_EPCR, &tmp);
0082         if (ret < 0)
0083             return ret;
0084 
0085         /* ready */
0086         if (!(tmp & EPCR_ERRE))
0087             return 0;
0088     }
0089 
0090     netdev_err(dev->net, "%s write timed out!\n", phy ? "phy" : "eeprom");
0091 
0092     return -EIO;
0093 }
0094 
0095 static int sr_share_read_word(struct usbnet *dev, int phy, u8 reg,
0096                   __le16 *value)
0097 {
0098     int ret;
0099 
0100     mutex_lock(&dev->phy_mutex);
0101 
0102     sr_write_reg(dev, SR_EPAR, phy ? (reg | EPAR_PHY_ADR) : reg);
0103     sr_write_reg(dev, SR_EPCR, phy ? (EPCR_EPOS | EPCR_ERPRR) : EPCR_ERPRR);
0104 
0105     ret = wait_phy_eeprom_ready(dev, phy);
0106     if (ret < 0)
0107         goto out_unlock;
0108 
0109     sr_write_reg(dev, SR_EPCR, 0x0);
0110     ret = sr_read(dev, SR_EPDR, 2, value);
0111 
0112     netdev_dbg(dev->net, "read shared %d 0x%02x returned 0x%04x, %d\n",
0113            phy, reg, *value, ret);
0114 
0115 out_unlock:
0116     mutex_unlock(&dev->phy_mutex);
0117     return ret;
0118 }
0119 
0120 static int sr_share_write_word(struct usbnet *dev, int phy, u8 reg,
0121                    __le16 value)
0122 {
0123     int ret;
0124 
0125     mutex_lock(&dev->phy_mutex);
0126 
0127     ret = sr_write(dev, SR_EPDR, 2, &value);
0128     if (ret < 0)
0129         goto out_unlock;
0130 
0131     sr_write_reg(dev, SR_EPAR, phy ? (reg | EPAR_PHY_ADR) : reg);
0132     sr_write_reg(dev, SR_EPCR, phy ? (EPCR_WEP | EPCR_EPOS | EPCR_ERPRW) :
0133             (EPCR_WEP | EPCR_ERPRW));
0134 
0135     ret = wait_phy_eeprom_ready(dev, phy);
0136     if (ret < 0)
0137         goto out_unlock;
0138 
0139     sr_write_reg(dev, SR_EPCR, 0x0);
0140 
0141 out_unlock:
0142     mutex_unlock(&dev->phy_mutex);
0143     return ret;
0144 }
0145 
0146 static int sr_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
0147 {
0148     return sr_share_read_word(dev, 0, offset, value);
0149 }
0150 
0151 static int sr9700_get_eeprom_len(struct net_device *netdev)
0152 {
0153     return SR_EEPROM_LEN;
0154 }
0155 
0156 static int sr9700_get_eeprom(struct net_device *netdev,
0157                  struct ethtool_eeprom *eeprom, u8 *data)
0158 {
0159     struct usbnet *dev = netdev_priv(netdev);
0160     __le16 *buf = (__le16 *)data;
0161     int ret = 0;
0162     int i;
0163 
0164     /* access is 16bit */
0165     if ((eeprom->offset & 0x01) || (eeprom->len & 0x01))
0166         return -EINVAL;
0167 
0168     for (i = 0; i < eeprom->len / 2; i++) {
0169         ret = sr_read_eeprom_word(dev, eeprom->offset / 2 + i, buf + i);
0170         if (ret < 0)
0171             break;
0172     }
0173 
0174     return ret;
0175 }
0176 
0177 static int sr_mdio_read(struct net_device *netdev, int phy_id, int loc)
0178 {
0179     struct usbnet *dev = netdev_priv(netdev);
0180     __le16 res;
0181     int rc = 0;
0182 
0183     if (phy_id) {
0184         netdev_dbg(netdev, "Only internal phy supported\n");
0185         return 0;
0186     }
0187 
0188     /* Access NSR_LINKST bit for link status instead of MII_BMSR */
0189     if (loc == MII_BMSR) {
0190         u8 value;
0191 
0192         sr_read_reg(dev, SR_NSR, &value);
0193         if (value & NSR_LINKST)
0194             rc = 1;
0195     }
0196     sr_share_read_word(dev, 1, loc, &res);
0197     if (rc == 1)
0198         res = le16_to_cpu(res) | BMSR_LSTATUS;
0199     else
0200         res = le16_to_cpu(res) & ~BMSR_LSTATUS;
0201 
0202     netdev_dbg(netdev, "sr_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
0203            phy_id, loc, res);
0204 
0205     return res;
0206 }
0207 
0208 static void sr_mdio_write(struct net_device *netdev, int phy_id, int loc,
0209               int val)
0210 {
0211     struct usbnet *dev = netdev_priv(netdev);
0212     __le16 res = cpu_to_le16(val);
0213 
0214     if (phy_id) {
0215         netdev_dbg(netdev, "Only internal phy supported\n");
0216         return;
0217     }
0218 
0219     netdev_dbg(netdev, "sr_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
0220            phy_id, loc, val);
0221 
0222     sr_share_write_word(dev, 1, loc, res);
0223 }
0224 
0225 static u32 sr9700_get_link(struct net_device *netdev)
0226 {
0227     struct usbnet *dev = netdev_priv(netdev);
0228     u8 value = 0;
0229     int rc = 0;
0230 
0231     /* Get the Link Status directly */
0232     sr_read_reg(dev, SR_NSR, &value);
0233     if (value & NSR_LINKST)
0234         rc = 1;
0235 
0236     return rc;
0237 }
0238 
0239 static int sr9700_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
0240 {
0241     struct usbnet *dev = netdev_priv(netdev);
0242 
0243     return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
0244 }
0245 
0246 static const struct ethtool_ops sr9700_ethtool_ops = {
0247     .get_drvinfo    = usbnet_get_drvinfo,
0248     .get_link   = sr9700_get_link,
0249     .get_msglevel   = usbnet_get_msglevel,
0250     .set_msglevel   = usbnet_set_msglevel,
0251     .get_eeprom_len = sr9700_get_eeprom_len,
0252     .get_eeprom = sr9700_get_eeprom,
0253     .nway_reset = usbnet_nway_reset,
0254     .get_link_ksettings = usbnet_get_link_ksettings_mii,
0255     .set_link_ksettings = usbnet_set_link_ksettings_mii,
0256 };
0257 
0258 static void sr9700_set_multicast(struct net_device *netdev)
0259 {
0260     struct usbnet *dev = netdev_priv(netdev);
0261     /* We use the 20 byte dev->data for our 8 byte filter buffer
0262      * to avoid allocating memory that is tricky to free later
0263      */
0264     u8 *hashes = (u8 *)&dev->data;
0265     /* rx_ctl setting : enable, disable_long, disable_crc */
0266     u8 rx_ctl = RCR_RXEN | RCR_DIS_CRC | RCR_DIS_LONG;
0267 
0268     memset(hashes, 0x00, SR_MCAST_SIZE);
0269     /* broadcast address */
0270     hashes[SR_MCAST_SIZE - 1] |= SR_MCAST_ADDR_FLAG;
0271     if (netdev->flags & IFF_PROMISC) {
0272         rx_ctl |= RCR_PRMSC;
0273     } else if (netdev->flags & IFF_ALLMULTI ||
0274            netdev_mc_count(netdev) > SR_MCAST_MAX) {
0275         rx_ctl |= RCR_RUNT;
0276     } else if (!netdev_mc_empty(netdev)) {
0277         struct netdev_hw_addr *ha;
0278 
0279         netdev_for_each_mc_addr(ha, netdev) {
0280             u32 crc = ether_crc(ETH_ALEN, ha->addr) >> 26;
0281             hashes[crc >> 3] |= 1 << (crc & 0x7);
0282         }
0283     }
0284 
0285     sr_write_async(dev, SR_MAR, SR_MCAST_SIZE, hashes);
0286     sr_write_reg_async(dev, SR_RCR, rx_ctl);
0287 }
0288 
0289 static int sr9700_set_mac_address(struct net_device *netdev, void *p)
0290 {
0291     struct usbnet *dev = netdev_priv(netdev);
0292     struct sockaddr *addr = p;
0293 
0294     if (!is_valid_ether_addr(addr->sa_data)) {
0295         netdev_err(netdev, "not setting invalid mac address %pM\n",
0296                addr->sa_data);
0297         return -EINVAL;
0298     }
0299 
0300     eth_hw_addr_set(netdev, addr->sa_data);
0301     sr_write_async(dev, SR_PAR, 6, netdev->dev_addr);
0302 
0303     return 0;
0304 }
0305 
0306 static const struct net_device_ops sr9700_netdev_ops = {
0307     .ndo_open       = usbnet_open,
0308     .ndo_stop       = usbnet_stop,
0309     .ndo_start_xmit     = usbnet_start_xmit,
0310     .ndo_tx_timeout     = usbnet_tx_timeout,
0311     .ndo_change_mtu     = usbnet_change_mtu,
0312     .ndo_get_stats64    = dev_get_tstats64,
0313     .ndo_validate_addr  = eth_validate_addr,
0314     .ndo_eth_ioctl      = sr9700_ioctl,
0315     .ndo_set_rx_mode    = sr9700_set_multicast,
0316     .ndo_set_mac_address    = sr9700_set_mac_address,
0317 };
0318 
0319 static int sr9700_bind(struct usbnet *dev, struct usb_interface *intf)
0320 {
0321     struct net_device *netdev;
0322     struct mii_if_info *mii;
0323     u8 addr[ETH_ALEN];
0324     int ret;
0325 
0326     ret = usbnet_get_endpoints(dev, intf);
0327     if (ret)
0328         goto out;
0329 
0330     netdev = dev->net;
0331 
0332     netdev->netdev_ops = &sr9700_netdev_ops;
0333     netdev->ethtool_ops = &sr9700_ethtool_ops;
0334     netdev->hard_header_len += SR_TX_OVERHEAD;
0335     dev->hard_mtu = netdev->mtu + netdev->hard_header_len;
0336     /* bulkin buffer is preferably not less than 3K */
0337     dev->rx_urb_size = 3072;
0338 
0339     mii = &dev->mii;
0340     mii->dev = netdev;
0341     mii->mdio_read = sr_mdio_read;
0342     mii->mdio_write = sr_mdio_write;
0343     mii->phy_id_mask = 0x1f;
0344     mii->reg_num_mask = 0x1f;
0345 
0346     sr_write_reg(dev, SR_NCR, NCR_RST);
0347     udelay(20);
0348 
0349     /* read MAC
0350      * After Chip Power on, the Chip will reload the MAC from
0351      * EEPROM automatically to PAR. In case there is no EEPROM externally,
0352      * a default MAC address is stored in PAR for making chip work properly.
0353      */
0354     if (sr_read(dev, SR_PAR, ETH_ALEN, addr) < 0) {
0355         netdev_err(netdev, "Error reading MAC address\n");
0356         ret = -ENODEV;
0357         goto out;
0358     }
0359     eth_hw_addr_set(netdev, addr);
0360 
0361     /* power up and reset phy */
0362     sr_write_reg(dev, SR_PRR, PRR_PHY_RST);
0363     /* at least 10ms, here 20ms for safe */
0364     msleep(20);
0365     sr_write_reg(dev, SR_PRR, 0);
0366     /* at least 1ms, here 2ms for reading right register */
0367     udelay(2 * 1000);
0368 
0369     /* receive broadcast packets */
0370     sr9700_set_multicast(netdev);
0371 
0372     sr_mdio_write(netdev, mii->phy_id, MII_BMCR, BMCR_RESET);
0373     sr_mdio_write(netdev, mii->phy_id, MII_ADVERTISE, ADVERTISE_ALL |
0374               ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
0375     mii_nway_restart(mii);
0376 
0377 out:
0378     return ret;
0379 }
0380 
0381 static int sr9700_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
0382 {
0383     struct sk_buff *sr_skb;
0384     int len;
0385 
0386     /* skb content (packets) format :
0387      *                    p0            p1            p2    ......    pm
0388      *                 /      \
0389      *            /                \
0390      *        /                            \
0391      *  /                                        \
0392      * p0b0 p0b1 p0b2 p0b3 ...... p0b(n-4) p0b(n-3)...p0bn
0393      *
0394      * p0 : packet 0
0395      * p0b0 : packet 0 byte 0
0396      *
0397      * b0: rx status
0398      * b1: packet length (incl crc) low
0399      * b2: packet length (incl crc) high
0400      * b3..n-4: packet data
0401      * bn-3..bn: ethernet packet crc
0402      */
0403     if (unlikely(skb->len < SR_RX_OVERHEAD)) {
0404         netdev_err(dev->net, "unexpected tiny rx frame\n");
0405         return 0;
0406     }
0407 
0408     /* one skb may contains multiple packets */
0409     while (skb->len > SR_RX_OVERHEAD) {
0410         if (skb->data[0] != 0x40)
0411             return 0;
0412 
0413         /* ignore the CRC length */
0414         len = (skb->data[1] | (skb->data[2] << 8)) - 4;
0415 
0416         if (len > ETH_FRAME_LEN || len > skb->len)
0417             return 0;
0418 
0419         /* the last packet of current skb */
0420         if (skb->len == (len + SR_RX_OVERHEAD)) {
0421             skb_pull(skb, 3);
0422             skb->len = len;
0423             skb_set_tail_pointer(skb, len);
0424             skb->truesize = len + sizeof(struct sk_buff);
0425             return 2;
0426         }
0427 
0428         /* skb_clone is used for address align */
0429         sr_skb = skb_clone(skb, GFP_ATOMIC);
0430         if (!sr_skb)
0431             return 0;
0432 
0433         sr_skb->len = len;
0434         sr_skb->data = skb->data + 3;
0435         skb_set_tail_pointer(sr_skb, len);
0436         sr_skb->truesize = len + sizeof(struct sk_buff);
0437         usbnet_skb_return(dev, sr_skb);
0438 
0439         skb_pull(skb, len + SR_RX_OVERHEAD);
0440     }
0441 
0442     return 0;
0443 }
0444 
0445 static struct sk_buff *sr9700_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
0446                        gfp_t flags)
0447 {
0448     int len;
0449 
0450     /* SR9700 can only send out one ethernet packet at once.
0451      *
0452      * b0 b1 b2 b3 ...... b(n-4) b(n-3)...bn
0453      *
0454      * b0: rx status
0455      * b1: packet length (incl crc) low
0456      * b2: packet length (incl crc) high
0457      * b3..n-4: packet data
0458      * bn-3..bn: ethernet packet crc
0459      */
0460 
0461     len = skb->len;
0462 
0463     if (skb_cow_head(skb, SR_TX_OVERHEAD)) {
0464         dev_kfree_skb_any(skb);
0465         return NULL;
0466     }
0467 
0468     __skb_push(skb, SR_TX_OVERHEAD);
0469 
0470     /* usbnet adds padding if length is a multiple of packet size
0471      * if so, adjust length value in header
0472      */
0473     if ((skb->len % dev->maxpacket) == 0)
0474         len++;
0475 
0476     skb->data[0] = len;
0477     skb->data[1] = len >> 8;
0478 
0479     return skb;
0480 }
0481 
0482 static void sr9700_status(struct usbnet *dev, struct urb *urb)
0483 {
0484     int link;
0485     u8 *buf;
0486 
0487     /* format:
0488        b0: net status
0489        b1: tx status 1
0490        b2: tx status 2
0491        b3: rx status
0492        b4: rx overflow
0493        b5: rx count
0494        b6: tx count
0495        b7: gpr
0496     */
0497 
0498     if (urb->actual_length < 8)
0499         return;
0500 
0501     buf = urb->transfer_buffer;
0502 
0503     link = !!(buf[0] & 0x40);
0504     if (netif_carrier_ok(dev->net) != link) {
0505         usbnet_link_change(dev, link, 1);
0506         netdev_dbg(dev->net, "Link Status is: %d\n", link);
0507     }
0508 }
0509 
0510 static int sr9700_link_reset(struct usbnet *dev)
0511 {
0512     struct ethtool_cmd ecmd;
0513 
0514     mii_check_media(&dev->mii, 1, 1);
0515     mii_ethtool_gset(&dev->mii, &ecmd);
0516 
0517     netdev_dbg(dev->net, "link_reset() speed: %d duplex: %d\n",
0518            ecmd.speed, ecmd.duplex);
0519 
0520     return 0;
0521 }
0522 
0523 static const struct driver_info sr9700_driver_info = {
0524     .description    = "CoreChip SR9700 USB Ethernet",
0525     .flags      = FLAG_ETHER,
0526     .bind       = sr9700_bind,
0527     .rx_fixup   = sr9700_rx_fixup,
0528     .tx_fixup   = sr9700_tx_fixup,
0529     .status     = sr9700_status,
0530     .link_reset = sr9700_link_reset,
0531     .reset      = sr9700_link_reset,
0532 };
0533 
0534 static const struct usb_device_id products[] = {
0535     {
0536         USB_DEVICE(0x0fe6, 0x9700), /* SR9700 device */
0537         .driver_info = (unsigned long)&sr9700_driver_info,
0538     },
0539     {},         /* END */
0540 };
0541 
0542 MODULE_DEVICE_TABLE(usb, products);
0543 
0544 static struct usb_driver sr9700_usb_driver = {
0545     .name       = "sr9700",
0546     .id_table   = products,
0547     .probe      = usbnet_probe,
0548     .disconnect = usbnet_disconnect,
0549     .suspend    = usbnet_suspend,
0550     .resume     = usbnet_resume,
0551     .disable_hub_initiated_lpm = 1,
0552 };
0553 
0554 module_usb_driver(sr9700_usb_driver);
0555 
0556 MODULE_AUTHOR("liujl <liujunliang_ljl@163.com>");
0557 MODULE_DESCRIPTION("SR9700 one chip USB 1.1 USB to Ethernet device from http://www.corechip-sz.com/");
0558 MODULE_LICENSE("GPL");