2 * ASIX AX8817X based USB 2.0 Ethernet Devices
3 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
4 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
5 * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
6 * Copyright (c) 2002-2003 TiVo Inc.
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.
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.
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
25 int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
31 netdev_dbg(dev->net, "asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
32 cmd, value, index, size);
34 buf = kmalloc(size, GFP_KERNEL);
38 err = usb_control_msg(
40 usb_rcvctrlpipe(dev->udev, 0),
42 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
47 USB_CTRL_GET_TIMEOUT);
49 memcpy(data, buf, size);
58 int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
64 netdev_dbg(dev->net, "asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
65 cmd, value, index, size);
68 buf = kmemdup(data, size, GFP_KERNEL);
73 err = usb_control_msg(
75 usb_sndctrlpipe(dev->udev, 0),
77 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
82 USB_CTRL_SET_TIMEOUT);
89 static void asix_async_cmd_callback(struct urb *urb)
91 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
92 int status = urb->status;
95 printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
102 void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
103 u16 size, void *data)
105 struct usb_ctrlrequest *req;
109 netdev_dbg(dev->net, "asix_write_cmd_async() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
110 cmd, value, index, size);
112 urb = usb_alloc_urb(0, GFP_ATOMIC);
114 netdev_err(dev->net, "Error allocating URB in write_cmd_async!\n");
118 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
120 netdev_err(dev->net, "Failed to allocate memory for control request\n");
125 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
127 req->wValue = cpu_to_le16(value);
128 req->wIndex = cpu_to_le16(index);
129 req->wLength = cpu_to_le16(size);
131 usb_fill_control_urb(urb, dev->udev,
132 usb_sndctrlpipe(dev->udev, 0),
133 (void *)req, data, size,
134 asix_async_cmd_callback, req);
136 status = usb_submit_urb(urb, GFP_ATOMIC);
138 netdev_err(dev->net, "Error submitting the control message: status=%d\n",
145 int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
149 while (offset + sizeof(u32) < skb->len) {
150 struct sk_buff *ax_skb;
152 u32 header = get_unaligned_le32(skb->data + offset);
154 offset += sizeof(u32);
156 /* get the packet length */
157 size = (u16) (header & 0x7ff);
158 if (size != ((~header >> 16) & 0x07ff)) {
159 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
163 if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
164 (size + offset > skb->len)) {
165 netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
169 ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
173 skb_put(ax_skb, size);
174 memcpy(ax_skb->data, skb->data + offset, size);
175 usbnet_skb_return(dev, ax_skb);
177 offset += (size + 1) & 0xfffe;
180 if (skb->len != offset) {
181 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
188 struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
192 int headroom = skb_headroom(skb);
193 int tailroom = skb_tailroom(skb);
195 u32 padbytes = 0xffff0000;
197 padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
199 /* We need to push 4 bytes in front of frame (packet_len)
200 * and maybe add 4 bytes after the end (if padlen is 4)
202 * Avoid skb_copy_expand() expensive call, using following rules :
203 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
204 * is false (and if we have 4 bytes of headroom)
205 * - We are allowed to put 4 bytes at tail if skb_cloned()
206 * is false (and if we have 4 bytes of tailroom)
208 * TCP packets for example are cloned, but skb_header_release()
209 * was called in tcp stack, allowing us to use headroom for our needs.
211 if (!skb_header_cloned(skb) &&
212 !(padlen && skb_cloned(skb)) &&
213 headroom + tailroom >= 4 + padlen) {
214 /* following should not happen, but better be safe */
217 skb->data = memmove(skb->head + 4, skb->data, skb->len);
218 skb_set_tail_pointer(skb, skb->len);
221 struct sk_buff *skb2;
223 skb2 = skb_copy_expand(skb, 4, padlen, flags);
224 dev_kfree_skb_any(skb);
230 packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
232 cpu_to_le32s(&packet_len);
233 skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
236 cpu_to_le32s(&padbytes);
237 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
238 skb_put(skb, sizeof(padbytes));
243 int asix_set_sw_mii(struct usbnet *dev)
246 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
248 netdev_err(dev->net, "Failed to enable software MII access\n");
252 int asix_set_hw_mii(struct usbnet *dev)
255 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
257 netdev_err(dev->net, "Failed to enable hardware MII access\n");
261 int asix_get_phy_addr(struct usbnet *dev)
264 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
266 netdev_dbg(dev->net, "asix_get_phy_addr()\n");
269 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
272 netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
280 int asix_sw_reset(struct usbnet *dev, u8 flags)
284 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
286 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
291 u16 asix_read_rx_ctl(struct usbnet *dev)
294 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
297 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
300 ret = le16_to_cpu(v);
305 int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
309 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
310 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
312 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
318 u16 asix_read_medium_status(struct usbnet *dev)
321 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
324 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
326 return ret; /* TODO: callers not checking for error ret */
329 return le16_to_cpu(v);
333 int asix_write_medium_mode(struct usbnet *dev, u16 mode)
337 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
338 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
340 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
346 int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
350 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
351 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
353 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
363 * AX88772 & AX88178 have a 16-bit RX_CTL value
365 void asix_set_multicast(struct net_device *net)
367 struct usbnet *dev = netdev_priv(net);
368 struct asix_data *data = (struct asix_data *)&dev->data;
369 u16 rx_ctl = AX_DEFAULT_RX_CTL;
371 if (net->flags & IFF_PROMISC) {
372 rx_ctl |= AX_RX_CTL_PRO;
373 } else if (net->flags & IFF_ALLMULTI ||
374 netdev_mc_count(net) > AX_MAX_MCAST) {
375 rx_ctl |= AX_RX_CTL_AMALL;
376 } else if (netdev_mc_empty(net)) {
377 /* just broadcast and directed */
379 /* We use the 20 byte dev->data
380 * for our 8 byte filter buffer
381 * to avoid allocating memory that
382 * is tricky to free later */
383 struct netdev_hw_addr *ha;
386 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
388 /* Build the multicast hash filter. */
389 netdev_for_each_mc_addr(ha, net) {
390 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
391 data->multi_filter[crc_bits >> 3] |=
395 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
396 AX_MCAST_FILTER_SIZE, data->multi_filter);
398 rx_ctl |= AX_RX_CTL_AM;
401 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
404 int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
406 struct usbnet *dev = netdev_priv(netdev);
409 mutex_lock(&dev->phy_mutex);
410 asix_set_sw_mii(dev);
411 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
412 (__u16)loc, 2, &res);
413 asix_set_hw_mii(dev);
414 mutex_unlock(&dev->phy_mutex);
416 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
417 phy_id, loc, le16_to_cpu(res));
419 return le16_to_cpu(res);
422 void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
424 struct usbnet *dev = netdev_priv(netdev);
425 __le16 res = cpu_to_le16(val);
427 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
429 mutex_lock(&dev->phy_mutex);
430 asix_set_sw_mii(dev);
431 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
432 asix_set_hw_mii(dev);
433 mutex_unlock(&dev->phy_mutex);
436 void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
438 struct usbnet *dev = netdev_priv(net);
441 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
442 wolinfo->supported = 0;
443 wolinfo->wolopts = 0;
446 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
447 wolinfo->wolopts = 0;
448 if (opt & AX_MONITOR_LINK)
449 wolinfo->wolopts |= WAKE_PHY;
450 if (opt & AX_MONITOR_MAGIC)
451 wolinfo->wolopts |= WAKE_MAGIC;
454 int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
456 struct usbnet *dev = netdev_priv(net);
459 if (wolinfo->wolopts & WAKE_PHY)
460 opt |= AX_MONITOR_LINK;
461 if (wolinfo->wolopts & WAKE_MAGIC)
462 opt |= AX_MONITOR_MAGIC;
464 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
465 opt, 0, 0, NULL) < 0)
471 int asix_get_eeprom_len(struct net_device *net)
473 struct usbnet *dev = netdev_priv(net);
474 struct asix_data *data = (struct asix_data *)&dev->data;
476 return data->eeprom_len;
479 int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
482 struct usbnet *dev = netdev_priv(net);
483 __le16 *ebuf = (__le16 *)data;
486 /* Crude hack to ensure that we don't overwrite memory
487 * if an odd length is supplied
492 eeprom->magic = AX_EEPROM_MAGIC;
494 /* ax8817x returns 2 bytes from eeprom on read */
495 for (i=0; i < eeprom->len / 2; i++) {
496 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
497 eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
503 void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
505 struct usbnet *dev = netdev_priv(net);
506 struct asix_data *data = (struct asix_data *)&dev->data;
508 /* Inherit standard device info */
509 usbnet_get_drvinfo(net, info);
510 strncpy (info->driver, DRIVER_NAME, sizeof info->driver);
511 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
512 info->eedump_len = data->eeprom_len;
515 int asix_set_mac_address(struct net_device *net, void *p)
517 struct usbnet *dev = netdev_priv(net);
518 struct asix_data *data = (struct asix_data *)&dev->data;
519 struct sockaddr *addr = p;
521 if (netif_running(net))
523 if (!is_valid_ether_addr(addr->sa_data))
524 return -EADDRNOTAVAIL;
526 memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
528 /* We use the 20 byte dev->data
529 * for our 6 byte mac buffer
530 * to avoid allocating memory that
531 * is tricky to free later */
532 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
533 asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,