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
asix_read_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data,int in_pm)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
asix_write_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data,int in_pm)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
asix_write_cmd_async(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data)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
asix_set_sw_mii(struct usbnet * dev,int in_pm)717e40e16eSZhengchao Shao static int asix_set_sw_mii(struct usbnet *dev, int in_pm)
727e40e16eSZhengchao Shao {
737e40e16eSZhengchao Shao int ret;
747e40e16eSZhengchao Shao
757e40e16eSZhengchao Shao ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm);
767e40e16eSZhengchao Shao
777e40e16eSZhengchao Shao if (ret < 0)
787e40e16eSZhengchao Shao netdev_err(dev->net, "Failed to enable software MII access\n");
797e40e16eSZhengchao Shao return ret;
807e40e16eSZhengchao Shao }
817e40e16eSZhengchao Shao
asix_set_hw_mii(struct usbnet * dev,int in_pm)827e40e16eSZhengchao Shao static int asix_set_hw_mii(struct usbnet *dev, int in_pm)
837e40e16eSZhengchao Shao {
847e40e16eSZhengchao Shao int ret;
857e40e16eSZhengchao Shao
867e40e16eSZhengchao Shao ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm);
877e40e16eSZhengchao Shao if (ret < 0)
887e40e16eSZhengchao Shao netdev_err(dev->net, "Failed to enable hardware MII access\n");
897e40e16eSZhengchao Shao return ret;
907e40e16eSZhengchao Shao }
917e40e16eSZhengchao Shao
asix_check_host_enable(struct usbnet * dev,int in_pm)92a786e319SPavel Skripkin static int asix_check_host_enable(struct usbnet *dev, int in_pm)
93a786e319SPavel Skripkin {
94a786e319SPavel Skripkin int i, ret;
95a786e319SPavel Skripkin u8 smsr;
96a786e319SPavel Skripkin
97d1652b70SPavel Skripkin for (i = 0; i < AX_HOST_EN_RETRIES; ++i) {
98a786e319SPavel Skripkin ret = asix_set_sw_mii(dev, in_pm);
99a786e319SPavel Skripkin if (ret == -ENODEV || ret == -ETIMEDOUT)
100a786e319SPavel Skripkin break;
101a786e319SPavel Skripkin usleep_range(1000, 1100);
102a786e319SPavel Skripkin ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
103a786e319SPavel Skripkin 0, 0, 1, &smsr, in_pm);
104a786e319SPavel Skripkin if (ret == -ENODEV)
105a786e319SPavel Skripkin break;
106920a9fa2SPavel Skripkin else if (ret < 0)
107a786e319SPavel Skripkin continue;
108a786e319SPavel Skripkin else if (smsr & AX_HOST_EN)
109a786e319SPavel Skripkin break;
110a786e319SPavel Skripkin }
111a786e319SPavel Skripkin
112d1652b70SPavel Skripkin return i >= AX_HOST_EN_RETRIES ? -ETIMEDOUT : ret;
113a786e319SPavel Skripkin }
114a786e319SPavel Skripkin
reset_asix_rx_fixup_info(struct asix_rx_fixup_info * rx)115960eb4eeSDean Jenkins static void reset_asix_rx_fixup_info(struct asix_rx_fixup_info *rx)
116960eb4eeSDean Jenkins {
117960eb4eeSDean Jenkins /* Reset the variables that have a lifetime outside of
118960eb4eeSDean Jenkins * asix_rx_fixup_internal() so that future processing starts from a
119960eb4eeSDean Jenkins * known set of initial conditions.
120960eb4eeSDean Jenkins */
121960eb4eeSDean Jenkins
122960eb4eeSDean Jenkins if (rx->ax_skb) {
123960eb4eeSDean Jenkins /* Discard any incomplete Ethernet frame in the netdev buffer */
124960eb4eeSDean Jenkins kfree_skb(rx->ax_skb);
125960eb4eeSDean Jenkins rx->ax_skb = NULL;
126960eb4eeSDean Jenkins }
127960eb4eeSDean Jenkins
128960eb4eeSDean Jenkins /* Assume the Data header 32-bit word is at the start of the current
129960eb4eeSDean Jenkins * or next URB socket buffer so reset all the state variables.
130960eb4eeSDean Jenkins */
131960eb4eeSDean Jenkins rx->remaining = 0;
132960eb4eeSDean Jenkins rx->split_head = false;
133960eb4eeSDean Jenkins rx->header = 0;
134960eb4eeSDean Jenkins }
135960eb4eeSDean Jenkins
asix_rx_fixup_internal(struct usbnet * dev,struct sk_buff * skb,struct asix_rx_fixup_info * rx)1368b5b6f54SLucas Stach int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
1378b5b6f54SLucas Stach struct asix_rx_fixup_info *rx)
138b4cdae20SChristian Riesch {
139b4cdae20SChristian Riesch int offset = 0;
1407b0378f5SDean Jenkins u16 size;
141b4cdae20SChristian Riesch
1423f30b158SDean Jenkins /* When an Ethernet frame spans multiple URB socket buffers,
1433f30b158SDean Jenkins * do a sanity test for the Data header synchronisation.
1443f30b158SDean Jenkins * Attempt to detect the situation of the previous socket buffer having
1453f30b158SDean Jenkins * been truncated or a socket buffer was missing. These situations
1463f30b158SDean Jenkins * cause a discontinuity in the data stream and therefore need to avoid
1473f30b158SDean Jenkins * appending bad data to the end of the current netdev socket buffer.
1483f30b158SDean Jenkins * Also avoid unnecessarily discarding a good current netdev socket
1493f30b158SDean Jenkins * buffer.
1503f30b158SDean Jenkins */
1513f30b158SDean Jenkins if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
152cd9e2e5dSJohn Stultz offset = ((rx->remaining + 1) & 0xfffe);
1533f30b158SDean Jenkins rx->header = get_unaligned_le32(skb->data + offset);
1543f30b158SDean Jenkins offset = 0;
1553f30b158SDean Jenkins
1563f30b158SDean Jenkins size = (u16)(rx->header & 0x7ff);
1573f30b158SDean Jenkins if (size != ((~rx->header >> 16) & 0x7ff)) {
1583f30b158SDean Jenkins netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
1593f30b158SDean Jenkins rx->remaining);
160960eb4eeSDean Jenkins reset_asix_rx_fixup_info(rx);
1613f30b158SDean Jenkins }
1623f30b158SDean Jenkins }
1633f30b158SDean Jenkins
1648b5b6f54SLucas Stach while (offset + sizeof(u16) <= skb->len) {
1657b0378f5SDean Jenkins u16 copy_length;
166b4cdae20SChristian Riesch
1677b0378f5SDean Jenkins if (!rx->remaining) {
1683bfc69abSDean Jenkins if (skb->len - offset == sizeof(u16)) {
1698b5b6f54SLucas Stach rx->header = get_unaligned_le16(
1708b5b6f54SLucas Stach skb->data + offset);
1718b5b6f54SLucas Stach rx->split_head = true;
1728b5b6f54SLucas Stach offset += sizeof(u16);
1738b5b6f54SLucas Stach break;
1743bfc69abSDean Jenkins }
1753bfc69abSDean Jenkins
1763bfc69abSDean Jenkins if (rx->split_head == true) {
1778b5b6f54SLucas Stach rx->header |= (get_unaligned_le16(
1783bfc69abSDean Jenkins skb->data + offset) << 16);
1798b5b6f54SLucas Stach rx->split_head = false;
1808b5b6f54SLucas Stach offset += sizeof(u16);
1818b5b6f54SLucas Stach } else {
1828b5b6f54SLucas Stach rx->header = get_unaligned_le32(skb->data +
1838b5b6f54SLucas Stach offset);
184b4cdae20SChristian Riesch offset += sizeof(u32);
1858b5b6f54SLucas Stach }
186b4cdae20SChristian Riesch
1877b0378f5SDean Jenkins /* take frame length from Data header 32-bit word */
1887b0378f5SDean Jenkins size = (u16)(rx->header & 0x7ff);
1897b0378f5SDean Jenkins if (size != ((~rx->header >> 16) & 0x7ff)) {
1908b5b6f54SLucas Stach netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
1918b5b6f54SLucas Stach rx->header, offset);
192960eb4eeSDean Jenkins reset_asix_rx_fixup_info(rx);
1938b5b6f54SLucas Stach return 0;
1948b5b6f54SLucas Stach }
1959a5ccd8eSDean Jenkins if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
196b70183dbSstephen hemminger netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
1979a5ccd8eSDean Jenkins size);
198960eb4eeSDean Jenkins reset_asix_rx_fixup_info(rx);
1999a5ccd8eSDean Jenkins return 0;
2009a5ccd8eSDean Jenkins }
2019a5ccd8eSDean Jenkins
2026a570814SDean Jenkins /* Sometimes may fail to get a netdev socket buffer but
2036a570814SDean Jenkins * continue to process the URB socket buffer so that
2046a570814SDean Jenkins * synchronisation of the Ethernet frame Data header
2056a570814SDean Jenkins * word is maintained.
2066a570814SDean Jenkins */
2077b0378f5SDean Jenkins rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
208b4cdae20SChristian Riesch
2099a5ccd8eSDean Jenkins rx->remaining = size;
210b4cdae20SChristian Riesch }
211b4cdae20SChristian Riesch
2127b0378f5SDean Jenkins if (rx->remaining > skb->len - offset) {
2137b0378f5SDean Jenkins copy_length = skb->len - offset;
2147b0378f5SDean Jenkins rx->remaining -= copy_length;
2157b0378f5SDean Jenkins } else {
2167b0378f5SDean Jenkins copy_length = rx->remaining;
2177b0378f5SDean Jenkins rx->remaining = 0;
2188b5b6f54SLucas Stach }
219b4cdae20SChristian Riesch
2206a570814SDean Jenkins if (rx->ax_skb) {
221b952f4dfSyuan linyu skb_put_data(rx->ax_skb, skb->data + offset,
22259ae1d12SJohannes Berg copy_length);
22322889dbbSDean Jenkins if (!rx->remaining) {
2248b5b6f54SLucas Stach usbnet_skb_return(dev, rx->ax_skb);
22522889dbbSDean Jenkins rx->ax_skb = NULL;
22622889dbbSDean Jenkins }
2276a570814SDean Jenkins }
2288b5b6f54SLucas Stach
2297b0378f5SDean Jenkins offset += (copy_length + 1) & 0xfffe;
230b4cdae20SChristian Riesch }
231b4cdae20SChristian Riesch
232b4cdae20SChristian Riesch if (skb->len != offset) {
2338b5b6f54SLucas Stach netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
2348b5b6f54SLucas Stach skb->len, offset);
235960eb4eeSDean Jenkins reset_asix_rx_fixup_info(rx);
236b4cdae20SChristian Riesch return 0;
237b4cdae20SChristian Riesch }
2388b5b6f54SLucas Stach
239b4cdae20SChristian Riesch return 1;
240b4cdae20SChristian Riesch }
241b4cdae20SChristian Riesch
asix_rx_fixup_common(struct usbnet * dev,struct sk_buff * skb)2428b5b6f54SLucas Stach int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
2438b5b6f54SLucas Stach {
2448b5b6f54SLucas Stach struct asix_common_private *dp = dev->driver_priv;
2458b5b6f54SLucas Stach struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
2468b5b6f54SLucas Stach
2478b5b6f54SLucas Stach return asix_rx_fixup_internal(dev, skb, rx);
2488b5b6f54SLucas Stach }
2498b5b6f54SLucas Stach
asix_rx_fixup_common_free(struct asix_common_private * dp)250d0c8f338SDean Jenkins void asix_rx_fixup_common_free(struct asix_common_private *dp)
251d0c8f338SDean Jenkins {
252d0c8f338SDean Jenkins struct asix_rx_fixup_info *rx;
253d0c8f338SDean Jenkins
254d0c8f338SDean Jenkins if (!dp)
255d0c8f338SDean Jenkins return;
256d0c8f338SDean Jenkins
257d0c8f338SDean Jenkins rx = &dp->rx_fixup_info;
258d0c8f338SDean Jenkins
259d0c8f338SDean Jenkins if (rx->ax_skb) {
260d0c8f338SDean Jenkins kfree_skb(rx->ax_skb);
261d0c8f338SDean Jenkins rx->ax_skb = NULL;
262d0c8f338SDean Jenkins }
263d0c8f338SDean Jenkins }
264d0c8f338SDean Jenkins
asix_tx_fixup(struct usbnet * dev,struct sk_buff * skb,gfp_t flags)265607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
266b4cdae20SChristian Riesch gfp_t flags)
267b4cdae20SChristian Riesch {
268b4cdae20SChristian Riesch int padlen;
269b4cdae20SChristian Riesch int headroom = skb_headroom(skb);
270b4cdae20SChristian Riesch int tailroom = skb_tailroom(skb);
271b4cdae20SChristian Riesch u32 packet_len;
272b4cdae20SChristian Riesch u32 padbytes = 0xffff0000;
2737e24b4edSChuhong Yuan void *ptr;
274b4cdae20SChristian Riesch
275b4cdae20SChristian Riesch padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
276b4cdae20SChristian Riesch
277b4cdae20SChristian Riesch /* We need to push 4 bytes in front of frame (packet_len)
278b4cdae20SChristian Riesch * and maybe add 4 bytes after the end (if padlen is 4)
279b4cdae20SChristian Riesch *
280b4cdae20SChristian Riesch * Avoid skb_copy_expand() expensive call, using following rules :
281b4cdae20SChristian Riesch * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
282b4cdae20SChristian Riesch * is false (and if we have 4 bytes of headroom)
283b4cdae20SChristian Riesch * - We are allowed to put 4 bytes at tail if skb_cloned()
284b4cdae20SChristian Riesch * is false (and if we have 4 bytes of tailroom)
285b4cdae20SChristian Riesch *
286242c1a28SGao Feng * TCP packets for example are cloned, but __skb_header_release()
287b4cdae20SChristian Riesch * was called in tcp stack, allowing us to use headroom for our needs.
288b4cdae20SChristian Riesch */
289b4cdae20SChristian Riesch if (!skb_header_cloned(skb) &&
290b4cdae20SChristian Riesch !(padlen && skb_cloned(skb)) &&
291b4cdae20SChristian Riesch headroom + tailroom >= 4 + padlen) {
292b4cdae20SChristian Riesch /* following should not happen, but better be safe */
293b4cdae20SChristian Riesch if (headroom < 4 ||
294b4cdae20SChristian Riesch tailroom < padlen) {
295b4cdae20SChristian Riesch skb->data = memmove(skb->head + 4, skb->data, skb->len);
296b4cdae20SChristian Riesch skb_set_tail_pointer(skb, skb->len);
297b4cdae20SChristian Riesch }
298b4cdae20SChristian Riesch } else {
299b4cdae20SChristian Riesch struct sk_buff *skb2;
300b4cdae20SChristian Riesch
301b4cdae20SChristian Riesch skb2 = skb_copy_expand(skb, 4, padlen, flags);
302b4cdae20SChristian Riesch dev_kfree_skb_any(skb);
303b4cdae20SChristian Riesch skb = skb2;
304b4cdae20SChristian Riesch if (!skb)
305b4cdae20SChristian Riesch return NULL;
306b4cdae20SChristian Riesch }
307b4cdae20SChristian Riesch
308b4cdae20SChristian Riesch packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
3097e24b4edSChuhong Yuan ptr = skb_push(skb, 4);
3107e24b4edSChuhong Yuan put_unaligned_le32(packet_len, ptr);
311b4cdae20SChristian Riesch
312b4cdae20SChristian Riesch if (padlen) {
3137e24b4edSChuhong Yuan put_unaligned_le32(padbytes, skb_tail_pointer(skb));
314b4cdae20SChristian Riesch skb_put(skb, sizeof(padbytes));
315b4cdae20SChristian Riesch }
3161e9e39f4SBen Hutchings
3177a1e890eSBen Hutchings usbnet_set_skb_tx_stats(skb, 1, 0);
318b4cdae20SChristian Riesch return skb;
319b4cdae20SChristian Riesch }
320b4cdae20SChristian Riesch
asix_read_phy_addr(struct usbnet * dev,bool internal)3217e88b11aSOleksij Rempel int asix_read_phy_addr(struct usbnet *dev, bool internal)
322b4cdae20SChristian Riesch {
3237e88b11aSOleksij Rempel int ret, offset;
324b4cdae20SChristian Riesch u8 buf[2];
325b4cdae20SChristian Riesch
3267e88b11aSOleksij Rempel ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0);
3277e88b11aSOleksij Rempel if (ret < 0)
3287e88b11aSOleksij Rempel goto error;
329b4cdae20SChristian Riesch
330a092b723SHimadri Pandya if (ret < 2) {
3317e88b11aSOleksij Rempel ret = -EIO;
3327e88b11aSOleksij Rempel goto error;
333b4cdae20SChristian Riesch }
3347e88b11aSOleksij Rempel
3357e88b11aSOleksij Rempel offset = (internal ? 1 : 0);
33616626b0cSChristian Riesch ret = buf[offset];
337b4cdae20SChristian Riesch
3387e88b11aSOleksij Rempel netdev_dbg(dev->net, "%s PHY address 0x%x\n",
3397e88b11aSOleksij Rempel internal ? "internal" : "external", ret);
3407e88b11aSOleksij Rempel
3417e88b11aSOleksij Rempel return ret;
3427e88b11aSOleksij Rempel
3437e88b11aSOleksij Rempel error:
3447e88b11aSOleksij Rempel netdev_err(dev->net, "Error reading PHY_ID register: %02x\n", ret);
3457e88b11aSOleksij Rempel
346b4cdae20SChristian Riesch return ret;
347b4cdae20SChristian Riesch }
348b4cdae20SChristian Riesch
asix_sw_reset(struct usbnet * dev,u8 flags,int in_pm)349d9fe64e5SRobert Foss int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
350b4cdae20SChristian Riesch {
351b4cdae20SChristian Riesch int ret;
352b4cdae20SChristian Riesch
353d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
354b4cdae20SChristian Riesch if (ret < 0)
355b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
356b4cdae20SChristian Riesch
357b4cdae20SChristian Riesch return ret;
358b4cdae20SChristian Riesch }
359b4cdae20SChristian Riesch
asix_read_rx_ctl(struct usbnet * dev,int in_pm)360d9fe64e5SRobert Foss u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
361b4cdae20SChristian Riesch {
362b4cdae20SChristian Riesch __le16 v;
363d9fe64e5SRobert Foss int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
364b4cdae20SChristian Riesch
365b4cdae20SChristian Riesch if (ret < 0) {
366b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
367b4cdae20SChristian Riesch goto out;
368b4cdae20SChristian Riesch }
369b4cdae20SChristian Riesch ret = le16_to_cpu(v);
370b4cdae20SChristian Riesch out:
371b4cdae20SChristian Riesch return ret;
372b4cdae20SChristian Riesch }
373b4cdae20SChristian Riesch
asix_write_rx_ctl(struct usbnet * dev,u16 mode,int in_pm)374d9fe64e5SRobert Foss int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
375b4cdae20SChristian Riesch {
376b4cdae20SChristian Riesch int ret;
377b4cdae20SChristian Riesch
378b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
379d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
380b4cdae20SChristian Riesch if (ret < 0)
381b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
382b4cdae20SChristian Riesch mode, ret);
383b4cdae20SChristian Riesch
384b4cdae20SChristian Riesch return ret;
385b4cdae20SChristian Riesch }
386b4cdae20SChristian Riesch
asix_read_medium_status(struct usbnet * dev,int in_pm)387d9fe64e5SRobert Foss u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
388b4cdae20SChristian Riesch {
389b4cdae20SChristian Riesch __le16 v;
390d9fe64e5SRobert Foss int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
391d9fe64e5SRobert Foss 0, 0, 2, &v, in_pm);
392b4cdae20SChristian Riesch
393b4cdae20SChristian Riesch if (ret < 0) {
394b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
395b4cdae20SChristian Riesch ret);
396b4cdae20SChristian Riesch return ret; /* TODO: callers not checking for error ret */
397b4cdae20SChristian Riesch }
398b4cdae20SChristian Riesch
399b4cdae20SChristian Riesch return le16_to_cpu(v);
400b4cdae20SChristian Riesch
401b4cdae20SChristian Riesch }
402b4cdae20SChristian Riesch
asix_write_medium_mode(struct usbnet * dev,u16 mode,int in_pm)403d9fe64e5SRobert Foss int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
404b4cdae20SChristian Riesch {
405b4cdae20SChristian Riesch int ret;
406b4cdae20SChristian Riesch
407b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
408d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
409d9fe64e5SRobert Foss mode, 0, 0, NULL, in_pm);
410b4cdae20SChristian Riesch if (ret < 0)
411b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
412b4cdae20SChristian Riesch mode, ret);
413b4cdae20SChristian Riesch
414b4cdae20SChristian Riesch return ret;
415b4cdae20SChristian Riesch }
416b4cdae20SChristian Riesch
417e532a096SOleksij Rempel /* set MAC link settings according to information from phylib */
asix_adjust_link(struct net_device * netdev)418e532a096SOleksij Rempel void asix_adjust_link(struct net_device *netdev)
419e532a096SOleksij Rempel {
420e532a096SOleksij Rempel struct phy_device *phydev = netdev->phydev;
421e532a096SOleksij Rempel struct usbnet *dev = netdev_priv(netdev);
422e532a096SOleksij Rempel u16 mode = 0;
423e532a096SOleksij Rempel
424e532a096SOleksij Rempel if (phydev->link) {
425e532a096SOleksij Rempel mode = AX88772_MEDIUM_DEFAULT;
426e532a096SOleksij Rempel
427e532a096SOleksij Rempel if (phydev->duplex == DUPLEX_HALF)
428e532a096SOleksij Rempel mode &= ~AX_MEDIUM_FD;
429e532a096SOleksij Rempel
430e532a096SOleksij Rempel if (phydev->speed != SPEED_100)
431e532a096SOleksij Rempel mode &= ~AX_MEDIUM_PS;
432e532a096SOleksij Rempel }
433e532a096SOleksij Rempel
434e532a096SOleksij Rempel asix_write_medium_mode(dev, mode, 0);
435e532a096SOleksij Rempel phy_print_status(phydev);
436805206e6SOleksij Rempel usbnet_link_change(dev, phydev->link, 0);
437e532a096SOleksij Rempel }
438e532a096SOleksij Rempel
asix_write_gpio(struct usbnet * dev,u16 value,int sleep,int in_pm)439d9fe64e5SRobert Foss int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
440b4cdae20SChristian Riesch {
441b4cdae20SChristian Riesch int ret;
442b4cdae20SChristian Riesch
443b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
444d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
445b4cdae20SChristian Riesch if (ret < 0)
446b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
447b4cdae20SChristian Riesch value, ret);
448b4cdae20SChristian Riesch
449b4cdae20SChristian Riesch if (sleep)
450b4cdae20SChristian Riesch msleep(sleep);
451b4cdae20SChristian Riesch
452b4cdae20SChristian Riesch return ret;
453b4cdae20SChristian Riesch }
454b4cdae20SChristian Riesch
455b4cdae20SChristian Riesch /*
456b4cdae20SChristian Riesch * AX88772 & AX88178 have a 16-bit RX_CTL value
457b4cdae20SChristian Riesch */
asix_set_multicast(struct net_device * net)458607740bcSChristian Riesch void asix_set_multicast(struct net_device *net)
459b4cdae20SChristian Riesch {
460b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net);
461b4cdae20SChristian Riesch struct asix_data *data = (struct asix_data *)&dev->data;
462b4cdae20SChristian Riesch u16 rx_ctl = AX_DEFAULT_RX_CTL;
463b4cdae20SChristian Riesch
464b4cdae20SChristian Riesch if (net->flags & IFF_PROMISC) {
465b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_PRO;
466b4cdae20SChristian Riesch } else if (net->flags & IFF_ALLMULTI ||
467b4cdae20SChristian Riesch netdev_mc_count(net) > AX_MAX_MCAST) {
468b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_AMALL;
469b4cdae20SChristian Riesch } else if (netdev_mc_empty(net)) {
470b4cdae20SChristian Riesch /* just broadcast and directed */
471b4cdae20SChristian Riesch } else {
472b4cdae20SChristian Riesch /* We use the 20 byte dev->data
473b4cdae20SChristian Riesch * for our 8 byte filter buffer
474b4cdae20SChristian Riesch * to avoid allocating memory that
475b4cdae20SChristian Riesch * is tricky to free later */
476b4cdae20SChristian Riesch struct netdev_hw_addr *ha;
477b4cdae20SChristian Riesch u32 crc_bits;
478b4cdae20SChristian Riesch
479b4cdae20SChristian Riesch memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
480b4cdae20SChristian Riesch
481b4cdae20SChristian Riesch /* Build the multicast hash filter. */
482b4cdae20SChristian Riesch netdev_for_each_mc_addr(ha, net) {
483b4cdae20SChristian Riesch crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
484b4cdae20SChristian Riesch data->multi_filter[crc_bits >> 3] |=
485b4cdae20SChristian Riesch 1 << (crc_bits & 7);
486b4cdae20SChristian Riesch }
487b4cdae20SChristian Riesch
488b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
489b4cdae20SChristian Riesch AX_MCAST_FILTER_SIZE, data->multi_filter);
490b4cdae20SChristian Riesch
491b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_AM;
492b4cdae20SChristian Riesch }
493b4cdae20SChristian Riesch
494b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
495b4cdae20SChristian Riesch }
496b4cdae20SChristian Riesch
__asix_mdio_read(struct net_device * netdev,int phy_id,int loc,bool in_pm)49789183b6eSOleksij Rempel static int __asix_mdio_read(struct net_device *netdev, int phy_id, int loc,
49889183b6eSOleksij Rempel bool in_pm)
499b4cdae20SChristian Riesch {
500b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(netdev);
501b4cdae20SChristian Riesch __le16 res;
5028a46f665SRobert Foss int ret;
503b4cdae20SChristian Riesch
504b4cdae20SChristian Riesch mutex_lock(&dev->phy_mutex);
505a786e319SPavel Skripkin
50689183b6eSOleksij Rempel ret = asix_check_host_enable(dev, in_pm);
507610df1d2SGuenter Roeck if (ret == -ENODEV || ret == -ETIMEDOUT) {
5088a46f665SRobert Foss mutex_unlock(&dev->phy_mutex);
5098a46f665SRobert Foss return ret;
5108a46f665SRobert Foss }
511d9fe64e5SRobert Foss
512d275afb6SOleksij Rempel ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2,
51389183b6eSOleksij Rempel &res, in_pm);
514d275afb6SOleksij Rempel if (ret < 0)
515d275afb6SOleksij Rempel goto out;
516d275afb6SOleksij Rempel
51789183b6eSOleksij Rempel ret = asix_set_hw_mii(dev, in_pm);
518d275afb6SOleksij Rempel out:
519b4cdae20SChristian Riesch mutex_unlock(&dev->phy_mutex);
520b4cdae20SChristian Riesch
521b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
522b4cdae20SChristian Riesch phy_id, loc, le16_to_cpu(res));
523b4cdae20SChristian Riesch
524d275afb6SOleksij Rempel return ret < 0 ? ret : le16_to_cpu(res);
525b4cdae20SChristian Riesch }
526b4cdae20SChristian Riesch
asix_mdio_read(struct net_device * netdev,int phy_id,int loc)52789183b6eSOleksij Rempel int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
52889183b6eSOleksij Rempel {
52989183b6eSOleksij Rempel return __asix_mdio_read(netdev, phy_id, loc, false);
53089183b6eSOleksij Rempel }
53189183b6eSOleksij Rempel
__asix_mdio_write(struct net_device * netdev,int phy_id,int loc,int val,bool in_pm)532d275afb6SOleksij Rempel static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc,
53389183b6eSOleksij Rempel int val, bool in_pm)
534b4cdae20SChristian Riesch {
535b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(netdev);
536b4cdae20SChristian Riesch __le16 res = cpu_to_le16(val);
5378a46f665SRobert Foss int ret;
538b4cdae20SChristian Riesch
539b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
540b4cdae20SChristian Riesch phy_id, loc, val);
541d9fe64e5SRobert Foss
542b4cdae20SChristian Riesch mutex_lock(&dev->phy_mutex);
543d275afb6SOleksij Rempel
54489183b6eSOleksij Rempel ret = asix_check_host_enable(dev, in_pm);
545d275afb6SOleksij Rempel if (ret == -ENODEV)
546d275afb6SOleksij Rempel goto out;
547d275afb6SOleksij Rempel
548d275afb6SOleksij Rempel ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2,
54989183b6eSOleksij Rempel &res, in_pm);
550d275afb6SOleksij Rempel if (ret < 0)
551d275afb6SOleksij Rempel goto out;
552d275afb6SOleksij Rempel
55389183b6eSOleksij Rempel ret = asix_set_hw_mii(dev, in_pm);
554d275afb6SOleksij Rempel out:
5558a46f665SRobert Foss mutex_unlock(&dev->phy_mutex);
556d275afb6SOleksij Rempel
557d275afb6SOleksij Rempel return ret < 0 ? ret : 0;
5588a46f665SRobert Foss }
559d9fe64e5SRobert Foss
asix_mdio_write(struct net_device * netdev,int phy_id,int loc,int val)560d275afb6SOleksij Rempel void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
561d275afb6SOleksij Rempel {
56289183b6eSOleksij Rempel __asix_mdio_write(netdev, phy_id, loc, val, false);
563d9fe64e5SRobert Foss }
564d9fe64e5SRobert Foss
565e532a096SOleksij Rempel /* MDIO read and write wrappers for phylib */
asix_mdio_bus_read(struct mii_bus * bus,int phy_id,int regnum)566e532a096SOleksij Rempel int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
567e532a096SOleksij Rempel {
568e532a096SOleksij Rempel struct usbnet *priv = bus->priv;
569e532a096SOleksij Rempel
57089183b6eSOleksij Rempel return __asix_mdio_read(priv->net, phy_id, regnum, false);
571e532a096SOleksij Rempel }
572e532a096SOleksij Rempel
asix_mdio_bus_write(struct mii_bus * bus,int phy_id,int regnum,u16 val)573e532a096SOleksij Rempel int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
574e532a096SOleksij Rempel {
575e532a096SOleksij Rempel struct usbnet *priv = bus->priv;
576e532a096SOleksij Rempel
57789183b6eSOleksij Rempel return __asix_mdio_write(priv->net, phy_id, regnum, val, false);
578e532a096SOleksij Rempel }
579e532a096SOleksij Rempel
asix_mdio_read_nopm(struct net_device * netdev,int phy_id,int loc)580d9fe64e5SRobert Foss int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
581d9fe64e5SRobert Foss {
58289183b6eSOleksij Rempel return __asix_mdio_read(netdev, phy_id, loc, true);
583d9fe64e5SRobert Foss }
584d9fe64e5SRobert Foss
585d9fe64e5SRobert Foss void
asix_mdio_write_nopm(struct net_device * netdev,int phy_id,int loc,int val)586d9fe64e5SRobert Foss asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
587d9fe64e5SRobert Foss {
58889183b6eSOleksij Rempel __asix_mdio_write(netdev, phy_id, loc, val, true);
589b4cdae20SChristian Riesch }
590b4cdae20SChristian Riesch
asix_get_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)591607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
592b4cdae20SChristian Riesch {
593b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net);
594b4cdae20SChristian Riesch u8 opt;
595b4cdae20SChristian Riesch
596d9fe64e5SRobert Foss if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
597d9fe64e5SRobert Foss 0, 0, 1, &opt, 0) < 0) {
598b4cdae20SChristian Riesch wolinfo->supported = 0;
599b4cdae20SChristian Riesch wolinfo->wolopts = 0;
600b4cdae20SChristian Riesch return;
601b4cdae20SChristian Riesch }
602b4cdae20SChristian Riesch wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
603b4cdae20SChristian Riesch wolinfo->wolopts = 0;
604b4cdae20SChristian Riesch if (opt & AX_MONITOR_LINK)
605b4cdae20SChristian Riesch wolinfo->wolopts |= WAKE_PHY;
606b4cdae20SChristian Riesch if (opt & AX_MONITOR_MAGIC)
607b4cdae20SChristian Riesch wolinfo->wolopts |= WAKE_MAGIC;
608b4cdae20SChristian Riesch }
609b4cdae20SChristian Riesch
asix_set_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)610607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
611b4cdae20SChristian Riesch {
612b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net);
613b4cdae20SChristian Riesch u8 opt = 0;
614b4cdae20SChristian Riesch
615c4ce446eSFlorian Fainelli if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
616c4ce446eSFlorian Fainelli return -EINVAL;
617c4ce446eSFlorian Fainelli
618b4cdae20SChristian Riesch if (wolinfo->wolopts & WAKE_PHY)
619b4cdae20SChristian Riesch opt |= AX_MONITOR_LINK;
620b4cdae20SChristian Riesch if (wolinfo->wolopts & WAKE_MAGIC)
621b4cdae20SChristian Riesch opt |= AX_MONITOR_MAGIC;
622b4cdae20SChristian Riesch
623b4cdae20SChristian Riesch if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
624d9fe64e5SRobert Foss opt, 0, 0, NULL, 0) < 0)
625b4cdae20SChristian Riesch return -EINVAL;
626b4cdae20SChristian Riesch
627b4cdae20SChristian Riesch return 0;
628b4cdae20SChristian Riesch }
629b4cdae20SChristian Riesch
asix_get_eeprom_len(struct net_device * net)630607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net)
631b4cdae20SChristian Riesch {
632ceb02c91SChristian Riesch return AX_EEPROM_LEN;
633b4cdae20SChristian Riesch }
634b4cdae20SChristian Riesch
asix_get_eeprom(struct net_device * net,struct ethtool_eeprom * eeprom,u8 * data)635607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
636607740bcSChristian Riesch u8 *data)
637b4cdae20SChristian Riesch {
638b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net);
639ceb02c91SChristian Riesch u16 *eeprom_buff;
640ceb02c91SChristian Riesch int first_word, last_word;
641b4cdae20SChristian Riesch int i;
642b4cdae20SChristian Riesch
643ceb02c91SChristian Riesch if (eeprom->len == 0)
644b4cdae20SChristian Riesch return -EINVAL;
645b4cdae20SChristian Riesch
646b4cdae20SChristian Riesch eeprom->magic = AX_EEPROM_MAGIC;
647b4cdae20SChristian Riesch
648ceb02c91SChristian Riesch first_word = eeprom->offset >> 1;
649ceb02c91SChristian Riesch last_word = (eeprom->offset + eeprom->len - 1) >> 1;
650ceb02c91SChristian Riesch
6516da2ec56SKees Cook eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
652ceb02c91SChristian Riesch GFP_KERNEL);
653ceb02c91SChristian Riesch if (!eeprom_buff)
654ceb02c91SChristian Riesch return -ENOMEM;
655ceb02c91SChristian Riesch
656b4cdae20SChristian Riesch /* ax8817x returns 2 bytes from eeprom on read */
657ceb02c91SChristian Riesch for (i = first_word; i <= last_word; i++) {
658ceb02c91SChristian Riesch if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
659d9fe64e5SRobert Foss &eeprom_buff[i - first_word], 0) < 0) {
660ceb02c91SChristian Riesch kfree(eeprom_buff);
661ceb02c91SChristian Riesch return -EIO;
662b4cdae20SChristian Riesch }
663ceb02c91SChristian Riesch }
664ceb02c91SChristian Riesch
665ceb02c91SChristian Riesch memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
666ceb02c91SChristian Riesch kfree(eeprom_buff);
667b4cdae20SChristian Riesch return 0;
668b4cdae20SChristian Riesch }
669b4cdae20SChristian Riesch
asix_set_eeprom(struct net_device * net,struct ethtool_eeprom * eeprom,u8 * data)670cb7b24cdSChristian Riesch int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
671cb7b24cdSChristian Riesch u8 *data)
672cb7b24cdSChristian Riesch {
673cb7b24cdSChristian Riesch struct usbnet *dev = netdev_priv(net);
674cb7b24cdSChristian Riesch u16 *eeprom_buff;
675cb7b24cdSChristian Riesch int first_word, last_word;
676cb7b24cdSChristian Riesch int i;
677cb7b24cdSChristian Riesch int ret;
678cb7b24cdSChristian Riesch
679cb7b24cdSChristian Riesch netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
680cb7b24cdSChristian Riesch eeprom->len, eeprom->offset, eeprom->magic);
681cb7b24cdSChristian Riesch
682cb7b24cdSChristian Riesch if (eeprom->len == 0)
683cb7b24cdSChristian Riesch return -EINVAL;
684cb7b24cdSChristian Riesch
685cb7b24cdSChristian Riesch if (eeprom->magic != AX_EEPROM_MAGIC)
686cb7b24cdSChristian Riesch return -EINVAL;
687cb7b24cdSChristian Riesch
688cb7b24cdSChristian Riesch first_word = eeprom->offset >> 1;
689cb7b24cdSChristian Riesch last_word = (eeprom->offset + eeprom->len - 1) >> 1;
690cb7b24cdSChristian Riesch
6916da2ec56SKees Cook eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
692cb7b24cdSChristian Riesch GFP_KERNEL);
693cb7b24cdSChristian Riesch if (!eeprom_buff)
694cb7b24cdSChristian Riesch return -ENOMEM;
695cb7b24cdSChristian Riesch
696cb7b24cdSChristian Riesch /* align data to 16 bit boundaries, read the missing data from
697cb7b24cdSChristian Riesch the EEPROM */
698cb7b24cdSChristian Riesch if (eeprom->offset & 1) {
699cb7b24cdSChristian Riesch ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
700d9fe64e5SRobert Foss &eeprom_buff[0], 0);
701cb7b24cdSChristian Riesch if (ret < 0) {
702cb7b24cdSChristian Riesch netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
703cb7b24cdSChristian Riesch goto free;
704cb7b24cdSChristian Riesch }
705cb7b24cdSChristian Riesch }
706cb7b24cdSChristian Riesch
707cb7b24cdSChristian Riesch if ((eeprom->offset + eeprom->len) & 1) {
708cb7b24cdSChristian Riesch ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
709d9fe64e5SRobert Foss &eeprom_buff[last_word - first_word], 0);
710cb7b24cdSChristian Riesch if (ret < 0) {
711cb7b24cdSChristian Riesch netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
712cb7b24cdSChristian Riesch goto free;
713cb7b24cdSChristian Riesch }
714cb7b24cdSChristian Riesch }
715cb7b24cdSChristian Riesch
716cb7b24cdSChristian Riesch memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
717cb7b24cdSChristian Riesch
718cb7b24cdSChristian Riesch /* write data to EEPROM */
719d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
720cb7b24cdSChristian Riesch if (ret < 0) {
721cb7b24cdSChristian Riesch netdev_err(net, "Failed to enable EEPROM write\n");
722cb7b24cdSChristian Riesch goto free;
723cb7b24cdSChristian Riesch }
724cb7b24cdSChristian Riesch msleep(20);
725cb7b24cdSChristian Riesch
726cb7b24cdSChristian Riesch for (i = first_word; i <= last_word; i++) {
727cb7b24cdSChristian Riesch netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
728cb7b24cdSChristian Riesch i, eeprom_buff[i - first_word]);
729cb7b24cdSChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
730d9fe64e5SRobert Foss eeprom_buff[i - first_word], 0, NULL, 0);
731cb7b24cdSChristian Riesch if (ret < 0) {
732cb7b24cdSChristian Riesch netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
733cb7b24cdSChristian Riesch i);
734cb7b24cdSChristian Riesch goto free;
735cb7b24cdSChristian Riesch }
736cb7b24cdSChristian Riesch msleep(20);
737cb7b24cdSChristian Riesch }
738cb7b24cdSChristian Riesch
739d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
740cb7b24cdSChristian Riesch if (ret < 0) {
741cb7b24cdSChristian Riesch netdev_err(net, "Failed to disable EEPROM write\n");
742cb7b24cdSChristian Riesch goto free;
743cb7b24cdSChristian Riesch }
744cb7b24cdSChristian Riesch
745cb7b24cdSChristian Riesch ret = 0;
746cb7b24cdSChristian Riesch free:
747cb7b24cdSChristian Riesch kfree(eeprom_buff);
748cb7b24cdSChristian Riesch return ret;
749cb7b24cdSChristian Riesch }
750cb7b24cdSChristian Riesch
asix_get_drvinfo(struct net_device * net,struct ethtool_drvinfo * info)751607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
752b4cdae20SChristian Riesch {
753b4cdae20SChristian Riesch /* Inherit standard device info */
754b4cdae20SChristian Riesch usbnet_get_drvinfo(net, info);
755*fb3ceec1SWolfram Sang strscpy(info->driver, DRIVER_NAME, sizeof(info->driver));
756*fb3ceec1SWolfram Sang strscpy(info->version, DRIVER_VERSION, sizeof(info->version));
757b4cdae20SChristian Riesch }
758b4cdae20SChristian Riesch
asix_set_mac_address(struct net_device * net,void * p)759607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p)
760b4cdae20SChristian Riesch {
761b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net);
762b4cdae20SChristian Riesch struct asix_data *data = (struct asix_data *)&dev->data;
763b4cdae20SChristian Riesch struct sockaddr *addr = p;
764b4cdae20SChristian Riesch
765b4cdae20SChristian Riesch if (netif_running(net))
766b4cdae20SChristian Riesch return -EBUSY;
767b4cdae20SChristian Riesch if (!is_valid_ether_addr(addr->sa_data))
768b4cdae20SChristian Riesch return -EADDRNOTAVAIL;
769b4cdae20SChristian Riesch
77016813717SJakub Kicinski eth_hw_addr_set(net, addr->sa_data);
771b4cdae20SChristian Riesch
772b4cdae20SChristian Riesch /* We use the 20 byte dev->data
773b4cdae20SChristian Riesch * for our 6 byte mac buffer
774b4cdae20SChristian Riesch * to avoid allocating memory that
775b4cdae20SChristian Riesch * is tricky to free later */
776b4cdae20SChristian Riesch memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
777b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
778b4cdae20SChristian Riesch data->mac_addr);
779b4cdae20SChristian Riesch
780b4cdae20SChristian Riesch return 0;
781b4cdae20SChristian Riesch }
782