blob: 12b599a0b53989d69233734c956865eeab006d70 [file] [log] [blame]
David Brownell2e55cc72005-08-31 09:53:10 -07001/*
2 * ASIX AX8817X based USB 2.0 Ethernet Devices
3 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
4 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
5 * Copyright (c) 2002-2003 TiVo Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22// #define DEBUG // error path messages, extra info
23// #define VERBOSE // more; success messages
24
25#include <linux/config.h>
David Brownell2e55cc72005-08-31 09:53:10 -070026#include <linux/module.h>
27#include <linux/kmod.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/ethtool.h>
33#include <linux/workqueue.h>
34#include <linux/mii.h>
35#include <linux/usb.h>
36#include <linux/crc32.h>
37
38#include "usbnet.h"
39
David Brownell2e55cc72005-08-31 09:53:10 -070040/* ASIX AX8817X based USB 2.0 Ethernet Devices */
41
42#define AX_CMD_SET_SW_MII 0x06
43#define AX_CMD_READ_MII_REG 0x07
44#define AX_CMD_WRITE_MII_REG 0x08
45#define AX_CMD_SET_HW_MII 0x0a
46#define AX_CMD_READ_EEPROM 0x0b
47#define AX_CMD_WRITE_EEPROM 0x0c
48#define AX_CMD_WRITE_ENABLE 0x0d
49#define AX_CMD_WRITE_DISABLE 0x0e
50#define AX_CMD_WRITE_RX_CTL 0x10
51#define AX_CMD_READ_IPG012 0x11
52#define AX_CMD_WRITE_IPG0 0x12
53#define AX_CMD_WRITE_IPG1 0x13
54#define AX_CMD_WRITE_IPG2 0x14
55#define AX_CMD_WRITE_MULTI_FILTER 0x16
56#define AX_CMD_READ_NODE_ID 0x17
57#define AX_CMD_READ_PHY_ID 0x19
58#define AX_CMD_READ_MEDIUM_STATUS 0x1a
59#define AX_CMD_WRITE_MEDIUM_MODE 0x1b
60#define AX_CMD_READ_MONITOR_MODE 0x1c
61#define AX_CMD_WRITE_MONITOR_MODE 0x1d
62#define AX_CMD_WRITE_GPIOS 0x1f
63#define AX_CMD_SW_RESET 0x20
64#define AX_CMD_SW_PHY_STATUS 0x21
65#define AX_CMD_SW_PHY_SELECT 0x22
66#define AX88772_CMD_READ_NODE_ID 0x13
67
68#define AX_MONITOR_MODE 0x01
69#define AX_MONITOR_LINK 0x02
70#define AX_MONITOR_MAGIC 0x04
71#define AX_MONITOR_HSFS 0x10
72
73/* AX88172 Medium Status Register values */
74#define AX_MEDIUM_FULL_DUPLEX 0x02
75#define AX_MEDIUM_TX_ABORT_ALLOW 0x04
76#define AX_MEDIUM_FLOW_CONTROL_EN 0x10
77
78#define AX_MCAST_FILTER_SIZE 8
79#define AX_MAX_MCAST 64
80
81#define AX_EEPROM_LEN 0x40
82
83#define AX_SWRESET_CLEAR 0x00
84#define AX_SWRESET_RR 0x01
85#define AX_SWRESET_RT 0x02
86#define AX_SWRESET_PRTE 0x04
87#define AX_SWRESET_PRL 0x08
88#define AX_SWRESET_BZ 0x10
89#define AX_SWRESET_IPRL 0x20
90#define AX_SWRESET_IPPD 0x40
91
92#define AX88772_IPG0_DEFAULT 0x15
93#define AX88772_IPG1_DEFAULT 0x0c
94#define AX88772_IPG2_DEFAULT 0x12
95
96#define AX88772_MEDIUM_FULL_DUPLEX 0x0002
97#define AX88772_MEDIUM_RESERVED 0x0004
98#define AX88772_MEDIUM_RX_FC_ENABLE 0x0010
99#define AX88772_MEDIUM_TX_FC_ENABLE 0x0020
100#define AX88772_MEDIUM_PAUSE_FORMAT 0x0080
101#define AX88772_MEDIUM_RX_ENABLE 0x0100
102#define AX88772_MEDIUM_100MB 0x0200
103#define AX88772_MEDIUM_DEFAULT \
104 (AX88772_MEDIUM_FULL_DUPLEX | AX88772_MEDIUM_RX_FC_ENABLE | \
105 AX88772_MEDIUM_TX_FC_ENABLE | AX88772_MEDIUM_100MB | \
106 AX88772_MEDIUM_RESERVED | AX88772_MEDIUM_RX_ENABLE )
107
108#define AX_EEPROM_MAGIC 0xdeadbeef
109
110/* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
David Hollis48b1be62006-03-28 20:15:42 -0500111struct asix_data {
David Brownell2e55cc72005-08-31 09:53:10 -0700112 u8 multi_filter[AX_MCAST_FILTER_SIZE];
113};
114
115struct ax88172_int_data {
116 u16 res1;
117 u8 link;
118 u16 res2;
119 u8 status;
120 u16 res3;
121} __attribute__ ((packed));
122
David Hollis48b1be62006-03-28 20:15:42 -0500123static int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
David Brownell2e55cc72005-08-31 09:53:10 -0700124 u16 size, void *data)
125{
126 return usb_control_msg(
127 dev->udev,
128 usb_rcvctrlpipe(dev->udev, 0),
129 cmd,
130 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
131 value,
132 index,
133 data,
134 size,
135 USB_CTRL_GET_TIMEOUT);
136}
137
David Hollis48b1be62006-03-28 20:15:42 -0500138static int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
David Brownell2e55cc72005-08-31 09:53:10 -0700139 u16 size, void *data)
140{
141 return usb_control_msg(
142 dev->udev,
143 usb_sndctrlpipe(dev->udev, 0),
144 cmd,
145 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
146 value,
147 index,
148 data,
149 size,
150 USB_CTRL_SET_TIMEOUT);
151}
152
David Hollis48b1be62006-03-28 20:15:42 -0500153static void asix_async_cmd_callback(struct urb *urb, struct pt_regs *regs)
David Brownell2e55cc72005-08-31 09:53:10 -0700154{
155 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
156
157 if (urb->status < 0)
David Hollis48b1be62006-03-28 20:15:42 -0500158 printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
David Brownell2e55cc72005-08-31 09:53:10 -0700159 urb->status);
160
161 kfree(req);
162 usb_free_urb(urb);
163}
164
David Hollis48b1be62006-03-28 20:15:42 -0500165static inline int asix_set_sw_mii(struct usbnet *dev)
166{
167 int ret;
168 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
169 if (ret < 0)
170 devdbg(dev, "Failed to enable software MII access");
171 return ret;
172}
173
174static inline int asix_set_hw_mii(struct usbnet *dev)
175{
176 int ret;
177 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
178 if (ret < 0)
179 devdbg(dev, "Failed to enable hardware MII access");
180 return ret;
181}
182
183static inline int asix_get_phyid(struct usbnet *dev)
184{
185 int ret = 0;
186 void *buf;
187
188 buf = kmalloc(2, GFP_KERNEL);
189 if (!buf)
190 goto out1;
191
192 if ((ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID,
193 0, 0, 2, buf)) < 2) {
194 devdbg(dev, "Error reading PHYID register: %02x", ret);
195 goto out2;
196 }
197 ret = *((u8 *)buf + 1);
198out2:
199 kfree(buf);
200out1:
201 return ret;
202}
203
204static int asix_sw_reset(struct usbnet *dev, u8 flags)
205{
206 int ret;
207
208 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
209 if (ret < 0)
210 devdbg(dev,"Failed to send software reset: %02x", ret);
211
212 return ret;
213}
214
215static int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
216{
217 int ret;
218
219 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
220 if (ret < 0)
221 devdbg(dev, "Failed to write RX_CTL mode: %02x", ret);
222
223 return ret;
224}
225
226static void asix_status(struct usbnet *dev, struct urb *urb)
David Brownell2e55cc72005-08-31 09:53:10 -0700227{
228 struct ax88172_int_data *event;
229 int link;
230
231 if (urb->actual_length < 8)
232 return;
233
234 event = urb->transfer_buffer;
235 link = event->link & 0x01;
236 if (netif_carrier_ok(dev->net) != link) {
237 if (link) {
238 netif_carrier_on(dev->net);
239 usbnet_defer_kevent (dev, EVENT_LINK_RESET );
240 } else
241 netif_carrier_off(dev->net);
David Hollis48b1be62006-03-28 20:15:42 -0500242 devdbg(dev, "Link Status is: %d", link);
David Brownell2e55cc72005-08-31 09:53:10 -0700243 }
244}
245
246static void
David Hollis48b1be62006-03-28 20:15:42 -0500247asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
David Brownell2e55cc72005-08-31 09:53:10 -0700248 u16 size, void *data)
249{
250 struct usb_ctrlrequest *req;
251 int status;
252 struct urb *urb;
253
254 if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) {
255 devdbg(dev, "Error allocating URB in write_cmd_async!");
256 return;
257 }
258
259 if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) {
260 deverr(dev, "Failed to allocate memory for control request");
261 usb_free_urb(urb);
262 return;
263 }
264
265 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
266 req->bRequest = cmd;
267 req->wValue = cpu_to_le16(value);
268 req->wIndex = cpu_to_le16(index);
269 req->wLength = cpu_to_le16(size);
270
271 usb_fill_control_urb(urb, dev->udev,
272 usb_sndctrlpipe(dev->udev, 0),
273 (void *)req, data, size,
David Hollis48b1be62006-03-28 20:15:42 -0500274 asix_async_cmd_callback, req);
David Brownell2e55cc72005-08-31 09:53:10 -0700275
276 if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
277 deverr(dev, "Error submitting the control message: status=%d",
278 status);
279 kfree(req);
280 usb_free_urb(urb);
281 }
282}
283
David Hollis48b1be62006-03-28 20:15:42 -0500284static void asix_set_multicast(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700285{
286 struct usbnet *dev = netdev_priv(net);
David Hollis48b1be62006-03-28 20:15:42 -0500287 struct asix_data *data = (struct asix_data *)&dev->data;
David Brownell2e55cc72005-08-31 09:53:10 -0700288 u8 rx_ctl = 0x8c;
289
290 if (net->flags & IFF_PROMISC) {
291 rx_ctl |= 0x01;
292 } else if (net->flags & IFF_ALLMULTI
293 || net->mc_count > AX_MAX_MCAST) {
294 rx_ctl |= 0x02;
295 } else if (net->mc_count == 0) {
296 /* just broadcast and directed */
297 } else {
298 /* We use the 20 byte dev->data
299 * for our 8 byte filter buffer
300 * to avoid allocating memory that
301 * is tricky to free later */
302 struct dev_mc_list *mc_list = net->mc_list;
303 u32 crc_bits;
304 int i;
305
306 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
307
308 /* Build the multicast hash filter. */
309 for (i = 0; i < net->mc_count; i++) {
310 crc_bits =
311 ether_crc(ETH_ALEN,
312 mc_list->dmi_addr) >> 26;
313 data->multi_filter[crc_bits >> 3] |=
314 1 << (crc_bits & 7);
315 mc_list = mc_list->next;
316 }
317
David Hollis48b1be62006-03-28 20:15:42 -0500318 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
David Brownell2e55cc72005-08-31 09:53:10 -0700319 AX_MCAST_FILTER_SIZE, data->multi_filter);
320
321 rx_ctl |= 0x10;
322 }
323
David Hollis48b1be62006-03-28 20:15:42 -0500324 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700325}
326
David Hollis48b1be62006-03-28 20:15:42 -0500327static int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
David Brownell2e55cc72005-08-31 09:53:10 -0700328{
329 struct usbnet *dev = netdev_priv(netdev);
330 u16 res;
David Brownell2e55cc72005-08-31 09:53:10 -0700331
David Hollis48b1be62006-03-28 20:15:42 -0500332 asix_set_sw_mii(dev);
333 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
David Brownell2e55cc72005-08-31 09:53:10 -0700334 (__u16)loc, 2, (u16 *)&res);
David Hollis48b1be62006-03-28 20:15:42 -0500335 asix_set_hw_mii(dev);
David Brownell2e55cc72005-08-31 09:53:10 -0700336
337 return res & 0xffff;
338}
339
340/* same as above, but converts resulting value to cpu byte order */
David Hollis48b1be62006-03-28 20:15:42 -0500341static int asix_mdio_read_le(struct net_device *netdev, int phy_id, int loc)
David Brownell2e55cc72005-08-31 09:53:10 -0700342{
David Hollis48b1be62006-03-28 20:15:42 -0500343 return le16_to_cpu(asix_mdio_read(netdev,phy_id, loc));
David Brownell2e55cc72005-08-31 09:53:10 -0700344}
345
346static void
David Hollis48b1be62006-03-28 20:15:42 -0500347asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
David Brownell2e55cc72005-08-31 09:53:10 -0700348{
349 struct usbnet *dev = netdev_priv(netdev);
350 u16 res = val;
David Brownell2e55cc72005-08-31 09:53:10 -0700351
David Hollis48b1be62006-03-28 20:15:42 -0500352 asix_set_sw_mii(dev);
353 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
David Brownell2e55cc72005-08-31 09:53:10 -0700354 (__u16)loc, 2, (u16 *)&res);
David Hollis48b1be62006-03-28 20:15:42 -0500355 asix_set_hw_mii(dev);
David Brownell2e55cc72005-08-31 09:53:10 -0700356}
357
358/* same as above, but converts new value to le16 byte order before writing */
359static void
David Hollis48b1be62006-03-28 20:15:42 -0500360asix_mdio_write_le(struct net_device *netdev, int phy_id, int loc, int val)
David Brownell2e55cc72005-08-31 09:53:10 -0700361{
David Hollis48b1be62006-03-28 20:15:42 -0500362 asix_mdio_write( netdev, phy_id, loc, cpu_to_le16(val) );
David Brownell2e55cc72005-08-31 09:53:10 -0700363}
364
365static int ax88172_link_reset(struct usbnet *dev)
366{
367 u16 lpa;
368 u16 adv;
369 u16 res;
370 u8 mode;
371
372 mode = AX_MEDIUM_TX_ABORT_ALLOW | AX_MEDIUM_FLOW_CONTROL_EN;
David Hollis48b1be62006-03-28 20:15:42 -0500373 lpa = asix_mdio_read_le(dev->net, dev->mii.phy_id, MII_LPA);
374 adv = asix_mdio_read_le(dev->net, dev->mii.phy_id, MII_ADVERTISE);
David Brownell2e55cc72005-08-31 09:53:10 -0700375 res = mii_nway_result(lpa|adv);
376 if (res & LPA_DUPLEX)
377 mode |= AX_MEDIUM_FULL_DUPLEX;
David Hollis48b1be62006-03-28 20:15:42 -0500378 asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700379
380 return 0;
381}
382
383static void
David Hollis48b1be62006-03-28 20:15:42 -0500384asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700385{
386 struct usbnet *dev = netdev_priv(net);
387 u8 opt;
388
David Hollis48b1be62006-03-28 20:15:42 -0500389 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700390 wolinfo->supported = 0;
391 wolinfo->wolopts = 0;
392 return;
393 }
394 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
395 wolinfo->wolopts = 0;
396 if (opt & AX_MONITOR_MODE) {
397 if (opt & AX_MONITOR_LINK)
398 wolinfo->wolopts |= WAKE_PHY;
399 if (opt & AX_MONITOR_MAGIC)
400 wolinfo->wolopts |= WAKE_MAGIC;
401 }
402}
403
404static int
David Hollis48b1be62006-03-28 20:15:42 -0500405asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700406{
407 struct usbnet *dev = netdev_priv(net);
408 u8 opt = 0;
409 u8 buf[1];
410
411 if (wolinfo->wolopts & WAKE_PHY)
412 opt |= AX_MONITOR_LINK;
413 if (wolinfo->wolopts & WAKE_MAGIC)
414 opt |= AX_MONITOR_MAGIC;
415 if (opt != 0)
416 opt |= AX_MONITOR_MODE;
417
David Hollis48b1be62006-03-28 20:15:42 -0500418 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
David Brownell2e55cc72005-08-31 09:53:10 -0700419 opt, 0, 0, &buf) < 0)
420 return -EINVAL;
421
422 return 0;
423}
424
David Hollis48b1be62006-03-28 20:15:42 -0500425static int asix_get_eeprom_len(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700426{
427 return AX_EEPROM_LEN;
428}
429
David Hollis48b1be62006-03-28 20:15:42 -0500430static int asix_get_eeprom(struct net_device *net,
David Brownell2e55cc72005-08-31 09:53:10 -0700431 struct ethtool_eeprom *eeprom, u8 *data)
432{
433 struct usbnet *dev = netdev_priv(net);
434 u16 *ebuf = (u16 *)data;
435 int i;
436
437 /* Crude hack to ensure that we don't overwrite memory
438 * if an odd length is supplied
439 */
440 if (eeprom->len % 2)
441 return -EINVAL;
442
443 eeprom->magic = AX_EEPROM_MAGIC;
444
445 /* ax8817x returns 2 bytes from eeprom on read */
446 for (i=0; i < eeprom->len / 2; i++) {
David Hollis48b1be62006-03-28 20:15:42 -0500447 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
David Brownell2e55cc72005-08-31 09:53:10 -0700448 eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
449 return -EINVAL;
450 }
451 return 0;
452}
453
David Hollis48b1be62006-03-28 20:15:42 -0500454static void asix_get_drvinfo (struct net_device *net,
David Brownell2e55cc72005-08-31 09:53:10 -0700455 struct ethtool_drvinfo *info)
456{
457 /* Inherit standard device info */
458 usbnet_get_drvinfo(net, info);
459 info->eedump_len = 0x3e;
460}
461
David Hollis48b1be62006-03-28 20:15:42 -0500462static int asix_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
David Brownell2e55cc72005-08-31 09:53:10 -0700463{
464 struct usbnet *dev = netdev_priv(net);
465
466 return mii_ethtool_gset(&dev->mii,cmd);
467}
468
David Hollis48b1be62006-03-28 20:15:42 -0500469static int asix_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
David Brownell2e55cc72005-08-31 09:53:10 -0700470{
471 struct usbnet *dev = netdev_priv(net);
472
473 return mii_ethtool_sset(&dev->mii,cmd);
474}
475
476/* We need to override some ethtool_ops so we require our
477 own structure so we don't interfere with other usbnet
478 devices that may be connected at the same time. */
David Hollis48b1be62006-03-28 20:15:42 -0500479static struct ethtool_ops ax88172_ethtool_ops = {
480 .get_drvinfo = asix_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700481 .get_link = ethtool_op_get_link,
482 .get_msglevel = usbnet_get_msglevel,
483 .set_msglevel = usbnet_set_msglevel,
David Hollis48b1be62006-03-28 20:15:42 -0500484 .get_wol = asix_get_wol,
485 .set_wol = asix_set_wol,
486 .get_eeprom_len = asix_get_eeprom_len,
487 .get_eeprom = asix_get_eeprom,
488 .get_settings = asix_get_settings,
489 .set_settings = asix_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700490};
491
David Hollis48b1be62006-03-28 20:15:42 -0500492static int asix_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
David Brownell2e55cc72005-08-31 09:53:10 -0700493{
494 struct usbnet *dev = netdev_priv(net);
495
496 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
497}
498
David Hollis48b1be62006-03-28 20:15:42 -0500499static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
David Brownell2e55cc72005-08-31 09:53:10 -0700500{
501 int ret = 0;
502 void *buf;
503 int i;
504 unsigned long gpio_bits = dev->driver_info->data;
505
506 usbnet_get_endpoints(dev,intf);
507
508 buf = kmalloc(ETH_ALEN, GFP_KERNEL);
509 if(!buf) {
510 ret = -ENOMEM;
511 goto out1;
512 }
513
514 /* Toggle the GPIOs in a manufacturer/model specific way */
515 for (i = 2; i >= 0; i--) {
David Hollis48b1be62006-03-28 20:15:42 -0500516 if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
David Brownell2e55cc72005-08-31 09:53:10 -0700517 (gpio_bits >> (i * 8)) & 0xff, 0, 0,
518 buf)) < 0)
519 goto out2;
520 msleep(5);
521 }
522
David Hollis48b1be62006-03-28 20:15:42 -0500523 if ((ret = asix_write_rx_ctl(dev,0x80)) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700524 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700525
526 /* Get the MAC address */
527 memset(buf, 0, ETH_ALEN);
David Hollis48b1be62006-03-28 20:15:42 -0500528 if ((ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID,
David Brownell2e55cc72005-08-31 09:53:10 -0700529 0, 0, 6, buf)) < 0) {
530 dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
531 goto out2;
532 }
533 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
534
David Brownell2e55cc72005-08-31 09:53:10 -0700535 /* Initialize MII structure */
536 dev->mii.dev = dev->net;
David Hollis48b1be62006-03-28 20:15:42 -0500537 dev->mii.mdio_read = asix_mdio_read;
538 dev->mii.mdio_write = asix_mdio_write;
David Brownell2e55cc72005-08-31 09:53:10 -0700539 dev->mii.phy_id_mask = 0x3f;
540 dev->mii.reg_num_mask = 0x1f;
David Hollis48b1be62006-03-28 20:15:42 -0500541 dev->mii.phy_id = asix_get_phyid(dev);
542 dev->net->do_ioctl = asix_ioctl;
David Brownell2e55cc72005-08-31 09:53:10 -0700543
David Hollis48b1be62006-03-28 20:15:42 -0500544 dev->net->set_multicast_list = asix_set_multicast;
545 dev->net->ethtool_ops = &ax88172_ethtool_ops;
David Brownell2e55cc72005-08-31 09:53:10 -0700546
David Hollis48b1be62006-03-28 20:15:42 -0500547 asix_mdio_write_le(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
548 asix_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
David Brownell2e55cc72005-08-31 09:53:10 -0700549 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
550 mii_nway_restart(&dev->mii);
551
552 return 0;
553out2:
554 kfree(buf);
555out1:
556 return ret;
557}
558
559static struct ethtool_ops ax88772_ethtool_ops = {
David Hollis48b1be62006-03-28 20:15:42 -0500560 .get_drvinfo = asix_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700561 .get_link = ethtool_op_get_link,
562 .get_msglevel = usbnet_get_msglevel,
563 .set_msglevel = usbnet_set_msglevel,
David Hollis48b1be62006-03-28 20:15:42 -0500564 .get_wol = asix_get_wol,
565 .set_wol = asix_set_wol,
566 .get_eeprom_len = asix_get_eeprom_len,
567 .get_eeprom = asix_get_eeprom,
568 .get_settings = asix_get_settings,
569 .set_settings = asix_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700570};
571
572static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
573{
574 int ret;
575 void *buf;
576
577 usbnet_get_endpoints(dev,intf);
578
579 buf = kmalloc(6, GFP_KERNEL);
580 if(!buf) {
581 dbg ("Cannot allocate memory for buffer");
582 ret = -ENOMEM;
583 goto out1;
584 }
585
David Hollis48b1be62006-03-28 20:15:42 -0500586 if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
David Brownell2e55cc72005-08-31 09:53:10 -0700587 0x00B0, 0, 0, buf)) < 0)
588 goto out2;
589
590 msleep(5);
David Hollis48b1be62006-03-28 20:15:42 -0500591 if ((ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
David Brownell2e55cc72005-08-31 09:53:10 -0700592 0x0001, 0, 0, buf)) < 0) {
593 dbg("Select PHY #1 failed: %d", ret);
594 goto out2;
595 }
596
David Hollis48b1be62006-03-28 20:15:42 -0500597 if ((ret = asix_sw_reset(dev, AX_SWRESET_IPPD)) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700598 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700599
600 msleep(150);
David Hollis48b1be62006-03-28 20:15:42 -0500601 if ((ret = asix_sw_reset(dev, AX_SWRESET_CLEAR)) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700602 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700603
604 msleep(150);
David Hollis48b1be62006-03-28 20:15:42 -0500605 if ((ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL)) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700606 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700607
608 msleep(150);
David Hollis48b1be62006-03-28 20:15:42 -0500609 if ((ret = asix_write_rx_ctl(dev, 0x00)) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700610 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700611
612 /* Get the MAC address */
613 memset(buf, 0, ETH_ALEN);
David Hollis48b1be62006-03-28 20:15:42 -0500614 if ((ret = asix_read_cmd(dev, AX88772_CMD_READ_NODE_ID,
David Brownell2e55cc72005-08-31 09:53:10 -0700615 0, 0, ETH_ALEN, buf)) < 0) {
616 dbg("Failed to read MAC address: %d", ret);
617 goto out2;
618 }
619 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
620
David Hollis48b1be62006-03-28 20:15:42 -0500621 if ((ret = asix_set_sw_mii(dev)) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700622 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700623
David Hollis48b1be62006-03-28 20:15:42 -0500624 if (((ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG,
David Brownell2e55cc72005-08-31 09:53:10 -0700625 0x0010, 2, 2, buf)) < 0)
626 || (*((u16 *)buf) != 0x003b)) {
627 dbg("Read PHY register 2 must be 0x3b00: %d", ret);
628 goto out2;
629 }
630
631 /* Initialize MII structure */
632 dev->mii.dev = dev->net;
David Hollis48b1be62006-03-28 20:15:42 -0500633 dev->mii.mdio_read = asix_mdio_read;
634 dev->mii.mdio_write = asix_mdio_write;
David Brownell2e55cc72005-08-31 09:53:10 -0700635 dev->mii.phy_id_mask = 0xff;
636 dev->mii.reg_num_mask = 0xff;
David Hollis48b1be62006-03-28 20:15:42 -0500637 dev->net->do_ioctl = asix_ioctl;
638 dev->mii.phy_id = asix_get_phyid(dev);
David Brownell2e55cc72005-08-31 09:53:10 -0700639
David Hollis48b1be62006-03-28 20:15:42 -0500640 if ((ret = asix_sw_reset(dev, AX_SWRESET_PRL)) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700641 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700642
David Brownell2e55cc72005-08-31 09:53:10 -0700643 msleep(150);
644
David Hollis48b1be62006-03-28 20:15:42 -0500645 if ((ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL)) < 0)
646 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700647
David Hollis48b1be62006-03-28 20:15:42 -0500648 msleep(150);
649
650 dev->net->set_multicast_list = asix_set_multicast;
David Brownell2e55cc72005-08-31 09:53:10 -0700651 dev->net->ethtool_ops = &ax88772_ethtool_ops;
652
David Hollis48b1be62006-03-28 20:15:42 -0500653 asix_mdio_write_le(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
654 asix_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
David Brownell2e55cc72005-08-31 09:53:10 -0700655 ADVERTISE_ALL | ADVERTISE_CSMA);
656 mii_nway_restart(&dev->mii);
657
David Hollis48b1be62006-03-28 20:15:42 -0500658 if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
David Brownell2e55cc72005-08-31 09:53:10 -0700659 AX88772_MEDIUM_DEFAULT, 0, 0, buf)) < 0) {
660 dbg("Write medium mode register: %d", ret);
661 goto out2;
662 }
663
David Hollis48b1be62006-03-28 20:15:42 -0500664 if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
David Brownell2e55cc72005-08-31 09:53:10 -0700665 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
666 AX88772_IPG2_DEFAULT, 0, buf)) < 0) {
667 dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
668 goto out2;
669 }
David Hollis48b1be62006-03-28 20:15:42 -0500670 if ((ret = asix_set_hw_mii(dev)) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700671 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700672
673 /* Set RX_CTL to default values with 2k buffer, and enable cactus */
David Hollis48b1be62006-03-28 20:15:42 -0500674 if ((ret = asix_write_rx_ctl(dev, 0x0088)) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700675 goto out2;
David Brownell2e55cc72005-08-31 09:53:10 -0700676
677 kfree(buf);
678
679 /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
680 if (dev->driver_info->flags & FLAG_FRAMING_AX) {
681 /* hard_mtu is still the default - the device does not support
682 jumbo eth frames */
683 dev->rx_urb_size = 2048;
684 }
685
686 return 0;
687
688out2:
689 kfree(buf);
690out1:
691 return ret;
692}
693
694static int ax88772_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
695{
696 u8 *head;
697 u32 header;
698 char *packet;
699 struct sk_buff *ax_skb;
700 u16 size;
701
702 head = (u8 *) skb->data;
703 memcpy(&header, head, sizeof(header));
704 le32_to_cpus(&header);
705 packet = head + sizeof(header);
706
707 skb_pull(skb, 4);
708
709 while (skb->len > 0) {
710 if ((short)(header & 0x0000ffff) !=
711 ~((short)((header & 0xffff0000) >> 16))) {
712 devdbg(dev,"header length data is error");
713 }
714 /* get the packet length */
715 size = (u16) (header & 0x0000ffff);
716
717 if ((skb->len) - ((size + 1) & 0xfffe) == 0)
718 return 2;
719 if (size > ETH_FRAME_LEN) {
720 devdbg(dev,"invalid rx length %d", size);
721 return 0;
722 }
723 ax_skb = skb_clone(skb, GFP_ATOMIC);
724 if (ax_skb) {
725 ax_skb->len = size;
726 ax_skb->data = packet;
727 ax_skb->tail = packet + size;
728 usbnet_skb_return(dev, ax_skb);
729 } else {
730 return 0;
731 }
732
733 skb_pull(skb, (size + 1) & 0xfffe);
734
735 if (skb->len == 0)
736 break;
737
738 head = (u8 *) skb->data;
739 memcpy(&header, head, sizeof(header));
740 le32_to_cpus(&header);
741 packet = head + sizeof(header);
742 skb_pull(skb, 4);
743 }
744
745 if (skb->len < 0) {
746 devdbg(dev,"invalid rx length %d", skb->len);
747 return 0;
748 }
749 return 1;
750}
751
752static struct sk_buff *ax88772_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
Al Viro55016f12005-10-21 03:21:58 -0400753 gfp_t flags)
David Brownell2e55cc72005-08-31 09:53:10 -0700754{
755 int padlen;
756 int headroom = skb_headroom(skb);
757 int tailroom = skb_tailroom(skb);
758 u32 packet_len;
759 u32 padbytes = 0xffff0000;
760
761 padlen = ((skb->len + 4) % 512) ? 0 : 4;
762
763 if ((!skb_cloned(skb))
764 && ((headroom + tailroom) >= (4 + padlen))) {
765 if ((headroom < 4) || (tailroom < padlen)) {
766 skb->data = memmove(skb->head + 4, skb->data, skb->len);
767 skb->tail = skb->data + skb->len;
768 }
769 } else {
770 struct sk_buff *skb2;
771 skb2 = skb_copy_expand(skb, 4, padlen, flags);
772 dev_kfree_skb_any(skb);
773 skb = skb2;
774 if (!skb)
775 return NULL;
776 }
777
778 skb_push(skb, 4);
779 packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
780 memcpy(skb->data, &packet_len, sizeof(packet_len));
781
782 if ((skb->len % 512) == 0) {
783 memcpy( skb->tail, &padbytes, sizeof(padbytes));
784 skb_put(skb, sizeof(padbytes));
785 }
786 return skb;
787}
788
789static int ax88772_link_reset(struct usbnet *dev)
790{
791 u16 lpa;
792 u16 adv;
793 u16 res;
794 u16 mode;
795
796 mode = AX88772_MEDIUM_DEFAULT;
David Hollis48b1be62006-03-28 20:15:42 -0500797 lpa = asix_mdio_read_le(dev->net, dev->mii.phy_id, MII_LPA);
798 adv = asix_mdio_read_le(dev->net, dev->mii.phy_id, MII_ADVERTISE);
David Brownell2e55cc72005-08-31 09:53:10 -0700799 res = mii_nway_result(lpa|adv);
800
801 if ((res & LPA_DUPLEX) == 0)
802 mode &= ~AX88772_MEDIUM_FULL_DUPLEX;
803 if ((res & LPA_100) == 0)
804 mode &= ~AX88772_MEDIUM_100MB;
David Hollis48b1be62006-03-28 20:15:42 -0500805 asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700806
807 return 0;
808}
809
810static const struct driver_info ax8817x_info = {
811 .description = "ASIX AX8817x USB 2.0 Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -0500812 .bind = ax88172_bind,
813 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -0700814 .link_reset = ax88172_link_reset,
815 .reset = ax88172_link_reset,
816 .flags = FLAG_ETHER,
817 .data = 0x00130103,
818};
819
820static const struct driver_info dlink_dub_e100_info = {
821 .description = "DLink DUB-E100 USB Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -0500822 .bind = ax88172_bind,
823 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -0700824 .link_reset = ax88172_link_reset,
825 .reset = ax88172_link_reset,
826 .flags = FLAG_ETHER,
827 .data = 0x009f9d9f,
828};
829
830static const struct driver_info netgear_fa120_info = {
831 .description = "Netgear FA-120 USB Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -0500832 .bind = ax88172_bind,
833 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -0700834 .link_reset = ax88172_link_reset,
835 .reset = ax88172_link_reset,
836 .flags = FLAG_ETHER,
837 .data = 0x00130103,
838};
839
840static const struct driver_info hawking_uf200_info = {
841 .description = "Hawking UF200 USB Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -0500842 .bind = ax88172_bind,
843 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -0700844 .link_reset = ax88172_link_reset,
845 .reset = ax88172_link_reset,
846 .flags = FLAG_ETHER,
847 .data = 0x001f1d1f,
848};
849
850static const struct driver_info ax88772_info = {
851 .description = "ASIX AX88772 USB 2.0 Ethernet",
852 .bind = ax88772_bind,
David Hollis48b1be62006-03-28 20:15:42 -0500853 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -0700854 .link_reset = ax88772_link_reset,
855 .reset = ax88772_link_reset,
856 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
857 .rx_fixup = ax88772_rx_fixup,
858 .tx_fixup = ax88772_tx_fixup,
David Brownell2e55cc72005-08-31 09:53:10 -0700859};
860
861static const struct usb_device_id products [] = {
862{
863 // Linksys USB200M
864 USB_DEVICE (0x077b, 0x2226),
865 .driver_info = (unsigned long) &ax8817x_info,
866}, {
867 // Netgear FA120
868 USB_DEVICE (0x0846, 0x1040),
869 .driver_info = (unsigned long) &netgear_fa120_info,
870}, {
871 // DLink DUB-E100
872 USB_DEVICE (0x2001, 0x1a00),
873 .driver_info = (unsigned long) &dlink_dub_e100_info,
874}, {
875 // Intellinet, ST Lab USB Ethernet
876 USB_DEVICE (0x0b95, 0x1720),
877 .driver_info = (unsigned long) &ax8817x_info,
878}, {
879 // Hawking UF200, TrendNet TU2-ET100
880 USB_DEVICE (0x07b8, 0x420a),
881 .driver_info = (unsigned long) &hawking_uf200_info,
882}, {
883 // Billionton Systems, USB2AR
884 USB_DEVICE (0x08dd, 0x90ff),
885 .driver_info = (unsigned long) &ax8817x_info,
886}, {
887 // ATEN UC210T
888 USB_DEVICE (0x0557, 0x2009),
889 .driver_info = (unsigned long) &ax8817x_info,
890}, {
891 // Buffalo LUA-U2-KTX
892 USB_DEVICE (0x0411, 0x003d),
893 .driver_info = (unsigned long) &ax8817x_info,
894}, {
895 // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
896 USB_DEVICE (0x6189, 0x182d),
897 .driver_info = (unsigned long) &ax8817x_info,
898}, {
899 // corega FEther USB2-TX
900 USB_DEVICE (0x07aa, 0x0017),
901 .driver_info = (unsigned long) &ax8817x_info,
902}, {
903 // Surecom EP-1427X-2
904 USB_DEVICE (0x1189, 0x0893),
905 .driver_info = (unsigned long) &ax8817x_info,
906}, {
907 // goodway corp usb gwusb2e
908 USB_DEVICE (0x1631, 0x6200),
909 .driver_info = (unsigned long) &ax8817x_info,
910}, {
911 // ASIX AX88772 10/100
912 USB_DEVICE (0x0b95, 0x7720),
913 .driver_info = (unsigned long) &ax88772_info,
David Hollis5e0f76c2005-12-19 13:58:38 -0500914}, {
915 // Linksys USB200M Rev 2
916 USB_DEVICE (0x13b1, 0x0018),
917 .driver_info = (unsigned long) &ax88772_info,
David Hollis5732ce82006-01-05 14:39:49 -0500918}, {
919 // 0Q0 cable ethernet
920 USB_DEVICE (0x1557, 0x7720),
921 .driver_info = (unsigned long) &ax88772_info,
David Brownell2e55cc72005-08-31 09:53:10 -0700922},
923 { }, // END
924};
925MODULE_DEVICE_TABLE(usb, products);
926
927static struct usb_driver asix_driver = {
David Brownell2e55cc72005-08-31 09:53:10 -0700928 .name = "asix",
929 .id_table = products,
930 .probe = usbnet_probe,
931 .suspend = usbnet_suspend,
932 .resume = usbnet_resume,
933 .disconnect = usbnet_disconnect,
934};
935
936static int __init asix_init(void)
937{
938 return usb_register(&asix_driver);
939}
940module_init(asix_init);
941
942static void __exit asix_exit(void)
943{
944 usb_deregister(&asix_driver);
945}
946module_exit(asix_exit);
947
948MODULE_AUTHOR("David Hollis");
949MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
950MODULE_LICENSE("GPL");
951