blob: 763364f0972e3657327107b37e30a5e809e03eb4 [file] [log] [blame]
David Brownell2e55cc72005-08-31 09:53:10 -07001/*
2 * ASIX AX8817X based USB 2.0 Ethernet Devices
David Hollis933a27d2006-07-29 10:12:50 -04003 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
David Brownell2e55cc72005-08-31 09:53:10 -07004 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
David Hollis933a27d2006-07-29 10:12:50 -04005 * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
David Brownell2e55cc72005-08-31 09:53:10 -07006 * Copyright (c) 2002-2003 TiVo Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23// #define DEBUG // error path messages, extra info
24// #define VERBOSE // more; success messages
25
David Brownell2e55cc72005-08-31 09:53:10 -070026#include <linux/module.h>
27#include <linux/kmod.h>
David Brownell2e55cc72005-08-31 09:53:10 -070028#include <linux/init.h>
29#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/ethtool.h>
32#include <linux/workqueue.h>
33#include <linux/mii.h>
34#include <linux/usb.h>
35#include <linux/crc32.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020036#include <linux/usb/usbnet.h>
David Brownell2e55cc72005-08-31 09:53:10 -070037
David Hollis933a27d2006-07-29 10:12:50 -040038#define DRIVER_VERSION "14-Jun-2006"
39static const char driver_name [] = "asix";
40
David Brownell2e55cc72005-08-31 09:53:10 -070041/* ASIX AX8817X based USB 2.0 Ethernet Devices */
42
43#define AX_CMD_SET_SW_MII 0x06
44#define AX_CMD_READ_MII_REG 0x07
45#define AX_CMD_WRITE_MII_REG 0x08
46#define AX_CMD_SET_HW_MII 0x0a
47#define AX_CMD_READ_EEPROM 0x0b
48#define AX_CMD_WRITE_EEPROM 0x0c
49#define AX_CMD_WRITE_ENABLE 0x0d
50#define AX_CMD_WRITE_DISABLE 0x0e
David Hollis933a27d2006-07-29 10:12:50 -040051#define AX_CMD_READ_RX_CTL 0x0f
David Brownell2e55cc72005-08-31 09:53:10 -070052#define AX_CMD_WRITE_RX_CTL 0x10
53#define AX_CMD_READ_IPG012 0x11
54#define AX_CMD_WRITE_IPG0 0x12
55#define AX_CMD_WRITE_IPG1 0x13
David Hollis933a27d2006-07-29 10:12:50 -040056#define AX_CMD_READ_NODE_ID 0x13
Jussi Kivilinna7f29a3b2010-03-09 12:24:38 +000057#define AX_CMD_WRITE_NODE_ID 0x14
David Brownell2e55cc72005-08-31 09:53:10 -070058#define AX_CMD_WRITE_IPG2 0x14
59#define AX_CMD_WRITE_MULTI_FILTER 0x16
David Hollis933a27d2006-07-29 10:12:50 -040060#define AX88172_CMD_READ_NODE_ID 0x17
David Brownell2e55cc72005-08-31 09:53:10 -070061#define AX_CMD_READ_PHY_ID 0x19
62#define AX_CMD_READ_MEDIUM_STATUS 0x1a
63#define AX_CMD_WRITE_MEDIUM_MODE 0x1b
64#define AX_CMD_READ_MONITOR_MODE 0x1c
65#define AX_CMD_WRITE_MONITOR_MODE 0x1d
David Hollis933a27d2006-07-29 10:12:50 -040066#define AX_CMD_READ_GPIOS 0x1e
David Brownell2e55cc72005-08-31 09:53:10 -070067#define AX_CMD_WRITE_GPIOS 0x1f
68#define AX_CMD_SW_RESET 0x20
69#define AX_CMD_SW_PHY_STATUS 0x21
70#define AX_CMD_SW_PHY_SELECT 0x22
David Brownell2e55cc72005-08-31 09:53:10 -070071
72#define AX_MONITOR_MODE 0x01
73#define AX_MONITOR_LINK 0x02
74#define AX_MONITOR_MAGIC 0x04
75#define AX_MONITOR_HSFS 0x10
76
77/* AX88172 Medium Status Register values */
David Hollis933a27d2006-07-29 10:12:50 -040078#define AX88172_MEDIUM_FD 0x02
79#define AX88172_MEDIUM_TX 0x04
80#define AX88172_MEDIUM_FC 0x10
81#define AX88172_MEDIUM_DEFAULT \
82 ( AX88172_MEDIUM_FD | AX88172_MEDIUM_TX | AX88172_MEDIUM_FC )
David Brownell2e55cc72005-08-31 09:53:10 -070083
84#define AX_MCAST_FILTER_SIZE 8
85#define AX_MAX_MCAST 64
86
David Brownell2e55cc72005-08-31 09:53:10 -070087#define AX_SWRESET_CLEAR 0x00
88#define AX_SWRESET_RR 0x01
89#define AX_SWRESET_RT 0x02
90#define AX_SWRESET_PRTE 0x04
91#define AX_SWRESET_PRL 0x08
92#define AX_SWRESET_BZ 0x10
93#define AX_SWRESET_IPRL 0x20
94#define AX_SWRESET_IPPD 0x40
95
96#define AX88772_IPG0_DEFAULT 0x15
97#define AX88772_IPG1_DEFAULT 0x0c
98#define AX88772_IPG2_DEFAULT 0x12
99
David Hollis933a27d2006-07-29 10:12:50 -0400100/* AX88772 & AX88178 Medium Mode Register */
101#define AX_MEDIUM_PF 0x0080
102#define AX_MEDIUM_JFE 0x0040
103#define AX_MEDIUM_TFC 0x0020
104#define AX_MEDIUM_RFC 0x0010
105#define AX_MEDIUM_ENCK 0x0008
106#define AX_MEDIUM_AC 0x0004
107#define AX_MEDIUM_FD 0x0002
108#define AX_MEDIUM_GM 0x0001
109#define AX_MEDIUM_SM 0x1000
110#define AX_MEDIUM_SBP 0x0800
111#define AX_MEDIUM_PS 0x0200
112#define AX_MEDIUM_RE 0x0100
David Brownell2e55cc72005-08-31 09:53:10 -0700113
David Hollis933a27d2006-07-29 10:12:50 -0400114#define AX88178_MEDIUM_DEFAULT \
115 (AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \
116 AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \
117 AX_MEDIUM_RE )
118
119#define AX88772_MEDIUM_DEFAULT \
120 (AX_MEDIUM_FD | AX_MEDIUM_RFC | \
121 AX_MEDIUM_TFC | AX_MEDIUM_PS | \
122 AX_MEDIUM_AC | AX_MEDIUM_RE )
123
124/* AX88772 & AX88178 RX_CTL values */
125#define AX_RX_CTL_SO 0x0080
126#define AX_RX_CTL_AP 0x0020
127#define AX_RX_CTL_AM 0x0010
128#define AX_RX_CTL_AB 0x0008
129#define AX_RX_CTL_SEP 0x0004
130#define AX_RX_CTL_AMALL 0x0002
131#define AX_RX_CTL_PRO 0x0001
132#define AX_RX_CTL_MFB_2048 0x0000
133#define AX_RX_CTL_MFB_4096 0x0100
134#define AX_RX_CTL_MFB_8192 0x0200
135#define AX_RX_CTL_MFB_16384 0x0300
136
137#define AX_DEFAULT_RX_CTL \
138 (AX_RX_CTL_SO | AX_RX_CTL_AB )
139
140/* GPIO 0 .. 2 toggles */
141#define AX_GPIO_GPO0EN 0x01 /* GPIO0 Output enable */
142#define AX_GPIO_GPO_0 0x02 /* GPIO0 Output value */
143#define AX_GPIO_GPO1EN 0x04 /* GPIO1 Output enable */
144#define AX_GPIO_GPO_1 0x08 /* GPIO1 Output value */
145#define AX_GPIO_GPO2EN 0x10 /* GPIO2 Output enable */
146#define AX_GPIO_GPO_2 0x20 /* GPIO2 Output value */
147#define AX_GPIO_RESERVED 0x40 /* Reserved */
148#define AX_GPIO_RSE 0x80 /* Reload serial EEPROM */
149
150#define AX_EEPROM_MAGIC 0xdeadbeef
151#define AX88172_EEPROM_LEN 0x40
152#define AX88772_EEPROM_LEN 0xff
153
154#define PHY_MODE_MARVELL 0x0000
155#define MII_MARVELL_LED_CTRL 0x0018
156#define MII_MARVELL_STATUS 0x001b
157#define MII_MARVELL_CTRL 0x0014
158
159#define MARVELL_LED_MANUAL 0x0019
160
161#define MARVELL_STATUS_HWCFG 0x0004
162
163#define MARVELL_CTRL_TXDELAY 0x0002
164#define MARVELL_CTRL_RXDELAY 0x0080
David Brownell2e55cc72005-08-31 09:53:10 -0700165
166/* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
David Hollis48b1be62006-03-28 20:15:42 -0500167struct asix_data {
David Brownell2e55cc72005-08-31 09:53:10 -0700168 u8 multi_filter[AX_MCAST_FILTER_SIZE];
Jussi Kivilinna7f29a3b2010-03-09 12:24:38 +0000169 u8 mac_addr[ETH_ALEN];
David Hollis933a27d2006-07-29 10:12:50 -0400170 u8 phymode;
171 u8 ledmode;
172 u8 eeprom_len;
David Brownell2e55cc72005-08-31 09:53:10 -0700173};
174
175struct ax88172_int_data {
Al Viro51bf2972007-12-22 17:42:36 +0000176 __le16 res1;
David Brownell2e55cc72005-08-31 09:53:10 -0700177 u8 link;
Al Viro51bf2972007-12-22 17:42:36 +0000178 __le16 res2;
David Brownell2e55cc72005-08-31 09:53:10 -0700179 u8 status;
Al Viro51bf2972007-12-22 17:42:36 +0000180 __le16 res3;
David Brownell2e55cc72005-08-31 09:53:10 -0700181} __attribute__ ((packed));
182
David Hollis48b1be62006-03-28 20:15:42 -0500183static int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
David Brownell2e55cc72005-08-31 09:53:10 -0700184 u16 size, void *data)
185{
Al Viro51bf2972007-12-22 17:42:36 +0000186 void *buf;
187 int err = -ENOMEM;
188
Joe Perches60b86752010-02-17 10:30:23 +0000189 netdev_dbg(dev->net, "asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
190 cmd, value, index, size);
Al Viro51bf2972007-12-22 17:42:36 +0000191
192 buf = kmalloc(size, GFP_KERNEL);
193 if (!buf)
194 goto out;
195
196 err = usb_control_msg(
David Brownell2e55cc72005-08-31 09:53:10 -0700197 dev->udev,
198 usb_rcvctrlpipe(dev->udev, 0),
199 cmd,
200 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
201 value,
202 index,
Al Viro51bf2972007-12-22 17:42:36 +0000203 buf,
David Brownell2e55cc72005-08-31 09:53:10 -0700204 size,
205 USB_CTRL_GET_TIMEOUT);
Russ Dill94d43362008-01-09 21:32:07 -0700206 if (err == size)
Al Viro51bf2972007-12-22 17:42:36 +0000207 memcpy(data, buf, size);
Russ Dill94d43362008-01-09 21:32:07 -0700208 else if (err >= 0)
209 err = -EINVAL;
Al Viro51bf2972007-12-22 17:42:36 +0000210 kfree(buf);
211
212out:
213 return err;
David Brownell2e55cc72005-08-31 09:53:10 -0700214}
215
David Hollis48b1be62006-03-28 20:15:42 -0500216static int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
David Brownell2e55cc72005-08-31 09:53:10 -0700217 u16 size, void *data)
218{
Al Viro51bf2972007-12-22 17:42:36 +0000219 void *buf = NULL;
220 int err = -ENOMEM;
221
Joe Perches60b86752010-02-17 10:30:23 +0000222 netdev_dbg(dev->net, "asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
223 cmd, value, index, size);
Al Viro51bf2972007-12-22 17:42:36 +0000224
225 if (data) {
226 buf = kmalloc(size, GFP_KERNEL);
227 if (!buf)
228 goto out;
229 memcpy(buf, data, size);
230 }
231
232 err = usb_control_msg(
David Brownell2e55cc72005-08-31 09:53:10 -0700233 dev->udev,
234 usb_sndctrlpipe(dev->udev, 0),
235 cmd,
236 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
237 value,
238 index,
Al Viro51bf2972007-12-22 17:42:36 +0000239 buf,
David Brownell2e55cc72005-08-31 09:53:10 -0700240 size,
241 USB_CTRL_SET_TIMEOUT);
Al Viro51bf2972007-12-22 17:42:36 +0000242 kfree(buf);
243
244out:
245 return err;
David Brownell2e55cc72005-08-31 09:53:10 -0700246}
247
David Howells7d12e782006-10-05 14:55:46 +0100248static void asix_async_cmd_callback(struct urb *urb)
David Brownell2e55cc72005-08-31 09:53:10 -0700249{
250 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800251 int status = urb->status;
David Brownell2e55cc72005-08-31 09:53:10 -0700252
Oliver Neukumc94cb312008-12-18 23:00:59 -0800253 if (status < 0)
David Hollis48b1be62006-03-28 20:15:42 -0500254 printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800255 status);
David Brownell2e55cc72005-08-31 09:53:10 -0700256
257 kfree(req);
258 usb_free_urb(urb);
259}
260
David Hollis933a27d2006-07-29 10:12:50 -0400261static void
262asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
263 u16 size, void *data)
David Hollis48b1be62006-03-28 20:15:42 -0500264{
David Hollis933a27d2006-07-29 10:12:50 -0400265 struct usb_ctrlrequest *req;
266 int status;
267 struct urb *urb;
David Hollis48b1be62006-03-28 20:15:42 -0500268
Joe Perches60b86752010-02-17 10:30:23 +0000269 netdev_dbg(dev->net, "asix_write_cmd_async() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
270 cmd, value, index, size);
David Hollis933a27d2006-07-29 10:12:50 -0400271 if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) {
Joe Perches60b86752010-02-17 10:30:23 +0000272 netdev_err(dev->net, "Error allocating URB in write_cmd_async!\n");
David Hollis933a27d2006-07-29 10:12:50 -0400273 return;
David Hollis48b1be62006-03-28 20:15:42 -0500274 }
David Hollis933a27d2006-07-29 10:12:50 -0400275
276 if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) {
Joe Perches60b86752010-02-17 10:30:23 +0000277 netdev_err(dev->net, "Failed to allocate memory for control request\n");
David Hollis933a27d2006-07-29 10:12:50 -0400278 usb_free_urb(urb);
279 return;
280 }
281
282 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
283 req->bRequest = cmd;
Oliver Neukum9aa742e2006-11-23 12:45:31 +0100284 req->wValue = cpu_to_le16(value);
285 req->wIndex = cpu_to_le16(index);
286 req->wLength = cpu_to_le16(size);
David Hollis933a27d2006-07-29 10:12:50 -0400287
288 usb_fill_control_urb(urb, dev->udev,
289 usb_sndctrlpipe(dev->udev, 0),
290 (void *)req, data, size,
291 asix_async_cmd_callback, req);
292
293 if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000294 netdev_err(dev->net, "Error submitting the control message: status=%d\n",
295 status);
David Hollis933a27d2006-07-29 10:12:50 -0400296 kfree(req);
297 usb_free_urb(urb);
298 }
David Hollis48b1be62006-03-28 20:15:42 -0500299}
300
David Hollis933a27d2006-07-29 10:12:50 -0400301static int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
David Hollis48b1be62006-03-28 20:15:42 -0500302{
David Hollis933a27d2006-07-29 10:12:50 -0400303 u8 *head;
304 u32 header;
305 char *packet;
306 struct sk_buff *ax_skb;
307 u16 size;
David Hollis48b1be62006-03-28 20:15:42 -0500308
David Hollis933a27d2006-07-29 10:12:50 -0400309 head = (u8 *) skb->data;
310 memcpy(&header, head, sizeof(header));
311 le32_to_cpus(&header);
312 packet = head + sizeof(header);
David Hollis48b1be62006-03-28 20:15:42 -0500313
David Hollis933a27d2006-07-29 10:12:50 -0400314 skb_pull(skb, 4);
315
316 while (skb->len > 0) {
317 if ((short)(header & 0x0000ffff) !=
318 ~((short)((header & 0xffff0000) >> 16))) {
Joe Perches60b86752010-02-17 10:30:23 +0000319 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
David Hollis933a27d2006-07-29 10:12:50 -0400320 }
321 /* get the packet length */
322 size = (u16) (header & 0x0000ffff);
323
324 if ((skb->len) - ((size + 1) & 0xfffe) == 0)
325 return 2;
326 if (size > ETH_FRAME_LEN) {
Joe Perches60b86752010-02-17 10:30:23 +0000327 netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
328 size);
David Hollis933a27d2006-07-29 10:12:50 -0400329 return 0;
330 }
331 ax_skb = skb_clone(skb, GFP_ATOMIC);
332 if (ax_skb) {
333 ax_skb->len = size;
334 ax_skb->data = packet;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700335 skb_set_tail_pointer(ax_skb, size);
David Hollis933a27d2006-07-29 10:12:50 -0400336 usbnet_skb_return(dev, ax_skb);
337 } else {
338 return 0;
339 }
340
341 skb_pull(skb, (size + 1) & 0xfffe);
342
343 if (skb->len == 0)
344 break;
345
346 head = (u8 *) skb->data;
347 memcpy(&header, head, sizeof(header));
348 le32_to_cpus(&header);
349 packet = head + sizeof(header);
350 skb_pull(skb, 4);
351 }
352
353 if (skb->len < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000354 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
355 skb->len);
David Hollis933a27d2006-07-29 10:12:50 -0400356 return 0;
357 }
358 return 1;
David Hollis48b1be62006-03-28 20:15:42 -0500359}
360
David Hollis933a27d2006-07-29 10:12:50 -0400361static struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
362 gfp_t flags)
David Hollis48b1be62006-03-28 20:15:42 -0500363{
David Hollis933a27d2006-07-29 10:12:50 -0400364 int padlen;
365 int headroom = skb_headroom(skb);
366 int tailroom = skb_tailroom(skb);
367 u32 packet_len;
368 u32 padbytes = 0xffff0000;
David Hollis48b1be62006-03-28 20:15:42 -0500369
David Hollis933a27d2006-07-29 10:12:50 -0400370 padlen = ((skb->len + 4) % 512) ? 0 : 4;
David Hollis48b1be62006-03-28 20:15:42 -0500371
Joe Perches8e95a202009-12-03 07:58:21 +0000372 if ((!skb_cloned(skb)) &&
373 ((headroom + tailroom) >= (4 + padlen))) {
David Hollis933a27d2006-07-29 10:12:50 -0400374 if ((headroom < 4) || (tailroom < padlen)) {
375 skb->data = memmove(skb->head + 4, skb->data, skb->len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700376 skb_set_tail_pointer(skb, skb->len);
David Hollis933a27d2006-07-29 10:12:50 -0400377 }
378 } else {
379 struct sk_buff *skb2;
380 skb2 = skb_copy_expand(skb, 4, padlen, flags);
381 dev_kfree_skb_any(skb);
382 skb = skb2;
383 if (!skb)
384 return NULL;
385 }
386
387 skb_push(skb, 4);
388 packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
David Hollis57e4f042007-02-05 12:03:03 -0500389 cpu_to_le32s(&packet_len);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300390 skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
David Hollis933a27d2006-07-29 10:12:50 -0400391
392 if ((skb->len % 512) == 0) {
David Hollis57e4f042007-02-05 12:03:03 -0500393 cpu_to_le32s(&padbytes);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700394 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
David Hollis933a27d2006-07-29 10:12:50 -0400395 skb_put(skb, sizeof(padbytes));
396 }
397 return skb;
David Hollis48b1be62006-03-28 20:15:42 -0500398}
399
400static void asix_status(struct usbnet *dev, struct urb *urb)
David Brownell2e55cc72005-08-31 09:53:10 -0700401{
402 struct ax88172_int_data *event;
403 int link;
404
405 if (urb->actual_length < 8)
406 return;
407
408 event = urb->transfer_buffer;
409 link = event->link & 0x01;
410 if (netif_carrier_ok(dev->net) != link) {
411 if (link) {
412 netif_carrier_on(dev->net);
413 usbnet_defer_kevent (dev, EVENT_LINK_RESET );
414 } else
415 netif_carrier_off(dev->net);
Joe Perches60b86752010-02-17 10:30:23 +0000416 netdev_dbg(dev->net, "Link Status is: %d\n", link);
David Brownell2e55cc72005-08-31 09:53:10 -0700417 }
418}
419
David Hollis933a27d2006-07-29 10:12:50 -0400420static inline int asix_set_sw_mii(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -0700421{
David Hollis933a27d2006-07-29 10:12:50 -0400422 int ret;
423 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
424 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000425 netdev_err(dev->net, "Failed to enable software MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400426 return ret;
David Brownell2e55cc72005-08-31 09:53:10 -0700427}
428
David Hollis933a27d2006-07-29 10:12:50 -0400429static inline int asix_set_hw_mii(struct usbnet *dev)
430{
431 int ret;
432 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
433 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000434 netdev_err(dev->net, "Failed to enable hardware MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400435 return ret;
436}
437
438static inline int asix_get_phy_addr(struct usbnet *dev)
439{
Al Viro51bf2972007-12-22 17:42:36 +0000440 u8 buf[2];
441 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
David Hollis933a27d2006-07-29 10:12:50 -0400442
Joe Perches60b86752010-02-17 10:30:23 +0000443 netdev_dbg(dev->net, "asix_get_phy_addr()\n");
David Hollis933a27d2006-07-29 10:12:50 -0400444
Al Viro51bf2972007-12-22 17:42:36 +0000445 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000446 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000447 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400448 }
Joe Perches60b86752010-02-17 10:30:23 +0000449 netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
450 *((__le16 *)buf));
Al Viro51bf2972007-12-22 17:42:36 +0000451 ret = buf[1];
452
453out:
David Hollis933a27d2006-07-29 10:12:50 -0400454 return ret;
455}
456
457static int asix_sw_reset(struct usbnet *dev, u8 flags)
458{
459 int ret;
460
461 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
462 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000463 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
David Hollis933a27d2006-07-29 10:12:50 -0400464
465 return ret;
466}
467
468static u16 asix_read_rx_ctl(struct usbnet *dev)
469{
Al Viro51bf2972007-12-22 17:42:36 +0000470 __le16 v;
471 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400472
Al Viro51bf2972007-12-22 17:42:36 +0000473 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000474 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000475 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400476 }
Al Viro51bf2972007-12-22 17:42:36 +0000477 ret = le16_to_cpu(v);
478out:
David Hollis933a27d2006-07-29 10:12:50 -0400479 return ret;
480}
481
482static int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
483{
484 int ret;
485
Joe Perches60b86752010-02-17 10:30:23 +0000486 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400487 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
488 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000489 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
490 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400491
492 return ret;
493}
494
495static u16 asix_read_medium_status(struct usbnet *dev)
496{
Al Viro51bf2972007-12-22 17:42:36 +0000497 __le16 v;
498 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400499
Al Viro51bf2972007-12-22 17:42:36 +0000500 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000501 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
502 ret);
Al Viro51bf2972007-12-22 17:42:36 +0000503 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400504 }
Al Viro51bf2972007-12-22 17:42:36 +0000505 ret = le16_to_cpu(v);
506out:
David Hollis933a27d2006-07-29 10:12:50 -0400507 return ret;
508}
509
510static int asix_write_medium_mode(struct usbnet *dev, u16 mode)
511{
512 int ret;
513
Joe Perches60b86752010-02-17 10:30:23 +0000514 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400515 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
516 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000517 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
518 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400519
520 return ret;
521}
522
523static int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
524{
525 int ret;
526
Joe Perches60b86752010-02-17 10:30:23 +0000527 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
David Hollis933a27d2006-07-29 10:12:50 -0400528 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
529 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000530 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
531 value, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400532
533 if (sleep)
534 msleep(sleep);
535
536 return ret;
537}
538
539/*
540 * AX88772 & AX88178 have a 16-bit RX_CTL value
541 */
David Hollis48b1be62006-03-28 20:15:42 -0500542static void asix_set_multicast(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700543{
544 struct usbnet *dev = netdev_priv(net);
David Hollis48b1be62006-03-28 20:15:42 -0500545 struct asix_data *data = (struct asix_data *)&dev->data;
David Hollis933a27d2006-07-29 10:12:50 -0400546 u16 rx_ctl = AX_DEFAULT_RX_CTL;
David Brownell2e55cc72005-08-31 09:53:10 -0700547
548 if (net->flags & IFF_PROMISC) {
David Hollis933a27d2006-07-29 10:12:50 -0400549 rx_ctl |= AX_RX_CTL_PRO;
Joe Perches8e95a202009-12-03 07:58:21 +0000550 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000551 netdev_mc_count(net) > AX_MAX_MCAST) {
David Hollis933a27d2006-07-29 10:12:50 -0400552 rx_ctl |= AX_RX_CTL_AMALL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000553 } else if (netdev_mc_empty(net)) {
David Brownell2e55cc72005-08-31 09:53:10 -0700554 /* just broadcast and directed */
555 } else {
556 /* We use the 20 byte dev->data
557 * for our 8 byte filter buffer
558 * to avoid allocating memory that
559 * is tricky to free later */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000560 struct netdev_hw_addr *ha;
David Brownell2e55cc72005-08-31 09:53:10 -0700561 u32 crc_bits;
David Brownell2e55cc72005-08-31 09:53:10 -0700562
563 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
564
565 /* Build the multicast hash filter. */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000566 netdev_for_each_mc_addr(ha, net) {
567 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
David Brownell2e55cc72005-08-31 09:53:10 -0700568 data->multi_filter[crc_bits >> 3] |=
569 1 << (crc_bits & 7);
David Brownell2e55cc72005-08-31 09:53:10 -0700570 }
571
David Hollis48b1be62006-03-28 20:15:42 -0500572 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
David Brownell2e55cc72005-08-31 09:53:10 -0700573 AX_MCAST_FILTER_SIZE, data->multi_filter);
574
David Hollis933a27d2006-07-29 10:12:50 -0400575 rx_ctl |= AX_RX_CTL_AM;
David Brownell2e55cc72005-08-31 09:53:10 -0700576 }
577
David Hollis48b1be62006-03-28 20:15:42 -0500578 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700579}
580
David Hollis48b1be62006-03-28 20:15:42 -0500581static int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
David Brownell2e55cc72005-08-31 09:53:10 -0700582{
583 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000584 __le16 res;
David Brownell2e55cc72005-08-31 09:53:10 -0700585
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200586 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500587 asix_set_sw_mii(dev);
588 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
Al Viro51bf2972007-12-22 17:42:36 +0000589 (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500590 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200591 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700592
Joe Perches60b86752010-02-17 10:30:23 +0000593 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
594 phy_id, loc, le16_to_cpu(res));
David Brownell2e55cc72005-08-31 09:53:10 -0700595
Al Viro51bf2972007-12-22 17:42:36 +0000596 return le16_to_cpu(res);
David Brownell2e55cc72005-08-31 09:53:10 -0700597}
598
599static void
David Hollis48b1be62006-03-28 20:15:42 -0500600asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
David Brownell2e55cc72005-08-31 09:53:10 -0700601{
602 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000603 __le16 res = cpu_to_le16(val);
David Brownell2e55cc72005-08-31 09:53:10 -0700604
Joe Perches60b86752010-02-17 10:30:23 +0000605 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
606 phy_id, loc, val);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200607 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500608 asix_set_sw_mii(dev);
Al Viro51bf2972007-12-22 17:42:36 +0000609 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500610 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200611 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700612}
613
David Hollis933a27d2006-07-29 10:12:50 -0400614/* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
615static u32 asix_get_phyid(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -0700616{
David Hollis933a27d2006-07-29 10:12:50 -0400617 int phy_reg;
618 u32 phy_id;
David Brownell2e55cc72005-08-31 09:53:10 -0700619
David Hollis933a27d2006-07-29 10:12:50 -0400620 phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
621 if (phy_reg < 0)
622 return 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700623
David Hollis933a27d2006-07-29 10:12:50 -0400624 phy_id = (phy_reg & 0xffff) << 16;
David Brownell2e55cc72005-08-31 09:53:10 -0700625
David Hollis933a27d2006-07-29 10:12:50 -0400626 phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
627 if (phy_reg < 0)
628 return 0;
629
630 phy_id |= (phy_reg & 0xffff);
631
632 return phy_id;
David Brownell2e55cc72005-08-31 09:53:10 -0700633}
634
635static void
David Hollis48b1be62006-03-28 20:15:42 -0500636asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700637{
638 struct usbnet *dev = netdev_priv(net);
639 u8 opt;
640
David Hollis48b1be62006-03-28 20:15:42 -0500641 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700642 wolinfo->supported = 0;
643 wolinfo->wolopts = 0;
644 return;
645 }
646 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
647 wolinfo->wolopts = 0;
648 if (opt & AX_MONITOR_MODE) {
649 if (opt & AX_MONITOR_LINK)
650 wolinfo->wolopts |= WAKE_PHY;
651 if (opt & AX_MONITOR_MAGIC)
652 wolinfo->wolopts |= WAKE_MAGIC;
653 }
654}
655
656static int
David Hollis48b1be62006-03-28 20:15:42 -0500657asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700658{
659 struct usbnet *dev = netdev_priv(net);
660 u8 opt = 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700661
662 if (wolinfo->wolopts & WAKE_PHY)
663 opt |= AX_MONITOR_LINK;
664 if (wolinfo->wolopts & WAKE_MAGIC)
665 opt |= AX_MONITOR_MAGIC;
666 if (opt != 0)
667 opt |= AX_MONITOR_MODE;
668
David Hollis48b1be62006-03-28 20:15:42 -0500669 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
Al Viro51bf2972007-12-22 17:42:36 +0000670 opt, 0, 0, NULL) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700671 return -EINVAL;
672
673 return 0;
674}
675
David Hollis48b1be62006-03-28 20:15:42 -0500676static int asix_get_eeprom_len(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700677{
David Hollis933a27d2006-07-29 10:12:50 -0400678 struct usbnet *dev = netdev_priv(net);
679 struct asix_data *data = (struct asix_data *)&dev->data;
680
681 return data->eeprom_len;
David Brownell2e55cc72005-08-31 09:53:10 -0700682}
683
David Hollis48b1be62006-03-28 20:15:42 -0500684static int asix_get_eeprom(struct net_device *net,
David Brownell2e55cc72005-08-31 09:53:10 -0700685 struct ethtool_eeprom *eeprom, u8 *data)
686{
687 struct usbnet *dev = netdev_priv(net);
Al Viro51bf2972007-12-22 17:42:36 +0000688 __le16 *ebuf = (__le16 *)data;
David Brownell2e55cc72005-08-31 09:53:10 -0700689 int i;
690
691 /* Crude hack to ensure that we don't overwrite memory
692 * if an odd length is supplied
693 */
694 if (eeprom->len % 2)
695 return -EINVAL;
696
697 eeprom->magic = AX_EEPROM_MAGIC;
698
699 /* ax8817x returns 2 bytes from eeprom on read */
700 for (i=0; i < eeprom->len / 2; i++) {
David Hollis48b1be62006-03-28 20:15:42 -0500701 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
David Brownell2e55cc72005-08-31 09:53:10 -0700702 eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
703 return -EINVAL;
704 }
705 return 0;
706}
707
David Hollis48b1be62006-03-28 20:15:42 -0500708static void asix_get_drvinfo (struct net_device *net,
David Brownell2e55cc72005-08-31 09:53:10 -0700709 struct ethtool_drvinfo *info)
710{
David Hollis933a27d2006-07-29 10:12:50 -0400711 struct usbnet *dev = netdev_priv(net);
712 struct asix_data *data = (struct asix_data *)&dev->data;
713
David Brownell2e55cc72005-08-31 09:53:10 -0700714 /* Inherit standard device info */
715 usbnet_get_drvinfo(net, info);
David Hollis933a27d2006-07-29 10:12:50 -0400716 strncpy (info->driver, driver_name, sizeof info->driver);
717 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
718 info->eedump_len = data->eeprom_len;
David Brownell2e55cc72005-08-31 09:53:10 -0700719}
720
David Hollis933a27d2006-07-29 10:12:50 -0400721static u32 asix_get_link(struct net_device *net)
722{
723 struct usbnet *dev = netdev_priv(net);
724
725 return mii_link_ok(&dev->mii);
726}
727
728static int asix_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
729{
730 struct usbnet *dev = netdev_priv(net);
731
732 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700733}
734
Jussi Kivilinna7f29a3b2010-03-09 12:24:38 +0000735static int asix_set_mac_address(struct net_device *net, void *p)
736{
737 struct usbnet *dev = netdev_priv(net);
738 struct asix_data *data = (struct asix_data *)&dev->data;
739 struct sockaddr *addr = p;
740
741 if (netif_running(net))
742 return -EBUSY;
743 if (!is_valid_ether_addr(addr->sa_data))
744 return -EADDRNOTAVAIL;
745
746 memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
747
748 /* We use the 20 byte dev->data
749 * for our 6 byte mac buffer
750 * to avoid allocating memory that
751 * is tricky to free later */
752 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
753 asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
754 data->mac_addr);
755
756 return 0;
757}
758
David Brownell2e55cc72005-08-31 09:53:10 -0700759/* We need to override some ethtool_ops so we require our
760 own structure so we don't interfere with other usbnet
761 devices that may be connected at the same time. */
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700762static const struct ethtool_ops ax88172_ethtool_ops = {
David Hollis48b1be62006-03-28 20:15:42 -0500763 .get_drvinfo = asix_get_drvinfo,
David Hollis933a27d2006-07-29 10:12:50 -0400764 .get_link = asix_get_link,
David Brownell2e55cc72005-08-31 09:53:10 -0700765 .get_msglevel = usbnet_get_msglevel,
766 .set_msglevel = usbnet_set_msglevel,
David Hollis48b1be62006-03-28 20:15:42 -0500767 .get_wol = asix_get_wol,
768 .set_wol = asix_set_wol,
769 .get_eeprom_len = asix_get_eeprom_len,
770 .get_eeprom = asix_get_eeprom,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200771 .get_settings = usbnet_get_settings,
772 .set_settings = usbnet_set_settings,
773 .nway_reset = usbnet_nway_reset,
David Brownell2e55cc72005-08-31 09:53:10 -0700774};
775
David Hollis933a27d2006-07-29 10:12:50 -0400776static void ax88172_set_multicast(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700777{
778 struct usbnet *dev = netdev_priv(net);
David Hollis933a27d2006-07-29 10:12:50 -0400779 struct asix_data *data = (struct asix_data *)&dev->data;
780 u8 rx_ctl = 0x8c;
David Brownell2e55cc72005-08-31 09:53:10 -0700781
David Hollis933a27d2006-07-29 10:12:50 -0400782 if (net->flags & IFF_PROMISC) {
783 rx_ctl |= 0x01;
Joe Perches8e95a202009-12-03 07:58:21 +0000784 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000785 netdev_mc_count(net) > AX_MAX_MCAST) {
David Hollis933a27d2006-07-29 10:12:50 -0400786 rx_ctl |= 0x02;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000787 } else if (netdev_mc_empty(net)) {
David Hollis933a27d2006-07-29 10:12:50 -0400788 /* just broadcast and directed */
789 } else {
790 /* We use the 20 byte dev->data
791 * for our 8 byte filter buffer
792 * to avoid allocating memory that
793 * is tricky to free later */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000794 struct netdev_hw_addr *ha;
David Hollis933a27d2006-07-29 10:12:50 -0400795 u32 crc_bits;
David Hollis933a27d2006-07-29 10:12:50 -0400796
797 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
798
799 /* Build the multicast hash filter. */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000800 netdev_for_each_mc_addr(ha, net) {
801 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
David Hollis933a27d2006-07-29 10:12:50 -0400802 data->multi_filter[crc_bits >> 3] |=
803 1 << (crc_bits & 7);
David Hollis933a27d2006-07-29 10:12:50 -0400804 }
805
806 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
807 AX_MCAST_FILTER_SIZE, data->multi_filter);
808
809 rx_ctl |= 0x10;
810 }
811
812 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
813}
814
815static int ax88172_link_reset(struct usbnet *dev)
816{
817 u8 mode;
818 struct ethtool_cmd ecmd;
819
820 mii_check_media(&dev->mii, 1, 1);
821 mii_ethtool_gset(&dev->mii, &ecmd);
822 mode = AX88172_MEDIUM_DEFAULT;
823
824 if (ecmd.duplex != DUPLEX_FULL)
825 mode |= ~AX88172_MEDIUM_FD;
826
Joe Perches60b86752010-02-17 10:30:23 +0000827 netdev_dbg(dev->net, "ax88172_link_reset() speed: %d duplex: %d setting mode to 0x%04x\n",
828 ecmd.speed, ecmd.duplex, mode);
David Hollis933a27d2006-07-29 10:12:50 -0400829
830 asix_write_medium_mode(dev, mode);
831
832 return 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700833}
834
Stephen Hemminger17033382009-03-20 19:35:55 +0000835static const struct net_device_ops ax88172_netdev_ops = {
836 .ndo_open = usbnet_open,
837 .ndo_stop = usbnet_stop,
838 .ndo_start_xmit = usbnet_start_xmit,
839 .ndo_tx_timeout = usbnet_tx_timeout,
840 .ndo_change_mtu = usbnet_change_mtu,
841 .ndo_set_mac_address = eth_mac_addr,
842 .ndo_validate_addr = eth_validate_addr,
843 .ndo_do_ioctl = asix_ioctl,
844 .ndo_set_multicast_list = ax88172_set_multicast,
845};
846
David Hollis48b1be62006-03-28 20:15:42 -0500847static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
David Brownell2e55cc72005-08-31 09:53:10 -0700848{
849 int ret = 0;
Al Viro51bf2972007-12-22 17:42:36 +0000850 u8 buf[ETH_ALEN];
David Brownell2e55cc72005-08-31 09:53:10 -0700851 int i;
852 unsigned long gpio_bits = dev->driver_info->data;
David Hollis933a27d2006-07-29 10:12:50 -0400853 struct asix_data *data = (struct asix_data *)&dev->data;
854
855 data->eeprom_len = AX88172_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700856
857 usbnet_get_endpoints(dev,intf);
858
David Brownell2e55cc72005-08-31 09:53:10 -0700859 /* Toggle the GPIOs in a manufacturer/model specific way */
860 for (i = 2; i >= 0; i--) {
David Hollis48b1be62006-03-28 20:15:42 -0500861 if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
David Brownell2e55cc72005-08-31 09:53:10 -0700862 (gpio_bits >> (i * 8)) & 0xff, 0, 0,
Al Viro51bf2972007-12-22 17:42:36 +0000863 NULL)) < 0)
864 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700865 msleep(5);
866 }
867
David Hollis933a27d2006-07-29 10:12:50 -0400868 if ((ret = asix_write_rx_ctl(dev, 0x80)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000869 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700870
871 /* Get the MAC address */
David Hollis933a27d2006-07-29 10:12:50 -0400872 if ((ret = asix_read_cmd(dev, AX88172_CMD_READ_NODE_ID,
Al Viro51bf2972007-12-22 17:42:36 +0000873 0, 0, ETH_ALEN, buf)) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700874 dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000875 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700876 }
877 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
878
David Brownell2e55cc72005-08-31 09:53:10 -0700879 /* Initialize MII structure */
880 dev->mii.dev = dev->net;
David Hollis48b1be62006-03-28 20:15:42 -0500881 dev->mii.mdio_read = asix_mdio_read;
882 dev->mii.mdio_write = asix_mdio_write;
David Brownell2e55cc72005-08-31 09:53:10 -0700883 dev->mii.phy_id_mask = 0x3f;
884 dev->mii.reg_num_mask = 0x1f;
David Hollis933a27d2006-07-29 10:12:50 -0400885 dev->mii.phy_id = asix_get_phy_addr(dev);
David Brownell2e55cc72005-08-31 09:53:10 -0700886
Stephen Hemminger17033382009-03-20 19:35:55 +0000887 dev->net->netdev_ops = &ax88172_netdev_ops;
David Hollis48b1be62006-03-28 20:15:42 -0500888 dev->net->ethtool_ops = &ax88172_ethtool_ops;
David Brownell2e55cc72005-08-31 09:53:10 -0700889
David Hollis933a27d2006-07-29 10:12:50 -0400890 asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
891 asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
David Brownell2e55cc72005-08-31 09:53:10 -0700892 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
893 mii_nway_restart(&dev->mii);
894
895 return 0;
Al Viro51bf2972007-12-22 17:42:36 +0000896
897out:
David Brownell2e55cc72005-08-31 09:53:10 -0700898 return ret;
899}
900
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700901static const struct ethtool_ops ax88772_ethtool_ops = {
David Hollis48b1be62006-03-28 20:15:42 -0500902 .get_drvinfo = asix_get_drvinfo,
David Hollis933a27d2006-07-29 10:12:50 -0400903 .get_link = asix_get_link,
David Brownell2e55cc72005-08-31 09:53:10 -0700904 .get_msglevel = usbnet_get_msglevel,
905 .set_msglevel = usbnet_set_msglevel,
David Hollis48b1be62006-03-28 20:15:42 -0500906 .get_wol = asix_get_wol,
907 .set_wol = asix_set_wol,
908 .get_eeprom_len = asix_get_eeprom_len,
909 .get_eeprom = asix_get_eeprom,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200910 .get_settings = usbnet_get_settings,
911 .set_settings = usbnet_set_settings,
912 .nway_reset = usbnet_nway_reset,
David Brownell2e55cc72005-08-31 09:53:10 -0700913};
914
David Hollis933a27d2006-07-29 10:12:50 -0400915static int ax88772_link_reset(struct usbnet *dev)
916{
917 u16 mode;
918 struct ethtool_cmd ecmd;
919
920 mii_check_media(&dev->mii, 1, 1);
921 mii_ethtool_gset(&dev->mii, &ecmd);
922 mode = AX88772_MEDIUM_DEFAULT;
923
924 if (ecmd.speed != SPEED_100)
925 mode &= ~AX_MEDIUM_PS;
926
927 if (ecmd.duplex != DUPLEX_FULL)
928 mode &= ~AX_MEDIUM_FD;
929
Joe Perches60b86752010-02-17 10:30:23 +0000930 netdev_dbg(dev->net, "ax88772_link_reset() speed: %d duplex: %d setting mode to 0x%04x\n",
931 ecmd.speed, ecmd.duplex, mode);
David Hollis933a27d2006-07-29 10:12:50 -0400932
933 asix_write_medium_mode(dev, mode);
934
935 return 0;
936}
937
Stephen Hemminger17033382009-03-20 19:35:55 +0000938static const struct net_device_ops ax88772_netdev_ops = {
939 .ndo_open = usbnet_open,
940 .ndo_stop = usbnet_stop,
941 .ndo_start_xmit = usbnet_start_xmit,
942 .ndo_tx_timeout = usbnet_tx_timeout,
943 .ndo_change_mtu = usbnet_change_mtu,
Jussi Kivilinna7f29a3b2010-03-09 12:24:38 +0000944 .ndo_set_mac_address = asix_set_mac_address,
Stephen Hemminger17033382009-03-20 19:35:55 +0000945 .ndo_validate_addr = eth_validate_addr,
946 .ndo_do_ioctl = asix_ioctl,
947 .ndo_set_multicast_list = asix_set_multicast,
948};
949
David Brownell2e55cc72005-08-31 09:53:10 -0700950static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
951{
Andres Salomond0ffff82007-01-11 18:39:16 -0500952 int ret, embd_phy;
David Hollis933a27d2006-07-29 10:12:50 -0400953 u16 rx_ctl;
954 struct asix_data *data = (struct asix_data *)&dev->data;
Al Viro51bf2972007-12-22 17:42:36 +0000955 u8 buf[ETH_ALEN];
David Hollis933a27d2006-07-29 10:12:50 -0400956 u32 phyid;
957
958 data->eeprom_len = AX88772_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700959
960 usbnet_get_endpoints(dev,intf);
961
David Hollis933a27d2006-07-29 10:12:50 -0400962 if ((ret = asix_write_gpio(dev,
963 AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000964 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700965
Andres Salomond0ffff82007-01-11 18:39:16 -0500966 /* 0x10 is the phy id of the embedded 10/100 ethernet phy */
967 embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
David Hollis48b1be62006-03-28 20:15:42 -0500968 if ((ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
Al Viro51bf2972007-12-22 17:42:36 +0000969 embd_phy, 0, 0, NULL)) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700970 dbg("Select PHY #1 failed: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000971 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700972 }
973
Andres Salomond0ffff82007-01-11 18:39:16 -0500974 if ((ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000975 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700976
977 msleep(150);
David Hollis48b1be62006-03-28 20:15:42 -0500978 if ((ret = asix_sw_reset(dev, AX_SWRESET_CLEAR)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000979 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700980
981 msleep(150);
Andres Salomond0ffff82007-01-11 18:39:16 -0500982 if (embd_phy) {
983 if ((ret = asix_sw_reset(dev, AX_SWRESET_IPRL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000984 goto out;
Andres Salomond0ffff82007-01-11 18:39:16 -0500985 }
986 else {
987 if ((ret = asix_sw_reset(dev, AX_SWRESET_PRTE)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000988 goto out;
Andres Salomond0ffff82007-01-11 18:39:16 -0500989 }
David Brownell2e55cc72005-08-31 09:53:10 -0700990
991 msleep(150);
David Hollis933a27d2006-07-29 10:12:50 -0400992 rx_ctl = asix_read_rx_ctl(dev);
993 dbg("RX_CTL is 0x%04x after software reset", rx_ctl);
994 if ((ret = asix_write_rx_ctl(dev, 0x0000)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000995 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700996
David Hollis933a27d2006-07-29 10:12:50 -0400997 rx_ctl = asix_read_rx_ctl(dev);
998 dbg("RX_CTL is 0x%04x setting to 0x0000", rx_ctl);
999
David Brownell2e55cc72005-08-31 09:53:10 -07001000 /* Get the MAC address */
David Hollis933a27d2006-07-29 10:12:50 -04001001 if ((ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID,
David Brownell2e55cc72005-08-31 09:53:10 -07001002 0, 0, ETH_ALEN, buf)) < 0) {
1003 dbg("Failed to read MAC address: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +00001004 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001005 }
1006 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
1007
David Brownell2e55cc72005-08-31 09:53:10 -07001008 /* Initialize MII structure */
1009 dev->mii.dev = dev->net;
David Hollis48b1be62006-03-28 20:15:42 -05001010 dev->mii.mdio_read = asix_mdio_read;
1011 dev->mii.mdio_write = asix_mdio_write;
David Hollis933a27d2006-07-29 10:12:50 -04001012 dev->mii.phy_id_mask = 0x1f;
1013 dev->mii.reg_num_mask = 0x1f;
David Hollis933a27d2006-07-29 10:12:50 -04001014 dev->mii.phy_id = asix_get_phy_addr(dev);
1015
1016 phyid = asix_get_phyid(dev);
1017 dbg("PHYID=0x%08x", phyid);
David Brownell2e55cc72005-08-31 09:53:10 -07001018
David Hollis48b1be62006-03-28 20:15:42 -05001019 if ((ret = asix_sw_reset(dev, AX_SWRESET_PRL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001020 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001021
David Brownell2e55cc72005-08-31 09:53:10 -07001022 msleep(150);
1023
David Hollis48b1be62006-03-28 20:15:42 -05001024 if ((ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001025 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001026
David Hollis48b1be62006-03-28 20:15:42 -05001027 msleep(150);
1028
Stephen Hemminger17033382009-03-20 19:35:55 +00001029 dev->net->netdev_ops = &ax88772_netdev_ops;
David Brownell2e55cc72005-08-31 09:53:10 -07001030 dev->net->ethtool_ops = &ax88772_ethtool_ops;
1031
David Hollis933a27d2006-07-29 10:12:50 -04001032 asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
1033 asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
David Brownell2e55cc72005-08-31 09:53:10 -07001034 ADVERTISE_ALL | ADVERTISE_CSMA);
1035 mii_nway_restart(&dev->mii);
1036
David Hollis933a27d2006-07-29 10:12:50 -04001037 if ((ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001038 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001039
David Hollis48b1be62006-03-28 20:15:42 -05001040 if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
David Brownell2e55cc72005-08-31 09:53:10 -07001041 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
Al Viro51bf2972007-12-22 17:42:36 +00001042 AX88772_IPG2_DEFAULT, 0, NULL)) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -07001043 dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +00001044 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001045 }
David Brownell2e55cc72005-08-31 09:53:10 -07001046
1047 /* Set RX_CTL to default values with 2k buffer, and enable cactus */
David Hollis933a27d2006-07-29 10:12:50 -04001048 if ((ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001049 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001050
David Hollis933a27d2006-07-29 10:12:50 -04001051 rx_ctl = asix_read_rx_ctl(dev);
1052 dbg("RX_CTL is 0x%04x after all initializations", rx_ctl);
1053
1054 rx_ctl = asix_read_medium_status(dev);
1055 dbg("Medium Status is 0x%04x after all initializations", rx_ctl);
1056
David Brownell2e55cc72005-08-31 09:53:10 -07001057 /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
1058 if (dev->driver_info->flags & FLAG_FRAMING_AX) {
1059 /* hard_mtu is still the default - the device does not support
1060 jumbo eth frames */
1061 dev->rx_urb_size = 2048;
1062 }
David Brownell2e55cc72005-08-31 09:53:10 -07001063 return 0;
1064
Al Viro51bf2972007-12-22 17:42:36 +00001065out:
David Brownell2e55cc72005-08-31 09:53:10 -07001066 return ret;
1067}
1068
David Hollis933a27d2006-07-29 10:12:50 -04001069static struct ethtool_ops ax88178_ethtool_ops = {
1070 .get_drvinfo = asix_get_drvinfo,
1071 .get_link = asix_get_link,
David Hollis933a27d2006-07-29 10:12:50 -04001072 .get_msglevel = usbnet_get_msglevel,
1073 .set_msglevel = usbnet_set_msglevel,
1074 .get_wol = asix_get_wol,
1075 .set_wol = asix_set_wol,
1076 .get_eeprom_len = asix_get_eeprom_len,
1077 .get_eeprom = asix_get_eeprom,
Arnd Bergmannc41286f2006-10-09 00:08:01 +02001078 .get_settings = usbnet_get_settings,
1079 .set_settings = usbnet_set_settings,
1080 .nway_reset = usbnet_nway_reset,
David Hollis933a27d2006-07-29 10:12:50 -04001081};
1082
1083static int marvell_phy_init(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -07001084{
David Hollis933a27d2006-07-29 10:12:50 -04001085 struct asix_data *data = (struct asix_data *)&dev->data;
1086 u16 reg;
David Brownell2e55cc72005-08-31 09:53:10 -07001087
Joe Perches60b86752010-02-17 10:30:23 +00001088 netdev_dbg(dev->net, "marvell_phy_init()\n");
David Brownell2e55cc72005-08-31 09:53:10 -07001089
David Hollis933a27d2006-07-29 10:12:50 -04001090 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_MARVELL_STATUS);
Joe Perches60b86752010-02-17 10:30:23 +00001091 netdev_dbg(dev->net, "MII_MARVELL_STATUS = 0x%04x\n", reg);
David Brownell2e55cc72005-08-31 09:53:10 -07001092
David Hollis933a27d2006-07-29 10:12:50 -04001093 asix_mdio_write(dev->net, dev->mii.phy_id, MII_MARVELL_CTRL,
1094 MARVELL_CTRL_RXDELAY | MARVELL_CTRL_TXDELAY);
David Brownell2e55cc72005-08-31 09:53:10 -07001095
David Hollis933a27d2006-07-29 10:12:50 -04001096 if (data->ledmode) {
1097 reg = asix_mdio_read(dev->net, dev->mii.phy_id,
1098 MII_MARVELL_LED_CTRL);
Joe Perches60b86752010-02-17 10:30:23 +00001099 netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (1) = 0x%04x\n", reg);
David Brownell2e55cc72005-08-31 09:53:10 -07001100
David Hollis933a27d2006-07-29 10:12:50 -04001101 reg &= 0xf8ff;
1102 reg |= (1 + 0x0100);
1103 asix_mdio_write(dev->net, dev->mii.phy_id,
1104 MII_MARVELL_LED_CTRL, reg);
David Brownell2e55cc72005-08-31 09:53:10 -07001105
David Hollis933a27d2006-07-29 10:12:50 -04001106 reg = asix_mdio_read(dev->net, dev->mii.phy_id,
1107 MII_MARVELL_LED_CTRL);
Joe Perches60b86752010-02-17 10:30:23 +00001108 netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (2) = 0x%04x\n", reg);
David Hollis933a27d2006-07-29 10:12:50 -04001109 reg &= 0xfc0f;
David Brownell2e55cc72005-08-31 09:53:10 -07001110 }
1111
David Brownell2e55cc72005-08-31 09:53:10 -07001112 return 0;
1113}
1114
David Hollis933a27d2006-07-29 10:12:50 -04001115static int marvell_led_status(struct usbnet *dev, u16 speed)
1116{
1117 u16 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL);
1118
Joe Perches60b86752010-02-17 10:30:23 +00001119 netdev_dbg(dev->net, "marvell_led_status() read 0x%04x\n", reg);
David Hollis933a27d2006-07-29 10:12:50 -04001120
1121 /* Clear out the center LED bits - 0x03F0 */
1122 reg &= 0xfc0f;
1123
1124 switch (speed) {
1125 case SPEED_1000:
1126 reg |= 0x03e0;
1127 break;
1128 case SPEED_100:
1129 reg |= 0x03b0;
1130 break;
1131 default:
1132 reg |= 0x02f0;
1133 }
1134
Joe Perches60b86752010-02-17 10:30:23 +00001135 netdev_dbg(dev->net, "marvell_led_status() writing 0x%04x\n", reg);
David Hollis933a27d2006-07-29 10:12:50 -04001136 asix_mdio_write(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL, reg);
1137
1138 return 0;
1139}
1140
1141static int ax88178_link_reset(struct usbnet *dev)
1142{
1143 u16 mode;
1144 struct ethtool_cmd ecmd;
1145 struct asix_data *data = (struct asix_data *)&dev->data;
1146
Joe Perches60b86752010-02-17 10:30:23 +00001147 netdev_dbg(dev->net, "ax88178_link_reset()\n");
David Hollis933a27d2006-07-29 10:12:50 -04001148
1149 mii_check_media(&dev->mii, 1, 1);
1150 mii_ethtool_gset(&dev->mii, &ecmd);
1151 mode = AX88178_MEDIUM_DEFAULT;
1152
1153 if (ecmd.speed == SPEED_1000)
Pantelis Koukousoulasa7f75c02008-11-20 01:48:46 -08001154 mode |= AX_MEDIUM_GM;
David Hollis933a27d2006-07-29 10:12:50 -04001155 else if (ecmd.speed == SPEED_100)
1156 mode |= AX_MEDIUM_PS;
1157 else
1158 mode &= ~(AX_MEDIUM_PS | AX_MEDIUM_GM);
1159
Pantelis Koukousoulasa7f75c02008-11-20 01:48:46 -08001160 mode |= AX_MEDIUM_ENCK;
1161
David Hollis933a27d2006-07-29 10:12:50 -04001162 if (ecmd.duplex == DUPLEX_FULL)
1163 mode |= AX_MEDIUM_FD;
1164 else
1165 mode &= ~AX_MEDIUM_FD;
1166
Joe Perches60b86752010-02-17 10:30:23 +00001167 netdev_dbg(dev->net, "ax88178_link_reset() speed: %d duplex: %d setting mode to 0x%04x\n",
1168 ecmd.speed, ecmd.duplex, mode);
David Hollis933a27d2006-07-29 10:12:50 -04001169
1170 asix_write_medium_mode(dev, mode);
1171
1172 if (data->phymode == PHY_MODE_MARVELL && data->ledmode)
1173 marvell_led_status(dev, ecmd.speed);
1174
1175 return 0;
1176}
1177
1178static void ax88178_set_mfb(struct usbnet *dev)
1179{
1180 u16 mfb = AX_RX_CTL_MFB_16384;
1181 u16 rxctl;
1182 u16 medium;
1183 int old_rx_urb_size = dev->rx_urb_size;
1184
1185 if (dev->hard_mtu < 2048) {
1186 dev->rx_urb_size = 2048;
1187 mfb = AX_RX_CTL_MFB_2048;
1188 } else if (dev->hard_mtu < 4096) {
1189 dev->rx_urb_size = 4096;
1190 mfb = AX_RX_CTL_MFB_4096;
1191 } else if (dev->hard_mtu < 8192) {
1192 dev->rx_urb_size = 8192;
1193 mfb = AX_RX_CTL_MFB_8192;
1194 } else if (dev->hard_mtu < 16384) {
1195 dev->rx_urb_size = 16384;
1196 mfb = AX_RX_CTL_MFB_16384;
1197 }
1198
1199 rxctl = asix_read_rx_ctl(dev);
1200 asix_write_rx_ctl(dev, (rxctl & ~AX_RX_CTL_MFB_16384) | mfb);
1201
1202 medium = asix_read_medium_status(dev);
1203 if (dev->net->mtu > 1500)
1204 medium |= AX_MEDIUM_JFE;
1205 else
1206 medium &= ~AX_MEDIUM_JFE;
1207 asix_write_medium_mode(dev, medium);
1208
1209 if (dev->rx_urb_size > old_rx_urb_size)
1210 usbnet_unlink_rx_urbs(dev);
1211}
1212
1213static int ax88178_change_mtu(struct net_device *net, int new_mtu)
1214{
1215 struct usbnet *dev = netdev_priv(net);
1216 int ll_mtu = new_mtu + net->hard_header_len + 4;
1217
Joe Perches60b86752010-02-17 10:30:23 +00001218 netdev_dbg(dev->net, "ax88178_change_mtu() new_mtu=%d\n", new_mtu);
David Hollis933a27d2006-07-29 10:12:50 -04001219
1220 if (new_mtu <= 0 || ll_mtu > 16384)
1221 return -EINVAL;
1222
1223 if ((ll_mtu % dev->maxpacket) == 0)
1224 return -EDOM;
1225
1226 net->mtu = new_mtu;
1227 dev->hard_mtu = net->mtu + net->hard_header_len;
1228 ax88178_set_mfb(dev);
1229
1230 return 0;
1231}
1232
Stephen Hemminger17033382009-03-20 19:35:55 +00001233static const struct net_device_ops ax88178_netdev_ops = {
1234 .ndo_open = usbnet_open,
1235 .ndo_stop = usbnet_stop,
1236 .ndo_start_xmit = usbnet_start_xmit,
1237 .ndo_tx_timeout = usbnet_tx_timeout,
Jussi Kivilinna7f29a3b2010-03-09 12:24:38 +00001238 .ndo_set_mac_address = asix_set_mac_address,
Stephen Hemminger17033382009-03-20 19:35:55 +00001239 .ndo_validate_addr = eth_validate_addr,
1240 .ndo_set_multicast_list = asix_set_multicast,
1241 .ndo_do_ioctl = asix_ioctl,
1242 .ndo_change_mtu = ax88178_change_mtu,
1243};
1244
David Hollis933a27d2006-07-29 10:12:50 -04001245static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
1246{
1247 struct asix_data *data = (struct asix_data *)&dev->data;
1248 int ret;
Al Viro51bf2972007-12-22 17:42:36 +00001249 u8 buf[ETH_ALEN];
1250 __le16 eeprom;
1251 u8 status;
David Hollis933a27d2006-07-29 10:12:50 -04001252 int gpio0 = 0;
1253 u32 phyid;
1254
1255 usbnet_get_endpoints(dev,intf);
1256
Al Viro51bf2972007-12-22 17:42:36 +00001257 asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status);
1258 dbg("GPIO Status: 0x%04x", status);
David Hollis933a27d2006-07-29 10:12:50 -04001259
1260 asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL);
1261 asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom);
1262 asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL);
1263
1264 dbg("EEPROM index 0x17 is 0x%04x", eeprom);
1265
Al Viro51bf2972007-12-22 17:42:36 +00001266 if (eeprom == cpu_to_le16(0xffff)) {
David Hollis933a27d2006-07-29 10:12:50 -04001267 data->phymode = PHY_MODE_MARVELL;
1268 data->ledmode = 0;
1269 gpio0 = 1;
1270 } else {
Al Viro51bf2972007-12-22 17:42:36 +00001271 data->phymode = le16_to_cpu(eeprom) & 7;
1272 data->ledmode = le16_to_cpu(eeprom) >> 8;
1273 gpio0 = (le16_to_cpu(eeprom) & 0x80) ? 0 : 1;
David Hollis933a27d2006-07-29 10:12:50 -04001274 }
1275 dbg("GPIO0: %d, PhyMode: %d", gpio0, data->phymode);
1276
1277 asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_1 | AX_GPIO_GPO1EN, 40);
Al Viro51bf2972007-12-22 17:42:36 +00001278 if ((le16_to_cpu(eeprom) >> 8) != 1) {
David Hollis933a27d2006-07-29 10:12:50 -04001279 asix_write_gpio(dev, 0x003c, 30);
1280 asix_write_gpio(dev, 0x001c, 300);
1281 asix_write_gpio(dev, 0x003c, 30);
1282 } else {
1283 dbg("gpio phymode == 1 path");
1284 asix_write_gpio(dev, AX_GPIO_GPO1EN, 30);
1285 asix_write_gpio(dev, AX_GPIO_GPO1EN | AX_GPIO_GPO_1, 30);
1286 }
1287
1288 asix_sw_reset(dev, 0);
1289 msleep(150);
1290
1291 asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
1292 msleep(150);
1293
1294 asix_write_rx_ctl(dev, 0);
1295
1296 /* Get the MAC address */
David Hollis933a27d2006-07-29 10:12:50 -04001297 if ((ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID,
1298 0, 0, ETH_ALEN, buf)) < 0) {
1299 dbg("Failed to read MAC address: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +00001300 goto out;
David Hollis933a27d2006-07-29 10:12:50 -04001301 }
1302 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
1303
1304 /* Initialize MII structure */
1305 dev->mii.dev = dev->net;
1306 dev->mii.mdio_read = asix_mdio_read;
1307 dev->mii.mdio_write = asix_mdio_write;
1308 dev->mii.phy_id_mask = 0x1f;
1309 dev->mii.reg_num_mask = 0xff;
1310 dev->mii.supports_gmii = 1;
David Hollis933a27d2006-07-29 10:12:50 -04001311 dev->mii.phy_id = asix_get_phy_addr(dev);
Stephen Hemminger17033382009-03-20 19:35:55 +00001312
1313 dev->net->netdev_ops = &ax88178_netdev_ops;
David Hollis933a27d2006-07-29 10:12:50 -04001314 dev->net->ethtool_ops = &ax88178_ethtool_ops;
David Hollis933a27d2006-07-29 10:12:50 -04001315
1316 phyid = asix_get_phyid(dev);
1317 dbg("PHYID=0x%08x", phyid);
1318
1319 if (data->phymode == PHY_MODE_MARVELL) {
1320 marvell_phy_init(dev);
1321 msleep(60);
1322 }
1323
1324 asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR,
1325 BMCR_RESET | BMCR_ANENABLE);
1326 asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
1327 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
1328 asix_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
1329 ADVERTISE_1000FULL);
1330
1331 mii_nway_restart(&dev->mii);
1332
1333 if ((ret = asix_write_medium_mode(dev, AX88178_MEDIUM_DEFAULT)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001334 goto out;
David Hollis933a27d2006-07-29 10:12:50 -04001335
1336 if ((ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001337 goto out;
David Hollis933a27d2006-07-29 10:12:50 -04001338
1339 /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
1340 if (dev->driver_info->flags & FLAG_FRAMING_AX) {
1341 /* hard_mtu is still the default - the device does not support
1342 jumbo eth frames */
1343 dev->rx_urb_size = 2048;
1344 }
David Hollis933a27d2006-07-29 10:12:50 -04001345 return 0;
1346
Al Viro51bf2972007-12-22 17:42:36 +00001347out:
David Hollis933a27d2006-07-29 10:12:50 -04001348 return ret;
1349}
1350
David Brownell2e55cc72005-08-31 09:53:10 -07001351static const struct driver_info ax8817x_info = {
1352 .description = "ASIX AX8817x USB 2.0 Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -05001353 .bind = ax88172_bind,
1354 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001355 .link_reset = ax88172_link_reset,
1356 .reset = ax88172_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001357 .flags = FLAG_ETHER | FLAG_LINK_INTR,
David Brownell2e55cc72005-08-31 09:53:10 -07001358 .data = 0x00130103,
1359};
1360
1361static const struct driver_info dlink_dub_e100_info = {
1362 .description = "DLink DUB-E100 USB Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -05001363 .bind = ax88172_bind,
1364 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001365 .link_reset = ax88172_link_reset,
1366 .reset = ax88172_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001367 .flags = FLAG_ETHER | FLAG_LINK_INTR,
David Brownell2e55cc72005-08-31 09:53:10 -07001368 .data = 0x009f9d9f,
1369};
1370
1371static const struct driver_info netgear_fa120_info = {
1372 .description = "Netgear FA-120 USB Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -05001373 .bind = ax88172_bind,
1374 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001375 .link_reset = ax88172_link_reset,
1376 .reset = ax88172_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001377 .flags = FLAG_ETHER | FLAG_LINK_INTR,
David Brownell2e55cc72005-08-31 09:53:10 -07001378 .data = 0x00130103,
1379};
1380
1381static const struct driver_info hawking_uf200_info = {
1382 .description = "Hawking UF200 USB Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -05001383 .bind = ax88172_bind,
1384 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001385 .link_reset = ax88172_link_reset,
1386 .reset = ax88172_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001387 .flags = FLAG_ETHER | FLAG_LINK_INTR,
David Brownell2e55cc72005-08-31 09:53:10 -07001388 .data = 0x001f1d1f,
1389};
1390
1391static const struct driver_info ax88772_info = {
1392 .description = "ASIX AX88772 USB 2.0 Ethernet",
1393 .bind = ax88772_bind,
David Hollis48b1be62006-03-28 20:15:42 -05001394 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001395 .link_reset = ax88772_link_reset,
1396 .reset = ax88772_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001397 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
David Hollis933a27d2006-07-29 10:12:50 -04001398 .rx_fixup = asix_rx_fixup,
1399 .tx_fixup = asix_tx_fixup,
1400};
1401
1402static const struct driver_info ax88178_info = {
1403 .description = "ASIX AX88178 USB 2.0 Ethernet",
1404 .bind = ax88178_bind,
1405 .status = asix_status,
1406 .link_reset = ax88178_link_reset,
1407 .reset = ax88178_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001408 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
David Hollis933a27d2006-07-29 10:12:50 -04001409 .rx_fixup = asix_rx_fixup,
1410 .tx_fixup = asix_tx_fixup,
David Brownell2e55cc72005-08-31 09:53:10 -07001411};
1412
1413static const struct usb_device_id products [] = {
1414{
1415 // Linksys USB200M
1416 USB_DEVICE (0x077b, 0x2226),
1417 .driver_info = (unsigned long) &ax8817x_info,
1418}, {
1419 // Netgear FA120
1420 USB_DEVICE (0x0846, 0x1040),
1421 .driver_info = (unsigned long) &netgear_fa120_info,
1422}, {
1423 // DLink DUB-E100
1424 USB_DEVICE (0x2001, 0x1a00),
1425 .driver_info = (unsigned long) &dlink_dub_e100_info,
1426}, {
1427 // Intellinet, ST Lab USB Ethernet
1428 USB_DEVICE (0x0b95, 0x1720),
1429 .driver_info = (unsigned long) &ax8817x_info,
1430}, {
1431 // Hawking UF200, TrendNet TU2-ET100
1432 USB_DEVICE (0x07b8, 0x420a),
1433 .driver_info = (unsigned long) &hawking_uf200_info,
1434}, {
David Hollis39c4b382007-02-20 08:02:24 -05001435 // Billionton Systems, USB2AR
1436 USB_DEVICE (0x08dd, 0x90ff),
1437 .driver_info = (unsigned long) &ax8817x_info,
David Brownell2e55cc72005-08-31 09:53:10 -07001438}, {
1439 // ATEN UC210T
1440 USB_DEVICE (0x0557, 0x2009),
1441 .driver_info = (unsigned long) &ax8817x_info,
1442}, {
1443 // Buffalo LUA-U2-KTX
1444 USB_DEVICE (0x0411, 0x003d),
1445 .driver_info = (unsigned long) &ax8817x_info,
1446}, {
Mattia Dongiliac7b77f2008-05-06 20:42:35 -07001447 // Buffalo LUA-U2-GT 10/100/1000
1448 USB_DEVICE (0x0411, 0x006e),
1449 .driver_info = (unsigned long) &ax88178_info,
1450}, {
David Brownell2e55cc72005-08-31 09:53:10 -07001451 // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
1452 USB_DEVICE (0x6189, 0x182d),
1453 .driver_info = (unsigned long) &ax8817x_info,
1454}, {
1455 // corega FEther USB2-TX
1456 USB_DEVICE (0x07aa, 0x0017),
1457 .driver_info = (unsigned long) &ax8817x_info,
1458}, {
1459 // Surecom EP-1427X-2
1460 USB_DEVICE (0x1189, 0x0893),
1461 .driver_info = (unsigned long) &ax8817x_info,
1462}, {
1463 // goodway corp usb gwusb2e
1464 USB_DEVICE (0x1631, 0x6200),
1465 .driver_info = (unsigned long) &ax8817x_info,
1466}, {
David Hollis39c4b382007-02-20 08:02:24 -05001467 // JVC MP-PRX1 Port Replicator
1468 USB_DEVICE (0x04f1, 0x3008),
1469 .driver_info = (unsigned long) &ax8817x_info,
1470}, {
David Brownell2e55cc72005-08-31 09:53:10 -07001471 // ASIX AX88772 10/100
David Hollis39c4b382007-02-20 08:02:24 -05001472 USB_DEVICE (0x0b95, 0x7720),
1473 .driver_info = (unsigned long) &ax88772_info,
David Hollis5e0f76c6b2005-12-19 13:58:38 -05001474}, {
Eduard Warkentin73274132006-05-18 01:13:17 -07001475 // ASIX AX88178 10/100/1000
1476 USB_DEVICE (0x0b95, 0x1780),
David Hollis933a27d2006-07-29 10:12:50 -04001477 .driver_info = (unsigned long) &ax88178_info,
Eduard Warkentin73274132006-05-18 01:13:17 -07001478}, {
David Hollis5e0f76c6b2005-12-19 13:58:38 -05001479 // Linksys USB200M Rev 2
1480 USB_DEVICE (0x13b1, 0x0018),
1481 .driver_info = (unsigned long) &ax88772_info,
David Hollis5732ce82006-01-05 14:39:49 -05001482}, {
1483 // 0Q0 cable ethernet
1484 USB_DEVICE (0x1557, 0x7720),
1485 .driver_info = (unsigned long) &ax88772_info,
David Hollis933a27d2006-07-29 10:12:50 -04001486}, {
1487 // DLink DUB-E100 H/W Ver B1
1488 USB_DEVICE (0x07d1, 0x3c05),
1489 .driver_info = (unsigned long) &ax88772_info,
1490}, {
David Hollisb923e7f2006-09-21 08:09:29 -04001491 // DLink DUB-E100 H/W Ver B1 Alternate
1492 USB_DEVICE (0x2001, 0x3c05),
1493 .driver_info = (unsigned long) &ax88772_info,
1494}, {
David Hollis933a27d2006-07-29 10:12:50 -04001495 // Linksys USB1000
1496 USB_DEVICE (0x1737, 0x0039),
1497 .driver_info = (unsigned long) &ax88178_info,
YOSHIFUJI Hideaki / 吉藤英明b29cf312007-01-26 22:57:38 +09001498}, {
1499 // IO-DATA ETG-US2
1500 USB_DEVICE (0x04bb, 0x0930),
1501 .driver_info = (unsigned long) &ax88178_info,
David Hollis2ed22bc2007-05-23 07:33:17 -04001502}, {
1503 // Belkin F5D5055
1504 USB_DEVICE(0x050d, 0x5055),
1505 .driver_info = (unsigned long) &ax88178_info,
Aurelien Nephtali3d60efb52008-05-14 17:04:13 -07001506}, {
1507 // Apple USB Ethernet Adapter
1508 USB_DEVICE(0x05ac, 0x1402),
1509 .driver_info = (unsigned long) &ax88772_info,
Jason Cooperccf95402008-11-11 13:02:53 -05001510}, {
1511 // Cables-to-Go USB Ethernet Adapter
1512 USB_DEVICE(0x0b95, 0x772a),
1513 .driver_info = (unsigned long) &ax88772_info,
Greg Kroah-Hartmanfef7cc02009-02-24 23:52:24 -08001514}, {
1515 // ABOCOM for pci
1516 USB_DEVICE(0x14ea, 0xab11),
1517 .driver_info = (unsigned long) &ax88178_info,
1518}, {
1519 // ASIX 88772a
1520 USB_DEVICE(0x0db0, 0xa877),
1521 .driver_info = (unsigned long) &ax88772_info,
David Brownell2e55cc72005-08-31 09:53:10 -07001522},
1523 { }, // END
1524};
1525MODULE_DEVICE_TABLE(usb, products);
1526
1527static struct usb_driver asix_driver = {
David Brownell2e55cc72005-08-31 09:53:10 -07001528 .name = "asix",
1529 .id_table = products,
1530 .probe = usbnet_probe,
1531 .suspend = usbnet_suspend,
1532 .resume = usbnet_resume,
1533 .disconnect = usbnet_disconnect,
Oliver Neukuma11a6542007-08-03 13:52:19 +02001534 .supports_autosuspend = 1,
David Brownell2e55cc72005-08-31 09:53:10 -07001535};
1536
1537static int __init asix_init(void)
1538{
1539 return usb_register(&asix_driver);
1540}
1541module_init(asix_init);
1542
1543static void __exit asix_exit(void)
1544{
1545 usb_deregister(&asix_driver);
1546}
1547module_exit(asix_exit);
1548
1549MODULE_AUTHOR("David Hollis");
1550MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
1551MODULE_LICENSE("GPL");
1552