xref: /openbmc/linux/drivers/net/usb/asix_common.c (revision 6da2ec56059c3c7a7e5f729e6349e74ace1e5c57)
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 
78960eb4eeSDean Jenkins static void reset_asix_rx_fixup_info(struct asix_rx_fixup_info *rx)
79960eb4eeSDean Jenkins {
80960eb4eeSDean Jenkins 	/* Reset the variables that have a lifetime outside of
81960eb4eeSDean Jenkins 	 * asix_rx_fixup_internal() so that future processing starts from a
82960eb4eeSDean Jenkins 	 * known set of initial conditions.
83960eb4eeSDean Jenkins 	 */
84960eb4eeSDean Jenkins 
85960eb4eeSDean Jenkins 	if (rx->ax_skb) {
86960eb4eeSDean Jenkins 		/* Discard any incomplete Ethernet frame in the netdev buffer */
87960eb4eeSDean Jenkins 		kfree_skb(rx->ax_skb);
88960eb4eeSDean Jenkins 		rx->ax_skb = NULL;
89960eb4eeSDean Jenkins 	}
90960eb4eeSDean Jenkins 
91960eb4eeSDean Jenkins 	/* Assume the Data header 32-bit word is at the start of the current
92960eb4eeSDean Jenkins 	 * or next URB socket buffer so reset all the state variables.
93960eb4eeSDean Jenkins 	 */
94960eb4eeSDean Jenkins 	rx->remaining = 0;
95960eb4eeSDean Jenkins 	rx->split_head = false;
96960eb4eeSDean Jenkins 	rx->header = 0;
97960eb4eeSDean Jenkins }
98960eb4eeSDean Jenkins 
998b5b6f54SLucas Stach int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
1008b5b6f54SLucas Stach 			   struct asix_rx_fixup_info *rx)
101b4cdae20SChristian Riesch {
102b4cdae20SChristian Riesch 	int offset = 0;
1037b0378f5SDean Jenkins 	u16 size;
104b4cdae20SChristian Riesch 
1053f30b158SDean Jenkins 	/* When an Ethernet frame spans multiple URB socket buffers,
1063f30b158SDean Jenkins 	 * do a sanity test for the Data header synchronisation.
1073f30b158SDean Jenkins 	 * Attempt to detect the situation of the previous socket buffer having
1083f30b158SDean Jenkins 	 * been truncated or a socket buffer was missing. These situations
1093f30b158SDean Jenkins 	 * cause a discontinuity in the data stream and therefore need to avoid
1103f30b158SDean Jenkins 	 * appending bad data to the end of the current netdev socket buffer.
1113f30b158SDean Jenkins 	 * Also avoid unnecessarily discarding a good current netdev socket
1123f30b158SDean Jenkins 	 * buffer.
1133f30b158SDean Jenkins 	 */
1143f30b158SDean Jenkins 	if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
115cd9e2e5dSJohn Stultz 		offset = ((rx->remaining + 1) & 0xfffe);
1163f30b158SDean Jenkins 		rx->header = get_unaligned_le32(skb->data + offset);
1173f30b158SDean Jenkins 		offset = 0;
1183f30b158SDean Jenkins 
1193f30b158SDean Jenkins 		size = (u16)(rx->header & 0x7ff);
1203f30b158SDean Jenkins 		if (size != ((~rx->header >> 16) & 0x7ff)) {
1213f30b158SDean Jenkins 			netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
1223f30b158SDean Jenkins 				   rx->remaining);
123960eb4eeSDean Jenkins 			reset_asix_rx_fixup_info(rx);
1243f30b158SDean Jenkins 		}
1253f30b158SDean Jenkins 	}
1263f30b158SDean Jenkins 
1278b5b6f54SLucas Stach 	while (offset + sizeof(u16) <= skb->len) {
1287b0378f5SDean Jenkins 		u16 copy_length;
129b4cdae20SChristian Riesch 
1307b0378f5SDean Jenkins 		if (!rx->remaining) {
1313bfc69abSDean Jenkins 			if (skb->len - offset == sizeof(u16)) {
1328b5b6f54SLucas Stach 				rx->header = get_unaligned_le16(
1338b5b6f54SLucas Stach 						skb->data + offset);
1348b5b6f54SLucas Stach 				rx->split_head = true;
1358b5b6f54SLucas Stach 				offset += sizeof(u16);
1368b5b6f54SLucas Stach 				break;
1373bfc69abSDean Jenkins 			}
1383bfc69abSDean Jenkins 
1393bfc69abSDean Jenkins 			if (rx->split_head == true) {
1408b5b6f54SLucas Stach 				rx->header |= (get_unaligned_le16(
1413bfc69abSDean Jenkins 						skb->data + offset) << 16);
1428b5b6f54SLucas Stach 				rx->split_head = false;
1438b5b6f54SLucas Stach 				offset += sizeof(u16);
1448b5b6f54SLucas Stach 			} else {
1458b5b6f54SLucas Stach 				rx->header = get_unaligned_le32(skb->data +
1468b5b6f54SLucas Stach 								offset);
147b4cdae20SChristian Riesch 				offset += sizeof(u32);
1488b5b6f54SLucas Stach 			}
149b4cdae20SChristian Riesch 
1507b0378f5SDean Jenkins 			/* take frame length from Data header 32-bit word */
1517b0378f5SDean Jenkins 			size = (u16)(rx->header & 0x7ff);
1527b0378f5SDean Jenkins 			if (size != ((~rx->header >> 16) & 0x7ff)) {
1538b5b6f54SLucas Stach 				netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
1548b5b6f54SLucas Stach 					   rx->header, offset);
155960eb4eeSDean Jenkins 				reset_asix_rx_fixup_info(rx);
1568b5b6f54SLucas Stach 				return 0;
1578b5b6f54SLucas Stach 			}
1589a5ccd8eSDean Jenkins 			if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
159b70183dbSstephen hemminger 				netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
1609a5ccd8eSDean Jenkins 					   size);
161960eb4eeSDean Jenkins 				reset_asix_rx_fixup_info(rx);
1629a5ccd8eSDean Jenkins 				return 0;
1639a5ccd8eSDean Jenkins 			}
1649a5ccd8eSDean Jenkins 
1656a570814SDean Jenkins 			/* Sometimes may fail to get a netdev socket buffer but
1666a570814SDean Jenkins 			 * continue to process the URB socket buffer so that
1676a570814SDean Jenkins 			 * synchronisation of the Ethernet frame Data header
1686a570814SDean Jenkins 			 * word is maintained.
1696a570814SDean Jenkins 			 */
1707b0378f5SDean Jenkins 			rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
171b4cdae20SChristian Riesch 
1729a5ccd8eSDean Jenkins 			rx->remaining = size;
173b4cdae20SChristian Riesch 		}
174b4cdae20SChristian Riesch 
1757b0378f5SDean Jenkins 		if (rx->remaining > skb->len - offset) {
1767b0378f5SDean Jenkins 			copy_length = skb->len - offset;
1777b0378f5SDean Jenkins 			rx->remaining -= copy_length;
1787b0378f5SDean Jenkins 		} else {
1797b0378f5SDean Jenkins 			copy_length = rx->remaining;
1807b0378f5SDean Jenkins 			rx->remaining = 0;
1818b5b6f54SLucas Stach 		}
182b4cdae20SChristian Riesch 
1836a570814SDean Jenkins 		if (rx->ax_skb) {
184b952f4dfSyuan linyu 			skb_put_data(rx->ax_skb, skb->data + offset,
18559ae1d12SJohannes Berg 				     copy_length);
18622889dbbSDean Jenkins 			if (!rx->remaining) {
1878b5b6f54SLucas Stach 				usbnet_skb_return(dev, rx->ax_skb);
18822889dbbSDean Jenkins 				rx->ax_skb = NULL;
18922889dbbSDean Jenkins 			}
1906a570814SDean Jenkins 		}
1918b5b6f54SLucas Stach 
1927b0378f5SDean Jenkins 		offset += (copy_length + 1) & 0xfffe;
193b4cdae20SChristian Riesch 	}
194b4cdae20SChristian Riesch 
195b4cdae20SChristian Riesch 	if (skb->len != offset) {
1968b5b6f54SLucas Stach 		netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
1978b5b6f54SLucas Stach 			   skb->len, offset);
198960eb4eeSDean Jenkins 		reset_asix_rx_fixup_info(rx);
199b4cdae20SChristian Riesch 		return 0;
200b4cdae20SChristian Riesch 	}
2018b5b6f54SLucas Stach 
202b4cdae20SChristian Riesch 	return 1;
203b4cdae20SChristian Riesch }
204b4cdae20SChristian Riesch 
2058b5b6f54SLucas Stach int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
2068b5b6f54SLucas Stach {
2078b5b6f54SLucas Stach 	struct asix_common_private *dp = dev->driver_priv;
2088b5b6f54SLucas Stach 	struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
2098b5b6f54SLucas Stach 
2108b5b6f54SLucas Stach 	return asix_rx_fixup_internal(dev, skb, rx);
2118b5b6f54SLucas Stach }
2128b5b6f54SLucas Stach 
213d0c8f338SDean Jenkins void asix_rx_fixup_common_free(struct asix_common_private *dp)
214d0c8f338SDean Jenkins {
215d0c8f338SDean Jenkins 	struct asix_rx_fixup_info *rx;
216d0c8f338SDean Jenkins 
217d0c8f338SDean Jenkins 	if (!dp)
218d0c8f338SDean Jenkins 		return;
219d0c8f338SDean Jenkins 
220d0c8f338SDean Jenkins 	rx = &dp->rx_fixup_info;
221d0c8f338SDean Jenkins 
222d0c8f338SDean Jenkins 	if (rx->ax_skb) {
223d0c8f338SDean Jenkins 		kfree_skb(rx->ax_skb);
224d0c8f338SDean Jenkins 		rx->ax_skb = NULL;
225d0c8f338SDean Jenkins 	}
226d0c8f338SDean Jenkins }
227d0c8f338SDean Jenkins 
228607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
229b4cdae20SChristian Riesch 			      gfp_t flags)
230b4cdae20SChristian Riesch {
231b4cdae20SChristian Riesch 	int padlen;
232b4cdae20SChristian Riesch 	int headroom = skb_headroom(skb);
233b4cdae20SChristian Riesch 	int tailroom = skb_tailroom(skb);
234b4cdae20SChristian Riesch 	u32 packet_len;
235b4cdae20SChristian Riesch 	u32 padbytes = 0xffff0000;
236b4cdae20SChristian Riesch 
237b4cdae20SChristian Riesch 	padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
238b4cdae20SChristian Riesch 
239b4cdae20SChristian Riesch 	/* We need to push 4 bytes in front of frame (packet_len)
240b4cdae20SChristian Riesch 	 * and maybe add 4 bytes after the end (if padlen is 4)
241b4cdae20SChristian Riesch 	 *
242b4cdae20SChristian Riesch 	 * Avoid skb_copy_expand() expensive call, using following rules :
243b4cdae20SChristian Riesch 	 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
244b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of headroom)
245b4cdae20SChristian Riesch 	 * - We are allowed to put 4 bytes at tail if skb_cloned()
246b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of tailroom)
247b4cdae20SChristian Riesch 	 *
248242c1a28SGao Feng 	 * TCP packets for example are cloned, but __skb_header_release()
249b4cdae20SChristian Riesch 	 * was called in tcp stack, allowing us to use headroom for our needs.
250b4cdae20SChristian Riesch 	 */
251b4cdae20SChristian Riesch 	if (!skb_header_cloned(skb) &&
252b4cdae20SChristian Riesch 	    !(padlen && skb_cloned(skb)) &&
253b4cdae20SChristian Riesch 	    headroom + tailroom >= 4 + padlen) {
254b4cdae20SChristian Riesch 		/* following should not happen, but better be safe */
255b4cdae20SChristian Riesch 		if (headroom < 4 ||
256b4cdae20SChristian Riesch 		    tailroom < padlen) {
257b4cdae20SChristian Riesch 			skb->data = memmove(skb->head + 4, skb->data, skb->len);
258b4cdae20SChristian Riesch 			skb_set_tail_pointer(skb, skb->len);
259b4cdae20SChristian Riesch 		}
260b4cdae20SChristian Riesch 	} else {
261b4cdae20SChristian Riesch 		struct sk_buff *skb2;
262b4cdae20SChristian Riesch 
263b4cdae20SChristian Riesch 		skb2 = skb_copy_expand(skb, 4, padlen, flags);
264b4cdae20SChristian Riesch 		dev_kfree_skb_any(skb);
265b4cdae20SChristian Riesch 		skb = skb2;
266b4cdae20SChristian Riesch 		if (!skb)
267b4cdae20SChristian Riesch 			return NULL;
268b4cdae20SChristian Riesch 	}
269b4cdae20SChristian Riesch 
270b4cdae20SChristian Riesch 	packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
271b4cdae20SChristian Riesch 	skb_push(skb, 4);
272b4cdae20SChristian Riesch 	cpu_to_le32s(&packet_len);
273b4cdae20SChristian Riesch 	skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
274b4cdae20SChristian Riesch 
275b4cdae20SChristian Riesch 	if (padlen) {
276b4cdae20SChristian Riesch 		cpu_to_le32s(&padbytes);
277b4cdae20SChristian Riesch 		memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
278b4cdae20SChristian Riesch 		skb_put(skb, sizeof(padbytes));
279b4cdae20SChristian Riesch 	}
2801e9e39f4SBen Hutchings 
2817a1e890eSBen Hutchings 	usbnet_set_skb_tx_stats(skb, 1, 0);
282b4cdae20SChristian Riesch 	return skb;
283b4cdae20SChristian Riesch }
284b4cdae20SChristian Riesch 
285d9fe64e5SRobert Foss int asix_set_sw_mii(struct usbnet *dev, int in_pm)
286b4cdae20SChristian Riesch {
287b4cdae20SChristian Riesch 	int ret;
288d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm);
289d9fe64e5SRobert Foss 
290b4cdae20SChristian Riesch 	if (ret < 0)
291b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable software MII access\n");
292b4cdae20SChristian Riesch 	return ret;
293b4cdae20SChristian Riesch }
294b4cdae20SChristian Riesch 
295d9fe64e5SRobert Foss int asix_set_hw_mii(struct usbnet *dev, int in_pm)
296b4cdae20SChristian Riesch {
297b4cdae20SChristian Riesch 	int ret;
298d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm);
299b4cdae20SChristian Riesch 	if (ret < 0)
300b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable hardware MII access\n");
301b4cdae20SChristian Riesch 	return ret;
302b4cdae20SChristian Riesch }
303b4cdae20SChristian Riesch 
30416626b0cSChristian Riesch int asix_read_phy_addr(struct usbnet *dev, int internal)
305b4cdae20SChristian Riesch {
30616626b0cSChristian Riesch 	int offset = (internal ? 1 : 0);
307b4cdae20SChristian Riesch 	u8 buf[2];
308d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0);
309b4cdae20SChristian Riesch 
310b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr()\n");
311b4cdae20SChristian Riesch 
312b4cdae20SChristian Riesch 	if (ret < 0) {
313b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
314b4cdae20SChristian Riesch 		goto out;
315b4cdae20SChristian Riesch 	}
316b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
317b4cdae20SChristian Riesch 		   *((__le16 *)buf));
31816626b0cSChristian Riesch 	ret = buf[offset];
319b4cdae20SChristian Riesch 
320b4cdae20SChristian Riesch out:
321b4cdae20SChristian Riesch 	return ret;
322b4cdae20SChristian Riesch }
323b4cdae20SChristian Riesch 
32416626b0cSChristian Riesch int asix_get_phy_addr(struct usbnet *dev)
32516626b0cSChristian Riesch {
32616626b0cSChristian Riesch 	/* return the address of the internal phy */
32716626b0cSChristian Riesch 	return asix_read_phy_addr(dev, 1);
32816626b0cSChristian Riesch }
32916626b0cSChristian Riesch 
33016626b0cSChristian Riesch 
331d9fe64e5SRobert Foss int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
332b4cdae20SChristian Riesch {
333b4cdae20SChristian Riesch 	int ret;
334b4cdae20SChristian Riesch 
335d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
336b4cdae20SChristian Riesch 	if (ret < 0)
337b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
338b4cdae20SChristian Riesch 
339b4cdae20SChristian Riesch 	return ret;
340b4cdae20SChristian Riesch }
341b4cdae20SChristian Riesch 
342d9fe64e5SRobert Foss u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
343b4cdae20SChristian Riesch {
344b4cdae20SChristian Riesch 	__le16 v;
345d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
346b4cdae20SChristian Riesch 
347b4cdae20SChristian Riesch 	if (ret < 0) {
348b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
349b4cdae20SChristian Riesch 		goto out;
350b4cdae20SChristian Riesch 	}
351b4cdae20SChristian Riesch 	ret = le16_to_cpu(v);
352b4cdae20SChristian Riesch out:
353b4cdae20SChristian Riesch 	return ret;
354b4cdae20SChristian Riesch }
355b4cdae20SChristian Riesch 
356d9fe64e5SRobert Foss int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
357b4cdae20SChristian Riesch {
358b4cdae20SChristian Riesch 	int ret;
359b4cdae20SChristian Riesch 
360b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
361d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
362b4cdae20SChristian Riesch 	if (ret < 0)
363b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
364b4cdae20SChristian Riesch 			   mode, ret);
365b4cdae20SChristian Riesch 
366b4cdae20SChristian Riesch 	return ret;
367b4cdae20SChristian Riesch }
368b4cdae20SChristian Riesch 
369d9fe64e5SRobert Foss u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
370b4cdae20SChristian Riesch {
371b4cdae20SChristian Riesch 	__le16 v;
372d9fe64e5SRobert Foss 	int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
373d9fe64e5SRobert Foss 				0, 0, 2, &v, in_pm);
374b4cdae20SChristian Riesch 
375b4cdae20SChristian Riesch 	if (ret < 0) {
376b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
377b4cdae20SChristian Riesch 			   ret);
378b4cdae20SChristian Riesch 		return ret;	/* TODO: callers not checking for error ret */
379b4cdae20SChristian Riesch 	}
380b4cdae20SChristian Riesch 
381b4cdae20SChristian Riesch 	return le16_to_cpu(v);
382b4cdae20SChristian Riesch 
383b4cdae20SChristian Riesch }
384b4cdae20SChristian Riesch 
385d9fe64e5SRobert Foss int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
386b4cdae20SChristian Riesch {
387b4cdae20SChristian Riesch 	int ret;
388b4cdae20SChristian Riesch 
389b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
390d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
391d9fe64e5SRobert Foss 			     mode, 0, 0, NULL, in_pm);
392b4cdae20SChristian Riesch 	if (ret < 0)
393b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
394b4cdae20SChristian Riesch 			   mode, ret);
395b4cdae20SChristian Riesch 
396b4cdae20SChristian Riesch 	return ret;
397b4cdae20SChristian Riesch }
398b4cdae20SChristian Riesch 
399d9fe64e5SRobert Foss int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
400b4cdae20SChristian Riesch {
401b4cdae20SChristian Riesch 	int ret;
402b4cdae20SChristian Riesch 
403b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
404d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
405b4cdae20SChristian Riesch 	if (ret < 0)
406b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
407b4cdae20SChristian Riesch 			   value, ret);
408b4cdae20SChristian Riesch 
409b4cdae20SChristian Riesch 	if (sleep)
410b4cdae20SChristian Riesch 		msleep(sleep);
411b4cdae20SChristian Riesch 
412b4cdae20SChristian Riesch 	return ret;
413b4cdae20SChristian Riesch }
414b4cdae20SChristian Riesch 
415b4cdae20SChristian Riesch /*
416b4cdae20SChristian Riesch  * AX88772 & AX88178 have a 16-bit RX_CTL value
417b4cdae20SChristian Riesch  */
418607740bcSChristian Riesch void asix_set_multicast(struct net_device *net)
419b4cdae20SChristian Riesch {
420b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
421b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
422b4cdae20SChristian Riesch 	u16 rx_ctl = AX_DEFAULT_RX_CTL;
423b4cdae20SChristian Riesch 
424b4cdae20SChristian Riesch 	if (net->flags & IFF_PROMISC) {
425b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_PRO;
426b4cdae20SChristian Riesch 	} else if (net->flags & IFF_ALLMULTI ||
427b4cdae20SChristian Riesch 		   netdev_mc_count(net) > AX_MAX_MCAST) {
428b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AMALL;
429b4cdae20SChristian Riesch 	} else if (netdev_mc_empty(net)) {
430b4cdae20SChristian Riesch 		/* just broadcast and directed */
431b4cdae20SChristian Riesch 	} else {
432b4cdae20SChristian Riesch 		/* We use the 20 byte dev->data
433b4cdae20SChristian Riesch 		 * for our 8 byte filter buffer
434b4cdae20SChristian Riesch 		 * to avoid allocating memory that
435b4cdae20SChristian Riesch 		 * is tricky to free later */
436b4cdae20SChristian Riesch 		struct netdev_hw_addr *ha;
437b4cdae20SChristian Riesch 		u32 crc_bits;
438b4cdae20SChristian Riesch 
439b4cdae20SChristian Riesch 		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
440b4cdae20SChristian Riesch 
441b4cdae20SChristian Riesch 		/* Build the multicast hash filter. */
442b4cdae20SChristian Riesch 		netdev_for_each_mc_addr(ha, net) {
443b4cdae20SChristian Riesch 			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
444b4cdae20SChristian Riesch 			data->multi_filter[crc_bits >> 3] |=
445b4cdae20SChristian Riesch 			    1 << (crc_bits & 7);
446b4cdae20SChristian Riesch 		}
447b4cdae20SChristian Riesch 
448b4cdae20SChristian Riesch 		asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
449b4cdae20SChristian Riesch 				   AX_MCAST_FILTER_SIZE, data->multi_filter);
450b4cdae20SChristian Riesch 
451b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AM;
452b4cdae20SChristian Riesch 	}
453b4cdae20SChristian Riesch 
454b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
455b4cdae20SChristian Riesch }
456b4cdae20SChristian Riesch 
457607740bcSChristian Riesch int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
458b4cdae20SChristian Riesch {
459b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
460b4cdae20SChristian Riesch 	__le16 res;
461d9fe64e5SRobert Foss 	u8 smsr;
462d9fe64e5SRobert Foss 	int i = 0;
4638a46f665SRobert Foss 	int ret;
464b4cdae20SChristian Riesch 
465b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
466d9fe64e5SRobert Foss 	do {
4678a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 0);
468610df1d2SGuenter Roeck 		if (ret == -ENODEV || ret == -ETIMEDOUT)
4698a46f665SRobert Foss 			break;
470d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
4718a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
4728a46f665SRobert Foss 				    0, 0, 1, &smsr, 0);
4738a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
474610df1d2SGuenter Roeck 	if (ret == -ENODEV || ret == -ETIMEDOUT) {
4758a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
4768a46f665SRobert Foss 		return ret;
4778a46f665SRobert Foss 	}
478d9fe64e5SRobert Foss 
479b4cdae20SChristian Riesch 	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
480d9fe64e5SRobert Foss 				(__u16)loc, 2, &res, 0);
481d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 0);
482b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
483b4cdae20SChristian Riesch 
484b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
485b4cdae20SChristian Riesch 			phy_id, loc, le16_to_cpu(res));
486b4cdae20SChristian Riesch 
487b4cdae20SChristian Riesch 	return le16_to_cpu(res);
488b4cdae20SChristian Riesch }
489b4cdae20SChristian Riesch 
490607740bcSChristian Riesch void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
491b4cdae20SChristian Riesch {
492b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
493b4cdae20SChristian Riesch 	__le16 res = cpu_to_le16(val);
494d9fe64e5SRobert Foss 	u8 smsr;
495d9fe64e5SRobert Foss 	int i = 0;
4968a46f665SRobert Foss 	int ret;
497b4cdae20SChristian Riesch 
498b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
499b4cdae20SChristian Riesch 			phy_id, loc, val);
500d9fe64e5SRobert Foss 
501b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
502d9fe64e5SRobert Foss 	do {
5038a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 0);
5048a46f665SRobert Foss 		if (ret == -ENODEV)
5058a46f665SRobert Foss 			break;
506d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
5078a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
5088a46f665SRobert Foss 				    0, 0, 1, &smsr, 0);
5098a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
5108a46f665SRobert Foss 	if (ret == -ENODEV) {
5118a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
5128a46f665SRobert Foss 		return;
5138a46f665SRobert Foss 	}
514d9fe64e5SRobert Foss 
515d9fe64e5SRobert Foss 	asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
516d9fe64e5SRobert Foss 		       (__u16)loc, 2, &res, 0);
517d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 0);
518d9fe64e5SRobert Foss 	mutex_unlock(&dev->phy_mutex);
519d9fe64e5SRobert Foss }
520d9fe64e5SRobert Foss 
521d9fe64e5SRobert Foss int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
522d9fe64e5SRobert Foss {
523d9fe64e5SRobert Foss 	struct usbnet *dev = netdev_priv(netdev);
524d9fe64e5SRobert Foss 	__le16 res;
525d9fe64e5SRobert Foss 	u8 smsr;
526d9fe64e5SRobert Foss 	int i = 0;
5278a46f665SRobert Foss 	int ret;
528d9fe64e5SRobert Foss 
529d9fe64e5SRobert Foss 	mutex_lock(&dev->phy_mutex);
530d9fe64e5SRobert Foss 	do {
5318a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 1);
532610df1d2SGuenter Roeck 		if (ret == -ENODEV || ret == -ETIMEDOUT)
5338a46f665SRobert Foss 			break;
534d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
5358a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
5368a46f665SRobert Foss 				    0, 0, 1, &smsr, 1);
5378a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
538610df1d2SGuenter Roeck 	if (ret == -ENODEV || ret == -ETIMEDOUT) {
5398a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
5408a46f665SRobert Foss 		return ret;
5418a46f665SRobert Foss 	}
542d9fe64e5SRobert Foss 
543d9fe64e5SRobert Foss 	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
544d9fe64e5SRobert Foss 		      (__u16)loc, 2, &res, 1);
545d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 1);
546d9fe64e5SRobert Foss 	mutex_unlock(&dev->phy_mutex);
547d9fe64e5SRobert Foss 
548d9fe64e5SRobert Foss 	netdev_dbg(dev->net, "asix_mdio_read_nopm() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
549d9fe64e5SRobert Foss 			phy_id, loc, le16_to_cpu(res));
550d9fe64e5SRobert Foss 
551d9fe64e5SRobert Foss 	return le16_to_cpu(res);
552d9fe64e5SRobert Foss }
553d9fe64e5SRobert Foss 
554d9fe64e5SRobert Foss void
555d9fe64e5SRobert Foss asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
556d9fe64e5SRobert Foss {
557d9fe64e5SRobert Foss 	struct usbnet *dev = netdev_priv(netdev);
558d9fe64e5SRobert Foss 	__le16 res = cpu_to_le16(val);
559d9fe64e5SRobert Foss 	u8 smsr;
560d9fe64e5SRobert Foss 	int i = 0;
5618a46f665SRobert Foss 	int ret;
562d9fe64e5SRobert Foss 
563d9fe64e5SRobert Foss 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
564d9fe64e5SRobert Foss 			phy_id, loc, val);
565d9fe64e5SRobert Foss 
566d9fe64e5SRobert Foss 	mutex_lock(&dev->phy_mutex);
567d9fe64e5SRobert Foss 	do {
5688a46f665SRobert Foss 		ret = asix_set_sw_mii(dev, 1);
5698a46f665SRobert Foss 		if (ret == -ENODEV)
5708a46f665SRobert Foss 			break;
571d9fe64e5SRobert Foss 		usleep_range(1000, 1100);
5728a46f665SRobert Foss 		ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
5738a46f665SRobert Foss 				    0, 0, 1, &smsr, 1);
5748a46f665SRobert Foss 	} while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
5758a46f665SRobert Foss 	if (ret == -ENODEV) {
5768a46f665SRobert Foss 		mutex_unlock(&dev->phy_mutex);
5778a46f665SRobert Foss 		return;
5788a46f665SRobert Foss 	}
579d9fe64e5SRobert Foss 
580d9fe64e5SRobert Foss 	asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
581d9fe64e5SRobert Foss 		       (__u16)loc, 2, &res, 1);
582d9fe64e5SRobert Foss 	asix_set_hw_mii(dev, 1);
583b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
584b4cdae20SChristian Riesch }
585b4cdae20SChristian Riesch 
586607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
587b4cdae20SChristian Riesch {
588b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
589b4cdae20SChristian Riesch 	u8 opt;
590b4cdae20SChristian Riesch 
591d9fe64e5SRobert Foss 	if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
592d9fe64e5SRobert Foss 			  0, 0, 1, &opt, 0) < 0) {
593b4cdae20SChristian Riesch 		wolinfo->supported = 0;
594b4cdae20SChristian Riesch 		wolinfo->wolopts = 0;
595b4cdae20SChristian Riesch 		return;
596b4cdae20SChristian Riesch 	}
597b4cdae20SChristian Riesch 	wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
598b4cdae20SChristian Riesch 	wolinfo->wolopts = 0;
599b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_LINK)
600b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_PHY;
601b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_MAGIC)
602b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_MAGIC;
603b4cdae20SChristian Riesch }
604b4cdae20SChristian Riesch 
605607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
606b4cdae20SChristian Riesch {
607b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
608b4cdae20SChristian Riesch 	u8 opt = 0;
609b4cdae20SChristian Riesch 
610b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_PHY)
611b4cdae20SChristian Riesch 		opt |= AX_MONITOR_LINK;
612b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_MAGIC)
613b4cdae20SChristian Riesch 		opt |= AX_MONITOR_MAGIC;
614b4cdae20SChristian Riesch 
615b4cdae20SChristian Riesch 	if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
616d9fe64e5SRobert Foss 			      opt, 0, 0, NULL, 0) < 0)
617b4cdae20SChristian Riesch 		return -EINVAL;
618b4cdae20SChristian Riesch 
619b4cdae20SChristian Riesch 	return 0;
620b4cdae20SChristian Riesch }
621b4cdae20SChristian Riesch 
622607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net)
623b4cdae20SChristian Riesch {
624ceb02c91SChristian Riesch 	return AX_EEPROM_LEN;
625b4cdae20SChristian Riesch }
626b4cdae20SChristian Riesch 
627607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
628607740bcSChristian Riesch 		    u8 *data)
629b4cdae20SChristian Riesch {
630b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
631ceb02c91SChristian Riesch 	u16 *eeprom_buff;
632ceb02c91SChristian Riesch 	int first_word, last_word;
633b4cdae20SChristian Riesch 	int i;
634b4cdae20SChristian Riesch 
635ceb02c91SChristian Riesch 	if (eeprom->len == 0)
636b4cdae20SChristian Riesch 		return -EINVAL;
637b4cdae20SChristian Riesch 
638b4cdae20SChristian Riesch 	eeprom->magic = AX_EEPROM_MAGIC;
639b4cdae20SChristian Riesch 
640ceb02c91SChristian Riesch 	first_word = eeprom->offset >> 1;
641ceb02c91SChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
642ceb02c91SChristian Riesch 
643*6da2ec56SKees Cook 	eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
644ceb02c91SChristian Riesch 				    GFP_KERNEL);
645ceb02c91SChristian Riesch 	if (!eeprom_buff)
646ceb02c91SChristian Riesch 		return -ENOMEM;
647ceb02c91SChristian Riesch 
648b4cdae20SChristian Riesch 	/* ax8817x returns 2 bytes from eeprom on read */
649ceb02c91SChristian Riesch 	for (i = first_word; i <= last_word; i++) {
650ceb02c91SChristian Riesch 		if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
651d9fe64e5SRobert Foss 				  &eeprom_buff[i - first_word], 0) < 0) {
652ceb02c91SChristian Riesch 			kfree(eeprom_buff);
653ceb02c91SChristian Riesch 			return -EIO;
654b4cdae20SChristian Riesch 		}
655ceb02c91SChristian Riesch 	}
656ceb02c91SChristian Riesch 
657ceb02c91SChristian Riesch 	memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
658ceb02c91SChristian Riesch 	kfree(eeprom_buff);
659b4cdae20SChristian Riesch 	return 0;
660b4cdae20SChristian Riesch }
661b4cdae20SChristian Riesch 
662cb7b24cdSChristian Riesch int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
663cb7b24cdSChristian Riesch 		    u8 *data)
664cb7b24cdSChristian Riesch {
665cb7b24cdSChristian Riesch 	struct usbnet *dev = netdev_priv(net);
666cb7b24cdSChristian Riesch 	u16 *eeprom_buff;
667cb7b24cdSChristian Riesch 	int first_word, last_word;
668cb7b24cdSChristian Riesch 	int i;
669cb7b24cdSChristian Riesch 	int ret;
670cb7b24cdSChristian Riesch 
671cb7b24cdSChristian Riesch 	netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
672cb7b24cdSChristian Riesch 		   eeprom->len, eeprom->offset, eeprom->magic);
673cb7b24cdSChristian Riesch 
674cb7b24cdSChristian Riesch 	if (eeprom->len == 0)
675cb7b24cdSChristian Riesch 		return -EINVAL;
676cb7b24cdSChristian Riesch 
677cb7b24cdSChristian Riesch 	if (eeprom->magic != AX_EEPROM_MAGIC)
678cb7b24cdSChristian Riesch 		return -EINVAL;
679cb7b24cdSChristian Riesch 
680cb7b24cdSChristian Riesch 	first_word = eeprom->offset >> 1;
681cb7b24cdSChristian Riesch 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
682cb7b24cdSChristian Riesch 
683*6da2ec56SKees Cook 	eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
684cb7b24cdSChristian Riesch 				    GFP_KERNEL);
685cb7b24cdSChristian Riesch 	if (!eeprom_buff)
686cb7b24cdSChristian Riesch 		return -ENOMEM;
687cb7b24cdSChristian Riesch 
688cb7b24cdSChristian Riesch 	/* align data to 16 bit boundaries, read the missing data from
689cb7b24cdSChristian Riesch 	   the EEPROM */
690cb7b24cdSChristian Riesch 	if (eeprom->offset & 1) {
691cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
692d9fe64e5SRobert Foss 				    &eeprom_buff[0], 0);
693cb7b24cdSChristian Riesch 		if (ret < 0) {
694cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
695cb7b24cdSChristian Riesch 			goto free;
696cb7b24cdSChristian Riesch 		}
697cb7b24cdSChristian Riesch 	}
698cb7b24cdSChristian Riesch 
699cb7b24cdSChristian Riesch 	if ((eeprom->offset + eeprom->len) & 1) {
700cb7b24cdSChristian Riesch 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
701d9fe64e5SRobert Foss 				    &eeprom_buff[last_word - first_word], 0);
702cb7b24cdSChristian Riesch 		if (ret < 0) {
703cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
704cb7b24cdSChristian Riesch 			goto free;
705cb7b24cdSChristian Riesch 		}
706cb7b24cdSChristian Riesch 	}
707cb7b24cdSChristian Riesch 
708cb7b24cdSChristian Riesch 	memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
709cb7b24cdSChristian Riesch 
710cb7b24cdSChristian Riesch 	/* write data to EEPROM */
711d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
712cb7b24cdSChristian Riesch 	if (ret < 0) {
713cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to enable EEPROM write\n");
714cb7b24cdSChristian Riesch 		goto free;
715cb7b24cdSChristian Riesch 	}
716cb7b24cdSChristian Riesch 	msleep(20);
717cb7b24cdSChristian Riesch 
718cb7b24cdSChristian Riesch 	for (i = first_word; i <= last_word; i++) {
719cb7b24cdSChristian Riesch 		netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
720cb7b24cdSChristian Riesch 			   i, eeprom_buff[i - first_word]);
721cb7b24cdSChristian Riesch 		ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
722d9fe64e5SRobert Foss 				     eeprom_buff[i - first_word], 0, NULL, 0);
723cb7b24cdSChristian Riesch 		if (ret < 0) {
724cb7b24cdSChristian Riesch 			netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
725cb7b24cdSChristian Riesch 				   i);
726cb7b24cdSChristian Riesch 			goto free;
727cb7b24cdSChristian Riesch 		}
728cb7b24cdSChristian Riesch 		msleep(20);
729cb7b24cdSChristian Riesch 	}
730cb7b24cdSChristian Riesch 
731d9fe64e5SRobert Foss 	ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
732cb7b24cdSChristian Riesch 	if (ret < 0) {
733cb7b24cdSChristian Riesch 		netdev_err(net, "Failed to disable EEPROM write\n");
734cb7b24cdSChristian Riesch 		goto free;
735cb7b24cdSChristian Riesch 	}
736cb7b24cdSChristian Riesch 
737cb7b24cdSChristian Riesch 	ret = 0;
738cb7b24cdSChristian Riesch free:
739cb7b24cdSChristian Riesch 	kfree(eeprom_buff);
740cb7b24cdSChristian Riesch 	return ret;
741cb7b24cdSChristian Riesch }
742cb7b24cdSChristian Riesch 
743607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
744b4cdae20SChristian Riesch {
745b4cdae20SChristian Riesch 	/* Inherit standard device info */
746b4cdae20SChristian Riesch 	usbnet_get_drvinfo(net, info);
7477826d43fSJiri Pirko 	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
7487826d43fSJiri Pirko 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
749b4cdae20SChristian Riesch }
750b4cdae20SChristian Riesch 
751607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p)
752b4cdae20SChristian Riesch {
753b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
754b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
755b4cdae20SChristian Riesch 	struct sockaddr *addr = p;
756b4cdae20SChristian Riesch 
757b4cdae20SChristian Riesch 	if (netif_running(net))
758b4cdae20SChristian Riesch 		return -EBUSY;
759b4cdae20SChristian Riesch 	if (!is_valid_ether_addr(addr->sa_data))
760b4cdae20SChristian Riesch 		return -EADDRNOTAVAIL;
761b4cdae20SChristian Riesch 
762b4cdae20SChristian Riesch 	memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
763b4cdae20SChristian Riesch 
764b4cdae20SChristian Riesch 	/* We use the 20 byte dev->data
765b4cdae20SChristian Riesch 	 * for our 6 byte mac buffer
766b4cdae20SChristian Riesch 	 * to avoid allocating memory that
767b4cdae20SChristian Riesch 	 * is tricky to free later */
768b4cdae20SChristian Riesch 	memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
769b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
770b4cdae20SChristian Riesch 							data->mac_addr);
771b4cdae20SChristian Riesch 
772b4cdae20SChristian Riesch 	return 0;
773b4cdae20SChristian Riesch }
774