xref: /openbmc/linux/drivers/net/usb/asix_common.c (revision b952f4dff2751252db073c27c0f8a16a416a2ddc)
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
199cb00073SJeff Kirsher  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20b4cdae20SChristian Riesch  */
21b4cdae20SChristian Riesch 
22607740bcSChristian Riesch #include "asix.h"
23b4cdae20SChristian Riesch 
24607740bcSChristian Riesch int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
25d9fe64e5SRobert Foss 		  u16 size, void *data, int in_pm)
26b4cdae20SChristian Riesch {
270bc69efbSMing Lei 	int ret;
28d9fe64e5SRobert Foss 	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
29d9fe64e5SRobert Foss 
30d9fe64e5SRobert Foss 	BUG_ON(!dev);
31d9fe64e5SRobert Foss 
32d9fe64e5SRobert Foss 	if (!in_pm)
33d9fe64e5SRobert Foss 		fn = usbnet_read_cmd;
34d9fe64e5SRobert Foss 	else
35d9fe64e5SRobert Foss 		fn = usbnet_read_cmd_nopm;
36d9fe64e5SRobert Foss 
37d9fe64e5SRobert Foss 	ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
380bc69efbSMing Lei 		 value, index, data, size);
39b4cdae20SChristian Riesch 
40d9fe64e5SRobert Foss 	if (unlikely(ret < 0))
41d9fe64e5SRobert Foss 		netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
42d9fe64e5SRobert Foss 			    index, ret);
43d9fe64e5SRobert Foss 
440bc69efbSMing Lei 	return ret;
45b4cdae20SChristian Riesch }
46b4cdae20SChristian Riesch 
47607740bcSChristian Riesch int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
48d9fe64e5SRobert Foss 		   u16 size, void *data, int in_pm)
49b4cdae20SChristian Riesch {
50d9fe64e5SRobert Foss 	int ret;
51d9fe64e5SRobert Foss 	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
52d9fe64e5SRobert Foss 
53d9fe64e5SRobert Foss 	BUG_ON(!dev);
54d9fe64e5SRobert Foss 
55d9fe64e5SRobert Foss 	if (!in_pm)
56d9fe64e5SRobert Foss 		fn = usbnet_write_cmd;
57d9fe64e5SRobert Foss 	else
58d9fe64e5SRobert Foss 		fn = usbnet_write_cmd_nopm;
59d9fe64e5SRobert Foss 
60d9fe64e5SRobert Foss 	ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
610bc69efbSMing Lei 		 value, index, data, size);
62d9fe64e5SRobert Foss 
63d9fe64e5SRobert Foss 	if (unlikely(ret < 0))
64d9fe64e5SRobert Foss 		netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
65d9fe64e5SRobert Foss 			    index, ret);
66d9fe64e5SRobert Foss 
67d9fe64e5SRobert Foss 	return ret;
68b4cdae20SChristian Riesch }
69b4cdae20SChristian Riesch 
70607740bcSChristian Riesch void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
71b4cdae20SChristian Riesch 			  u16 size, void *data)
72b4cdae20SChristian Riesch {
730bc69efbSMing Lei 	usbnet_write_cmd_async(dev, cmd,
740bc69efbSMing Lei 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
750bc69efbSMing Lei 			       value, index, data, size);
76b4cdae20SChristian Riesch }
77b4cdae20SChristian Riesch 
788b5b6f54SLucas Stach int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
798b5b6f54SLucas Stach 			   struct asix_rx_fixup_info *rx)
80b4cdae20SChristian Riesch {
81b4cdae20SChristian Riesch 	int offset = 0;
827b0378f5SDean Jenkins 	u16 size;
83b4cdae20SChristian Riesch 
843f30b158SDean Jenkins 	/* When an Ethernet frame spans multiple URB socket buffers,
853f30b158SDean Jenkins 	 * do a sanity test for the Data header synchronisation.
863f30b158SDean Jenkins 	 * Attempt to detect the situation of the previous socket buffer having
873f30b158SDean Jenkins 	 * been truncated or a socket buffer was missing. These situations
883f30b158SDean Jenkins 	 * cause a discontinuity in the data stream and therefore need to avoid
893f30b158SDean Jenkins 	 * appending bad data to the end of the current netdev socket buffer.
903f30b158SDean Jenkins 	 * Also avoid unnecessarily discarding a good current netdev socket
913f30b158SDean Jenkins 	 * buffer.
923f30b158SDean Jenkins 	 */
933f30b158SDean Jenkins 	if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
94cd9e2e5dSJohn Stultz 		offset = ((rx->remaining + 1) & 0xfffe);
953f30b158SDean Jenkins 		rx->header = get_unaligned_le32(skb->data + offset);
963f30b158SDean Jenkins 		offset = 0;
973f30b158SDean Jenkins 
983f30b158SDean Jenkins 		size = (u16)(rx->header & 0x7ff);
993f30b158SDean Jenkins 		if (size != ((~rx->header >> 16) & 0x7ff)) {
1003f30b158SDean Jenkins 			netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
1013f30b158SDean Jenkins 				   rx->remaining);
1026a570814SDean Jenkins 			if (rx->ax_skb) {
1033f30b158SDean Jenkins 				kfree_skb(rx->ax_skb);
1043f30b158SDean Jenkins 				rx->ax_skb = NULL;
1056a570814SDean Jenkins 				/* Discard the incomplete netdev Ethernet frame
1066a570814SDean Jenkins 				 * and assume the Data header is at the start of
1076a570814SDean Jenkins 				 * the current URB socket buffer.
1083f30b158SDean Jenkins 				 */
1096a570814SDean Jenkins 			}
1103f30b158SDean Jenkins 			rx->remaining = 0;
1113f30b158SDean Jenkins 		}
1123f30b158SDean Jenkins 	}
1133f30b158SDean Jenkins 
1148b5b6f54SLucas Stach 	while (offset + sizeof(u16) <= skb->len) {
1157b0378f5SDean Jenkins 		u16 copy_length;
116b4cdae20SChristian Riesch 
1177b0378f5SDean Jenkins 		if (!rx->remaining) {
1183bfc69abSDean Jenkins 			if (skb->len - offset == sizeof(u16)) {
1198b5b6f54SLucas Stach 				rx->header = get_unaligned_le16(
1208b5b6f54SLucas Stach 						skb->data + offset);
1218b5b6f54SLucas Stach 				rx->split_head = true;
1228b5b6f54SLucas Stach 				offset += sizeof(u16);
1238b5b6f54SLucas Stach 				break;
1243bfc69abSDean Jenkins 			}
1253bfc69abSDean Jenkins 
1263bfc69abSDean Jenkins 			if (rx->split_head == true) {
1278b5b6f54SLucas Stach 				rx->header |= (get_unaligned_le16(
1283bfc69abSDean Jenkins 						skb->data + offset) << 16);
1298b5b6f54SLucas Stach 				rx->split_head = false;
1308b5b6f54SLucas Stach 				offset += sizeof(u16);
1318b5b6f54SLucas Stach 			} else {
1328b5b6f54SLucas Stach 				rx->header = get_unaligned_le32(skb->data +
1338b5b6f54SLucas Stach 								offset);
134b4cdae20SChristian Riesch 				offset += sizeof(u32);
1358b5b6f54SLucas Stach 			}
136b4cdae20SChristian Riesch 
1377b0378f5SDean Jenkins 			/* take frame length from Data header 32-bit word */
1387b0378f5SDean Jenkins 			size = (u16)(rx->header & 0x7ff);
1397b0378f5SDean Jenkins 			if (size != ((~rx->header >> 16) & 0x7ff)) {
1408b5b6f54SLucas Stach 				netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
1418b5b6f54SLucas Stach 					   rx->header, offset);
1428b5b6f54SLucas Stach 				return 0;
1438b5b6f54SLucas Stach 			}
1449a5ccd8eSDean Jenkins 			if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
145b70183dbSstephen hemminger 				netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
1469a5ccd8eSDean Jenkins 					   size);
1479a5ccd8eSDean Jenkins 				return 0;
1489a5ccd8eSDean Jenkins 			}
1499a5ccd8eSDean Jenkins 
1506a570814SDean Jenkins 			/* Sometimes may fail to get a netdev socket buffer but
1516a570814SDean Jenkins 			 * continue to process the URB socket buffer so that
1526a570814SDean Jenkins 			 * synchronisation of the Ethernet frame Data header
1536a570814SDean Jenkins 			 * word is maintained.
1546a570814SDean Jenkins 			 */
1557b0378f5SDean Jenkins 			rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
156b4cdae20SChristian Riesch 
1579a5ccd8eSDean Jenkins 			rx->remaining = size;
158b4cdae20SChristian Riesch 		}
159b4cdae20SChristian Riesch 
1607b0378f5SDean Jenkins 		if (rx->remaining > skb->len - offset) {
1617b0378f5SDean Jenkins 			copy_length = skb->len - offset;
1627b0378f5SDean Jenkins 			rx->remaining -= copy_length;
1637b0378f5SDean Jenkins 		} else {
1647b0378f5SDean Jenkins 			copy_length = rx->remaining;
1657b0378f5SDean Jenkins 			rx->remaining = 0;
1668b5b6f54SLucas Stach 		}
167b4cdae20SChristian Riesch 
1686a570814SDean Jenkins 		if (rx->ax_skb) {
169*b952f4dfSyuan linyu 			skb_put_data(rx->ax_skb, skb->data + offset,
17059ae1d12SJohannes Berg 				     copy_length);
1717b0378f5SDean Jenkins 			if (!rx->remaining)
1728b5b6f54SLucas Stach 				usbnet_skb_return(dev, rx->ax_skb);
1736a570814SDean Jenkins 		}
1748b5b6f54SLucas Stach 
1757b0378f5SDean Jenkins 		offset += (copy_length + 1) & 0xfffe;
176b4cdae20SChristian Riesch 	}
177b4cdae20SChristian Riesch 
178b4cdae20SChristian Riesch 	if (skb->len != offset) {
1798b5b6f54SLucas Stach 		netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
1808b5b6f54SLucas Stach 			   skb->len, offset);
181b4cdae20SChristian Riesch 		return 0;
182b4cdae20SChristian Riesch 	}
1838b5b6f54SLucas Stach 
184b4cdae20SChristian Riesch 	return 1;
185b4cdae20SChristian Riesch }
186b4cdae20SChristian Riesch 
1878b5b6f54SLucas Stach int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
1888b5b6f54SLucas Stach {
1898b5b6f54SLucas Stach 	struct asix_common_private *dp = dev->driver_priv;
1908b5b6f54SLucas Stach 	struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
1918b5b6f54SLucas Stach 
1928b5b6f54SLucas Stach 	return asix_rx_fixup_internal(dev, skb, rx);
1938b5b6f54SLucas Stach }
1948b5b6f54SLucas Stach 
195607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
196b4cdae20SChristian Riesch 			      gfp_t flags)
197b4cdae20SChristian Riesch {
198b4cdae20SChristian Riesch 	int padlen;
199b4cdae20SChristian Riesch 	int headroom = skb_headroom(skb);
200b4cdae20SChristian Riesch 	int tailroom = skb_tailroom(skb);
201b4cdae20SChristian Riesch 	u32 packet_len;
202b4cdae20SChristian Riesch 	u32 padbytes = 0xffff0000;
203b4cdae20SChristian Riesch 
204b4cdae20SChristian Riesch 	padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
205b4cdae20SChristian Riesch 
206b4cdae20SChristian Riesch 	/* We need to push 4 bytes in front of frame (packet_len)
207b4cdae20SChristian Riesch 	 * and maybe add 4 bytes after the end (if padlen is 4)
208b4cdae20SChristian Riesch 	 *
209b4cdae20SChristian Riesch 	 * Avoid skb_copy_expand() expensive call, using following rules :
210b4cdae20SChristian Riesch 	 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
211b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of headroom)
212b4cdae20SChristian Riesch 	 * - We are allowed to put 4 bytes at tail if skb_cloned()
213b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of tailroom)
214b4cdae20SChristian Riesch 	 *
215b4cdae20SChristian Riesch 	 * TCP packets for example are cloned, but skb_header_release()
216b4cdae20SChristian Riesch 	 * was called in tcp stack, allowing us to use headroom for our needs.
217b4cdae20SChristian Riesch 	 */
218b4cdae20SChristian Riesch 	if (!skb_header_cloned(skb) &&
219b4cdae20SChristian Riesch 	    !(padlen && skb_cloned(skb)) &&
220b4cdae20SChristian Riesch 	    headroom + tailroom >= 4 + padlen) {
221b4cdae20SChristian Riesch 		/* following should not happen, but better be safe */
222b4cdae20SChristian Riesch 		if (headroom < 4 ||
223b4cdae20SChristian Riesch 		    tailroom < padlen) {
224b4cdae20SChristian Riesch 			skb->data = memmove(skb->head + 4, skb->data, skb->len);
225b4cdae20SChristian Riesch 			skb_set_tail_pointer(skb, skb->len);
226b4cdae20SChristian Riesch 		}
227b4cdae20SChristian Riesch 	} else {
228b4cdae20SChristian Riesch 		struct sk_buff *skb2;
229b4cdae20SChristian Riesch 
230b4cdae20SChristian Riesch 		skb2 = skb_copy_expand(skb, 4, padlen, flags);
231b4cdae20SChristian Riesch 		dev_kfree_skb_any(skb);
232b4cdae20SChristian Riesch 		skb = skb2;
233b4cdae20SChristian Riesch 		if (!skb)
234b4cdae20SChristian Riesch 			return NULL;
235b4cdae20SChristian Riesch 	}
236b4cdae20SChristian Riesch 
237b4cdae20SChristian Riesch 	packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
238b4cdae20SChristian Riesch 	skb_push(skb, 4);
239b4cdae20SChristian Riesch 	cpu_to_le32s(&packet_len);
240b4cdae20SChristian Riesch 	skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
241b4cdae20SChristian Riesch 
242b4cdae20SChristian Riesch 	if (padlen) {
243b4cdae20SChristian Riesch 		cpu_to_le32s(&padbytes);
244b4cdae20SChristian Riesch 		memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
245b4cdae20SChristian Riesch 		skb_put(skb, sizeof(padbytes));
246b4cdae20SChristian Riesch 	}
2471e9e39f4SBen Hutchings 
2487a1e890eSBen Hutchings 	usbnet_set_skb_tx_stats(skb, 1, 0);
249b4cdae20SChristian Riesch 	return skb;
250b4cdae20SChristian Riesch }
251b4cdae20SChristian Riesch 
252d9fe64e5SRobert Foss int asix_set_sw_mii(struct usbnet *dev, int in_pm)
253b4cdae20SChristian Riesch {
254b4cdae20SChristian Riesch 	int ret;
255d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm);
256d9fe64e5SRobert Foss 
257b4cdae20SChristian Riesch 	if (ret < 0)
258b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable software MII access\n");
259b4cdae20SChristian Riesch 	return ret;
260b4cdae20SChristian Riesch }
261b4cdae20SChristian Riesch 
262d9fe64e5SRobert Foss int asix_set_hw_mii(struct usbnet *dev, int in_pm)
263b4cdae20SChristian Riesch {
264b4cdae20SChristian Riesch 	int ret;
265d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm);
266b4cdae20SChristian Riesch 	if (ret < 0)
267b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable hardware MII access\n");
268b4cdae20SChristian Riesch 	return ret;
269b4cdae20SChristian Riesch }
270b4cdae20SChristian Riesch 
27116626b0cSChristian Riesch int asix_read_phy_addr(struct usbnet *dev, int internal)
272b4cdae20SChristian Riesch {
27316626b0cSChristian Riesch 	int offset = (internal ? 1 : 0);
274b4cdae20SChristian Riesch 	u8 buf[2];
275d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0);
276b4cdae20SChristian Riesch 
277b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr()\n");
278b4cdae20SChristian Riesch 
279b4cdae20SChristian Riesch 	if (ret < 0) {
280b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
281b4cdae20SChristian Riesch 		goto out;
282b4cdae20SChristian Riesch 	}
283b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
284b4cdae20SChristian Riesch 		   *((__le16 *)buf));
28516626b0cSChristian Riesch 	ret = buf[offset];
286b4cdae20SChristian Riesch 
287b4cdae20SChristian Riesch out:
288b4cdae20SChristian Riesch 	return ret;
289b4cdae20SChristian Riesch }
290b4cdae20SChristian Riesch 
29116626b0cSChristian Riesch int asix_get_phy_addr(struct usbnet *dev)
29216626b0cSChristian Riesch {
29316626b0cSChristian Riesch 	/* return the address of the internal phy */
29416626b0cSChristian Riesch 	return asix_read_phy_addr(dev, 1);
29516626b0cSChristian Riesch }
29616626b0cSChristian Riesch 
29716626b0cSChristian Riesch 
298d9fe64e5SRobert Foss int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
299b4cdae20SChristian Riesch {
300b4cdae20SChristian Riesch 	int ret;
301b4cdae20SChristian Riesch 
302d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
303b4cdae20SChristian Riesch 	if (ret < 0)
304b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
305b4cdae20SChristian Riesch 
306b4cdae20SChristian Riesch 	return ret;
307b4cdae20SChristian Riesch }
308b4cdae20SChristian Riesch 
309d9fe64e5SRobert Foss u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
310b4cdae20SChristian Riesch {
311b4cdae20SChristian Riesch 	__le16 v;
312d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
313b4cdae20SChristian Riesch 
314b4cdae20SChristian Riesch 	if (ret < 0) {
315b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
316b4cdae20SChristian Riesch 		goto out;
317b4cdae20SChristian Riesch 	}
318b4cdae20SChristian Riesch 	ret = le16_to_cpu(v);
319b4cdae20SChristian Riesch out:
320b4cdae20SChristian Riesch 	return ret;
321b4cdae20SChristian Riesch }
322b4cdae20SChristian Riesch 
323d9fe64e5SRobert Foss int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
324b4cdae20SChristian Riesch {
325b4cdae20SChristian Riesch 	int ret;
326b4cdae20SChristian Riesch 
327b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
328d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
329b4cdae20SChristian Riesch 	if (ret < 0)
330b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
331b4cdae20SChristian Riesch 			   mode, ret);
332b4cdae20SChristian Riesch 
333b4cdae20SChristian Riesch 	return ret;
334b4cdae20SChristian Riesch }
335b4cdae20SChristian Riesch 
336d9fe64e5SRobert Foss u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
337b4cdae20SChristian Riesch {
338b4cdae20SChristian Riesch 	__le16 v;
339d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
340d9fe64e5SRobert Foss 				0, 0, 2, &v, in_pm);
341b4cdae20SChristian Riesch 
342b4cdae20SChristian Riesch 	if (ret < 0) {
343b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
344b4cdae20SChristian Riesch 			   ret);
345b4cdae20SChristian Riesch 		return ret;	/* TODO: callers not checking for error ret */
346b4cdae20SChristian Riesch 	}
347b4cdae20SChristian Riesch 
348b4cdae20SChristian Riesch 	return le16_to_cpu(v);
349b4cdae20SChristian Riesch 
350b4cdae20SChristian Riesch }
351b4cdae20SChristian Riesch 
352d9fe64e5SRobert Foss int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
353b4cdae20SChristian Riesch {
354b4cdae20SChristian Riesch 	int ret;
355b4cdae20SChristian Riesch 
356b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
357d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
358d9fe64e5SRobert Foss 			     mode, 0, 0, NULL, in_pm);
359b4cdae20SChristian Riesch 	if (ret < 0)
360b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
361b4cdae20SChristian Riesch 			   mode, ret);
362b4cdae20SChristian Riesch 
363b4cdae20SChristian Riesch 	return ret;
364b4cdae20SChristian Riesch }
365b4cdae20SChristian Riesch 
366d9fe64e5SRobert Foss int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
367b4cdae20SChristian Riesch {
368b4cdae20SChristian Riesch 	int ret;
369b4cdae20SChristian Riesch 
370b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
371d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
372b4cdae20SChristian Riesch 	if (ret < 0)
373b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
374b4cdae20SChristian Riesch 			   value, ret);
375b4cdae20SChristian Riesch 
376b4cdae20SChristian Riesch 	if (sleep)
377b4cdae20SChristian Riesch 		msleep(sleep);
378b4cdae20SChristian Riesch 
379b4cdae20SChristian Riesch 	return ret;
380b4cdae20SChristian Riesch }
381b4cdae20SChristian Riesch 
382b4cdae20SChristian Riesch /*
383b4cdae20SChristian Riesch  * AX88772 & AX88178 have a 16-bit RX_CTL value
384b4cdae20SChristian Riesch  */
385607740bcSChristian Riesch void asix_set_multicast(struct net_device *net)
386b4cdae20SChristian Riesch {
387b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
388b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
389b4cdae20SChristian Riesch 	u16 rx_ctl = AX_DEFAULT_RX_CTL;
390b4cdae20SChristian Riesch 
391b4cdae20SChristian Riesch 	if (net->flags & IFF_PROMISC) {
392b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_PRO;
393b4cdae20SChristian Riesch 	} else if (net->flags & IFF_ALLMULTI ||
394b4cdae20SChristian Riesch 		   netdev_mc_count(net) > AX_MAX_MCAST) {
395b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AMALL;
396b4cdae20SChristian Riesch 	} else if (netdev_mc_empty(net)) {
397b4cdae20SChristian Riesch 		/* just broadcast and directed */
398b4cdae20SChristian Riesch 	} else {
399b4cdae20SChristian Riesch 		/* We use the 20 byte dev->data
400b4cdae20SChristian Riesch 		 * for our 8 byte filter buffer
401b4cdae20SChristian Riesch 		 * to avoid allocating memory that
402b4cdae20SChristian Riesch 		 * is tricky to free later */
403b4cdae20SChristian Riesch 		struct netdev_hw_addr *ha;
404b4cdae20SChristian Riesch 		u32 crc_bits;
405b4cdae20SChristian Riesch 
406b4cdae20SChristian Riesch 		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
407b4cdae20SChristian Riesch 
408b4cdae20SChristian Riesch 		/* Build the multicast hash filter. */
409b4cdae20SChristian Riesch 		netdev_for_each_mc_addr(ha, net) {
410b4cdae20SChristian Riesch 			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
411b4cdae20SChristian Riesch 			data->multi_filter[crc_bits >> 3] |=
412b4cdae20SChristian Riesch 			    1 << (crc_bits & 7);
413b4cdae20SChristian Riesch 		}
414b4cdae20SChristian Riesch 
415b4cdae20SChristian Riesch 		asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
416b4cdae20SChristian Riesch 				   AX_MCAST_FILTER_SIZE, data->multi_filter);
417b4cdae20SChristian Riesch 
418b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AM;
419b4cdae20SChristian Riesch 	}
420b4cdae20SChristian Riesch 
421b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
422b4cdae20SChristian Riesch }
423b4cdae20SChristian Riesch 
424607740bcSChristian Riesch int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
425b4cdae20SChristian Riesch {
426b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
427b4cdae20SChristian Riesch 	__le16 res;
428d9fe64e5SRobert Foss 	u8 smsr;
429d9fe64e5SRobert Foss 	int i = 0;
4308a46f665SRobert Foss 	int ret;
431b4cdae20SChristian Riesch 
432b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
433d9fe64e5SRobert Foss 	do {
4348a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 0);
435610df1d2SGuenter Roeck 		if (ret == -ENODEV || ret == -ETIMEDOUT)
4368a46f665SRobert Foss 			break;
437d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
4388a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
4398a46f665SRobert Foss 				    0, 0, 1, &smsr, 0);
4408a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
441610df1d2SGuenter Roeck 	if (ret == -ENODEV || ret == -ETIMEDOUT) {
4428a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
4438a46f665SRobert Foss 		return ret;
4448a46f665SRobert Foss 	}
445d9fe64e5SRobert Foss 
446b4cdae20SChristian Riesch 	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
447d9fe64e5SRobert Foss 				(__u16)loc, 2, &res, 0);
448d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 0);
449b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
450b4cdae20SChristian Riesch 
451b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
452b4cdae20SChristian Riesch 			phy_id, loc, le16_to_cpu(res));
453b4cdae20SChristian Riesch 
454b4cdae20SChristian Riesch 	return le16_to_cpu(res);
455b4cdae20SChristian Riesch }
456b4cdae20SChristian Riesch 
457607740bcSChristian Riesch void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
458b4cdae20SChristian Riesch {
459b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
460b4cdae20SChristian Riesch 	__le16 res = cpu_to_le16(val);
461d9fe64e5SRobert Foss 	u8 smsr;
462d9fe64e5SRobert Foss 	int i = 0;
4638a46f665SRobert Foss 	int ret;
464b4cdae20SChristian Riesch 
465b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
466b4cdae20SChristian Riesch 			phy_id, loc, val);
467d9fe64e5SRobert Foss 
468b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
469d9fe64e5SRobert Foss 	do {
4708a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 0);
4718a46f665SRobert Foss 		if (ret == -ENODEV)
4728a46f665SRobert Foss 			break;
473d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
4748a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
4758a46f665SRobert Foss 				    0, 0, 1, &smsr, 0);
4768a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
4778a46f665SRobert Foss 	if (ret == -ENODEV) {
4788a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
4798a46f665SRobert Foss 		return;
4808a46f665SRobert Foss 	}
481d9fe64e5SRobert Foss 
482d9fe64e5SRobert Foss 	asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
483d9fe64e5SRobert Foss 		       (__u16)loc, 2, &res, 0);
484d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 0);
485d9fe64e5SRobert Foss 	mutex_unlock(&dev->phy_mutex);
486d9fe64e5SRobert Foss }
487d9fe64e5SRobert Foss 
488d9fe64e5SRobert Foss int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
489d9fe64e5SRobert Foss {
490d9fe64e5SRobert Foss 	struct usbnet *dev = netdev_priv(netdev);
491d9fe64e5SRobert Foss 	__le16 res;
492d9fe64e5SRobert Foss 	u8 smsr;
493d9fe64e5SRobert Foss 	int i = 0;
4948a46f665SRobert Foss 	int ret;
495d9fe64e5SRobert Foss 
496d9fe64e5SRobert Foss 	mutex_lock(&dev->phy_mutex);
497d9fe64e5SRobert Foss 	do {
4988a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 1);
499610df1d2SGuenter Roeck 		if (ret == -ENODEV || ret == -ETIMEDOUT)
5008a46f665SRobert Foss 			break;
501d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
5028a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
5038a46f665SRobert Foss 				    0, 0, 1, &smsr, 1);
5048a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
505610df1d2SGuenter Roeck 	if (ret == -ENODEV || ret == -ETIMEDOUT) {
5068a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
5078a46f665SRobert Foss 		return ret;
5088a46f665SRobert Foss 	}
509d9fe64e5SRobert Foss 
510d9fe64e5SRobert Foss 	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
511d9fe64e5SRobert Foss 		      (__u16)loc, 2, &res, 1);
512d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 1);
513d9fe64e5SRobert Foss 	mutex_unlock(&dev->phy_mutex);
514d9fe64e5SRobert Foss 
515d9fe64e5SRobert Foss 	netdev_dbg(dev->net, "asix_mdio_read_nopm() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
516d9fe64e5SRobert Foss 			phy_id, loc, le16_to_cpu(res));
517d9fe64e5SRobert Foss 
518d9fe64e5SRobert Foss 	return le16_to_cpu(res);
519d9fe64e5SRobert Foss }
520d9fe64e5SRobert Foss 
521d9fe64e5SRobert Foss void
522d9fe64e5SRobert Foss asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
523d9fe64e5SRobert Foss {
524d9fe64e5SRobert Foss 	struct usbnet *dev = netdev_priv(netdev);
525d9fe64e5SRobert Foss 	__le16 res = cpu_to_le16(val);
526d9fe64e5SRobert Foss 	u8 smsr;
527d9fe64e5SRobert Foss 	int i = 0;
5288a46f665SRobert Foss 	int ret;
529d9fe64e5SRobert Foss 
530d9fe64e5SRobert Foss 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
531d9fe64e5SRobert Foss 			phy_id, loc, val);
532d9fe64e5SRobert Foss 
533d9fe64e5SRobert Foss 	mutex_lock(&dev->phy_mutex);
534d9fe64e5SRobert Foss 	do {
5358a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 1);
5368a46f665SRobert Foss 		if (ret == -ENODEV)
5378a46f665SRobert Foss 			break;
538d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
5398a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
5408a46f665SRobert Foss 				    0, 0, 1, &smsr, 1);
5418a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
5428a46f665SRobert Foss 	if (ret == -ENODEV) {
5438a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
5448a46f665SRobert Foss 		return;
5458a46f665SRobert Foss 	}
546d9fe64e5SRobert Foss 
547d9fe64e5SRobert Foss 	asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
548d9fe64e5SRobert Foss 		       (__u16)loc, 2, &res, 1);
549d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 1);
550b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
551b4cdae20SChristian Riesch }
552b4cdae20SChristian Riesch 
553607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
554b4cdae20SChristian Riesch {
555b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
556b4cdae20SChristian Riesch 	u8 opt;
557b4cdae20SChristian Riesch 
558d9fe64e5SRobert Foss 	if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
559d9fe64e5SRobert Foss 			  0, 0, 1, &opt, 0) < 0) {
560b4cdae20SChristian Riesch 		wolinfo->supported = 0;
561b4cdae20SChristian Riesch 		wolinfo->wolopts = 0;
562b4cdae20SChristian Riesch 		return;
563b4cdae20SChristian Riesch 	}
564b4cdae20SChristian Riesch 	wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
565b4cdae20SChristian Riesch 	wolinfo->wolopts = 0;
566b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_LINK)
567b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_PHY;
568b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_MAGIC)
569b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_MAGIC;
570b4cdae20SChristian Riesch }
571b4cdae20SChristian Riesch 
572607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
573b4cdae20SChristian Riesch {
574b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
575b4cdae20SChristian Riesch 	u8 opt = 0;
576b4cdae20SChristian Riesch 
577b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_PHY)
578b4cdae20SChristian Riesch 		opt |= AX_MONITOR_LINK;
579b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_MAGIC)
580b4cdae20SChristian Riesch 		opt |= AX_MONITOR_MAGIC;
581b4cdae20SChristian Riesch 
582b4cdae20SChristian Riesch 	if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
583d9fe64e5SRobert Foss 			      opt, 0, 0, NULL, 0) < 0)
584b4cdae20SChristian Riesch 		return -EINVAL;
585b4cdae20SChristian Riesch 
586b4cdae20SChristian Riesch 	return 0;
587b4cdae20SChristian Riesch }
588b4cdae20SChristian Riesch 
589607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net)
590b4cdae20SChristian Riesch {
591ceb02c91SChristian Riesch 	return AX_EEPROM_LEN;
592b4cdae20SChristian Riesch }
593b4cdae20SChristian Riesch 
594607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
595607740bcSChristian Riesch 		    u8 *data)
596b4cdae20SChristian Riesch {
597b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
598ceb02c91SChristian Riesch 	u16 *eeprom_buff;
599ceb02c91SChristian Riesch 	int first_word, last_word;
600b4cdae20SChristian Riesch 	int i;
601b4cdae20SChristian Riesch 
602ceb02c91SChristian Riesch 	if (eeprom->len == 0)
603b4cdae20SChristian Riesch 		return -EINVAL;
604b4cdae20SChristian Riesch 
605b4cdae20SChristian Riesch 	eeprom->magic = AX_EEPROM_MAGIC;
606b4cdae20SChristian Riesch 
607ceb02c91SChristian Riesch 	first_word = eeprom->offset >> 1;
608ceb02c91SChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
609ceb02c91SChristian Riesch 
610ceb02c91SChristian Riesch 	eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
611ceb02c91SChristian Riesch 			      GFP_KERNEL);
612ceb02c91SChristian Riesch 	if (!eeprom_buff)
613ceb02c91SChristian Riesch 		return -ENOMEM;
614ceb02c91SChristian Riesch 
615b4cdae20SChristian Riesch 	/* ax8817x returns 2 bytes from eeprom on read */
616ceb02c91SChristian Riesch 	for (i = first_word; i <= last_word; i++) {
617ceb02c91SChristian Riesch 		if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
618d9fe64e5SRobert Foss 				  &eeprom_buff[i - first_word], 0) < 0) {
619ceb02c91SChristian Riesch 			kfree(eeprom_buff);
620ceb02c91SChristian Riesch 			return -EIO;
621b4cdae20SChristian Riesch 		}
622ceb02c91SChristian Riesch 	}
623ceb02c91SChristian Riesch 
624ceb02c91SChristian Riesch 	memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
625ceb02c91SChristian Riesch 	kfree(eeprom_buff);
626b4cdae20SChristian Riesch 	return 0;
627b4cdae20SChristian Riesch }
628b4cdae20SChristian Riesch 
629cb7b24cdSChristian Riesch int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
630cb7b24cdSChristian Riesch 		    u8 *data)
631cb7b24cdSChristian Riesch {
632cb7b24cdSChristian Riesch 	struct usbnet *dev = netdev_priv(net);
633cb7b24cdSChristian Riesch 	u16 *eeprom_buff;
634cb7b24cdSChristian Riesch 	int first_word, last_word;
635cb7b24cdSChristian Riesch 	int i;
636cb7b24cdSChristian Riesch 	int ret;
637cb7b24cdSChristian Riesch 
638cb7b24cdSChristian Riesch 	netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
639cb7b24cdSChristian Riesch 		   eeprom->len, eeprom->offset, eeprom->magic);
640cb7b24cdSChristian Riesch 
641cb7b24cdSChristian Riesch 	if (eeprom->len == 0)
642cb7b24cdSChristian Riesch 		return -EINVAL;
643cb7b24cdSChristian Riesch 
644cb7b24cdSChristian Riesch 	if (eeprom->magic != AX_EEPROM_MAGIC)
645cb7b24cdSChristian Riesch 		return -EINVAL;
646cb7b24cdSChristian Riesch 
647cb7b24cdSChristian Riesch 	first_word = eeprom->offset >> 1;
648cb7b24cdSChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
649cb7b24cdSChristian Riesch 
650cb7b24cdSChristian Riesch 	eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
651cb7b24cdSChristian Riesch 			      GFP_KERNEL);
652cb7b24cdSChristian Riesch 	if (!eeprom_buff)
653cb7b24cdSChristian Riesch 		return -ENOMEM;
654cb7b24cdSChristian Riesch 
655cb7b24cdSChristian Riesch 	/* align data to 16 bit boundaries, read the missing data from
656cb7b24cdSChristian Riesch 	   the EEPROM */
657cb7b24cdSChristian Riesch 	if (eeprom->offset & 1) {
658cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
659d9fe64e5SRobert Foss 				    &eeprom_buff[0], 0);
660cb7b24cdSChristian Riesch 		if (ret < 0) {
661cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
662cb7b24cdSChristian Riesch 			goto free;
663cb7b24cdSChristian Riesch 		}
664cb7b24cdSChristian Riesch 	}
665cb7b24cdSChristian Riesch 
666cb7b24cdSChristian Riesch 	if ((eeprom->offset + eeprom->len) & 1) {
667cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
668d9fe64e5SRobert Foss 				    &eeprom_buff[last_word - first_word], 0);
669cb7b24cdSChristian Riesch 		if (ret < 0) {
670cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
671cb7b24cdSChristian Riesch 			goto free;
672cb7b24cdSChristian Riesch 		}
673cb7b24cdSChristian Riesch 	}
674cb7b24cdSChristian Riesch 
675cb7b24cdSChristian Riesch 	memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
676cb7b24cdSChristian Riesch 
677cb7b24cdSChristian Riesch 	/* write data to EEPROM */
678d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
679cb7b24cdSChristian Riesch 	if (ret < 0) {
680cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to enable EEPROM write\n");
681cb7b24cdSChristian Riesch 		goto free;
682cb7b24cdSChristian Riesch 	}
683cb7b24cdSChristian Riesch 	msleep(20);
684cb7b24cdSChristian Riesch 
685cb7b24cdSChristian Riesch 	for (i = first_word; i <= last_word; i++) {
686cb7b24cdSChristian Riesch 		netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
687cb7b24cdSChristian Riesch 			   i, eeprom_buff[i - first_word]);
688cb7b24cdSChristian Riesch 		ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
689d9fe64e5SRobert Foss 				     eeprom_buff[i - first_word], 0, NULL, 0);
690cb7b24cdSChristian Riesch 		if (ret < 0) {
691cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
692cb7b24cdSChristian Riesch 				   i);
693cb7b24cdSChristian Riesch 			goto free;
694cb7b24cdSChristian Riesch 		}
695cb7b24cdSChristian Riesch 		msleep(20);
696cb7b24cdSChristian Riesch 	}
697cb7b24cdSChristian Riesch 
698d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
699cb7b24cdSChristian Riesch 	if (ret < 0) {
700cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to disable EEPROM write\n");
701cb7b24cdSChristian Riesch 		goto free;
702cb7b24cdSChristian Riesch 	}
703cb7b24cdSChristian Riesch 
704cb7b24cdSChristian Riesch 	ret = 0;
705cb7b24cdSChristian Riesch free:
706cb7b24cdSChristian Riesch 	kfree(eeprom_buff);
707cb7b24cdSChristian Riesch 	return ret;
708cb7b24cdSChristian Riesch }
709cb7b24cdSChristian Riesch 
710607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
711b4cdae20SChristian Riesch {
712b4cdae20SChristian Riesch 	/* Inherit standard device info */
713b4cdae20SChristian Riesch 	usbnet_get_drvinfo(net, info);
7147826d43fSJiri Pirko 	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
7157826d43fSJiri Pirko 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
716b4cdae20SChristian Riesch }
717b4cdae20SChristian Riesch 
718607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p)
719b4cdae20SChristian Riesch {
720b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
721b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
722b4cdae20SChristian Riesch 	struct sockaddr *addr = p;
723b4cdae20SChristian Riesch 
724b4cdae20SChristian Riesch 	if (netif_running(net))
725b4cdae20SChristian Riesch 		return -EBUSY;
726b4cdae20SChristian Riesch 	if (!is_valid_ether_addr(addr->sa_data))
727b4cdae20SChristian Riesch 		return -EADDRNOTAVAIL;
728b4cdae20SChristian Riesch 
729b4cdae20SChristian Riesch 	memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
730b4cdae20SChristian Riesch 
731b4cdae20SChristian Riesch 	/* We use the 20 byte dev->data
732b4cdae20SChristian Riesch 	 * for our 6 byte mac buffer
733b4cdae20SChristian Riesch 	 * to avoid allocating memory that
734b4cdae20SChristian Riesch 	 * is tricky to free later */
735b4cdae20SChristian Riesch 	memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
736b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
737b4cdae20SChristian Riesch 							data->mac_addr);
738b4cdae20SChristian Riesch 
739b4cdae20SChristian Riesch 	return 0;
740b4cdae20SChristian Riesch }
741