blob: 50d167330d384e76b8d3ebfe9cd15cd26dd45c87 [file] [log] [blame]
David Brownell2e55cc72005-08-31 09:53:10 -07001/*
2 * ASIX AX8817X based USB 2.0 Ethernet Devices
David Hollis933a27d2006-07-29 10:12:50 -04003 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
David Brownell2e55cc72005-08-31 09:53:10 -07004 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
David Hollis933a27d2006-07-29 10:12:50 -04005 * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
David Brownell2e55cc72005-08-31 09:53:10 -07006 * Copyright (c) 2002-2003 TiVo Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Christian Riesch607740b2012-07-13 05:26:30 +000023#include "asix.h"
David Brownell2e55cc72005-08-31 09:53:10 -070024
Christian Riesch607740b2012-07-13 05:26:30 +000025int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
26 u16 size, void *data)
David Brownell2e55cc72005-08-31 09:53:10 -070027{
Ming Lei0bc69ef2012-10-24 19:46:55 +000028 int ret;
29 ret = usbnet_read_cmd(dev, cmd,
30 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
31 value, index, data, size);
Al Viro51bf2972007-12-22 17:42:36 +000032
Ming Lei0bc69ef2012-10-24 19:46:55 +000033 if (ret != size && ret >= 0)
34 return -EINVAL;
35 return ret;
David Brownell2e55cc72005-08-31 09:53:10 -070036}
37
Christian Riesch607740b2012-07-13 05:26:30 +000038int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
39 u16 size, void *data)
David Brownell2e55cc72005-08-31 09:53:10 -070040{
Ming Lei0bc69ef2012-10-24 19:46:55 +000041 return usbnet_write_cmd(dev, cmd,
42 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
43 value, index, data, size);
David Brownell2e55cc72005-08-31 09:53:10 -070044}
45
Christian Riesch607740b2012-07-13 05:26:30 +000046void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
47 u16 size, void *data)
David Hollis48b1be62006-03-28 20:15:42 -050048{
Ming Lei0bc69ef2012-10-24 19:46:55 +000049 usbnet_write_cmd_async(dev, cmd,
50 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
51 value, index, data, size);
David Hollis48b1be62006-03-28 20:15:42 -050052}
53
Christian Riesch607740b2012-07-13 05:26:30 +000054int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
David Hollis48b1be62006-03-28 20:15:42 -050055{
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000056 int offset = 0;
David Hollis48b1be62006-03-28 20:15:42 -050057
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000058 while (offset + sizeof(u32) < skb->len) {
59 struct sk_buff *ax_skb;
60 u16 size;
61 u32 header = get_unaligned_le32(skb->data + offset);
David Hollis48b1be62006-03-28 20:15:42 -050062
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000063 offset += sizeof(u32);
Marek Vasutbc466e62011-07-26 16:44:46 +000064
David Hollis933a27d2006-07-29 10:12:50 -040065 /* get the packet length */
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000066 size = (u16) (header & 0x7ff);
67 if (size != ((~header >> 16) & 0x07ff)) {
68 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
69 return 0;
Neil Jones3f78d1f2010-05-17 17:18:28 -070070 }
71
Eric Dumazet9dae3102012-05-28 22:31:41 +000072 if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000073 (size + offset > skb->len)) {
Joe Perches60b86752010-02-17 10:30:23 +000074 netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
75 size);
David Hollis933a27d2006-07-29 10:12:50 -040076 return 0;
77 }
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000078 ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
79 if (!ax_skb)
David Hollis933a27d2006-07-29 10:12:50 -040080 return 0;
David Hollis933a27d2006-07-29 10:12:50 -040081
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000082 skb_put(ax_skb, size);
83 memcpy(ax_skb->data, skb->data + offset, size);
84 usbnet_skb_return(dev, ax_skb);
David Hollis933a27d2006-07-29 10:12:50 -040085
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000086 offset += (size + 1) & 0xfffe;
David Hollis933a27d2006-07-29 10:12:50 -040087 }
88
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000089 if (skb->len != offset) {
Joe Perches60b86752010-02-17 10:30:23 +000090 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
91 skb->len);
David Hollis933a27d2006-07-29 10:12:50 -040092 return 0;
93 }
94 return 1;
David Hollis48b1be62006-03-28 20:15:42 -050095}
96
Christian Riesch607740b2012-07-13 05:26:30 +000097struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
98 gfp_t flags)
David Hollis48b1be62006-03-28 20:15:42 -050099{
David Hollis933a27d2006-07-29 10:12:50 -0400100 int padlen;
101 int headroom = skb_headroom(skb);
102 int tailroom = skb_tailroom(skb);
103 u32 packet_len;
104 u32 padbytes = 0xffff0000;
David Hollis48b1be62006-03-28 20:15:42 -0500105
Ingo van Lil2a580942012-04-23 22:05:38 +0000106 padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
David Hollis48b1be62006-03-28 20:15:42 -0500107
Eric Dumazet95162d62012-07-05 04:31:01 +0000108 /* We need to push 4 bytes in front of frame (packet_len)
109 * and maybe add 4 bytes after the end (if padlen is 4)
110 *
111 * Avoid skb_copy_expand() expensive call, using following rules :
112 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
113 * is false (and if we have 4 bytes of headroom)
114 * - We are allowed to put 4 bytes at tail if skb_cloned()
115 * is false (and if we have 4 bytes of tailroom)
116 *
117 * TCP packets for example are cloned, but skb_header_release()
118 * was called in tcp stack, allowing us to use headroom for our needs.
119 */
120 if (!skb_header_cloned(skb) &&
121 !(padlen && skb_cloned(skb)) &&
122 headroom + tailroom >= 4 + padlen) {
123 /* following should not happen, but better be safe */
124 if (headroom < 4 ||
125 tailroom < padlen) {
David Hollis933a27d2006-07-29 10:12:50 -0400126 skb->data = memmove(skb->head + 4, skb->data, skb->len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700127 skb_set_tail_pointer(skb, skb->len);
David Hollis933a27d2006-07-29 10:12:50 -0400128 }
129 } else {
130 struct sk_buff *skb2;
Eric Dumazet95162d62012-07-05 04:31:01 +0000131
David Hollis933a27d2006-07-29 10:12:50 -0400132 skb2 = skb_copy_expand(skb, 4, padlen, flags);
133 dev_kfree_skb_any(skb);
134 skb = skb2;
135 if (!skb)
136 return NULL;
137 }
138
Eric Dumazet95162d62012-07-05 04:31:01 +0000139 packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
David Hollis933a27d2006-07-29 10:12:50 -0400140 skb_push(skb, 4);
David Hollis57e4f042007-02-05 12:03:03 -0500141 cpu_to_le32s(&packet_len);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300142 skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
David Hollis933a27d2006-07-29 10:12:50 -0400143
Ingo van Lil2a580942012-04-23 22:05:38 +0000144 if (padlen) {
David Hollis57e4f042007-02-05 12:03:03 -0500145 cpu_to_le32s(&padbytes);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700146 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
David Hollis933a27d2006-07-29 10:12:50 -0400147 skb_put(skb, sizeof(padbytes));
148 }
149 return skb;
David Hollis48b1be62006-03-28 20:15:42 -0500150}
151
Christian Riesch607740b2012-07-13 05:26:30 +0000152int asix_set_sw_mii(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -0700153{
David Hollis933a27d2006-07-29 10:12:50 -0400154 int ret;
155 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
156 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000157 netdev_err(dev->net, "Failed to enable software MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400158 return ret;
David Brownell2e55cc72005-08-31 09:53:10 -0700159}
160
Christian Riesch607740b2012-07-13 05:26:30 +0000161int asix_set_hw_mii(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400162{
163 int ret;
164 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
165 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000166 netdev_err(dev->net, "Failed to enable hardware MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400167 return ret;
168}
169
Christian Riesch16626b02012-07-13 05:26:31 +0000170int asix_read_phy_addr(struct usbnet *dev, int internal)
David Hollis933a27d2006-07-29 10:12:50 -0400171{
Christian Riesch16626b02012-07-13 05:26:31 +0000172 int offset = (internal ? 1 : 0);
Al Viro51bf2972007-12-22 17:42:36 +0000173 u8 buf[2];
174 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
David Hollis933a27d2006-07-29 10:12:50 -0400175
Joe Perches60b86752010-02-17 10:30:23 +0000176 netdev_dbg(dev->net, "asix_get_phy_addr()\n");
David Hollis933a27d2006-07-29 10:12:50 -0400177
Al Viro51bf2972007-12-22 17:42:36 +0000178 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000179 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000180 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400181 }
Joe Perches60b86752010-02-17 10:30:23 +0000182 netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
183 *((__le16 *)buf));
Christian Riesch16626b02012-07-13 05:26:31 +0000184 ret = buf[offset];
Al Viro51bf2972007-12-22 17:42:36 +0000185
186out:
David Hollis933a27d2006-07-29 10:12:50 -0400187 return ret;
188}
189
Christian Riesch16626b02012-07-13 05:26:31 +0000190int asix_get_phy_addr(struct usbnet *dev)
191{
192 /* return the address of the internal phy */
193 return asix_read_phy_addr(dev, 1);
194}
195
196
Christian Riesch607740b2012-07-13 05:26:30 +0000197int asix_sw_reset(struct usbnet *dev, u8 flags)
David Hollis933a27d2006-07-29 10:12:50 -0400198{
199 int ret;
200
201 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
202 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000203 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
David Hollis933a27d2006-07-29 10:12:50 -0400204
205 return ret;
206}
207
Christian Riesch607740b2012-07-13 05:26:30 +0000208u16 asix_read_rx_ctl(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400209{
Al Viro51bf2972007-12-22 17:42:36 +0000210 __le16 v;
211 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400212
Al Viro51bf2972007-12-22 17:42:36 +0000213 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000214 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000215 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400216 }
Al Viro51bf2972007-12-22 17:42:36 +0000217 ret = le16_to_cpu(v);
218out:
David Hollis933a27d2006-07-29 10:12:50 -0400219 return ret;
220}
221
Christian Riesch607740b2012-07-13 05:26:30 +0000222int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
David Hollis933a27d2006-07-29 10:12:50 -0400223{
224 int ret;
225
Joe Perches60b86752010-02-17 10:30:23 +0000226 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400227 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
228 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000229 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
230 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400231
232 return ret;
233}
234
Christian Riesch607740b2012-07-13 05:26:30 +0000235u16 asix_read_medium_status(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400236{
Al Viro51bf2972007-12-22 17:42:36 +0000237 __le16 v;
238 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400239
Al Viro51bf2972007-12-22 17:42:36 +0000240 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000241 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
242 ret);
Grant Grundler83e1b912011-10-04 09:55:18 +0000243 return ret; /* TODO: callers not checking for error ret */
David Hollis933a27d2006-07-29 10:12:50 -0400244 }
Grant Grundler83e1b912011-10-04 09:55:18 +0000245
246 return le16_to_cpu(v);
247
David Hollis933a27d2006-07-29 10:12:50 -0400248}
249
Christian Riesch607740b2012-07-13 05:26:30 +0000250int asix_write_medium_mode(struct usbnet *dev, u16 mode)
David Hollis933a27d2006-07-29 10:12:50 -0400251{
252 int ret;
253
Joe Perches60b86752010-02-17 10:30:23 +0000254 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400255 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
256 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000257 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
258 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400259
260 return ret;
261}
262
Christian Riesch607740b2012-07-13 05:26:30 +0000263int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
David Hollis933a27d2006-07-29 10:12:50 -0400264{
265 int ret;
266
Joe Perches60b86752010-02-17 10:30:23 +0000267 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
David Hollis933a27d2006-07-29 10:12:50 -0400268 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
269 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000270 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
271 value, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400272
273 if (sleep)
274 msleep(sleep);
275
276 return ret;
277}
278
279/*
280 * AX88772 & AX88178 have a 16-bit RX_CTL value
281 */
Christian Riesch607740b2012-07-13 05:26:30 +0000282void asix_set_multicast(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700283{
284 struct usbnet *dev = netdev_priv(net);
David Hollis48b1be62006-03-28 20:15:42 -0500285 struct asix_data *data = (struct asix_data *)&dev->data;
David Hollis933a27d2006-07-29 10:12:50 -0400286 u16 rx_ctl = AX_DEFAULT_RX_CTL;
David Brownell2e55cc72005-08-31 09:53:10 -0700287
288 if (net->flags & IFF_PROMISC) {
David Hollis933a27d2006-07-29 10:12:50 -0400289 rx_ctl |= AX_RX_CTL_PRO;
Joe Perches8e95a202009-12-03 07:58:21 +0000290 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000291 netdev_mc_count(net) > AX_MAX_MCAST) {
David Hollis933a27d2006-07-29 10:12:50 -0400292 rx_ctl |= AX_RX_CTL_AMALL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000293 } else if (netdev_mc_empty(net)) {
David Brownell2e55cc72005-08-31 09:53:10 -0700294 /* just broadcast and directed */
295 } else {
296 /* We use the 20 byte dev->data
297 * for our 8 byte filter buffer
298 * to avoid allocating memory that
299 * is tricky to free later */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000300 struct netdev_hw_addr *ha;
David Brownell2e55cc72005-08-31 09:53:10 -0700301 u32 crc_bits;
David Brownell2e55cc72005-08-31 09:53:10 -0700302
303 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
304
305 /* Build the multicast hash filter. */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000306 netdev_for_each_mc_addr(ha, net) {
307 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
David Brownell2e55cc72005-08-31 09:53:10 -0700308 data->multi_filter[crc_bits >> 3] |=
309 1 << (crc_bits & 7);
David Brownell2e55cc72005-08-31 09:53:10 -0700310 }
311
David Hollis48b1be62006-03-28 20:15:42 -0500312 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
David Brownell2e55cc72005-08-31 09:53:10 -0700313 AX_MCAST_FILTER_SIZE, data->multi_filter);
314
David Hollis933a27d2006-07-29 10:12:50 -0400315 rx_ctl |= AX_RX_CTL_AM;
David Brownell2e55cc72005-08-31 09:53:10 -0700316 }
317
David Hollis48b1be62006-03-28 20:15:42 -0500318 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700319}
320
Christian Riesch607740b2012-07-13 05:26:30 +0000321int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
David Brownell2e55cc72005-08-31 09:53:10 -0700322{
323 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000324 __le16 res;
David Brownell2e55cc72005-08-31 09:53:10 -0700325
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200326 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500327 asix_set_sw_mii(dev);
328 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
Al Viro51bf2972007-12-22 17:42:36 +0000329 (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500330 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200331 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700332
Joe Perches60b86752010-02-17 10:30:23 +0000333 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
334 phy_id, loc, le16_to_cpu(res));
David Brownell2e55cc72005-08-31 09:53:10 -0700335
Al Viro51bf2972007-12-22 17:42:36 +0000336 return le16_to_cpu(res);
David Brownell2e55cc72005-08-31 09:53:10 -0700337}
338
Christian Riesch607740b2012-07-13 05:26:30 +0000339void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
David Brownell2e55cc72005-08-31 09:53:10 -0700340{
341 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000342 __le16 res = cpu_to_le16(val);
David Brownell2e55cc72005-08-31 09:53:10 -0700343
Joe Perches60b86752010-02-17 10:30:23 +0000344 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
345 phy_id, loc, val);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200346 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500347 asix_set_sw_mii(dev);
Al Viro51bf2972007-12-22 17:42:36 +0000348 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500349 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200350 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700351}
352
Christian Riesch607740b2012-07-13 05:26:30 +0000353void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700354{
355 struct usbnet *dev = netdev_priv(net);
356 u8 opt;
357
David Hollis48b1be62006-03-28 20:15:42 -0500358 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700359 wolinfo->supported = 0;
360 wolinfo->wolopts = 0;
361 return;
362 }
363 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
364 wolinfo->wolopts = 0;
allanf87ce5b2011-12-22 20:38:51 +0000365 if (opt & AX_MONITOR_LINK)
366 wolinfo->wolopts |= WAKE_PHY;
367 if (opt & AX_MONITOR_MAGIC)
368 wolinfo->wolopts |= WAKE_MAGIC;
David Brownell2e55cc72005-08-31 09:53:10 -0700369}
370
Christian Riesch607740b2012-07-13 05:26:30 +0000371int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700372{
373 struct usbnet *dev = netdev_priv(net);
374 u8 opt = 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700375
376 if (wolinfo->wolopts & WAKE_PHY)
377 opt |= AX_MONITOR_LINK;
378 if (wolinfo->wolopts & WAKE_MAGIC)
379 opt |= AX_MONITOR_MAGIC;
David Brownell2e55cc72005-08-31 09:53:10 -0700380
David Hollis48b1be62006-03-28 20:15:42 -0500381 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
Al Viro51bf2972007-12-22 17:42:36 +0000382 opt, 0, 0, NULL) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700383 return -EINVAL;
384
385 return 0;
386}
387
Christian Riesch607740b2012-07-13 05:26:30 +0000388int asix_get_eeprom_len(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700389{
Christian Rieschceb02c92012-07-19 00:23:06 +0000390 return AX_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700391}
392
Christian Riesch607740b2012-07-13 05:26:30 +0000393int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
394 u8 *data)
David Brownell2e55cc72005-08-31 09:53:10 -0700395{
396 struct usbnet *dev = netdev_priv(net);
Christian Rieschceb02c92012-07-19 00:23:06 +0000397 u16 *eeprom_buff;
398 int first_word, last_word;
David Brownell2e55cc72005-08-31 09:53:10 -0700399 int i;
400
Christian Rieschceb02c92012-07-19 00:23:06 +0000401 if (eeprom->len == 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700402 return -EINVAL;
403
404 eeprom->magic = AX_EEPROM_MAGIC;
405
Christian Rieschceb02c92012-07-19 00:23:06 +0000406 first_word = eeprom->offset >> 1;
407 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
408
409 eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
410 GFP_KERNEL);
411 if (!eeprom_buff)
412 return -ENOMEM;
413
David Brownell2e55cc72005-08-31 09:53:10 -0700414 /* ax8817x returns 2 bytes from eeprom on read */
Christian Rieschceb02c92012-07-19 00:23:06 +0000415 for (i = first_word; i <= last_word; i++) {
416 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
417 &(eeprom_buff[i - first_word])) < 0) {
418 kfree(eeprom_buff);
419 return -EIO;
420 }
David Brownell2e55cc72005-08-31 09:53:10 -0700421 }
Christian Rieschceb02c92012-07-19 00:23:06 +0000422
423 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
424 kfree(eeprom_buff);
David Brownell2e55cc72005-08-31 09:53:10 -0700425 return 0;
426}
427
Christian Rieschcb7b24c2012-07-19 00:23:07 +0000428int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
429 u8 *data)
430{
431 struct usbnet *dev = netdev_priv(net);
432 u16 *eeprom_buff;
433 int first_word, last_word;
434 int i;
435 int ret;
436
437 netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
438 eeprom->len, eeprom->offset, eeprom->magic);
439
440 if (eeprom->len == 0)
441 return -EINVAL;
442
443 if (eeprom->magic != AX_EEPROM_MAGIC)
444 return -EINVAL;
445
446 first_word = eeprom->offset >> 1;
447 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
448
449 eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
450 GFP_KERNEL);
451 if (!eeprom_buff)
452 return -ENOMEM;
453
454 /* align data to 16 bit boundaries, read the missing data from
455 the EEPROM */
456 if (eeprom->offset & 1) {
457 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
458 &(eeprom_buff[0]));
459 if (ret < 0) {
460 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
461 goto free;
462 }
463 }
464
465 if ((eeprom->offset + eeprom->len) & 1) {
466 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
467 &(eeprom_buff[last_word - first_word]));
468 if (ret < 0) {
469 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
470 goto free;
471 }
472 }
473
474 memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
475
476 /* write data to EEPROM */
477 ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL);
478 if (ret < 0) {
479 netdev_err(net, "Failed to enable EEPROM write\n");
480 goto free;
481 }
482 msleep(20);
483
484 for (i = first_word; i <= last_word; i++) {
485 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
486 i, eeprom_buff[i - first_word]);
487 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
488 eeprom_buff[i - first_word], 0, NULL);
489 if (ret < 0) {
490 netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
491 i);
492 goto free;
493 }
494 msleep(20);
495 }
496
497 ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL);
498 if (ret < 0) {
499 netdev_err(net, "Failed to disable EEPROM write\n");
500 goto free;
501 }
502
503 ret = 0;
504free:
505 kfree(eeprom_buff);
506 return ret;
507}
508
Christian Riesch607740b2012-07-13 05:26:30 +0000509void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
David Brownell2e55cc72005-08-31 09:53:10 -0700510{
511 /* Inherit standard device info */
512 usbnet_get_drvinfo(net, info);
Grant Grundler83e1b912011-10-04 09:55:18 +0000513 strncpy (info->driver, DRIVER_NAME, sizeof info->driver);
David Hollis933a27d2006-07-29 10:12:50 -0400514 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
Christian Rieschceb02c92012-07-19 00:23:06 +0000515 info->eedump_len = AX_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700516}
517
Christian Riesch607740b2012-07-13 05:26:30 +0000518int asix_set_mac_address(struct net_device *net, void *p)
Jussi Kivilinna7f29a3b2010-03-09 12:24:38 +0000519{
520 struct usbnet *dev = netdev_priv(net);
521 struct asix_data *data = (struct asix_data *)&dev->data;
522 struct sockaddr *addr = p;
523
524 if (netif_running(net))
525 return -EBUSY;
526 if (!is_valid_ether_addr(addr->sa_data))
527 return -EADDRNOTAVAIL;
528
529 memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
530
531 /* We use the 20 byte dev->data
532 * for our 6 byte mac buffer
533 * to avoid allocating memory that
534 * is tricky to free later */
535 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
536 asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
537 data->mac_addr);
538
539 return 0;
540}