1 /* 2 * CoreChip-sz SR9700 one chip USB 1.1 Ethernet Devices 3 * 4 * Author : Liu Junliang <liujunliang_ljl@163.com> 5 * 6 * Based on dm9601.c 7 * 8 * This file is licensed under the terms of the GNU General Public License 9 * version 2. This program is licensed "as is" without any warranty of any 10 * kind, whether express or implied. 11 */ 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/usb.h> 20 #include <linux/crc32.h> 21 #include <linux/usb/usbnet.h> 22 23 #include "sr9700.h" 24 25 static int sr_read(struct usbnet *dev, u8 reg, u16 length, void *data) 26 { 27 int err; 28 29 err = usbnet_read_cmd(dev, SR_RD_REGS, SR_REQ_RD_REG, 0, reg, data, 30 length); 31 if ((err != length) && (err >= 0)) 32 err = -EINVAL; 33 return err; 34 } 35 36 static int sr_write(struct usbnet *dev, u8 reg, u16 length, void *data) 37 { 38 int err; 39 40 err = usbnet_write_cmd(dev, SR_WR_REGS, SR_REQ_WR_REG, 0, reg, data, 41 length); 42 if ((err >= 0) && (err < length)) 43 err = -EINVAL; 44 return err; 45 } 46 47 static int sr_read_reg(struct usbnet *dev, u8 reg, u8 *value) 48 { 49 return sr_read(dev, reg, 1, value); 50 } 51 52 static int sr_write_reg(struct usbnet *dev, u8 reg, u8 value) 53 { 54 return usbnet_write_cmd(dev, SR_WR_REG, SR_REQ_WR_REG, 55 value, reg, NULL, 0); 56 } 57 58 static void sr_write_async(struct usbnet *dev, u8 reg, u16 length, 59 const void *data) 60 { 61 usbnet_write_cmd_async(dev, SR_WR_REGS, SR_REQ_WR_REG, 62 0, reg, data, length); 63 } 64 65 static void sr_write_reg_async(struct usbnet *dev, u8 reg, u8 value) 66 { 67 usbnet_write_cmd_async(dev, SR_WR_REG, SR_REQ_WR_REG, 68 value, reg, NULL, 0); 69 } 70 71 static int wait_eeprom_ready(struct usbnet *dev) 72 { 73 int i; 74 75 for (i = 0; i < SR_EEPROM_TIMEOUT; i++) { 76 u8 tmp = 0; 77 int ret; 78 79 udelay(1); 80 ret = sr_read_reg(dev, SR_EPCR, &tmp); 81 if (ret < 0) 82 return ret; 83 84 /* ready */ 85 if (!(tmp & EPCR_ERRE)) 86 return 0; 87 } 88 89 netdev_err(dev->net, "eeprom write timed out!\n"); 90 91 return -EIO; 92 } 93 94 static int sr_read_eeprom_word(struct usbnet *dev, u8 reg, __le16 *value) 95 { 96 int ret; 97 98 mutex_lock(&dev->phy_mutex); 99 100 sr_write_reg(dev, SR_EPAR, reg); 101 sr_write_reg(dev, SR_EPCR, EPCR_ERPRR); 102 103 ret = wait_eeprom_ready(dev); 104 if (ret < 0) 105 goto out_unlock; 106 107 sr_write_reg(dev, SR_EPCR, 0x0); 108 ret = sr_read(dev, SR_EPDR, 2, value); 109 110 netdev_dbg(dev->net, "read eeprom 0x%02x returned 0x%04x, %d\n", 111 reg, *value, ret); 112 113 out_unlock: 114 mutex_unlock(&dev->phy_mutex); 115 return ret; 116 } 117 118 static int __maybe_unused sr_write_eeprom_word(struct usbnet *dev, u8 reg, 119 __le16 value) 120 { 121 int ret; 122 123 mutex_lock(&dev->phy_mutex); 124 125 ret = sr_write(dev, SR_EPDR, 2, &value); 126 if (ret < 0) 127 goto out_unlock; 128 129 sr_write_reg(dev, SR_EPAR, reg); 130 sr_write_reg(dev, SR_EPCR, EPCR_WEP | EPCR_ERPRW); 131 132 ret = wait_eeprom_ready(dev); 133 if (ret < 0) 134 goto out_unlock; 135 136 sr_write_reg(dev, SR_EPCR, 0x0); 137 138 out_unlock: 139 mutex_unlock(&dev->phy_mutex); 140 return ret; 141 } 142 143 static int sr9700_get_eeprom_len(struct net_device *netdev) 144 { 145 return SR_EEPROM_LEN; 146 } 147 148 static int sr9700_get_eeprom(struct net_device *netdev, 149 struct ethtool_eeprom *eeprom, u8 *data) 150 { 151 struct usbnet *dev = netdev_priv(netdev); 152 __le16 *buf = (__le16 *)data; 153 int ret = 0; 154 int i; 155 156 /* access is 16bit */ 157 if ((eeprom->offset & 0x01) || (eeprom->len & 0x01)) 158 return -EINVAL; 159 160 for (i = 0; i < eeprom->len / 2; i++) { 161 ret = sr_read_eeprom_word(dev, eeprom->offset / 2 + i, buf + i); 162 if (ret < 0) 163 break; 164 } 165 166 return ret; 167 } 168 169 static void sr9700_handle_link_change(struct net_device *netdev, bool link) 170 { 171 if (netif_carrier_ok(netdev) != link) { 172 if (link) { 173 netif_carrier_on(netdev); 174 netdev_info(netdev, "link up, 10Mbps, half-duplex\n"); 175 } else { 176 netif_carrier_off(netdev); 177 netdev_info(netdev, "link down\n"); 178 } 179 } 180 } 181 182 static u32 sr9700_get_link(struct net_device *netdev) 183 { 184 struct usbnet *dev = netdev_priv(netdev); 185 u8 value = 0; 186 u32 link = 0; 187 188 sr_read_reg(dev, SR_NSR, &value); 189 link = !!(value & NSR_LINKST); 190 191 sr9700_handle_link_change(netdev, link); 192 193 return link; 194 } 195 196 /* 197 * The device supports only 10Mbps half-duplex operation. It implements the 198 * DM9601 speed/duplex status registers, but as the values are always the same, 199 * using them would add unnecessary complexity. 200 */ 201 static int sr9700_get_link_ksettings(struct net_device *dev, 202 struct ethtool_link_ksettings *cmd) 203 { 204 ethtool_link_ksettings_zero_link_mode(cmd, supported); 205 ethtool_link_ksettings_add_link_mode(cmd, supported, 10baseT_Half); 206 ethtool_link_ksettings_add_link_mode(cmd, supported, TP); 207 208 ethtool_link_ksettings_zero_link_mode(cmd, advertising); 209 ethtool_link_ksettings_add_link_mode(cmd, advertising, 10baseT_Half); 210 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP); 211 212 cmd->base.speed = SPEED_10; 213 cmd->base.duplex = DUPLEX_HALF; 214 cmd->base.port = PORT_TP; 215 cmd->base.phy_address = 0; 216 cmd->base.autoneg = AUTONEG_DISABLE; 217 218 return 0; 219 } 220 221 static const struct ethtool_ops sr9700_ethtool_ops = { 222 .get_drvinfo = usbnet_get_drvinfo, 223 .get_link = sr9700_get_link, 224 .get_msglevel = usbnet_get_msglevel, 225 .set_msglevel = usbnet_set_msglevel, 226 .get_eeprom_len = sr9700_get_eeprom_len, 227 .get_eeprom = sr9700_get_eeprom, 228 .get_link_ksettings = sr9700_get_link_ksettings, 229 }; 230 231 static void sr9700_set_multicast(struct net_device *netdev) 232 { 233 struct usbnet *dev = netdev_priv(netdev); 234 /* We use the 20 byte dev->data for our 8 byte filter buffer 235 * to avoid allocating memory that is tricky to free later 236 */ 237 u8 *hashes = (u8 *)&dev->data; 238 /* rx_ctl setting : enable, disable_long, disable_crc */ 239 u8 rx_ctl = RCR_RXEN | RCR_DIS_CRC | RCR_DIS_LONG; 240 241 memset(hashes, 0x00, SR_MCAST_SIZE); 242 /* broadcast address */ 243 hashes[SR_MCAST_SIZE - 1] |= SR_MCAST_ADDR_FLAG; 244 if (netdev->flags & IFF_PROMISC) { 245 rx_ctl |= RCR_PRMSC; 246 } else if (netdev->flags & IFF_ALLMULTI || 247 netdev_mc_count(netdev) > SR_MCAST_MAX) { 248 rx_ctl |= RCR_RUNT; 249 } else if (!netdev_mc_empty(netdev)) { 250 struct netdev_hw_addr *ha; 251 252 netdev_for_each_mc_addr(ha, netdev) { 253 u32 crc = ether_crc(ETH_ALEN, ha->addr) >> 26; 254 hashes[crc >> 3] |= 1 << (crc & 0x7); 255 } 256 } 257 258 sr_write_async(dev, SR_MAR, SR_MCAST_SIZE, hashes); 259 sr_write_reg_async(dev, SR_RCR, rx_ctl); 260 } 261 262 static int sr9700_set_mac_address(struct net_device *netdev, void *p) 263 { 264 struct usbnet *dev = netdev_priv(netdev); 265 struct sockaddr *addr = p; 266 267 if (!is_valid_ether_addr(addr->sa_data)) { 268 netdev_err(netdev, "not setting invalid mac address %pM\n", 269 addr->sa_data); 270 return -EINVAL; 271 } 272 273 eth_hw_addr_set(netdev, addr->sa_data); 274 sr_write_async(dev, SR_PAR, 6, netdev->dev_addr); 275 276 return 0; 277 } 278 279 static const struct net_device_ops sr9700_netdev_ops = { 280 .ndo_open = usbnet_open, 281 .ndo_stop = usbnet_stop, 282 .ndo_start_xmit = usbnet_start_xmit, 283 .ndo_tx_timeout = usbnet_tx_timeout, 284 .ndo_change_mtu = usbnet_change_mtu, 285 .ndo_get_stats64 = dev_get_tstats64, 286 .ndo_validate_addr = eth_validate_addr, 287 .ndo_set_rx_mode = sr9700_set_multicast, 288 .ndo_set_mac_address = sr9700_set_mac_address, 289 }; 290 291 static int sr9700_bind(struct usbnet *dev, struct usb_interface *intf) 292 { 293 struct net_device *netdev; 294 u8 addr[ETH_ALEN]; 295 int ret; 296 297 ret = usbnet_get_endpoints(dev, intf); 298 if (ret) 299 goto out; 300 301 netdev = dev->net; 302 303 netdev->netdev_ops = &sr9700_netdev_ops; 304 netdev->ethtool_ops = &sr9700_ethtool_ops; 305 netdev->hard_header_len += SR_TX_OVERHEAD; 306 dev->hard_mtu = netdev->mtu + netdev->hard_header_len; 307 /* bulkin buffer is preferably not less than 3K */ 308 dev->rx_urb_size = 3072; 309 310 sr_write_reg(dev, SR_NCR, NCR_RST); 311 udelay(20); 312 313 /* read MAC 314 * After Chip Power on, the Chip will reload the MAC from 315 * EEPROM automatically to PAR. In case there is no EEPROM externally, 316 * a default MAC address is stored in PAR for making chip work properly. 317 */ 318 if (sr_read(dev, SR_PAR, ETH_ALEN, addr) < 0) { 319 netdev_err(netdev, "Error reading MAC address\n"); 320 ret = -ENODEV; 321 goto out; 322 } 323 eth_hw_addr_set(netdev, addr); 324 325 /* power up and reset phy */ 326 sr_write_reg(dev, SR_PRR, PRR_PHY_RST); 327 /* at least 10ms, here 20ms for safe */ 328 msleep(20); 329 sr_write_reg(dev, SR_PRR, 0); 330 /* at least 1ms, here 2ms for reading right register */ 331 udelay(2 * 1000); 332 333 /* receive broadcast packets */ 334 sr9700_set_multicast(netdev); 335 336 out: 337 return ret; 338 } 339 340 static int sr9700_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 341 { 342 struct sk_buff *sr_skb; 343 int len; 344 345 /* skb content (packets) format : 346 * p1 p2 p3 ...... pn 347 * / \ 348 * / \ 349 * / \ 350 * / \ 351 * p1b1 p1b2 p1b3 p1b4 ...... p1b(n-4) p1b(n-3)...p1bn 352 * 353 * p1 : packet 1 354 * p1b1 : packet 1 byte 1 355 * 356 * b1: rx status 357 * b2: packet length (incl crc) low 358 * b3: packet length (incl crc) high 359 * b4..n-4: packet data 360 * bn-3..bn: ethernet packet crc 361 */ 362 if (unlikely(skb->len < SR_RX_OVERHEAD)) { 363 netdev_err(dev->net, "unexpected tiny rx frame\n"); 364 return 0; 365 } 366 367 /* one skb may contains multiple packets */ 368 while (skb->len > SR_RX_OVERHEAD) { 369 if (skb->data[0] != 0x40) 370 return 0; 371 372 /* ignore the CRC length */ 373 len = (skb->data[1] | (skb->data[2] << 8)) - 4; 374 375 if (len > ETH_FRAME_LEN || len > skb->len || len < 0) 376 return 0; 377 378 /* the last packet of current skb */ 379 if (skb->len == (len + SR_RX_OVERHEAD)) { 380 skb_pull(skb, 3); 381 skb->len = len; 382 skb_set_tail_pointer(skb, len); 383 return 2; 384 } 385 386 sr_skb = netdev_alloc_skb_ip_align(dev->net, len); 387 if (!sr_skb) 388 return 0; 389 390 skb_put(sr_skb, len); 391 memcpy(sr_skb->data, skb->data + 3, len); 392 usbnet_skb_return(dev, sr_skb); 393 394 skb_pull(skb, len + SR_RX_OVERHEAD); 395 } 396 397 return 0; 398 } 399 400 static struct sk_buff *sr9700_tx_fixup(struct usbnet *dev, struct sk_buff *skb, 401 gfp_t flags) 402 { 403 int len; 404 405 /* SR9700 can only send out one ethernet packet at once. 406 * 407 * b1 b2 b3 b4 ...... b(n-4) b(n-3)...bn 408 * 409 * b1: rx status 410 * b2: packet length (incl crc) low 411 * b3: packet length (incl crc) high 412 * b4..n-4: packet data 413 * bn-3..bn: ethernet packet crc 414 */ 415 416 len = skb->len; 417 418 if (skb_cow_head(skb, SR_TX_OVERHEAD)) { 419 dev_kfree_skb_any(skb); 420 return NULL; 421 } 422 423 __skb_push(skb, SR_TX_OVERHEAD); 424 425 /* usbnet adds padding if length is a multiple of packet size 426 * if so, adjust length value in header 427 */ 428 if ((skb->len % dev->maxpacket) == 0) 429 len++; 430 431 skb->data[0] = len; 432 skb->data[1] = len >> 8; 433 434 return skb; 435 } 436 437 static void sr9700_status(struct usbnet *dev, struct urb *urb) 438 { 439 bool link; 440 u8 *buf; 441 442 /* format: 443 b1: net status 444 b2: tx status 1 445 b3: tx status 2 446 b4: rx status 447 b5: rx overflow 448 b6: rx count 449 b7: tx count 450 b8: gpr 451 */ 452 453 if (urb->actual_length < 8) 454 return; 455 456 buf = urb->transfer_buffer; 457 458 link = !!(buf[0] & 0x40); 459 sr9700_handle_link_change(dev->net, link); 460 } 461 462 static const struct driver_info sr9700_driver_info = { 463 .description = "CoreChip SR9700 USB Ethernet", 464 .flags = FLAG_ETHER, 465 .bind = sr9700_bind, 466 .rx_fixup = sr9700_rx_fixup, 467 .tx_fixup = sr9700_tx_fixup, 468 .status = sr9700_status, 469 }; 470 471 static const struct usb_device_id products[] = { 472 { 473 USB_DEVICE(0x0fe6, 0x9700), /* SR9700 device */ 474 .driver_info = (unsigned long)&sr9700_driver_info, 475 }, 476 { 477 /* SR9700 with virtual driver CD-ROM - interface 0 is the CD-ROM device */ 478 USB_DEVICE_INTERFACE_NUMBER(0x0fe6, 0x9702, 1), 479 .driver_info = (unsigned long)&sr9700_driver_info, 480 }, 481 {}, /* END */ 482 }; 483 484 MODULE_DEVICE_TABLE(usb, products); 485 486 static struct usb_driver sr9700_usb_driver = { 487 .name = "sr9700", 488 .id_table = products, 489 .probe = usbnet_probe, 490 .disconnect = usbnet_disconnect, 491 .suspend = usbnet_suspend, 492 .resume = usbnet_resume, 493 .disable_hub_initiated_lpm = 1, 494 }; 495 496 module_usb_driver(sr9700_usb_driver); 497 498 MODULE_AUTHOR("liujl <liujunliang_ljl@163.com>"); 499 MODULE_DESCRIPTION("SR9700 one chip USB 1.1 USB to Ethernet device from http://www.corechip-sz.com/"); 500 MODULE_LICENSE("GPL"); 501