blob: 774d9ce2dafcefa699311c8f33532cebd3187dc8 [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{
Al Viro51bf2972007-12-22 17:42:36 +000028 void *buf;
29 int err = -ENOMEM;
30
Joe Perches60b86752010-02-17 10:30:23 +000031 netdev_dbg(dev->net, "asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
32 cmd, value, index, size);
Al Viro51bf2972007-12-22 17:42:36 +000033
34 buf = kmalloc(size, GFP_KERNEL);
35 if (!buf)
36 goto out;
37
38 err = usb_control_msg(
David Brownell2e55cc72005-08-31 09:53:10 -070039 dev->udev,
40 usb_rcvctrlpipe(dev->udev, 0),
41 cmd,
42 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
43 value,
44 index,
Al Viro51bf2972007-12-22 17:42:36 +000045 buf,
David Brownell2e55cc72005-08-31 09:53:10 -070046 size,
47 USB_CTRL_GET_TIMEOUT);
Russ Dill94d43362008-01-09 21:32:07 -070048 if (err == size)
Al Viro51bf2972007-12-22 17:42:36 +000049 memcpy(data, buf, size);
Russ Dill94d43362008-01-09 21:32:07 -070050 else if (err >= 0)
51 err = -EINVAL;
Al Viro51bf2972007-12-22 17:42:36 +000052 kfree(buf);
53
54out:
55 return err;
David Brownell2e55cc72005-08-31 09:53:10 -070056}
57
Christian Riesch607740b2012-07-13 05:26:30 +000058int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
59 u16 size, void *data)
David Brownell2e55cc72005-08-31 09:53:10 -070060{
Al Viro51bf2972007-12-22 17:42:36 +000061 void *buf = NULL;
62 int err = -ENOMEM;
63
Joe Perches60b86752010-02-17 10:30:23 +000064 netdev_dbg(dev->net, "asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
65 cmd, value, index, size);
Al Viro51bf2972007-12-22 17:42:36 +000066
67 if (data) {
Julia Lawall99bf2362010-05-15 11:20:45 +000068 buf = kmemdup(data, size, GFP_KERNEL);
Al Viro51bf2972007-12-22 17:42:36 +000069 if (!buf)
70 goto out;
Al Viro51bf2972007-12-22 17:42:36 +000071 }
72
73 err = usb_control_msg(
David Brownell2e55cc72005-08-31 09:53:10 -070074 dev->udev,
75 usb_sndctrlpipe(dev->udev, 0),
76 cmd,
77 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
78 value,
79 index,
Al Viro51bf2972007-12-22 17:42:36 +000080 buf,
David Brownell2e55cc72005-08-31 09:53:10 -070081 size,
82 USB_CTRL_SET_TIMEOUT);
Al Viro51bf2972007-12-22 17:42:36 +000083 kfree(buf);
84
85out:
86 return err;
David Brownell2e55cc72005-08-31 09:53:10 -070087}
88
David Howells7d12e782006-10-05 14:55:46 +010089static void asix_async_cmd_callback(struct urb *urb)
David Brownell2e55cc72005-08-31 09:53:10 -070090{
91 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -080092 int status = urb->status;
David Brownell2e55cc72005-08-31 09:53:10 -070093
Oliver Neukumc94cb312008-12-18 23:00:59 -080094 if (status < 0)
David Hollis48b1be62006-03-28 20:15:42 -050095 printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
Oliver Neukumc94cb312008-12-18 23:00:59 -080096 status);
David Brownell2e55cc72005-08-31 09:53:10 -070097
98 kfree(req);
99 usb_free_urb(urb);
100}
101
Christian Riesch607740b2012-07-13 05:26:30 +0000102void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
103 u16 size, void *data)
David Hollis48b1be62006-03-28 20:15:42 -0500104{
David Hollis933a27d2006-07-29 10:12:50 -0400105 struct usb_ctrlrequest *req;
106 int status;
107 struct urb *urb;
David Hollis48b1be62006-03-28 20:15:42 -0500108
Joe Perches60b86752010-02-17 10:30:23 +0000109 netdev_dbg(dev->net, "asix_write_cmd_async() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
110 cmd, value, index, size);
Grant Grundler83e1b912011-10-04 09:55:18 +0000111
112 urb = usb_alloc_urb(0, GFP_ATOMIC);
113 if (!urb) {
Joe Perches60b86752010-02-17 10:30:23 +0000114 netdev_err(dev->net, "Error allocating URB in write_cmd_async!\n");
David Hollis933a27d2006-07-29 10:12:50 -0400115 return;
David Hollis48b1be62006-03-28 20:15:42 -0500116 }
David Hollis933a27d2006-07-29 10:12:50 -0400117
Grant Grundler83e1b912011-10-04 09:55:18 +0000118 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
119 if (!req) {
Joe Perches60b86752010-02-17 10:30:23 +0000120 netdev_err(dev->net, "Failed to allocate memory for control request\n");
David Hollis933a27d2006-07-29 10:12:50 -0400121 usb_free_urb(urb);
122 return;
123 }
124
125 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
126 req->bRequest = cmd;
Oliver Neukum9aa742e2006-11-23 12:45:31 +0100127 req->wValue = cpu_to_le16(value);
128 req->wIndex = cpu_to_le16(index);
129 req->wLength = cpu_to_le16(size);
David Hollis933a27d2006-07-29 10:12:50 -0400130
131 usb_fill_control_urb(urb, dev->udev,
132 usb_sndctrlpipe(dev->udev, 0),
133 (void *)req, data, size,
134 asix_async_cmd_callback, req);
135
Grant Grundler83e1b912011-10-04 09:55:18 +0000136 status = usb_submit_urb(urb, GFP_ATOMIC);
137 if (status < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000138 netdev_err(dev->net, "Error submitting the control message: status=%d\n",
139 status);
David Hollis933a27d2006-07-29 10:12:50 -0400140 kfree(req);
141 usb_free_urb(urb);
142 }
David Hollis48b1be62006-03-28 20:15:42 -0500143}
144
Christian Riesch607740b2012-07-13 05:26:30 +0000145int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
David Hollis48b1be62006-03-28 20:15:42 -0500146{
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000147 int offset = 0;
David Hollis48b1be62006-03-28 20:15:42 -0500148
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000149 while (offset + sizeof(u32) < skb->len) {
150 struct sk_buff *ax_skb;
151 u16 size;
152 u32 header = get_unaligned_le32(skb->data + offset);
David Hollis48b1be62006-03-28 20:15:42 -0500153
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000154 offset += sizeof(u32);
Marek Vasutbc466e62011-07-26 16:44:46 +0000155
David Hollis933a27d2006-07-29 10:12:50 -0400156 /* get the packet length */
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000157 size = (u16) (header & 0x7ff);
158 if (size != ((~header >> 16) & 0x07ff)) {
159 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
160 return 0;
Neil Jones3f78d1f2010-05-17 17:18:28 -0700161 }
162
Eric Dumazet9dae3102012-05-28 22:31:41 +0000163 if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000164 (size + offset > skb->len)) {
Joe Perches60b86752010-02-17 10:30:23 +0000165 netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
166 size);
David Hollis933a27d2006-07-29 10:12:50 -0400167 return 0;
168 }
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000169 ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
170 if (!ax_skb)
David Hollis933a27d2006-07-29 10:12:50 -0400171 return 0;
David Hollis933a27d2006-07-29 10:12:50 -0400172
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000173 skb_put(ax_skb, size);
174 memcpy(ax_skb->data, skb->data + offset, size);
175 usbnet_skb_return(dev, ax_skb);
David Hollis933a27d2006-07-29 10:12:50 -0400176
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000177 offset += (size + 1) & 0xfffe;
David Hollis933a27d2006-07-29 10:12:50 -0400178 }
179
Eric Dumazeta9e0aca2012-03-14 20:18:32 +0000180 if (skb->len != offset) {
Joe Perches60b86752010-02-17 10:30:23 +0000181 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
182 skb->len);
David Hollis933a27d2006-07-29 10:12:50 -0400183 return 0;
184 }
185 return 1;
David Hollis48b1be62006-03-28 20:15:42 -0500186}
187
Christian Riesch607740b2012-07-13 05:26:30 +0000188struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
189 gfp_t flags)
David Hollis48b1be62006-03-28 20:15:42 -0500190{
David Hollis933a27d2006-07-29 10:12:50 -0400191 int padlen;
192 int headroom = skb_headroom(skb);
193 int tailroom = skb_tailroom(skb);
194 u32 packet_len;
195 u32 padbytes = 0xffff0000;
David Hollis48b1be62006-03-28 20:15:42 -0500196
Ingo van Lil2a580942012-04-23 22:05:38 +0000197 padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
David Hollis48b1be62006-03-28 20:15:42 -0500198
Eric Dumazet95162d62012-07-05 04:31:01 +0000199 /* We need to push 4 bytes in front of frame (packet_len)
200 * and maybe add 4 bytes after the end (if padlen is 4)
201 *
202 * Avoid skb_copy_expand() expensive call, using following rules :
203 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
204 * is false (and if we have 4 bytes of headroom)
205 * - We are allowed to put 4 bytes at tail if skb_cloned()
206 * is false (and if we have 4 bytes of tailroom)
207 *
208 * TCP packets for example are cloned, but skb_header_release()
209 * was called in tcp stack, allowing us to use headroom for our needs.
210 */
211 if (!skb_header_cloned(skb) &&
212 !(padlen && skb_cloned(skb)) &&
213 headroom + tailroom >= 4 + padlen) {
214 /* following should not happen, but better be safe */
215 if (headroom < 4 ||
216 tailroom < padlen) {
David Hollis933a27d2006-07-29 10:12:50 -0400217 skb->data = memmove(skb->head + 4, skb->data, skb->len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700218 skb_set_tail_pointer(skb, skb->len);
David Hollis933a27d2006-07-29 10:12:50 -0400219 }
220 } else {
221 struct sk_buff *skb2;
Eric Dumazet95162d62012-07-05 04:31:01 +0000222
David Hollis933a27d2006-07-29 10:12:50 -0400223 skb2 = skb_copy_expand(skb, 4, padlen, flags);
224 dev_kfree_skb_any(skb);
225 skb = skb2;
226 if (!skb)
227 return NULL;
228 }
229
Eric Dumazet95162d62012-07-05 04:31:01 +0000230 packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
David Hollis933a27d2006-07-29 10:12:50 -0400231 skb_push(skb, 4);
David Hollis57e4f042007-02-05 12:03:03 -0500232 cpu_to_le32s(&packet_len);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300233 skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
David Hollis933a27d2006-07-29 10:12:50 -0400234
Ingo van Lil2a580942012-04-23 22:05:38 +0000235 if (padlen) {
David Hollis57e4f042007-02-05 12:03:03 -0500236 cpu_to_le32s(&padbytes);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700237 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
David Hollis933a27d2006-07-29 10:12:50 -0400238 skb_put(skb, sizeof(padbytes));
239 }
240 return skb;
David Hollis48b1be62006-03-28 20:15:42 -0500241}
242
Christian Riesch607740b2012-07-13 05:26:30 +0000243int asix_set_sw_mii(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -0700244{
David Hollis933a27d2006-07-29 10:12:50 -0400245 int ret;
246 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
247 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000248 netdev_err(dev->net, "Failed to enable software MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400249 return ret;
David Brownell2e55cc72005-08-31 09:53:10 -0700250}
251
Christian Riesch607740b2012-07-13 05:26:30 +0000252int asix_set_hw_mii(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400253{
254 int ret;
255 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
256 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000257 netdev_err(dev->net, "Failed to enable hardware MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400258 return ret;
259}
260
Christian Riesch16626b02012-07-13 05:26:31 +0000261int asix_read_phy_addr(struct usbnet *dev, int internal)
David Hollis933a27d2006-07-29 10:12:50 -0400262{
Christian Riesch16626b02012-07-13 05:26:31 +0000263 int offset = (internal ? 1 : 0);
Al Viro51bf2972007-12-22 17:42:36 +0000264 u8 buf[2];
265 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
David Hollis933a27d2006-07-29 10:12:50 -0400266
Joe Perches60b86752010-02-17 10:30:23 +0000267 netdev_dbg(dev->net, "asix_get_phy_addr()\n");
David Hollis933a27d2006-07-29 10:12:50 -0400268
Al Viro51bf2972007-12-22 17:42:36 +0000269 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000270 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000271 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400272 }
Joe Perches60b86752010-02-17 10:30:23 +0000273 netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
274 *((__le16 *)buf));
Christian Riesch16626b02012-07-13 05:26:31 +0000275 ret = buf[offset];
Al Viro51bf2972007-12-22 17:42:36 +0000276
277out:
David Hollis933a27d2006-07-29 10:12:50 -0400278 return ret;
279}
280
Christian Riesch16626b02012-07-13 05:26:31 +0000281int asix_get_phy_addr(struct usbnet *dev)
282{
283 /* return the address of the internal phy */
284 return asix_read_phy_addr(dev, 1);
285}
286
287
Christian Riesch607740b2012-07-13 05:26:30 +0000288int asix_sw_reset(struct usbnet *dev, u8 flags)
David Hollis933a27d2006-07-29 10:12:50 -0400289{
290 int ret;
291
292 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
293 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000294 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
David Hollis933a27d2006-07-29 10:12:50 -0400295
296 return ret;
297}
298
Christian Riesch607740b2012-07-13 05:26:30 +0000299u16 asix_read_rx_ctl(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400300{
Al Viro51bf2972007-12-22 17:42:36 +0000301 __le16 v;
302 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400303
Al Viro51bf2972007-12-22 17:42:36 +0000304 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000305 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000306 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400307 }
Al Viro51bf2972007-12-22 17:42:36 +0000308 ret = le16_to_cpu(v);
309out:
David Hollis933a27d2006-07-29 10:12:50 -0400310 return ret;
311}
312
Christian Riesch607740b2012-07-13 05:26:30 +0000313int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
David Hollis933a27d2006-07-29 10:12:50 -0400314{
315 int ret;
316
Joe Perches60b86752010-02-17 10:30:23 +0000317 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400318 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
319 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000320 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
321 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400322
323 return ret;
324}
325
Christian Riesch607740b2012-07-13 05:26:30 +0000326u16 asix_read_medium_status(struct usbnet *dev)
David Hollis933a27d2006-07-29 10:12:50 -0400327{
Al Viro51bf2972007-12-22 17:42:36 +0000328 __le16 v;
329 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400330
Al Viro51bf2972007-12-22 17:42:36 +0000331 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000332 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
333 ret);
Grant Grundler83e1b912011-10-04 09:55:18 +0000334 return ret; /* TODO: callers not checking for error ret */
David Hollis933a27d2006-07-29 10:12:50 -0400335 }
Grant Grundler83e1b912011-10-04 09:55:18 +0000336
337 return le16_to_cpu(v);
338
David Hollis933a27d2006-07-29 10:12:50 -0400339}
340
Christian Riesch607740b2012-07-13 05:26:30 +0000341int asix_write_medium_mode(struct usbnet *dev, u16 mode)
David Hollis933a27d2006-07-29 10:12:50 -0400342{
343 int ret;
344
Joe Perches60b86752010-02-17 10:30:23 +0000345 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400346 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
347 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000348 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
349 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400350
351 return ret;
352}
353
Christian Riesch607740b2012-07-13 05:26:30 +0000354int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
David Hollis933a27d2006-07-29 10:12:50 -0400355{
356 int ret;
357
Joe Perches60b86752010-02-17 10:30:23 +0000358 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
David Hollis933a27d2006-07-29 10:12:50 -0400359 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
360 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000361 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
362 value, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400363
364 if (sleep)
365 msleep(sleep);
366
367 return ret;
368}
369
370/*
371 * AX88772 & AX88178 have a 16-bit RX_CTL value
372 */
Christian Riesch607740b2012-07-13 05:26:30 +0000373void asix_set_multicast(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700374{
375 struct usbnet *dev = netdev_priv(net);
David Hollis48b1be62006-03-28 20:15:42 -0500376 struct asix_data *data = (struct asix_data *)&dev->data;
David Hollis933a27d2006-07-29 10:12:50 -0400377 u16 rx_ctl = AX_DEFAULT_RX_CTL;
David Brownell2e55cc72005-08-31 09:53:10 -0700378
379 if (net->flags & IFF_PROMISC) {
David Hollis933a27d2006-07-29 10:12:50 -0400380 rx_ctl |= AX_RX_CTL_PRO;
Joe Perches8e95a202009-12-03 07:58:21 +0000381 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000382 netdev_mc_count(net) > AX_MAX_MCAST) {
David Hollis933a27d2006-07-29 10:12:50 -0400383 rx_ctl |= AX_RX_CTL_AMALL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000384 } else if (netdev_mc_empty(net)) {
David Brownell2e55cc72005-08-31 09:53:10 -0700385 /* just broadcast and directed */
386 } else {
387 /* We use the 20 byte dev->data
388 * for our 8 byte filter buffer
389 * to avoid allocating memory that
390 * is tricky to free later */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000391 struct netdev_hw_addr *ha;
David Brownell2e55cc72005-08-31 09:53:10 -0700392 u32 crc_bits;
David Brownell2e55cc72005-08-31 09:53:10 -0700393
394 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
395
396 /* Build the multicast hash filter. */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000397 netdev_for_each_mc_addr(ha, net) {
398 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
David Brownell2e55cc72005-08-31 09:53:10 -0700399 data->multi_filter[crc_bits >> 3] |=
400 1 << (crc_bits & 7);
David Brownell2e55cc72005-08-31 09:53:10 -0700401 }
402
David Hollis48b1be62006-03-28 20:15:42 -0500403 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
David Brownell2e55cc72005-08-31 09:53:10 -0700404 AX_MCAST_FILTER_SIZE, data->multi_filter);
405
David Hollis933a27d2006-07-29 10:12:50 -0400406 rx_ctl |= AX_RX_CTL_AM;
David Brownell2e55cc72005-08-31 09:53:10 -0700407 }
408
David Hollis48b1be62006-03-28 20:15:42 -0500409 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700410}
411
Christian Riesch607740b2012-07-13 05:26:30 +0000412int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
David Brownell2e55cc72005-08-31 09:53:10 -0700413{
414 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000415 __le16 res;
David Brownell2e55cc72005-08-31 09:53:10 -0700416
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200417 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500418 asix_set_sw_mii(dev);
419 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
Al Viro51bf2972007-12-22 17:42:36 +0000420 (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500421 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200422 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700423
Joe Perches60b86752010-02-17 10:30:23 +0000424 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
425 phy_id, loc, le16_to_cpu(res));
David Brownell2e55cc72005-08-31 09:53:10 -0700426
Al Viro51bf2972007-12-22 17:42:36 +0000427 return le16_to_cpu(res);
David Brownell2e55cc72005-08-31 09:53:10 -0700428}
429
Christian Riesch607740b2012-07-13 05:26:30 +0000430void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
David Brownell2e55cc72005-08-31 09:53:10 -0700431{
432 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000433 __le16 res = cpu_to_le16(val);
David Brownell2e55cc72005-08-31 09:53:10 -0700434
Joe Perches60b86752010-02-17 10:30:23 +0000435 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
436 phy_id, loc, val);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200437 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500438 asix_set_sw_mii(dev);
Al Viro51bf2972007-12-22 17:42:36 +0000439 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500440 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200441 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700442}
443
Christian Riesch607740b2012-07-13 05:26:30 +0000444void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700445{
446 struct usbnet *dev = netdev_priv(net);
447 u8 opt;
448
David Hollis48b1be62006-03-28 20:15:42 -0500449 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700450 wolinfo->supported = 0;
451 wolinfo->wolopts = 0;
452 return;
453 }
454 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
455 wolinfo->wolopts = 0;
allanf87ce5b2011-12-22 20:38:51 +0000456 if (opt & AX_MONITOR_LINK)
457 wolinfo->wolopts |= WAKE_PHY;
458 if (opt & AX_MONITOR_MAGIC)
459 wolinfo->wolopts |= WAKE_MAGIC;
David Brownell2e55cc72005-08-31 09:53:10 -0700460}
461
Christian Riesch607740b2012-07-13 05:26:30 +0000462int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700463{
464 struct usbnet *dev = netdev_priv(net);
465 u8 opt = 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700466
467 if (wolinfo->wolopts & WAKE_PHY)
468 opt |= AX_MONITOR_LINK;
469 if (wolinfo->wolopts & WAKE_MAGIC)
470 opt |= AX_MONITOR_MAGIC;
David Brownell2e55cc72005-08-31 09:53:10 -0700471
David Hollis48b1be62006-03-28 20:15:42 -0500472 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
Al Viro51bf2972007-12-22 17:42:36 +0000473 opt, 0, 0, NULL) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700474 return -EINVAL;
475
476 return 0;
477}
478
Christian Riesch607740b2012-07-13 05:26:30 +0000479int asix_get_eeprom_len(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700480{
Christian Rieschceb02c92012-07-19 00:23:06 +0000481 return AX_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700482}
483
Christian Riesch607740b2012-07-13 05:26:30 +0000484int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
485 u8 *data)
David Brownell2e55cc72005-08-31 09:53:10 -0700486{
487 struct usbnet *dev = netdev_priv(net);
Christian Rieschceb02c92012-07-19 00:23:06 +0000488 u16 *eeprom_buff;
489 int first_word, last_word;
David Brownell2e55cc72005-08-31 09:53:10 -0700490 int i;
491
Christian Rieschceb02c92012-07-19 00:23:06 +0000492 if (eeprom->len == 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700493 return -EINVAL;
494
495 eeprom->magic = AX_EEPROM_MAGIC;
496
Christian Rieschceb02c92012-07-19 00:23:06 +0000497 first_word = eeprom->offset >> 1;
498 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
499
500 eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
501 GFP_KERNEL);
502 if (!eeprom_buff)
503 return -ENOMEM;
504
David Brownell2e55cc72005-08-31 09:53:10 -0700505 /* ax8817x returns 2 bytes from eeprom on read */
Christian Rieschceb02c92012-07-19 00:23:06 +0000506 for (i = first_word; i <= last_word; i++) {
507 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
508 &(eeprom_buff[i - first_word])) < 0) {
509 kfree(eeprom_buff);
510 return -EIO;
511 }
David Brownell2e55cc72005-08-31 09:53:10 -0700512 }
Christian Rieschceb02c92012-07-19 00:23:06 +0000513
514 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
515 kfree(eeprom_buff);
David Brownell2e55cc72005-08-31 09:53:10 -0700516 return 0;
517}
518
Christian Rieschcb7b24c2012-07-19 00:23:07 +0000519int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
520 u8 *data)
521{
522 struct usbnet *dev = netdev_priv(net);
523 u16 *eeprom_buff;
524 int first_word, last_word;
525 int i;
526 int ret;
527
528 netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
529 eeprom->len, eeprom->offset, eeprom->magic);
530
531 if (eeprom->len == 0)
532 return -EINVAL;
533
534 if (eeprom->magic != AX_EEPROM_MAGIC)
535 return -EINVAL;
536
537 first_word = eeprom->offset >> 1;
538 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
539
540 eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
541 GFP_KERNEL);
542 if (!eeprom_buff)
543 return -ENOMEM;
544
545 /* align data to 16 bit boundaries, read the missing data from
546 the EEPROM */
547 if (eeprom->offset & 1) {
548 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
549 &(eeprom_buff[0]));
550 if (ret < 0) {
551 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
552 goto free;
553 }
554 }
555
556 if ((eeprom->offset + eeprom->len) & 1) {
557 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
558 &(eeprom_buff[last_word - first_word]));
559 if (ret < 0) {
560 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
561 goto free;
562 }
563 }
564
565 memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
566
567 /* write data to EEPROM */
568 ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL);
569 if (ret < 0) {
570 netdev_err(net, "Failed to enable EEPROM write\n");
571 goto free;
572 }
573 msleep(20);
574
575 for (i = first_word; i <= last_word; i++) {
576 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
577 i, eeprom_buff[i - first_word]);
578 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
579 eeprom_buff[i - first_word], 0, NULL);
580 if (ret < 0) {
581 netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
582 i);
583 goto free;
584 }
585 msleep(20);
586 }
587
588 ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL);
589 if (ret < 0) {
590 netdev_err(net, "Failed to disable EEPROM write\n");
591 goto free;
592 }
593
594 ret = 0;
595free:
596 kfree(eeprom_buff);
597 return ret;
598}
599
Christian Riesch607740b2012-07-13 05:26:30 +0000600void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
David Brownell2e55cc72005-08-31 09:53:10 -0700601{
602 /* Inherit standard device info */
603 usbnet_get_drvinfo(net, info);
Grant Grundler83e1b912011-10-04 09:55:18 +0000604 strncpy (info->driver, DRIVER_NAME, sizeof info->driver);
David Hollis933a27d2006-07-29 10:12:50 -0400605 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
Christian Rieschceb02c92012-07-19 00:23:06 +0000606 info->eedump_len = AX_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700607}
608
Christian Riesch607740b2012-07-13 05:26:30 +0000609int asix_set_mac_address(struct net_device *net, void *p)
Jussi Kivilinna7f29a3b2010-03-09 12:24:38 +0000610{
611 struct usbnet *dev = netdev_priv(net);
612 struct asix_data *data = (struct asix_data *)&dev->data;
613 struct sockaddr *addr = p;
614
615 if (netif_running(net))
616 return -EBUSY;
617 if (!is_valid_ether_addr(addr->sa_data))
618 return -EADDRNOTAVAIL;
619
620 memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
621
622 /* We use the 20 byte dev->data
623 * for our 6 byte mac buffer
624 * to avoid allocating memory that
625 * is tricky to free later */
626 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
627 asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
628 data->mac_addr);
629
630 return 0;
631}