Back to home page

OSCL-LXR

 
 

    


0001 /* CoreChip-sz SR9800 one chip USB 2.0 Ethernet Devices
0002  *
0003  * Author : Liu Junliang <liujunliang_ljl@163.com>
0004  *
0005  * Based on asix_common.c, asix_devices.c
0006  *
0007  * This file is licensed under the terms of the GNU General Public License
0008  * version 2.  This program is licensed "as is" without any warranty of any
0009  * kind, whether express or implied.*
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/kmod.h>
0014 #include <linux/init.h>
0015 #include <linux/netdevice.h>
0016 #include <linux/etherdevice.h>
0017 #include <linux/ethtool.h>
0018 #include <linux/workqueue.h>
0019 #include <linux/mii.h>
0020 #include <linux/usb.h>
0021 #include <linux/crc32.h>
0022 #include <linux/usb/usbnet.h>
0023 #include <linux/slab.h>
0024 #include <linux/if_vlan.h>
0025 
0026 #include "sr9800.h"
0027 
0028 static int sr_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
0029                 u16 size, void *data)
0030 {
0031     int err;
0032 
0033     err = usbnet_read_cmd(dev, cmd, SR_REQ_RD_REG, value, index,
0034                   data, size);
0035     if ((err != size) && (err >= 0))
0036         err = -EINVAL;
0037 
0038     return err;
0039 }
0040 
0041 static int sr_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
0042                  u16 size, void *data)
0043 {
0044     int err;
0045 
0046     err = usbnet_write_cmd(dev, cmd, SR_REQ_WR_REG, value, index,
0047                   data, size);
0048     if ((err != size) && (err >= 0))
0049         err = -EINVAL;
0050 
0051     return err;
0052 }
0053 
0054 static void
0055 sr_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
0056            u16 size, void *data)
0057 {
0058     usbnet_write_cmd_async(dev, cmd, SR_REQ_WR_REG, value, index, data,
0059                    size);
0060 }
0061 
0062 static int sr_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
0063 {
0064     int offset = 0;
0065 
0066     /* This check is no longer done by usbnet */
0067     if (skb->len < dev->net->hard_header_len)
0068         return 0;
0069 
0070     while (offset + sizeof(u32) < skb->len) {
0071         struct sk_buff *sr_skb;
0072         u16 size;
0073         u32 header = get_unaligned_le32(skb->data + offset);
0074 
0075         offset += sizeof(u32);
0076         /* get the packet length */
0077         size = (u16) (header & 0x7ff);
0078         if (size != ((~header >> 16) & 0x07ff)) {
0079             netdev_err(dev->net, "%s : Bad Header Length\n",
0080                    __func__);
0081             return 0;
0082         }
0083 
0084         if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
0085             (size + offset > skb->len)) {
0086             netdev_err(dev->net, "%s : Bad RX Length %d\n",
0087                    __func__, size);
0088             return 0;
0089         }
0090         sr_skb = netdev_alloc_skb_ip_align(dev->net, size);
0091         if (!sr_skb)
0092             return 0;
0093 
0094         skb_put(sr_skb, size);
0095         memcpy(sr_skb->data, skb->data + offset, size);
0096         usbnet_skb_return(dev, sr_skb);
0097 
0098         offset += (size + 1) & 0xfffe;
0099     }
0100 
0101     if (skb->len != offset) {
0102         netdev_err(dev->net, "%s : Bad SKB Length %d\n", __func__,
0103                skb->len);
0104         return 0;
0105     }
0106 
0107     return 1;
0108 }
0109 
0110 static struct sk_buff *sr_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
0111                     gfp_t flags)
0112 {
0113     int headroom = skb_headroom(skb);
0114     int tailroom = skb_tailroom(skb);
0115     u32 padbytes = 0xffff0000;
0116     u32 packet_len;
0117     int padlen;
0118     void *ptr;
0119 
0120     padlen = ((skb->len + 4) % (dev->maxpacket - 1)) ? 0 : 4;
0121 
0122     if ((!skb_cloned(skb)) && ((headroom + tailroom) >= (4 + padlen))) {
0123         if ((headroom < 4) || (tailroom < padlen)) {
0124             skb->data = memmove(skb->head + 4, skb->data,
0125                         skb->len);
0126             skb_set_tail_pointer(skb, skb->len);
0127         }
0128     } else {
0129         struct sk_buff *skb2;
0130         skb2 = skb_copy_expand(skb, 4, padlen, flags);
0131         dev_kfree_skb_any(skb);
0132         skb = skb2;
0133         if (!skb)
0134             return NULL;
0135     }
0136 
0137     ptr = skb_push(skb, 4);
0138     packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
0139     put_unaligned_le32(packet_len, ptr);
0140 
0141     if (padlen) {
0142         put_unaligned_le32(padbytes, skb_tail_pointer(skb));
0143         skb_put(skb, sizeof(padbytes));
0144     }
0145 
0146     usbnet_set_skb_tx_stats(skb, 1, 0);
0147     return skb;
0148 }
0149 
0150 static void sr_status(struct usbnet *dev, struct urb *urb)
0151 {
0152     struct sr9800_int_data *event;
0153     int link;
0154 
0155     if (urb->actual_length < 8)
0156         return;
0157 
0158     event = urb->transfer_buffer;
0159     link = event->link & 0x01;
0160     if (netif_carrier_ok(dev->net) != link) {
0161         usbnet_link_change(dev, link, 1);
0162         netdev_dbg(dev->net, "Link Status is: %d\n", link);
0163     }
0164 
0165     return;
0166 }
0167 
0168 static inline int sr_set_sw_mii(struct usbnet *dev)
0169 {
0170     int ret;
0171 
0172     ret = sr_write_cmd(dev, SR_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
0173     if (ret < 0)
0174         netdev_err(dev->net, "Failed to enable software MII access\n");
0175     return ret;
0176 }
0177 
0178 static inline int sr_set_hw_mii(struct usbnet *dev)
0179 {
0180     int ret;
0181 
0182     ret = sr_write_cmd(dev, SR_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
0183     if (ret < 0)
0184         netdev_err(dev->net, "Failed to enable hardware MII access\n");
0185     return ret;
0186 }
0187 
0188 static inline int sr_get_phy_addr(struct usbnet *dev)
0189 {
0190     u8 buf[2];
0191     int ret;
0192 
0193     ret = sr_read_cmd(dev, SR_CMD_READ_PHY_ID, 0, 0, 2, buf);
0194     if (ret < 0) {
0195         netdev_err(dev->net, "%s : Error reading PHYID register:%02x\n",
0196                __func__, ret);
0197         goto out;
0198     }
0199     netdev_dbg(dev->net, "%s : returning 0x%04x\n", __func__,
0200            *((__le16 *)buf));
0201 
0202     ret = buf[1];
0203 
0204 out:
0205     return ret;
0206 }
0207 
0208 static int sr_sw_reset(struct usbnet *dev, u8 flags)
0209 {
0210     int ret;
0211 
0212     ret = sr_write_cmd(dev, SR_CMD_SW_RESET, flags, 0, 0, NULL);
0213     if (ret < 0)
0214         netdev_err(dev->net, "Failed to send software reset:%02x\n",
0215                ret);
0216 
0217     return ret;
0218 }
0219 
0220 static u16 sr_read_rx_ctl(struct usbnet *dev)
0221 {
0222     __le16 v;
0223     int ret;
0224 
0225     ret = sr_read_cmd(dev, SR_CMD_READ_RX_CTL, 0, 0, 2, &v);
0226     if (ret < 0) {
0227         netdev_err(dev->net, "Error reading RX_CTL register:%02x\n",
0228                ret);
0229         goto out;
0230     }
0231 
0232     ret = le16_to_cpu(v);
0233 out:
0234     return ret;
0235 }
0236 
0237 static int sr_write_rx_ctl(struct usbnet *dev, u16 mode)
0238 {
0239     int ret;
0240 
0241     netdev_dbg(dev->net, "%s : mode = 0x%04x\n", __func__, mode);
0242     ret = sr_write_cmd(dev, SR_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
0243     if (ret < 0)
0244         netdev_err(dev->net,
0245                "Failed to write RX_CTL mode to 0x%04x:%02x\n",
0246                mode, ret);
0247 
0248     return ret;
0249 }
0250 
0251 static u16 sr_read_medium_status(struct usbnet *dev)
0252 {
0253     __le16 v;
0254     int ret;
0255 
0256     ret = sr_read_cmd(dev, SR_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
0257     if (ret < 0) {
0258         netdev_err(dev->net,
0259                "Error reading Medium Status register:%02x\n", ret);
0260         return ret; /* TODO: callers not checking for error ret */
0261     }
0262 
0263     return le16_to_cpu(v);
0264 }
0265 
0266 static int sr_write_medium_mode(struct usbnet *dev, u16 mode)
0267 {
0268     int ret;
0269 
0270     netdev_dbg(dev->net, "%s : mode = 0x%04x\n", __func__, mode);
0271     ret = sr_write_cmd(dev, SR_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
0272     if (ret < 0)
0273         netdev_err(dev->net,
0274                "Failed to write Medium Mode mode to 0x%04x:%02x\n",
0275                mode, ret);
0276     return ret;
0277 }
0278 
0279 static int sr_write_gpio(struct usbnet *dev, u16 value, int sleep)
0280 {
0281     int ret;
0282 
0283     netdev_dbg(dev->net, "%s : value = 0x%04x\n", __func__, value);
0284     ret = sr_write_cmd(dev, SR_CMD_WRITE_GPIOS, value, 0, 0, NULL);
0285     if (ret < 0)
0286         netdev_err(dev->net, "Failed to write GPIO value 0x%04x:%02x\n",
0287                value, ret);
0288     if (sleep)
0289         msleep(sleep);
0290 
0291     return ret;
0292 }
0293 
0294 /* SR9800 have a 16-bit RX_CTL value */
0295 static void sr_set_multicast(struct net_device *net)
0296 {
0297     struct usbnet *dev = netdev_priv(net);
0298     struct sr_data *data = (struct sr_data *)&dev->data;
0299     u16 rx_ctl = SR_DEFAULT_RX_CTL;
0300 
0301     if (net->flags & IFF_PROMISC) {
0302         rx_ctl |= SR_RX_CTL_PRO;
0303     } else if (net->flags & IFF_ALLMULTI ||
0304            netdev_mc_count(net) > SR_MAX_MCAST) {
0305         rx_ctl |= SR_RX_CTL_AMALL;
0306     } else if (netdev_mc_empty(net)) {
0307         /* just broadcast and directed */
0308     } else {
0309         /* We use the 20 byte dev->data
0310          * for our 8 byte filter buffer
0311          * to avoid allocating memory that
0312          * is tricky to free later
0313          */
0314         struct netdev_hw_addr *ha;
0315         u32 crc_bits;
0316 
0317         memset(data->multi_filter, 0, SR_MCAST_FILTER_SIZE);
0318 
0319         /* Build the multicast hash filter. */
0320         netdev_for_each_mc_addr(ha, net) {
0321             crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
0322             data->multi_filter[crc_bits >> 3] |=
0323                 1 << (crc_bits & 7);
0324         }
0325 
0326         sr_write_cmd_async(dev, SR_CMD_WRITE_MULTI_FILTER, 0, 0,
0327                    SR_MCAST_FILTER_SIZE, data->multi_filter);
0328 
0329         rx_ctl |= SR_RX_CTL_AM;
0330     }
0331 
0332     sr_write_cmd_async(dev, SR_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
0333 }
0334 
0335 static int sr_mdio_read(struct net_device *net, int phy_id, int loc)
0336 {
0337     struct usbnet *dev = netdev_priv(net);
0338     __le16 res = 0;
0339 
0340     mutex_lock(&dev->phy_mutex);
0341     sr_set_sw_mii(dev);
0342     sr_read_cmd(dev, SR_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, &res);
0343     sr_set_hw_mii(dev);
0344     mutex_unlock(&dev->phy_mutex);
0345 
0346     netdev_dbg(dev->net,
0347            "%s : phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", __func__,
0348            phy_id, loc, le16_to_cpu(res));
0349 
0350     return le16_to_cpu(res);
0351 }
0352 
0353 static void
0354 sr_mdio_write(struct net_device *net, int phy_id, int loc, int val)
0355 {
0356     struct usbnet *dev = netdev_priv(net);
0357     __le16 res = cpu_to_le16(val);
0358 
0359     netdev_dbg(dev->net,
0360            "%s : phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", __func__,
0361            phy_id, loc, val);
0362     mutex_lock(&dev->phy_mutex);
0363     sr_set_sw_mii(dev);
0364     sr_write_cmd(dev, SR_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
0365     sr_set_hw_mii(dev);
0366     mutex_unlock(&dev->phy_mutex);
0367 }
0368 
0369 /* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
0370 static u32 sr_get_phyid(struct usbnet *dev)
0371 {
0372     int phy_reg;
0373     u32 phy_id;
0374     int i;
0375 
0376     /* Poll for the rare case the FW or phy isn't ready yet.  */
0377     for (i = 0; i < 100; i++) {
0378         phy_reg = sr_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
0379         if (phy_reg != 0 && phy_reg != 0xFFFF)
0380             break;
0381         mdelay(1);
0382     }
0383 
0384     if (phy_reg <= 0 || phy_reg == 0xFFFF)
0385         return 0;
0386 
0387     phy_id = (phy_reg & 0xffff) << 16;
0388 
0389     phy_reg = sr_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
0390     if (phy_reg < 0)
0391         return 0;
0392 
0393     phy_id |= (phy_reg & 0xffff);
0394 
0395     return phy_id;
0396 }
0397 
0398 static void
0399 sr_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
0400 {
0401     struct usbnet *dev = netdev_priv(net);
0402     u8 opt;
0403 
0404     if (sr_read_cmd(dev, SR_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
0405         wolinfo->supported = 0;
0406         wolinfo->wolopts = 0;
0407         return;
0408     }
0409     wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
0410     wolinfo->wolopts = 0;
0411     if (opt & SR_MONITOR_LINK)
0412         wolinfo->wolopts |= WAKE_PHY;
0413     if (opt & SR_MONITOR_MAGIC)
0414         wolinfo->wolopts |= WAKE_MAGIC;
0415 }
0416 
0417 static int
0418 sr_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
0419 {
0420     struct usbnet *dev = netdev_priv(net);
0421     u8 opt = 0;
0422 
0423     if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
0424         return -EINVAL;
0425 
0426     if (wolinfo->wolopts & WAKE_PHY)
0427         opt |= SR_MONITOR_LINK;
0428     if (wolinfo->wolopts & WAKE_MAGIC)
0429         opt |= SR_MONITOR_MAGIC;
0430 
0431     if (sr_write_cmd(dev, SR_CMD_WRITE_MONITOR_MODE,
0432              opt, 0, 0, NULL) < 0)
0433         return -EINVAL;
0434 
0435     return 0;
0436 }
0437 
0438 static int sr_get_eeprom_len(struct net_device *net)
0439 {
0440     struct usbnet *dev = netdev_priv(net);
0441     struct sr_data *data = (struct sr_data *)&dev->data;
0442 
0443     return data->eeprom_len;
0444 }
0445 
0446 static int sr_get_eeprom(struct net_device *net,
0447                   struct ethtool_eeprom *eeprom, u8 *data)
0448 {
0449     struct usbnet *dev = netdev_priv(net);
0450     __le16 *ebuf = (__le16 *)data;
0451     int ret;
0452     int i;
0453 
0454     /* Crude hack to ensure that we don't overwrite memory
0455      * if an odd length is supplied
0456      */
0457     if (eeprom->len % 2)
0458         return -EINVAL;
0459 
0460     eeprom->magic = SR_EEPROM_MAGIC;
0461 
0462     /* sr9800 returns 2 bytes from eeprom on read */
0463     for (i = 0; i < eeprom->len / 2; i++) {
0464         ret = sr_read_cmd(dev, SR_CMD_READ_EEPROM, eeprom->offset + i,
0465                   0, 2, &ebuf[i]);
0466         if (ret < 0)
0467             return -EINVAL;
0468     }
0469     return 0;
0470 }
0471 
0472 static void sr_get_drvinfo(struct net_device *net,
0473                  struct ethtool_drvinfo *info)
0474 {
0475     /* Inherit standard device info */
0476     usbnet_get_drvinfo(net, info);
0477     strncpy(info->driver, DRIVER_NAME, sizeof(info->driver));
0478     strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
0479 }
0480 
0481 static u32 sr_get_link(struct net_device *net)
0482 {
0483     struct usbnet *dev = netdev_priv(net);
0484 
0485     return mii_link_ok(&dev->mii);
0486 }
0487 
0488 static int sr_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
0489 {
0490     struct usbnet *dev = netdev_priv(net);
0491 
0492     return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
0493 }
0494 
0495 static int sr_set_mac_address(struct net_device *net, void *p)
0496 {
0497     struct usbnet *dev = netdev_priv(net);
0498     struct sr_data *data = (struct sr_data *)&dev->data;
0499     struct sockaddr *addr = p;
0500 
0501     if (netif_running(net))
0502         return -EBUSY;
0503     if (!is_valid_ether_addr(addr->sa_data))
0504         return -EADDRNOTAVAIL;
0505 
0506     eth_hw_addr_set(net, addr->sa_data);
0507 
0508     /* We use the 20 byte dev->data
0509      * for our 6 byte mac buffer
0510      * to avoid allocating memory that
0511      * is tricky to free later
0512      */
0513     memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
0514     sr_write_cmd_async(dev, SR_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
0515                data->mac_addr);
0516 
0517     return 0;
0518 }
0519 
0520 static const struct ethtool_ops sr9800_ethtool_ops = {
0521     .get_drvinfo    = sr_get_drvinfo,
0522     .get_link   = sr_get_link,
0523     .get_msglevel   = usbnet_get_msglevel,
0524     .set_msglevel   = usbnet_set_msglevel,
0525     .get_wol    = sr_get_wol,
0526     .set_wol    = sr_set_wol,
0527     .get_eeprom_len = sr_get_eeprom_len,
0528     .get_eeprom = sr_get_eeprom,
0529     .nway_reset = usbnet_nway_reset,
0530     .get_link_ksettings = usbnet_get_link_ksettings_mii,
0531     .set_link_ksettings = usbnet_set_link_ksettings_mii,
0532 };
0533 
0534 static int sr9800_link_reset(struct usbnet *dev)
0535 {
0536     struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
0537     u16 mode;
0538 
0539     mii_check_media(&dev->mii, 1, 1);
0540     mii_ethtool_gset(&dev->mii, &ecmd);
0541     mode = SR9800_MEDIUM_DEFAULT;
0542 
0543     if (ethtool_cmd_speed(&ecmd) != SPEED_100)
0544         mode &= ~SR_MEDIUM_PS;
0545 
0546     if (ecmd.duplex != DUPLEX_FULL)
0547         mode &= ~SR_MEDIUM_FD;
0548 
0549     netdev_dbg(dev->net, "%s : speed: %u duplex: %d mode: 0x%04x\n",
0550            __func__, ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
0551 
0552     sr_write_medium_mode(dev, mode);
0553 
0554     return 0;
0555 }
0556 
0557 
0558 static int sr9800_set_default_mode(struct usbnet *dev)
0559 {
0560     u16 rx_ctl;
0561     int ret;
0562 
0563     sr_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
0564     sr_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
0565               ADVERTISE_ALL | ADVERTISE_CSMA);
0566     mii_nway_restart(&dev->mii);
0567 
0568     ret = sr_write_medium_mode(dev, SR9800_MEDIUM_DEFAULT);
0569     if (ret < 0)
0570         goto out;
0571 
0572     ret = sr_write_cmd(dev, SR_CMD_WRITE_IPG012,
0573                 SR9800_IPG0_DEFAULT | SR9800_IPG1_DEFAULT,
0574                 SR9800_IPG2_DEFAULT, 0, NULL);
0575     if (ret < 0) {
0576         netdev_dbg(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
0577         goto out;
0578     }
0579 
0580     /* Set RX_CTL to default values with 2k buffer, and enable cactus */
0581     ret = sr_write_rx_ctl(dev, SR_DEFAULT_RX_CTL);
0582     if (ret < 0)
0583         goto out;
0584 
0585     rx_ctl = sr_read_rx_ctl(dev);
0586     netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
0587            rx_ctl);
0588 
0589     rx_ctl = sr_read_medium_status(dev);
0590     netdev_dbg(dev->net, "Medium Status:0x%04x after all initializations\n",
0591            rx_ctl);
0592 
0593     return 0;
0594 out:
0595     return ret;
0596 }
0597 
0598 static int sr9800_reset(struct usbnet *dev)
0599 {
0600     struct sr_data *data = (struct sr_data *)&dev->data;
0601     int ret, embd_phy;
0602     u16 rx_ctl;
0603 
0604     ret = sr_write_gpio(dev,
0605             SR_GPIO_RSE | SR_GPIO_GPO_2 | SR_GPIO_GPO2EN, 5);
0606     if (ret < 0)
0607         goto out;
0608 
0609     embd_phy = ((sr_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
0610 
0611     ret = sr_write_cmd(dev, SR_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
0612     if (ret < 0) {
0613         netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
0614         goto out;
0615     }
0616 
0617     ret = sr_sw_reset(dev, SR_SWRESET_IPPD | SR_SWRESET_PRL);
0618     if (ret < 0)
0619         goto out;
0620 
0621     msleep(150);
0622 
0623     ret = sr_sw_reset(dev, SR_SWRESET_CLEAR);
0624     if (ret < 0)
0625         goto out;
0626 
0627     msleep(150);
0628 
0629     if (embd_phy) {
0630         ret = sr_sw_reset(dev, SR_SWRESET_IPRL);
0631         if (ret < 0)
0632             goto out;
0633     } else {
0634         ret = sr_sw_reset(dev, SR_SWRESET_PRTE);
0635         if (ret < 0)
0636             goto out;
0637     }
0638 
0639     msleep(150);
0640     rx_ctl = sr_read_rx_ctl(dev);
0641     netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
0642     ret = sr_write_rx_ctl(dev, 0x0000);
0643     if (ret < 0)
0644         goto out;
0645 
0646     rx_ctl = sr_read_rx_ctl(dev);
0647     netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
0648 
0649     ret = sr_sw_reset(dev, SR_SWRESET_PRL);
0650     if (ret < 0)
0651         goto out;
0652 
0653     msleep(150);
0654 
0655     ret = sr_sw_reset(dev, SR_SWRESET_IPRL | SR_SWRESET_PRL);
0656     if (ret < 0)
0657         goto out;
0658 
0659     msleep(150);
0660 
0661     ret = sr9800_set_default_mode(dev);
0662     if (ret < 0)
0663         goto out;
0664 
0665     /* Rewrite MAC address */
0666     memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
0667     ret = sr_write_cmd(dev, SR_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
0668                             data->mac_addr);
0669     if (ret < 0)
0670         goto out;
0671 
0672     return 0;
0673 
0674 out:
0675     return ret;
0676 }
0677 
0678 static const struct net_device_ops sr9800_netdev_ops = {
0679     .ndo_open       = usbnet_open,
0680     .ndo_stop       = usbnet_stop,
0681     .ndo_start_xmit     = usbnet_start_xmit,
0682     .ndo_tx_timeout     = usbnet_tx_timeout,
0683     .ndo_change_mtu     = usbnet_change_mtu,
0684     .ndo_get_stats64    = dev_get_tstats64,
0685     .ndo_set_mac_address    = sr_set_mac_address,
0686     .ndo_validate_addr  = eth_validate_addr,
0687     .ndo_eth_ioctl      = sr_ioctl,
0688     .ndo_set_rx_mode        = sr_set_multicast,
0689 };
0690 
0691 static int sr9800_phy_powerup(struct usbnet *dev)
0692 {
0693     int ret;
0694 
0695     /* set the embedded Ethernet PHY in power-down state */
0696     ret = sr_sw_reset(dev, SR_SWRESET_IPPD | SR_SWRESET_IPRL);
0697     if (ret < 0) {
0698         netdev_err(dev->net, "Failed to power down PHY : %d\n", ret);
0699         return ret;
0700     }
0701     msleep(20);
0702 
0703     /* set the embedded Ethernet PHY in power-up state */
0704     ret = sr_sw_reset(dev, SR_SWRESET_IPRL);
0705     if (ret < 0) {
0706         netdev_err(dev->net, "Failed to reset PHY: %d\n", ret);
0707         return ret;
0708     }
0709     msleep(600);
0710 
0711     /* set the embedded Ethernet PHY in reset state */
0712     ret = sr_sw_reset(dev, SR_SWRESET_CLEAR);
0713     if (ret < 0) {
0714         netdev_err(dev->net, "Failed to power up PHY: %d\n", ret);
0715         return ret;
0716     }
0717     msleep(20);
0718 
0719     /* set the embedded Ethernet PHY in power-up state */
0720     ret = sr_sw_reset(dev, SR_SWRESET_IPRL);
0721     if (ret < 0) {
0722         netdev_err(dev->net, "Failed to reset PHY: %d\n", ret);
0723         return ret;
0724     }
0725 
0726     return 0;
0727 }
0728 
0729 static int sr9800_bind(struct usbnet *dev, struct usb_interface *intf)
0730 {
0731     struct sr_data *data = (struct sr_data *)&dev->data;
0732     u16 led01_mux, led23_mux;
0733     int ret, embd_phy;
0734     u8 addr[ETH_ALEN];
0735     u32 phyid;
0736     u16 rx_ctl;
0737 
0738     data->eeprom_len = SR9800_EEPROM_LEN;
0739 
0740     usbnet_get_endpoints(dev, intf);
0741 
0742     /* LED Setting Rule :
0743      * AABB:CCDD
0744      * AA : MFA0(LED0)
0745      * BB : MFA1(LED1)
0746      * CC : MFA2(LED2), Reserved for SR9800
0747      * DD : MFA3(LED3), Reserved for SR9800
0748      */
0749     led01_mux = (SR_LED_MUX_LINK_ACTIVE << 8) | SR_LED_MUX_LINK;
0750     led23_mux = (SR_LED_MUX_LINK_ACTIVE << 8) | SR_LED_MUX_TX_ACTIVE;
0751     ret = sr_write_cmd(dev, SR_CMD_LED_MUX, led01_mux, led23_mux, 0, NULL);
0752     if (ret < 0) {
0753             netdev_err(dev->net, "set LINK LED failed : %d\n", ret);
0754             goto out;
0755     }
0756 
0757     /* Get the MAC address */
0758     ret = sr_read_cmd(dev, SR_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, addr);
0759     if (ret < 0) {
0760         netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
0761         return ret;
0762     }
0763     eth_hw_addr_set(dev->net, addr);
0764     netdev_dbg(dev->net, "mac addr : %pM\n", dev->net->dev_addr);
0765 
0766     /* Initialize MII structure */
0767     dev->mii.dev = dev->net;
0768     dev->mii.mdio_read = sr_mdio_read;
0769     dev->mii.mdio_write = sr_mdio_write;
0770     dev->mii.phy_id_mask = 0x1f;
0771     dev->mii.reg_num_mask = 0x1f;
0772     dev->mii.phy_id = sr_get_phy_addr(dev);
0773 
0774     dev->net->netdev_ops = &sr9800_netdev_ops;
0775     dev->net->ethtool_ops = &sr9800_ethtool_ops;
0776 
0777     embd_phy = ((dev->mii.phy_id & 0x1f) == 0x10 ? 1 : 0);
0778     /* Reset the PHY to normal operation mode */
0779     ret = sr_write_cmd(dev, SR_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
0780     if (ret < 0) {
0781         netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
0782         return ret;
0783     }
0784 
0785     /* Init PHY routine */
0786     ret = sr9800_phy_powerup(dev);
0787     if (ret < 0)
0788         goto out;
0789 
0790     rx_ctl = sr_read_rx_ctl(dev);
0791     netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
0792     ret = sr_write_rx_ctl(dev, 0x0000);
0793     if (ret < 0)
0794         goto out;
0795 
0796     rx_ctl = sr_read_rx_ctl(dev);
0797     netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
0798 
0799     /* Read PHYID register *AFTER* the PHY was reset properly */
0800     phyid = sr_get_phyid(dev);
0801     netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
0802 
0803     /* medium mode setting */
0804     ret = sr9800_set_default_mode(dev);
0805     if (ret < 0)
0806         goto out;
0807 
0808     if (dev->udev->speed == USB_SPEED_HIGH) {
0809         ret = sr_write_cmd(dev, SR_CMD_BULKIN_SIZE,
0810             SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].byte_cnt,
0811             SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].threshold,
0812             0, NULL);
0813         if (ret < 0) {
0814             netdev_err(dev->net, "Reset RX_CTL failed: %d\n", ret);
0815             goto out;
0816         }
0817         dev->rx_urb_size =
0818             SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].size;
0819     } else {
0820         ret = sr_write_cmd(dev, SR_CMD_BULKIN_SIZE,
0821             SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].byte_cnt,
0822             SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].threshold,
0823             0, NULL);
0824         if (ret < 0) {
0825             netdev_err(dev->net, "Reset RX_CTL failed: %d\n", ret);
0826             goto out;
0827         }
0828         dev->rx_urb_size =
0829             SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].size;
0830     }
0831     netdev_dbg(dev->net, "%s : setting rx_urb_size with : %zu\n", __func__,
0832            dev->rx_urb_size);
0833     return 0;
0834 
0835 out:
0836     return ret;
0837 }
0838 
0839 static const struct driver_info sr9800_driver_info = {
0840     .description    = "CoreChip SR9800 USB 2.0 Ethernet",
0841     .bind       = sr9800_bind,
0842     .status     = sr_status,
0843     .link_reset = sr9800_link_reset,
0844     .reset      = sr9800_reset,
0845     .flags      = DRIVER_FLAG,
0846     .rx_fixup   = sr_rx_fixup,
0847     .tx_fixup   = sr_tx_fixup,
0848 };
0849 
0850 static const struct usb_device_id   products[] = {
0851     {
0852         USB_DEVICE(0x0fe6, 0x9800), /* SR9800 Device  */
0853         .driver_info = (unsigned long) &sr9800_driver_info,
0854     },
0855     {},     /* END */
0856 };
0857 
0858 MODULE_DEVICE_TABLE(usb, products);
0859 
0860 static struct usb_driver sr_driver = {
0861     .name       = DRIVER_NAME,
0862     .id_table   = products,
0863     .probe      = usbnet_probe,
0864     .suspend    = usbnet_suspend,
0865     .resume     = usbnet_resume,
0866     .disconnect = usbnet_disconnect,
0867     .supports_autosuspend = 1,
0868 };
0869 
0870 module_usb_driver(sr_driver);
0871 
0872 MODULE_AUTHOR("Liu Junliang <liujunliang_ljl@163.com");
0873 MODULE_VERSION(DRIVER_VERSION);
0874 MODULE_DESCRIPTION("SR9800 USB 2.0 USB2NET Dev : http://www.corechip-sz.com");
0875 MODULE_LICENSE("GPL");