xref: /openbmc/linux/drivers/net/usb/asix_common.c (revision 59ae1d127ac0ae404baf414c434ba2651b793f46)
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;
1168b5b6f54SLucas Stach 		unsigned char *data;
117b4cdae20SChristian Riesch 
1187b0378f5SDean Jenkins 		if (!rx->remaining) {
1193bfc69abSDean Jenkins 			if (skb->len - offset == sizeof(u16)) {
1208b5b6f54SLucas Stach 				rx->header = get_unaligned_le16(
1218b5b6f54SLucas Stach 						skb->data + offset);
1228b5b6f54SLucas Stach 				rx->split_head = true;
1238b5b6f54SLucas Stach 				offset += sizeof(u16);
1248b5b6f54SLucas Stach 				break;
1253bfc69abSDean Jenkins 			}
1263bfc69abSDean Jenkins 
1273bfc69abSDean Jenkins 			if (rx->split_head == true) {
1288b5b6f54SLucas Stach 				rx->header |= (get_unaligned_le16(
1293bfc69abSDean Jenkins 						skb->data + offset) << 16);
1308b5b6f54SLucas Stach 				rx->split_head = false;
1318b5b6f54SLucas Stach 				offset += sizeof(u16);
1328b5b6f54SLucas Stach 			} else {
1338b5b6f54SLucas Stach 				rx->header = get_unaligned_le32(skb->data +
1348b5b6f54SLucas Stach 								offset);
135b4cdae20SChristian Riesch 				offset += sizeof(u32);
1368b5b6f54SLucas Stach 			}
137b4cdae20SChristian Riesch 
1387b0378f5SDean Jenkins 			/* take frame length from Data header 32-bit word */
1397b0378f5SDean Jenkins 			size = (u16)(rx->header & 0x7ff);
1407b0378f5SDean Jenkins 			if (size != ((~rx->header >> 16) & 0x7ff)) {
1418b5b6f54SLucas Stach 				netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
1428b5b6f54SLucas Stach 					   rx->header, offset);
1438b5b6f54SLucas Stach 				return 0;
1448b5b6f54SLucas Stach 			}
1459a5ccd8eSDean Jenkins 			if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
146b70183dbSstephen hemminger 				netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
1479a5ccd8eSDean Jenkins 					   size);
1489a5ccd8eSDean Jenkins 				return 0;
1499a5ccd8eSDean Jenkins 			}
1509a5ccd8eSDean Jenkins 
1516a570814SDean Jenkins 			/* Sometimes may fail to get a netdev socket buffer but
1526a570814SDean Jenkins 			 * continue to process the URB socket buffer so that
1536a570814SDean Jenkins 			 * synchronisation of the Ethernet frame Data header
1546a570814SDean Jenkins 			 * word is maintained.
1556a570814SDean Jenkins 			 */
1567b0378f5SDean Jenkins 			rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
157b4cdae20SChristian Riesch 
1589a5ccd8eSDean Jenkins 			rx->remaining = size;
159b4cdae20SChristian Riesch 		}
160b4cdae20SChristian Riesch 
1617b0378f5SDean Jenkins 		if (rx->remaining > skb->len - offset) {
1627b0378f5SDean Jenkins 			copy_length = skb->len - offset;
1637b0378f5SDean Jenkins 			rx->remaining -= copy_length;
1647b0378f5SDean Jenkins 		} else {
1657b0378f5SDean Jenkins 			copy_length = rx->remaining;
1667b0378f5SDean Jenkins 			rx->remaining = 0;
1678b5b6f54SLucas Stach 		}
168b4cdae20SChristian Riesch 
1696a570814SDean Jenkins 		if (rx->ax_skb) {
170*59ae1d12SJohannes Berg 			data = skb_put_data(rx->ax_skb, skb->data + offset,
171*59ae1d12SJohannes Berg 					    copy_length);
1727b0378f5SDean Jenkins 			if (!rx->remaining)
1738b5b6f54SLucas Stach 				usbnet_skb_return(dev, rx->ax_skb);
1746a570814SDean Jenkins 		}
1758b5b6f54SLucas Stach 
1767b0378f5SDean Jenkins 		offset += (copy_length + 1) & 0xfffe;
177b4cdae20SChristian Riesch 	}
178b4cdae20SChristian Riesch 
179b4cdae20SChristian Riesch 	if (skb->len != offset) {
1808b5b6f54SLucas Stach 		netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
1818b5b6f54SLucas Stach 			   skb->len, offset);
182b4cdae20SChristian Riesch 		return 0;
183b4cdae20SChristian Riesch 	}
1848b5b6f54SLucas Stach 
185b4cdae20SChristian Riesch 	return 1;
186b4cdae20SChristian Riesch }
187b4cdae20SChristian Riesch 
1888b5b6f54SLucas Stach int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
1898b5b6f54SLucas Stach {
1908b5b6f54SLucas Stach 	struct asix_common_private *dp = dev->driver_priv;
1918b5b6f54SLucas Stach 	struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
1928b5b6f54SLucas Stach 
1938b5b6f54SLucas Stach 	return asix_rx_fixup_internal(dev, skb, rx);
1948b5b6f54SLucas Stach }
1958b5b6f54SLucas Stach 
196607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
197b4cdae20SChristian Riesch 			      gfp_t flags)
198b4cdae20SChristian Riesch {
199b4cdae20SChristian Riesch 	int padlen;
200b4cdae20SChristian Riesch 	int headroom = skb_headroom(skb);
201b4cdae20SChristian Riesch 	int tailroom = skb_tailroom(skb);
202b4cdae20SChristian Riesch 	u32 packet_len;
203b4cdae20SChristian Riesch 	u32 padbytes = 0xffff0000;
204b4cdae20SChristian Riesch 
205b4cdae20SChristian Riesch 	padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
206b4cdae20SChristian Riesch 
207b4cdae20SChristian Riesch 	/* We need to push 4 bytes in front of frame (packet_len)
208b4cdae20SChristian Riesch 	 * and maybe add 4 bytes after the end (if padlen is 4)
209b4cdae20SChristian Riesch 	 *
210b4cdae20SChristian Riesch 	 * Avoid skb_copy_expand() expensive call, using following rules :
211b4cdae20SChristian Riesch 	 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
212b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of headroom)
213b4cdae20SChristian Riesch 	 * - We are allowed to put 4 bytes at tail if skb_cloned()
214b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of tailroom)
215b4cdae20SChristian Riesch 	 *
216b4cdae20SChristian Riesch 	 * TCP packets for example are cloned, but skb_header_release()
217b4cdae20SChristian Riesch 	 * was called in tcp stack, allowing us to use headroom for our needs.
218b4cdae20SChristian Riesch 	 */
219b4cdae20SChristian Riesch 	if (!skb_header_cloned(skb) &&
220b4cdae20SChristian Riesch 	    !(padlen && skb_cloned(skb)) &&
221b4cdae20SChristian Riesch 	    headroom + tailroom >= 4 + padlen) {
222b4cdae20SChristian Riesch 		/* following should not happen, but better be safe */
223b4cdae20SChristian Riesch 		if (headroom < 4 ||
224b4cdae20SChristian Riesch 		    tailroom < padlen) {
225b4cdae20SChristian Riesch 			skb->data = memmove(skb->head + 4, skb->data, skb->len);
226b4cdae20SChristian Riesch 			skb_set_tail_pointer(skb, skb->len);
227b4cdae20SChristian Riesch 		}
228b4cdae20SChristian Riesch 	} else {
229b4cdae20SChristian Riesch 		struct sk_buff *skb2;
230b4cdae20SChristian Riesch 
231b4cdae20SChristian Riesch 		skb2 = skb_copy_expand(skb, 4, padlen, flags);
232b4cdae20SChristian Riesch 		dev_kfree_skb_any(skb);
233b4cdae20SChristian Riesch 		skb = skb2;
234b4cdae20SChristian Riesch 		if (!skb)
235b4cdae20SChristian Riesch 			return NULL;
236b4cdae20SChristian Riesch 	}
237b4cdae20SChristian Riesch 
238b4cdae20SChristian Riesch 	packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
239b4cdae20SChristian Riesch 	skb_push(skb, 4);
240b4cdae20SChristian Riesch 	cpu_to_le32s(&packet_len);
241b4cdae20SChristian Riesch 	skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
242b4cdae20SChristian Riesch 
243b4cdae20SChristian Riesch 	if (padlen) {
244b4cdae20SChristian Riesch 		cpu_to_le32s(&padbytes);
245b4cdae20SChristian Riesch 		memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
246b4cdae20SChristian Riesch 		skb_put(skb, sizeof(padbytes));
247b4cdae20SChristian Riesch 	}
2481e9e39f4SBen Hutchings 
2497a1e890eSBen Hutchings 	usbnet_set_skb_tx_stats(skb, 1, 0);
250b4cdae20SChristian Riesch 	return skb;
251b4cdae20SChristian Riesch }
252b4cdae20SChristian Riesch 
253d9fe64e5SRobert Foss int asix_set_sw_mii(struct usbnet *dev, int in_pm)
254b4cdae20SChristian Riesch {
255b4cdae20SChristian Riesch 	int ret;
256d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm);
257d9fe64e5SRobert Foss 
258b4cdae20SChristian Riesch 	if (ret < 0)
259b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable software MII access\n");
260b4cdae20SChristian Riesch 	return ret;
261b4cdae20SChristian Riesch }
262b4cdae20SChristian Riesch 
263d9fe64e5SRobert Foss int asix_set_hw_mii(struct usbnet *dev, int in_pm)
264b4cdae20SChristian Riesch {
265b4cdae20SChristian Riesch 	int ret;
266d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm);
267b4cdae20SChristian Riesch 	if (ret < 0)
268b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable hardware MII access\n");
269b4cdae20SChristian Riesch 	return ret;
270b4cdae20SChristian Riesch }
271b4cdae20SChristian Riesch 
27216626b0cSChristian Riesch int asix_read_phy_addr(struct usbnet *dev, int internal)
273b4cdae20SChristian Riesch {
27416626b0cSChristian Riesch 	int offset = (internal ? 1 : 0);
275b4cdae20SChristian Riesch 	u8 buf[2];
276d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0);
277b4cdae20SChristian Riesch 
278b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr()\n");
279b4cdae20SChristian Riesch 
280b4cdae20SChristian Riesch 	if (ret < 0) {
281b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
282b4cdae20SChristian Riesch 		goto out;
283b4cdae20SChristian Riesch 	}
284b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
285b4cdae20SChristian Riesch 		   *((__le16 *)buf));
28616626b0cSChristian Riesch 	ret = buf[offset];
287b4cdae20SChristian Riesch 
288b4cdae20SChristian Riesch out:
289b4cdae20SChristian Riesch 	return ret;
290b4cdae20SChristian Riesch }
291b4cdae20SChristian Riesch 
29216626b0cSChristian Riesch int asix_get_phy_addr(struct usbnet *dev)
29316626b0cSChristian Riesch {
29416626b0cSChristian Riesch 	/* return the address of the internal phy */
29516626b0cSChristian Riesch 	return asix_read_phy_addr(dev, 1);
29616626b0cSChristian Riesch }
29716626b0cSChristian Riesch 
29816626b0cSChristian Riesch 
299d9fe64e5SRobert Foss int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
300b4cdae20SChristian Riesch {
301b4cdae20SChristian Riesch 	int ret;
302b4cdae20SChristian Riesch 
303d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
304b4cdae20SChristian Riesch 	if (ret < 0)
305b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
306b4cdae20SChristian Riesch 
307b4cdae20SChristian Riesch 	return ret;
308b4cdae20SChristian Riesch }
309b4cdae20SChristian Riesch 
310d9fe64e5SRobert Foss u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
311b4cdae20SChristian Riesch {
312b4cdae20SChristian Riesch 	__le16 v;
313d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
314b4cdae20SChristian Riesch 
315b4cdae20SChristian Riesch 	if (ret < 0) {
316b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
317b4cdae20SChristian Riesch 		goto out;
318b4cdae20SChristian Riesch 	}
319b4cdae20SChristian Riesch 	ret = le16_to_cpu(v);
320b4cdae20SChristian Riesch out:
321b4cdae20SChristian Riesch 	return ret;
322b4cdae20SChristian Riesch }
323b4cdae20SChristian Riesch 
324d9fe64e5SRobert Foss int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
325b4cdae20SChristian Riesch {
326b4cdae20SChristian Riesch 	int ret;
327b4cdae20SChristian Riesch 
328b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
329d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
330b4cdae20SChristian Riesch 	if (ret < 0)
331b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
332b4cdae20SChristian Riesch 			   mode, ret);
333b4cdae20SChristian Riesch 
334b4cdae20SChristian Riesch 	return ret;
335b4cdae20SChristian Riesch }
336b4cdae20SChristian Riesch 
337d9fe64e5SRobert Foss u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
338b4cdae20SChristian Riesch {
339b4cdae20SChristian Riesch 	__le16 v;
340d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
341d9fe64e5SRobert Foss 				0, 0, 2, &v, in_pm);
342b4cdae20SChristian Riesch 
343b4cdae20SChristian Riesch 	if (ret < 0) {
344b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
345b4cdae20SChristian Riesch 			   ret);
346b4cdae20SChristian Riesch 		return ret;	/* TODO: callers not checking for error ret */
347b4cdae20SChristian Riesch 	}
348b4cdae20SChristian Riesch 
349b4cdae20SChristian Riesch 	return le16_to_cpu(v);
350b4cdae20SChristian Riesch 
351b4cdae20SChristian Riesch }
352b4cdae20SChristian Riesch 
353d9fe64e5SRobert Foss int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
354b4cdae20SChristian Riesch {
355b4cdae20SChristian Riesch 	int ret;
356b4cdae20SChristian Riesch 
357b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
358d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
359d9fe64e5SRobert Foss 			     mode, 0, 0, NULL, in_pm);
360b4cdae20SChristian Riesch 	if (ret < 0)
361b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
362b4cdae20SChristian Riesch 			   mode, ret);
363b4cdae20SChristian Riesch 
364b4cdae20SChristian Riesch 	return ret;
365b4cdae20SChristian Riesch }
366b4cdae20SChristian Riesch 
367d9fe64e5SRobert Foss int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
368b4cdae20SChristian Riesch {
369b4cdae20SChristian Riesch 	int ret;
370b4cdae20SChristian Riesch 
371b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
372d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
373b4cdae20SChristian Riesch 	if (ret < 0)
374b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
375b4cdae20SChristian Riesch 			   value, ret);
376b4cdae20SChristian Riesch 
377b4cdae20SChristian Riesch 	if (sleep)
378b4cdae20SChristian Riesch 		msleep(sleep);
379b4cdae20SChristian Riesch 
380b4cdae20SChristian Riesch 	return ret;
381b4cdae20SChristian Riesch }
382b4cdae20SChristian Riesch 
383b4cdae20SChristian Riesch /*
384b4cdae20SChristian Riesch  * AX88772 & AX88178 have a 16-bit RX_CTL value
385b4cdae20SChristian Riesch  */
386607740bcSChristian Riesch void asix_set_multicast(struct net_device *net)
387b4cdae20SChristian Riesch {
388b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
389b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
390b4cdae20SChristian Riesch 	u16 rx_ctl = AX_DEFAULT_RX_CTL;
391b4cdae20SChristian Riesch 
392b4cdae20SChristian Riesch 	if (net->flags & IFF_PROMISC) {
393b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_PRO;
394b4cdae20SChristian Riesch 	} else if (net->flags & IFF_ALLMULTI ||
395b4cdae20SChristian Riesch 		   netdev_mc_count(net) > AX_MAX_MCAST) {
396b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AMALL;
397b4cdae20SChristian Riesch 	} else if (netdev_mc_empty(net)) {
398b4cdae20SChristian Riesch 		/* just broadcast and directed */
399b4cdae20SChristian Riesch 	} else {
400b4cdae20SChristian Riesch 		/* We use the 20 byte dev->data
401b4cdae20SChristian Riesch 		 * for our 8 byte filter buffer
402b4cdae20SChristian Riesch 		 * to avoid allocating memory that
403b4cdae20SChristian Riesch 		 * is tricky to free later */
404b4cdae20SChristian Riesch 		struct netdev_hw_addr *ha;
405b4cdae20SChristian Riesch 		u32 crc_bits;
406b4cdae20SChristian Riesch 
407b4cdae20SChristian Riesch 		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
408b4cdae20SChristian Riesch 
409b4cdae20SChristian Riesch 		/* Build the multicast hash filter. */
410b4cdae20SChristian Riesch 		netdev_for_each_mc_addr(ha, net) {
411b4cdae20SChristian Riesch 			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
412b4cdae20SChristian Riesch 			data->multi_filter[crc_bits >> 3] |=
413b4cdae20SChristian Riesch 			    1 << (crc_bits & 7);
414b4cdae20SChristian Riesch 		}
415b4cdae20SChristian Riesch 
416b4cdae20SChristian Riesch 		asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
417b4cdae20SChristian Riesch 				   AX_MCAST_FILTER_SIZE, data->multi_filter);
418b4cdae20SChristian Riesch 
419b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AM;
420b4cdae20SChristian Riesch 	}
421b4cdae20SChristian Riesch 
422b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
423b4cdae20SChristian Riesch }
424b4cdae20SChristian Riesch 
425607740bcSChristian Riesch int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
426b4cdae20SChristian Riesch {
427b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
428b4cdae20SChristian Riesch 	__le16 res;
429d9fe64e5SRobert Foss 	u8 smsr;
430d9fe64e5SRobert Foss 	int i = 0;
4318a46f665SRobert Foss 	int ret;
432b4cdae20SChristian Riesch 
433b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
434d9fe64e5SRobert Foss 	do {
4358a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 0);
436610df1d2SGuenter Roeck 		if (ret == -ENODEV || ret == -ETIMEDOUT)
4378a46f665SRobert Foss 			break;
438d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
4398a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
4408a46f665SRobert Foss 				    0, 0, 1, &smsr, 0);
4418a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
442610df1d2SGuenter Roeck 	if (ret == -ENODEV || ret == -ETIMEDOUT) {
4438a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
4448a46f665SRobert Foss 		return ret;
4458a46f665SRobert Foss 	}
446d9fe64e5SRobert Foss 
447b4cdae20SChristian Riesch 	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
448d9fe64e5SRobert Foss 				(__u16)loc, 2, &res, 0);
449d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 0);
450b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
451b4cdae20SChristian Riesch 
452b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
453b4cdae20SChristian Riesch 			phy_id, loc, le16_to_cpu(res));
454b4cdae20SChristian Riesch 
455b4cdae20SChristian Riesch 	return le16_to_cpu(res);
456b4cdae20SChristian Riesch }
457b4cdae20SChristian Riesch 
458607740bcSChristian Riesch void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
459b4cdae20SChristian Riesch {
460b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
461b4cdae20SChristian Riesch 	__le16 res = cpu_to_le16(val);
462d9fe64e5SRobert Foss 	u8 smsr;
463d9fe64e5SRobert Foss 	int i = 0;
4648a46f665SRobert Foss 	int ret;
465b4cdae20SChristian Riesch 
466b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
467b4cdae20SChristian Riesch 			phy_id, loc, val);
468d9fe64e5SRobert Foss 
469b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
470d9fe64e5SRobert Foss 	do {
4718a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 0);
4728a46f665SRobert Foss 		if (ret == -ENODEV)
4738a46f665SRobert Foss 			break;
474d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
4758a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
4768a46f665SRobert Foss 				    0, 0, 1, &smsr, 0);
4778a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
4788a46f665SRobert Foss 	if (ret == -ENODEV) {
4798a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
4808a46f665SRobert Foss 		return;
4818a46f665SRobert Foss 	}
482d9fe64e5SRobert Foss 
483d9fe64e5SRobert Foss 	asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
484d9fe64e5SRobert Foss 		       (__u16)loc, 2, &res, 0);
485d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 0);
486d9fe64e5SRobert Foss 	mutex_unlock(&dev->phy_mutex);
487d9fe64e5SRobert Foss }
488d9fe64e5SRobert Foss 
489d9fe64e5SRobert Foss int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
490d9fe64e5SRobert Foss {
491d9fe64e5SRobert Foss 	struct usbnet *dev = netdev_priv(netdev);
492d9fe64e5SRobert Foss 	__le16 res;
493d9fe64e5SRobert Foss 	u8 smsr;
494d9fe64e5SRobert Foss 	int i = 0;
4958a46f665SRobert Foss 	int ret;
496d9fe64e5SRobert Foss 
497d9fe64e5SRobert Foss 	mutex_lock(&dev->phy_mutex);
498d9fe64e5SRobert Foss 	do {
4998a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 1);
500610df1d2SGuenter Roeck 		if (ret == -ENODEV || ret == -ETIMEDOUT)
5018a46f665SRobert Foss 			break;
502d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
5038a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
5048a46f665SRobert Foss 				    0, 0, 1, &smsr, 1);
5058a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
506610df1d2SGuenter Roeck 	if (ret == -ENODEV || ret == -ETIMEDOUT) {
5078a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
5088a46f665SRobert Foss 		return ret;
5098a46f665SRobert Foss 	}
510d9fe64e5SRobert Foss 
511d9fe64e5SRobert Foss 	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
512d9fe64e5SRobert Foss 		      (__u16)loc, 2, &res, 1);
513d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 1);
514d9fe64e5SRobert Foss 	mutex_unlock(&dev->phy_mutex);
515d9fe64e5SRobert Foss 
516d9fe64e5SRobert Foss 	netdev_dbg(dev->net, "asix_mdio_read_nopm() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
517d9fe64e5SRobert Foss 			phy_id, loc, le16_to_cpu(res));
518d9fe64e5SRobert Foss 
519d9fe64e5SRobert Foss 	return le16_to_cpu(res);
520d9fe64e5SRobert Foss }
521d9fe64e5SRobert Foss 
522d9fe64e5SRobert Foss void
523d9fe64e5SRobert Foss asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
524d9fe64e5SRobert Foss {
525d9fe64e5SRobert Foss 	struct usbnet *dev = netdev_priv(netdev);
526d9fe64e5SRobert Foss 	__le16 res = cpu_to_le16(val);
527d9fe64e5SRobert Foss 	u8 smsr;
528d9fe64e5SRobert Foss 	int i = 0;
5298a46f665SRobert Foss 	int ret;
530d9fe64e5SRobert Foss 
531d9fe64e5SRobert Foss 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
532d9fe64e5SRobert Foss 			phy_id, loc, val);
533d9fe64e5SRobert Foss 
534d9fe64e5SRobert Foss 	mutex_lock(&dev->phy_mutex);
535d9fe64e5SRobert Foss 	do {
5368a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 1);
5378a46f665SRobert Foss 		if (ret == -ENODEV)
5388a46f665SRobert Foss 			break;
539d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
5408a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
5418a46f665SRobert Foss 				    0, 0, 1, &smsr, 1);
5428a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
5438a46f665SRobert Foss 	if (ret == -ENODEV) {
5448a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
5458a46f665SRobert Foss 		return;
5468a46f665SRobert Foss 	}
547d9fe64e5SRobert Foss 
548d9fe64e5SRobert Foss 	asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
549d9fe64e5SRobert Foss 		       (__u16)loc, 2, &res, 1);
550d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 1);
551b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
552b4cdae20SChristian Riesch }
553b4cdae20SChristian Riesch 
554607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
555b4cdae20SChristian Riesch {
556b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
557b4cdae20SChristian Riesch 	u8 opt;
558b4cdae20SChristian Riesch 
559d9fe64e5SRobert Foss 	if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
560d9fe64e5SRobert Foss 			  0, 0, 1, &opt, 0) < 0) {
561b4cdae20SChristian Riesch 		wolinfo->supported = 0;
562b4cdae20SChristian Riesch 		wolinfo->wolopts = 0;
563b4cdae20SChristian Riesch 		return;
564b4cdae20SChristian Riesch 	}
565b4cdae20SChristian Riesch 	wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
566b4cdae20SChristian Riesch 	wolinfo->wolopts = 0;
567b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_LINK)
568b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_PHY;
569b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_MAGIC)
570b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_MAGIC;
571b4cdae20SChristian Riesch }
572b4cdae20SChristian Riesch 
573607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
574b4cdae20SChristian Riesch {
575b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
576b4cdae20SChristian Riesch 	u8 opt = 0;
577b4cdae20SChristian Riesch 
578b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_PHY)
579b4cdae20SChristian Riesch 		opt |= AX_MONITOR_LINK;
580b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_MAGIC)
581b4cdae20SChristian Riesch 		opt |= AX_MONITOR_MAGIC;
582b4cdae20SChristian Riesch 
583b4cdae20SChristian Riesch 	if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
584d9fe64e5SRobert Foss 			      opt, 0, 0, NULL, 0) < 0)
585b4cdae20SChristian Riesch 		return -EINVAL;
586b4cdae20SChristian Riesch 
587b4cdae20SChristian Riesch 	return 0;
588b4cdae20SChristian Riesch }
589b4cdae20SChristian Riesch 
590607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net)
591b4cdae20SChristian Riesch {
592ceb02c91SChristian Riesch 	return AX_EEPROM_LEN;
593b4cdae20SChristian Riesch }
594b4cdae20SChristian Riesch 
595607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
596607740bcSChristian Riesch 		    u8 *data)
597b4cdae20SChristian Riesch {
598b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
599ceb02c91SChristian Riesch 	u16 *eeprom_buff;
600ceb02c91SChristian Riesch 	int first_word, last_word;
601b4cdae20SChristian Riesch 	int i;
602b4cdae20SChristian Riesch 
603ceb02c91SChristian Riesch 	if (eeprom->len == 0)
604b4cdae20SChristian Riesch 		return -EINVAL;
605b4cdae20SChristian Riesch 
606b4cdae20SChristian Riesch 	eeprom->magic = AX_EEPROM_MAGIC;
607b4cdae20SChristian Riesch 
608ceb02c91SChristian Riesch 	first_word = eeprom->offset >> 1;
609ceb02c91SChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
610ceb02c91SChristian Riesch 
611ceb02c91SChristian Riesch 	eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
612ceb02c91SChristian Riesch 			      GFP_KERNEL);
613ceb02c91SChristian Riesch 	if (!eeprom_buff)
614ceb02c91SChristian Riesch 		return -ENOMEM;
615ceb02c91SChristian Riesch 
616b4cdae20SChristian Riesch 	/* ax8817x returns 2 bytes from eeprom on read */
617ceb02c91SChristian Riesch 	for (i = first_word; i <= last_word; i++) {
618ceb02c91SChristian Riesch 		if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
619d9fe64e5SRobert Foss 				  &eeprom_buff[i - first_word], 0) < 0) {
620ceb02c91SChristian Riesch 			kfree(eeprom_buff);
621ceb02c91SChristian Riesch 			return -EIO;
622b4cdae20SChristian Riesch 		}
623ceb02c91SChristian Riesch 	}
624ceb02c91SChristian Riesch 
625ceb02c91SChristian Riesch 	memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
626ceb02c91SChristian Riesch 	kfree(eeprom_buff);
627b4cdae20SChristian Riesch 	return 0;
628b4cdae20SChristian Riesch }
629b4cdae20SChristian Riesch 
630cb7b24cdSChristian Riesch int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
631cb7b24cdSChristian Riesch 		    u8 *data)
632cb7b24cdSChristian Riesch {
633cb7b24cdSChristian Riesch 	struct usbnet *dev = netdev_priv(net);
634cb7b24cdSChristian Riesch 	u16 *eeprom_buff;
635cb7b24cdSChristian Riesch 	int first_word, last_word;
636cb7b24cdSChristian Riesch 	int i;
637cb7b24cdSChristian Riesch 	int ret;
638cb7b24cdSChristian Riesch 
639cb7b24cdSChristian Riesch 	netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
640cb7b24cdSChristian Riesch 		   eeprom->len, eeprom->offset, eeprom->magic);
641cb7b24cdSChristian Riesch 
642cb7b24cdSChristian Riesch 	if (eeprom->len == 0)
643cb7b24cdSChristian Riesch 		return -EINVAL;
644cb7b24cdSChristian Riesch 
645cb7b24cdSChristian Riesch 	if (eeprom->magic != AX_EEPROM_MAGIC)
646cb7b24cdSChristian Riesch 		return -EINVAL;
647cb7b24cdSChristian Riesch 
648cb7b24cdSChristian Riesch 	first_word = eeprom->offset >> 1;
649cb7b24cdSChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
650cb7b24cdSChristian Riesch 
651cb7b24cdSChristian Riesch 	eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
652cb7b24cdSChristian Riesch 			      GFP_KERNEL);
653cb7b24cdSChristian Riesch 	if (!eeprom_buff)
654cb7b24cdSChristian Riesch 		return -ENOMEM;
655cb7b24cdSChristian Riesch 
656cb7b24cdSChristian Riesch 	/* align data to 16 bit boundaries, read the missing data from
657cb7b24cdSChristian Riesch 	   the EEPROM */
658cb7b24cdSChristian Riesch 	if (eeprom->offset & 1) {
659cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
660d9fe64e5SRobert Foss 				    &eeprom_buff[0], 0);
661cb7b24cdSChristian Riesch 		if (ret < 0) {
662cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
663cb7b24cdSChristian Riesch 			goto free;
664cb7b24cdSChristian Riesch 		}
665cb7b24cdSChristian Riesch 	}
666cb7b24cdSChristian Riesch 
667cb7b24cdSChristian Riesch 	if ((eeprom->offset + eeprom->len) & 1) {
668cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
669d9fe64e5SRobert Foss 				    &eeprom_buff[last_word - first_word], 0);
670cb7b24cdSChristian Riesch 		if (ret < 0) {
671cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
672cb7b24cdSChristian Riesch 			goto free;
673cb7b24cdSChristian Riesch 		}
674cb7b24cdSChristian Riesch 	}
675cb7b24cdSChristian Riesch 
676cb7b24cdSChristian Riesch 	memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
677cb7b24cdSChristian Riesch 
678cb7b24cdSChristian Riesch 	/* write data to EEPROM */
679d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
680cb7b24cdSChristian Riesch 	if (ret < 0) {
681cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to enable EEPROM write\n");
682cb7b24cdSChristian Riesch 		goto free;
683cb7b24cdSChristian Riesch 	}
684cb7b24cdSChristian Riesch 	msleep(20);
685cb7b24cdSChristian Riesch 
686cb7b24cdSChristian Riesch 	for (i = first_word; i <= last_word; i++) {
687cb7b24cdSChristian Riesch 		netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
688cb7b24cdSChristian Riesch 			   i, eeprom_buff[i - first_word]);
689cb7b24cdSChristian Riesch 		ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
690d9fe64e5SRobert Foss 				     eeprom_buff[i - first_word], 0, NULL, 0);
691cb7b24cdSChristian Riesch 		if (ret < 0) {
692cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
693cb7b24cdSChristian Riesch 				   i);
694cb7b24cdSChristian Riesch 			goto free;
695cb7b24cdSChristian Riesch 		}
696cb7b24cdSChristian Riesch 		msleep(20);
697cb7b24cdSChristian Riesch 	}
698cb7b24cdSChristian Riesch 
699d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
700cb7b24cdSChristian Riesch 	if (ret < 0) {
701cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to disable EEPROM write\n");
702cb7b24cdSChristian Riesch 		goto free;
703cb7b24cdSChristian Riesch 	}
704cb7b24cdSChristian Riesch 
705cb7b24cdSChristian Riesch 	ret = 0;
706cb7b24cdSChristian Riesch free:
707cb7b24cdSChristian Riesch 	kfree(eeprom_buff);
708cb7b24cdSChristian Riesch 	return ret;
709cb7b24cdSChristian Riesch }
710cb7b24cdSChristian Riesch 
711607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
712b4cdae20SChristian Riesch {
713b4cdae20SChristian Riesch 	/* Inherit standard device info */
714b4cdae20SChristian Riesch 	usbnet_get_drvinfo(net, info);
7157826d43fSJiri Pirko 	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
7167826d43fSJiri Pirko 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
717b4cdae20SChristian Riesch }
718b4cdae20SChristian Riesch 
719607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p)
720b4cdae20SChristian Riesch {
721b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
722b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
723b4cdae20SChristian Riesch 	struct sockaddr *addr = p;
724b4cdae20SChristian Riesch 
725b4cdae20SChristian Riesch 	if (netif_running(net))
726b4cdae20SChristian Riesch 		return -EBUSY;
727b4cdae20SChristian Riesch 	if (!is_valid_ether_addr(addr->sa_data))
728b4cdae20SChristian Riesch 		return -EADDRNOTAVAIL;
729b4cdae20SChristian Riesch 
730b4cdae20SChristian Riesch 	memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
731b4cdae20SChristian Riesch 
732b4cdae20SChristian Riesch 	/* We use the 20 byte dev->data
733b4cdae20SChristian Riesch 	 * for our 6 byte mac buffer
734b4cdae20SChristian Riesch 	 * to avoid allocating memory that
735b4cdae20SChristian Riesch 	 * is tricky to free later */
736b4cdae20SChristian Riesch 	memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
737b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
738b4cdae20SChristian Riesch 							data->mac_addr);
739b4cdae20SChristian Riesch 
740b4cdae20SChristian Riesch 	return 0;
741b4cdae20SChristian Riesch }
742