xref: /openbmc/linux/drivers/net/usb/asix_common.c (revision 8b5b6f5413e97c3e8bafcdd67553d508f4f698cd)
1b4cdae20SChristian Riesch /*
2b4cdae20SChristian Riesch  * ASIX AX8817X based USB 2.0 Ethernet Devices
3b4cdae20SChristian Riesch  * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
4b4cdae20SChristian Riesch  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
5b4cdae20SChristian Riesch  * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
6b4cdae20SChristian Riesch  * Copyright (c) 2002-2003 TiVo Inc.
7b4cdae20SChristian Riesch  *
8b4cdae20SChristian Riesch  * This program is free software; you can redistribute it and/or modify
9b4cdae20SChristian Riesch  * it under the terms of the GNU General Public License as published by
10b4cdae20SChristian Riesch  * the Free Software Foundation; either version 2 of the License, or
11b4cdae20SChristian Riesch  * (at your option) any later version.
12b4cdae20SChristian Riesch  *
13b4cdae20SChristian Riesch  * This program is distributed in the hope that it will be useful,
14b4cdae20SChristian Riesch  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15b4cdae20SChristian Riesch  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16b4cdae20SChristian Riesch  * GNU General Public License for more details.
17b4cdae20SChristian Riesch  *
18b4cdae20SChristian Riesch  * You should have received a copy of the GNU General Public License
19b4cdae20SChristian Riesch  * along with this program; if not, write to the Free Software
20b4cdae20SChristian Riesch  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21b4cdae20SChristian Riesch  */
22b4cdae20SChristian Riesch 
23607740bcSChristian Riesch #include "asix.h"
24b4cdae20SChristian Riesch 
25607740bcSChristian Riesch int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
26b4cdae20SChristian Riesch 		  u16 size, void *data)
27b4cdae20SChristian Riesch {
280bc69efbSMing Lei 	int ret;
290bc69efbSMing Lei 	ret = usbnet_read_cmd(dev, cmd,
30b4cdae20SChristian Riesch 			       USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
310bc69efbSMing Lei 			       value, index, data, size);
32b4cdae20SChristian Riesch 
330bc69efbSMing Lei 	if (ret != size && ret >= 0)
340bc69efbSMing Lei 		return -EINVAL;
350bc69efbSMing Lei 	return ret;
36b4cdae20SChristian Riesch }
37b4cdae20SChristian Riesch 
38607740bcSChristian Riesch int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
39b4cdae20SChristian Riesch 		   u16 size, void *data)
40b4cdae20SChristian Riesch {
410bc69efbSMing Lei 	return usbnet_write_cmd(dev, cmd,
42b4cdae20SChristian Riesch 				USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
430bc69efbSMing Lei 				value, index, data, size);
44b4cdae20SChristian Riesch }
45b4cdae20SChristian Riesch 
46607740bcSChristian Riesch void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
47b4cdae20SChristian Riesch 			  u16 size, void *data)
48b4cdae20SChristian Riesch {
490bc69efbSMing Lei 	usbnet_write_cmd_async(dev, cmd,
500bc69efbSMing Lei 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
510bc69efbSMing Lei 			       value, index, data, size);
52b4cdae20SChristian Riesch }
53b4cdae20SChristian Riesch 
54*8b5b6f54SLucas Stach int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
55*8b5b6f54SLucas Stach 			   struct asix_rx_fixup_info *rx)
56b4cdae20SChristian Riesch {
57b4cdae20SChristian Riesch 	int offset = 0;
58b4cdae20SChristian Riesch 
59*8b5b6f54SLucas Stach 	while (offset + sizeof(u16) <= skb->len) {
60*8b5b6f54SLucas Stach 		u16 remaining = 0;
61*8b5b6f54SLucas Stach 		unsigned char *data;
62b4cdae20SChristian Riesch 
63*8b5b6f54SLucas Stach 		if (!rx->size) {
64*8b5b6f54SLucas Stach 			if ((skb->len - offset == sizeof(u16)) ||
65*8b5b6f54SLucas Stach 			    rx->split_head) {
66*8b5b6f54SLucas Stach 				if(!rx->split_head) {
67*8b5b6f54SLucas Stach 					rx->header = get_unaligned_le16(
68*8b5b6f54SLucas Stach 							skb->data + offset);
69*8b5b6f54SLucas Stach 					rx->split_head = true;
70*8b5b6f54SLucas Stach 					offset += sizeof(u16);
71*8b5b6f54SLucas Stach 					break;
72*8b5b6f54SLucas Stach 				} else {
73*8b5b6f54SLucas Stach 					rx->header |= (get_unaligned_le16(
74*8b5b6f54SLucas Stach 							skb->data + offset)
75*8b5b6f54SLucas Stach 							<< 16);
76*8b5b6f54SLucas Stach 					rx->split_head = false;
77*8b5b6f54SLucas Stach 					offset += sizeof(u16);
78*8b5b6f54SLucas Stach 				}
79*8b5b6f54SLucas Stach 			} else {
80*8b5b6f54SLucas Stach 				rx->header = get_unaligned_le32(skb->data +
81*8b5b6f54SLucas Stach 								offset);
82b4cdae20SChristian Riesch 				offset += sizeof(u32);
83*8b5b6f54SLucas Stach 			}
84b4cdae20SChristian Riesch 
85b4cdae20SChristian Riesch 			/* get the packet length */
86*8b5b6f54SLucas Stach 			rx->size = (u16) (rx->header & 0x7ff);
87*8b5b6f54SLucas Stach 			if (rx->size != ((~rx->header >> 16) & 0x7ff)) {
88*8b5b6f54SLucas Stach 				netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
89*8b5b6f54SLucas Stach 					   rx->header, offset);
90*8b5b6f54SLucas Stach 				rx->size = 0;
91*8b5b6f54SLucas Stach 				return 0;
92*8b5b6f54SLucas Stach 			}
93*8b5b6f54SLucas Stach 			rx->ax_skb = netdev_alloc_skb_ip_align(dev->net,
94*8b5b6f54SLucas Stach 							       rx->size);
95*8b5b6f54SLucas Stach 			if (!rx->ax_skb)
96b4cdae20SChristian Riesch 				return 0;
97b4cdae20SChristian Riesch 		}
98b4cdae20SChristian Riesch 
99*8b5b6f54SLucas Stach 		if (rx->size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
100b4cdae20SChristian Riesch 			netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
101*8b5b6f54SLucas Stach 				   rx->size);
102*8b5b6f54SLucas Stach 			kfree_skb(rx->ax_skb);
103b4cdae20SChristian Riesch 			return 0;
104b4cdae20SChristian Riesch 		}
105b4cdae20SChristian Riesch 
106*8b5b6f54SLucas Stach 		if (rx->size > skb->len - offset) {
107*8b5b6f54SLucas Stach 			remaining = rx->size - (skb->len - offset);
108*8b5b6f54SLucas Stach 			rx->size = skb->len - offset;
109*8b5b6f54SLucas Stach 		}
110b4cdae20SChristian Riesch 
111*8b5b6f54SLucas Stach 		data = skb_put(rx->ax_skb, rx->size);
112*8b5b6f54SLucas Stach 		memcpy(data, skb->data + offset, rx->size);
113*8b5b6f54SLucas Stach 		if (!remaining)
114*8b5b6f54SLucas Stach 			usbnet_skb_return(dev, rx->ax_skb);
115*8b5b6f54SLucas Stach 
116*8b5b6f54SLucas Stach 		offset += (rx->size + 1) & 0xfffe;
117*8b5b6f54SLucas Stach 		rx->size = remaining;
118b4cdae20SChristian Riesch 	}
119b4cdae20SChristian Riesch 
120b4cdae20SChristian Riesch 	if (skb->len != offset) {
121*8b5b6f54SLucas Stach 		netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
122*8b5b6f54SLucas Stach 			   skb->len, offset);
123b4cdae20SChristian Riesch 		return 0;
124b4cdae20SChristian Riesch 	}
125*8b5b6f54SLucas Stach 
126b4cdae20SChristian Riesch 	return 1;
127b4cdae20SChristian Riesch }
128b4cdae20SChristian Riesch 
129*8b5b6f54SLucas Stach int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
130*8b5b6f54SLucas Stach {
131*8b5b6f54SLucas Stach 	struct asix_common_private *dp = dev->driver_priv;
132*8b5b6f54SLucas Stach 	struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
133*8b5b6f54SLucas Stach 
134*8b5b6f54SLucas Stach 	return asix_rx_fixup_internal(dev, skb, rx);
135*8b5b6f54SLucas Stach }
136*8b5b6f54SLucas Stach 
137607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
138b4cdae20SChristian Riesch 			      gfp_t flags)
139b4cdae20SChristian Riesch {
140b4cdae20SChristian Riesch 	int padlen;
141b4cdae20SChristian Riesch 	int headroom = skb_headroom(skb);
142b4cdae20SChristian Riesch 	int tailroom = skb_tailroom(skb);
143b4cdae20SChristian Riesch 	u32 packet_len;
144b4cdae20SChristian Riesch 	u32 padbytes = 0xffff0000;
145b4cdae20SChristian Riesch 
146b4cdae20SChristian Riesch 	padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
147b4cdae20SChristian Riesch 
148b4cdae20SChristian Riesch 	/* We need to push 4 bytes in front of frame (packet_len)
149b4cdae20SChristian Riesch 	 * and maybe add 4 bytes after the end (if padlen is 4)
150b4cdae20SChristian Riesch 	 *
151b4cdae20SChristian Riesch 	 * Avoid skb_copy_expand() expensive call, using following rules :
152b4cdae20SChristian Riesch 	 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
153b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of headroom)
154b4cdae20SChristian Riesch 	 * - We are allowed to put 4 bytes at tail if skb_cloned()
155b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of tailroom)
156b4cdae20SChristian Riesch 	 *
157b4cdae20SChristian Riesch 	 * TCP packets for example are cloned, but skb_header_release()
158b4cdae20SChristian Riesch 	 * was called in tcp stack, allowing us to use headroom for our needs.
159b4cdae20SChristian Riesch 	 */
160b4cdae20SChristian Riesch 	if (!skb_header_cloned(skb) &&
161b4cdae20SChristian Riesch 	    !(padlen && skb_cloned(skb)) &&
162b4cdae20SChristian Riesch 	    headroom + tailroom >= 4 + padlen) {
163b4cdae20SChristian Riesch 		/* following should not happen, but better be safe */
164b4cdae20SChristian Riesch 		if (headroom < 4 ||
165b4cdae20SChristian Riesch 		    tailroom < padlen) {
166b4cdae20SChristian Riesch 			skb->data = memmove(skb->head + 4, skb->data, skb->len);
167b4cdae20SChristian Riesch 			skb_set_tail_pointer(skb, skb->len);
168b4cdae20SChristian Riesch 		}
169b4cdae20SChristian Riesch 	} else {
170b4cdae20SChristian Riesch 		struct sk_buff *skb2;
171b4cdae20SChristian Riesch 
172b4cdae20SChristian Riesch 		skb2 = skb_copy_expand(skb, 4, padlen, flags);
173b4cdae20SChristian Riesch 		dev_kfree_skb_any(skb);
174b4cdae20SChristian Riesch 		skb = skb2;
175b4cdae20SChristian Riesch 		if (!skb)
176b4cdae20SChristian Riesch 			return NULL;
177b4cdae20SChristian Riesch 	}
178b4cdae20SChristian Riesch 
179b4cdae20SChristian Riesch 	packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
180b4cdae20SChristian Riesch 	skb_push(skb, 4);
181b4cdae20SChristian Riesch 	cpu_to_le32s(&packet_len);
182b4cdae20SChristian Riesch 	skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
183b4cdae20SChristian Riesch 
184b4cdae20SChristian Riesch 	if (padlen) {
185b4cdae20SChristian Riesch 		cpu_to_le32s(&padbytes);
186b4cdae20SChristian Riesch 		memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
187b4cdae20SChristian Riesch 		skb_put(skb, sizeof(padbytes));
188b4cdae20SChristian Riesch 	}
189b4cdae20SChristian Riesch 	return skb;
190b4cdae20SChristian Riesch }
191b4cdae20SChristian Riesch 
192607740bcSChristian Riesch int asix_set_sw_mii(struct usbnet *dev)
193b4cdae20SChristian Riesch {
194b4cdae20SChristian Riesch 	int ret;
195b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
196b4cdae20SChristian Riesch 	if (ret < 0)
197b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable software MII access\n");
198b4cdae20SChristian Riesch 	return ret;
199b4cdae20SChristian Riesch }
200b4cdae20SChristian Riesch 
201607740bcSChristian Riesch int asix_set_hw_mii(struct usbnet *dev)
202b4cdae20SChristian Riesch {
203b4cdae20SChristian Riesch 	int ret;
204b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
205b4cdae20SChristian Riesch 	if (ret < 0)
206b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable hardware MII access\n");
207b4cdae20SChristian Riesch 	return ret;
208b4cdae20SChristian Riesch }
209b4cdae20SChristian Riesch 
21016626b0cSChristian Riesch int asix_read_phy_addr(struct usbnet *dev, int internal)
211b4cdae20SChristian Riesch {
21216626b0cSChristian Riesch 	int offset = (internal ? 1 : 0);
213b4cdae20SChristian Riesch 	u8 buf[2];
214b4cdae20SChristian Riesch 	int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
215b4cdae20SChristian Riesch 
216b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr()\n");
217b4cdae20SChristian Riesch 
218b4cdae20SChristian Riesch 	if (ret < 0) {
219b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
220b4cdae20SChristian Riesch 		goto out;
221b4cdae20SChristian Riesch 	}
222b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
223b4cdae20SChristian Riesch 		   *((__le16 *)buf));
22416626b0cSChristian Riesch 	ret = buf[offset];
225b4cdae20SChristian Riesch 
226b4cdae20SChristian Riesch out:
227b4cdae20SChristian Riesch 	return ret;
228b4cdae20SChristian Riesch }
229b4cdae20SChristian Riesch 
23016626b0cSChristian Riesch int asix_get_phy_addr(struct usbnet *dev)
23116626b0cSChristian Riesch {
23216626b0cSChristian Riesch 	/* return the address of the internal phy */
23316626b0cSChristian Riesch 	return asix_read_phy_addr(dev, 1);
23416626b0cSChristian Riesch }
23516626b0cSChristian Riesch 
23616626b0cSChristian Riesch 
237607740bcSChristian Riesch int asix_sw_reset(struct usbnet *dev, u8 flags)
238b4cdae20SChristian Riesch {
239b4cdae20SChristian Riesch 	int ret;
240b4cdae20SChristian Riesch 
241b4cdae20SChristian Riesch         ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
242b4cdae20SChristian Riesch 	if (ret < 0)
243b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
244b4cdae20SChristian Riesch 
245b4cdae20SChristian Riesch 	return ret;
246b4cdae20SChristian Riesch }
247b4cdae20SChristian Riesch 
248607740bcSChristian Riesch u16 asix_read_rx_ctl(struct usbnet *dev)
249b4cdae20SChristian Riesch {
250b4cdae20SChristian Riesch 	__le16 v;
251b4cdae20SChristian Riesch 	int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
252b4cdae20SChristian Riesch 
253b4cdae20SChristian Riesch 	if (ret < 0) {
254b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
255b4cdae20SChristian Riesch 		goto out;
256b4cdae20SChristian Riesch 	}
257b4cdae20SChristian Riesch 	ret = le16_to_cpu(v);
258b4cdae20SChristian Riesch out:
259b4cdae20SChristian Riesch 	return ret;
260b4cdae20SChristian Riesch }
261b4cdae20SChristian Riesch 
262607740bcSChristian Riesch int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
263b4cdae20SChristian Riesch {
264b4cdae20SChristian Riesch 	int ret;
265b4cdae20SChristian Riesch 
266b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
267b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
268b4cdae20SChristian Riesch 	if (ret < 0)
269b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
270b4cdae20SChristian Riesch 			   mode, ret);
271b4cdae20SChristian Riesch 
272b4cdae20SChristian Riesch 	return ret;
273b4cdae20SChristian Riesch }
274b4cdae20SChristian Riesch 
275607740bcSChristian Riesch u16 asix_read_medium_status(struct usbnet *dev)
276b4cdae20SChristian Riesch {
277b4cdae20SChristian Riesch 	__le16 v;
278b4cdae20SChristian Riesch 	int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
279b4cdae20SChristian Riesch 
280b4cdae20SChristian Riesch 	if (ret < 0) {
281b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
282b4cdae20SChristian Riesch 			   ret);
283b4cdae20SChristian Riesch 		return ret;	/* TODO: callers not checking for error ret */
284b4cdae20SChristian Riesch 	}
285b4cdae20SChristian Riesch 
286b4cdae20SChristian Riesch 	return le16_to_cpu(v);
287b4cdae20SChristian Riesch 
288b4cdae20SChristian Riesch }
289b4cdae20SChristian Riesch 
290607740bcSChristian Riesch int asix_write_medium_mode(struct usbnet *dev, u16 mode)
291b4cdae20SChristian Riesch {
292b4cdae20SChristian Riesch 	int ret;
293b4cdae20SChristian Riesch 
294b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
295b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
296b4cdae20SChristian Riesch 	if (ret < 0)
297b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
298b4cdae20SChristian Riesch 			   mode, ret);
299b4cdae20SChristian Riesch 
300b4cdae20SChristian Riesch 	return ret;
301b4cdae20SChristian Riesch }
302b4cdae20SChristian Riesch 
303607740bcSChristian Riesch int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
304b4cdae20SChristian Riesch {
305b4cdae20SChristian Riesch 	int ret;
306b4cdae20SChristian Riesch 
307b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
308b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
309b4cdae20SChristian Riesch 	if (ret < 0)
310b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
311b4cdae20SChristian Riesch 			   value, ret);
312b4cdae20SChristian Riesch 
313b4cdae20SChristian Riesch 	if (sleep)
314b4cdae20SChristian Riesch 		msleep(sleep);
315b4cdae20SChristian Riesch 
316b4cdae20SChristian Riesch 	return ret;
317b4cdae20SChristian Riesch }
318b4cdae20SChristian Riesch 
319b4cdae20SChristian Riesch /*
320b4cdae20SChristian Riesch  * AX88772 & AX88178 have a 16-bit RX_CTL value
321b4cdae20SChristian Riesch  */
322607740bcSChristian Riesch void asix_set_multicast(struct net_device *net)
323b4cdae20SChristian Riesch {
324b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
325b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
326b4cdae20SChristian Riesch 	u16 rx_ctl = AX_DEFAULT_RX_CTL;
327b4cdae20SChristian Riesch 
328b4cdae20SChristian Riesch 	if (net->flags & IFF_PROMISC) {
329b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_PRO;
330b4cdae20SChristian Riesch 	} else if (net->flags & IFF_ALLMULTI ||
331b4cdae20SChristian Riesch 		   netdev_mc_count(net) > AX_MAX_MCAST) {
332b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AMALL;
333b4cdae20SChristian Riesch 	} else if (netdev_mc_empty(net)) {
334b4cdae20SChristian Riesch 		/* just broadcast and directed */
335b4cdae20SChristian Riesch 	} else {
336b4cdae20SChristian Riesch 		/* We use the 20 byte dev->data
337b4cdae20SChristian Riesch 		 * for our 8 byte filter buffer
338b4cdae20SChristian Riesch 		 * to avoid allocating memory that
339b4cdae20SChristian Riesch 		 * is tricky to free later */
340b4cdae20SChristian Riesch 		struct netdev_hw_addr *ha;
341b4cdae20SChristian Riesch 		u32 crc_bits;
342b4cdae20SChristian Riesch 
343b4cdae20SChristian Riesch 		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
344b4cdae20SChristian Riesch 
345b4cdae20SChristian Riesch 		/* Build the multicast hash filter. */
346b4cdae20SChristian Riesch 		netdev_for_each_mc_addr(ha, net) {
347b4cdae20SChristian Riesch 			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
348b4cdae20SChristian Riesch 			data->multi_filter[crc_bits >> 3] |=
349b4cdae20SChristian Riesch 			    1 << (crc_bits & 7);
350b4cdae20SChristian Riesch 		}
351b4cdae20SChristian Riesch 
352b4cdae20SChristian Riesch 		asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
353b4cdae20SChristian Riesch 				   AX_MCAST_FILTER_SIZE, data->multi_filter);
354b4cdae20SChristian Riesch 
355b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AM;
356b4cdae20SChristian Riesch 	}
357b4cdae20SChristian Riesch 
358b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
359b4cdae20SChristian Riesch }
360b4cdae20SChristian Riesch 
361607740bcSChristian Riesch int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
362b4cdae20SChristian Riesch {
363b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
364b4cdae20SChristian Riesch 	__le16 res;
365b4cdae20SChristian Riesch 
366b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
367b4cdae20SChristian Riesch 	asix_set_sw_mii(dev);
368b4cdae20SChristian Riesch 	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
369b4cdae20SChristian Riesch 				(__u16)loc, 2, &res);
370b4cdae20SChristian Riesch 	asix_set_hw_mii(dev);
371b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
372b4cdae20SChristian Riesch 
373b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
374b4cdae20SChristian Riesch 		   phy_id, loc, le16_to_cpu(res));
375b4cdae20SChristian Riesch 
376b4cdae20SChristian Riesch 	return le16_to_cpu(res);
377b4cdae20SChristian Riesch }
378b4cdae20SChristian Riesch 
379607740bcSChristian Riesch void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
380b4cdae20SChristian Riesch {
381b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
382b4cdae20SChristian Riesch 	__le16 res = cpu_to_le16(val);
383b4cdae20SChristian Riesch 
384b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
385b4cdae20SChristian Riesch 		   phy_id, loc, val);
386b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
387b4cdae20SChristian Riesch 	asix_set_sw_mii(dev);
388b4cdae20SChristian Riesch 	asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
389b4cdae20SChristian Riesch 	asix_set_hw_mii(dev);
390b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
391b4cdae20SChristian Riesch }
392b4cdae20SChristian Riesch 
393607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
394b4cdae20SChristian Riesch {
395b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
396b4cdae20SChristian Riesch 	u8 opt;
397b4cdae20SChristian Riesch 
398b4cdae20SChristian Riesch 	if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
399b4cdae20SChristian Riesch 		wolinfo->supported = 0;
400b4cdae20SChristian Riesch 		wolinfo->wolopts = 0;
401b4cdae20SChristian Riesch 		return;
402b4cdae20SChristian Riesch 	}
403b4cdae20SChristian Riesch 	wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
404b4cdae20SChristian Riesch 	wolinfo->wolopts = 0;
405b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_LINK)
406b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_PHY;
407b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_MAGIC)
408b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_MAGIC;
409b4cdae20SChristian Riesch }
410b4cdae20SChristian Riesch 
411607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
412b4cdae20SChristian Riesch {
413b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
414b4cdae20SChristian Riesch 	u8 opt = 0;
415b4cdae20SChristian Riesch 
416b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_PHY)
417b4cdae20SChristian Riesch 		opt |= AX_MONITOR_LINK;
418b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_MAGIC)
419b4cdae20SChristian Riesch 		opt |= AX_MONITOR_MAGIC;
420b4cdae20SChristian Riesch 
421b4cdae20SChristian Riesch 	if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
422b4cdae20SChristian Riesch 			      opt, 0, 0, NULL) < 0)
423b4cdae20SChristian Riesch 		return -EINVAL;
424b4cdae20SChristian Riesch 
425b4cdae20SChristian Riesch 	return 0;
426b4cdae20SChristian Riesch }
427b4cdae20SChristian Riesch 
428607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net)
429b4cdae20SChristian Riesch {
430ceb02c91SChristian Riesch 	return AX_EEPROM_LEN;
431b4cdae20SChristian Riesch }
432b4cdae20SChristian Riesch 
433607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
434607740bcSChristian Riesch 		    u8 *data)
435b4cdae20SChristian Riesch {
436b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
437ceb02c91SChristian Riesch 	u16 *eeprom_buff;
438ceb02c91SChristian Riesch 	int first_word, last_word;
439b4cdae20SChristian Riesch 	int i;
440b4cdae20SChristian Riesch 
441ceb02c91SChristian Riesch 	if (eeprom->len == 0)
442b4cdae20SChristian Riesch 		return -EINVAL;
443b4cdae20SChristian Riesch 
444b4cdae20SChristian Riesch 	eeprom->magic = AX_EEPROM_MAGIC;
445b4cdae20SChristian Riesch 
446ceb02c91SChristian Riesch 	first_word = eeprom->offset >> 1;
447ceb02c91SChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
448ceb02c91SChristian Riesch 
449ceb02c91SChristian Riesch 	eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
450ceb02c91SChristian Riesch 			      GFP_KERNEL);
451ceb02c91SChristian Riesch 	if (!eeprom_buff)
452ceb02c91SChristian Riesch 		return -ENOMEM;
453ceb02c91SChristian Riesch 
454b4cdae20SChristian Riesch 	/* ax8817x returns 2 bytes from eeprom on read */
455ceb02c91SChristian Riesch 	for (i = first_word; i <= last_word; i++) {
456ceb02c91SChristian Riesch 		if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
457ceb02c91SChristian Riesch 				  &(eeprom_buff[i - first_word])) < 0) {
458ceb02c91SChristian Riesch 			kfree(eeprom_buff);
459ceb02c91SChristian Riesch 			return -EIO;
460b4cdae20SChristian Riesch 		}
461ceb02c91SChristian Riesch 	}
462ceb02c91SChristian Riesch 
463ceb02c91SChristian Riesch 	memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
464ceb02c91SChristian Riesch 	kfree(eeprom_buff);
465b4cdae20SChristian Riesch 	return 0;
466b4cdae20SChristian Riesch }
467b4cdae20SChristian Riesch 
468cb7b24cdSChristian Riesch int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
469cb7b24cdSChristian Riesch 		    u8 *data)
470cb7b24cdSChristian Riesch {
471cb7b24cdSChristian Riesch 	struct usbnet *dev = netdev_priv(net);
472cb7b24cdSChristian Riesch 	u16 *eeprom_buff;
473cb7b24cdSChristian Riesch 	int first_word, last_word;
474cb7b24cdSChristian Riesch 	int i;
475cb7b24cdSChristian Riesch 	int ret;
476cb7b24cdSChristian Riesch 
477cb7b24cdSChristian Riesch 	netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
478cb7b24cdSChristian Riesch 		   eeprom->len, eeprom->offset, eeprom->magic);
479cb7b24cdSChristian Riesch 
480cb7b24cdSChristian Riesch 	if (eeprom->len == 0)
481cb7b24cdSChristian Riesch 		return -EINVAL;
482cb7b24cdSChristian Riesch 
483cb7b24cdSChristian Riesch 	if (eeprom->magic != AX_EEPROM_MAGIC)
484cb7b24cdSChristian Riesch 		return -EINVAL;
485cb7b24cdSChristian Riesch 
486cb7b24cdSChristian Riesch 	first_word = eeprom->offset >> 1;
487cb7b24cdSChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
488cb7b24cdSChristian Riesch 
489cb7b24cdSChristian Riesch 	eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
490cb7b24cdSChristian Riesch 			      GFP_KERNEL);
491cb7b24cdSChristian Riesch 	if (!eeprom_buff)
492cb7b24cdSChristian Riesch 		return -ENOMEM;
493cb7b24cdSChristian Riesch 
494cb7b24cdSChristian Riesch 	/* align data to 16 bit boundaries, read the missing data from
495cb7b24cdSChristian Riesch 	   the EEPROM */
496cb7b24cdSChristian Riesch 	if (eeprom->offset & 1) {
497cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
498cb7b24cdSChristian Riesch 				    &(eeprom_buff[0]));
499cb7b24cdSChristian Riesch 		if (ret < 0) {
500cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
501cb7b24cdSChristian Riesch 			goto free;
502cb7b24cdSChristian Riesch 		}
503cb7b24cdSChristian Riesch 	}
504cb7b24cdSChristian Riesch 
505cb7b24cdSChristian Riesch 	if ((eeprom->offset + eeprom->len) & 1) {
506cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
507cb7b24cdSChristian Riesch 				    &(eeprom_buff[last_word - first_word]));
508cb7b24cdSChristian Riesch 		if (ret < 0) {
509cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
510cb7b24cdSChristian Riesch 			goto free;
511cb7b24cdSChristian Riesch 		}
512cb7b24cdSChristian Riesch 	}
513cb7b24cdSChristian Riesch 
514cb7b24cdSChristian Riesch 	memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
515cb7b24cdSChristian Riesch 
516cb7b24cdSChristian Riesch 	/* write data to EEPROM */
517cb7b24cdSChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL);
518cb7b24cdSChristian Riesch 	if (ret < 0) {
519cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to enable EEPROM write\n");
520cb7b24cdSChristian Riesch 		goto free;
521cb7b24cdSChristian Riesch 	}
522cb7b24cdSChristian Riesch 	msleep(20);
523cb7b24cdSChristian Riesch 
524cb7b24cdSChristian Riesch 	for (i = first_word; i <= last_word; i++) {
525cb7b24cdSChristian Riesch 		netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
526cb7b24cdSChristian Riesch 			   i, eeprom_buff[i - first_word]);
527cb7b24cdSChristian Riesch 		ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
528cb7b24cdSChristian Riesch 				     eeprom_buff[i - first_word], 0, NULL);
529cb7b24cdSChristian Riesch 		if (ret < 0) {
530cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
531cb7b24cdSChristian Riesch 				   i);
532cb7b24cdSChristian Riesch 			goto free;
533cb7b24cdSChristian Riesch 		}
534cb7b24cdSChristian Riesch 		msleep(20);
535cb7b24cdSChristian Riesch 	}
536cb7b24cdSChristian Riesch 
537cb7b24cdSChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL);
538cb7b24cdSChristian Riesch 	if (ret < 0) {
539cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to disable EEPROM write\n");
540cb7b24cdSChristian Riesch 		goto free;
541cb7b24cdSChristian Riesch 	}
542cb7b24cdSChristian Riesch 
543cb7b24cdSChristian Riesch 	ret = 0;
544cb7b24cdSChristian Riesch free:
545cb7b24cdSChristian Riesch 	kfree(eeprom_buff);
546cb7b24cdSChristian Riesch 	return ret;
547cb7b24cdSChristian Riesch }
548cb7b24cdSChristian Riesch 
549607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
550b4cdae20SChristian Riesch {
551b4cdae20SChristian Riesch 	/* Inherit standard device info */
552b4cdae20SChristian Riesch 	usbnet_get_drvinfo(net, info);
5537826d43fSJiri Pirko 	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
5547826d43fSJiri Pirko 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
555ceb02c91SChristian Riesch 	info->eedump_len = AX_EEPROM_LEN;
556b4cdae20SChristian Riesch }
557b4cdae20SChristian Riesch 
558607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p)
559b4cdae20SChristian Riesch {
560b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
561b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
562b4cdae20SChristian Riesch 	struct sockaddr *addr = p;
563b4cdae20SChristian Riesch 
564b4cdae20SChristian Riesch 	if (netif_running(net))
565b4cdae20SChristian Riesch 		return -EBUSY;
566b4cdae20SChristian Riesch 	if (!is_valid_ether_addr(addr->sa_data))
567b4cdae20SChristian Riesch 		return -EADDRNOTAVAIL;
568b4cdae20SChristian Riesch 
569b4cdae20SChristian Riesch 	memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
570b4cdae20SChristian Riesch 
571b4cdae20SChristian Riesch 	/* We use the 20 byte dev->data
572b4cdae20SChristian Riesch 	 * for our 6 byte mac buffer
573b4cdae20SChristian Riesch 	 * to avoid allocating memory that
574b4cdae20SChristian Riesch 	 * is tricky to free later */
575b4cdae20SChristian Riesch 	memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
576b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
577b4cdae20SChristian Riesch 							data->mac_addr);
578b4cdae20SChristian Riesch 
579b4cdae20SChristian Riesch 	return 0;
580b4cdae20SChristian Riesch }
581