1 /***************************************************************************
3 * Copyright (C) 2007-2008 SMSC
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *****************************************************************************/
21 #include <linux/module.h>
22 #include <linux/kmod.h>
23 #include <linux/init.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/mii.h>
28 #include <linux/usb.h>
29 #include <linux/crc32.h>
30 #include <linux/usb/usbnet.h>
31 #include <linux/slab.h>
34 #define SMSC_CHIPNAME "smsc95xx"
35 #define SMSC_DRIVER_VERSION "1.0.4"
36 #define HS_USB_PKT_SIZE (512)
37 #define FS_USB_PKT_SIZE (64)
38 #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
39 #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
40 #define DEFAULT_BULK_IN_DELAY (0x00002000)
41 #define MAX_SINGLE_PACKET_SIZE (2048)
42 #define LAN95XX_EEPROM_MAGIC (0x9500)
43 #define EEPROM_MAC_OFFSET (0x01)
44 #define DEFAULT_TX_CSUM_ENABLE (true)
45 #define DEFAULT_RX_CSUM_ENABLE (true)
46 #define SMSC95XX_INTERNAL_PHY_ID (1)
47 #define SMSC95XX_TX_OVERHEAD (8)
48 #define SMSC95XX_TX_OVERHEAD_CSUM (12)
50 struct smsc95xx_priv
{
54 spinlock_t mac_cr_lock
;
58 struct usb_ctrlrequest req
;
62 static bool turbo_mode
= true;
63 module_param(turbo_mode
, bool, 0644);
64 MODULE_PARM_DESC(turbo_mode
, "Enable multiple frames per Rx transaction");
66 static int smsc95xx_read_reg(struct usbnet
*dev
, u32 index
, u32
*data
)
68 u32
*buf
= kmalloc(4, GFP_KERNEL
);
76 ret
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
77 USB_VENDOR_REQUEST_READ_REGISTER
,
78 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
79 00, index
, buf
, 4, USB_CTRL_GET_TIMEOUT
);
81 if (unlikely(ret
< 0))
82 netdev_warn(dev
->net
, "Failed to read register index 0x%08x\n", index
);
91 static int smsc95xx_write_reg(struct usbnet
*dev
, u32 index
, u32 data
)
93 u32
*buf
= kmalloc(4, GFP_KERNEL
);
104 ret
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
105 USB_VENDOR_REQUEST_WRITE_REGISTER
,
106 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
107 00, index
, buf
, 4, USB_CTRL_SET_TIMEOUT
);
109 if (unlikely(ret
< 0))
110 netdev_warn(dev
->net
, "Failed to write register index 0x%08x\n", index
);
117 /* Loop until the read is completed with timeout
118 * called with phy_mutex held */
119 static int smsc95xx_phy_wait_not_busy(struct usbnet
*dev
)
121 unsigned long start_time
= jiffies
;
125 smsc95xx_read_reg(dev
, MII_ADDR
, &val
);
126 if (!(val
& MII_BUSY_
))
128 } while (!time_after(jiffies
, start_time
+ HZ
));
133 static int smsc95xx_mdio_read(struct net_device
*netdev
, int phy_id
, int idx
)
135 struct usbnet
*dev
= netdev_priv(netdev
);
138 mutex_lock(&dev
->phy_mutex
);
140 /* confirm MII not busy */
141 if (smsc95xx_phy_wait_not_busy(dev
)) {
142 netdev_warn(dev
->net
, "MII is busy in smsc95xx_mdio_read\n");
143 mutex_unlock(&dev
->phy_mutex
);
147 /* set the address, index & direction (read from PHY) */
148 phy_id
&= dev
->mii
.phy_id_mask
;
149 idx
&= dev
->mii
.reg_num_mask
;
150 addr
= (phy_id
<< 11) | (idx
<< 6) | MII_READ_
;
151 smsc95xx_write_reg(dev
, MII_ADDR
, addr
);
153 if (smsc95xx_phy_wait_not_busy(dev
)) {
154 netdev_warn(dev
->net
, "Timed out reading MII reg %02X\n", idx
);
155 mutex_unlock(&dev
->phy_mutex
);
159 smsc95xx_read_reg(dev
, MII_DATA
, &val
);
161 mutex_unlock(&dev
->phy_mutex
);
163 return (u16
)(val
& 0xFFFF);
166 static void smsc95xx_mdio_write(struct net_device
*netdev
, int phy_id
, int idx
,
169 struct usbnet
*dev
= netdev_priv(netdev
);
172 mutex_lock(&dev
->phy_mutex
);
174 /* confirm MII not busy */
175 if (smsc95xx_phy_wait_not_busy(dev
)) {
176 netdev_warn(dev
->net
, "MII is busy in smsc95xx_mdio_write\n");
177 mutex_unlock(&dev
->phy_mutex
);
182 smsc95xx_write_reg(dev
, MII_DATA
, val
);
184 /* set the address, index & direction (write to PHY) */
185 phy_id
&= dev
->mii
.phy_id_mask
;
186 idx
&= dev
->mii
.reg_num_mask
;
187 addr
= (phy_id
<< 11) | (idx
<< 6) | MII_WRITE_
;
188 smsc95xx_write_reg(dev
, MII_ADDR
, addr
);
190 if (smsc95xx_phy_wait_not_busy(dev
))
191 netdev_warn(dev
->net
, "Timed out writing MII reg %02X\n", idx
);
193 mutex_unlock(&dev
->phy_mutex
);
196 static int smsc95xx_wait_eeprom(struct usbnet
*dev
)
198 unsigned long start_time
= jiffies
;
202 smsc95xx_read_reg(dev
, E2P_CMD
, &val
);
203 if (!(val
& E2P_CMD_BUSY_
) || (val
& E2P_CMD_TIMEOUT_
))
206 } while (!time_after(jiffies
, start_time
+ HZ
));
208 if (val
& (E2P_CMD_TIMEOUT_
| E2P_CMD_BUSY_
)) {
209 netdev_warn(dev
->net
, "EEPROM read operation timeout\n");
216 static int smsc95xx_eeprom_confirm_not_busy(struct usbnet
*dev
)
218 unsigned long start_time
= jiffies
;
222 smsc95xx_read_reg(dev
, E2P_CMD
, &val
);
224 if (!(val
& E2P_CMD_BUSY_
))
228 } while (!time_after(jiffies
, start_time
+ HZ
));
230 netdev_warn(dev
->net
, "EEPROM is busy\n");
234 static int smsc95xx_read_eeprom(struct usbnet
*dev
, u32 offset
, u32 length
,
243 ret
= smsc95xx_eeprom_confirm_not_busy(dev
);
247 for (i
= 0; i
< length
; i
++) {
248 val
= E2P_CMD_BUSY_
| E2P_CMD_READ_
| (offset
& E2P_CMD_ADDR_
);
249 smsc95xx_write_reg(dev
, E2P_CMD
, val
);
251 ret
= smsc95xx_wait_eeprom(dev
);
255 smsc95xx_read_reg(dev
, E2P_DATA
, &val
);
257 data
[i
] = val
& 0xFF;
264 static int smsc95xx_write_eeprom(struct usbnet
*dev
, u32 offset
, u32 length
,
273 ret
= smsc95xx_eeprom_confirm_not_busy(dev
);
277 /* Issue write/erase enable command */
278 val
= E2P_CMD_BUSY_
| E2P_CMD_EWEN_
;
279 smsc95xx_write_reg(dev
, E2P_CMD
, val
);
281 ret
= smsc95xx_wait_eeprom(dev
);
285 for (i
= 0; i
< length
; i
++) {
287 /* Fill data register */
289 smsc95xx_write_reg(dev
, E2P_DATA
, val
);
291 /* Send "write" command */
292 val
= E2P_CMD_BUSY_
| E2P_CMD_WRITE_
| (offset
& E2P_CMD_ADDR_
);
293 smsc95xx_write_reg(dev
, E2P_CMD
, val
);
295 ret
= smsc95xx_wait_eeprom(dev
);
305 static void smsc95xx_async_cmd_callback(struct urb
*urb
)
307 struct usb_context
*usb_context
= urb
->context
;
308 struct usbnet
*dev
= usb_context
->dev
;
309 int status
= urb
->status
;
312 netdev_warn(dev
->net
, "async callback failed with %d\n", status
);
318 static int smsc95xx_write_reg_async(struct usbnet
*dev
, u16 index
, u32
*data
)
320 struct usb_context
*usb_context
;
325 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
327 netdev_warn(dev
->net
, "Error allocating URB\n");
331 usb_context
= kmalloc(sizeof(struct usb_context
), GFP_ATOMIC
);
332 if (usb_context
== NULL
) {
333 netdev_warn(dev
->net
, "Error allocating control msg\n");
338 usb_context
->req
.bRequestType
=
339 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
;
340 usb_context
->req
.bRequest
= USB_VENDOR_REQUEST_WRITE_REGISTER
;
341 usb_context
->req
.wValue
= 00;
342 usb_context
->req
.wIndex
= cpu_to_le16(index
);
343 usb_context
->req
.wLength
= cpu_to_le16(size
);
345 usb_fill_control_urb(urb
, dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
346 (void *)&usb_context
->req
, data
, size
,
347 smsc95xx_async_cmd_callback
,
348 (void *)usb_context
);
350 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
352 netdev_warn(dev
->net
, "Error submitting control msg, sts=%d\n",
361 /* returns hash bit number for given MAC address
363 * 01 00 5E 00 00 01 -> returns bit number 31 */
364 static unsigned int smsc95xx_hash(char addr
[ETH_ALEN
])
366 return (ether_crc(ETH_ALEN
, addr
) >> 26) & 0x3f;
369 static void smsc95xx_set_multicast(struct net_device
*netdev
)
371 struct usbnet
*dev
= netdev_priv(netdev
);
372 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
378 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
380 if (dev
->net
->flags
& IFF_PROMISC
) {
381 netif_dbg(dev
, drv
, dev
->net
, "promiscuous mode enabled\n");
382 pdata
->mac_cr
|= MAC_CR_PRMS_
;
383 pdata
->mac_cr
&= ~(MAC_CR_MCPAS_
| MAC_CR_HPFILT_
);
384 } else if (dev
->net
->flags
& IFF_ALLMULTI
) {
385 netif_dbg(dev
, drv
, dev
->net
, "receive all multicast enabled\n");
386 pdata
->mac_cr
|= MAC_CR_MCPAS_
;
387 pdata
->mac_cr
&= ~(MAC_CR_PRMS_
| MAC_CR_HPFILT_
);
388 } else if (!netdev_mc_empty(dev
->net
)) {
389 struct netdev_hw_addr
*ha
;
391 pdata
->mac_cr
|= MAC_CR_HPFILT_
;
392 pdata
->mac_cr
&= ~(MAC_CR_PRMS_
| MAC_CR_MCPAS_
);
394 netdev_for_each_mc_addr(ha
, netdev
) {
395 u32 bitnum
= smsc95xx_hash(ha
->addr
);
396 u32 mask
= 0x01 << (bitnum
& 0x1F);
398 pdata
->hash_hi
|= mask
;
400 pdata
->hash_lo
|= mask
;
403 netif_dbg(dev
, drv
, dev
->net
, "HASHH=0x%08X, HASHL=0x%08X\n",
404 pdata
->hash_hi
, pdata
->hash_lo
);
406 netif_dbg(dev
, drv
, dev
->net
, "receive own packets only\n");
408 ~(MAC_CR_PRMS_
| MAC_CR_MCPAS_
| MAC_CR_HPFILT_
);
411 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
413 /* Initiate async writes, as we can't wait for completion here */
414 smsc95xx_write_reg_async(dev
, HASHH
, &pdata
->hash_hi
);
415 smsc95xx_write_reg_async(dev
, HASHL
, &pdata
->hash_lo
);
416 smsc95xx_write_reg_async(dev
, MAC_CR
, &pdata
->mac_cr
);
419 static void smsc95xx_phy_update_flowcontrol(struct usbnet
*dev
, u8 duplex
,
420 u16 lcladv
, u16 rmtadv
)
422 u32 flow
, afc_cfg
= 0;
424 int ret
= smsc95xx_read_reg(dev
, AFC_CFG
, &afc_cfg
);
426 netdev_warn(dev
->net
, "error reading AFC_CFG\n");
430 if (duplex
== DUPLEX_FULL
) {
431 u8 cap
= mii_resolve_flowctrl_fdx(lcladv
, rmtadv
);
433 if (cap
& FLOW_CTRL_RX
)
438 if (cap
& FLOW_CTRL_TX
)
443 netif_dbg(dev
, link
, dev
->net
, "rx pause %s, tx pause %s\n",
444 cap
& FLOW_CTRL_RX
? "enabled" : "disabled",
445 cap
& FLOW_CTRL_TX
? "enabled" : "disabled");
447 netif_dbg(dev
, link
, dev
->net
, "half duplex\n");
452 smsc95xx_write_reg(dev
, FLOW
, flow
);
453 smsc95xx_write_reg(dev
, AFC_CFG
, afc_cfg
);
456 static int smsc95xx_link_reset(struct usbnet
*dev
)
458 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
459 struct mii_if_info
*mii
= &dev
->mii
;
460 struct ethtool_cmd ecmd
= { .cmd
= ETHTOOL_GSET
};
465 /* clear interrupt status */
466 smsc95xx_mdio_read(dev
->net
, mii
->phy_id
, PHY_INT_SRC
);
467 intdata
= 0xFFFFFFFF;
468 smsc95xx_write_reg(dev
, INT_STS
, intdata
);
470 mii_check_media(mii
, 1, 1);
471 mii_ethtool_gset(&dev
->mii
, &ecmd
);
472 lcladv
= smsc95xx_mdio_read(dev
->net
, mii
->phy_id
, MII_ADVERTISE
);
473 rmtadv
= smsc95xx_mdio_read(dev
->net
, mii
->phy_id
, MII_LPA
);
475 netif_dbg(dev
, link
, dev
->net
,
476 "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
477 ethtool_cmd_speed(&ecmd
), ecmd
.duplex
, lcladv
, rmtadv
);
479 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
480 if (ecmd
.duplex
!= DUPLEX_FULL
) {
481 pdata
->mac_cr
&= ~MAC_CR_FDPX_
;
482 pdata
->mac_cr
|= MAC_CR_RCVOWN_
;
484 pdata
->mac_cr
&= ~MAC_CR_RCVOWN_
;
485 pdata
->mac_cr
|= MAC_CR_FDPX_
;
487 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
489 smsc95xx_write_reg(dev
, MAC_CR
, pdata
->mac_cr
);
491 smsc95xx_phy_update_flowcontrol(dev
, ecmd
.duplex
, lcladv
, rmtadv
);
496 static void smsc95xx_status(struct usbnet
*dev
, struct urb
*urb
)
500 if (urb
->actual_length
!= 4) {
501 netdev_warn(dev
->net
, "unexpected urb length %d\n",
506 memcpy(&intdata
, urb
->transfer_buffer
, 4);
507 le32_to_cpus(&intdata
);
509 netif_dbg(dev
, link
, dev
->net
, "intdata: 0x%08X\n", intdata
);
511 if (intdata
& INT_ENP_PHY_INT_
)
512 usbnet_defer_kevent(dev
, EVENT_LINK_RESET
);
514 netdev_warn(dev
->net
, "unexpected interrupt, intdata=0x%08X\n",
518 /* Enable or disable Tx & Rx checksum offload engines */
519 static int smsc95xx_set_features(struct net_device
*netdev
,
520 netdev_features_t features
)
522 struct usbnet
*dev
= netdev_priv(netdev
);
526 ret
= smsc95xx_read_reg(dev
, COE_CR
, &read_buf
);
528 netdev_warn(dev
->net
, "Failed to read COE_CR: %d\n", ret
);
532 if (features
& NETIF_F_HW_CSUM
)
533 read_buf
|= Tx_COE_EN_
;
535 read_buf
&= ~Tx_COE_EN_
;
537 if (features
& NETIF_F_RXCSUM
)
538 read_buf
|= Rx_COE_EN_
;
540 read_buf
&= ~Rx_COE_EN_
;
542 ret
= smsc95xx_write_reg(dev
, COE_CR
, read_buf
);
544 netdev_warn(dev
->net
, "Failed to write COE_CR: %d\n", ret
);
548 netif_dbg(dev
, hw
, dev
->net
, "COE_CR = 0x%08x\n", read_buf
);
552 static int smsc95xx_ethtool_get_eeprom_len(struct net_device
*net
)
554 return MAX_EEPROM_SIZE
;
557 static int smsc95xx_ethtool_get_eeprom(struct net_device
*netdev
,
558 struct ethtool_eeprom
*ee
, u8
*data
)
560 struct usbnet
*dev
= netdev_priv(netdev
);
562 ee
->magic
= LAN95XX_EEPROM_MAGIC
;
564 return smsc95xx_read_eeprom(dev
, ee
->offset
, ee
->len
, data
);
567 static int smsc95xx_ethtool_set_eeprom(struct net_device
*netdev
,
568 struct ethtool_eeprom
*ee
, u8
*data
)
570 struct usbnet
*dev
= netdev_priv(netdev
);
572 if (ee
->magic
!= LAN95XX_EEPROM_MAGIC
) {
573 netdev_warn(dev
->net
, "EEPROM: magic value mismatch, magic = 0x%x\n",
578 return smsc95xx_write_eeprom(dev
, ee
->offset
, ee
->len
, data
);
581 static const struct ethtool_ops smsc95xx_ethtool_ops
= {
582 .get_link
= usbnet_get_link
,
583 .nway_reset
= usbnet_nway_reset
,
584 .get_drvinfo
= usbnet_get_drvinfo
,
585 .get_msglevel
= usbnet_get_msglevel
,
586 .set_msglevel
= usbnet_set_msglevel
,
587 .get_settings
= usbnet_get_settings
,
588 .set_settings
= usbnet_set_settings
,
589 .get_eeprom_len
= smsc95xx_ethtool_get_eeprom_len
,
590 .get_eeprom
= smsc95xx_ethtool_get_eeprom
,
591 .set_eeprom
= smsc95xx_ethtool_set_eeprom
,
594 static int smsc95xx_ioctl(struct net_device
*netdev
, struct ifreq
*rq
, int cmd
)
596 struct usbnet
*dev
= netdev_priv(netdev
);
598 if (!netif_running(netdev
))
601 return generic_mii_ioctl(&dev
->mii
, if_mii(rq
), cmd
, NULL
);
604 static void smsc95xx_init_mac_address(struct usbnet
*dev
)
606 /* try reading mac address from EEPROM */
607 if (smsc95xx_read_eeprom(dev
, EEPROM_MAC_OFFSET
, ETH_ALEN
,
608 dev
->net
->dev_addr
) == 0) {
609 if (is_valid_ether_addr(dev
->net
->dev_addr
)) {
610 /* eeprom values are valid so use them */
611 netif_dbg(dev
, ifup
, dev
->net
, "MAC address read from EEPROM\n");
616 /* no eeprom, or eeprom values are invalid. generate random MAC */
617 eth_hw_addr_random(dev
->net
);
618 netif_dbg(dev
, ifup
, dev
->net
, "MAC address set to random_ether_addr\n");
621 static int smsc95xx_set_mac_address(struct usbnet
*dev
)
623 u32 addr_lo
= dev
->net
->dev_addr
[0] | dev
->net
->dev_addr
[1] << 8 |
624 dev
->net
->dev_addr
[2] << 16 | dev
->net
->dev_addr
[3] << 24;
625 u32 addr_hi
= dev
->net
->dev_addr
[4] | dev
->net
->dev_addr
[5] << 8;
628 ret
= smsc95xx_write_reg(dev
, ADDRL
, addr_lo
);
630 netdev_warn(dev
->net
, "Failed to write ADDRL: %d\n", ret
);
634 ret
= smsc95xx_write_reg(dev
, ADDRH
, addr_hi
);
636 netdev_warn(dev
->net
, "Failed to write ADDRH: %d\n", ret
);
643 /* starts the TX path */
644 static void smsc95xx_start_tx_path(struct usbnet
*dev
)
646 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
650 /* Enable Tx at MAC */
651 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
652 pdata
->mac_cr
|= MAC_CR_TXEN_
;
653 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
655 smsc95xx_write_reg(dev
, MAC_CR
, pdata
->mac_cr
);
657 /* Enable Tx at SCSRs */
658 reg_val
= TX_CFG_ON_
;
659 smsc95xx_write_reg(dev
, TX_CFG
, reg_val
);
662 /* Starts the Receive path */
663 static void smsc95xx_start_rx_path(struct usbnet
*dev
)
665 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
668 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
669 pdata
->mac_cr
|= MAC_CR_RXEN_
;
670 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
672 smsc95xx_write_reg(dev
, MAC_CR
, pdata
->mac_cr
);
675 static int smsc95xx_phy_initialize(struct usbnet
*dev
)
677 int bmcr
, timeout
= 0;
679 /* Initialize MII structure */
680 dev
->mii
.dev
= dev
->net
;
681 dev
->mii
.mdio_read
= smsc95xx_mdio_read
;
682 dev
->mii
.mdio_write
= smsc95xx_mdio_write
;
683 dev
->mii
.phy_id_mask
= 0x1f;
684 dev
->mii
.reg_num_mask
= 0x1f;
685 dev
->mii
.phy_id
= SMSC95XX_INTERNAL_PHY_ID
;
687 /* reset phy and wait for reset to complete */
688 smsc95xx_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_BMCR
, BMCR_RESET
);
692 bmcr
= smsc95xx_mdio_read(dev
->net
, dev
->mii
.phy_id
, MII_BMCR
);
694 } while ((bmcr
& BMCR_RESET
) && (timeout
< 100));
696 if (timeout
>= 100) {
697 netdev_warn(dev
->net
, "timeout on PHY Reset");
701 smsc95xx_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_ADVERTISE
,
702 ADVERTISE_ALL
| ADVERTISE_CSMA
| ADVERTISE_PAUSE_CAP
|
703 ADVERTISE_PAUSE_ASYM
);
706 smsc95xx_mdio_read(dev
->net
, dev
->mii
.phy_id
, PHY_INT_SRC
);
708 smsc95xx_mdio_write(dev
->net
, dev
->mii
.phy_id
, PHY_INT_MASK
,
709 PHY_INT_MASK_DEFAULT_
);
710 mii_nway_restart(&dev
->mii
);
712 netif_dbg(dev
, ifup
, dev
->net
, "phy initialised successfully\n");
716 static int smsc95xx_reset(struct usbnet
*dev
)
718 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
719 u32 read_buf
, write_buf
, burst_cap
;
720 int ret
= 0, timeout
;
722 netif_dbg(dev
, ifup
, dev
->net
, "entering smsc95xx_reset\n");
724 write_buf
= HW_CFG_LRST_
;
725 ret
= smsc95xx_write_reg(dev
, HW_CFG
, write_buf
);
727 netdev_warn(dev
->net
, "Failed to write HW_CFG_LRST_ bit in HW_CFG register, ret = %d\n",
734 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
736 netdev_warn(dev
->net
, "Failed to read HW_CFG: %d\n", ret
);
741 } while ((read_buf
& HW_CFG_LRST_
) && (timeout
< 100));
743 if (timeout
>= 100) {
744 netdev_warn(dev
->net
, "timeout waiting for completion of Lite Reset\n");
748 write_buf
= PM_CTL_PHY_RST_
;
749 ret
= smsc95xx_write_reg(dev
, PM_CTRL
, write_buf
);
751 netdev_warn(dev
->net
, "Failed to write PM_CTRL: %d\n", ret
);
757 ret
= smsc95xx_read_reg(dev
, PM_CTRL
, &read_buf
);
759 netdev_warn(dev
->net
, "Failed to read PM_CTRL: %d\n", ret
);
764 } while ((read_buf
& PM_CTL_PHY_RST_
) && (timeout
< 100));
766 if (timeout
>= 100) {
767 netdev_warn(dev
->net
, "timeout waiting for PHY Reset\n");
771 ret
= smsc95xx_set_mac_address(dev
);
775 netif_dbg(dev
, ifup
, dev
->net
,
776 "MAC Address: %pM\n", dev
->net
->dev_addr
);
778 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
780 netdev_warn(dev
->net
, "Failed to read HW_CFG: %d\n", ret
);
784 netif_dbg(dev
, ifup
, dev
->net
,
785 "Read Value from HW_CFG : 0x%08x\n", read_buf
);
787 read_buf
|= HW_CFG_BIR_
;
789 ret
= smsc95xx_write_reg(dev
, HW_CFG
, read_buf
);
791 netdev_warn(dev
->net
, "Failed to write HW_CFG_BIR_ bit in HW_CFG register, ret = %d\n",
796 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
798 netdev_warn(dev
->net
, "Failed to read HW_CFG: %d\n", ret
);
801 netif_dbg(dev
, ifup
, dev
->net
,
802 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
807 dev
->rx_urb_size
= MAX_SINGLE_PACKET_SIZE
;
808 } else if (dev
->udev
->speed
== USB_SPEED_HIGH
) {
809 burst_cap
= DEFAULT_HS_BURST_CAP_SIZE
/ HS_USB_PKT_SIZE
;
810 dev
->rx_urb_size
= DEFAULT_HS_BURST_CAP_SIZE
;
812 burst_cap
= DEFAULT_FS_BURST_CAP_SIZE
/ FS_USB_PKT_SIZE
;
813 dev
->rx_urb_size
= DEFAULT_FS_BURST_CAP_SIZE
;
816 netif_dbg(dev
, ifup
, dev
->net
,
817 "rx_urb_size=%ld\n", (ulong
)dev
->rx_urb_size
);
819 ret
= smsc95xx_write_reg(dev
, BURST_CAP
, burst_cap
);
821 netdev_warn(dev
->net
, "Failed to write BURST_CAP: %d\n", ret
);
825 ret
= smsc95xx_read_reg(dev
, BURST_CAP
, &read_buf
);
827 netdev_warn(dev
->net
, "Failed to read BURST_CAP: %d\n", ret
);
830 netif_dbg(dev
, ifup
, dev
->net
,
831 "Read Value from BURST_CAP after writing: 0x%08x\n",
834 read_buf
= DEFAULT_BULK_IN_DELAY
;
835 ret
= smsc95xx_write_reg(dev
, BULK_IN_DLY
, read_buf
);
837 netdev_warn(dev
->net
, "ret = %d\n", ret
);
841 ret
= smsc95xx_read_reg(dev
, BULK_IN_DLY
, &read_buf
);
843 netdev_warn(dev
->net
, "Failed to read BULK_IN_DLY: %d\n", ret
);
846 netif_dbg(dev
, ifup
, dev
->net
,
847 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
850 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
852 netdev_warn(dev
->net
, "Failed to read HW_CFG: %d\n", ret
);
855 netif_dbg(dev
, ifup
, dev
->net
,
856 "Read Value from HW_CFG: 0x%08x\n", read_buf
);
859 read_buf
|= (HW_CFG_MEF_
| HW_CFG_BCE_
);
861 read_buf
&= ~HW_CFG_RXDOFF_
;
863 /* set Rx data offset=2, Make IP header aligns on word boundary. */
864 read_buf
|= NET_IP_ALIGN
<< 9;
866 ret
= smsc95xx_write_reg(dev
, HW_CFG
, read_buf
);
868 netdev_warn(dev
->net
, "Failed to write HW_CFG register, ret=%d\n",
873 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
875 netdev_warn(dev
->net
, "Failed to read HW_CFG: %d\n", ret
);
878 netif_dbg(dev
, ifup
, dev
->net
,
879 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf
);
881 write_buf
= 0xFFFFFFFF;
882 ret
= smsc95xx_write_reg(dev
, INT_STS
, write_buf
);
884 netdev_warn(dev
->net
, "Failed to write INT_STS register, ret=%d\n",
889 ret
= smsc95xx_read_reg(dev
, ID_REV
, &read_buf
);
891 netdev_warn(dev
->net
, "Failed to read ID_REV: %d\n", ret
);
894 netif_dbg(dev
, ifup
, dev
->net
, "ID_REV = 0x%08x\n", read_buf
);
896 /* Configure GPIO pins as LED outputs */
897 write_buf
= LED_GPIO_CFG_SPD_LED
| LED_GPIO_CFG_LNK_LED
|
898 LED_GPIO_CFG_FDX_LED
;
899 ret
= smsc95xx_write_reg(dev
, LED_GPIO_CFG
, write_buf
);
901 netdev_warn(dev
->net
, "Failed to write LED_GPIO_CFG register, ret=%d\n",
908 ret
= smsc95xx_write_reg(dev
, FLOW
, write_buf
);
910 netdev_warn(dev
->net
, "Failed to write FLOW: %d\n", ret
);
914 read_buf
= AFC_CFG_DEFAULT
;
915 ret
= smsc95xx_write_reg(dev
, AFC_CFG
, read_buf
);
917 netdev_warn(dev
->net
, "Failed to write AFC_CFG: %d\n", ret
);
921 /* Don't need mac_cr_lock during initialisation */
922 ret
= smsc95xx_read_reg(dev
, MAC_CR
, &pdata
->mac_cr
);
924 netdev_warn(dev
->net
, "Failed to read MAC_CR: %d\n", ret
);
930 write_buf
= (u32
)ETH_P_8021Q
;
931 ret
= smsc95xx_write_reg(dev
, VLAN1
, write_buf
);
933 netdev_warn(dev
->net
, "Failed to write VAN1: %d\n", ret
);
937 /* Enable or disable checksum offload engines */
938 smsc95xx_set_features(dev
->net
, dev
->net
->features
);
940 smsc95xx_set_multicast(dev
->net
);
942 if (smsc95xx_phy_initialize(dev
) < 0)
945 ret
= smsc95xx_read_reg(dev
, INT_EP_CTL
, &read_buf
);
947 netdev_warn(dev
->net
, "Failed to read INT_EP_CTL: %d\n", ret
);
951 /* enable PHY interrupts */
952 read_buf
|= INT_EP_CTL_PHY_INT_
;
954 ret
= smsc95xx_write_reg(dev
, INT_EP_CTL
, read_buf
);
956 netdev_warn(dev
->net
, "Failed to write INT_EP_CTL: %d\n", ret
);
960 smsc95xx_start_tx_path(dev
);
961 smsc95xx_start_rx_path(dev
);
963 netif_dbg(dev
, ifup
, dev
->net
, "smsc95xx_reset, return 0\n");
967 static const struct net_device_ops smsc95xx_netdev_ops
= {
968 .ndo_open
= usbnet_open
,
969 .ndo_stop
= usbnet_stop
,
970 .ndo_start_xmit
= usbnet_start_xmit
,
971 .ndo_tx_timeout
= usbnet_tx_timeout
,
972 .ndo_change_mtu
= usbnet_change_mtu
,
973 .ndo_set_mac_address
= eth_mac_addr
,
974 .ndo_validate_addr
= eth_validate_addr
,
975 .ndo_do_ioctl
= smsc95xx_ioctl
,
976 .ndo_set_rx_mode
= smsc95xx_set_multicast
,
977 .ndo_set_features
= smsc95xx_set_features
,
980 static int smsc95xx_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
982 struct smsc95xx_priv
*pdata
= NULL
;
985 printk(KERN_INFO SMSC_CHIPNAME
" v" SMSC_DRIVER_VERSION
"\n");
987 ret
= usbnet_get_endpoints(dev
, intf
);
989 netdev_warn(dev
->net
, "usbnet_get_endpoints failed: %d\n", ret
);
993 dev
->data
[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv
),
996 pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
998 netdev_warn(dev
->net
, "Unable to allocate struct smsc95xx_priv\n");
1002 spin_lock_init(&pdata
->mac_cr_lock
);
1004 if (DEFAULT_TX_CSUM_ENABLE
)
1005 dev
->net
->features
|= NETIF_F_HW_CSUM
;
1006 if (DEFAULT_RX_CSUM_ENABLE
)
1007 dev
->net
->features
|= NETIF_F_RXCSUM
;
1009 dev
->net
->hw_features
= NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1011 smsc95xx_init_mac_address(dev
);
1013 /* Init all registers */
1014 ret
= smsc95xx_reset(dev
);
1016 dev
->net
->netdev_ops
= &smsc95xx_netdev_ops
;
1017 dev
->net
->ethtool_ops
= &smsc95xx_ethtool_ops
;
1018 dev
->net
->flags
|= IFF_MULTICAST
;
1019 dev
->net
->hard_header_len
+= SMSC95XX_TX_OVERHEAD_CSUM
;
1020 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
1024 static void smsc95xx_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
1026 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1028 netif_dbg(dev
, ifdown
, dev
->net
, "free pdata\n");
1035 static void smsc95xx_rx_csum_offload(struct sk_buff
*skb
)
1037 skb
->csum
= *(u16
*)(skb_tail_pointer(skb
) - 2);
1038 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1039 skb_trim(skb
, skb
->len
- 2);
1042 static int smsc95xx_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
1044 while (skb
->len
> 0) {
1045 u32 header
, align_count
;
1046 struct sk_buff
*ax_skb
;
1047 unsigned char *packet
;
1050 memcpy(&header
, skb
->data
, sizeof(header
));
1051 le32_to_cpus(&header
);
1052 skb_pull(skb
, 4 + NET_IP_ALIGN
);
1055 /* get the packet length */
1056 size
= (u16
)((header
& RX_STS_FL_
) >> 16);
1057 align_count
= (4 - ((size
+ NET_IP_ALIGN
) % 4)) % 4;
1059 if (unlikely(header
& RX_STS_ES_
)) {
1060 netif_dbg(dev
, rx_err
, dev
->net
,
1061 "Error header=0x%08x\n", header
);
1062 dev
->net
->stats
.rx_errors
++;
1063 dev
->net
->stats
.rx_dropped
++;
1065 if (header
& RX_STS_CRC_
) {
1066 dev
->net
->stats
.rx_crc_errors
++;
1068 if (header
& (RX_STS_TL_
| RX_STS_RF_
))
1069 dev
->net
->stats
.rx_frame_errors
++;
1071 if ((header
& RX_STS_LE_
) &&
1072 (!(header
& RX_STS_FT_
)))
1073 dev
->net
->stats
.rx_length_errors
++;
1076 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1077 if (unlikely(size
> (ETH_FRAME_LEN
+ 12))) {
1078 netif_dbg(dev
, rx_err
, dev
->net
,
1079 "size err header=0x%08x\n", header
);
1083 /* last frame in this batch */
1084 if (skb
->len
== size
) {
1085 if (dev
->net
->features
& NETIF_F_RXCSUM
)
1086 smsc95xx_rx_csum_offload(skb
);
1087 skb_trim(skb
, skb
->len
- 4); /* remove fcs */
1088 skb
->truesize
= size
+ sizeof(struct sk_buff
);
1093 ax_skb
= skb_clone(skb
, GFP_ATOMIC
);
1094 if (unlikely(!ax_skb
)) {
1095 netdev_warn(dev
->net
, "Error allocating skb\n");
1100 ax_skb
->data
= packet
;
1101 skb_set_tail_pointer(ax_skb
, size
);
1103 if (dev
->net
->features
& NETIF_F_RXCSUM
)
1104 smsc95xx_rx_csum_offload(ax_skb
);
1105 skb_trim(ax_skb
, ax_skb
->len
- 4); /* remove fcs */
1106 ax_skb
->truesize
= size
+ sizeof(struct sk_buff
);
1108 usbnet_skb_return(dev
, ax_skb
);
1111 skb_pull(skb
, size
);
1113 /* padding bytes before the next frame starts */
1115 skb_pull(skb
, align_count
);
1118 if (unlikely(skb
->len
< 0)) {
1119 netdev_warn(dev
->net
, "invalid rx length<0 %d\n", skb
->len
);
1126 static u32
smsc95xx_calc_csum_preamble(struct sk_buff
*skb
)
1128 u16 low_16
= (u16
)skb_checksum_start_offset(skb
);
1129 u16 high_16
= low_16
+ skb
->csum_offset
;
1130 return (high_16
<< 16) | low_16
;
1133 static struct sk_buff
*smsc95xx_tx_fixup(struct usbnet
*dev
,
1134 struct sk_buff
*skb
, gfp_t flags
)
1136 bool csum
= skb
->ip_summed
== CHECKSUM_PARTIAL
;
1137 int overhead
= csum
? SMSC95XX_TX_OVERHEAD_CSUM
: SMSC95XX_TX_OVERHEAD
;
1138 u32 tx_cmd_a
, tx_cmd_b
;
1140 /* We do not advertise SG, so skbs should be already linearized */
1141 BUG_ON(skb_shinfo(skb
)->nr_frags
);
1143 if (skb_headroom(skb
) < overhead
) {
1144 struct sk_buff
*skb2
= skb_copy_expand(skb
,
1145 overhead
, 0, flags
);
1146 dev_kfree_skb_any(skb
);
1153 if (skb
->len
<= 45) {
1154 /* workaround - hardware tx checksum does not work
1155 * properly with extremely small packets */
1156 long csstart
= skb_checksum_start_offset(skb
);
1157 __wsum calc
= csum_partial(skb
->data
+ csstart
,
1158 skb
->len
- csstart
, 0);
1159 *((__sum16
*)(skb
->data
+ csstart
1160 + skb
->csum_offset
)) = csum_fold(calc
);
1164 u32 csum_preamble
= smsc95xx_calc_csum_preamble(skb
);
1166 memcpy(skb
->data
, &csum_preamble
, 4);
1171 tx_cmd_b
= (u32
)(skb
->len
- 4);
1173 tx_cmd_b
|= TX_CMD_B_CSUM_ENABLE
;
1174 cpu_to_le32s(&tx_cmd_b
);
1175 memcpy(skb
->data
, &tx_cmd_b
, 4);
1178 tx_cmd_a
= (u32
)(skb
->len
- 8) | TX_CMD_A_FIRST_SEG_
|
1180 cpu_to_le32s(&tx_cmd_a
);
1181 memcpy(skb
->data
, &tx_cmd_a
, 4);
1186 static const struct driver_info smsc95xx_info
= {
1187 .description
= "smsc95xx USB 2.0 Ethernet",
1188 .bind
= smsc95xx_bind
,
1189 .unbind
= smsc95xx_unbind
,
1190 .link_reset
= smsc95xx_link_reset
,
1191 .reset
= smsc95xx_reset
,
1192 .rx_fixup
= smsc95xx_rx_fixup
,
1193 .tx_fixup
= smsc95xx_tx_fixup
,
1194 .status
= smsc95xx_status
,
1195 .flags
= FLAG_ETHER
| FLAG_SEND_ZLP
| FLAG_LINK_INTR
,
1198 static const struct usb_device_id products
[] = {
1200 /* SMSC9500 USB Ethernet Device */
1201 USB_DEVICE(0x0424, 0x9500),
1202 .driver_info
= (unsigned long) &smsc95xx_info
,
1205 /* SMSC9505 USB Ethernet Device */
1206 USB_DEVICE(0x0424, 0x9505),
1207 .driver_info
= (unsigned long) &smsc95xx_info
,
1210 /* SMSC9500A USB Ethernet Device */
1211 USB_DEVICE(0x0424, 0x9E00),
1212 .driver_info
= (unsigned long) &smsc95xx_info
,
1215 /* SMSC9505A USB Ethernet Device */
1216 USB_DEVICE(0x0424, 0x9E01),
1217 .driver_info
= (unsigned long) &smsc95xx_info
,
1220 /* SMSC9512/9514 USB Hub & Ethernet Device */
1221 USB_DEVICE(0x0424, 0xec00),
1222 .driver_info
= (unsigned long) &smsc95xx_info
,
1225 /* SMSC9500 USB Ethernet Device (SAL10) */
1226 USB_DEVICE(0x0424, 0x9900),
1227 .driver_info
= (unsigned long) &smsc95xx_info
,
1230 /* SMSC9505 USB Ethernet Device (SAL10) */
1231 USB_DEVICE(0x0424, 0x9901),
1232 .driver_info
= (unsigned long) &smsc95xx_info
,
1235 /* SMSC9500A USB Ethernet Device (SAL10) */
1236 USB_DEVICE(0x0424, 0x9902),
1237 .driver_info
= (unsigned long) &smsc95xx_info
,
1240 /* SMSC9505A USB Ethernet Device (SAL10) */
1241 USB_DEVICE(0x0424, 0x9903),
1242 .driver_info
= (unsigned long) &smsc95xx_info
,
1245 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
1246 USB_DEVICE(0x0424, 0x9904),
1247 .driver_info
= (unsigned long) &smsc95xx_info
,
1250 /* SMSC9500A USB Ethernet Device (HAL) */
1251 USB_DEVICE(0x0424, 0x9905),
1252 .driver_info
= (unsigned long) &smsc95xx_info
,
1255 /* SMSC9505A USB Ethernet Device (HAL) */
1256 USB_DEVICE(0x0424, 0x9906),
1257 .driver_info
= (unsigned long) &smsc95xx_info
,
1260 /* SMSC9500 USB Ethernet Device (Alternate ID) */
1261 USB_DEVICE(0x0424, 0x9907),
1262 .driver_info
= (unsigned long) &smsc95xx_info
,
1265 /* SMSC9500A USB Ethernet Device (Alternate ID) */
1266 USB_DEVICE(0x0424, 0x9908),
1267 .driver_info
= (unsigned long) &smsc95xx_info
,
1270 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
1271 USB_DEVICE(0x0424, 0x9909),
1272 .driver_info
= (unsigned long) &smsc95xx_info
,
1275 /* SMSC LAN9530 USB Ethernet Device */
1276 USB_DEVICE(0x0424, 0x9530),
1277 .driver_info
= (unsigned long) &smsc95xx_info
,
1280 /* SMSC LAN9730 USB Ethernet Device */
1281 USB_DEVICE(0x0424, 0x9730),
1282 .driver_info
= (unsigned long) &smsc95xx_info
,
1285 /* SMSC LAN89530 USB Ethernet Device */
1286 USB_DEVICE(0x0424, 0x9E08),
1287 .driver_info
= (unsigned long) &smsc95xx_info
,
1291 MODULE_DEVICE_TABLE(usb
, products
);
1293 static struct usb_driver smsc95xx_driver
= {
1295 .id_table
= products
,
1296 .probe
= usbnet_probe
,
1297 .suspend
= usbnet_suspend
,
1298 .resume
= usbnet_resume
,
1299 .disconnect
= usbnet_disconnect
,
1300 .disable_hub_initiated_lpm
= 1,
1303 module_usb_driver(smsc95xx_driver
);
1305 MODULE_AUTHOR("Nancy Lin");
1306 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@smsc.com>");
1307 MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
1308 MODULE_LICENSE("GPL");