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) { 64*3bfc69abSDean 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; 70*3bfc69abSDean Jenkins } 71*3bfc69abSDean Jenkins 72*3bfc69abSDean Jenkins if (rx->split_head == true) { 738b5b6f54SLucas Stach rx->header |= (get_unaligned_le16( 74*3bfc69abSDean 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 } 907b0378f5SDean Jenkins rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size); 918b5b6f54SLucas Stach if (!rx->ax_skb) 92b4cdae20SChristian Riesch return 0; 937b0378f5SDean Jenkins rx->remaining = size; 94b4cdae20SChristian Riesch } 95b4cdae20SChristian Riesch 967b0378f5SDean Jenkins if (rx->remaining > dev->net->mtu + ETH_HLEN + VLAN_HLEN) { 97b4cdae20SChristian Riesch netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n", 987b0378f5SDean Jenkins rx->remaining); 998b5b6f54SLucas Stach kfree_skb(rx->ax_skb); 100c5060cecSholger@eitzenberger.org rx->ax_skb = NULL; 1017b0378f5SDean Jenkins rx->remaining = 0; 102b4cdae20SChristian Riesch return 0; 103b4cdae20SChristian Riesch } 104b4cdae20SChristian Riesch 1057b0378f5SDean Jenkins if (rx->remaining > skb->len - offset) { 1067b0378f5SDean Jenkins copy_length = skb->len - offset; 1077b0378f5SDean Jenkins rx->remaining -= copy_length; 1087b0378f5SDean Jenkins } else { 1097b0378f5SDean Jenkins copy_length = rx->remaining; 1107b0378f5SDean Jenkins rx->remaining = 0; 1118b5b6f54SLucas Stach } 112b4cdae20SChristian Riesch 1137b0378f5SDean Jenkins data = skb_put(rx->ax_skb, copy_length); 1147b0378f5SDean Jenkins memcpy(data, skb->data + offset, copy_length); 1157b0378f5SDean Jenkins if (!rx->remaining) 1168b5b6f54SLucas Stach usbnet_skb_return(dev, rx->ax_skb); 1178b5b6f54SLucas Stach 1187b0378f5SDean Jenkins offset += (copy_length + 1) & 0xfffe; 119b4cdae20SChristian Riesch } 120b4cdae20SChristian Riesch 121b4cdae20SChristian Riesch if (skb->len != offset) { 1228b5b6f54SLucas Stach netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n", 1238b5b6f54SLucas Stach skb->len, offset); 124b4cdae20SChristian Riesch return 0; 125b4cdae20SChristian Riesch } 1268b5b6f54SLucas Stach 127b4cdae20SChristian Riesch return 1; 128b4cdae20SChristian Riesch } 129b4cdae20SChristian Riesch 1308b5b6f54SLucas Stach int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb) 1318b5b6f54SLucas Stach { 1328b5b6f54SLucas Stach struct asix_common_private *dp = dev->driver_priv; 1338b5b6f54SLucas Stach struct asix_rx_fixup_info *rx = &dp->rx_fixup_info; 1348b5b6f54SLucas Stach 1358b5b6f54SLucas Stach return asix_rx_fixup_internal(dev, skb, rx); 1368b5b6f54SLucas Stach } 1378b5b6f54SLucas Stach 138607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb, 139b4cdae20SChristian Riesch gfp_t flags) 140b4cdae20SChristian Riesch { 141b4cdae20SChristian Riesch int padlen; 142b4cdae20SChristian Riesch int headroom = skb_headroom(skb); 143b4cdae20SChristian Riesch int tailroom = skb_tailroom(skb); 144b4cdae20SChristian Riesch u32 packet_len; 145b4cdae20SChristian Riesch u32 padbytes = 0xffff0000; 146b4cdae20SChristian Riesch 147b4cdae20SChristian Riesch padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4; 148b4cdae20SChristian Riesch 149b4cdae20SChristian Riesch /* We need to push 4 bytes in front of frame (packet_len) 150b4cdae20SChristian Riesch * and maybe add 4 bytes after the end (if padlen is 4) 151b4cdae20SChristian Riesch * 152b4cdae20SChristian Riesch * Avoid skb_copy_expand() expensive call, using following rules : 153b4cdae20SChristian Riesch * - We are allowed to push 4 bytes in headroom if skb_header_cloned() 154b4cdae20SChristian Riesch * is false (and if we have 4 bytes of headroom) 155b4cdae20SChristian Riesch * - We are allowed to put 4 bytes at tail if skb_cloned() 156b4cdae20SChristian Riesch * is false (and if we have 4 bytes of tailroom) 157b4cdae20SChristian Riesch * 158b4cdae20SChristian Riesch * TCP packets for example are cloned, but skb_header_release() 159b4cdae20SChristian Riesch * was called in tcp stack, allowing us to use headroom for our needs. 160b4cdae20SChristian Riesch */ 161b4cdae20SChristian Riesch if (!skb_header_cloned(skb) && 162b4cdae20SChristian Riesch !(padlen && skb_cloned(skb)) && 163b4cdae20SChristian Riesch headroom + tailroom >= 4 + padlen) { 164b4cdae20SChristian Riesch /* following should not happen, but better be safe */ 165b4cdae20SChristian Riesch if (headroom < 4 || 166b4cdae20SChristian Riesch tailroom < padlen) { 167b4cdae20SChristian Riesch skb->data = memmove(skb->head + 4, skb->data, skb->len); 168b4cdae20SChristian Riesch skb_set_tail_pointer(skb, skb->len); 169b4cdae20SChristian Riesch } 170b4cdae20SChristian Riesch } else { 171b4cdae20SChristian Riesch struct sk_buff *skb2; 172b4cdae20SChristian Riesch 173b4cdae20SChristian Riesch skb2 = skb_copy_expand(skb, 4, padlen, flags); 174b4cdae20SChristian Riesch dev_kfree_skb_any(skb); 175b4cdae20SChristian Riesch skb = skb2; 176b4cdae20SChristian Riesch if (!skb) 177b4cdae20SChristian Riesch return NULL; 178b4cdae20SChristian Riesch } 179b4cdae20SChristian Riesch 180b4cdae20SChristian Riesch packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len; 181b4cdae20SChristian Riesch skb_push(skb, 4); 182b4cdae20SChristian Riesch cpu_to_le32s(&packet_len); 183b4cdae20SChristian Riesch skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len)); 184b4cdae20SChristian Riesch 185b4cdae20SChristian Riesch if (padlen) { 186b4cdae20SChristian Riesch cpu_to_le32s(&padbytes); 187b4cdae20SChristian Riesch memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes)); 188b4cdae20SChristian Riesch skb_put(skb, sizeof(padbytes)); 189b4cdae20SChristian Riesch } 1901e9e39f4SBen Hutchings 1917a1e890eSBen Hutchings usbnet_set_skb_tx_stats(skb, 1, 0); 192b4cdae20SChristian Riesch return skb; 193b4cdae20SChristian Riesch } 194b4cdae20SChristian Riesch 195607740bcSChristian Riesch int asix_set_sw_mii(struct usbnet *dev) 196b4cdae20SChristian Riesch { 197b4cdae20SChristian Riesch int ret; 198b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL); 199b4cdae20SChristian Riesch if (ret < 0) 200b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to enable software MII access\n"); 201b4cdae20SChristian Riesch return ret; 202b4cdae20SChristian Riesch } 203b4cdae20SChristian Riesch 204607740bcSChristian Riesch int asix_set_hw_mii(struct usbnet *dev) 205b4cdae20SChristian Riesch { 206b4cdae20SChristian Riesch int ret; 207b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL); 208b4cdae20SChristian Riesch if (ret < 0) 209b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to enable hardware MII access\n"); 210b4cdae20SChristian Riesch return ret; 211b4cdae20SChristian Riesch } 212b4cdae20SChristian Riesch 21316626b0cSChristian Riesch int asix_read_phy_addr(struct usbnet *dev, int internal) 214b4cdae20SChristian Riesch { 21516626b0cSChristian Riesch int offset = (internal ? 1 : 0); 216b4cdae20SChristian Riesch u8 buf[2]; 217b4cdae20SChristian Riesch int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf); 218b4cdae20SChristian Riesch 219b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_get_phy_addr()\n"); 220b4cdae20SChristian Riesch 221b4cdae20SChristian Riesch if (ret < 0) { 222b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret); 223b4cdae20SChristian Riesch goto out; 224b4cdae20SChristian Riesch } 225b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n", 226b4cdae20SChristian Riesch *((__le16 *)buf)); 22716626b0cSChristian Riesch ret = buf[offset]; 228b4cdae20SChristian Riesch 229b4cdae20SChristian Riesch out: 230b4cdae20SChristian Riesch return ret; 231b4cdae20SChristian Riesch } 232b4cdae20SChristian Riesch 23316626b0cSChristian Riesch int asix_get_phy_addr(struct usbnet *dev) 23416626b0cSChristian Riesch { 23516626b0cSChristian Riesch /* return the address of the internal phy */ 23616626b0cSChristian Riesch return asix_read_phy_addr(dev, 1); 23716626b0cSChristian Riesch } 23816626b0cSChristian Riesch 23916626b0cSChristian Riesch 240607740bcSChristian Riesch int asix_sw_reset(struct usbnet *dev, u8 flags) 241b4cdae20SChristian Riesch { 242b4cdae20SChristian Riesch int ret; 243b4cdae20SChristian Riesch 244b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL); 245b4cdae20SChristian Riesch if (ret < 0) 246b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to send software reset: %02x\n", ret); 247b4cdae20SChristian Riesch 248b4cdae20SChristian Riesch return ret; 249b4cdae20SChristian Riesch } 250b4cdae20SChristian Riesch 251607740bcSChristian Riesch u16 asix_read_rx_ctl(struct usbnet *dev) 252b4cdae20SChristian Riesch { 253b4cdae20SChristian Riesch __le16 v; 254b4cdae20SChristian Riesch int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v); 255b4cdae20SChristian Riesch 256b4cdae20SChristian Riesch if (ret < 0) { 257b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret); 258b4cdae20SChristian Riesch goto out; 259b4cdae20SChristian Riesch } 260b4cdae20SChristian Riesch ret = le16_to_cpu(v); 261b4cdae20SChristian Riesch out: 262b4cdae20SChristian Riesch return ret; 263b4cdae20SChristian Riesch } 264b4cdae20SChristian Riesch 265607740bcSChristian Riesch int asix_write_rx_ctl(struct usbnet *dev, u16 mode) 266b4cdae20SChristian Riesch { 267b4cdae20SChristian Riesch int ret; 268b4cdae20SChristian Riesch 269b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode); 270b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL); 271b4cdae20SChristian Riesch if (ret < 0) 272b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n", 273b4cdae20SChristian Riesch mode, ret); 274b4cdae20SChristian Riesch 275b4cdae20SChristian Riesch return ret; 276b4cdae20SChristian Riesch } 277b4cdae20SChristian Riesch 278607740bcSChristian Riesch u16 asix_read_medium_status(struct usbnet *dev) 279b4cdae20SChristian Riesch { 280b4cdae20SChristian Riesch __le16 v; 281b4cdae20SChristian Riesch int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v); 282b4cdae20SChristian Riesch 283b4cdae20SChristian Riesch if (ret < 0) { 284b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading Medium Status register: %02x\n", 285b4cdae20SChristian Riesch ret); 286b4cdae20SChristian Riesch return ret; /* TODO: callers not checking for error ret */ 287b4cdae20SChristian Riesch } 288b4cdae20SChristian Riesch 289b4cdae20SChristian Riesch return le16_to_cpu(v); 290b4cdae20SChristian Riesch 291b4cdae20SChristian Riesch } 292b4cdae20SChristian Riesch 293607740bcSChristian Riesch int asix_write_medium_mode(struct usbnet *dev, u16 mode) 294b4cdae20SChristian Riesch { 295b4cdae20SChristian Riesch int ret; 296b4cdae20SChristian Riesch 297b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode); 298b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL); 299b4cdae20SChristian Riesch if (ret < 0) 300b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n", 301b4cdae20SChristian Riesch mode, ret); 302b4cdae20SChristian Riesch 303b4cdae20SChristian Riesch return ret; 304b4cdae20SChristian Riesch } 305b4cdae20SChristian Riesch 306607740bcSChristian Riesch int asix_write_gpio(struct usbnet *dev, u16 value, int sleep) 307b4cdae20SChristian Riesch { 308b4cdae20SChristian Riesch int ret; 309b4cdae20SChristian Riesch 310b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value); 311b4cdae20SChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL); 312b4cdae20SChristian Riesch if (ret < 0) 313b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n", 314b4cdae20SChristian Riesch value, ret); 315b4cdae20SChristian Riesch 316b4cdae20SChristian Riesch if (sleep) 317b4cdae20SChristian Riesch msleep(sleep); 318b4cdae20SChristian Riesch 319b4cdae20SChristian Riesch return ret; 320b4cdae20SChristian Riesch } 321b4cdae20SChristian Riesch 322b4cdae20SChristian Riesch /* 323b4cdae20SChristian Riesch * AX88772 & AX88178 have a 16-bit RX_CTL value 324b4cdae20SChristian Riesch */ 325607740bcSChristian Riesch void asix_set_multicast(struct net_device *net) 326b4cdae20SChristian Riesch { 327b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 328b4cdae20SChristian Riesch struct asix_data *data = (struct asix_data *)&dev->data; 329b4cdae20SChristian Riesch u16 rx_ctl = AX_DEFAULT_RX_CTL; 330b4cdae20SChristian Riesch 331b4cdae20SChristian Riesch if (net->flags & IFF_PROMISC) { 332b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_PRO; 333b4cdae20SChristian Riesch } else if (net->flags & IFF_ALLMULTI || 334b4cdae20SChristian Riesch netdev_mc_count(net) > AX_MAX_MCAST) { 335b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_AMALL; 336b4cdae20SChristian Riesch } else if (netdev_mc_empty(net)) { 337b4cdae20SChristian Riesch /* just broadcast and directed */ 338b4cdae20SChristian Riesch } else { 339b4cdae20SChristian Riesch /* We use the 20 byte dev->data 340b4cdae20SChristian Riesch * for our 8 byte filter buffer 341b4cdae20SChristian Riesch * to avoid allocating memory that 342b4cdae20SChristian Riesch * is tricky to free later */ 343b4cdae20SChristian Riesch struct netdev_hw_addr *ha; 344b4cdae20SChristian Riesch u32 crc_bits; 345b4cdae20SChristian Riesch 346b4cdae20SChristian Riesch memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE); 347b4cdae20SChristian Riesch 348b4cdae20SChristian Riesch /* Build the multicast hash filter. */ 349b4cdae20SChristian Riesch netdev_for_each_mc_addr(ha, net) { 350b4cdae20SChristian Riesch crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26; 351b4cdae20SChristian Riesch data->multi_filter[crc_bits >> 3] |= 352b4cdae20SChristian Riesch 1 << (crc_bits & 7); 353b4cdae20SChristian Riesch } 354b4cdae20SChristian Riesch 355b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0, 356b4cdae20SChristian Riesch AX_MCAST_FILTER_SIZE, data->multi_filter); 357b4cdae20SChristian Riesch 358b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_AM; 359b4cdae20SChristian Riesch } 360b4cdae20SChristian Riesch 361b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL); 362b4cdae20SChristian Riesch } 363b4cdae20SChristian Riesch 364607740bcSChristian Riesch int asix_mdio_read(struct net_device *netdev, int phy_id, int loc) 365b4cdae20SChristian Riesch { 366b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(netdev); 367b4cdae20SChristian Riesch __le16 res; 368b4cdae20SChristian Riesch 369b4cdae20SChristian Riesch mutex_lock(&dev->phy_mutex); 370b4cdae20SChristian Riesch asix_set_sw_mii(dev); 371b4cdae20SChristian Riesch asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, 372b4cdae20SChristian Riesch (__u16)loc, 2, &res); 373b4cdae20SChristian Riesch asix_set_hw_mii(dev); 374b4cdae20SChristian Riesch mutex_unlock(&dev->phy_mutex); 375b4cdae20SChristian Riesch 376b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", 377b4cdae20SChristian Riesch phy_id, loc, le16_to_cpu(res)); 378b4cdae20SChristian Riesch 379b4cdae20SChristian Riesch return le16_to_cpu(res); 380b4cdae20SChristian Riesch } 381b4cdae20SChristian Riesch 382607740bcSChristian Riesch void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val) 383b4cdae20SChristian Riesch { 384b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(netdev); 385b4cdae20SChristian Riesch __le16 res = cpu_to_le16(val); 386b4cdae20SChristian Riesch 387b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", 388b4cdae20SChristian Riesch phy_id, loc, val); 389b4cdae20SChristian Riesch mutex_lock(&dev->phy_mutex); 390b4cdae20SChristian Riesch asix_set_sw_mii(dev); 391b4cdae20SChristian Riesch asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res); 392b4cdae20SChristian Riesch asix_set_hw_mii(dev); 393b4cdae20SChristian Riesch mutex_unlock(&dev->phy_mutex); 394b4cdae20SChristian Riesch } 395b4cdae20SChristian Riesch 396607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 397b4cdae20SChristian Riesch { 398b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 399b4cdae20SChristian Riesch u8 opt; 400b4cdae20SChristian Riesch 401b4cdae20SChristian Riesch if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) { 402b4cdae20SChristian Riesch wolinfo->supported = 0; 403b4cdae20SChristian Riesch wolinfo->wolopts = 0; 404b4cdae20SChristian Riesch return; 405b4cdae20SChristian Riesch } 406b4cdae20SChristian Riesch wolinfo->supported = WAKE_PHY | WAKE_MAGIC; 407b4cdae20SChristian Riesch wolinfo->wolopts = 0; 408b4cdae20SChristian Riesch if (opt & AX_MONITOR_LINK) 409b4cdae20SChristian Riesch wolinfo->wolopts |= WAKE_PHY; 410b4cdae20SChristian Riesch if (opt & AX_MONITOR_MAGIC) 411b4cdae20SChristian Riesch wolinfo->wolopts |= WAKE_MAGIC; 412b4cdae20SChristian Riesch } 413b4cdae20SChristian Riesch 414607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 415b4cdae20SChristian Riesch { 416b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 417b4cdae20SChristian Riesch u8 opt = 0; 418b4cdae20SChristian Riesch 419b4cdae20SChristian Riesch if (wolinfo->wolopts & WAKE_PHY) 420b4cdae20SChristian Riesch opt |= AX_MONITOR_LINK; 421b4cdae20SChristian Riesch if (wolinfo->wolopts & WAKE_MAGIC) 422b4cdae20SChristian Riesch opt |= AX_MONITOR_MAGIC; 423b4cdae20SChristian Riesch 424b4cdae20SChristian Riesch if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE, 425b4cdae20SChristian Riesch opt, 0, 0, NULL) < 0) 426b4cdae20SChristian Riesch return -EINVAL; 427b4cdae20SChristian Riesch 428b4cdae20SChristian Riesch return 0; 429b4cdae20SChristian Riesch } 430b4cdae20SChristian Riesch 431607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net) 432b4cdae20SChristian Riesch { 433ceb02c91SChristian Riesch return AX_EEPROM_LEN; 434b4cdae20SChristian Riesch } 435b4cdae20SChristian Riesch 436607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom, 437607740bcSChristian Riesch u8 *data) 438b4cdae20SChristian Riesch { 439b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 440ceb02c91SChristian Riesch u16 *eeprom_buff; 441ceb02c91SChristian Riesch int first_word, last_word; 442b4cdae20SChristian Riesch int i; 443b4cdae20SChristian Riesch 444ceb02c91SChristian Riesch if (eeprom->len == 0) 445b4cdae20SChristian Riesch return -EINVAL; 446b4cdae20SChristian Riesch 447b4cdae20SChristian Riesch eeprom->magic = AX_EEPROM_MAGIC; 448b4cdae20SChristian Riesch 449ceb02c91SChristian Riesch first_word = eeprom->offset >> 1; 450ceb02c91SChristian Riesch last_word = (eeprom->offset + eeprom->len - 1) >> 1; 451ceb02c91SChristian Riesch 452ceb02c91SChristian Riesch eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1), 453ceb02c91SChristian Riesch GFP_KERNEL); 454ceb02c91SChristian Riesch if (!eeprom_buff) 455ceb02c91SChristian Riesch return -ENOMEM; 456ceb02c91SChristian Riesch 457b4cdae20SChristian Riesch /* ax8817x returns 2 bytes from eeprom on read */ 458ceb02c91SChristian Riesch for (i = first_word; i <= last_word; i++) { 459ceb02c91SChristian Riesch if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2, 460ceb02c91SChristian Riesch &(eeprom_buff[i - first_word])) < 0) { 461ceb02c91SChristian Riesch kfree(eeprom_buff); 462ceb02c91SChristian Riesch return -EIO; 463b4cdae20SChristian Riesch } 464ceb02c91SChristian Riesch } 465ceb02c91SChristian Riesch 466ceb02c91SChristian Riesch memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len); 467ceb02c91SChristian Riesch kfree(eeprom_buff); 468b4cdae20SChristian Riesch return 0; 469b4cdae20SChristian Riesch } 470b4cdae20SChristian Riesch 471cb7b24cdSChristian Riesch int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom, 472cb7b24cdSChristian Riesch u8 *data) 473cb7b24cdSChristian Riesch { 474cb7b24cdSChristian Riesch struct usbnet *dev = netdev_priv(net); 475cb7b24cdSChristian Riesch u16 *eeprom_buff; 476cb7b24cdSChristian Riesch int first_word, last_word; 477cb7b24cdSChristian Riesch int i; 478cb7b24cdSChristian Riesch int ret; 479cb7b24cdSChristian Riesch 480cb7b24cdSChristian Riesch netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n", 481cb7b24cdSChristian Riesch eeprom->len, eeprom->offset, eeprom->magic); 482cb7b24cdSChristian Riesch 483cb7b24cdSChristian Riesch if (eeprom->len == 0) 484cb7b24cdSChristian Riesch return -EINVAL; 485cb7b24cdSChristian Riesch 486cb7b24cdSChristian Riesch if (eeprom->magic != AX_EEPROM_MAGIC) 487cb7b24cdSChristian Riesch return -EINVAL; 488cb7b24cdSChristian Riesch 489cb7b24cdSChristian Riesch first_word = eeprom->offset >> 1; 490cb7b24cdSChristian Riesch last_word = (eeprom->offset + eeprom->len - 1) >> 1; 491cb7b24cdSChristian Riesch 492cb7b24cdSChristian Riesch eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1), 493cb7b24cdSChristian Riesch GFP_KERNEL); 494cb7b24cdSChristian Riesch if (!eeprom_buff) 495cb7b24cdSChristian Riesch return -ENOMEM; 496cb7b24cdSChristian Riesch 497cb7b24cdSChristian Riesch /* align data to 16 bit boundaries, read the missing data from 498cb7b24cdSChristian Riesch the EEPROM */ 499cb7b24cdSChristian Riesch if (eeprom->offset & 1) { 500cb7b24cdSChristian Riesch ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2, 501cb7b24cdSChristian Riesch &(eeprom_buff[0])); 502cb7b24cdSChristian Riesch if (ret < 0) { 503cb7b24cdSChristian Riesch netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word); 504cb7b24cdSChristian Riesch goto free; 505cb7b24cdSChristian Riesch } 506cb7b24cdSChristian Riesch } 507cb7b24cdSChristian Riesch 508cb7b24cdSChristian Riesch if ((eeprom->offset + eeprom->len) & 1) { 509cb7b24cdSChristian Riesch ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2, 510cb7b24cdSChristian Riesch &(eeprom_buff[last_word - first_word])); 511cb7b24cdSChristian Riesch if (ret < 0) { 512cb7b24cdSChristian Riesch netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word); 513cb7b24cdSChristian Riesch goto free; 514cb7b24cdSChristian Riesch } 515cb7b24cdSChristian Riesch } 516cb7b24cdSChristian Riesch 517cb7b24cdSChristian Riesch memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len); 518cb7b24cdSChristian Riesch 519cb7b24cdSChristian Riesch /* write data to EEPROM */ 520cb7b24cdSChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL); 521cb7b24cdSChristian Riesch if (ret < 0) { 522cb7b24cdSChristian Riesch netdev_err(net, "Failed to enable EEPROM write\n"); 523cb7b24cdSChristian Riesch goto free; 524cb7b24cdSChristian Riesch } 525cb7b24cdSChristian Riesch msleep(20); 526cb7b24cdSChristian Riesch 527cb7b24cdSChristian Riesch for (i = first_word; i <= last_word; i++) { 528cb7b24cdSChristian Riesch netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n", 529cb7b24cdSChristian Riesch i, eeprom_buff[i - first_word]); 530cb7b24cdSChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i, 531cb7b24cdSChristian Riesch eeprom_buff[i - first_word], 0, NULL); 532cb7b24cdSChristian Riesch if (ret < 0) { 533cb7b24cdSChristian Riesch netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n", 534cb7b24cdSChristian Riesch i); 535cb7b24cdSChristian Riesch goto free; 536cb7b24cdSChristian Riesch } 537cb7b24cdSChristian Riesch msleep(20); 538cb7b24cdSChristian Riesch } 539cb7b24cdSChristian Riesch 540cb7b24cdSChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL); 541cb7b24cdSChristian Riesch if (ret < 0) { 542cb7b24cdSChristian Riesch netdev_err(net, "Failed to disable EEPROM write\n"); 543cb7b24cdSChristian Riesch goto free; 544cb7b24cdSChristian Riesch } 545cb7b24cdSChristian Riesch 546cb7b24cdSChristian Riesch ret = 0; 547cb7b24cdSChristian Riesch free: 548cb7b24cdSChristian Riesch kfree(eeprom_buff); 549cb7b24cdSChristian Riesch return ret; 550cb7b24cdSChristian Riesch } 551cb7b24cdSChristian Riesch 552607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info) 553b4cdae20SChristian Riesch { 554b4cdae20SChristian Riesch /* Inherit standard device info */ 555b4cdae20SChristian Riesch usbnet_get_drvinfo(net, info); 5567826d43fSJiri Pirko strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver)); 5577826d43fSJiri Pirko strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); 558ceb02c91SChristian Riesch info->eedump_len = AX_EEPROM_LEN; 559b4cdae20SChristian Riesch } 560b4cdae20SChristian Riesch 561607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p) 562b4cdae20SChristian Riesch { 563b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 564b4cdae20SChristian Riesch struct asix_data *data = (struct asix_data *)&dev->data; 565b4cdae20SChristian Riesch struct sockaddr *addr = p; 566b4cdae20SChristian Riesch 567b4cdae20SChristian Riesch if (netif_running(net)) 568b4cdae20SChristian Riesch return -EBUSY; 569b4cdae20SChristian Riesch if (!is_valid_ether_addr(addr->sa_data)) 570b4cdae20SChristian Riesch return -EADDRNOTAVAIL; 571b4cdae20SChristian Riesch 572b4cdae20SChristian Riesch memcpy(net->dev_addr, addr->sa_data, ETH_ALEN); 573b4cdae20SChristian Riesch 574b4cdae20SChristian Riesch /* We use the 20 byte dev->data 575b4cdae20SChristian Riesch * for our 6 byte mac buffer 576b4cdae20SChristian Riesch * to avoid allocating memory that 577b4cdae20SChristian Riesch * is tricky to free later */ 578b4cdae20SChristian Riesch memcpy(data->mac_addr, addr->sa_data, ETH_ALEN); 579b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, 580b4cdae20SChristian Riesch data->mac_addr); 581b4cdae20SChristian Riesch 582b4cdae20SChristian Riesch return 0; 583b4cdae20SChristian Riesch } 584