blob: f7f623a5390ee94ec44fbe6dd8a47f1ebfd6a332 [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
Lucas Stach8b5b6f52013-01-16 04:24:07 +000054int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
55 struct asix_rx_fixup_info *rx)
David Hollis48b1be62006-03-28 20:15:42 -050056{
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000057 int offset = 0;
David Hollis48b1be62006-03-28 20:15:42 -050058
Lucas Stach8b5b6f52013-01-16 04:24:07 +000059 while (offset + sizeof(u16) <= skb->len) {
60 u16 remaining = 0;
61 unsigned char *data;
David Hollis48b1be62006-03-28 20:15:42 -050062
Lucas Stach8b5b6f52013-01-16 04:24:07 +000063 if (!rx->size) {
64 if ((skb->len - offset == sizeof(u16)) ||
65 rx->split_head) {
66 if(!rx->split_head) {
67 rx->header = get_unaligned_le16(
68 skb->data + offset);
69 rx->split_head = true;
70 offset += sizeof(u16);
71 break;
72 } else {
73 rx->header |= (get_unaligned_le16(
74 skb->data + offset)
75 << 16);
76 rx->split_head = false;
77 offset += sizeof(u16);
78 }
79 } else {
80 rx->header = get_unaligned_le32(skb->data +
81 offset);
82 offset += sizeof(u32);
83 }
Marek Vasutbc466e62011-07-26 16:44:46 +000084
Lucas Stach8b5b6f52013-01-16 04:24:07 +000085 /* get the packet length */
86 rx->size = (u16) (rx->header & 0x7ff);
87 if (rx->size != ((~rx->header >> 16) & 0x7ff)) {
88 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
89 rx->header, offset);
90 rx->size = 0;
91 return 0;
92 }
93 rx->ax_skb = netdev_alloc_skb_ip_align(dev->net,
94 rx->size);
95 if (!rx->ax_skb)
96 return 0;
Neil Jones3f78d1f2010-05-17 17:18:28 -070097 }
98
Lucas Stach8b5b6f52013-01-16 04:24:07 +000099 if (rx->size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
Joe Perches60b86752010-02-17 10:30:23 +0000100 netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000101 rx->size);
102 kfree_skb(rx->ax_skb);
David Hollis933a27d2006-07-29 10:12:50 -0400103 return 0;
104 }
David Hollis933a27d2006-07-29 10:12:50 -0400105
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000106 if (rx->size > skb->len - offset) {
107 remaining = rx->size - (skb->len - offset);
108 rx->size = skb->len - offset;
109 }
David Hollis933a27d2006-07-29 10:12:50 -0400110
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000111 data = skb_put(rx->ax_skb, rx->size);
112 memcpy(data, skb->data + offset, rx->size);
113 if (!remaining)
114 usbnet_skb_return(dev, rx->ax_skb);
115
116 offset += (rx->size + 1) & 0xfffe;
117 rx->size = remaining;
David Hollis933a27d2006-07-29 10:12:50 -0400118 }
119
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000120 if (skb->len != offset) {
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000121 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
122 skb->len, offset);
David Hollis933a27d2006-07-29 10:12:50 -0400123 return 0;
124 }
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000125
David Hollis933a27d2006-07-29 10:12:50 -0400126 return 1;
David Hollis48b1be62006-03-28 20:15:42 -0500127}
128
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000129int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
130{
131 struct asix_common_private *dp = dev->driver_priv;
132 struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
133
134 return asix_rx_fixup_internal(dev, skb, rx);
135}
136
Christian Riesch607740b2012-07-13 05:26:30 +0000137struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
138 gfp_t flags)
David Hollis48b1be62006-03-28 20:15:42 -0500139{
David Hollis933a27d2006-07-29 10:12:50 -0400140 int padlen;
141 int headroom = skb_headroom(skb);
142 int tailroom = skb_tailroom(skb);
143 u32 packet_len;
144 u32 padbytes = 0xffff0000;
David Hollis48b1be62006-03-28 20:15:42 -0500145
Ingo van Lil2a580942012-04-23 22:05:38 +0000146 padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
David Hollis48b1be62006-03-28 20:15:42 -0500147
Eric Dumazet95162d62012-07-05 04:31:01 +0000148 /* We need to push 4 bytes in front of frame (packet_len)
149 * and maybe add 4 bytes after the end (if padlen is 4)
150 *
151 * Avoid skb_copy_expand() expensive call, using following rules :
152 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
153 * is false (and if we have 4 bytes of headroom)
154 * - We are allowed to put 4 bytes at tail if skb_cloned()
155 * is false (and if we have 4 bytes of tailroom)
156 *
157 * TCP packets for example are cloned, but skb_header_release()
158 * was called in tcp stack, allowing us to use headroom for our needs.
159 */
160 if (!skb_header_cloned(skb) &&
161 !(padlen && skb_cloned(skb)) &&
162 headroom + tailroom >= 4 + padlen) {
163 /* following should not happen, but better be safe */
164 if (headroom < 4 ||
165 tailroom < padlen) {
David Hollis933a27d2006-07-29 10:12:50 -0400166 skb->data = memmove(skb->head + 4, skb->data, skb->len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700167 skb_set_tail_pointer(skb, skb->len);
David Hollis933a27d2006-07-29 10:12:50 -0400168 }
169 } else {
170 struct sk_buff *skb2;
Eric Dumazet95162d62012-07-05 04:31:01 +0000171
David Hollis933a27d2006-07-29 10:12:50 -0400172 skb2 = skb_copy_expand(skb, 4, padlen, flags);
173 dev_kfree_skb_any(skb);
174 skb = skb2;
175 if (!skb)
176 return NULL;
177 }
178
Eric Dumazet95162d62012-07-05 04:31:01 +0000179 packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
David Hollis933a27d2006-07-29 10:12:50 -0400180 skb_push(skb, 4);
David Hollis57e4f042007-02-05 12:03:03 -0500181 cpu_to_le32s(&packet_len);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300182 skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
David Hollis933a27d2006-07-29 10:12:50 -0400183
Ingo van Lil2a580942012-04-23 22:05:38 +0000184 if (padlen) {
David Hollis57e4f042007-02-05 12:03:03 -0500185 cpu_to_le32s(&padbytes);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700186 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
David Hollis933a27d2006-07-29 10:12:50 -0400187 skb_put(skb, sizeof(padbytes));
188 }
189 return skb;
David Hollis48b1be62006-03-28 20:15:42 -0500190}
191
Christian Riesch607740b2012-07-13 05:26:30 +0000192int asix_set_sw_mii(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -0700193{
David Hollis933a27d2006-07-29 10:12:50 -0400194 int ret;
195 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
196 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000197 netdev_err(dev->net, "Failed to enable software MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400198 return ret;
David Brownell2e55cc72005-08-31 09:53:10 -0700199}
200
Christian Riesch607740b2012-07-13 05:26:30 +0000201int asix_set_hw_mii(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400202{
203 int ret;
204 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
205 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000206 netdev_err(dev->net, "Failed to enable hardware MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400207 return ret;
208}
209
Christian Riesch16626b02012-07-13 05:26:31 +0000210int asix_read_phy_addr(struct usbnet *dev, int internal)
David Hollis933a27d2006-07-29 10:12:50 -0400211{
Christian Riesch16626b02012-07-13 05:26:31 +0000212 int offset = (internal ? 1 : 0);
Al Viro51bf2972007-12-22 17:42:36 +0000213 u8 buf[2];
214 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
David Hollis933a27d2006-07-29 10:12:50 -0400215
Joe Perches60b86752010-02-17 10:30:23 +0000216 netdev_dbg(dev->net, "asix_get_phy_addr()\n");
David Hollis933a27d2006-07-29 10:12:50 -0400217
Al Viro51bf2972007-12-22 17:42:36 +0000218 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000219 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000220 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400221 }
Joe Perches60b86752010-02-17 10:30:23 +0000222 netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
223 *((__le16 *)buf));
Christian Riesch16626b02012-07-13 05:26:31 +0000224 ret = buf[offset];
Al Viro51bf2972007-12-22 17:42:36 +0000225
226out:
David Hollis933a27d2006-07-29 10:12:50 -0400227 return ret;
228}
229
Christian Riesch16626b02012-07-13 05:26:31 +0000230int asix_get_phy_addr(struct usbnet *dev)
231{
232 /* return the address of the internal phy */
233 return asix_read_phy_addr(dev, 1);
234}
235
236
Christian Riesch607740b2012-07-13 05:26:30 +0000237int asix_sw_reset(struct usbnet *dev, u8 flags)
David Hollis933a27d2006-07-29 10:12:50 -0400238{
239 int ret;
240
241 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
242 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000243 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
David Hollis933a27d2006-07-29 10:12:50 -0400244
245 return ret;
246}
247
Christian Riesch607740b2012-07-13 05:26:30 +0000248u16 asix_read_rx_ctl(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400249{
Al Viro51bf2972007-12-22 17:42:36 +0000250 __le16 v;
251 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400252
Al Viro51bf2972007-12-22 17:42:36 +0000253 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000254 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000255 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400256 }
Al Viro51bf2972007-12-22 17:42:36 +0000257 ret = le16_to_cpu(v);
258out:
David Hollis933a27d2006-07-29 10:12:50 -0400259 return ret;
260}
261
Christian Riesch607740b2012-07-13 05:26:30 +0000262int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
David Hollis933a27d2006-07-29 10:12:50 -0400263{
264 int ret;
265
Joe Perches60b86752010-02-17 10:30:23 +0000266 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400267 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
268 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000269 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
270 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400271
272 return ret;
273}
274
Christian Riesch607740b2012-07-13 05:26:30 +0000275u16 asix_read_medium_status(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400276{
Al Viro51bf2972007-12-22 17:42:36 +0000277 __le16 v;
278 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400279
Al Viro51bf2972007-12-22 17:42:36 +0000280 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000281 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
282 ret);
Grant Grundler83e1b912011-10-04 09:55:18 +0000283 return ret; /* TODO: callers not checking for error ret */
David Hollis933a27d2006-07-29 10:12:50 -0400284 }
Grant Grundler83e1b912011-10-04 09:55:18 +0000285
286 return le16_to_cpu(v);
287
David Hollis933a27d2006-07-29 10:12:50 -0400288}
289
Christian Riesch607740b2012-07-13 05:26:30 +0000290int asix_write_medium_mode(struct usbnet *dev, u16 mode)
David Hollis933a27d2006-07-29 10:12:50 -0400291{
292 int ret;
293
Joe Perches60b86752010-02-17 10:30:23 +0000294 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400295 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
296 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000297 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
298 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400299
300 return ret;
301}
302
Christian Riesch607740b2012-07-13 05:26:30 +0000303int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
David Hollis933a27d2006-07-29 10:12:50 -0400304{
305 int ret;
306
Joe Perches60b86752010-02-17 10:30:23 +0000307 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
David Hollis933a27d2006-07-29 10:12:50 -0400308 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
309 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000310 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
311 value, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400312
313 if (sleep)
314 msleep(sleep);
315
316 return ret;
317}
318
319/*
320 * AX88772 & AX88178 have a 16-bit RX_CTL value
321 */
Christian Riesch607740b2012-07-13 05:26:30 +0000322void asix_set_multicast(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700323{
324 struct usbnet *dev = netdev_priv(net);
David Hollis48b1be62006-03-28 20:15:42 -0500325 struct asix_data *data = (struct asix_data *)&dev->data;
David Hollis933a27d2006-07-29 10:12:50 -0400326 u16 rx_ctl = AX_DEFAULT_RX_CTL;
David Brownell2e55cc72005-08-31 09:53:10 -0700327
328 if (net->flags & IFF_PROMISC) {
David Hollis933a27d2006-07-29 10:12:50 -0400329 rx_ctl |= AX_RX_CTL_PRO;
Joe Perches8e95a202009-12-03 07:58:21 +0000330 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000331 netdev_mc_count(net) > AX_MAX_MCAST) {
David Hollis933a27d2006-07-29 10:12:50 -0400332 rx_ctl |= AX_RX_CTL_AMALL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000333 } else if (netdev_mc_empty(net)) {
David Brownell2e55cc72005-08-31 09:53:10 -0700334 /* just broadcast and directed */
335 } else {
336 /* We use the 20 byte dev->data
337 * for our 8 byte filter buffer
338 * to avoid allocating memory that
339 * is tricky to free later */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000340 struct netdev_hw_addr *ha;
David Brownell2e55cc72005-08-31 09:53:10 -0700341 u32 crc_bits;
David Brownell2e55cc72005-08-31 09:53:10 -0700342
343 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
344
345 /* Build the multicast hash filter. */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000346 netdev_for_each_mc_addr(ha, net) {
347 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
David Brownell2e55cc72005-08-31 09:53:10 -0700348 data->multi_filter[crc_bits >> 3] |=
349 1 << (crc_bits & 7);
David Brownell2e55cc72005-08-31 09:53:10 -0700350 }
351
David Hollis48b1be62006-03-28 20:15:42 -0500352 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
David Brownell2e55cc72005-08-31 09:53:10 -0700353 AX_MCAST_FILTER_SIZE, data->multi_filter);
354
David Hollis933a27d2006-07-29 10:12:50 -0400355 rx_ctl |= AX_RX_CTL_AM;
David Brownell2e55cc72005-08-31 09:53:10 -0700356 }
357
David Hollis48b1be62006-03-28 20:15:42 -0500358 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700359}
360
Christian Riesch607740b2012-07-13 05:26:30 +0000361int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
David Brownell2e55cc72005-08-31 09:53:10 -0700362{
363 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000364 __le16 res;
David Brownell2e55cc72005-08-31 09:53:10 -0700365
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200366 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500367 asix_set_sw_mii(dev);
368 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
Al Viro51bf2972007-12-22 17:42:36 +0000369 (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500370 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200371 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700372
Joe Perches60b86752010-02-17 10:30:23 +0000373 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
374 phy_id, loc, le16_to_cpu(res));
David Brownell2e55cc72005-08-31 09:53:10 -0700375
Al Viro51bf2972007-12-22 17:42:36 +0000376 return le16_to_cpu(res);
David Brownell2e55cc72005-08-31 09:53:10 -0700377}
378
Christian Riesch607740b2012-07-13 05:26:30 +0000379void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
David Brownell2e55cc72005-08-31 09:53:10 -0700380{
381 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000382 __le16 res = cpu_to_le16(val);
David Brownell2e55cc72005-08-31 09:53:10 -0700383
Joe Perches60b86752010-02-17 10:30:23 +0000384 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
385 phy_id, loc, val);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200386 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500387 asix_set_sw_mii(dev);
Al Viro51bf2972007-12-22 17:42:36 +0000388 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500389 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200390 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700391}
392
Christian Riesch607740b2012-07-13 05:26:30 +0000393void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700394{
395 struct usbnet *dev = netdev_priv(net);
396 u8 opt;
397
David Hollis48b1be62006-03-28 20:15:42 -0500398 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700399 wolinfo->supported = 0;
400 wolinfo->wolopts = 0;
401 return;
402 }
403 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
404 wolinfo->wolopts = 0;
allanf87ce5b2011-12-22 20:38:51 +0000405 if (opt & AX_MONITOR_LINK)
406 wolinfo->wolopts |= WAKE_PHY;
407 if (opt & AX_MONITOR_MAGIC)
408 wolinfo->wolopts |= WAKE_MAGIC;
David Brownell2e55cc72005-08-31 09:53:10 -0700409}
410
Christian Riesch607740b2012-07-13 05:26:30 +0000411int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700412{
413 struct usbnet *dev = netdev_priv(net);
414 u8 opt = 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700415
416 if (wolinfo->wolopts & WAKE_PHY)
417 opt |= AX_MONITOR_LINK;
418 if (wolinfo->wolopts & WAKE_MAGIC)
419 opt |= AX_MONITOR_MAGIC;
David Brownell2e55cc72005-08-31 09:53:10 -0700420
David Hollis48b1be62006-03-28 20:15:42 -0500421 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
Al Viro51bf2972007-12-22 17:42:36 +0000422 opt, 0, 0, NULL) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700423 return -EINVAL;
424
425 return 0;
426}
427
Christian Riesch607740b2012-07-13 05:26:30 +0000428int asix_get_eeprom_len(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700429{
Christian Rieschceb02c92012-07-19 00:23:06 +0000430 return AX_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700431}
432
Christian Riesch607740b2012-07-13 05:26:30 +0000433int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
434 u8 *data)
David Brownell2e55cc72005-08-31 09:53:10 -0700435{
436 struct usbnet *dev = netdev_priv(net);
Christian Rieschceb02c92012-07-19 00:23:06 +0000437 u16 *eeprom_buff;
438 int first_word, last_word;
David Brownell2e55cc72005-08-31 09:53:10 -0700439 int i;
440
Christian Rieschceb02c92012-07-19 00:23:06 +0000441 if (eeprom->len == 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700442 return -EINVAL;
443
444 eeprom->magic = AX_EEPROM_MAGIC;
445
Christian Rieschceb02c92012-07-19 00:23:06 +0000446 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
David Brownell2e55cc72005-08-31 09:53:10 -0700454 /* ax8817x returns 2 bytes from eeprom on read */
Christian Rieschceb02c92012-07-19 00:23:06 +0000455 for (i = first_word; i <= last_word; i++) {
456 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
457 &(eeprom_buff[i - first_word])) < 0) {
458 kfree(eeprom_buff);
459 return -EIO;
460 }
David Brownell2e55cc72005-08-31 09:53:10 -0700461 }
Christian Rieschceb02c92012-07-19 00:23:06 +0000462
463 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
464 kfree(eeprom_buff);
David Brownell2e55cc72005-08-31 09:53:10 -0700465 return 0;
466}
467
Christian Rieschcb7b24c2012-07-19 00:23:07 +0000468int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
469 u8 *data)
470{
471 struct usbnet *dev = netdev_priv(net);
472 u16 *eeprom_buff;
473 int first_word, last_word;
474 int i;
475 int ret;
476
477 netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
478 eeprom->len, eeprom->offset, eeprom->magic);
479
480 if (eeprom->len == 0)
481 return -EINVAL;
482
483 if (eeprom->magic != AX_EEPROM_MAGIC)
484 return -EINVAL;
485
486 first_word = eeprom->offset >> 1;
487 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
488
489 eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
490 GFP_KERNEL);
491 if (!eeprom_buff)
492 return -ENOMEM;
493
494 /* align data to 16 bit boundaries, read the missing data from
495 the EEPROM */
496 if (eeprom->offset & 1) {
497 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
498 &(eeprom_buff[0]));
499 if (ret < 0) {
500 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
501 goto free;
502 }
503 }
504
505 if ((eeprom->offset + eeprom->len) & 1) {
506 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
507 &(eeprom_buff[last_word - first_word]));
508 if (ret < 0) {
509 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
510 goto free;
511 }
512 }
513
514 memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
515
516 /* write data to EEPROM */
517 ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL);
518 if (ret < 0) {
519 netdev_err(net, "Failed to enable EEPROM write\n");
520 goto free;
521 }
522 msleep(20);
523
524 for (i = first_word; i <= last_word; i++) {
525 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
526 i, eeprom_buff[i - first_word]);
527 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
528 eeprom_buff[i - first_word], 0, NULL);
529 if (ret < 0) {
530 netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
531 i);
532 goto free;
533 }
534 msleep(20);
535 }
536
537 ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL);
538 if (ret < 0) {
539 netdev_err(net, "Failed to disable EEPROM write\n");
540 goto free;
541 }
542
543 ret = 0;
544free:
545 kfree(eeprom_buff);
546 return ret;
547}
548
Christian Riesch607740b2012-07-13 05:26:30 +0000549void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
David Brownell2e55cc72005-08-31 09:53:10 -0700550{
551 /* Inherit standard device info */
552 usbnet_get_drvinfo(net, info);
Jiri Pirko7826d432013-01-06 00:44:26 +0000553 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
554 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
Christian Rieschceb02c92012-07-19 00:23:06 +0000555 info->eedump_len = AX_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700556}
557
Christian Riesch607740b2012-07-13 05:26:30 +0000558int asix_set_mac_address(struct net_device *net, void *p)
Jussi Kivilinna7f29a3b2010-03-09 12:24:38 +0000559{
560 struct usbnet *dev = netdev_priv(net);
561 struct asix_data *data = (struct asix_data *)&dev->data;
562 struct sockaddr *addr = p;
563
564 if (netif_running(net))
565 return -EBUSY;
566 if (!is_valid_ether_addr(addr->sa_data))
567 return -EADDRNOTAVAIL;
568
569 memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
570
571 /* We use the 20 byte dev->data
572 * for our 6 byte mac buffer
573 * to avoid allocating memory that
574 * is tricky to free later */
575 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
576 asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
577 data->mac_addr);
578
579 return 0;
580}