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_read_phy_addr(struct usbnet
*dev
, int internal
)
263 int offset
= (internal
? 1 : 0);
265 int ret
= asix_read_cmd(dev
, AX_CMD_READ_PHY_ID
, 0, 0, 2, buf
);
267 netdev_dbg(dev
->net
, "asix_get_phy_addr()\n");
270 netdev_err(dev
->net
, "Error reading PHYID register: %02x\n", ret
);
273 netdev_dbg(dev
->net
, "asix_get_phy_addr() returning 0x%04x\n",
281 int asix_get_phy_addr(struct usbnet
*dev
)
283 /* return the address of the internal phy */
284 return asix_read_phy_addr(dev
, 1);
288 int asix_sw_reset(struct usbnet
*dev
, u8 flags
)
292 ret
= asix_write_cmd(dev
, AX_CMD_SW_RESET
, flags
, 0, 0, NULL
);
294 netdev_err(dev
->net
, "Failed to send software reset: %02x\n", ret
);
299 u16
asix_read_rx_ctl(struct usbnet
*dev
)
302 int ret
= asix_read_cmd(dev
, AX_CMD_READ_RX_CTL
, 0, 0, 2, &v
);
305 netdev_err(dev
->net
, "Error reading RX_CTL register: %02x\n", ret
);
308 ret
= le16_to_cpu(v
);
313 int asix_write_rx_ctl(struct usbnet
*dev
, u16 mode
)
317 netdev_dbg(dev
->net
, "asix_write_rx_ctl() - mode = 0x%04x\n", mode
);
318 ret
= asix_write_cmd(dev
, AX_CMD_WRITE_RX_CTL
, mode
, 0, 0, NULL
);
320 netdev_err(dev
->net
, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
326 u16
asix_read_medium_status(struct usbnet
*dev
)
329 int ret
= asix_read_cmd(dev
, AX_CMD_READ_MEDIUM_STATUS
, 0, 0, 2, &v
);
332 netdev_err(dev
->net
, "Error reading Medium Status register: %02x\n",
334 return ret
; /* TODO: callers not checking for error ret */
337 return le16_to_cpu(v
);
341 int asix_write_medium_mode(struct usbnet
*dev
, u16 mode
)
345 netdev_dbg(dev
->net
, "asix_write_medium_mode() - mode = 0x%04x\n", mode
);
346 ret
= asix_write_cmd(dev
, AX_CMD_WRITE_MEDIUM_MODE
, mode
, 0, 0, NULL
);
348 netdev_err(dev
->net
, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
354 int asix_write_gpio(struct usbnet
*dev
, u16 value
, int sleep
)
358 netdev_dbg(dev
->net
, "asix_write_gpio() - value = 0x%04x\n", value
);
359 ret
= asix_write_cmd(dev
, AX_CMD_WRITE_GPIOS
, value
, 0, 0, NULL
);
361 netdev_err(dev
->net
, "Failed to write GPIO value 0x%04x: %02x\n",
371 * AX88772 & AX88178 have a 16-bit RX_CTL value
373 void asix_set_multicast(struct net_device
*net
)
375 struct usbnet
*dev
= netdev_priv(net
);
376 struct asix_data
*data
= (struct asix_data
*)&dev
->data
;
377 u16 rx_ctl
= AX_DEFAULT_RX_CTL
;
379 if (net
->flags
& IFF_PROMISC
) {
380 rx_ctl
|= AX_RX_CTL_PRO
;
381 } else if (net
->flags
& IFF_ALLMULTI
||
382 netdev_mc_count(net
) > AX_MAX_MCAST
) {
383 rx_ctl
|= AX_RX_CTL_AMALL
;
384 } else if (netdev_mc_empty(net
)) {
385 /* just broadcast and directed */
387 /* We use the 20 byte dev->data
388 * for our 8 byte filter buffer
389 * to avoid allocating memory that
390 * is tricky to free later */
391 struct netdev_hw_addr
*ha
;
394 memset(data
->multi_filter
, 0, AX_MCAST_FILTER_SIZE
);
396 /* Build the multicast hash filter. */
397 netdev_for_each_mc_addr(ha
, net
) {
398 crc_bits
= ether_crc(ETH_ALEN
, ha
->addr
) >> 26;
399 data
->multi_filter
[crc_bits
>> 3] |=
403 asix_write_cmd_async(dev
, AX_CMD_WRITE_MULTI_FILTER
, 0, 0,
404 AX_MCAST_FILTER_SIZE
, data
->multi_filter
);
406 rx_ctl
|= AX_RX_CTL_AM
;
409 asix_write_cmd_async(dev
, AX_CMD_WRITE_RX_CTL
, rx_ctl
, 0, 0, NULL
);
412 int asix_mdio_read(struct net_device
*netdev
, int phy_id
, int loc
)
414 struct usbnet
*dev
= netdev_priv(netdev
);
417 mutex_lock(&dev
->phy_mutex
);
418 asix_set_sw_mii(dev
);
419 asix_read_cmd(dev
, AX_CMD_READ_MII_REG
, phy_id
,
420 (__u16
)loc
, 2, &res
);
421 asix_set_hw_mii(dev
);
422 mutex_unlock(&dev
->phy_mutex
);
424 netdev_dbg(dev
->net
, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
425 phy_id
, loc
, le16_to_cpu(res
));
427 return le16_to_cpu(res
);
430 void asix_mdio_write(struct net_device
*netdev
, int phy_id
, int loc
, int val
)
432 struct usbnet
*dev
= netdev_priv(netdev
);
433 __le16 res
= cpu_to_le16(val
);
435 netdev_dbg(dev
->net
, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
437 mutex_lock(&dev
->phy_mutex
);
438 asix_set_sw_mii(dev
);
439 asix_write_cmd(dev
, AX_CMD_WRITE_MII_REG
, phy_id
, (__u16
)loc
, 2, &res
);
440 asix_set_hw_mii(dev
);
441 mutex_unlock(&dev
->phy_mutex
);
444 void asix_get_wol(struct net_device
*net
, struct ethtool_wolinfo
*wolinfo
)
446 struct usbnet
*dev
= netdev_priv(net
);
449 if (asix_read_cmd(dev
, AX_CMD_READ_MONITOR_MODE
, 0, 0, 1, &opt
) < 0) {
450 wolinfo
->supported
= 0;
451 wolinfo
->wolopts
= 0;
454 wolinfo
->supported
= WAKE_PHY
| WAKE_MAGIC
;
455 wolinfo
->wolopts
= 0;
456 if (opt
& AX_MONITOR_LINK
)
457 wolinfo
->wolopts
|= WAKE_PHY
;
458 if (opt
& AX_MONITOR_MAGIC
)
459 wolinfo
->wolopts
|= WAKE_MAGIC
;
462 int asix_set_wol(struct net_device
*net
, struct ethtool_wolinfo
*wolinfo
)
464 struct usbnet
*dev
= netdev_priv(net
);
467 if (wolinfo
->wolopts
& WAKE_PHY
)
468 opt
|= AX_MONITOR_LINK
;
469 if (wolinfo
->wolopts
& WAKE_MAGIC
)
470 opt
|= AX_MONITOR_MAGIC
;
472 if (asix_write_cmd(dev
, AX_CMD_WRITE_MONITOR_MODE
,
473 opt
, 0, 0, NULL
) < 0)
479 int asix_get_eeprom_len(struct net_device
*net
)
481 return AX_EEPROM_LEN
;
484 int asix_get_eeprom(struct net_device
*net
, struct ethtool_eeprom
*eeprom
,
487 struct usbnet
*dev
= netdev_priv(net
);
489 int first_word
, last_word
;
492 if (eeprom
->len
== 0)
495 eeprom
->magic
= AX_EEPROM_MAGIC
;
497 first_word
= eeprom
->offset
>> 1;
498 last_word
= (eeprom
->offset
+ eeprom
->len
- 1) >> 1;
500 eeprom_buff
= kmalloc(sizeof(u16
) * (last_word
- first_word
+ 1),
505 /* ax8817x returns 2 bytes from eeprom on read */
506 for (i
= first_word
; i
<= last_word
; i
++) {
507 if (asix_read_cmd(dev
, AX_CMD_READ_EEPROM
, i
, 0, 2,
508 &(eeprom_buff
[i
- first_word
])) < 0) {
514 memcpy(data
, (u8
*)eeprom_buff
+ (eeprom
->offset
& 1), eeprom
->len
);
519 int asix_set_eeprom(struct net_device
*net
, struct ethtool_eeprom
*eeprom
,
522 struct usbnet
*dev
= netdev_priv(net
);
524 int first_word
, last_word
;
528 netdev_dbg(net
, "write EEPROM len %d, offset %d, magic 0x%x\n",
529 eeprom
->len
, eeprom
->offset
, eeprom
->magic
);
531 if (eeprom
->len
== 0)
534 if (eeprom
->magic
!= AX_EEPROM_MAGIC
)
537 first_word
= eeprom
->offset
>> 1;
538 last_word
= (eeprom
->offset
+ eeprom
->len
- 1) >> 1;
540 eeprom_buff
= kmalloc(sizeof(u16
) * (last_word
- first_word
+ 1),
545 /* align data to 16 bit boundaries, read the missing data from
547 if (eeprom
->offset
& 1) {
548 ret
= asix_read_cmd(dev
, AX_CMD_READ_EEPROM
, first_word
, 0, 2,
551 netdev_err(net
, "Failed to read EEPROM at offset 0x%02x.\n", first_word
);
556 if ((eeprom
->offset
+ eeprom
->len
) & 1) {
557 ret
= asix_read_cmd(dev
, AX_CMD_READ_EEPROM
, last_word
, 0, 2,
558 &(eeprom_buff
[last_word
- first_word
]));
560 netdev_err(net
, "Failed to read EEPROM at offset 0x%02x.\n", last_word
);
565 memcpy((u8
*)eeprom_buff
+ (eeprom
->offset
& 1), data
, eeprom
->len
);
567 /* write data to EEPROM */
568 ret
= asix_write_cmd(dev
, AX_CMD_WRITE_ENABLE
, 0x0000, 0, 0, NULL
);
570 netdev_err(net
, "Failed to enable EEPROM write\n");
575 for (i
= first_word
; i
<= last_word
; i
++) {
576 netdev_dbg(net
, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
577 i
, eeprom_buff
[i
- first_word
]);
578 ret
= asix_write_cmd(dev
, AX_CMD_WRITE_EEPROM
, i
,
579 eeprom_buff
[i
- first_word
], 0, NULL
);
581 netdev_err(net
, "Failed to write EEPROM at offset 0x%02x.\n",
588 ret
= asix_write_cmd(dev
, AX_CMD_WRITE_DISABLE
, 0x0000, 0, 0, NULL
);
590 netdev_err(net
, "Failed to disable EEPROM write\n");
600 void asix_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*info
)
602 /* Inherit standard device info */
603 usbnet_get_drvinfo(net
, info
);
604 strncpy (info
->driver
, DRIVER_NAME
, sizeof info
->driver
);
605 strncpy (info
->version
, DRIVER_VERSION
, sizeof info
->version
);
606 info
->eedump_len
= AX_EEPROM_LEN
;
609 int asix_set_mac_address(struct net_device
*net
, void *p
)
611 struct usbnet
*dev
= netdev_priv(net
);
612 struct asix_data
*data
= (struct asix_data
*)&dev
->data
;
613 struct sockaddr
*addr
= p
;
615 if (netif_running(net
))
617 if (!is_valid_ether_addr(addr
->sa_data
))
618 return -EADDRNOTAVAIL
;
620 memcpy(net
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
622 /* We use the 20 byte dev->data
623 * for our 6 byte mac buffer
624 * to avoid allocating memory that
625 * is tricky to free later */
626 memcpy(data
->mac_addr
, addr
->sa_data
, ETH_ALEN
);
627 asix_write_cmd_async(dev
, AX_CMD_WRITE_NODE_ID
, 0, 0, ETH_ALEN
,