1 /* 2 * Davicom DM96xx USB 10/100Mbps ethernet devices 3 * 4 * Peter Korsgaard <jacmet@sunsite.dk> 5 * 6 * This file is licensed under the terms of the GNU General Public License 7 * version 2. This program is licensed "as is" without any warranty of any 8 * kind, whether express or implied. 9 */ 10 11 //#define DEBUG 12 13 #include <linux/module.h> 14 #include <linux/sched.h> 15 #include <linux/stddef.h> 16 #include <linux/netdevice.h> 17 #include <linux/etherdevice.h> 18 #include <linux/ethtool.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 25 /* datasheet: 26 http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/DM9601-DS-P01-930914.pdf 27 */ 28 29 /* control requests */ 30 #define DM_READ_REGS 0x00 31 #define DM_WRITE_REGS 0x01 32 #define DM_READ_MEMS 0x02 33 #define DM_WRITE_REG 0x03 34 #define DM_WRITE_MEMS 0x05 35 #define DM_WRITE_MEM 0x07 36 37 /* registers */ 38 #define DM_NET_CTRL 0x00 39 #define DM_RX_CTRL 0x05 40 #define DM_SHARED_CTRL 0x0b 41 #define DM_SHARED_ADDR 0x0c 42 #define DM_SHARED_DATA 0x0d /* low + high */ 43 #define DM_PHY_ADDR 0x10 /* 6 bytes */ 44 #define DM_MCAST_ADDR 0x16 /* 8 bytes */ 45 #define DM_GPR_CTRL 0x1e 46 #define DM_GPR_DATA 0x1f 47 #define DM_CHIP_ID 0x2c 48 #define DM_MODE_CTRL 0x91 /* only on dm9620 */ 49 50 /* chip id values */ 51 #define ID_DM9601 0 52 #define ID_DM9620 1 53 54 #define DM_MAX_MCAST 64 55 #define DM_MCAST_SIZE 8 56 #define DM_EEPROM_LEN 256 57 #define DM_TX_OVERHEAD 2 /* 2 byte header */ 58 #define DM_RX_OVERHEAD 7 /* 3 byte header + 4 byte crc tail */ 59 #define DM_TIMEOUT 1000 60 61 static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data) 62 { 63 int err; 64 err = usbnet_read_cmd(dev, DM_READ_REGS, 65 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 66 0, reg, data, length); 67 if(err != length && err >= 0) 68 err = -EINVAL; 69 return err; 70 } 71 72 static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value) 73 { 74 return dm_read(dev, reg, 1, value); 75 } 76 77 static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data) 78 { 79 int err; 80 err = usbnet_write_cmd(dev, DM_WRITE_REGS, 81 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 82 0, reg, data, length); 83 84 if (err >= 0 && err < length) 85 err = -EINVAL; 86 return err; 87 } 88 89 static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value) 90 { 91 return usbnet_write_cmd(dev, DM_WRITE_REG, 92 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 93 value, reg, NULL, 0); 94 } 95 96 static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, 97 const void *data) 98 { 99 usbnet_write_cmd_async(dev, DM_WRITE_REGS, 100 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 101 0, reg, data, length); 102 } 103 104 static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value) 105 { 106 usbnet_write_cmd_async(dev, DM_WRITE_REG, 107 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 108 value, reg, NULL, 0); 109 } 110 111 static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 *value) 112 { 113 int ret, i; 114 115 mutex_lock(&dev->phy_mutex); 116 117 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg); 118 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4); 119 120 for (i = 0; i < DM_TIMEOUT; i++) { 121 u8 tmp = 0; 122 123 udelay(1); 124 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp); 125 if (ret < 0) 126 goto out; 127 128 /* ready */ 129 if ((tmp & 1) == 0) 130 break; 131 } 132 133 if (i == DM_TIMEOUT) { 134 netdev_err(dev->net, "%s read timed out!\n", phy ? "phy" : "eeprom"); 135 ret = -EIO; 136 goto out; 137 } 138 139 dm_write_reg(dev, DM_SHARED_CTRL, 0x0); 140 ret = dm_read(dev, DM_SHARED_DATA, 2, value); 141 142 netdev_dbg(dev->net, "read shared %d 0x%02x returned 0x%04x, %d\n", 143 phy, reg, *value, ret); 144 145 out: 146 mutex_unlock(&dev->phy_mutex); 147 return ret; 148 } 149 150 static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 value) 151 { 152 int ret, i; 153 154 mutex_lock(&dev->phy_mutex); 155 156 ret = dm_write(dev, DM_SHARED_DATA, 2, &value); 157 if (ret < 0) 158 goto out; 159 160 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg); 161 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1a : 0x12); 162 163 for (i = 0; i < DM_TIMEOUT; i++) { 164 u8 tmp = 0; 165 166 udelay(1); 167 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp); 168 if (ret < 0) 169 goto out; 170 171 /* ready */ 172 if ((tmp & 1) == 0) 173 break; 174 } 175 176 if (i == DM_TIMEOUT) { 177 netdev_err(dev->net, "%s write timed out!\n", phy ? "phy" : "eeprom"); 178 ret = -EIO; 179 goto out; 180 } 181 182 dm_write_reg(dev, DM_SHARED_CTRL, 0x0); 183 184 out: 185 mutex_unlock(&dev->phy_mutex); 186 return ret; 187 } 188 189 static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value) 190 { 191 return dm_read_shared_word(dev, 0, offset, value); 192 } 193 194 195 196 static int dm9601_get_eeprom_len(struct net_device *dev) 197 { 198 return DM_EEPROM_LEN; 199 } 200 201 static int dm9601_get_eeprom(struct net_device *net, 202 struct ethtool_eeprom *eeprom, u8 * data) 203 { 204 struct usbnet *dev = netdev_priv(net); 205 __le16 *ebuf = (__le16 *) data; 206 int i; 207 208 /* access is 16bit */ 209 if ((eeprom->offset % 2) || (eeprom->len % 2)) 210 return -EINVAL; 211 212 for (i = 0; i < eeprom->len / 2; i++) { 213 if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i, 214 &ebuf[i]) < 0) 215 return -EINVAL; 216 } 217 return 0; 218 } 219 220 static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc) 221 { 222 struct usbnet *dev = netdev_priv(netdev); 223 224 __le16 res; 225 int err; 226 227 if (phy_id) { 228 netdev_dbg(dev->net, "Only internal phy supported\n"); 229 return 0; 230 } 231 232 err = dm_read_shared_word(dev, 1, loc, &res); 233 if (err < 0) { 234 netdev_err(dev->net, "MDIO read error: %d\n", err); 235 return 0; 236 } 237 238 netdev_dbg(dev->net, 239 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", 240 phy_id, loc, le16_to_cpu(res)); 241 242 return le16_to_cpu(res); 243 } 244 245 static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc, 246 int val) 247 { 248 struct usbnet *dev = netdev_priv(netdev); 249 __le16 res = cpu_to_le16(val); 250 251 if (phy_id) { 252 netdev_dbg(dev->net, "Only internal phy supported\n"); 253 return; 254 } 255 256 netdev_dbg(dev->net, "dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", 257 phy_id, loc, val); 258 259 dm_write_shared_word(dev, 1, loc, res); 260 } 261 262 static void dm9601_get_drvinfo(struct net_device *net, 263 struct ethtool_drvinfo *info) 264 { 265 /* Inherit standard device info */ 266 usbnet_get_drvinfo(net, info); 267 } 268 269 static u32 dm9601_get_link(struct net_device *net) 270 { 271 struct usbnet *dev = netdev_priv(net); 272 273 return mii_link_ok(&dev->mii); 274 } 275 276 static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd) 277 { 278 struct usbnet *dev = netdev_priv(net); 279 280 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL); 281 } 282 283 static const struct ethtool_ops dm9601_ethtool_ops = { 284 .get_drvinfo = dm9601_get_drvinfo, 285 .get_link = dm9601_get_link, 286 .get_msglevel = usbnet_get_msglevel, 287 .set_msglevel = usbnet_set_msglevel, 288 .get_eeprom_len = dm9601_get_eeprom_len, 289 .get_eeprom = dm9601_get_eeprom, 290 .nway_reset = usbnet_nway_reset, 291 .get_link_ksettings = usbnet_get_link_ksettings_mii, 292 .set_link_ksettings = usbnet_set_link_ksettings_mii, 293 }; 294 295 static void dm9601_set_multicast(struct net_device *net) 296 { 297 struct usbnet *dev = netdev_priv(net); 298 /* We use the 20 byte dev->data for our 8 byte filter buffer 299 * to avoid allocating memory that is tricky to free later */ 300 u8 *hashes = (u8 *) & dev->data; 301 u8 rx_ctl = 0x31; 302 303 memset(hashes, 0x00, DM_MCAST_SIZE); 304 hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */ 305 306 if (net->flags & IFF_PROMISC) { 307 rx_ctl |= 0x02; 308 } else if (net->flags & IFF_ALLMULTI || 309 netdev_mc_count(net) > DM_MAX_MCAST) { 310 rx_ctl |= 0x08; 311 } else if (!netdev_mc_empty(net)) { 312 struct netdev_hw_addr *ha; 313 314 netdev_for_each_mc_addr(ha, net) { 315 u32 crc = ether_crc(ETH_ALEN, ha->addr) >> 26; 316 hashes[crc >> 3] |= 1 << (crc & 0x7); 317 } 318 } 319 320 dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes); 321 dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl); 322 } 323 324 static void __dm9601_set_mac_address(struct usbnet *dev) 325 { 326 dm_write_async(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr); 327 } 328 329 static int dm9601_set_mac_address(struct net_device *net, void *p) 330 { 331 struct sockaddr *addr = p; 332 struct usbnet *dev = netdev_priv(net); 333 334 if (!is_valid_ether_addr(addr->sa_data)) { 335 dev_err(&net->dev, "not setting invalid mac address %pM\n", 336 addr->sa_data); 337 return -EINVAL; 338 } 339 340 eth_hw_addr_set(net, addr->sa_data); 341 __dm9601_set_mac_address(dev); 342 343 return 0; 344 } 345 346 static const struct net_device_ops dm9601_netdev_ops = { 347 .ndo_open = usbnet_open, 348 .ndo_stop = usbnet_stop, 349 .ndo_start_xmit = usbnet_start_xmit, 350 .ndo_tx_timeout = usbnet_tx_timeout, 351 .ndo_change_mtu = usbnet_change_mtu, 352 .ndo_get_stats64 = dev_get_tstats64, 353 .ndo_validate_addr = eth_validate_addr, 354 .ndo_eth_ioctl = dm9601_ioctl, 355 .ndo_set_rx_mode = dm9601_set_multicast, 356 .ndo_set_mac_address = dm9601_set_mac_address, 357 }; 358 359 static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf) 360 { 361 int ret; 362 u8 mac[ETH_ALEN], id; 363 364 ret = usbnet_get_endpoints(dev, intf); 365 if (ret) 366 goto out; 367 368 dev->net->netdev_ops = &dm9601_netdev_ops; 369 dev->net->ethtool_ops = &dm9601_ethtool_ops; 370 dev->net->hard_header_len += DM_TX_OVERHEAD; 371 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len; 372 373 /* dm9620/21a require room for 4 byte padding, even in dm9601 374 * mode, so we need +1 to be able to receive full size 375 * ethernet frames. 376 */ 377 dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD + 1; 378 379 dev->mii.dev = dev->net; 380 dev->mii.mdio_read = dm9601_mdio_read; 381 dev->mii.mdio_write = dm9601_mdio_write; 382 dev->mii.phy_id_mask = 0x1f; 383 dev->mii.reg_num_mask = 0x1f; 384 385 /* reset */ 386 dm_write_reg(dev, DM_NET_CTRL, 1); 387 udelay(20); 388 389 /* read MAC */ 390 if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, mac) < 0) { 391 printk(KERN_ERR "Error reading MAC address\n"); 392 ret = -ENODEV; 393 goto out; 394 } 395 396 /* 397 * Overwrite the auto-generated address only with good ones. 398 */ 399 if (is_valid_ether_addr(mac)) 400 eth_hw_addr_set(dev->net, mac); 401 else { 402 printk(KERN_WARNING 403 "dm9601: No valid MAC address in EEPROM, using %pM\n", 404 dev->net->dev_addr); 405 __dm9601_set_mac_address(dev); 406 } 407 408 if (dm_read_reg(dev, DM_CHIP_ID, &id) < 0) { 409 netdev_err(dev->net, "Error reading chip ID\n"); 410 ret = -ENODEV; 411 goto out; 412 } 413 414 /* put dm9620 devices in dm9601 mode */ 415 if (id == ID_DM9620) { 416 u8 mode; 417 418 if (dm_read_reg(dev, DM_MODE_CTRL, &mode) < 0) { 419 netdev_err(dev->net, "Error reading MODE_CTRL\n"); 420 ret = -ENODEV; 421 goto out; 422 } 423 dm_write_reg(dev, DM_MODE_CTRL, mode & 0x7f); 424 } 425 426 /* power up phy */ 427 dm_write_reg(dev, DM_GPR_CTRL, 1); 428 dm_write_reg(dev, DM_GPR_DATA, 0); 429 430 /* receive broadcast packets */ 431 dm9601_set_multicast(dev->net); 432 433 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET); 434 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE, 435 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP); 436 mii_nway_restart(&dev->mii); 437 438 out: 439 return ret; 440 } 441 442 static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 443 { 444 u8 status; 445 int len; 446 447 /* format: 448 b1: rx status 449 b2: packet length (incl crc) low 450 b3: packet length (incl crc) high 451 b4..n-4: packet data 452 bn-3..bn: ethernet crc 453 */ 454 455 if (unlikely(skb->len < DM_RX_OVERHEAD)) { 456 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n"); 457 return 0; 458 } 459 460 status = skb->data[0]; 461 len = (skb->data[1] | (skb->data[2] << 8)) - 4; 462 463 if (unlikely(status & 0xbf)) { 464 if (status & 0x01) dev->net->stats.rx_fifo_errors++; 465 if (status & 0x02) dev->net->stats.rx_crc_errors++; 466 if (status & 0x04) dev->net->stats.rx_frame_errors++; 467 if (status & 0x20) dev->net->stats.rx_missed_errors++; 468 if (status & 0x90) dev->net->stats.rx_length_errors++; 469 return 0; 470 } 471 472 skb_pull(skb, 3); 473 skb_trim(skb, len); 474 475 return 1; 476 } 477 478 static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb, 479 gfp_t flags) 480 { 481 int len, pad; 482 483 /* format: 484 b1: packet length low 485 b2: packet length high 486 b3..n: packet data 487 */ 488 489 len = skb->len + DM_TX_OVERHEAD; 490 491 /* workaround for dm962x errata with tx fifo getting out of 492 * sync if a USB bulk transfer retry happens right after a 493 * packet with odd / maxpacket length by adding up to 3 bytes 494 * padding. 495 */ 496 while ((len & 1) || !(len % dev->maxpacket)) 497 len++; 498 499 len -= DM_TX_OVERHEAD; /* hw header doesn't count as part of length */ 500 pad = len - skb->len; 501 502 if (skb_headroom(skb) < DM_TX_OVERHEAD || skb_tailroom(skb) < pad) { 503 struct sk_buff *skb2; 504 505 skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, pad, flags); 506 dev_kfree_skb_any(skb); 507 skb = skb2; 508 if (!skb) 509 return NULL; 510 } 511 512 __skb_push(skb, DM_TX_OVERHEAD); 513 514 if (pad) { 515 memset(skb->data + skb->len, 0, pad); 516 __skb_put(skb, pad); 517 } 518 519 skb->data[0] = len; 520 skb->data[1] = len >> 8; 521 522 return skb; 523 } 524 525 static void dm9601_status(struct usbnet *dev, struct urb *urb) 526 { 527 int link; 528 u8 *buf; 529 530 /* format: 531 b0: net status 532 b1: tx status 1 533 b2: tx status 2 534 b3: rx status 535 b4: rx overflow 536 b5: rx count 537 b6: tx count 538 b7: gpr 539 */ 540 541 if (urb->actual_length < 8) 542 return; 543 544 buf = urb->transfer_buffer; 545 546 link = !!(buf[0] & 0x40); 547 if (netif_carrier_ok(dev->net) != link) { 548 usbnet_link_change(dev, link, 1); 549 netdev_dbg(dev->net, "Link Status is: %d\n", link); 550 } 551 } 552 553 static int dm9601_link_reset(struct usbnet *dev) 554 { 555 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET }; 556 557 mii_check_media(&dev->mii, 1, 1); 558 mii_ethtool_gset(&dev->mii, &ecmd); 559 560 netdev_dbg(dev->net, "link_reset() speed: %u duplex: %d\n", 561 ethtool_cmd_speed(&ecmd), ecmd.duplex); 562 563 return 0; 564 } 565 566 static const struct driver_info dm9601_info = { 567 .description = "Davicom DM96xx USB 10/100 Ethernet", 568 .flags = FLAG_ETHER | FLAG_LINK_INTR, 569 .bind = dm9601_bind, 570 .rx_fixup = dm9601_rx_fixup, 571 .tx_fixup = dm9601_tx_fixup, 572 .status = dm9601_status, 573 .link_reset = dm9601_link_reset, 574 .reset = dm9601_link_reset, 575 }; 576 577 static const struct usb_device_id products[] = { 578 { 579 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */ 580 .driver_info = (unsigned long)&dm9601_info, 581 }, 582 { 583 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */ 584 .driver_info = (unsigned long)&dm9601_info, 585 }, 586 { 587 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */ 588 .driver_info = (unsigned long)&dm9601_info, 589 }, 590 { 591 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */ 592 .driver_info = (unsigned long)&dm9601_info, 593 }, 594 { 595 USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */ 596 .driver_info = (unsigned long)&dm9601_info, 597 }, 598 { 599 USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */ 600 .driver_info = (unsigned long)&dm9601_info, 601 }, 602 { 603 USB_DEVICE(0x0fe6, 0x8101), /* DM9601 USB to Fast Ethernet Adapter */ 604 .driver_info = (unsigned long)&dm9601_info, 605 }, 606 { 607 USB_DEVICE(0x0fe6, 0x9700), /* DM9601 USB to Fast Ethernet Adapter */ 608 .driver_info = (unsigned long)&dm9601_info, 609 }, 610 { 611 USB_DEVICE(0x0a46, 0x9000), /* DM9000E */ 612 .driver_info = (unsigned long)&dm9601_info, 613 }, 614 { 615 USB_DEVICE(0x0a46, 0x9620), /* DM9620 USB to Fast Ethernet Adapter */ 616 .driver_info = (unsigned long)&dm9601_info, 617 }, 618 { 619 USB_DEVICE(0x0a46, 0x9621), /* DM9621A USB to Fast Ethernet Adapter */ 620 .driver_info = (unsigned long)&dm9601_info, 621 }, 622 { 623 USB_DEVICE(0x0a46, 0x9622), /* DM9622 USB to Fast Ethernet Adapter */ 624 .driver_info = (unsigned long)&dm9601_info, 625 }, 626 { 627 USB_DEVICE(0x0a46, 0x0269), /* DM962OA USB to Fast Ethernet Adapter */ 628 .driver_info = (unsigned long)&dm9601_info, 629 }, 630 { 631 USB_DEVICE(0x0a46, 0x1269), /* DM9621A USB to Fast Ethernet Adapter */ 632 .driver_info = (unsigned long)&dm9601_info, 633 }, 634 { 635 USB_DEVICE(0x0586, 0x3427), /* ZyXEL Keenetic Plus DSL xDSL modem */ 636 .driver_info = (unsigned long)&dm9601_info, 637 }, 638 {}, // END 639 }; 640 641 MODULE_DEVICE_TABLE(usb, products); 642 643 static struct usb_driver dm9601_driver = { 644 .name = "dm9601", 645 .id_table = products, 646 .probe = usbnet_probe, 647 .disconnect = usbnet_disconnect, 648 .suspend = usbnet_suspend, 649 .resume = usbnet_resume, 650 .disable_hub_initiated_lpm = 1, 651 }; 652 653 module_usb_driver(dm9601_driver); 654 655 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>"); 656 MODULE_DESCRIPTION("Davicom DM96xx USB 10/100 ethernet devices"); 657 MODULE_LICENSE("GPL"); 658