xref: /openbmc/linux/drivers/net/usb/asix_common.c (revision 607740bc1605395c96b09416cdcc5e77c8976eed)
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
19b4cdae20SChristian Riesch  * along with this program; if not, write to the Free Software
20b4cdae20SChristian Riesch  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21b4cdae20SChristian Riesch  */
22b4cdae20SChristian Riesch 
23*607740bcSChristian Riesch #include "asix.h"
24b4cdae20SChristian Riesch 
25*607740bcSChristian Riesch int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
26b4cdae20SChristian Riesch 		  u16 size, void *data)
27b4cdae20SChristian Riesch {
28b4cdae20SChristian Riesch 	void *buf;
29b4cdae20SChristian Riesch 	int err = -ENOMEM;
30b4cdae20SChristian Riesch 
31b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
32b4cdae20SChristian Riesch 		   cmd, value, index, size);
33b4cdae20SChristian Riesch 
34b4cdae20SChristian Riesch 	buf = kmalloc(size, GFP_KERNEL);
35b4cdae20SChristian Riesch 	if (!buf)
36b4cdae20SChristian Riesch 		goto out;
37b4cdae20SChristian Riesch 
38b4cdae20SChristian Riesch 	err = usb_control_msg(
39b4cdae20SChristian Riesch 		dev->udev,
40b4cdae20SChristian Riesch 		usb_rcvctrlpipe(dev->udev, 0),
41b4cdae20SChristian Riesch 		cmd,
42b4cdae20SChristian Riesch 		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
43b4cdae20SChristian Riesch 		value,
44b4cdae20SChristian Riesch 		index,
45b4cdae20SChristian Riesch 		buf,
46b4cdae20SChristian Riesch 		size,
47b4cdae20SChristian Riesch 		USB_CTRL_GET_TIMEOUT);
48b4cdae20SChristian Riesch 	if (err == size)
49b4cdae20SChristian Riesch 		memcpy(data, buf, size);
50b4cdae20SChristian Riesch 	else if (err >= 0)
51b4cdae20SChristian Riesch 		err = -EINVAL;
52b4cdae20SChristian Riesch 	kfree(buf);
53b4cdae20SChristian Riesch 
54b4cdae20SChristian Riesch out:
55b4cdae20SChristian Riesch 	return err;
56b4cdae20SChristian Riesch }
57b4cdae20SChristian Riesch 
58*607740bcSChristian Riesch int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
59b4cdae20SChristian Riesch 		   u16 size, void *data)
60b4cdae20SChristian Riesch {
61b4cdae20SChristian Riesch 	void *buf = NULL;
62b4cdae20SChristian Riesch 	int err = -ENOMEM;
63b4cdae20SChristian Riesch 
64b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
65b4cdae20SChristian Riesch 		   cmd, value, index, size);
66b4cdae20SChristian Riesch 
67b4cdae20SChristian Riesch 	if (data) {
68b4cdae20SChristian Riesch 		buf = kmemdup(data, size, GFP_KERNEL);
69b4cdae20SChristian Riesch 		if (!buf)
70b4cdae20SChristian Riesch 			goto out;
71b4cdae20SChristian Riesch 	}
72b4cdae20SChristian Riesch 
73b4cdae20SChristian Riesch 	err = usb_control_msg(
74b4cdae20SChristian Riesch 		dev->udev,
75b4cdae20SChristian Riesch 		usb_sndctrlpipe(dev->udev, 0),
76b4cdae20SChristian Riesch 		cmd,
77b4cdae20SChristian Riesch 		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
78b4cdae20SChristian Riesch 		value,
79b4cdae20SChristian Riesch 		index,
80b4cdae20SChristian Riesch 		buf,
81b4cdae20SChristian Riesch 		size,
82b4cdae20SChristian Riesch 		USB_CTRL_SET_TIMEOUT);
83b4cdae20SChristian Riesch 	kfree(buf);
84b4cdae20SChristian Riesch 
85b4cdae20SChristian Riesch out:
86b4cdae20SChristian Riesch 	return err;
87b4cdae20SChristian Riesch }
88b4cdae20SChristian Riesch 
89b4cdae20SChristian Riesch static void asix_async_cmd_callback(struct urb *urb)
90b4cdae20SChristian Riesch {
91b4cdae20SChristian Riesch 	struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
92b4cdae20SChristian Riesch 	int status = urb->status;
93b4cdae20SChristian Riesch 
94b4cdae20SChristian Riesch 	if (status < 0)
95b4cdae20SChristian Riesch 		printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
96b4cdae20SChristian Riesch 			status);
97b4cdae20SChristian Riesch 
98b4cdae20SChristian Riesch 	kfree(req);
99b4cdae20SChristian Riesch 	usb_free_urb(urb);
100b4cdae20SChristian Riesch }
101b4cdae20SChristian Riesch 
102*607740bcSChristian Riesch void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
103b4cdae20SChristian Riesch 			  u16 size, void *data)
104b4cdae20SChristian Riesch {
105b4cdae20SChristian Riesch 	struct usb_ctrlrequest *req;
106b4cdae20SChristian Riesch 	int status;
107b4cdae20SChristian Riesch 	struct urb *urb;
108b4cdae20SChristian Riesch 
109b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_cmd_async() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
110b4cdae20SChristian Riesch 		   cmd, value, index, size);
111b4cdae20SChristian Riesch 
112b4cdae20SChristian Riesch 	urb = usb_alloc_urb(0, GFP_ATOMIC);
113b4cdae20SChristian Riesch 	if (!urb) {
114b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error allocating URB in write_cmd_async!\n");
115b4cdae20SChristian Riesch 		return;
116b4cdae20SChristian Riesch 	}
117b4cdae20SChristian Riesch 
118b4cdae20SChristian Riesch 	req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
119b4cdae20SChristian Riesch 	if (!req) {
120b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to allocate memory for control request\n");
121b4cdae20SChristian Riesch 		usb_free_urb(urb);
122b4cdae20SChristian Riesch 		return;
123b4cdae20SChristian Riesch 	}
124b4cdae20SChristian Riesch 
125b4cdae20SChristian Riesch 	req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
126b4cdae20SChristian Riesch 	req->bRequest = cmd;
127b4cdae20SChristian Riesch 	req->wValue = cpu_to_le16(value);
128b4cdae20SChristian Riesch 	req->wIndex = cpu_to_le16(index);
129b4cdae20SChristian Riesch 	req->wLength = cpu_to_le16(size);
130b4cdae20SChristian Riesch 
131b4cdae20SChristian Riesch 	usb_fill_control_urb(urb, dev->udev,
132b4cdae20SChristian Riesch 			     usb_sndctrlpipe(dev->udev, 0),
133b4cdae20SChristian Riesch 			     (void *)req, data, size,
134b4cdae20SChristian Riesch 			     asix_async_cmd_callback, req);
135b4cdae20SChristian Riesch 
136b4cdae20SChristian Riesch 	status = usb_submit_urb(urb, GFP_ATOMIC);
137b4cdae20SChristian Riesch 	if (status < 0) {
138b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error submitting the control message: status=%d\n",
139b4cdae20SChristian Riesch 			   status);
140b4cdae20SChristian Riesch 		kfree(req);
141b4cdae20SChristian Riesch 		usb_free_urb(urb);
142b4cdae20SChristian Riesch 	}
143b4cdae20SChristian Riesch }
144b4cdae20SChristian Riesch 
145*607740bcSChristian Riesch int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
146b4cdae20SChristian Riesch {
147b4cdae20SChristian Riesch 	int offset = 0;
148b4cdae20SChristian Riesch 
149b4cdae20SChristian Riesch 	while (offset + sizeof(u32) < skb->len) {
150b4cdae20SChristian Riesch 		struct sk_buff *ax_skb;
151b4cdae20SChristian Riesch 		u16 size;
152b4cdae20SChristian Riesch 		u32 header = get_unaligned_le32(skb->data + offset);
153b4cdae20SChristian Riesch 
154b4cdae20SChristian Riesch 		offset += sizeof(u32);
155b4cdae20SChristian Riesch 
156b4cdae20SChristian Riesch 		/* get the packet length */
157b4cdae20SChristian Riesch 		size = (u16) (header & 0x7ff);
158b4cdae20SChristian Riesch 		if (size != ((~header >> 16) & 0x07ff)) {
159b4cdae20SChristian Riesch 			netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
160b4cdae20SChristian Riesch 			return 0;
161b4cdae20SChristian Riesch 		}
162b4cdae20SChristian Riesch 
163b4cdae20SChristian Riesch 		if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
164b4cdae20SChristian Riesch 		    (size + offset > skb->len)) {
165b4cdae20SChristian Riesch 			netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
166b4cdae20SChristian Riesch 				   size);
167b4cdae20SChristian Riesch 			return 0;
168b4cdae20SChristian Riesch 		}
169b4cdae20SChristian Riesch 		ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
170b4cdae20SChristian Riesch 		if (!ax_skb)
171b4cdae20SChristian Riesch 			return 0;
172b4cdae20SChristian Riesch 
173b4cdae20SChristian Riesch 		skb_put(ax_skb, size);
174b4cdae20SChristian Riesch 		memcpy(ax_skb->data, skb->data + offset, size);
175b4cdae20SChristian Riesch 		usbnet_skb_return(dev, ax_skb);
176b4cdae20SChristian Riesch 
177b4cdae20SChristian Riesch 		offset += (size + 1) & 0xfffe;
178b4cdae20SChristian Riesch 	}
179b4cdae20SChristian Riesch 
180b4cdae20SChristian Riesch 	if (skb->len != offset) {
181b4cdae20SChristian Riesch 		netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
182b4cdae20SChristian Riesch 			   skb->len);
183b4cdae20SChristian Riesch 		return 0;
184b4cdae20SChristian Riesch 	}
185b4cdae20SChristian Riesch 	return 1;
186b4cdae20SChristian Riesch }
187b4cdae20SChristian Riesch 
188*607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
189b4cdae20SChristian Riesch 			      gfp_t flags)
190b4cdae20SChristian Riesch {
191b4cdae20SChristian Riesch 	int padlen;
192b4cdae20SChristian Riesch 	int headroom = skb_headroom(skb);
193b4cdae20SChristian Riesch 	int tailroom = skb_tailroom(skb);
194b4cdae20SChristian Riesch 	u32 packet_len;
195b4cdae20SChristian Riesch 	u32 padbytes = 0xffff0000;
196b4cdae20SChristian Riesch 
197b4cdae20SChristian Riesch 	padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
198b4cdae20SChristian Riesch 
199b4cdae20SChristian Riesch 	/* We need to push 4 bytes in front of frame (packet_len)
200b4cdae20SChristian Riesch 	 * and maybe add 4 bytes after the end (if padlen is 4)
201b4cdae20SChristian Riesch 	 *
202b4cdae20SChristian Riesch 	 * Avoid skb_copy_expand() expensive call, using following rules :
203b4cdae20SChristian Riesch 	 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
204b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of headroom)
205b4cdae20SChristian Riesch 	 * - We are allowed to put 4 bytes at tail if skb_cloned()
206b4cdae20SChristian Riesch 	 *   is false (and if we have 4 bytes of tailroom)
207b4cdae20SChristian Riesch 	 *
208b4cdae20SChristian Riesch 	 * TCP packets for example are cloned, but skb_header_release()
209b4cdae20SChristian Riesch 	 * was called in tcp stack, allowing us to use headroom for our needs.
210b4cdae20SChristian Riesch 	 */
211b4cdae20SChristian Riesch 	if (!skb_header_cloned(skb) &&
212b4cdae20SChristian Riesch 	    !(padlen && skb_cloned(skb)) &&
213b4cdae20SChristian Riesch 	    headroom + tailroom >= 4 + padlen) {
214b4cdae20SChristian Riesch 		/* following should not happen, but better be safe */
215b4cdae20SChristian Riesch 		if (headroom < 4 ||
216b4cdae20SChristian Riesch 		    tailroom < padlen) {
217b4cdae20SChristian Riesch 			skb->data = memmove(skb->head + 4, skb->data, skb->len);
218b4cdae20SChristian Riesch 			skb_set_tail_pointer(skb, skb->len);
219b4cdae20SChristian Riesch 		}
220b4cdae20SChristian Riesch 	} else {
221b4cdae20SChristian Riesch 		struct sk_buff *skb2;
222b4cdae20SChristian Riesch 
223b4cdae20SChristian Riesch 		skb2 = skb_copy_expand(skb, 4, padlen, flags);
224b4cdae20SChristian Riesch 		dev_kfree_skb_any(skb);
225b4cdae20SChristian Riesch 		skb = skb2;
226b4cdae20SChristian Riesch 		if (!skb)
227b4cdae20SChristian Riesch 			return NULL;
228b4cdae20SChristian Riesch 	}
229b4cdae20SChristian Riesch 
230b4cdae20SChristian Riesch 	packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
231b4cdae20SChristian Riesch 	skb_push(skb, 4);
232b4cdae20SChristian Riesch 	cpu_to_le32s(&packet_len);
233b4cdae20SChristian Riesch 	skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
234b4cdae20SChristian Riesch 
235b4cdae20SChristian Riesch 	if (padlen) {
236b4cdae20SChristian Riesch 		cpu_to_le32s(&padbytes);
237b4cdae20SChristian Riesch 		memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
238b4cdae20SChristian Riesch 		skb_put(skb, sizeof(padbytes));
239b4cdae20SChristian Riesch 	}
240b4cdae20SChristian Riesch 	return skb;
241b4cdae20SChristian Riesch }
242b4cdae20SChristian Riesch 
243*607740bcSChristian Riesch int asix_set_sw_mii(struct usbnet *dev)
244b4cdae20SChristian Riesch {
245b4cdae20SChristian Riesch 	int ret;
246b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
247b4cdae20SChristian Riesch 	if (ret < 0)
248b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable software MII access\n");
249b4cdae20SChristian Riesch 	return ret;
250b4cdae20SChristian Riesch }
251b4cdae20SChristian Riesch 
252*607740bcSChristian Riesch int asix_set_hw_mii(struct usbnet *dev)
253b4cdae20SChristian Riesch {
254b4cdae20SChristian Riesch 	int ret;
255b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
256b4cdae20SChristian Riesch 	if (ret < 0)
257b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to enable hardware MII access\n");
258b4cdae20SChristian Riesch 	return ret;
259b4cdae20SChristian Riesch }
260b4cdae20SChristian Riesch 
261*607740bcSChristian Riesch int asix_get_phy_addr(struct usbnet *dev)
262b4cdae20SChristian Riesch {
263b4cdae20SChristian Riesch 	u8 buf[2];
264b4cdae20SChristian Riesch 	int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
265b4cdae20SChristian Riesch 
266b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr()\n");
267b4cdae20SChristian Riesch 
268b4cdae20SChristian Riesch 	if (ret < 0) {
269b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
270b4cdae20SChristian Riesch 		goto out;
271b4cdae20SChristian Riesch 	}
272b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
273b4cdae20SChristian Riesch 		   *((__le16 *)buf));
274b4cdae20SChristian Riesch 	ret = buf[1];
275b4cdae20SChristian Riesch 
276b4cdae20SChristian Riesch out:
277b4cdae20SChristian Riesch 	return ret;
278b4cdae20SChristian Riesch }
279b4cdae20SChristian Riesch 
280*607740bcSChristian Riesch int asix_sw_reset(struct usbnet *dev, u8 flags)
281b4cdae20SChristian Riesch {
282b4cdae20SChristian Riesch 	int ret;
283b4cdae20SChristian Riesch 
284b4cdae20SChristian Riesch         ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
285b4cdae20SChristian Riesch 	if (ret < 0)
286b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
287b4cdae20SChristian Riesch 
288b4cdae20SChristian Riesch 	return ret;
289b4cdae20SChristian Riesch }
290b4cdae20SChristian Riesch 
291*607740bcSChristian Riesch u16 asix_read_rx_ctl(struct usbnet *dev)
292b4cdae20SChristian Riesch {
293b4cdae20SChristian Riesch 	__le16 v;
294b4cdae20SChristian Riesch 	int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
295b4cdae20SChristian Riesch 
296b4cdae20SChristian Riesch 	if (ret < 0) {
297b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
298b4cdae20SChristian Riesch 		goto out;
299b4cdae20SChristian Riesch 	}
300b4cdae20SChristian Riesch 	ret = le16_to_cpu(v);
301b4cdae20SChristian Riesch out:
302b4cdae20SChristian Riesch 	return ret;
303b4cdae20SChristian Riesch }
304b4cdae20SChristian Riesch 
305*607740bcSChristian Riesch int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
306b4cdae20SChristian Riesch {
307b4cdae20SChristian Riesch 	int ret;
308b4cdae20SChristian Riesch 
309b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
310b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
311b4cdae20SChristian Riesch 	if (ret < 0)
312b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
313b4cdae20SChristian Riesch 			   mode, ret);
314b4cdae20SChristian Riesch 
315b4cdae20SChristian Riesch 	return ret;
316b4cdae20SChristian Riesch }
317b4cdae20SChristian Riesch 
318*607740bcSChristian Riesch u16 asix_read_medium_status(struct usbnet *dev)
319b4cdae20SChristian Riesch {
320b4cdae20SChristian Riesch 	__le16 v;
321b4cdae20SChristian Riesch 	int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
322b4cdae20SChristian Riesch 
323b4cdae20SChristian Riesch 	if (ret < 0) {
324b4cdae20SChristian Riesch 		netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
325b4cdae20SChristian Riesch 			   ret);
326b4cdae20SChristian Riesch 		return ret;	/* TODO: callers not checking for error ret */
327b4cdae20SChristian Riesch 	}
328b4cdae20SChristian Riesch 
329b4cdae20SChristian Riesch 	return le16_to_cpu(v);
330b4cdae20SChristian Riesch 
331b4cdae20SChristian Riesch }
332b4cdae20SChristian Riesch 
333*607740bcSChristian Riesch int asix_write_medium_mode(struct usbnet *dev, u16 mode)
334b4cdae20SChristian Riesch {
335b4cdae20SChristian Riesch 	int ret;
336b4cdae20SChristian Riesch 
337b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
338b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
339b4cdae20SChristian Riesch 	if (ret < 0)
340b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
341b4cdae20SChristian Riesch 			   mode, ret);
342b4cdae20SChristian Riesch 
343b4cdae20SChristian Riesch 	return ret;
344b4cdae20SChristian Riesch }
345b4cdae20SChristian Riesch 
346*607740bcSChristian Riesch int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
347b4cdae20SChristian Riesch {
348b4cdae20SChristian Riesch 	int ret;
349b4cdae20SChristian Riesch 
350b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
351b4cdae20SChristian Riesch 	ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
352b4cdae20SChristian Riesch 	if (ret < 0)
353b4cdae20SChristian Riesch 		netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
354b4cdae20SChristian Riesch 			   value, ret);
355b4cdae20SChristian Riesch 
356b4cdae20SChristian Riesch 	if (sleep)
357b4cdae20SChristian Riesch 		msleep(sleep);
358b4cdae20SChristian Riesch 
359b4cdae20SChristian Riesch 	return ret;
360b4cdae20SChristian Riesch }
361b4cdae20SChristian Riesch 
362b4cdae20SChristian Riesch /*
363b4cdae20SChristian Riesch  * AX88772 & AX88178 have a 16-bit RX_CTL value
364b4cdae20SChristian Riesch  */
365*607740bcSChristian Riesch void asix_set_multicast(struct net_device *net)
366b4cdae20SChristian Riesch {
367b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
368b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
369b4cdae20SChristian Riesch 	u16 rx_ctl = AX_DEFAULT_RX_CTL;
370b4cdae20SChristian Riesch 
371b4cdae20SChristian Riesch 	if (net->flags & IFF_PROMISC) {
372b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_PRO;
373b4cdae20SChristian Riesch 	} else if (net->flags & IFF_ALLMULTI ||
374b4cdae20SChristian Riesch 		   netdev_mc_count(net) > AX_MAX_MCAST) {
375b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AMALL;
376b4cdae20SChristian Riesch 	} else if (netdev_mc_empty(net)) {
377b4cdae20SChristian Riesch 		/* just broadcast and directed */
378b4cdae20SChristian Riesch 	} else {
379b4cdae20SChristian Riesch 		/* We use the 20 byte dev->data
380b4cdae20SChristian Riesch 		 * for our 8 byte filter buffer
381b4cdae20SChristian Riesch 		 * to avoid allocating memory that
382b4cdae20SChristian Riesch 		 * is tricky to free later */
383b4cdae20SChristian Riesch 		struct netdev_hw_addr *ha;
384b4cdae20SChristian Riesch 		u32 crc_bits;
385b4cdae20SChristian Riesch 
386b4cdae20SChristian Riesch 		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
387b4cdae20SChristian Riesch 
388b4cdae20SChristian Riesch 		/* Build the multicast hash filter. */
389b4cdae20SChristian Riesch 		netdev_for_each_mc_addr(ha, net) {
390b4cdae20SChristian Riesch 			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
391b4cdae20SChristian Riesch 			data->multi_filter[crc_bits >> 3] |=
392b4cdae20SChristian Riesch 			    1 << (crc_bits & 7);
393b4cdae20SChristian Riesch 		}
394b4cdae20SChristian Riesch 
395b4cdae20SChristian Riesch 		asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
396b4cdae20SChristian Riesch 				   AX_MCAST_FILTER_SIZE, data->multi_filter);
397b4cdae20SChristian Riesch 
398b4cdae20SChristian Riesch 		rx_ctl |= AX_RX_CTL_AM;
399b4cdae20SChristian Riesch 	}
400b4cdae20SChristian Riesch 
401b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
402b4cdae20SChristian Riesch }
403b4cdae20SChristian Riesch 
404*607740bcSChristian Riesch int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
405b4cdae20SChristian Riesch {
406b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
407b4cdae20SChristian Riesch 	__le16 res;
408b4cdae20SChristian Riesch 
409b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
410b4cdae20SChristian Riesch 	asix_set_sw_mii(dev);
411b4cdae20SChristian Riesch 	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
412b4cdae20SChristian Riesch 				(__u16)loc, 2, &res);
413b4cdae20SChristian Riesch 	asix_set_hw_mii(dev);
414b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
415b4cdae20SChristian Riesch 
416b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
417b4cdae20SChristian Riesch 		   phy_id, loc, le16_to_cpu(res));
418b4cdae20SChristian Riesch 
419b4cdae20SChristian Riesch 	return le16_to_cpu(res);
420b4cdae20SChristian Riesch }
421b4cdae20SChristian Riesch 
422*607740bcSChristian Riesch void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
423b4cdae20SChristian Riesch {
424b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(netdev);
425b4cdae20SChristian Riesch 	__le16 res = cpu_to_le16(val);
426b4cdae20SChristian Riesch 
427b4cdae20SChristian Riesch 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
428b4cdae20SChristian Riesch 		   phy_id, loc, val);
429b4cdae20SChristian Riesch 	mutex_lock(&dev->phy_mutex);
430b4cdae20SChristian Riesch 	asix_set_sw_mii(dev);
431b4cdae20SChristian Riesch 	asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
432b4cdae20SChristian Riesch 	asix_set_hw_mii(dev);
433b4cdae20SChristian Riesch 	mutex_unlock(&dev->phy_mutex);
434b4cdae20SChristian Riesch }
435b4cdae20SChristian Riesch 
436*607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
437b4cdae20SChristian Riesch {
438b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
439b4cdae20SChristian Riesch 	u8 opt;
440b4cdae20SChristian Riesch 
441b4cdae20SChristian Riesch 	if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
442b4cdae20SChristian Riesch 		wolinfo->supported = 0;
443b4cdae20SChristian Riesch 		wolinfo->wolopts = 0;
444b4cdae20SChristian Riesch 		return;
445b4cdae20SChristian Riesch 	}
446b4cdae20SChristian Riesch 	wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
447b4cdae20SChristian Riesch 	wolinfo->wolopts = 0;
448b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_LINK)
449b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_PHY;
450b4cdae20SChristian Riesch 	if (opt & AX_MONITOR_MAGIC)
451b4cdae20SChristian Riesch 		wolinfo->wolopts |= WAKE_MAGIC;
452b4cdae20SChristian Riesch }
453b4cdae20SChristian Riesch 
454*607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
455b4cdae20SChristian Riesch {
456b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
457b4cdae20SChristian Riesch 	u8 opt = 0;
458b4cdae20SChristian Riesch 
459b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_PHY)
460b4cdae20SChristian Riesch 		opt |= AX_MONITOR_LINK;
461b4cdae20SChristian Riesch 	if (wolinfo->wolopts & WAKE_MAGIC)
462b4cdae20SChristian Riesch 		opt |= AX_MONITOR_MAGIC;
463b4cdae20SChristian Riesch 
464b4cdae20SChristian Riesch 	if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
465b4cdae20SChristian Riesch 			      opt, 0, 0, NULL) < 0)
466b4cdae20SChristian Riesch 		return -EINVAL;
467b4cdae20SChristian Riesch 
468b4cdae20SChristian Riesch 	return 0;
469b4cdae20SChristian Riesch }
470b4cdae20SChristian Riesch 
471*607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net)
472b4cdae20SChristian Riesch {
473b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
474b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
475b4cdae20SChristian Riesch 
476b4cdae20SChristian Riesch 	return data->eeprom_len;
477b4cdae20SChristian Riesch }
478b4cdae20SChristian Riesch 
479*607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
480*607740bcSChristian Riesch 		    u8 *data)
481b4cdae20SChristian Riesch {
482b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
483b4cdae20SChristian Riesch 	__le16 *ebuf = (__le16 *)data;
484b4cdae20SChristian Riesch 	int i;
485b4cdae20SChristian Riesch 
486b4cdae20SChristian Riesch 	/* Crude hack to ensure that we don't overwrite memory
487b4cdae20SChristian Riesch 	 * if an odd length is supplied
488b4cdae20SChristian Riesch 	 */
489b4cdae20SChristian Riesch 	if (eeprom->len % 2)
490b4cdae20SChristian Riesch 		return -EINVAL;
491b4cdae20SChristian Riesch 
492b4cdae20SChristian Riesch 	eeprom->magic = AX_EEPROM_MAGIC;
493b4cdae20SChristian Riesch 
494b4cdae20SChristian Riesch 	/* ax8817x returns 2 bytes from eeprom on read */
495b4cdae20SChristian Riesch 	for (i=0; i < eeprom->len / 2; i++) {
496b4cdae20SChristian Riesch 		if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
497b4cdae20SChristian Riesch 			eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
498b4cdae20SChristian Riesch 			return -EINVAL;
499b4cdae20SChristian Riesch 	}
500b4cdae20SChristian Riesch 	return 0;
501b4cdae20SChristian Riesch }
502b4cdae20SChristian Riesch 
503*607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
504b4cdae20SChristian Riesch {
505b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
506b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
507b4cdae20SChristian Riesch 
508b4cdae20SChristian Riesch 	/* Inherit standard device info */
509b4cdae20SChristian Riesch 	usbnet_get_drvinfo(net, info);
510b4cdae20SChristian Riesch 	strncpy (info->driver, DRIVER_NAME, sizeof info->driver);
511b4cdae20SChristian Riesch 	strncpy (info->version, DRIVER_VERSION, sizeof info->version);
512b4cdae20SChristian Riesch 	info->eedump_len = data->eeprom_len;
513b4cdae20SChristian Riesch }
514b4cdae20SChristian Riesch 
515*607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p)
516b4cdae20SChristian Riesch {
517b4cdae20SChristian Riesch 	struct usbnet *dev = netdev_priv(net);
518b4cdae20SChristian Riesch 	struct asix_data *data = (struct asix_data *)&dev->data;
519b4cdae20SChristian Riesch 	struct sockaddr *addr = p;
520b4cdae20SChristian Riesch 
521b4cdae20SChristian Riesch 	if (netif_running(net))
522b4cdae20SChristian Riesch 		return -EBUSY;
523b4cdae20SChristian Riesch 	if (!is_valid_ether_addr(addr->sa_data))
524b4cdae20SChristian Riesch 		return -EADDRNOTAVAIL;
525b4cdae20SChristian Riesch 
526b4cdae20SChristian Riesch 	memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
527b4cdae20SChristian Riesch 
528b4cdae20SChristian Riesch 	/* We use the 20 byte dev->data
529b4cdae20SChristian Riesch 	 * for our 6 byte mac buffer
530b4cdae20SChristian Riesch 	 * to avoid allocating memory that
531b4cdae20SChristian Riesch 	 * is tricky to free later */
532b4cdae20SChristian Riesch 	memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
533b4cdae20SChristian Riesch 	asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
534b4cdae20SChristian Riesch 							data->mac_addr);
535b4cdae20SChristian Riesch 
536b4cdae20SChristian Riesch 	return 0;
537b4cdae20SChristian Riesch }
538