xref: /openbmc/linux/drivers/net/usb/asix_common.c (revision 805206e66fab4ba1e0ebd19402006d62cd1d4902)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b4cdae20SChristian Riesch /*
3b4cdae20SChristian Riesch  * ASIX AX8817X based USB 2.0 Ethernet Devices
4b4cdae20SChristian Riesch  * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
5b4cdae20SChristian Riesch  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
6b4cdae20SChristian Riesch  * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
7b4cdae20SChristian Riesch  * Copyright (c) 2002-2003 TiVo Inc.
8b4cdae20SChristian Riesch  */
9b4cdae20SChristian Riesch 
10607740bcSChristian Riesch #include "asix.h"
11b4cdae20SChristian Riesch 
12d1652b70SPavel Skripkin #define AX_HOST_EN_RETRIES	30
13d1652b70SPavel Skripkin 
14920a9fa2SPavel Skripkin int __must_check asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
15d9fe64e5SRobert Foss 			       u16 size, void *data, int in_pm)
16b4cdae20SChristian Riesch {
170bc69efbSMing Lei 	int ret;
18d9fe64e5SRobert Foss 	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
19d9fe64e5SRobert Foss 
20d9fe64e5SRobert Foss 	BUG_ON(!dev);
21d9fe64e5SRobert Foss 
22d9fe64e5SRobert Foss 	if (!in_pm)
23d9fe64e5SRobert Foss 		fn = usbnet_read_cmd;
24d9fe64e5SRobert Foss 	else
25d9fe64e5SRobert Foss 		fn = usbnet_read_cmd_nopm;
26d9fe64e5SRobert Foss 
27d9fe64e5SRobert Foss 	ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
280bc69efbSMing Lei 		 value, index, data, size);
29b4cdae20SChristian Riesch 
30920a9fa2SPavel Skripkin 	if (unlikely(ret < size)) {
31920a9fa2SPavel Skripkin 		ret = ret < 0 ? ret : -ENODATA;
32920a9fa2SPavel Skripkin 
33d9fe64e5SRobert Foss 		netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
34d9fe64e5SRobert Foss 			    index, ret);
35920a9fa2SPavel Skripkin 	}
36d9fe64e5SRobert Foss 
370bc69efbSMing Lei 	return ret;
38b4cdae20SChristian Riesch }
39b4cdae20SChristian Riesch 
40607740bcSChristian Riesch int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
41d9fe64e5SRobert Foss 		   u16 size, void *data, int in_pm)
42b4cdae20SChristian Riesch {
43d9fe64e5SRobert Foss 	int ret;
44d9fe64e5SRobert Foss 	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
45d9fe64e5SRobert Foss 
46d9fe64e5SRobert Foss 	BUG_ON(!dev);
47d9fe64e5SRobert Foss 
48d9fe64e5SRobert Foss 	if (!in_pm)
49d9fe64e5SRobert Foss 		fn = usbnet_write_cmd;
50d9fe64e5SRobert Foss 	else
51d9fe64e5SRobert Foss 		fn = usbnet_write_cmd_nopm;
52d9fe64e5SRobert Foss 
53d9fe64e5SRobert Foss 	ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
540bc69efbSMing Lei 		 value, index, data, size);
55d9fe64e5SRobert Foss 
56d9fe64e5SRobert Foss 	if (unlikely(ret < 0))
57d9fe64e5SRobert Foss 		netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
58d9fe64e5SRobert Foss 			    index, ret);
59d9fe64e5SRobert Foss 
60d9fe64e5SRobert Foss 	return ret;
61b4cdae20SChristian Riesch }
62b4cdae20SChristian Riesch 
63607740bcSChristian Riesch void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
64b4cdae20SChristian Riesch 			  u16 size, void *data)
65b4cdae20SChristian Riesch {
660bc69efbSMing Lei 	usbnet_write_cmd_async(dev, cmd,
670bc69efbSMing Lei 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
680bc69efbSMing Lei 			       value, index, data, size);
69b4cdae20SChristian Riesch }
70b4cdae20SChristian Riesch 
71a786e319SPavel Skripkin static int asix_check_host_enable(struct usbnet *dev, int in_pm)
72a786e319SPavel Skripkin {
73a786e319SPavel Skripkin 	int i, ret;
74a786e319SPavel Skripkin 	u8 smsr;
75a786e319SPavel Skripkin 
76d1652b70SPavel Skripkin 	for (i = 0; i < AX_HOST_EN_RETRIES; ++i) {
77a786e319SPavel Skripkin 		ret = asix_set_sw_mii(dev, in_pm);
78a786e319SPavel Skripkin 		if (ret == -ENODEV || ret == -ETIMEDOUT)
79a786e319SPavel Skripkin 			break;
80a786e319SPavel Skripkin 		usleep_range(1000, 1100);
81a786e319SPavel Skripkin 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
82a786e319SPavel Skripkin 				    0, 0, 1, &smsr, in_pm);
83a786e319SPavel Skripkin 		if (ret == -ENODEV)
84a786e319SPavel Skripkin 			break;
85920a9fa2SPavel Skripkin 		else if (ret < 0)
86a786e319SPavel Skripkin 			continue;
87a786e319SPavel Skripkin 		else if (smsr & AX_HOST_EN)
88a786e319SPavel Skripkin 			break;
89a786e319SPavel Skripkin 	}
90a786e319SPavel Skripkin 
91d1652b70SPavel Skripkin 	return i >= AX_HOST_EN_RETRIES ? -ETIMEDOUT : ret;
92a786e319SPavel Skripkin }
93a786e319SPavel Skripkin 
94960eb4eeSDean Jenkins static void reset_asix_rx_fixup_info(struct asix_rx_fixup_info *rx)
95960eb4eeSDean Jenkins {
96960eb4eeSDean Jenkins 	/* Reset the variables that have a lifetime outside of
97960eb4eeSDean Jenkins 	 * asix_rx_fixup_internal() so that future processing starts from a
98960eb4eeSDean Jenkins 	 * known set of initial conditions.
99960eb4eeSDean Jenkins 	 */
100960eb4eeSDean Jenkins 
101960eb4eeSDean Jenkins 	if (rx->ax_skb) {
102960eb4eeSDean Jenkins 		/* Discard any incomplete Ethernet frame in the netdev buffer */
103960eb4eeSDean Jenkins 		kfree_skb(rx->ax_skb);
104960eb4eeSDean Jenkins 		rx->ax_skb = NULL;
105960eb4eeSDean Jenkins 	}
106960eb4eeSDean Jenkins 
107960eb4eeSDean Jenkins 	/* Assume the Data header 32-bit word is at the start of the current
108960eb4eeSDean Jenkins 	 * or next URB socket buffer so reset all the state variables.
109960eb4eeSDean Jenkins 	 */
110960eb4eeSDean Jenkins 	rx->remaining = 0;
111960eb4eeSDean Jenkins 	rx->split_head = false;
112960eb4eeSDean Jenkins 	rx->header = 0;
113960eb4eeSDean Jenkins }
114960eb4eeSDean Jenkins 
1158b5b6f54SLucas Stach int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
1168b5b6f54SLucas Stach 			   struct asix_rx_fixup_info *rx)
117b4cdae20SChristian Riesch {
118b4cdae20SChristian Riesch 	int offset = 0;
1197b0378f5SDean Jenkins 	u16 size;
120b4cdae20SChristian Riesch 
1213f30b158SDean Jenkins 	/* When an Ethernet frame spans multiple URB socket buffers,
1223f30b158SDean Jenkins 	 * do a sanity test for the Data header synchronisation.
1233f30b158SDean Jenkins 	 * Attempt to detect the situation of the previous socket buffer having
1243f30b158SDean Jenkins 	 * been truncated or a socket buffer was missing. These situations
1253f30b158SDean Jenkins 	 * cause a discontinuity in the data stream and therefore need to avoid
1263f30b158SDean Jenkins 	 * appending bad data to the end of the current netdev socket buffer.
1273f30b158SDean Jenkins 	 * Also avoid unnecessarily discarding a good current netdev socket
1283f30b158SDean Jenkins 	 * buffer.
1293f30b158SDean Jenkins 	 */
1303f30b158SDean Jenkins 	if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
131cd9e2e5dSJohn Stultz 		offset = ((rx->remaining + 1) & 0xfffe);
1323f30b158SDean Jenkins 		rx->header = get_unaligned_le32(skb->data + offset);
1333f30b158SDean Jenkins 		offset = 0;
1343f30b158SDean Jenkins 
1353f30b158SDean Jenkins 		size = (u16)(rx->header & 0x7ff);
1363f30b158SDean Jenkins 		if (size != ((~rx->header >> 16) & 0x7ff)) {
1373f30b158SDean Jenkins 			netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
1383f30b158SDean Jenkins 				   rx->remaining);
139960eb4eeSDean Jenkins 			reset_asix_rx_fixup_info(rx);
1403f30b158SDean Jenkins 		}
1413f30b158SDean Jenkins 	}
1423f30b158SDean Jenkins 
1438b5b6f54SLucas Stach 	while (offset + sizeof(u16) <= skb->len) {
1447b0378f5SDean Jenkins 		u16 copy_length;
145b4cdae20SChristian Riesch 
1467b0378f5SDean Jenkins 		if (!rx->remaining) {
1473bfc69abSDean Jenkins 			if (skb->len - offset == sizeof(u16)) {
1488b5b6f54SLucas Stach 				rx->header = get_unaligned_le16(
1498b5b6f54SLucas Stach 						skb->data + offset);
1508b5b6f54SLucas Stach 				rx->split_head = true;
1518b5b6f54SLucas Stach 				offset += sizeof(u16);
1528b5b6f54SLucas Stach 				break;
1533bfc69abSDean Jenkins 			}
1543bfc69abSDean Jenkins 
1553bfc69abSDean Jenkins 			if (rx->split_head == true) {
1568b5b6f54SLucas Stach 				rx->header |= (get_unaligned_le16(
1573bfc69abSDean Jenkins 						skb->data + offset) << 16);
1588b5b6f54SLucas Stach 				rx->split_head = false;
1598b5b6f54SLucas Stach 				offset += sizeof(u16);
1608b5b6f54SLucas Stach 			} else {
1618b5b6f54SLucas Stach 				rx->header = get_unaligned_le32(skb->data +
1628b5b6f54SLucas Stach 								offset);
163b4cdae20SChristian Riesch 				offset += sizeof(u32);
1648b5b6f54SLucas Stach 			}
165b4cdae20SChristian Riesch 
1667b0378f5SDean Jenkins 			/* take frame length from Data header 32-bit word */
1677b0378f5SDean Jenkins 			size = (u16)(rx->header & 0x7ff);
1687b0378f5SDean Jenkins 			if (size != ((~rx->header >> 16) & 0x7ff)) {
1698b5b6f54SLucas Stach 				netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
1708b5b6f54SLucas Stach 					   rx->header, offset);
171960eb4eeSDean Jenkins 				reset_asix_rx_fixup_info(rx);
1728b5b6f54SLucas Stach 				return 0;
1738b5b6f54SLucas Stach 			}
1749a5ccd8eSDean Jenkins 			if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
175b70183dbSstephen hemminger 				netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
1769a5ccd8eSDean Jenkins 					   size);
177960eb4eeSDean Jenkins 				reset_asix_rx_fixup_info(rx);
1789a5ccd8eSDean Jenkins 				return 0;
1799a5ccd8eSDean Jenkins 			}
1809a5ccd8eSDean Jenkins 
1816a570814SDean Jenkins 			/* Sometimes may fail to get a netdev socket buffer but
1826a570814SDean Jenkins 			 * continue to process the URB socket buffer so that
1836a570814SDean Jenkins 			 * synchronisation of the Ethernet frame Data header
1846a570814SDean Jenkins 			 * word is maintained.
1856a570814SDean Jenkins 			 */
1867b0378f5SDean Jenkins 			rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
187b4cdae20SChristian Riesch 
1889a5ccd8eSDean Jenkins 			rx->remaining = size;
189b4cdae20SChristian Riesch 		}
190b4cdae20SChristian Riesch 
1917b0378f5SDean Jenkins 		if (rx->remaining > skb->len - offset) {
1927b0378f5SDean Jenkins 			copy_length = skb->len - offset;
1937b0378f5SDean Jenkins 			rx->remaining -= copy_length;
1947b0378f5SDean Jenkins 		} else {
1957b0378f5SDean Jenkins 			copy_length = rx->remaining;
1967b0378f5SDean Jenkins 			rx->remaining = 0;
1978b5b6f54SLucas Stach 		}
198b4cdae20SChristian Riesch 
1996a570814SDean Jenkins 		if (rx->ax_skb) {
200b952f4dfSyuan linyu 			skb_put_data(rx->ax_skb, skb->data + offset,
20159ae1d12SJohannes Berg 				     copy_length);
20222889dbbSDean Jenkins 			if (!rx->remaining) {
2038b5b6f54SLucas Stach 				usbnet_skb_return(dev, rx->ax_skb);
20422889dbbSDean Jenkins 				rx->ax_skb = NULL;
20522889dbbSDean Jenkins 			}
2066a570814SDean Jenkins 		}
2078b5b6f54SLucas Stach 
2087b0378f5SDean Jenkins 		offset += (copy_length + 1) & 0xfffe;
209b4cdae20SChristian Riesch 	}
210b4cdae20SChristian Riesch 
211b4cdae20SChristian Riesch 	if (skb->len != offset) {
2128b5b6f54SLucas Stach 		netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
2138b5b6f54SLucas Stach 			   skb->len, offset);
214960eb4eeSDean Jenkins 		reset_asix_rx_fixup_info(rx);
215b4cdae20SChristian Riesch 		return 0;
216b4cdae20SChristian Riesch 	}
2178b5b6f54SLucas Stach 
218b4cdae20SChristian Riesch 	return 1;
219b4cdae20SChristian Riesch }
220b4cdae20SChristian Riesch 
2218b5b6f54SLucas Stach int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
2228b5b6f54SLucas Stach {
2238b5b6f54SLucas Stach 	struct asix_common_private *dp = dev->driver_priv;
2248b5b6f54SLucas Stach 	struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
2258b5b6f54SLucas Stach 
2268b5b6f54SLucas Stach 	return asix_rx_fixup_internal(dev, skb, rx);
2278b5b6f54SLucas Stach }
2288b5b6f54SLucas Stach 
229d0c8f338SDean Jenkins void asix_rx_fixup_common_free(struct asix_common_private *dp)
230d0c8f338SDean Jenkins {
231d0c8f338SDean Jenkins 	struct asix_rx_fixup_info *rx;
232d0c8f338SDean Jenkins 
233d0c8f338SDean Jenkins 	if (!dp)
234d0c8f338SDean Jenkins 		return;
235d0c8f338SDean Jenkins 
236d0c8f338SDean Jenkins 	rx = &dp->rx_fixup_info;
237d0c8f338SDean Jenkins 
238d0c8f338SDean Jenkins 	if (rx->ax_skb) {
239d0c8f338SDean Jenkins 		kfree_skb(rx->ax_skb);
240d0c8f338SDean Jenkins 		rx->ax_skb = NULL;
241d0c8f338SDean Jenkins 	}
242d0c8f338SDean Jenkins }
243d0c8f338SDean Jenkins 
244607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
245b4cdae20SChristian Riesch 			      gfp_t flags)
246b4cdae20SChristian Riesch {
247b4cdae20SChristian Riesch 	int padlen;
248b4cdae20SChristian Riesch 	int headroom = skb_headroom(skb);
249b4cdae20SChristian Riesch 	int tailroom = skb_tailroom(skb);
250b4cdae20SChristian Riesch 	u32 packet_len;
251b4cdae20SChristian Riesch 	u32 padbytes = 0xffff0000;
2527e24b4edSChuhong Yuan 	void *ptr;
253b4cdae20SChristian Riesch 
254b4cdae20SChristian Riesch 	padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
255b4cdae20SChristian Riesch 
256b4cdae20SChristian Riesch 	/* We need to push 4 bytes in front of frame (packet_len)
257b4cdae20SChristian Riesch 	 * and maybe add 4 bytes after the end (if padlen is 4)
258b4cdae20SChristian Riesch 	 *
259b4cdae20SChristian Riesch 	 * Avoid skb_copy_expand() expensive call, using following rules :
260b4cdae20SChristian Riesch 	 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
261b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of headroom)
262b4cdae20SChristian Riesch 	 * - We are allowed to put 4 bytes at tail if skb_cloned()
263b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of tailroom)
264b4cdae20SChristian Riesch 	 *
265242c1a28SGao Feng 	 * TCP packets for example are cloned, but __skb_header_release()
266b4cdae20SChristian Riesch 	 * was called in tcp stack, allowing us to use headroom for our needs.
267b4cdae20SChristian Riesch 	 */
268b4cdae20SChristian Riesch 	if (!skb_header_cloned(skb) &&
269b4cdae20SChristian Riesch 	    !(padlen && skb_cloned(skb)) &&
270b4cdae20SChristian Riesch 	    headroom + tailroom >= 4 + padlen) {
271b4cdae20SChristian Riesch 		/* following should not happen, but better be safe */
272b4cdae20SChristian Riesch 		if (headroom < 4 ||
273b4cdae20SChristian Riesch 		    tailroom < padlen) {
274b4cdae20SChristian Riesch 			skb->data = memmove(skb->head + 4, skb->data, skb->len);
275b4cdae20SChristian Riesch 			skb_set_tail_pointer(skb, skb->len);
276b4cdae20SChristian Riesch 		}
277b4cdae20SChristian Riesch 	} else {
278b4cdae20SChristian Riesch 		struct sk_buff *skb2;
279b4cdae20SChristian Riesch 
280b4cdae20SChristian Riesch 		skb2 = skb_copy_expand(skb, 4, padlen, flags);
281b4cdae20SChristian Riesch 		dev_kfree_skb_any(skb);
282b4cdae20SChristian Riesch 		skb = skb2;
283b4cdae20SChristian Riesch 		if (!skb)
284b4cdae20SChristian Riesch 			return NULL;
285b4cdae20SChristian Riesch 	}
286b4cdae20SChristian Riesch 
287b4cdae20SChristian Riesch 	packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
2887e24b4edSChuhong Yuan 	ptr = skb_push(skb, 4);
2897e24b4edSChuhong Yuan 	put_unaligned_le32(packet_len, ptr);
290b4cdae20SChristian Riesch 
291b4cdae20SChristian Riesch 	if (padlen) {
2927e24b4edSChuhong Yuan 		put_unaligned_le32(padbytes, skb_tail_pointer(skb));
293b4cdae20SChristian Riesch 		skb_put(skb, sizeof(padbytes));
294b4cdae20SChristian Riesch 	}
2951e9e39f4SBen Hutchings 
2967a1e890eSBen Hutchings 	usbnet_set_skb_tx_stats(skb, 1, 0);
297b4cdae20SChristian Riesch 	return skb;
298b4cdae20SChristian Riesch }
299b4cdae20SChristian Riesch 
300d9fe64e5SRobert Foss int asix_set_sw_mii(struct usbnet *dev, int in_pm)
301b4cdae20SChristian Riesch {
302b4cdae20SChristian Riesch 	int ret;
303d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm);
304d9fe64e5SRobert Foss 
305b4cdae20SChristian Riesch 	if (ret < 0)
306b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable software MII access\n");
307b4cdae20SChristian Riesch 	return ret;
308b4cdae20SChristian Riesch }
309b4cdae20SChristian Riesch 
310d9fe64e5SRobert Foss int asix_set_hw_mii(struct usbnet *dev, int in_pm)
311b4cdae20SChristian Riesch {
312b4cdae20SChristian Riesch 	int ret;
313d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm);
314b4cdae20SChristian Riesch 	if (ret < 0)
315b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable hardware MII access\n");
316b4cdae20SChristian Riesch 	return ret;
317b4cdae20SChristian Riesch }
318b4cdae20SChristian Riesch 
3197e88b11aSOleksij Rempel int asix_read_phy_addr(struct usbnet *dev, bool internal)
320b4cdae20SChristian Riesch {
3217e88b11aSOleksij Rempel 	int ret, offset;
322b4cdae20SChristian Riesch 	u8 buf[2];
323b4cdae20SChristian Riesch 
3247e88b11aSOleksij Rempel 	ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0);
3257e88b11aSOleksij Rempel 	if (ret < 0)
3267e88b11aSOleksij Rempel 		goto error;
327b4cdae20SChristian Riesch 
328a092b723SHimadri Pandya 	if (ret < 2) {
3297e88b11aSOleksij Rempel 		ret = -EIO;
3307e88b11aSOleksij Rempel 		goto error;
331b4cdae20SChristian Riesch 	}
3327e88b11aSOleksij Rempel 
3337e88b11aSOleksij Rempel 	offset = (internal ? 1 : 0);
33416626b0cSChristian Riesch 	ret = buf[offset];
335b4cdae20SChristian Riesch 
3367e88b11aSOleksij Rempel 	netdev_dbg(dev->net, "%s PHY address 0x%x\n",
3377e88b11aSOleksij Rempel 		   internal ? "internal" : "external", ret);
3387e88b11aSOleksij Rempel 
3397e88b11aSOleksij Rempel 	return ret;
3407e88b11aSOleksij Rempel 
3417e88b11aSOleksij Rempel error:
3427e88b11aSOleksij Rempel 	netdev_err(dev->net, "Error reading PHY_ID register: %02x\n", ret);
3437e88b11aSOleksij Rempel 
344b4cdae20SChristian Riesch 	return ret;
345b4cdae20SChristian Riesch }
346b4cdae20SChristian Riesch 
347d9fe64e5SRobert Foss int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
348b4cdae20SChristian Riesch {
349b4cdae20SChristian Riesch 	int ret;
350b4cdae20SChristian Riesch 
351d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
352b4cdae20SChristian Riesch 	if (ret < 0)
353b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
354b4cdae20SChristian Riesch 
355b4cdae20SChristian Riesch 	return ret;
356b4cdae20SChristian Riesch }
357b4cdae20SChristian Riesch 
358d9fe64e5SRobert Foss u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
359b4cdae20SChristian Riesch {
360b4cdae20SChristian Riesch 	__le16 v;
361d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
362b4cdae20SChristian Riesch 
363b4cdae20SChristian Riesch 	if (ret < 0) {
364b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
365b4cdae20SChristian Riesch 		goto out;
366b4cdae20SChristian Riesch 	}
367b4cdae20SChristian Riesch 	ret = le16_to_cpu(v);
368b4cdae20SChristian Riesch out:
369b4cdae20SChristian Riesch 	return ret;
370b4cdae20SChristian Riesch }
371b4cdae20SChristian Riesch 
372d9fe64e5SRobert Foss int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
373b4cdae20SChristian Riesch {
374b4cdae20SChristian Riesch 	int ret;
375b4cdae20SChristian Riesch 
376b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
377d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
378b4cdae20SChristian Riesch 	if (ret < 0)
379b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
380b4cdae20SChristian Riesch 			   mode, ret);
381b4cdae20SChristian Riesch 
382b4cdae20SChristian Riesch 	return ret;
383b4cdae20SChristian Riesch }
384b4cdae20SChristian Riesch 
385d9fe64e5SRobert Foss u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
386b4cdae20SChristian Riesch {
387b4cdae20SChristian Riesch 	__le16 v;
388d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
389d9fe64e5SRobert Foss 				0, 0, 2, &v, in_pm);
390b4cdae20SChristian Riesch 
391b4cdae20SChristian Riesch 	if (ret < 0) {
392b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
393b4cdae20SChristian Riesch 			   ret);
394b4cdae20SChristian Riesch 		return ret;	/* TODO: callers not checking for error ret */
395b4cdae20SChristian Riesch 	}
396b4cdae20SChristian Riesch 
397b4cdae20SChristian Riesch 	return le16_to_cpu(v);
398b4cdae20SChristian Riesch 
399b4cdae20SChristian Riesch }
400b4cdae20SChristian Riesch 
401d9fe64e5SRobert Foss int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
402b4cdae20SChristian Riesch {
403b4cdae20SChristian Riesch 	int ret;
404b4cdae20SChristian Riesch 
405b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
406d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
407d9fe64e5SRobert Foss 			     mode, 0, 0, NULL, in_pm);
408b4cdae20SChristian Riesch 	if (ret < 0)
409b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
410b4cdae20SChristian Riesch 			   mode, ret);
411b4cdae20SChristian Riesch 
412b4cdae20SChristian Riesch 	return ret;
413b4cdae20SChristian Riesch }
414b4cdae20SChristian Riesch 
415e532a096SOleksij Rempel /* set MAC link settings according to information from phylib */
416e532a096SOleksij Rempel void asix_adjust_link(struct net_device *netdev)
417e532a096SOleksij Rempel {
418e532a096SOleksij Rempel 	struct phy_device *phydev = netdev->phydev;
419e532a096SOleksij Rempel 	struct usbnet *dev = netdev_priv(netdev);
420e532a096SOleksij Rempel 	u16 mode = 0;
421e532a096SOleksij Rempel 
422e532a096SOleksij Rempel 	if (phydev->link) {
423e532a096SOleksij Rempel 		mode = AX88772_MEDIUM_DEFAULT;
424e532a096SOleksij Rempel 
425e532a096SOleksij Rempel 		if (phydev->duplex == DUPLEX_HALF)
426e532a096SOleksij Rempel 			mode &= ~AX_MEDIUM_FD;
427e532a096SOleksij Rempel 
428e532a096SOleksij Rempel 		if (phydev->speed != SPEED_100)
429e532a096SOleksij Rempel 			mode &= ~AX_MEDIUM_PS;
430e532a096SOleksij Rempel 	}
431e532a096SOleksij Rempel 
432e532a096SOleksij Rempel 	asix_write_medium_mode(dev, mode, 0);
433e532a096SOleksij Rempel 	phy_print_status(phydev);
434*805206e6SOleksij Rempel 	usbnet_link_change(dev, phydev->link, 0);
435e532a096SOleksij Rempel }
436e532a096SOleksij Rempel 
437d9fe64e5SRobert Foss int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
438b4cdae20SChristian Riesch {
439b4cdae20SChristian Riesch 	int ret;
440b4cdae20SChristian Riesch 
441b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
442d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
443b4cdae20SChristian Riesch 	if (ret < 0)
444b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
445b4cdae20SChristian Riesch 			   value, ret);
446b4cdae20SChristian Riesch 
447b4cdae20SChristian Riesch 	if (sleep)
448b4cdae20SChristian Riesch 		msleep(sleep);
449b4cdae20SChristian Riesch 
450b4cdae20SChristian Riesch 	return ret;
451b4cdae20SChristian Riesch }
452b4cdae20SChristian Riesch 
453b4cdae20SChristian Riesch /*
454b4cdae20SChristian Riesch  * AX88772 & AX88178 have a 16-bit RX_CTL value
455b4cdae20SChristian Riesch  */
456607740bcSChristian Riesch void asix_set_multicast(struct net_device *net)
457b4cdae20SChristian Riesch {
458b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
459b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
460b4cdae20SChristian Riesch 	u16 rx_ctl = AX_DEFAULT_RX_CTL;
461b4cdae20SChristian Riesch 
462b4cdae20SChristian Riesch 	if (net->flags & IFF_PROMISC) {
463b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_PRO;
464b4cdae20SChristian Riesch 	} else if (net->flags & IFF_ALLMULTI ||
465b4cdae20SChristian Riesch 		   netdev_mc_count(net) > AX_MAX_MCAST) {
466b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AMALL;
467b4cdae20SChristian Riesch 	} else if (netdev_mc_empty(net)) {
468b4cdae20SChristian Riesch 		/* just broadcast and directed */
469b4cdae20SChristian Riesch 	} else {
470b4cdae20SChristian Riesch 		/* We use the 20 byte dev->data
471b4cdae20SChristian Riesch 		 * for our 8 byte filter buffer
472b4cdae20SChristian Riesch 		 * to avoid allocating memory that
473b4cdae20SChristian Riesch 		 * is tricky to free later */
474b4cdae20SChristian Riesch 		struct netdev_hw_addr *ha;
475b4cdae20SChristian Riesch 		u32 crc_bits;
476b4cdae20SChristian Riesch 
477b4cdae20SChristian Riesch 		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
478b4cdae20SChristian Riesch 
479b4cdae20SChristian Riesch 		/* Build the multicast hash filter. */
480b4cdae20SChristian Riesch 		netdev_for_each_mc_addr(ha, net) {
481b4cdae20SChristian Riesch 			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
482b4cdae20SChristian Riesch 			data->multi_filter[crc_bits >> 3] |=
483b4cdae20SChristian Riesch 			    1 << (crc_bits & 7);
484b4cdae20SChristian Riesch 		}
485b4cdae20SChristian Riesch 
486b4cdae20SChristian Riesch 		asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
487b4cdae20SChristian Riesch 				   AX_MCAST_FILTER_SIZE, data->multi_filter);
488b4cdae20SChristian Riesch 
489b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AM;
490b4cdae20SChristian Riesch 	}
491b4cdae20SChristian Riesch 
492b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
493b4cdae20SChristian Riesch }
494b4cdae20SChristian Riesch 
49589183b6eSOleksij Rempel static int __asix_mdio_read(struct net_device *netdev, int phy_id, int loc,
49689183b6eSOleksij Rempel 			    bool in_pm)
497b4cdae20SChristian Riesch {
498b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
499b4cdae20SChristian Riesch 	__le16 res;
5008a46f665SRobert Foss 	int ret;
501b4cdae20SChristian Riesch 
502b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
503a786e319SPavel Skripkin 
50489183b6eSOleksij Rempel 	ret = asix_check_host_enable(dev, in_pm);
505610df1d2SGuenter Roeck 	if (ret == -ENODEV || ret == -ETIMEDOUT) {
5068a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
5078a46f665SRobert Foss 		return ret;
5088a46f665SRobert Foss 	}
509d9fe64e5SRobert Foss 
510d275afb6SOleksij Rempel 	ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2,
51189183b6eSOleksij Rempel 			    &res, in_pm);
512d275afb6SOleksij Rempel 	if (ret < 0)
513d275afb6SOleksij Rempel 		goto out;
514d275afb6SOleksij Rempel 
51589183b6eSOleksij Rempel 	ret = asix_set_hw_mii(dev, in_pm);
516d275afb6SOleksij Rempel out:
517b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
518b4cdae20SChristian Riesch 
519b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
520b4cdae20SChristian Riesch 			phy_id, loc, le16_to_cpu(res));
521b4cdae20SChristian Riesch 
522d275afb6SOleksij Rempel 	return ret < 0 ? ret : le16_to_cpu(res);
523b4cdae20SChristian Riesch }
524b4cdae20SChristian Riesch 
52589183b6eSOleksij Rempel int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
52689183b6eSOleksij Rempel {
52789183b6eSOleksij Rempel 	return __asix_mdio_read(netdev, phy_id, loc, false);
52889183b6eSOleksij Rempel }
52989183b6eSOleksij Rempel 
530d275afb6SOleksij Rempel static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc,
53189183b6eSOleksij Rempel 			     int val, bool in_pm)
532b4cdae20SChristian Riesch {
533b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
534b4cdae20SChristian Riesch 	__le16 res = cpu_to_le16(val);
5358a46f665SRobert Foss 	int ret;
536b4cdae20SChristian Riesch 
537b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
538b4cdae20SChristian Riesch 			phy_id, loc, val);
539d9fe64e5SRobert Foss 
540b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
541d275afb6SOleksij Rempel 
54289183b6eSOleksij Rempel 	ret = asix_check_host_enable(dev, in_pm);
543d275afb6SOleksij Rempel 	if (ret == -ENODEV)
544d275afb6SOleksij Rempel 		goto out;
545d275afb6SOleksij Rempel 
546d275afb6SOleksij Rempel 	ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2,
54789183b6eSOleksij Rempel 			     &res, in_pm);
548d275afb6SOleksij Rempel 	if (ret < 0)
549d275afb6SOleksij Rempel 		goto out;
550d275afb6SOleksij Rempel 
55189183b6eSOleksij Rempel 	ret = asix_set_hw_mii(dev, in_pm);
552d275afb6SOleksij Rempel out:
5538a46f665SRobert Foss 	mutex_unlock(&dev->phy_mutex);
554d275afb6SOleksij Rempel 
555d275afb6SOleksij Rempel 	return ret < 0 ? ret : 0;
5568a46f665SRobert Foss }
557d9fe64e5SRobert Foss 
558d275afb6SOleksij Rempel void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
559d275afb6SOleksij Rempel {
56089183b6eSOleksij Rempel 	__asix_mdio_write(netdev, phy_id, loc, val, false);
561d9fe64e5SRobert Foss }
562d9fe64e5SRobert Foss 
563e532a096SOleksij Rempel /* MDIO read and write wrappers for phylib */
564e532a096SOleksij Rempel int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
565e532a096SOleksij Rempel {
566e532a096SOleksij Rempel 	struct usbnet *priv = bus->priv;
567e532a096SOleksij Rempel 
56889183b6eSOleksij Rempel 	return __asix_mdio_read(priv->net, phy_id, regnum, false);
569e532a096SOleksij Rempel }
570e532a096SOleksij Rempel 
571e532a096SOleksij Rempel int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
572e532a096SOleksij Rempel {
573e532a096SOleksij Rempel 	struct usbnet *priv = bus->priv;
574e532a096SOleksij Rempel 
57589183b6eSOleksij Rempel 	return __asix_mdio_write(priv->net, phy_id, regnum, val, false);
576e532a096SOleksij Rempel }
577e532a096SOleksij Rempel 
578d9fe64e5SRobert Foss int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
579d9fe64e5SRobert Foss {
58089183b6eSOleksij Rempel 	return __asix_mdio_read(netdev, phy_id, loc, true);
581d9fe64e5SRobert Foss }
582d9fe64e5SRobert Foss 
583d9fe64e5SRobert Foss void
584d9fe64e5SRobert Foss asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
585d9fe64e5SRobert Foss {
58689183b6eSOleksij Rempel 	__asix_mdio_write(netdev, phy_id, loc, val, true);
587b4cdae20SChristian Riesch }
588b4cdae20SChristian Riesch 
589607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
590b4cdae20SChristian Riesch {
591b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
592b4cdae20SChristian Riesch 	u8 opt;
593b4cdae20SChristian Riesch 
594d9fe64e5SRobert Foss 	if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
595d9fe64e5SRobert Foss 			  0, 0, 1, &opt, 0) < 0) {
596b4cdae20SChristian Riesch 		wolinfo->supported = 0;
597b4cdae20SChristian Riesch 		wolinfo->wolopts = 0;
598b4cdae20SChristian Riesch 		return;
599b4cdae20SChristian Riesch 	}
600b4cdae20SChristian Riesch 	wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
601b4cdae20SChristian Riesch 	wolinfo->wolopts = 0;
602b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_LINK)
603b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_PHY;
604b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_MAGIC)
605b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_MAGIC;
606b4cdae20SChristian Riesch }
607b4cdae20SChristian Riesch 
608607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
609b4cdae20SChristian Riesch {
610b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
611b4cdae20SChristian Riesch 	u8 opt = 0;
612b4cdae20SChristian Riesch 
613c4ce446eSFlorian Fainelli 	if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
614c4ce446eSFlorian Fainelli 		return -EINVAL;
615c4ce446eSFlorian Fainelli 
616b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_PHY)
617b4cdae20SChristian Riesch 		opt |= AX_MONITOR_LINK;
618b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_MAGIC)
619b4cdae20SChristian Riesch 		opt |= AX_MONITOR_MAGIC;
620b4cdae20SChristian Riesch 
621b4cdae20SChristian Riesch 	if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
622d9fe64e5SRobert Foss 			      opt, 0, 0, NULL, 0) < 0)
623b4cdae20SChristian Riesch 		return -EINVAL;
624b4cdae20SChristian Riesch 
625b4cdae20SChristian Riesch 	return 0;
626b4cdae20SChristian Riesch }
627b4cdae20SChristian Riesch 
628607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net)
629b4cdae20SChristian Riesch {
630ceb02c91SChristian Riesch 	return AX_EEPROM_LEN;
631b4cdae20SChristian Riesch }
632b4cdae20SChristian Riesch 
633607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
634607740bcSChristian Riesch 		    u8 *data)
635b4cdae20SChristian Riesch {
636b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
637ceb02c91SChristian Riesch 	u16 *eeprom_buff;
638ceb02c91SChristian Riesch 	int first_word, last_word;
639b4cdae20SChristian Riesch 	int i;
640b4cdae20SChristian Riesch 
641ceb02c91SChristian Riesch 	if (eeprom->len == 0)
642b4cdae20SChristian Riesch 		return -EINVAL;
643b4cdae20SChristian Riesch 
644b4cdae20SChristian Riesch 	eeprom->magic = AX_EEPROM_MAGIC;
645b4cdae20SChristian Riesch 
646ceb02c91SChristian Riesch 	first_word = eeprom->offset >> 1;
647ceb02c91SChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
648ceb02c91SChristian Riesch 
6496da2ec56SKees Cook 	eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
650ceb02c91SChristian Riesch 				    GFP_KERNEL);
651ceb02c91SChristian Riesch 	if (!eeprom_buff)
652ceb02c91SChristian Riesch 		return -ENOMEM;
653ceb02c91SChristian Riesch 
654b4cdae20SChristian Riesch 	/* ax8817x returns 2 bytes from eeprom on read */
655ceb02c91SChristian Riesch 	for (i = first_word; i <= last_word; i++) {
656ceb02c91SChristian Riesch 		if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
657d9fe64e5SRobert Foss 				  &eeprom_buff[i - first_word], 0) < 0) {
658ceb02c91SChristian Riesch 			kfree(eeprom_buff);
659ceb02c91SChristian Riesch 			return -EIO;
660b4cdae20SChristian Riesch 		}
661ceb02c91SChristian Riesch 	}
662ceb02c91SChristian Riesch 
663ceb02c91SChristian Riesch 	memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
664ceb02c91SChristian Riesch 	kfree(eeprom_buff);
665b4cdae20SChristian Riesch 	return 0;
666b4cdae20SChristian Riesch }
667b4cdae20SChristian Riesch 
668cb7b24cdSChristian Riesch int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
669cb7b24cdSChristian Riesch 		    u8 *data)
670cb7b24cdSChristian Riesch {
671cb7b24cdSChristian Riesch 	struct usbnet *dev = netdev_priv(net);
672cb7b24cdSChristian Riesch 	u16 *eeprom_buff;
673cb7b24cdSChristian Riesch 	int first_word, last_word;
674cb7b24cdSChristian Riesch 	int i;
675cb7b24cdSChristian Riesch 	int ret;
676cb7b24cdSChristian Riesch 
677cb7b24cdSChristian Riesch 	netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
678cb7b24cdSChristian Riesch 		   eeprom->len, eeprom->offset, eeprom->magic);
679cb7b24cdSChristian Riesch 
680cb7b24cdSChristian Riesch 	if (eeprom->len == 0)
681cb7b24cdSChristian Riesch 		return -EINVAL;
682cb7b24cdSChristian Riesch 
683cb7b24cdSChristian Riesch 	if (eeprom->magic != AX_EEPROM_MAGIC)
684cb7b24cdSChristian Riesch 		return -EINVAL;
685cb7b24cdSChristian Riesch 
686cb7b24cdSChristian Riesch 	first_word = eeprom->offset >> 1;
687cb7b24cdSChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
688cb7b24cdSChristian Riesch 
6896da2ec56SKees Cook 	eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
690cb7b24cdSChristian Riesch 				    GFP_KERNEL);
691cb7b24cdSChristian Riesch 	if (!eeprom_buff)
692cb7b24cdSChristian Riesch 		return -ENOMEM;
693cb7b24cdSChristian Riesch 
694cb7b24cdSChristian Riesch 	/* align data to 16 bit boundaries, read the missing data from
695cb7b24cdSChristian Riesch 	   the EEPROM */
696cb7b24cdSChristian Riesch 	if (eeprom->offset & 1) {
697cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
698d9fe64e5SRobert Foss 				    &eeprom_buff[0], 0);
699cb7b24cdSChristian Riesch 		if (ret < 0) {
700cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
701cb7b24cdSChristian Riesch 			goto free;
702cb7b24cdSChristian Riesch 		}
703cb7b24cdSChristian Riesch 	}
704cb7b24cdSChristian Riesch 
705cb7b24cdSChristian Riesch 	if ((eeprom->offset + eeprom->len) & 1) {
706cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
707d9fe64e5SRobert Foss 				    &eeprom_buff[last_word - first_word], 0);
708cb7b24cdSChristian Riesch 		if (ret < 0) {
709cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
710cb7b24cdSChristian Riesch 			goto free;
711cb7b24cdSChristian Riesch 		}
712cb7b24cdSChristian Riesch 	}
713cb7b24cdSChristian Riesch 
714cb7b24cdSChristian Riesch 	memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
715cb7b24cdSChristian Riesch 
716cb7b24cdSChristian Riesch 	/* write data to EEPROM */
717d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
718cb7b24cdSChristian Riesch 	if (ret < 0) {
719cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to enable EEPROM write\n");
720cb7b24cdSChristian Riesch 		goto free;
721cb7b24cdSChristian Riesch 	}
722cb7b24cdSChristian Riesch 	msleep(20);
723cb7b24cdSChristian Riesch 
724cb7b24cdSChristian Riesch 	for (i = first_word; i <= last_word; i++) {
725cb7b24cdSChristian Riesch 		netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
726cb7b24cdSChristian Riesch 			   i, eeprom_buff[i - first_word]);
727cb7b24cdSChristian Riesch 		ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
728d9fe64e5SRobert Foss 				     eeprom_buff[i - first_word], 0, NULL, 0);
729cb7b24cdSChristian Riesch 		if (ret < 0) {
730cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
731cb7b24cdSChristian Riesch 				   i);
732cb7b24cdSChristian Riesch 			goto free;
733cb7b24cdSChristian Riesch 		}
734cb7b24cdSChristian Riesch 		msleep(20);
735cb7b24cdSChristian Riesch 	}
736cb7b24cdSChristian Riesch 
737d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
738cb7b24cdSChristian Riesch 	if (ret < 0) {
739cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to disable EEPROM write\n");
740cb7b24cdSChristian Riesch 		goto free;
741cb7b24cdSChristian Riesch 	}
742cb7b24cdSChristian Riesch 
743cb7b24cdSChristian Riesch 	ret = 0;
744cb7b24cdSChristian Riesch free:
745cb7b24cdSChristian Riesch 	kfree(eeprom_buff);
746cb7b24cdSChristian Riesch 	return ret;
747cb7b24cdSChristian Riesch }
748cb7b24cdSChristian Riesch 
749607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
750b4cdae20SChristian Riesch {
751b4cdae20SChristian Riesch 	/* Inherit standard device info */
752b4cdae20SChristian Riesch 	usbnet_get_drvinfo(net, info);
7537826d43fSJiri Pirko 	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
7547826d43fSJiri Pirko 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
755b4cdae20SChristian Riesch }
756b4cdae20SChristian Riesch 
757607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p)
758b4cdae20SChristian Riesch {
759b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
760b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
761b4cdae20SChristian Riesch 	struct sockaddr *addr = p;
762b4cdae20SChristian Riesch 
763b4cdae20SChristian Riesch 	if (netif_running(net))
764b4cdae20SChristian Riesch 		return -EBUSY;
765b4cdae20SChristian Riesch 	if (!is_valid_ether_addr(addr->sa_data))
766b4cdae20SChristian Riesch 		return -EADDRNOTAVAIL;
767b4cdae20SChristian Riesch 
76816813717SJakub Kicinski 	eth_hw_addr_set(net, addr->sa_data);
769b4cdae20SChristian Riesch 
770b4cdae20SChristian Riesch 	/* We use the 20 byte dev->data
771b4cdae20SChristian Riesch 	 * for our 6 byte mac buffer
772b4cdae20SChristian Riesch 	 * to avoid allocating memory that
773b4cdae20SChristian Riesch 	 * is tricky to free later */
774b4cdae20SChristian Riesch 	memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
775b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
776b4cdae20SChristian Riesch 							data->mac_addr);
777b4cdae20SChristian Riesch 
778b4cdae20SChristian Riesch 	return 0;
779b4cdae20SChristian Riesch }
780