1b4cdae20SChristian Riesch /* 2b4cdae20SChristian Riesch * ASIX AX8817X based USB 2.0 Ethernet Devices 3b4cdae20SChristian Riesch * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com> 4b4cdae20SChristian Riesch * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net> 5b4cdae20SChristian Riesch * Copyright (C) 2006 James Painter <jamie.painter@iname.com> 6b4cdae20SChristian Riesch * Copyright (c) 2002-2003 TiVo Inc. 7b4cdae20SChristian Riesch * 8b4cdae20SChristian Riesch * This program is free software; you can redistribute it and/or modify 9b4cdae20SChristian Riesch * it under the terms of the GNU General Public License as published by 10b4cdae20SChristian Riesch * the Free Software Foundation; either version 2 of the License, or 11b4cdae20SChristian Riesch * (at your option) any later version. 12b4cdae20SChristian Riesch * 13b4cdae20SChristian Riesch * This program is distributed in the hope that it will be useful, 14b4cdae20SChristian Riesch * but WITHOUT ANY WARRANTY; without even the implied warranty of 15b4cdae20SChristian Riesch * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16b4cdae20SChristian Riesch * GNU General Public License for more details. 17b4cdae20SChristian Riesch * 18b4cdae20SChristian Riesch * You should have received a copy of the GNU General Public License 199cb00073SJeff Kirsher * along with this program; if not, see <http://www.gnu.org/licenses/>. 20b4cdae20SChristian Riesch */ 21b4cdae20SChristian Riesch 22607740bcSChristian Riesch #include "asix.h" 23b4cdae20SChristian Riesch 24607740bcSChristian Riesch int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, 25b4cdae20SChristian Riesch u16 size, void *data) 26b4cdae20SChristian Riesch { 270bc69efbSMing Lei int ret; 280bc69efbSMing Lei ret = usbnet_read_cmd(dev, cmd, 29b4cdae20SChristian Riesch USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 300bc69efbSMing Lei value, index, data, size); 31b4cdae20SChristian Riesch 320bc69efbSMing Lei if (ret != size && ret >= 0) 330bc69efbSMing Lei return -EINVAL; 340bc69efbSMing Lei return ret; 35b4cdae20SChristian Riesch } 36b4cdae20SChristian Riesch 37607740bcSChristian Riesch int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, 38b4cdae20SChristian Riesch u16 size, void *data) 39b4cdae20SChristian Riesch { 400bc69efbSMing Lei return usbnet_write_cmd(dev, cmd, 41b4cdae20SChristian Riesch USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 420bc69efbSMing Lei value, index, data, size); 43b4cdae20SChristian Riesch } 44b4cdae20SChristian Riesch 45607740bcSChristian Riesch void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index, 46b4cdae20SChristian Riesch u16 size, void *data) 47b4cdae20SChristian Riesch { 480bc69efbSMing Lei usbnet_write_cmd_async(dev, cmd, 490bc69efbSMing Lei USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 500bc69efbSMing Lei value, index, data, size); 51b4cdae20SChristian Riesch } 52b4cdae20SChristian Riesch 538b5b6f54SLucas Stach int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb, 548b5b6f54SLucas Stach struct asix_rx_fixup_info *rx) 55b4cdae20SChristian Riesch { 56b4cdae20SChristian Riesch int offset = 0; 577b0378f5SDean Jenkins u16 size; 58b4cdae20SChristian Riesch 598b5b6f54SLucas Stach while (offset + sizeof(u16) <= skb->len) { 607b0378f5SDean Jenkins u16 copy_length; 618b5b6f54SLucas Stach unsigned char *data; 62b4cdae20SChristian Riesch 637b0378f5SDean Jenkins if (!rx->remaining) { 643bfc69abSDean Jenkins if (skb->len - offset == sizeof(u16)) { 658b5b6f54SLucas Stach rx->header = get_unaligned_le16( 668b5b6f54SLucas Stach skb->data + offset); 678b5b6f54SLucas Stach rx->split_head = true; 688b5b6f54SLucas Stach offset += sizeof(u16); 698b5b6f54SLucas Stach break; 703bfc69abSDean Jenkins } 713bfc69abSDean Jenkins 723bfc69abSDean Jenkins if (rx->split_head == true) { 738b5b6f54SLucas Stach rx->header |= (get_unaligned_le16( 743bfc69abSDean Jenkins skb->data + offset) << 16); 758b5b6f54SLucas Stach rx->split_head = false; 768b5b6f54SLucas Stach offset += sizeof(u16); 778b5b6f54SLucas Stach } else { 788b5b6f54SLucas Stach rx->header = get_unaligned_le32(skb->data + 798b5b6f54SLucas Stach offset); 80b4cdae20SChristian Riesch offset += sizeof(u32); 818b5b6f54SLucas Stach } 82b4cdae20SChristian Riesch 837b0378f5SDean Jenkins /* take frame length from Data header 32-bit word */ 847b0378f5SDean Jenkins size = (u16)(rx->header & 0x7ff); 857b0378f5SDean Jenkins if (size != ((~rx->header >> 16) & 0x7ff)) { 868b5b6f54SLucas Stach netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n", 878b5b6f54SLucas Stach rx->header, offset); 888b5b6f54SLucas Stach return 0; 898b5b6f54SLucas Stach } 90*9a5ccd8eSDean Jenkins if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) { 91*9a5ccd8eSDean Jenkins netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n", 92*9a5ccd8eSDean Jenkins size); 93*9a5ccd8eSDean Jenkins return 0; 94*9a5ccd8eSDean Jenkins } 95*9a5ccd8eSDean Jenkins 967b0378f5SDean Jenkins rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size); 978b5b6f54SLucas Stach if (!rx->ax_skb) 98b4cdae20SChristian Riesch return 0; 99b4cdae20SChristian Riesch 100*9a5ccd8eSDean Jenkins rx->remaining = size; 101b4cdae20SChristian Riesch } 102b4cdae20SChristian Riesch 1037b0378f5SDean Jenkins if (rx->remaining > skb->len - offset) { 1047b0378f5SDean Jenkins copy_length = skb->len - offset; 1057b0378f5SDean Jenkins rx->remaining -= copy_length; 1067b0378f5SDean Jenkins } else { 1077b0378f5SDean Jenkins copy_length = rx->remaining; 1087b0378f5SDean Jenkins rx->remaining = 0; 1098b5b6f54SLucas Stach } 110b4cdae20SChristian Riesch 1117b0378f5SDean Jenkins data = skb_put(rx->ax_skb, copy_length); 1127b0378f5SDean Jenkins memcpy(data, skb->data + offset, copy_length); 1137b0378f5SDean Jenkins if (!rx->remaining) 1148b5b6f54SLucas Stach usbnet_skb_return(dev, rx->ax_skb); 1158b5b6f54SLucas Stach 1167b0378f5SDean Jenkins offset += (copy_length + 1) & 0xfffe; 117b4cdae20SChristian Riesch } 118b4cdae20SChristian Riesch 119b4cdae20SChristian Riesch if (skb->len != offset) { 1208b5b6f54SLucas Stach netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n", 1218b5b6f54SLucas Stach skb->len, offset); 122b4cdae20SChristian Riesch return 0; 123b4cdae20SChristian Riesch } 1248b5b6f54SLucas Stach 125b4cdae20SChristian Riesch return 1; 126b4cdae20SChristian Riesch } 127b4cdae20SChristian Riesch 1288b5b6f54SLucas Stach int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb) 1298b5b6f54SLucas Stach { 1308b5b6f54SLucas Stach struct asix_common_private *dp = dev->driver_priv; 1318b5b6f54SLucas Stach struct asix_rx_fixup_info *rx = &dp->rx_fixup_info; 1328b5b6f54SLucas Stach 1338b5b6f54SLucas Stach return asix_rx_fixup_internal(dev, skb, rx); 1348b5b6f54SLucas Stach } 1358b5b6f54SLucas Stach 136607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb, 137b4cdae20SChristian Riesch gfp_t flags) 138b4cdae20SChristian Riesch { 139b4cdae20SChristian Riesch int padlen; 140b4cdae20SChristian Riesch int headroom = skb_headroom(skb); 141b4cdae20SChristian Riesch int tailroom = skb_tailroom(skb); 142b4cdae20SChristian Riesch u32 packet_len; 143b4cdae20SChristian Riesch u32 padbytes = 0xffff0000; 144b4cdae20SChristian Riesch 145b4cdae20SChristian Riesch padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4; 146b4cdae20SChristian Riesch 147b4cdae20SChristian Riesch /* We need to push 4 bytes in front of frame (packet_len) 148b4cdae20SChristian Riesch * and maybe add 4 bytes after the end (if padlen is 4) 149b4cdae20SChristian Riesch * 150b4cdae20SChristian Riesch * Avoid skb_copy_expand() expensive call, using following rules : 151b4cdae20SChristian Riesch * - We are allowed to push 4 bytes in headroom if skb_header_cloned() 152b4cdae20SChristian Riesch * is false (and if we have 4 bytes of headroom) 153b4cdae20SChristian Riesch * - We are allowed to put 4 bytes at tail if skb_cloned() 154b4cdae20SChristian Riesch * is false (and if we have 4 bytes of tailroom) 155b4cdae20SChristian Riesch * 156b4cdae20SChristian Riesch * TCP packets for example are cloned, but skb_header_release() 157b4cdae20SChristian Riesch * was called in tcp stack, allowing us to use headroom for our needs. 158b4cdae20SChristian Riesch */ 159b4cdae20SChristian Riesch if (!skb_header_cloned(skb) && 160b4cdae20SChristian Riesch !(padlen && skb_cloned(skb)) && 161b4cdae20SChristian Riesch headroom + tailroom >= 4 + padlen) { 162b4cdae20SChristian Riesch /* following should not happen, but better be safe */ 163b4cdae20SChristian Riesch if (headroom < 4 || 164b4cdae20SChristian Riesch tailroom < padlen) { 165b4cdae20SChristian Riesch skb->data = memmove(skb->head + 4, skb->data, skb->len); 166b4cdae20SChristian Riesch skb_set_tail_pointer(skb, skb->len); 167b4cdae20SChristian Riesch } 168b4cdae20SChristian Riesch } else { 169b4cdae20SChristian Riesch struct sk_buff *skb2; 170b4cdae20SChristian Riesch 171b4cdae20SChristian Riesch skb2 = skb_copy_expand(skb, 4, padlen, flags); 172b4cdae20SChristian Riesch dev_kfree_skb_any(skb); 173b4cdae20SChristian Riesch skb = skb2; 174b4cdae20SChristian Riesch if (!skb) 175b4cdae20SChristian Riesch return NULL; 176b4cdae20SChristian Riesch } 177b4cdae20SChristian Riesch 178b4cdae20SChristian Riesch packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len; 179b4cdae20SChristian Riesch skb_push(skb, 4); 180b4cdae20SChristian Riesch cpu_to_le32s(&packet_len); 181b4cdae20SChristian Riesch skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len)); 182b4cdae20SChristian Riesch 183b4cdae20SChristian Riesch if (padlen) { 184b4cdae20SChristian Riesch cpu_to_le32s(&padbytes); 185b4cdae20SChristian Riesch memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes)); 186b4cdae20SChristian Riesch skb_put(skb, sizeof(padbytes)); 187b4cdae20SChristian Riesch } 1881e9e39f4SBen Hutchings 1897a1e890eSBen Hutchings usbnet_set_skb_tx_stats(skb, 1, 0); 190b4cdae20SChristian Riesch return skb; 191b4cdae20SChristian Riesch } 192b4cdae20SChristian Riesch 193607740bcSChristian Riesch int asix_set_sw_mii(struct usbnet *dev) 194b4cdae20SChristian Riesch { 195b4cdae20SChristian Riesch int ret; 196b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL); 197b4cdae20SChristian Riesch if (ret < 0) 198b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to enable software MII access\n"); 199b4cdae20SChristian Riesch return ret; 200b4cdae20SChristian Riesch } 201b4cdae20SChristian Riesch 202607740bcSChristian Riesch int asix_set_hw_mii(struct usbnet *dev) 203b4cdae20SChristian Riesch { 204b4cdae20SChristian Riesch int ret; 205b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL); 206b4cdae20SChristian Riesch if (ret < 0) 207b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to enable hardware MII access\n"); 208b4cdae20SChristian Riesch return ret; 209b4cdae20SChristian Riesch } 210b4cdae20SChristian Riesch 21116626b0cSChristian Riesch int asix_read_phy_addr(struct usbnet *dev, int internal) 212b4cdae20SChristian Riesch { 21316626b0cSChristian Riesch int offset = (internal ? 1 : 0); 214b4cdae20SChristian Riesch u8 buf[2]; 215b4cdae20SChristian Riesch int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf); 216b4cdae20SChristian Riesch 217b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_get_phy_addr()\n"); 218b4cdae20SChristian Riesch 219b4cdae20SChristian Riesch if (ret < 0) { 220b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret); 221b4cdae20SChristian Riesch goto out; 222b4cdae20SChristian Riesch } 223b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n", 224b4cdae20SChristian Riesch *((__le16 *)buf)); 22516626b0cSChristian Riesch ret = buf[offset]; 226b4cdae20SChristian Riesch 227b4cdae20SChristian Riesch out: 228b4cdae20SChristian Riesch return ret; 229b4cdae20SChristian Riesch } 230b4cdae20SChristian Riesch 23116626b0cSChristian Riesch int asix_get_phy_addr(struct usbnet *dev) 23216626b0cSChristian Riesch { 23316626b0cSChristian Riesch /* return the address of the internal phy */ 23416626b0cSChristian Riesch return asix_read_phy_addr(dev, 1); 23516626b0cSChristian Riesch } 23616626b0cSChristian Riesch 23716626b0cSChristian Riesch 238607740bcSChristian Riesch int asix_sw_reset(struct usbnet *dev, u8 flags) 239b4cdae20SChristian Riesch { 240b4cdae20SChristian Riesch int ret; 241b4cdae20SChristian Riesch 242b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL); 243b4cdae20SChristian Riesch if (ret < 0) 244b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to send software reset: %02x\n", ret); 245b4cdae20SChristian Riesch 246b4cdae20SChristian Riesch return ret; 247b4cdae20SChristian Riesch } 248b4cdae20SChristian Riesch 249607740bcSChristian Riesch u16 asix_read_rx_ctl(struct usbnet *dev) 250b4cdae20SChristian Riesch { 251b4cdae20SChristian Riesch __le16 v; 252b4cdae20SChristian Riesch int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v); 253b4cdae20SChristian Riesch 254b4cdae20SChristian Riesch if (ret < 0) { 255b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret); 256b4cdae20SChristian Riesch goto out; 257b4cdae20SChristian Riesch } 258b4cdae20SChristian Riesch ret = le16_to_cpu(v); 259b4cdae20SChristian Riesch out: 260b4cdae20SChristian Riesch return ret; 261b4cdae20SChristian Riesch } 262b4cdae20SChristian Riesch 263607740bcSChristian Riesch int asix_write_rx_ctl(struct usbnet *dev, u16 mode) 264b4cdae20SChristian Riesch { 265b4cdae20SChristian Riesch int ret; 266b4cdae20SChristian Riesch 267b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode); 268b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL); 269b4cdae20SChristian Riesch if (ret < 0) 270b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n", 271b4cdae20SChristian Riesch mode, ret); 272b4cdae20SChristian Riesch 273b4cdae20SChristian Riesch return ret; 274b4cdae20SChristian Riesch } 275b4cdae20SChristian Riesch 276607740bcSChristian Riesch u16 asix_read_medium_status(struct usbnet *dev) 277b4cdae20SChristian Riesch { 278b4cdae20SChristian Riesch __le16 v; 279b4cdae20SChristian Riesch int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v); 280b4cdae20SChristian Riesch 281b4cdae20SChristian Riesch if (ret < 0) { 282b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading Medium Status register: %02x\n", 283b4cdae20SChristian Riesch ret); 284b4cdae20SChristian Riesch return ret; /* TODO: callers not checking for error ret */ 285b4cdae20SChristian Riesch } 286b4cdae20SChristian Riesch 287b4cdae20SChristian Riesch return le16_to_cpu(v); 288b4cdae20SChristian Riesch 289b4cdae20SChristian Riesch } 290b4cdae20SChristian Riesch 291607740bcSChristian Riesch int asix_write_medium_mode(struct usbnet *dev, u16 mode) 292b4cdae20SChristian Riesch { 293b4cdae20SChristian Riesch int ret; 294b4cdae20SChristian Riesch 295b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode); 296b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL); 297b4cdae20SChristian Riesch if (ret < 0) 298b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n", 299b4cdae20SChristian Riesch mode, ret); 300b4cdae20SChristian Riesch 301b4cdae20SChristian Riesch return ret; 302b4cdae20SChristian Riesch } 303b4cdae20SChristian Riesch 304607740bcSChristian Riesch int asix_write_gpio(struct usbnet *dev, u16 value, int sleep) 305b4cdae20SChristian Riesch { 306b4cdae20SChristian Riesch int ret; 307b4cdae20SChristian Riesch 308b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value); 309b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL); 310b4cdae20SChristian Riesch if (ret < 0) 311b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n", 312b4cdae20SChristian Riesch value, ret); 313b4cdae20SChristian Riesch 314b4cdae20SChristian Riesch if (sleep) 315b4cdae20SChristian Riesch msleep(sleep); 316b4cdae20SChristian Riesch 317b4cdae20SChristian Riesch return ret; 318b4cdae20SChristian Riesch } 319b4cdae20SChristian Riesch 320b4cdae20SChristian Riesch /* 321b4cdae20SChristian Riesch * AX88772 & AX88178 have a 16-bit RX_CTL value 322b4cdae20SChristian Riesch */ 323607740bcSChristian Riesch void asix_set_multicast(struct net_device *net) 324b4cdae20SChristian Riesch { 325b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 326b4cdae20SChristian Riesch struct asix_data *data = (struct asix_data *)&dev->data; 327b4cdae20SChristian Riesch u16 rx_ctl = AX_DEFAULT_RX_CTL; 328b4cdae20SChristian Riesch 329b4cdae20SChristian Riesch if (net->flags & IFF_PROMISC) { 330b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_PRO; 331b4cdae20SChristian Riesch } else if (net->flags & IFF_ALLMULTI || 332b4cdae20SChristian Riesch netdev_mc_count(net) > AX_MAX_MCAST) { 333b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_AMALL; 334b4cdae20SChristian Riesch } else if (netdev_mc_empty(net)) { 335b4cdae20SChristian Riesch /* just broadcast and directed */ 336b4cdae20SChristian Riesch } else { 337b4cdae20SChristian Riesch /* We use the 20 byte dev->data 338b4cdae20SChristian Riesch * for our 8 byte filter buffer 339b4cdae20SChristian Riesch * to avoid allocating memory that 340b4cdae20SChristian Riesch * is tricky to free later */ 341b4cdae20SChristian Riesch struct netdev_hw_addr *ha; 342b4cdae20SChristian Riesch u32 crc_bits; 343b4cdae20SChristian Riesch 344b4cdae20SChristian Riesch memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE); 345b4cdae20SChristian Riesch 346b4cdae20SChristian Riesch /* Build the multicast hash filter. */ 347b4cdae20SChristian Riesch netdev_for_each_mc_addr(ha, net) { 348b4cdae20SChristian Riesch crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26; 349b4cdae20SChristian Riesch data->multi_filter[crc_bits >> 3] |= 350b4cdae20SChristian Riesch 1 << (crc_bits & 7); 351b4cdae20SChristian Riesch } 352b4cdae20SChristian Riesch 353b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0, 354b4cdae20SChristian Riesch AX_MCAST_FILTER_SIZE, data->multi_filter); 355b4cdae20SChristian Riesch 356b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_AM; 357b4cdae20SChristian Riesch } 358b4cdae20SChristian Riesch 359b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL); 360b4cdae20SChristian Riesch } 361b4cdae20SChristian Riesch 362607740bcSChristian Riesch int asix_mdio_read(struct net_device *netdev, int phy_id, int loc) 363b4cdae20SChristian Riesch { 364b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(netdev); 365b4cdae20SChristian Riesch __le16 res; 366b4cdae20SChristian Riesch 367b4cdae20SChristian Riesch mutex_lock(&dev->phy_mutex); 368b4cdae20SChristian Riesch asix_set_sw_mii(dev); 369b4cdae20SChristian Riesch asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, 370b4cdae20SChristian Riesch (__u16)loc, 2, &res); 371b4cdae20SChristian Riesch asix_set_hw_mii(dev); 372b4cdae20SChristian Riesch mutex_unlock(&dev->phy_mutex); 373b4cdae20SChristian Riesch 374b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", 375b4cdae20SChristian Riesch phy_id, loc, le16_to_cpu(res)); 376b4cdae20SChristian Riesch 377b4cdae20SChristian Riesch return le16_to_cpu(res); 378b4cdae20SChristian Riesch } 379b4cdae20SChristian Riesch 380607740bcSChristian Riesch void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val) 381b4cdae20SChristian Riesch { 382b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(netdev); 383b4cdae20SChristian Riesch __le16 res = cpu_to_le16(val); 384b4cdae20SChristian Riesch 385b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", 386b4cdae20SChristian Riesch phy_id, loc, val); 387b4cdae20SChristian Riesch mutex_lock(&dev->phy_mutex); 388b4cdae20SChristian Riesch asix_set_sw_mii(dev); 389b4cdae20SChristian Riesch asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res); 390b4cdae20SChristian Riesch asix_set_hw_mii(dev); 391b4cdae20SChristian Riesch mutex_unlock(&dev->phy_mutex); 392b4cdae20SChristian Riesch } 393b4cdae20SChristian Riesch 394607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 395b4cdae20SChristian Riesch { 396b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 397b4cdae20SChristian Riesch u8 opt; 398b4cdae20SChristian Riesch 399b4cdae20SChristian Riesch if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) { 400b4cdae20SChristian Riesch wolinfo->supported = 0; 401b4cdae20SChristian Riesch wolinfo->wolopts = 0; 402b4cdae20SChristian Riesch return; 403b4cdae20SChristian Riesch } 404b4cdae20SChristian Riesch wolinfo->supported = WAKE_PHY | WAKE_MAGIC; 405b4cdae20SChristian Riesch wolinfo->wolopts = 0; 406b4cdae20SChristian Riesch if (opt & AX_MONITOR_LINK) 407b4cdae20SChristian Riesch wolinfo->wolopts |= WAKE_PHY; 408b4cdae20SChristian Riesch if (opt & AX_MONITOR_MAGIC) 409b4cdae20SChristian Riesch wolinfo->wolopts |= WAKE_MAGIC; 410b4cdae20SChristian Riesch } 411b4cdae20SChristian Riesch 412607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 413b4cdae20SChristian Riesch { 414b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 415b4cdae20SChristian Riesch u8 opt = 0; 416b4cdae20SChristian Riesch 417b4cdae20SChristian Riesch if (wolinfo->wolopts & WAKE_PHY) 418b4cdae20SChristian Riesch opt |= AX_MONITOR_LINK; 419b4cdae20SChristian Riesch if (wolinfo->wolopts & WAKE_MAGIC) 420b4cdae20SChristian Riesch opt |= AX_MONITOR_MAGIC; 421b4cdae20SChristian Riesch 422b4cdae20SChristian Riesch if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE, 423b4cdae20SChristian Riesch opt, 0, 0, NULL) < 0) 424b4cdae20SChristian Riesch return -EINVAL; 425b4cdae20SChristian Riesch 426b4cdae20SChristian Riesch return 0; 427b4cdae20SChristian Riesch } 428b4cdae20SChristian Riesch 429607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net) 430b4cdae20SChristian Riesch { 431ceb02c91SChristian Riesch return AX_EEPROM_LEN; 432b4cdae20SChristian Riesch } 433b4cdae20SChristian Riesch 434607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom, 435607740bcSChristian Riesch u8 *data) 436b4cdae20SChristian Riesch { 437b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 438ceb02c91SChristian Riesch u16 *eeprom_buff; 439ceb02c91SChristian Riesch int first_word, last_word; 440b4cdae20SChristian Riesch int i; 441b4cdae20SChristian Riesch 442ceb02c91SChristian Riesch if (eeprom->len == 0) 443b4cdae20SChristian Riesch return -EINVAL; 444b4cdae20SChristian Riesch 445b4cdae20SChristian Riesch eeprom->magic = AX_EEPROM_MAGIC; 446b4cdae20SChristian Riesch 447ceb02c91SChristian Riesch first_word = eeprom->offset >> 1; 448ceb02c91SChristian Riesch last_word = (eeprom->offset + eeprom->len - 1) >> 1; 449ceb02c91SChristian Riesch 450ceb02c91SChristian Riesch eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1), 451ceb02c91SChristian Riesch GFP_KERNEL); 452ceb02c91SChristian Riesch if (!eeprom_buff) 453ceb02c91SChristian Riesch return -ENOMEM; 454ceb02c91SChristian Riesch 455b4cdae20SChristian Riesch /* ax8817x returns 2 bytes from eeprom on read */ 456ceb02c91SChristian Riesch for (i = first_word; i <= last_word; i++) { 457ceb02c91SChristian Riesch if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2, 458ceb02c91SChristian Riesch &(eeprom_buff[i - first_word])) < 0) { 459ceb02c91SChristian Riesch kfree(eeprom_buff); 460ceb02c91SChristian Riesch return -EIO; 461b4cdae20SChristian Riesch } 462ceb02c91SChristian Riesch } 463ceb02c91SChristian Riesch 464ceb02c91SChristian Riesch memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len); 465ceb02c91SChristian Riesch kfree(eeprom_buff); 466b4cdae20SChristian Riesch return 0; 467b4cdae20SChristian Riesch } 468b4cdae20SChristian Riesch 469cb7b24cdSChristian Riesch int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom, 470cb7b24cdSChristian Riesch u8 *data) 471cb7b24cdSChristian Riesch { 472cb7b24cdSChristian Riesch struct usbnet *dev = netdev_priv(net); 473cb7b24cdSChristian Riesch u16 *eeprom_buff; 474cb7b24cdSChristian Riesch int first_word, last_word; 475cb7b24cdSChristian Riesch int i; 476cb7b24cdSChristian Riesch int ret; 477cb7b24cdSChristian Riesch 478cb7b24cdSChristian Riesch netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n", 479cb7b24cdSChristian Riesch eeprom->len, eeprom->offset, eeprom->magic); 480cb7b24cdSChristian Riesch 481cb7b24cdSChristian Riesch if (eeprom->len == 0) 482cb7b24cdSChristian Riesch return -EINVAL; 483cb7b24cdSChristian Riesch 484cb7b24cdSChristian Riesch if (eeprom->magic != AX_EEPROM_MAGIC) 485cb7b24cdSChristian Riesch return -EINVAL; 486cb7b24cdSChristian Riesch 487cb7b24cdSChristian Riesch first_word = eeprom->offset >> 1; 488cb7b24cdSChristian Riesch last_word = (eeprom->offset + eeprom->len - 1) >> 1; 489cb7b24cdSChristian Riesch 490cb7b24cdSChristian Riesch eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1), 491cb7b24cdSChristian Riesch GFP_KERNEL); 492cb7b24cdSChristian Riesch if (!eeprom_buff) 493cb7b24cdSChristian Riesch return -ENOMEM; 494cb7b24cdSChristian Riesch 495cb7b24cdSChristian Riesch /* align data to 16 bit boundaries, read the missing data from 496cb7b24cdSChristian Riesch the EEPROM */ 497cb7b24cdSChristian Riesch if (eeprom->offset & 1) { 498cb7b24cdSChristian Riesch ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2, 499cb7b24cdSChristian Riesch &(eeprom_buff[0])); 500cb7b24cdSChristian Riesch if (ret < 0) { 501cb7b24cdSChristian Riesch netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word); 502cb7b24cdSChristian Riesch goto free; 503cb7b24cdSChristian Riesch } 504cb7b24cdSChristian Riesch } 505cb7b24cdSChristian Riesch 506cb7b24cdSChristian Riesch if ((eeprom->offset + eeprom->len) & 1) { 507cb7b24cdSChristian Riesch ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2, 508cb7b24cdSChristian Riesch &(eeprom_buff[last_word - first_word])); 509cb7b24cdSChristian Riesch if (ret < 0) { 510cb7b24cdSChristian Riesch netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word); 511cb7b24cdSChristian Riesch goto free; 512cb7b24cdSChristian Riesch } 513cb7b24cdSChristian Riesch } 514cb7b24cdSChristian Riesch 515cb7b24cdSChristian Riesch memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len); 516cb7b24cdSChristian Riesch 517cb7b24cdSChristian Riesch /* write data to EEPROM */ 518cb7b24cdSChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL); 519cb7b24cdSChristian Riesch if (ret < 0) { 520cb7b24cdSChristian Riesch netdev_err(net, "Failed to enable EEPROM write\n"); 521cb7b24cdSChristian Riesch goto free; 522cb7b24cdSChristian Riesch } 523cb7b24cdSChristian Riesch msleep(20); 524cb7b24cdSChristian Riesch 525cb7b24cdSChristian Riesch for (i = first_word; i <= last_word; i++) { 526cb7b24cdSChristian Riesch netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n", 527cb7b24cdSChristian Riesch i, eeprom_buff[i - first_word]); 528cb7b24cdSChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i, 529cb7b24cdSChristian Riesch eeprom_buff[i - first_word], 0, NULL); 530cb7b24cdSChristian Riesch if (ret < 0) { 531cb7b24cdSChristian Riesch netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n", 532cb7b24cdSChristian Riesch i); 533cb7b24cdSChristian Riesch goto free; 534cb7b24cdSChristian Riesch } 535cb7b24cdSChristian Riesch msleep(20); 536cb7b24cdSChristian Riesch } 537cb7b24cdSChristian Riesch 538cb7b24cdSChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL); 539cb7b24cdSChristian Riesch if (ret < 0) { 540cb7b24cdSChristian Riesch netdev_err(net, "Failed to disable EEPROM write\n"); 541cb7b24cdSChristian Riesch goto free; 542cb7b24cdSChristian Riesch } 543cb7b24cdSChristian Riesch 544cb7b24cdSChristian Riesch ret = 0; 545cb7b24cdSChristian Riesch free: 546cb7b24cdSChristian Riesch kfree(eeprom_buff); 547cb7b24cdSChristian Riesch return ret; 548cb7b24cdSChristian Riesch } 549cb7b24cdSChristian Riesch 550607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info) 551b4cdae20SChristian Riesch { 552b4cdae20SChristian Riesch /* Inherit standard device info */ 553b4cdae20SChristian Riesch usbnet_get_drvinfo(net, info); 5547826d43fSJiri Pirko strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver)); 5557826d43fSJiri Pirko strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); 556ceb02c91SChristian Riesch info->eedump_len = AX_EEPROM_LEN; 557b4cdae20SChristian Riesch } 558b4cdae20SChristian Riesch 559607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p) 560b4cdae20SChristian Riesch { 561b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 562b4cdae20SChristian Riesch struct asix_data *data = (struct asix_data *)&dev->data; 563b4cdae20SChristian Riesch struct sockaddr *addr = p; 564b4cdae20SChristian Riesch 565b4cdae20SChristian Riesch if (netif_running(net)) 566b4cdae20SChristian Riesch return -EBUSY; 567b4cdae20SChristian Riesch if (!is_valid_ether_addr(addr->sa_data)) 568b4cdae20SChristian Riesch return -EADDRNOTAVAIL; 569b4cdae20SChristian Riesch 570b4cdae20SChristian Riesch memcpy(net->dev_addr, addr->sa_data, ETH_ALEN); 571b4cdae20SChristian Riesch 572b4cdae20SChristian Riesch /* We use the 20 byte dev->data 573b4cdae20SChristian Riesch * for our 6 byte mac buffer 574b4cdae20SChristian Riesch * to avoid allocating memory that 575b4cdae20SChristian Riesch * is tricky to free later */ 576b4cdae20SChristian Riesch memcpy(data->mac_addr, addr->sa_data, ETH_ALEN); 577b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, 578b4cdae20SChristian Riesch data->mac_addr); 579b4cdae20SChristian Riesch 580b4cdae20SChristian Riesch return 0; 581b4cdae20SChristian Riesch } 582