linux/drivers/net/usb/sr9800.c
<<
>>
Prefs
   1/* CoreChip-sz SR9800 one chip USB 2.0 Ethernet Devices
   2 *
   3 * Author : Liu Junliang <liujunliang_ljl@163.com>
   4 *
   5 * Based on asix_common.c, asix_devices.c
   6 *
   7 * This file is licensed under the terms of the GNU General Public License
   8 * version 2.  This program is licensed "as is" without any warranty of any
   9 * kind, whether express or implied.*
  10 */
  11
  12#include <linux/module.h>
  13#include <linux/kmod.h>
  14#include <linux/init.h>
  15#include <linux/netdevice.h>
  16#include <linux/etherdevice.h>
  17#include <linux/ethtool.h>
  18#include <linux/workqueue.h>
  19#include <linux/mii.h>
  20#include <linux/usb.h>
  21#include <linux/crc32.h>
  22#include <linux/usb/usbnet.h>
  23#include <linux/slab.h>
  24#include <linux/if_vlan.h>
  25
  26#include "sr9800.h"
  27
  28static int sr_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  29                            u16 size, void *data)
  30{
  31        int err;
  32
  33        err = usbnet_read_cmd(dev, cmd, SR_REQ_RD_REG, value, index,
  34                              data, size);
  35        if ((err != size) && (err >= 0))
  36                err = -EINVAL;
  37
  38        return err;
  39}
  40
  41static int sr_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  42                             u16 size, void *data)
  43{
  44        int err;
  45
  46        err = usbnet_write_cmd(dev, cmd, SR_REQ_WR_REG, value, index,
  47                              data, size);
  48        if ((err != size) && (err >= 0))
  49                err = -EINVAL;
  50
  51        return err;
  52}
  53
  54static void
  55sr_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  56                   u16 size, void *data)
  57{
  58        usbnet_write_cmd_async(dev, cmd, SR_REQ_WR_REG, value, index, data,
  59                               size);
  60}
  61
  62static int sr_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  63{
  64        int offset = 0;
  65
  66        while (offset + sizeof(u32) < skb->len) {
  67                struct sk_buff *sr_skb;
  68                u16 size;
  69                u32 header = get_unaligned_le32(skb->data + offset);
  70
  71                offset += sizeof(u32);
  72                /* get the packet length */
  73                size = (u16) (header & 0x7ff);
  74                if (size != ((~header >> 16) & 0x07ff)) {
  75                        netdev_err(dev->net, "%s : Bad Header Length\n",
  76                                   __func__);
  77                        return 0;
  78                }
  79
  80                if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
  81                    (size + offset > skb->len)) {
  82                        netdev_err(dev->net, "%s : Bad RX Length %d\n",
  83                                   __func__, size);
  84                        return 0;
  85                }
  86                sr_skb = netdev_alloc_skb_ip_align(dev->net, size);
  87                if (!sr_skb)
  88                        return 0;
  89
  90                skb_put(sr_skb, size);
  91                memcpy(sr_skb->data, skb->data + offset, size);
  92                usbnet_skb_return(dev, sr_skb);
  93
  94                offset += (size + 1) & 0xfffe;
  95        }
  96
  97        if (skb->len != offset) {
  98                netdev_err(dev->net, "%s : Bad SKB Length %d\n", __func__,
  99                           skb->len);
 100                return 0;
 101        }
 102
 103        return 1;
 104}
 105
 106static struct sk_buff *sr_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
 107                                        gfp_t flags)
 108{
 109        int headroom = skb_headroom(skb);
 110        int tailroom = skb_tailroom(skb);
 111        u32 padbytes = 0xffff0000;
 112        u32 packet_len;
 113        int padlen;
 114
 115        padlen = ((skb->len + 4) % (dev->maxpacket - 1)) ? 0 : 4;
 116
 117        if ((!skb_cloned(skb)) && ((headroom + tailroom) >= (4 + padlen))) {
 118                if ((headroom < 4) || (tailroom < padlen)) {
 119                        skb->data = memmove(skb->head + 4, skb->data,
 120                                            skb->len);
 121                        skb_set_tail_pointer(skb, skb->len);
 122                }
 123        } else {
 124                struct sk_buff *skb2;
 125                skb2 = skb_copy_expand(skb, 4, padlen, flags);
 126                dev_kfree_skb_any(skb);
 127                skb = skb2;
 128                if (!skb)
 129                        return NULL;
 130        }
 131
 132        skb_push(skb, 4);
 133        packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
 134        cpu_to_le32s(&packet_len);
 135        skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
 136
 137        if (padlen) {
 138                cpu_to_le32s(&padbytes);
 139                memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
 140                skb_put(skb, sizeof(padbytes));
 141        }
 142
 143        usbnet_set_skb_tx_stats(skb, 1, 0);
 144        return skb;
 145}
 146
 147static void sr_status(struct usbnet *dev, struct urb *urb)
 148{
 149        struct sr9800_int_data *event;
 150        int link;
 151
 152        if (urb->actual_length < 8)
 153                return;
 154
 155        event = urb->transfer_buffer;
 156        link = event->link & 0x01;
 157        if (netif_carrier_ok(dev->net) != link) {
 158                usbnet_link_change(dev, link, 1);
 159                netdev_dbg(dev->net, "Link Status is: %d\n", link);
 160        }
 161
 162        return;
 163}
 164
 165static inline int sr_set_sw_mii(struct usbnet *dev)
 166{
 167        int ret;
 168
 169        ret = sr_write_cmd(dev, SR_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
 170        if (ret < 0)
 171                netdev_err(dev->net, "Failed to enable software MII access\n");
 172        return ret;
 173}
 174
 175static inline int sr_set_hw_mii(struct usbnet *dev)
 176{
 177        int ret;
 178
 179        ret = sr_write_cmd(dev, SR_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
 180        if (ret < 0)
 181                netdev_err(dev->net, "Failed to enable hardware MII access\n");
 182        return ret;
 183}
 184
 185static inline int sr_get_phy_addr(struct usbnet *dev)
 186{
 187        u8 buf[2];
 188        int ret;
 189
 190        ret = sr_read_cmd(dev, SR_CMD_READ_PHY_ID, 0, 0, 2, buf);
 191        if (ret < 0) {
 192                netdev_err(dev->net, "%s : Error reading PHYID register:%02x\n",
 193                           __func__, ret);
 194                goto out;
 195        }
 196        netdev_dbg(dev->net, "%s : returning 0x%04x\n", __func__,
 197                   *((__le16 *)buf));
 198
 199        ret = buf[1];
 200
 201out:
 202        return ret;
 203}
 204
 205static int sr_sw_reset(struct usbnet *dev, u8 flags)
 206{
 207        int ret;
 208
 209        ret = sr_write_cmd(dev, SR_CMD_SW_RESET, flags, 0, 0, NULL);
 210        if (ret < 0)
 211                netdev_err(dev->net, "Failed to send software reset:%02x\n",
 212                           ret);
 213
 214        return ret;
 215}
 216
 217static u16 sr_read_rx_ctl(struct usbnet *dev)
 218{
 219        __le16 v;
 220        int ret;
 221
 222        ret = sr_read_cmd(dev, SR_CMD_READ_RX_CTL, 0, 0, 2, &v);
 223        if (ret < 0) {
 224                netdev_err(dev->net, "Error reading RX_CTL register:%02x\n",
 225                           ret);
 226                goto out;
 227        }
 228
 229        ret = le16_to_cpu(v);
 230out:
 231        return ret;
 232}
 233
 234static int sr_write_rx_ctl(struct usbnet *dev, u16 mode)
 235{
 236        int ret;
 237
 238        netdev_dbg(dev->net, "%s : mode = 0x%04x\n", __func__, mode);
 239        ret = sr_write_cmd(dev, SR_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
 240        if (ret < 0)
 241                netdev_err(dev->net,
 242                           "Failed to write RX_CTL mode to 0x%04x:%02x\n",
 243                           mode, ret);
 244
 245        return ret;
 246}
 247
 248static u16 sr_read_medium_status(struct usbnet *dev)
 249{
 250        __le16 v;
 251        int ret;
 252
 253        ret = sr_read_cmd(dev, SR_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
 254        if (ret < 0) {
 255                netdev_err(dev->net,
 256                           "Error reading Medium Status register:%02x\n", ret);
 257                return ret;     /* TODO: callers not checking for error ret */
 258        }
 259
 260        return le16_to_cpu(v);
 261}
 262
 263static int sr_write_medium_mode(struct usbnet *dev, u16 mode)
 264{
 265        int ret;
 266
 267        netdev_dbg(dev->net, "%s : mode = 0x%04x\n", __func__, mode);
 268        ret = sr_write_cmd(dev, SR_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
 269        if (ret < 0)
 270                netdev_err(dev->net,
 271                           "Failed to write Medium Mode mode to 0x%04x:%02x\n",
 272                           mode, ret);
 273        return ret;
 274}
 275
 276static int sr_write_gpio(struct usbnet *dev, u16 value, int sleep)
 277{
 278        int ret;
 279
 280        netdev_dbg(dev->net, "%s : value = 0x%04x\n", __func__, value);
 281        ret = sr_write_cmd(dev, SR_CMD_WRITE_GPIOS, value, 0, 0, NULL);
 282        if (ret < 0)
 283                netdev_err(dev->net, "Failed to write GPIO value 0x%04x:%02x\n",
 284                           value, ret);
 285        if (sleep)
 286                msleep(sleep);
 287
 288        return ret;
 289}
 290
 291/* SR9800 have a 16-bit RX_CTL value */
 292static void sr_set_multicast(struct net_device *net)
 293{
 294        struct usbnet *dev = netdev_priv(net);
 295        struct sr_data *data = (struct sr_data *)&dev->data;
 296        u16 rx_ctl = SR_DEFAULT_RX_CTL;
 297
 298        if (net->flags & IFF_PROMISC) {
 299                rx_ctl |= SR_RX_CTL_PRO;
 300        } else if (net->flags & IFF_ALLMULTI ||
 301                   netdev_mc_count(net) > SR_MAX_MCAST) {
 302                rx_ctl |= SR_RX_CTL_AMALL;
 303        } else if (netdev_mc_empty(net)) {
 304                /* just broadcast and directed */
 305        } else {
 306                /* We use the 20 byte dev->data
 307                 * for our 8 byte filter buffer
 308                 * to avoid allocating memory that
 309                 * is tricky to free later
 310                 */
 311                struct netdev_hw_addr *ha;
 312                u32 crc_bits;
 313
 314                memset(data->multi_filter, 0, SR_MCAST_FILTER_SIZE);
 315
 316                /* Build the multicast hash filter. */
 317                netdev_for_each_mc_addr(ha, net) {
 318                        crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
 319                        data->multi_filter[crc_bits >> 3] |=
 320                            1 << (crc_bits & 7);
 321                }
 322
 323                sr_write_cmd_async(dev, SR_CMD_WRITE_MULTI_FILTER, 0, 0,
 324                                   SR_MCAST_FILTER_SIZE, data->multi_filter);
 325
 326                rx_ctl |= SR_RX_CTL_AM;
 327        }
 328
 329        sr_write_cmd_async(dev, SR_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
 330}
 331
 332static int sr_mdio_read(struct net_device *net, int phy_id, int loc)
 333{
 334        struct usbnet *dev = netdev_priv(net);
 335        __le16 res;
 336
 337        mutex_lock(&dev->phy_mutex);
 338        sr_set_sw_mii(dev);
 339        sr_read_cmd(dev, SR_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, &res);
 340        sr_set_hw_mii(dev);
 341        mutex_unlock(&dev->phy_mutex);
 342
 343        netdev_dbg(dev->net,
 344                   "%s : phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", __func__,
 345                   phy_id, loc, le16_to_cpu(res));
 346
 347        return le16_to_cpu(res);
 348}
 349
 350static void
 351sr_mdio_write(struct net_device *net, int phy_id, int loc, int val)
 352{
 353        struct usbnet *dev = netdev_priv(net);
 354        __le16 res = cpu_to_le16(val);
 355
 356        netdev_dbg(dev->net,
 357                   "%s : phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", __func__,
 358                   phy_id, loc, val);
 359        mutex_lock(&dev->phy_mutex);
 360        sr_set_sw_mii(dev);
 361        sr_write_cmd(dev, SR_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
 362        sr_set_hw_mii(dev);
 363        mutex_unlock(&dev->phy_mutex);
 364}
 365
 366/* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
 367static u32 sr_get_phyid(struct usbnet *dev)
 368{
 369        int phy_reg;
 370        u32 phy_id;
 371        int i;
 372
 373        /* Poll for the rare case the FW or phy isn't ready yet.  */
 374        for (i = 0; i < 100; i++) {
 375                phy_reg = sr_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
 376                if (phy_reg != 0 && phy_reg != 0xFFFF)
 377                        break;
 378                mdelay(1);
 379        }
 380
 381        if (phy_reg <= 0 || phy_reg == 0xFFFF)
 382                return 0;
 383
 384        phy_id = (phy_reg & 0xffff) << 16;
 385
 386        phy_reg = sr_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
 387        if (phy_reg < 0)
 388                return 0;
 389
 390        phy_id |= (phy_reg & 0xffff);
 391
 392        return phy_id;
 393}
 394
 395static void
 396sr_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
 397{
 398        struct usbnet *dev = netdev_priv(net);
 399        u8 opt;
 400
 401        if (sr_read_cmd(dev, SR_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
 402                wolinfo->supported = 0;
 403                wolinfo->wolopts = 0;
 404                return;
 405        }
 406        wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
 407        wolinfo->wolopts = 0;
 408        if (opt & SR_MONITOR_LINK)
 409                wolinfo->wolopts |= WAKE_PHY;
 410        if (opt & SR_MONITOR_MAGIC)
 411                wolinfo->wolopts |= WAKE_MAGIC;
 412}
 413
 414static int
 415sr_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
 416{
 417        struct usbnet *dev = netdev_priv(net);
 418        u8 opt = 0;
 419
 420        if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
 421                return -EINVAL;
 422
 423        if (wolinfo->wolopts & WAKE_PHY)
 424                opt |= SR_MONITOR_LINK;
 425        if (wolinfo->wolopts & WAKE_MAGIC)
 426                opt |= SR_MONITOR_MAGIC;
 427
 428        if (sr_write_cmd(dev, SR_CMD_WRITE_MONITOR_MODE,
 429                         opt, 0, 0, NULL) < 0)
 430                return -EINVAL;
 431
 432        return 0;
 433}
 434
 435static int sr_get_eeprom_len(struct net_device *net)
 436{
 437        struct usbnet *dev = netdev_priv(net);
 438        struct sr_data *data = (struct sr_data *)&dev->data;
 439
 440        return data->eeprom_len;
 441}
 442
 443static int sr_get_eeprom(struct net_device *net,
 444                              struct ethtool_eeprom *eeprom, u8 *data)
 445{
 446        struct usbnet *dev = netdev_priv(net);
 447        __le16 *ebuf = (__le16 *)data;
 448        int ret;
 449        int i;
 450
 451        /* Crude hack to ensure that we don't overwrite memory
 452         * if an odd length is supplied
 453         */
 454        if (eeprom->len % 2)
 455                return -EINVAL;
 456
 457        eeprom->magic = SR_EEPROM_MAGIC;
 458
 459        /* sr9800 returns 2 bytes from eeprom on read */
 460        for (i = 0; i < eeprom->len / 2; i++) {
 461                ret = sr_read_cmd(dev, SR_CMD_READ_EEPROM, eeprom->offset + i,
 462                                  0, 2, &ebuf[i]);
 463                if (ret < 0)
 464                        return -EINVAL;
 465        }
 466        return 0;
 467}
 468
 469static void sr_get_drvinfo(struct net_device *net,
 470                                 struct ethtool_drvinfo *info)
 471{
 472        /* Inherit standard device info */
 473        usbnet_get_drvinfo(net, info);
 474        strncpy(info->driver, DRIVER_NAME, sizeof(info->driver));
 475        strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
 476}
 477
 478static u32 sr_get_link(struct net_device *net)
 479{
 480        struct usbnet *dev = netdev_priv(net);
 481
 482        return mii_link_ok(&dev->mii);
 483}
 484
 485static int sr_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
 486{
 487        struct usbnet *dev = netdev_priv(net);
 488
 489        return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
 490}
 491
 492static int sr_set_mac_address(struct net_device *net, void *p)
 493{
 494        struct usbnet *dev = netdev_priv(net);
 495        struct sr_data *data = (struct sr_data *)&dev->data;
 496        struct sockaddr *addr = p;
 497
 498        if (netif_running(net))
 499                return -EBUSY;
 500        if (!is_valid_ether_addr(addr->sa_data))
 501                return -EADDRNOTAVAIL;
 502
 503        memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
 504
 505        /* We use the 20 byte dev->data
 506         * for our 6 byte mac buffer
 507         * to avoid allocating memory that
 508         * is tricky to free later
 509         */
 510        memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
 511        sr_write_cmd_async(dev, SR_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
 512                           data->mac_addr);
 513
 514        return 0;
 515}
 516
 517static const struct ethtool_ops sr9800_ethtool_ops = {
 518        .get_drvinfo    = sr_get_drvinfo,
 519        .get_link       = sr_get_link,
 520        .get_msglevel   = usbnet_get_msglevel,
 521        .set_msglevel   = usbnet_set_msglevel,
 522        .get_wol        = sr_get_wol,
 523        .set_wol        = sr_set_wol,
 524        .get_eeprom_len = sr_get_eeprom_len,
 525        .get_eeprom     = sr_get_eeprom,
 526        .nway_reset     = usbnet_nway_reset,
 527        .get_link_ksettings     = usbnet_get_link_ksettings,
 528        .set_link_ksettings     = usbnet_set_link_ksettings,
 529};
 530
 531static int sr9800_link_reset(struct usbnet *dev)
 532{
 533        struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
 534        u16 mode;
 535
 536        mii_check_media(&dev->mii, 1, 1);
 537        mii_ethtool_gset(&dev->mii, &ecmd);
 538        mode = SR9800_MEDIUM_DEFAULT;
 539
 540        if (ethtool_cmd_speed(&ecmd) != SPEED_100)
 541                mode &= ~SR_MEDIUM_PS;
 542
 543        if (ecmd.duplex != DUPLEX_FULL)
 544                mode &= ~SR_MEDIUM_FD;
 545
 546        netdev_dbg(dev->net, "%s : speed: %u duplex: %d mode: 0x%04x\n",
 547                   __func__, ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
 548
 549        sr_write_medium_mode(dev, mode);
 550
 551        return 0;
 552}
 553
 554
 555static int sr9800_set_default_mode(struct usbnet *dev)
 556{
 557        u16 rx_ctl;
 558        int ret;
 559
 560        sr_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
 561        sr_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
 562                      ADVERTISE_ALL | ADVERTISE_CSMA);
 563        mii_nway_restart(&dev->mii);
 564
 565        ret = sr_write_medium_mode(dev, SR9800_MEDIUM_DEFAULT);
 566        if (ret < 0)
 567                goto out;
 568
 569        ret = sr_write_cmd(dev, SR_CMD_WRITE_IPG012,
 570                                SR9800_IPG0_DEFAULT | SR9800_IPG1_DEFAULT,
 571                                SR9800_IPG2_DEFAULT, 0, NULL);
 572        if (ret < 0) {
 573                netdev_dbg(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
 574                goto out;
 575        }
 576
 577        /* Set RX_CTL to default values with 2k buffer, and enable cactus */
 578        ret = sr_write_rx_ctl(dev, SR_DEFAULT_RX_CTL);
 579        if (ret < 0)
 580                goto out;
 581
 582        rx_ctl = sr_read_rx_ctl(dev);
 583        netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
 584                   rx_ctl);
 585
 586        rx_ctl = sr_read_medium_status(dev);
 587        netdev_dbg(dev->net, "Medium Status:0x%04x after all initializations\n",
 588                   rx_ctl);
 589
 590        return 0;
 591out:
 592        return ret;
 593}
 594
 595static int sr9800_reset(struct usbnet *dev)
 596{
 597        struct sr_data *data = (struct sr_data *)&dev->data;
 598        int ret, embd_phy;
 599        u16 rx_ctl;
 600
 601        ret = sr_write_gpio(dev,
 602                        SR_GPIO_RSE | SR_GPIO_GPO_2 | SR_GPIO_GPO2EN, 5);
 603        if (ret < 0)
 604                goto out;
 605
 606        embd_phy = ((sr_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
 607
 608        ret = sr_write_cmd(dev, SR_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
 609        if (ret < 0) {
 610                netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
 611                goto out;
 612        }
 613
 614        ret = sr_sw_reset(dev, SR_SWRESET_IPPD | SR_SWRESET_PRL);
 615        if (ret < 0)
 616                goto out;
 617
 618        msleep(150);
 619
 620        ret = sr_sw_reset(dev, SR_SWRESET_CLEAR);
 621        if (ret < 0)
 622                goto out;
 623
 624        msleep(150);
 625
 626        if (embd_phy) {
 627                ret = sr_sw_reset(dev, SR_SWRESET_IPRL);
 628                if (ret < 0)
 629                        goto out;
 630        } else {
 631                ret = sr_sw_reset(dev, SR_SWRESET_PRTE);
 632                if (ret < 0)
 633                        goto out;
 634        }
 635
 636        msleep(150);
 637        rx_ctl = sr_read_rx_ctl(dev);
 638        netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
 639        ret = sr_write_rx_ctl(dev, 0x0000);
 640        if (ret < 0)
 641                goto out;
 642
 643        rx_ctl = sr_read_rx_ctl(dev);
 644        netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
 645
 646        ret = sr_sw_reset(dev, SR_SWRESET_PRL);
 647        if (ret < 0)
 648                goto out;
 649
 650        msleep(150);
 651
 652        ret = sr_sw_reset(dev, SR_SWRESET_IPRL | SR_SWRESET_PRL);
 653        if (ret < 0)
 654                goto out;
 655
 656        msleep(150);
 657
 658        ret = sr9800_set_default_mode(dev);
 659        if (ret < 0)
 660                goto out;
 661
 662        /* Rewrite MAC address */
 663        memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
 664        ret = sr_write_cmd(dev, SR_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
 665                                                        data->mac_addr);
 666        if (ret < 0)
 667                goto out;
 668
 669        return 0;
 670
 671out:
 672        return ret;
 673}
 674
 675static const struct net_device_ops sr9800_netdev_ops = {
 676        .ndo_open               = usbnet_open,
 677        .ndo_stop               = usbnet_stop,
 678        .ndo_start_xmit         = usbnet_start_xmit,
 679        .ndo_tx_timeout         = usbnet_tx_timeout,
 680        .ndo_change_mtu         = usbnet_change_mtu,
 681        .ndo_set_mac_address    = sr_set_mac_address,
 682        .ndo_validate_addr      = eth_validate_addr,
 683        .ndo_do_ioctl           = sr_ioctl,
 684        .ndo_set_rx_mode        = sr_set_multicast,
 685};
 686
 687static int sr9800_phy_powerup(struct usbnet *dev)
 688{
 689        int ret;
 690
 691        /* set the embedded Ethernet PHY in power-down state */
 692        ret = sr_sw_reset(dev, SR_SWRESET_IPPD | SR_SWRESET_IPRL);
 693        if (ret < 0) {
 694                netdev_err(dev->net, "Failed to power down PHY : %d\n", ret);
 695                return ret;
 696        }
 697        msleep(20);
 698
 699        /* set the embedded Ethernet PHY in power-up state */
 700        ret = sr_sw_reset(dev, SR_SWRESET_IPRL);
 701        if (ret < 0) {
 702                netdev_err(dev->net, "Failed to reset PHY: %d\n", ret);
 703                return ret;
 704        }
 705        msleep(600);
 706
 707        /* set the embedded Ethernet PHY in reset state */
 708        ret = sr_sw_reset(dev, SR_SWRESET_CLEAR);
 709        if (ret < 0) {
 710                netdev_err(dev->net, "Failed to power up PHY: %d\n", ret);
 711                return ret;
 712        }
 713        msleep(20);
 714
 715        /* set the embedded Ethernet PHY in power-up state */
 716        ret = sr_sw_reset(dev, SR_SWRESET_IPRL);
 717        if (ret < 0) {
 718                netdev_err(dev->net, "Failed to reset PHY: %d\n", ret);
 719                return ret;
 720        }
 721
 722        return 0;
 723}
 724
 725static int sr9800_bind(struct usbnet *dev, struct usb_interface *intf)
 726{
 727        struct sr_data *data = (struct sr_data *)&dev->data;
 728        u16 led01_mux, led23_mux;
 729        int ret, embd_phy;
 730        u32 phyid;
 731        u16 rx_ctl;
 732
 733        data->eeprom_len = SR9800_EEPROM_LEN;
 734
 735        usbnet_get_endpoints(dev, intf);
 736
 737        /* LED Setting Rule :
 738         * AABB:CCDD
 739         * AA : MFA0(LED0)
 740         * BB : MFA1(LED1)
 741         * CC : MFA2(LED2), Reserved for SR9800
 742         * DD : MFA3(LED3), Reserved for SR9800
 743         */
 744        led01_mux = (SR_LED_MUX_LINK_ACTIVE << 8) | SR_LED_MUX_LINK;
 745        led23_mux = (SR_LED_MUX_LINK_ACTIVE << 8) | SR_LED_MUX_TX_ACTIVE;
 746        ret = sr_write_cmd(dev, SR_CMD_LED_MUX, led01_mux, led23_mux, 0, NULL);
 747        if (ret < 0) {
 748                        netdev_err(dev->net, "set LINK LED failed : %d\n", ret);
 749                        goto out;
 750        }
 751
 752        /* Get the MAC address */
 753        ret = sr_read_cmd(dev, SR_CMD_READ_NODE_ID, 0, 0, ETH_ALEN,
 754                          dev->net->dev_addr);
 755        if (ret < 0) {
 756                netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
 757                return ret;
 758        }
 759        netdev_dbg(dev->net, "mac addr : %pM\n", dev->net->dev_addr);
 760
 761        /* Initialize MII structure */
 762        dev->mii.dev = dev->net;
 763        dev->mii.mdio_read = sr_mdio_read;
 764        dev->mii.mdio_write = sr_mdio_write;
 765        dev->mii.phy_id_mask = 0x1f;
 766        dev->mii.reg_num_mask = 0x1f;
 767        dev->mii.phy_id = sr_get_phy_addr(dev);
 768
 769        dev->net->netdev_ops = &sr9800_netdev_ops;
 770        dev->net->ethtool_ops = &sr9800_ethtool_ops;
 771
 772        embd_phy = ((dev->mii.phy_id & 0x1f) == 0x10 ? 1 : 0);
 773        /* Reset the PHY to normal operation mode */
 774        ret = sr_write_cmd(dev, SR_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
 775        if (ret < 0) {
 776                netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
 777                return ret;
 778        }
 779
 780        /* Init PHY routine */
 781        ret = sr9800_phy_powerup(dev);
 782        if (ret < 0)
 783                goto out;
 784
 785        rx_ctl = sr_read_rx_ctl(dev);
 786        netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
 787        ret = sr_write_rx_ctl(dev, 0x0000);
 788        if (ret < 0)
 789                goto out;
 790
 791        rx_ctl = sr_read_rx_ctl(dev);
 792        netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
 793
 794        /* Read PHYID register *AFTER* the PHY was reset properly */
 795        phyid = sr_get_phyid(dev);
 796        netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
 797
 798        /* medium mode setting */
 799        ret = sr9800_set_default_mode(dev);
 800        if (ret < 0)
 801                goto out;
 802
 803        if (dev->udev->speed == USB_SPEED_HIGH) {
 804                ret = sr_write_cmd(dev, SR_CMD_BULKIN_SIZE,
 805                        SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].byte_cnt,
 806                        SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].threshold,
 807                        0, NULL);
 808                if (ret < 0) {
 809                        netdev_err(dev->net, "Reset RX_CTL failed: %d\n", ret);
 810                        goto out;
 811                }
 812                dev->rx_urb_size =
 813                        SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].size;
 814        } else {
 815                ret = sr_write_cmd(dev, SR_CMD_BULKIN_SIZE,
 816                        SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].byte_cnt,
 817                        SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].threshold,
 818                        0, NULL);
 819                if (ret < 0) {
 820                        netdev_err(dev->net, "Reset RX_CTL failed: %d\n", ret);
 821                        goto out;
 822                }
 823                dev->rx_urb_size =
 824                        SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].size;
 825        }
 826        netdev_dbg(dev->net, "%s : setting rx_urb_size with : %zu\n", __func__,
 827                   dev->rx_urb_size);
 828        return 0;
 829
 830out:
 831        return ret;
 832}
 833
 834static const struct driver_info sr9800_driver_info = {
 835        .description    = "CoreChip SR9800 USB 2.0 Ethernet",
 836        .bind           = sr9800_bind,
 837        .status         = sr_status,
 838        .link_reset     = sr9800_link_reset,
 839        .reset          = sr9800_reset,
 840        .flags          = DRIVER_FLAG,
 841        .rx_fixup       = sr_rx_fixup,
 842        .tx_fixup       = sr_tx_fixup,
 843};
 844
 845static const struct usb_device_id       products[] = {
 846        {
 847                USB_DEVICE(0x0fe6, 0x9800),     /* SR9800 Device  */
 848                .driver_info = (unsigned long) &sr9800_driver_info,
 849        },
 850        {},             /* END */
 851};
 852
 853MODULE_DEVICE_TABLE(usb, products);
 854
 855static struct usb_driver sr_driver = {
 856        .name           = DRIVER_NAME,
 857        .id_table       = products,
 858        .probe          = usbnet_probe,
 859        .suspend        = usbnet_suspend,
 860        .resume         = usbnet_resume,
 861        .disconnect     = usbnet_disconnect,
 862        .supports_autosuspend = 1,
 863};
 864
 865module_usb_driver(sr_driver);
 866
 867MODULE_AUTHOR("Liu Junliang <liujunliang_ljl@163.com");
 868MODULE_VERSION(DRIVER_VERSION);
 869MODULE_DESCRIPTION("SR9800 USB 2.0 USB2NET Dev : http://www.corechip-sz.com");
 870MODULE_LICENSE("GPL");
 871