blob: 1e4768debcc9e54704e52fc578a8ec32853e9fa6 [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
Jeff Kirsher9cb00072013-12-06 06:28:46 -080019 * along with this program; if not, see <http://www.gnu.org/licenses/>.
David Brownell2e55cc72005-08-31 09:53:10 -070020 */
21
Christian Riesch607740b2012-07-13 05:26:30 +000022#include "asix.h"
David Brownell2e55cc72005-08-31 09:53:10 -070023
Christian Riesch607740b2012-07-13 05:26:30 +000024int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
25 u16 size, void *data)
David Brownell2e55cc72005-08-31 09:53:10 -070026{
Ming Lei0bc69ef2012-10-24 19:46:55 +000027 int ret;
28 ret = usbnet_read_cmd(dev, cmd,
29 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
30 value, index, data, size);
Al Viro51bf2972007-12-22 17:42:36 +000031
Ming Lei0bc69ef2012-10-24 19:46:55 +000032 if (ret != size && ret >= 0)
33 return -EINVAL;
34 return ret;
David Brownell2e55cc72005-08-31 09:53:10 -070035}
36
Christian Riesch607740b2012-07-13 05:26:30 +000037int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
38 u16 size, void *data)
David Brownell2e55cc72005-08-31 09:53:10 -070039{
Ming Lei0bc69ef2012-10-24 19:46:55 +000040 return usbnet_write_cmd(dev, cmd,
41 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
42 value, index, data, size);
David Brownell2e55cc72005-08-31 09:53:10 -070043}
44
Christian Riesch607740b2012-07-13 05:26:30 +000045void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
46 u16 size, void *data)
David Hollis48b1be62006-03-28 20:15:42 -050047{
Ming Lei0bc69ef2012-10-24 19:46:55 +000048 usbnet_write_cmd_async(dev, cmd,
49 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
50 value, index, data, size);
David Hollis48b1be62006-03-28 20:15:42 -050051}
52
Lucas Stach8b5b6f52013-01-16 04:24:07 +000053int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
54 struct asix_rx_fixup_info *rx)
David Hollis48b1be62006-03-28 20:15:42 -050055{
Eric Dumazeta9e0aca2012-03-14 20:18:32 +000056 int offset = 0;
Dean Jenkins7b0378f2015-10-02 14:29:04 +010057 u16 size;
David Hollis48b1be62006-03-28 20:15:42 -050058
Dean Jenkins3f30b152015-10-02 14:29:07 +010059 /* When an Ethernet frame spans multiple URB socket buffers,
60 * do a sanity test for the Data header synchronisation.
61 * Attempt to detect the situation of the previous socket buffer having
62 * been truncated or a socket buffer was missing. These situations
63 * cause a discontinuity in the data stream and therefore need to avoid
64 * appending bad data to the end of the current netdev socket buffer.
65 * Also avoid unnecessarily discarding a good current netdev socket
66 * buffer.
67 */
68 if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
69 offset = ((rx->remaining + 1) & 0xfffe) + sizeof(u32);
70 rx->header = get_unaligned_le32(skb->data + offset);
71 offset = 0;
72
73 size = (u16)(rx->header & 0x7ff);
74 if (size != ((~rx->header >> 16) & 0x7ff)) {
75 netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
76 rx->remaining);
77 kfree_skb(rx->ax_skb);
78 rx->ax_skb = NULL;
79 /* Discard the incomplete netdev Ethernet frame and
80 * assume the Data header is at the start of the current
81 * URB socket buffer.
82 */
83 rx->remaining = 0;
84 }
85 }
86
Lucas Stach8b5b6f52013-01-16 04:24:07 +000087 while (offset + sizeof(u16) <= skb->len) {
Dean Jenkins7b0378f2015-10-02 14:29:04 +010088 u16 copy_length;
Lucas Stach8b5b6f52013-01-16 04:24:07 +000089 unsigned char *data;
David Hollis48b1be62006-03-28 20:15:42 -050090
Dean Jenkins7b0378f2015-10-02 14:29:04 +010091 if (!rx->remaining) {
Dean Jenkins3bfc69a2015-10-02 14:29:05 +010092 if (skb->len - offset == sizeof(u16)) {
93 rx->header = get_unaligned_le16(
94 skb->data + offset);
95 rx->split_head = true;
96 offset += sizeof(u16);
97 break;
98 }
99
100 if (rx->split_head == true) {
101 rx->header |= (get_unaligned_le16(
102 skb->data + offset) << 16);
103 rx->split_head = false;
104 offset += sizeof(u16);
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000105 } else {
106 rx->header = get_unaligned_le32(skb->data +
107 offset);
108 offset += sizeof(u32);
109 }
Marek Vasutbc466e62011-07-26 16:44:46 +0000110
Dean Jenkins7b0378f2015-10-02 14:29:04 +0100111 /* take frame length from Data header 32-bit word */
112 size = (u16)(rx->header & 0x7ff);
113 if (size != ((~rx->header >> 16) & 0x7ff)) {
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000114 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
115 rx->header, offset);
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000116 return 0;
117 }
Dean Jenkins9a5ccd82015-10-02 14:29:06 +0100118 if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
119 netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
120 size);
121 return 0;
122 }
123
Dean Jenkins7b0378f2015-10-02 14:29:04 +0100124 rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000125 if (!rx->ax_skb)
126 return 0;
Neil Jones3f78d1f2010-05-17 17:18:28 -0700127
Dean Jenkins9a5ccd82015-10-02 14:29:06 +0100128 rx->remaining = size;
David Hollis933a27d2006-07-29 10:12:50 -0400129 }
David Hollis933a27d2006-07-29 10:12:50 -0400130
Dean Jenkins7b0378f2015-10-02 14:29:04 +0100131 if (rx->remaining > skb->len - offset) {
132 copy_length = skb->len - offset;
133 rx->remaining -= copy_length;
134 } else {
135 copy_length = rx->remaining;
136 rx->remaining = 0;
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000137 }
David Hollis933a27d2006-07-29 10:12:50 -0400138
Dean Jenkins7b0378f2015-10-02 14:29:04 +0100139 data = skb_put(rx->ax_skb, copy_length);
140 memcpy(data, skb->data + offset, copy_length);
141 if (!rx->remaining)
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000142 usbnet_skb_return(dev, rx->ax_skb);
143
Dean Jenkins7b0378f2015-10-02 14:29:04 +0100144 offset += (copy_length + 1) & 0xfffe;
David Hollis933a27d2006-07-29 10:12:50 -0400145 }
146
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000147 if (skb->len != offset) {
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000148 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
149 skb->len, offset);
David Hollis933a27d2006-07-29 10:12:50 -0400150 return 0;
151 }
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000152
David Hollis933a27d2006-07-29 10:12:50 -0400153 return 1;
David Hollis48b1be62006-03-28 20:15:42 -0500154}
155
Lucas Stach8b5b6f52013-01-16 04:24:07 +0000156int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
157{
158 struct asix_common_private *dp = dev->driver_priv;
159 struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
160
161 return asix_rx_fixup_internal(dev, skb, rx);
162}
163
Christian Riesch607740b2012-07-13 05:26:30 +0000164struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
165 gfp_t flags)
David Hollis48b1be62006-03-28 20:15:42 -0500166{
David Hollis933a27d2006-07-29 10:12:50 -0400167 int padlen;
168 int headroom = skb_headroom(skb);
169 int tailroom = skb_tailroom(skb);
170 u32 packet_len;
171 u32 padbytes = 0xffff0000;
David Hollis48b1be62006-03-28 20:15:42 -0500172
Ingo van Lil2a580942012-04-23 22:05:38 +0000173 padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
David Hollis48b1be62006-03-28 20:15:42 -0500174
Eric Dumazet95162d62012-07-05 04:31:01 +0000175 /* We need to push 4 bytes in front of frame (packet_len)
176 * and maybe add 4 bytes after the end (if padlen is 4)
177 *
178 * Avoid skb_copy_expand() expensive call, using following rules :
179 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
180 * is false (and if we have 4 bytes of headroom)
181 * - We are allowed to put 4 bytes at tail if skb_cloned()
182 * is false (and if we have 4 bytes of tailroom)
183 *
184 * TCP packets for example are cloned, but skb_header_release()
185 * was called in tcp stack, allowing us to use headroom for our needs.
186 */
187 if (!skb_header_cloned(skb) &&
188 !(padlen && skb_cloned(skb)) &&
189 headroom + tailroom >= 4 + padlen) {
190 /* following should not happen, but better be safe */
191 if (headroom < 4 ||
192 tailroom < padlen) {
David Hollis933a27d2006-07-29 10:12:50 -0400193 skb->data = memmove(skb->head + 4, skb->data, skb->len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700194 skb_set_tail_pointer(skb, skb->len);
David Hollis933a27d2006-07-29 10:12:50 -0400195 }
196 } else {
197 struct sk_buff *skb2;
Eric Dumazet95162d62012-07-05 04:31:01 +0000198
David Hollis933a27d2006-07-29 10:12:50 -0400199 skb2 = skb_copy_expand(skb, 4, padlen, flags);
200 dev_kfree_skb_any(skb);
201 skb = skb2;
202 if (!skb)
203 return NULL;
204 }
205
Eric Dumazet95162d62012-07-05 04:31:01 +0000206 packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
David Hollis933a27d2006-07-29 10:12:50 -0400207 skb_push(skb, 4);
David Hollis57e4f042007-02-05 12:03:03 -0500208 cpu_to_le32s(&packet_len);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300209 skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
David Hollis933a27d2006-07-29 10:12:50 -0400210
Ingo van Lil2a580942012-04-23 22:05:38 +0000211 if (padlen) {
David Hollis57e4f042007-02-05 12:03:03 -0500212 cpu_to_le32s(&padbytes);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700213 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
David Hollis933a27d2006-07-29 10:12:50 -0400214 skb_put(skb, sizeof(padbytes));
215 }
Ben Hutchings1e9e39f2015-02-26 19:34:37 +0000216
Ben Hutchings7a1e8902015-03-25 21:41:33 +0100217 usbnet_set_skb_tx_stats(skb, 1, 0);
David Hollis933a27d2006-07-29 10:12:50 -0400218 return skb;
David Hollis48b1be62006-03-28 20:15:42 -0500219}
220
Christian Riesch607740b2012-07-13 05:26:30 +0000221int asix_set_sw_mii(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -0700222{
David Hollis933a27d2006-07-29 10:12:50 -0400223 int ret;
224 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
225 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000226 netdev_err(dev->net, "Failed to enable software MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400227 return ret;
David Brownell2e55cc72005-08-31 09:53:10 -0700228}
229
Christian Riesch607740b2012-07-13 05:26:30 +0000230int asix_set_hw_mii(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400231{
232 int ret;
233 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
234 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000235 netdev_err(dev->net, "Failed to enable hardware MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400236 return ret;
237}
238
Christian Riesch16626b02012-07-13 05:26:31 +0000239int asix_read_phy_addr(struct usbnet *dev, int internal)
David Hollis933a27d2006-07-29 10:12:50 -0400240{
Christian Riesch16626b02012-07-13 05:26:31 +0000241 int offset = (internal ? 1 : 0);
Al Viro51bf2972007-12-22 17:42:36 +0000242 u8 buf[2];
243 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
David Hollis933a27d2006-07-29 10:12:50 -0400244
Joe Perches60b86752010-02-17 10:30:23 +0000245 netdev_dbg(dev->net, "asix_get_phy_addr()\n");
David Hollis933a27d2006-07-29 10:12:50 -0400246
Al Viro51bf2972007-12-22 17:42:36 +0000247 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000248 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000249 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400250 }
Joe Perches60b86752010-02-17 10:30:23 +0000251 netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
252 *((__le16 *)buf));
Christian Riesch16626b02012-07-13 05:26:31 +0000253 ret = buf[offset];
Al Viro51bf2972007-12-22 17:42:36 +0000254
255out:
David Hollis933a27d2006-07-29 10:12:50 -0400256 return ret;
257}
258
Christian Riesch16626b02012-07-13 05:26:31 +0000259int asix_get_phy_addr(struct usbnet *dev)
260{
261 /* return the address of the internal phy */
262 return asix_read_phy_addr(dev, 1);
263}
264
265
Christian Riesch607740b2012-07-13 05:26:30 +0000266int asix_sw_reset(struct usbnet *dev, u8 flags)
David Hollis933a27d2006-07-29 10:12:50 -0400267{
268 int ret;
269
270 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
271 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000272 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
David Hollis933a27d2006-07-29 10:12:50 -0400273
274 return ret;
275}
276
Christian Riesch607740b2012-07-13 05:26:30 +0000277u16 asix_read_rx_ctl(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400278{
Al Viro51bf2972007-12-22 17:42:36 +0000279 __le16 v;
280 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400281
Al Viro51bf2972007-12-22 17:42:36 +0000282 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000283 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000284 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400285 }
Al Viro51bf2972007-12-22 17:42:36 +0000286 ret = le16_to_cpu(v);
287out:
David Hollis933a27d2006-07-29 10:12:50 -0400288 return ret;
289}
290
Christian Riesch607740b2012-07-13 05:26:30 +0000291int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
David Hollis933a27d2006-07-29 10:12:50 -0400292{
293 int ret;
294
Joe Perches60b86752010-02-17 10:30:23 +0000295 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400296 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
297 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000298 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
299 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400300
301 return ret;
302}
303
Christian Riesch607740b2012-07-13 05:26:30 +0000304u16 asix_read_medium_status(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400305{
Al Viro51bf2972007-12-22 17:42:36 +0000306 __le16 v;
307 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400308
Al Viro51bf2972007-12-22 17:42:36 +0000309 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000310 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
311 ret);
Grant Grundler83e1b912011-10-04 09:55:18 +0000312 return ret; /* TODO: callers not checking for error ret */
David Hollis933a27d2006-07-29 10:12:50 -0400313 }
Grant Grundler83e1b912011-10-04 09:55:18 +0000314
315 return le16_to_cpu(v);
316
David Hollis933a27d2006-07-29 10:12:50 -0400317}
318
Christian Riesch607740b2012-07-13 05:26:30 +0000319int asix_write_medium_mode(struct usbnet *dev, u16 mode)
David Hollis933a27d2006-07-29 10:12:50 -0400320{
321 int ret;
322
Joe Perches60b86752010-02-17 10:30:23 +0000323 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400324 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
325 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000326 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
327 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400328
329 return ret;
330}
331
Christian Riesch607740b2012-07-13 05:26:30 +0000332int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
David Hollis933a27d2006-07-29 10:12:50 -0400333{
334 int ret;
335
Joe Perches60b86752010-02-17 10:30:23 +0000336 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
David Hollis933a27d2006-07-29 10:12:50 -0400337 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
338 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000339 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
340 value, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400341
342 if (sleep)
343 msleep(sleep);
344
345 return ret;
346}
347
348/*
349 * AX88772 & AX88178 have a 16-bit RX_CTL value
350 */
Christian Riesch607740b2012-07-13 05:26:30 +0000351void asix_set_multicast(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700352{
353 struct usbnet *dev = netdev_priv(net);
David Hollis48b1be62006-03-28 20:15:42 -0500354 struct asix_data *data = (struct asix_data *)&dev->data;
David Hollis933a27d2006-07-29 10:12:50 -0400355 u16 rx_ctl = AX_DEFAULT_RX_CTL;
David Brownell2e55cc72005-08-31 09:53:10 -0700356
357 if (net->flags & IFF_PROMISC) {
David Hollis933a27d2006-07-29 10:12:50 -0400358 rx_ctl |= AX_RX_CTL_PRO;
Joe Perches8e95a202009-12-03 07:58:21 +0000359 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000360 netdev_mc_count(net) > AX_MAX_MCAST) {
David Hollis933a27d2006-07-29 10:12:50 -0400361 rx_ctl |= AX_RX_CTL_AMALL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000362 } else if (netdev_mc_empty(net)) {
David Brownell2e55cc72005-08-31 09:53:10 -0700363 /* just broadcast and directed */
364 } else {
365 /* We use the 20 byte dev->data
366 * for our 8 byte filter buffer
367 * to avoid allocating memory that
368 * is tricky to free later */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000369 struct netdev_hw_addr *ha;
David Brownell2e55cc72005-08-31 09:53:10 -0700370 u32 crc_bits;
David Brownell2e55cc72005-08-31 09:53:10 -0700371
372 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
373
374 /* Build the multicast hash filter. */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000375 netdev_for_each_mc_addr(ha, net) {
376 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
David Brownell2e55cc72005-08-31 09:53:10 -0700377 data->multi_filter[crc_bits >> 3] |=
378 1 << (crc_bits & 7);
David Brownell2e55cc72005-08-31 09:53:10 -0700379 }
380
David Hollis48b1be62006-03-28 20:15:42 -0500381 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
David Brownell2e55cc72005-08-31 09:53:10 -0700382 AX_MCAST_FILTER_SIZE, data->multi_filter);
383
David Hollis933a27d2006-07-29 10:12:50 -0400384 rx_ctl |= AX_RX_CTL_AM;
David Brownell2e55cc72005-08-31 09:53:10 -0700385 }
386
David Hollis48b1be62006-03-28 20:15:42 -0500387 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700388}
389
Christian Riesch607740b2012-07-13 05:26:30 +0000390int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
David Brownell2e55cc72005-08-31 09:53:10 -0700391{
392 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000393 __le16 res;
David Brownell2e55cc72005-08-31 09:53:10 -0700394
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200395 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500396 asix_set_sw_mii(dev);
397 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
Al Viro51bf2972007-12-22 17:42:36 +0000398 (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500399 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200400 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700401
Joe Perches60b86752010-02-17 10:30:23 +0000402 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
403 phy_id, loc, le16_to_cpu(res));
David Brownell2e55cc72005-08-31 09:53:10 -0700404
Al Viro51bf2972007-12-22 17:42:36 +0000405 return le16_to_cpu(res);
David Brownell2e55cc72005-08-31 09:53:10 -0700406}
407
Christian Riesch607740b2012-07-13 05:26:30 +0000408void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
David Brownell2e55cc72005-08-31 09:53:10 -0700409{
410 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000411 __le16 res = cpu_to_le16(val);
David Brownell2e55cc72005-08-31 09:53:10 -0700412
Joe Perches60b86752010-02-17 10:30:23 +0000413 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
414 phy_id, loc, val);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200415 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500416 asix_set_sw_mii(dev);
Al Viro51bf2972007-12-22 17:42:36 +0000417 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500418 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200419 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700420}
421
Christian Riesch607740b2012-07-13 05:26:30 +0000422void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700423{
424 struct usbnet *dev = netdev_priv(net);
425 u8 opt;
426
David Hollis48b1be62006-03-28 20:15:42 -0500427 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700428 wolinfo->supported = 0;
429 wolinfo->wolopts = 0;
430 return;
431 }
432 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
433 wolinfo->wolopts = 0;
allanf87ce5b2011-12-22 20:38:51 +0000434 if (opt & AX_MONITOR_LINK)
435 wolinfo->wolopts |= WAKE_PHY;
436 if (opt & AX_MONITOR_MAGIC)
437 wolinfo->wolopts |= WAKE_MAGIC;
David Brownell2e55cc72005-08-31 09:53:10 -0700438}
439
Christian Riesch607740b2012-07-13 05:26:30 +0000440int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700441{
442 struct usbnet *dev = netdev_priv(net);
443 u8 opt = 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700444
445 if (wolinfo->wolopts & WAKE_PHY)
446 opt |= AX_MONITOR_LINK;
447 if (wolinfo->wolopts & WAKE_MAGIC)
448 opt |= AX_MONITOR_MAGIC;
David Brownell2e55cc72005-08-31 09:53:10 -0700449
David Hollis48b1be62006-03-28 20:15:42 -0500450 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
Al Viro51bf2972007-12-22 17:42:36 +0000451 opt, 0, 0, NULL) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700452 return -EINVAL;
453
454 return 0;
455}
456
Christian Riesch607740b2012-07-13 05:26:30 +0000457int asix_get_eeprom_len(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700458{
Christian Rieschceb02c92012-07-19 00:23:06 +0000459 return AX_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700460}
461
Christian Riesch607740b2012-07-13 05:26:30 +0000462int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
463 u8 *data)
David Brownell2e55cc72005-08-31 09:53:10 -0700464{
465 struct usbnet *dev = netdev_priv(net);
Christian Rieschceb02c92012-07-19 00:23:06 +0000466 u16 *eeprom_buff;
467 int first_word, last_word;
David Brownell2e55cc72005-08-31 09:53:10 -0700468 int i;
469
Christian Rieschceb02c92012-07-19 00:23:06 +0000470 if (eeprom->len == 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700471 return -EINVAL;
472
473 eeprom->magic = AX_EEPROM_MAGIC;
474
Christian Rieschceb02c92012-07-19 00:23:06 +0000475 first_word = eeprom->offset >> 1;
476 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
477
478 eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
479 GFP_KERNEL);
480 if (!eeprom_buff)
481 return -ENOMEM;
482
David Brownell2e55cc72005-08-31 09:53:10 -0700483 /* ax8817x returns 2 bytes from eeprom on read */
Christian Rieschceb02c92012-07-19 00:23:06 +0000484 for (i = first_word; i <= last_word; i++) {
485 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
486 &(eeprom_buff[i - first_word])) < 0) {
487 kfree(eeprom_buff);
488 return -EIO;
489 }
David Brownell2e55cc72005-08-31 09:53:10 -0700490 }
Christian Rieschceb02c92012-07-19 00:23:06 +0000491
492 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
493 kfree(eeprom_buff);
David Brownell2e55cc72005-08-31 09:53:10 -0700494 return 0;
495}
496
Christian Rieschcb7b24c2012-07-19 00:23:07 +0000497int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
498 u8 *data)
499{
500 struct usbnet *dev = netdev_priv(net);
501 u16 *eeprom_buff;
502 int first_word, last_word;
503 int i;
504 int ret;
505
506 netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
507 eeprom->len, eeprom->offset, eeprom->magic);
508
509 if (eeprom->len == 0)
510 return -EINVAL;
511
512 if (eeprom->magic != AX_EEPROM_MAGIC)
513 return -EINVAL;
514
515 first_word = eeprom->offset >> 1;
516 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
517
518 eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
519 GFP_KERNEL);
520 if (!eeprom_buff)
521 return -ENOMEM;
522
523 /* align data to 16 bit boundaries, read the missing data from
524 the EEPROM */
525 if (eeprom->offset & 1) {
526 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
527 &(eeprom_buff[0]));
528 if (ret < 0) {
529 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
530 goto free;
531 }
532 }
533
534 if ((eeprom->offset + eeprom->len) & 1) {
535 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
536 &(eeprom_buff[last_word - first_word]));
537 if (ret < 0) {
538 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
539 goto free;
540 }
541 }
542
543 memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
544
545 /* write data to EEPROM */
546 ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL);
547 if (ret < 0) {
548 netdev_err(net, "Failed to enable EEPROM write\n");
549 goto free;
550 }
551 msleep(20);
552
553 for (i = first_word; i <= last_word; i++) {
554 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
555 i, eeprom_buff[i - first_word]);
556 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
557 eeprom_buff[i - first_word], 0, NULL);
558 if (ret < 0) {
559 netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
560 i);
561 goto free;
562 }
563 msleep(20);
564 }
565
566 ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL);
567 if (ret < 0) {
568 netdev_err(net, "Failed to disable EEPROM write\n");
569 goto free;
570 }
571
572 ret = 0;
573free:
574 kfree(eeprom_buff);
575 return ret;
576}
577
Christian Riesch607740b2012-07-13 05:26:30 +0000578void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
David Brownell2e55cc72005-08-31 09:53:10 -0700579{
580 /* Inherit standard device info */
581 usbnet_get_drvinfo(net, info);
Jiri Pirko7826d432013-01-06 00:44:26 +0000582 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
583 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
Christian Rieschceb02c92012-07-19 00:23:06 +0000584 info->eedump_len = AX_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700585}
586
Christian Riesch607740b2012-07-13 05:26:30 +0000587int asix_set_mac_address(struct net_device *net, void *p)
Jussi Kivilinna7f29a3b2010-03-09 12:24:38 +0000588{
589 struct usbnet *dev = netdev_priv(net);
590 struct asix_data *data = (struct asix_data *)&dev->data;
591 struct sockaddr *addr = p;
592
593 if (netif_running(net))
594 return -EBUSY;
595 if (!is_valid_ether_addr(addr->sa_data))
596 return -EADDRNOTAVAIL;
597
598 memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
599
600 /* We use the 20 byte dev->data
601 * for our 6 byte mac buffer
602 * to avoid allocating memory that
603 * is tricky to free later */
604 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
605 asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
606 data->mac_addr);
607
608 return 0;
609}