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, see <http://www.gnu.org/licenses/>.
18 *****************************************************************************/
20 #include <linux/module.h>
21 #include <linux/kmod.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/mii.h>
26 #include <linux/usb.h>
27 #include <linux/bitrev.h>
28 #include <linux/crc16.h>
29 #include <linux/crc32.h>
30 #include <linux/usb/usbnet.h>
31 #include <linux/slab.h>
32 #include <linux/of_net.h>
35 #define SMSC_CHIPNAME "smsc95xx"
36 #define SMSC_DRIVER_VERSION "1.0.4"
37 #define HS_USB_PKT_SIZE (512)
38 #define FS_USB_PKT_SIZE (64)
39 #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
40 #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
41 #define DEFAULT_BULK_IN_DELAY (0x00002000)
42 #define MAX_SINGLE_PACKET_SIZE (2048)
43 #define LAN95XX_EEPROM_MAGIC (0x9500)
44 #define EEPROM_MAC_OFFSET (0x01)
45 #define DEFAULT_TX_CSUM_ENABLE (true)
46 #define DEFAULT_RX_CSUM_ENABLE (true)
47 #define SMSC95XX_INTERNAL_PHY_ID (1)
48 #define SMSC95XX_TX_OVERHEAD (8)
49 #define SMSC95XX_TX_OVERHEAD_CSUM (12)
50 #define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
51 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
53 #define FEATURE_8_WAKEUP_FILTERS (0x01)
54 #define FEATURE_PHY_NLP_CROSSOVER (0x02)
55 #define FEATURE_REMOTE_WAKEUP (0x04)
57 #define SUSPEND_SUSPEND0 (0x01)
58 #define SUSPEND_SUSPEND1 (0x02)
59 #define SUSPEND_SUSPEND2 (0x04)
60 #define SUSPEND_SUSPEND3 (0x08)
61 #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
62 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
64 #define CARRIER_CHECK_DELAY (2 * HZ)
66 struct smsc95xx_priv
{
71 spinlock_t mac_cr_lock
;
75 struct delayed_work carrier_check
;
79 static bool turbo_mode
= true;
80 module_param(turbo_mode
, bool, 0644);
81 MODULE_PARM_DESC(turbo_mode
, "Enable multiple frames per Rx transaction");
83 static int __must_check
__smsc95xx_read_reg(struct usbnet
*dev
, u32 index
,
88 int (*fn
)(struct usbnet
*, u8
, u8
, u16
, u16
, void *, u16
);
95 fn
= usbnet_read_cmd_nopm
;
97 ret
= fn(dev
, USB_VENDOR_REQUEST_READ_REGISTER
, USB_DIR_IN
98 | USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
100 if (unlikely(ret
< 0)) {
101 netdev_warn(dev
->net
, "Failed to read reg index 0x%08x: %d\n",
112 static int __must_check
__smsc95xx_write_reg(struct usbnet
*dev
, u32 index
,
117 int (*fn
)(struct usbnet
*, u8
, u8
, u16
, u16
, const void *, u16
);
122 fn
= usbnet_write_cmd
;
124 fn
= usbnet_write_cmd_nopm
;
129 ret
= fn(dev
, USB_VENDOR_REQUEST_WRITE_REGISTER
, USB_DIR_OUT
130 | USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
132 if (unlikely(ret
< 0))
133 netdev_warn(dev
->net
, "Failed to write reg index 0x%08x: %d\n",
139 static int __must_check
smsc95xx_read_reg_nopm(struct usbnet
*dev
, u32 index
,
142 return __smsc95xx_read_reg(dev
, index
, data
, 1);
145 static int __must_check
smsc95xx_write_reg_nopm(struct usbnet
*dev
, u32 index
,
148 return __smsc95xx_write_reg(dev
, index
, data
, 1);
151 static int __must_check
smsc95xx_read_reg(struct usbnet
*dev
, u32 index
,
154 return __smsc95xx_read_reg(dev
, index
, data
, 0);
157 static int __must_check
smsc95xx_write_reg(struct usbnet
*dev
, u32 index
,
160 return __smsc95xx_write_reg(dev
, index
, data
, 0);
163 /* Loop until the read is completed with timeout
164 * called with phy_mutex held */
165 static int __must_check
__smsc95xx_phy_wait_not_busy(struct usbnet
*dev
,
168 unsigned long start_time
= jiffies
;
173 ret
= __smsc95xx_read_reg(dev
, MII_ADDR
, &val
, in_pm
);
175 netdev_warn(dev
->net
, "Error reading MII_ACCESS\n");
179 if (!(val
& MII_BUSY_
))
181 } while (!time_after(jiffies
, start_time
+ HZ
));
186 static int __smsc95xx_mdio_read(struct net_device
*netdev
, int phy_id
, int idx
,
189 struct usbnet
*dev
= netdev_priv(netdev
);
193 mutex_lock(&dev
->phy_mutex
);
195 /* confirm MII not busy */
196 ret
= __smsc95xx_phy_wait_not_busy(dev
, in_pm
);
198 netdev_warn(dev
->net
, "MII is busy in smsc95xx_mdio_read\n");
202 /* set the address, index & direction (read from PHY) */
203 phy_id
&= dev
->mii
.phy_id_mask
;
204 idx
&= dev
->mii
.reg_num_mask
;
205 addr
= (phy_id
<< 11) | (idx
<< 6) | MII_READ_
| MII_BUSY_
;
206 ret
= __smsc95xx_write_reg(dev
, MII_ADDR
, addr
, in_pm
);
208 netdev_warn(dev
->net
, "Error writing MII_ADDR\n");
212 ret
= __smsc95xx_phy_wait_not_busy(dev
, in_pm
);
214 netdev_warn(dev
->net
, "Timed out reading MII reg %02X\n", idx
);
218 ret
= __smsc95xx_read_reg(dev
, MII_DATA
, &val
, in_pm
);
220 netdev_warn(dev
->net
, "Error reading MII_DATA\n");
224 ret
= (u16
)(val
& 0xFFFF);
227 mutex_unlock(&dev
->phy_mutex
);
231 static void __smsc95xx_mdio_write(struct net_device
*netdev
, int phy_id
,
232 int idx
, int regval
, int in_pm
)
234 struct usbnet
*dev
= netdev_priv(netdev
);
238 mutex_lock(&dev
->phy_mutex
);
240 /* confirm MII not busy */
241 ret
= __smsc95xx_phy_wait_not_busy(dev
, in_pm
);
243 netdev_warn(dev
->net
, "MII is busy in smsc95xx_mdio_write\n");
248 ret
= __smsc95xx_write_reg(dev
, MII_DATA
, val
, in_pm
);
250 netdev_warn(dev
->net
, "Error writing MII_DATA\n");
254 /* set the address, index & direction (write to PHY) */
255 phy_id
&= dev
->mii
.phy_id_mask
;
256 idx
&= dev
->mii
.reg_num_mask
;
257 addr
= (phy_id
<< 11) | (idx
<< 6) | MII_WRITE_
| MII_BUSY_
;
258 ret
= __smsc95xx_write_reg(dev
, MII_ADDR
, addr
, in_pm
);
260 netdev_warn(dev
->net
, "Error writing MII_ADDR\n");
264 ret
= __smsc95xx_phy_wait_not_busy(dev
, in_pm
);
266 netdev_warn(dev
->net
, "Timed out writing MII reg %02X\n", idx
);
271 mutex_unlock(&dev
->phy_mutex
);
274 static int smsc95xx_mdio_read_nopm(struct net_device
*netdev
, int phy_id
,
277 return __smsc95xx_mdio_read(netdev
, phy_id
, idx
, 1);
280 static void smsc95xx_mdio_write_nopm(struct net_device
*netdev
, int phy_id
,
283 __smsc95xx_mdio_write(netdev
, phy_id
, idx
, regval
, 1);
286 static int smsc95xx_mdio_read(struct net_device
*netdev
, int phy_id
, int idx
)
288 return __smsc95xx_mdio_read(netdev
, phy_id
, idx
, 0);
291 static void smsc95xx_mdio_write(struct net_device
*netdev
, int phy_id
, int idx
,
294 __smsc95xx_mdio_write(netdev
, phy_id
, idx
, regval
, 0);
297 static int __must_check
smsc95xx_wait_eeprom(struct usbnet
*dev
)
299 unsigned long start_time
= jiffies
;
304 ret
= smsc95xx_read_reg(dev
, E2P_CMD
, &val
);
306 netdev_warn(dev
->net
, "Error reading E2P_CMD\n");
310 if (!(val
& E2P_CMD_BUSY_
) || (val
& E2P_CMD_TIMEOUT_
))
313 } while (!time_after(jiffies
, start_time
+ HZ
));
315 if (val
& (E2P_CMD_TIMEOUT_
| E2P_CMD_BUSY_
)) {
316 netdev_warn(dev
->net
, "EEPROM read operation timeout\n");
323 static int __must_check
smsc95xx_eeprom_confirm_not_busy(struct usbnet
*dev
)
325 unsigned long start_time
= jiffies
;
330 ret
= smsc95xx_read_reg(dev
, E2P_CMD
, &val
);
332 netdev_warn(dev
->net
, "Error reading E2P_CMD\n");
336 if (!(val
& E2P_CMD_BUSY_
))
340 } while (!time_after(jiffies
, start_time
+ HZ
));
342 netdev_warn(dev
->net
, "EEPROM is busy\n");
346 static int smsc95xx_read_eeprom(struct usbnet
*dev
, u32 offset
, u32 length
,
355 ret
= smsc95xx_eeprom_confirm_not_busy(dev
);
359 for (i
= 0; i
< length
; i
++) {
360 val
= E2P_CMD_BUSY_
| E2P_CMD_READ_
| (offset
& E2P_CMD_ADDR_
);
361 ret
= smsc95xx_write_reg(dev
, E2P_CMD
, val
);
363 netdev_warn(dev
->net
, "Error writing E2P_CMD\n");
367 ret
= smsc95xx_wait_eeprom(dev
);
371 ret
= smsc95xx_read_reg(dev
, E2P_DATA
, &val
);
373 netdev_warn(dev
->net
, "Error reading E2P_DATA\n");
377 data
[i
] = val
& 0xFF;
384 static int smsc95xx_write_eeprom(struct usbnet
*dev
, u32 offset
, u32 length
,
393 ret
= smsc95xx_eeprom_confirm_not_busy(dev
);
397 /* Issue write/erase enable command */
398 val
= E2P_CMD_BUSY_
| E2P_CMD_EWEN_
;
399 ret
= smsc95xx_write_reg(dev
, E2P_CMD
, val
);
401 netdev_warn(dev
->net
, "Error writing E2P_DATA\n");
405 ret
= smsc95xx_wait_eeprom(dev
);
409 for (i
= 0; i
< length
; i
++) {
411 /* Fill data register */
413 ret
= smsc95xx_write_reg(dev
, E2P_DATA
, val
);
415 netdev_warn(dev
->net
, "Error writing E2P_DATA\n");
419 /* Send "write" command */
420 val
= E2P_CMD_BUSY_
| E2P_CMD_WRITE_
| (offset
& E2P_CMD_ADDR_
);
421 ret
= smsc95xx_write_reg(dev
, E2P_CMD
, val
);
423 netdev_warn(dev
->net
, "Error writing E2P_CMD\n");
427 ret
= smsc95xx_wait_eeprom(dev
);
437 static int __must_check
smsc95xx_write_reg_async(struct usbnet
*dev
, u16 index
,
447 ret
= usbnet_write_cmd_async(dev
, USB_VENDOR_REQUEST_WRITE_REGISTER
,
448 USB_DIR_OUT
| USB_TYPE_VENDOR
|
450 0, index
, &buf
, size
);
452 netdev_warn(dev
->net
, "Error write async cmd, sts=%d\n",
457 /* returns hash bit number for given MAC address
459 * 01 00 5E 00 00 01 -> returns bit number 31 */
460 static unsigned int smsc95xx_hash(char addr
[ETH_ALEN
])
462 return (ether_crc(ETH_ALEN
, addr
) >> 26) & 0x3f;
465 static void smsc95xx_set_multicast(struct net_device
*netdev
)
467 struct usbnet
*dev
= netdev_priv(netdev
);
468 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
475 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
477 if (dev
->net
->flags
& IFF_PROMISC
) {
478 netif_dbg(dev
, drv
, dev
->net
, "promiscuous mode enabled\n");
479 pdata
->mac_cr
|= MAC_CR_PRMS_
;
480 pdata
->mac_cr
&= ~(MAC_CR_MCPAS_
| MAC_CR_HPFILT_
);
481 } else if (dev
->net
->flags
& IFF_ALLMULTI
) {
482 netif_dbg(dev
, drv
, dev
->net
, "receive all multicast enabled\n");
483 pdata
->mac_cr
|= MAC_CR_MCPAS_
;
484 pdata
->mac_cr
&= ~(MAC_CR_PRMS_
| MAC_CR_HPFILT_
);
485 } else if (!netdev_mc_empty(dev
->net
)) {
486 struct netdev_hw_addr
*ha
;
488 pdata
->mac_cr
|= MAC_CR_HPFILT_
;
489 pdata
->mac_cr
&= ~(MAC_CR_PRMS_
| MAC_CR_MCPAS_
);
491 netdev_for_each_mc_addr(ha
, netdev
) {
492 u32 bitnum
= smsc95xx_hash(ha
->addr
);
493 u32 mask
= 0x01 << (bitnum
& 0x1F);
495 pdata
->hash_hi
|= mask
;
497 pdata
->hash_lo
|= mask
;
500 netif_dbg(dev
, drv
, dev
->net
, "HASHH=0x%08X, HASHL=0x%08X\n",
501 pdata
->hash_hi
, pdata
->hash_lo
);
503 netif_dbg(dev
, drv
, dev
->net
, "receive own packets only\n");
505 ~(MAC_CR_PRMS_
| MAC_CR_MCPAS_
| MAC_CR_HPFILT_
);
508 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
510 /* Initiate async writes, as we can't wait for completion here */
511 ret
= smsc95xx_write_reg_async(dev
, HASHH
, pdata
->hash_hi
);
513 netdev_warn(dev
->net
, "failed to initiate async write to HASHH\n");
515 ret
= smsc95xx_write_reg_async(dev
, HASHL
, pdata
->hash_lo
);
517 netdev_warn(dev
->net
, "failed to initiate async write to HASHL\n");
519 ret
= smsc95xx_write_reg_async(dev
, MAC_CR
, pdata
->mac_cr
);
521 netdev_warn(dev
->net
, "failed to initiate async write to MAC_CR\n");
524 static int smsc95xx_phy_update_flowcontrol(struct usbnet
*dev
, u8 duplex
,
525 u16 lcladv
, u16 rmtadv
)
527 u32 flow
, afc_cfg
= 0;
529 int ret
= smsc95xx_read_reg(dev
, AFC_CFG
, &afc_cfg
);
533 if (duplex
== DUPLEX_FULL
) {
534 u8 cap
= mii_resolve_flowctrl_fdx(lcladv
, rmtadv
);
536 if (cap
& FLOW_CTRL_RX
)
541 if (cap
& FLOW_CTRL_TX
)
546 netif_dbg(dev
, link
, dev
->net
, "rx pause %s, tx pause %s\n",
547 cap
& FLOW_CTRL_RX
? "enabled" : "disabled",
548 cap
& FLOW_CTRL_TX
? "enabled" : "disabled");
550 netif_dbg(dev
, link
, dev
->net
, "half duplex\n");
555 ret
= smsc95xx_write_reg(dev
, FLOW
, flow
);
559 return smsc95xx_write_reg(dev
, AFC_CFG
, afc_cfg
);
562 static int smsc95xx_link_reset(struct usbnet
*dev
)
564 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
565 struct mii_if_info
*mii
= &dev
->mii
;
566 struct ethtool_cmd ecmd
= { .cmd
= ETHTOOL_GSET
};
571 /* clear interrupt status */
572 ret
= smsc95xx_mdio_read(dev
->net
, mii
->phy_id
, PHY_INT_SRC
);
576 ret
= smsc95xx_write_reg(dev
, INT_STS
, INT_STS_CLEAR_ALL_
);
580 mii_check_media(mii
, 1, 1);
581 mii_ethtool_gset(&dev
->mii
, &ecmd
);
582 lcladv
= smsc95xx_mdio_read(dev
->net
, mii
->phy_id
, MII_ADVERTISE
);
583 rmtadv
= smsc95xx_mdio_read(dev
->net
, mii
->phy_id
, MII_LPA
);
585 netif_dbg(dev
, link
, dev
->net
,
586 "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
587 ethtool_cmd_speed(&ecmd
), ecmd
.duplex
, lcladv
, rmtadv
);
589 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
590 if (ecmd
.duplex
!= DUPLEX_FULL
) {
591 pdata
->mac_cr
&= ~MAC_CR_FDPX_
;
592 pdata
->mac_cr
|= MAC_CR_RCVOWN_
;
594 pdata
->mac_cr
&= ~MAC_CR_RCVOWN_
;
595 pdata
->mac_cr
|= MAC_CR_FDPX_
;
597 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
599 ret
= smsc95xx_write_reg(dev
, MAC_CR
, pdata
->mac_cr
);
603 ret
= smsc95xx_phy_update_flowcontrol(dev
, ecmd
.duplex
, lcladv
, rmtadv
);
605 netdev_warn(dev
->net
, "Error updating PHY flow control\n");
610 static void smsc95xx_status(struct usbnet
*dev
, struct urb
*urb
)
614 if (urb
->actual_length
!= 4) {
615 netdev_warn(dev
->net
, "unexpected urb length %d\n",
620 memcpy(&intdata
, urb
->transfer_buffer
, 4);
621 le32_to_cpus(&intdata
);
623 netif_dbg(dev
, link
, dev
->net
, "intdata: 0x%08X\n", intdata
);
625 if (intdata
& INT_ENP_PHY_INT_
)
626 usbnet_defer_kevent(dev
, EVENT_LINK_RESET
);
628 netdev_warn(dev
->net
, "unexpected interrupt, intdata=0x%08X\n",
632 static void set_carrier(struct usbnet
*dev
, bool link
)
634 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
636 if (pdata
->link_ok
== link
)
639 pdata
->link_ok
= link
;
642 usbnet_link_change(dev
, 1, 0);
644 usbnet_link_change(dev
, 0, 0);
647 static void check_carrier(struct work_struct
*work
)
649 struct smsc95xx_priv
*pdata
= container_of(work
, struct smsc95xx_priv
,
651 struct usbnet
*dev
= pdata
->dev
;
654 if (pdata
->suspend_flags
!= 0)
657 ret
= smsc95xx_mdio_read(dev
->net
, dev
->mii
.phy_id
, MII_BMSR
);
659 netdev_warn(dev
->net
, "Failed to read MII_BMSR\n");
662 if (ret
& BMSR_LSTATUS
)
667 schedule_delayed_work(&pdata
->carrier_check
, CARRIER_CHECK_DELAY
);
670 /* Enable or disable Tx & Rx checksum offload engines */
671 static int smsc95xx_set_features(struct net_device
*netdev
,
672 netdev_features_t features
)
674 struct usbnet
*dev
= netdev_priv(netdev
);
678 ret
= smsc95xx_read_reg(dev
, COE_CR
, &read_buf
);
682 if (features
& NETIF_F_HW_CSUM
)
683 read_buf
|= Tx_COE_EN_
;
685 read_buf
&= ~Tx_COE_EN_
;
687 if (features
& NETIF_F_RXCSUM
)
688 read_buf
|= Rx_COE_EN_
;
690 read_buf
&= ~Rx_COE_EN_
;
692 ret
= smsc95xx_write_reg(dev
, COE_CR
, read_buf
);
696 netif_dbg(dev
, hw
, dev
->net
, "COE_CR = 0x%08x\n", read_buf
);
700 static int smsc95xx_ethtool_get_eeprom_len(struct net_device
*net
)
702 return MAX_EEPROM_SIZE
;
705 static int smsc95xx_ethtool_get_eeprom(struct net_device
*netdev
,
706 struct ethtool_eeprom
*ee
, u8
*data
)
708 struct usbnet
*dev
= netdev_priv(netdev
);
710 ee
->magic
= LAN95XX_EEPROM_MAGIC
;
712 return smsc95xx_read_eeprom(dev
, ee
->offset
, ee
->len
, data
);
715 static int smsc95xx_ethtool_set_eeprom(struct net_device
*netdev
,
716 struct ethtool_eeprom
*ee
, u8
*data
)
718 struct usbnet
*dev
= netdev_priv(netdev
);
720 if (ee
->magic
!= LAN95XX_EEPROM_MAGIC
) {
721 netdev_warn(dev
->net
, "EEPROM: magic value mismatch, magic = 0x%x\n",
726 return smsc95xx_write_eeprom(dev
, ee
->offset
, ee
->len
, data
);
729 static int smsc95xx_ethtool_getregslen(struct net_device
*netdev
)
731 /* all smsc95xx registers */
732 return COE_CR
- ID_REV
+ sizeof(u32
);
736 smsc95xx_ethtool_getregs(struct net_device
*netdev
, struct ethtool_regs
*regs
,
739 struct usbnet
*dev
= netdev_priv(netdev
);
744 retval
= smsc95xx_read_reg(dev
, ID_REV
, ®s
->version
);
746 netdev_warn(netdev
, "REGS: cannot read ID_REV\n");
750 for (i
= ID_REV
, j
= 0; i
<= COE_CR
; i
+= (sizeof(u32
)), j
++) {
751 retval
= smsc95xx_read_reg(dev
, i
, &data
[j
]);
753 netdev_warn(netdev
, "REGS: cannot read reg[%x]\n", i
);
759 static void smsc95xx_ethtool_get_wol(struct net_device
*net
,
760 struct ethtool_wolinfo
*wolinfo
)
762 struct usbnet
*dev
= netdev_priv(net
);
763 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
765 wolinfo
->supported
= SUPPORTED_WAKE
;
766 wolinfo
->wolopts
= pdata
->wolopts
;
769 static int smsc95xx_ethtool_set_wol(struct net_device
*net
,
770 struct ethtool_wolinfo
*wolinfo
)
772 struct usbnet
*dev
= netdev_priv(net
);
773 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
776 pdata
->wolopts
= wolinfo
->wolopts
& SUPPORTED_WAKE
;
778 ret
= device_set_wakeup_enable(&dev
->udev
->dev
, pdata
->wolopts
);
780 netdev_warn(dev
->net
, "device_set_wakeup_enable error %d\n", ret
);
785 static const struct ethtool_ops smsc95xx_ethtool_ops
= {
786 .get_link
= usbnet_get_link
,
787 .nway_reset
= usbnet_nway_reset
,
788 .get_drvinfo
= usbnet_get_drvinfo
,
789 .get_msglevel
= usbnet_get_msglevel
,
790 .set_msglevel
= usbnet_set_msglevel
,
791 .get_settings
= usbnet_get_settings
,
792 .set_settings
= usbnet_set_settings
,
793 .get_eeprom_len
= smsc95xx_ethtool_get_eeprom_len
,
794 .get_eeprom
= smsc95xx_ethtool_get_eeprom
,
795 .set_eeprom
= smsc95xx_ethtool_set_eeprom
,
796 .get_regs_len
= smsc95xx_ethtool_getregslen
,
797 .get_regs
= smsc95xx_ethtool_getregs
,
798 .get_wol
= smsc95xx_ethtool_get_wol
,
799 .set_wol
= smsc95xx_ethtool_set_wol
,
802 static int smsc95xx_ioctl(struct net_device
*netdev
, struct ifreq
*rq
, int cmd
)
804 struct usbnet
*dev
= netdev_priv(netdev
);
806 if (!netif_running(netdev
))
809 return generic_mii_ioctl(&dev
->mii
, if_mii(rq
), cmd
, NULL
);
812 static void smsc95xx_init_mac_address(struct usbnet
*dev
)
816 /* maybe the boot loader passed the MAC address in devicetree */
817 mac_addr
= of_get_mac_address(dev
->udev
->dev
.of_node
);
819 memcpy(dev
->net
->dev_addr
, mac_addr
, ETH_ALEN
);
823 /* try reading mac address from EEPROM */
824 if (smsc95xx_read_eeprom(dev
, EEPROM_MAC_OFFSET
, ETH_ALEN
,
825 dev
->net
->dev_addr
) == 0) {
826 if (is_valid_ether_addr(dev
->net
->dev_addr
)) {
827 /* eeprom values are valid so use them */
828 netif_dbg(dev
, ifup
, dev
->net
, "MAC address read from EEPROM\n");
833 /* no useful static MAC address found. generate a random one */
834 eth_hw_addr_random(dev
->net
);
835 netif_dbg(dev
, ifup
, dev
->net
, "MAC address set to eth_random_addr\n");
838 static int smsc95xx_set_mac_address(struct usbnet
*dev
)
840 u32 addr_lo
= dev
->net
->dev_addr
[0] | dev
->net
->dev_addr
[1] << 8 |
841 dev
->net
->dev_addr
[2] << 16 | dev
->net
->dev_addr
[3] << 24;
842 u32 addr_hi
= dev
->net
->dev_addr
[4] | dev
->net
->dev_addr
[5] << 8;
845 ret
= smsc95xx_write_reg(dev
, ADDRL
, addr_lo
);
849 return smsc95xx_write_reg(dev
, ADDRH
, addr_hi
);
852 /* starts the TX path */
853 static int smsc95xx_start_tx_path(struct usbnet
*dev
)
855 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
859 /* Enable Tx at MAC */
860 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
861 pdata
->mac_cr
|= MAC_CR_TXEN_
;
862 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
864 ret
= smsc95xx_write_reg(dev
, MAC_CR
, pdata
->mac_cr
);
868 /* Enable Tx at SCSRs */
869 return smsc95xx_write_reg(dev
, TX_CFG
, TX_CFG_ON_
);
872 /* Starts the Receive path */
873 static int smsc95xx_start_rx_path(struct usbnet
*dev
, int in_pm
)
875 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
878 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
879 pdata
->mac_cr
|= MAC_CR_RXEN_
;
880 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
882 return __smsc95xx_write_reg(dev
, MAC_CR
, pdata
->mac_cr
, in_pm
);
885 static int smsc95xx_phy_initialize(struct usbnet
*dev
)
887 int bmcr
, ret
, timeout
= 0;
889 /* Initialize MII structure */
890 dev
->mii
.dev
= dev
->net
;
891 dev
->mii
.mdio_read
= smsc95xx_mdio_read
;
892 dev
->mii
.mdio_write
= smsc95xx_mdio_write
;
893 dev
->mii
.phy_id_mask
= 0x1f;
894 dev
->mii
.reg_num_mask
= 0x1f;
895 dev
->mii
.phy_id
= SMSC95XX_INTERNAL_PHY_ID
;
897 /* reset phy and wait for reset to complete */
898 smsc95xx_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_BMCR
, BMCR_RESET
);
902 bmcr
= smsc95xx_mdio_read(dev
->net
, dev
->mii
.phy_id
, MII_BMCR
);
904 } while ((bmcr
& BMCR_RESET
) && (timeout
< 100));
906 if (timeout
>= 100) {
907 netdev_warn(dev
->net
, "timeout on PHY Reset");
911 smsc95xx_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_ADVERTISE
,
912 ADVERTISE_ALL
| ADVERTISE_CSMA
| ADVERTISE_PAUSE_CAP
|
913 ADVERTISE_PAUSE_ASYM
);
916 ret
= smsc95xx_mdio_read(dev
->net
, dev
->mii
.phy_id
, PHY_INT_SRC
);
918 netdev_warn(dev
->net
, "Failed to read PHY_INT_SRC during init\n");
922 smsc95xx_mdio_write(dev
->net
, dev
->mii
.phy_id
, PHY_INT_MASK
,
923 PHY_INT_MASK_DEFAULT_
);
924 mii_nway_restart(&dev
->mii
);
926 netif_dbg(dev
, ifup
, dev
->net
, "phy initialised successfully\n");
930 static int smsc95xx_reset(struct usbnet
*dev
)
932 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
933 u32 read_buf
, write_buf
, burst_cap
;
934 int ret
= 0, timeout
;
936 netif_dbg(dev
, ifup
, dev
->net
, "entering smsc95xx_reset\n");
938 ret
= smsc95xx_write_reg(dev
, HW_CFG
, HW_CFG_LRST_
);
945 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
949 } while ((read_buf
& HW_CFG_LRST_
) && (timeout
< 100));
951 if (timeout
>= 100) {
952 netdev_warn(dev
->net
, "timeout waiting for completion of Lite Reset\n");
956 ret
= smsc95xx_write_reg(dev
, PM_CTRL
, PM_CTL_PHY_RST_
);
963 ret
= smsc95xx_read_reg(dev
, PM_CTRL
, &read_buf
);
967 } while ((read_buf
& PM_CTL_PHY_RST_
) && (timeout
< 100));
969 if (timeout
>= 100) {
970 netdev_warn(dev
->net
, "timeout waiting for PHY Reset\n");
974 ret
= smsc95xx_set_mac_address(dev
);
978 netif_dbg(dev
, ifup
, dev
->net
, "MAC Address: %pM\n",
981 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
985 netif_dbg(dev
, ifup
, dev
->net
, "Read Value from HW_CFG : 0x%08x\n",
988 read_buf
|= HW_CFG_BIR_
;
990 ret
= smsc95xx_write_reg(dev
, HW_CFG
, read_buf
);
994 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
998 netif_dbg(dev
, ifup
, dev
->net
,
999 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
1004 dev
->rx_urb_size
= MAX_SINGLE_PACKET_SIZE
;
1005 } else if (dev
->udev
->speed
== USB_SPEED_HIGH
) {
1006 burst_cap
= DEFAULT_HS_BURST_CAP_SIZE
/ HS_USB_PKT_SIZE
;
1007 dev
->rx_urb_size
= DEFAULT_HS_BURST_CAP_SIZE
;
1009 burst_cap
= DEFAULT_FS_BURST_CAP_SIZE
/ FS_USB_PKT_SIZE
;
1010 dev
->rx_urb_size
= DEFAULT_FS_BURST_CAP_SIZE
;
1013 netif_dbg(dev
, ifup
, dev
->net
, "rx_urb_size=%ld\n",
1014 (ulong
)dev
->rx_urb_size
);
1016 ret
= smsc95xx_write_reg(dev
, BURST_CAP
, burst_cap
);
1020 ret
= smsc95xx_read_reg(dev
, BURST_CAP
, &read_buf
);
1024 netif_dbg(dev
, ifup
, dev
->net
,
1025 "Read Value from BURST_CAP after writing: 0x%08x\n",
1028 ret
= smsc95xx_write_reg(dev
, BULK_IN_DLY
, DEFAULT_BULK_IN_DELAY
);
1032 ret
= smsc95xx_read_reg(dev
, BULK_IN_DLY
, &read_buf
);
1036 netif_dbg(dev
, ifup
, dev
->net
,
1037 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
1040 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
1044 netif_dbg(dev
, ifup
, dev
->net
, "Read Value from HW_CFG: 0x%08x\n",
1048 read_buf
|= (HW_CFG_MEF_
| HW_CFG_BCE_
);
1050 read_buf
&= ~HW_CFG_RXDOFF_
;
1052 /* set Rx data offset=2, Make IP header aligns on word boundary. */
1053 read_buf
|= NET_IP_ALIGN
<< 9;
1055 ret
= smsc95xx_write_reg(dev
, HW_CFG
, read_buf
);
1059 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
1063 netif_dbg(dev
, ifup
, dev
->net
,
1064 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf
);
1066 ret
= smsc95xx_write_reg(dev
, INT_STS
, INT_STS_CLEAR_ALL_
);
1070 ret
= smsc95xx_read_reg(dev
, ID_REV
, &read_buf
);
1073 netif_dbg(dev
, ifup
, dev
->net
, "ID_REV = 0x%08x\n", read_buf
);
1075 /* Configure GPIO pins as LED outputs */
1076 write_buf
= LED_GPIO_CFG_SPD_LED
| LED_GPIO_CFG_LNK_LED
|
1077 LED_GPIO_CFG_FDX_LED
;
1078 ret
= smsc95xx_write_reg(dev
, LED_GPIO_CFG
, write_buf
);
1083 ret
= smsc95xx_write_reg(dev
, FLOW
, 0);
1087 ret
= smsc95xx_write_reg(dev
, AFC_CFG
, AFC_CFG_DEFAULT
);
1091 /* Don't need mac_cr_lock during initialisation */
1092 ret
= smsc95xx_read_reg(dev
, MAC_CR
, &pdata
->mac_cr
);
1098 ret
= smsc95xx_write_reg(dev
, VLAN1
, (u32
)ETH_P_8021Q
);
1102 /* Enable or disable checksum offload engines */
1103 ret
= smsc95xx_set_features(dev
->net
, dev
->net
->features
);
1105 netdev_warn(dev
->net
, "Failed to set checksum offload features\n");
1109 smsc95xx_set_multicast(dev
->net
);
1111 ret
= smsc95xx_phy_initialize(dev
);
1113 netdev_warn(dev
->net
, "Failed to init PHY\n");
1117 ret
= smsc95xx_read_reg(dev
, INT_EP_CTL
, &read_buf
);
1121 /* enable PHY interrupts */
1122 read_buf
|= INT_EP_CTL_PHY_INT_
;
1124 ret
= smsc95xx_write_reg(dev
, INT_EP_CTL
, read_buf
);
1128 ret
= smsc95xx_start_tx_path(dev
);
1130 netdev_warn(dev
->net
, "Failed to start TX path\n");
1134 ret
= smsc95xx_start_rx_path(dev
, 0);
1136 netdev_warn(dev
->net
, "Failed to start RX path\n");
1140 netif_dbg(dev
, ifup
, dev
->net
, "smsc95xx_reset, return 0\n");
1144 static const struct net_device_ops smsc95xx_netdev_ops
= {
1145 .ndo_open
= usbnet_open
,
1146 .ndo_stop
= usbnet_stop
,
1147 .ndo_start_xmit
= usbnet_start_xmit
,
1148 .ndo_tx_timeout
= usbnet_tx_timeout
,
1149 .ndo_change_mtu
= usbnet_change_mtu
,
1150 .ndo_set_mac_address
= eth_mac_addr
,
1151 .ndo_validate_addr
= eth_validate_addr
,
1152 .ndo_do_ioctl
= smsc95xx_ioctl
,
1153 .ndo_set_rx_mode
= smsc95xx_set_multicast
,
1154 .ndo_set_features
= smsc95xx_set_features
,
1157 static int smsc95xx_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
1159 struct smsc95xx_priv
*pdata
= NULL
;
1163 printk(KERN_INFO SMSC_CHIPNAME
" v" SMSC_DRIVER_VERSION
"\n");
1165 ret
= usbnet_get_endpoints(dev
, intf
);
1167 netdev_warn(dev
->net
, "usbnet_get_endpoints failed: %d\n", ret
);
1171 dev
->data
[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv
),
1174 pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1178 spin_lock_init(&pdata
->mac_cr_lock
);
1180 if (DEFAULT_TX_CSUM_ENABLE
)
1181 dev
->net
->features
|= NETIF_F_HW_CSUM
;
1182 if (DEFAULT_RX_CSUM_ENABLE
)
1183 dev
->net
->features
|= NETIF_F_RXCSUM
;
1185 dev
->net
->hw_features
= NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1187 smsc95xx_init_mac_address(dev
);
1189 /* Init all registers */
1190 ret
= smsc95xx_reset(dev
);
1192 /* detect device revision as different features may be available */
1193 ret
= smsc95xx_read_reg(dev
, ID_REV
, &val
);
1198 if ((val
== ID_REV_CHIP_ID_9500A_
) || (val
== ID_REV_CHIP_ID_9530_
) ||
1199 (val
== ID_REV_CHIP_ID_89530_
) || (val
== ID_REV_CHIP_ID_9730_
))
1200 pdata
->features
= (FEATURE_8_WAKEUP_FILTERS
|
1201 FEATURE_PHY_NLP_CROSSOVER
|
1202 FEATURE_REMOTE_WAKEUP
);
1203 else if (val
== ID_REV_CHIP_ID_9512_
)
1204 pdata
->features
= FEATURE_8_WAKEUP_FILTERS
;
1206 dev
->net
->netdev_ops
= &smsc95xx_netdev_ops
;
1207 dev
->net
->ethtool_ops
= &smsc95xx_ethtool_ops
;
1208 dev
->net
->flags
|= IFF_MULTICAST
;
1209 dev
->net
->hard_header_len
+= SMSC95XX_TX_OVERHEAD_CSUM
;
1210 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
1213 INIT_DELAYED_WORK(&pdata
->carrier_check
, check_carrier
);
1214 schedule_delayed_work(&pdata
->carrier_check
, CARRIER_CHECK_DELAY
);
1219 static void smsc95xx_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
1221 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1224 cancel_delayed_work(&pdata
->carrier_check
);
1225 netif_dbg(dev
, ifdown
, dev
->net
, "free pdata\n");
1232 static u32
smsc_crc(const u8
*buffer
, size_t len
, int filter
)
1234 u32 crc
= bitrev16(crc16(0xFFFF, buffer
, len
));
1235 return crc
<< ((filter
% 2) * 16);
1238 static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet
*dev
, u16 mask
)
1240 struct mii_if_info
*mii
= &dev
->mii
;
1243 netdev_dbg(dev
->net
, "enabling PHY wakeup interrupts\n");
1246 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, PHY_INT_SRC
);
1250 /* enable interrupt source */
1251 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, PHY_INT_MASK
);
1257 smsc95xx_mdio_write_nopm(dev
->net
, mii
->phy_id
, PHY_INT_MASK
, ret
);
1262 static int smsc95xx_link_ok_nopm(struct usbnet
*dev
)
1264 struct mii_if_info
*mii
= &dev
->mii
;
1267 /* first, a dummy read, needed to latch some MII phys */
1268 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, MII_BMSR
);
1272 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, MII_BMSR
);
1276 return !!(ret
& BMSR_LSTATUS
);
1279 static int smsc95xx_enter_suspend0(struct usbnet
*dev
)
1281 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1285 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1289 val
&= (~(PM_CTL_SUS_MODE_
| PM_CTL_WUPS_
| PM_CTL_PHY_RST_
));
1290 val
|= PM_CTL_SUS_MODE_0
;
1292 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1296 /* clear wol status */
1297 val
&= ~PM_CTL_WUPS_
;
1298 val
|= PM_CTL_WUPS_WOL_
;
1300 /* enable energy detection */
1301 if (pdata
->wolopts
& WAKE_PHY
)
1302 val
|= PM_CTL_WUPS_ED_
;
1304 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1308 /* read back PM_CTRL */
1309 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1313 pdata
->suspend_flags
|= SUSPEND_SUSPEND0
;
1318 static int smsc95xx_enter_suspend1(struct usbnet
*dev
)
1320 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1321 struct mii_if_info
*mii
= &dev
->mii
;
1325 /* reconfigure link pulse detection timing for
1326 * compatibility with non-standard link partners
1328 if (pdata
->features
& FEATURE_PHY_NLP_CROSSOVER
)
1329 smsc95xx_mdio_write_nopm(dev
->net
, mii
->phy_id
, PHY_EDPD_CONFIG
,
1330 PHY_EDPD_CONFIG_DEFAULT
);
1332 /* enable energy detect power-down mode */
1333 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, PHY_MODE_CTRL_STS
);
1337 ret
|= MODE_CTRL_STS_EDPWRDOWN_
;
1339 smsc95xx_mdio_write_nopm(dev
->net
, mii
->phy_id
, PHY_MODE_CTRL_STS
, ret
);
1341 /* enter SUSPEND1 mode */
1342 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1346 val
&= ~(PM_CTL_SUS_MODE_
| PM_CTL_WUPS_
| PM_CTL_PHY_RST_
);
1347 val
|= PM_CTL_SUS_MODE_1
;
1349 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1353 /* clear wol status, enable energy detection */
1354 val
&= ~PM_CTL_WUPS_
;
1355 val
|= (PM_CTL_WUPS_ED_
| PM_CTL_ED_EN_
);
1357 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1361 pdata
->suspend_flags
|= SUSPEND_SUSPEND1
;
1366 static int smsc95xx_enter_suspend2(struct usbnet
*dev
)
1368 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1372 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1376 val
&= ~(PM_CTL_SUS_MODE_
| PM_CTL_WUPS_
| PM_CTL_PHY_RST_
);
1377 val
|= PM_CTL_SUS_MODE_2
;
1379 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1383 pdata
->suspend_flags
|= SUSPEND_SUSPEND2
;
1388 static int smsc95xx_enter_suspend3(struct usbnet
*dev
)
1390 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1394 ret
= smsc95xx_read_reg_nopm(dev
, RX_FIFO_INF
, &val
);
1399 netdev_info(dev
->net
, "rx fifo not empty in autosuspend\n");
1403 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1407 val
&= ~(PM_CTL_SUS_MODE_
| PM_CTL_WUPS_
| PM_CTL_PHY_RST_
);
1408 val
|= PM_CTL_SUS_MODE_3
| PM_CTL_RES_CLR_WKP_STS
;
1410 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1414 /* clear wol status */
1415 val
&= ~PM_CTL_WUPS_
;
1416 val
|= PM_CTL_WUPS_WOL_
;
1418 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1422 pdata
->suspend_flags
|= SUSPEND_SUSPEND3
;
1427 static int smsc95xx_autosuspend(struct usbnet
*dev
, u32 link_up
)
1429 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1432 if (!netif_running(dev
->net
)) {
1433 /* interface is ifconfig down so fully power down hw */
1434 netdev_dbg(dev
->net
, "autosuspend entering SUSPEND2\n");
1435 return smsc95xx_enter_suspend2(dev
);
1439 /* link is down so enter EDPD mode, but only if device can
1440 * reliably resume from it. This check should be redundant
1441 * as current FEATURE_REMOTE_WAKEUP parts also support
1442 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1443 if (!(pdata
->features
& FEATURE_PHY_NLP_CROSSOVER
)) {
1444 netdev_warn(dev
->net
, "EDPD not supported\n");
1448 netdev_dbg(dev
->net
, "autosuspend entering SUSPEND1\n");
1450 /* enable PHY wakeup events for if cable is attached */
1451 ret
= smsc95xx_enable_phy_wakeup_interrupts(dev
,
1452 PHY_INT_MASK_ANEG_COMP_
);
1454 netdev_warn(dev
->net
, "error enabling PHY wakeup ints\n");
1458 netdev_info(dev
->net
, "entering SUSPEND1 mode\n");
1459 return smsc95xx_enter_suspend1(dev
);
1462 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1463 ret
= smsc95xx_enable_phy_wakeup_interrupts(dev
,
1464 PHY_INT_MASK_LINK_DOWN_
);
1466 netdev_warn(dev
->net
, "error enabling PHY wakeup ints\n");
1470 netdev_dbg(dev
->net
, "autosuspend entering SUSPEND3\n");
1471 return smsc95xx_enter_suspend3(dev
);
1474 static int smsc95xx_suspend(struct usb_interface
*intf
, pm_message_t message
)
1476 struct usbnet
*dev
= usb_get_intfdata(intf
);
1477 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1481 ret
= usbnet_suspend(intf
, message
);
1483 netdev_warn(dev
->net
, "usbnet_suspend error\n");
1487 if (pdata
->suspend_flags
) {
1488 netdev_warn(dev
->net
, "error during last resume\n");
1489 pdata
->suspend_flags
= 0;
1492 /* determine if link is up using only _nopm functions */
1493 link_up
= smsc95xx_link_ok_nopm(dev
);
1495 if (message
.event
== PM_EVENT_AUTO_SUSPEND
&&
1496 (pdata
->features
& FEATURE_REMOTE_WAKEUP
)) {
1497 ret
= smsc95xx_autosuspend(dev
, link_up
);
1501 /* if we get this far we're not autosuspending */
1502 /* if no wol options set, or if link is down and we're not waking on
1503 * PHY activity, enter lowest power SUSPEND2 mode
1505 if (!(pdata
->wolopts
& SUPPORTED_WAKE
) ||
1506 !(link_up
|| (pdata
->wolopts
& WAKE_PHY
))) {
1507 netdev_info(dev
->net
, "entering SUSPEND2 mode\n");
1509 /* disable energy detect (link up) & wake up events */
1510 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1514 val
&= ~(WUCSR_MPEN_
| WUCSR_WAKE_EN_
);
1516 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1520 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1524 val
&= ~(PM_CTL_ED_EN_
| PM_CTL_WOL_EN_
);
1526 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1530 ret
= smsc95xx_enter_suspend2(dev
);
1534 if (pdata
->wolopts
& WAKE_PHY
) {
1535 ret
= smsc95xx_enable_phy_wakeup_interrupts(dev
,
1536 (PHY_INT_MASK_ANEG_COMP_
| PHY_INT_MASK_LINK_DOWN_
));
1538 netdev_warn(dev
->net
, "error enabling PHY wakeup ints\n");
1542 /* if link is down then configure EDPD and enter SUSPEND1,
1543 * otherwise enter SUSPEND0 below
1546 netdev_info(dev
->net
, "entering SUSPEND1 mode\n");
1547 ret
= smsc95xx_enter_suspend1(dev
);
1552 if (pdata
->wolopts
& (WAKE_BCAST
| WAKE_MCAST
| WAKE_ARP
| WAKE_UCAST
)) {
1553 u32
*filter_mask
= kzalloc(sizeof(u32
) * 32, GFP_KERNEL
);
1557 int wuff_filter_count
=
1558 (pdata
->features
& FEATURE_8_WAKEUP_FILTERS
) ?
1559 LAN9500A_WUFF_NUM
: LAN9500_WUFF_NUM
;
1563 netdev_warn(dev
->net
, "Unable to allocate filter_mask\n");
1568 memset(command
, 0, sizeof(command
));
1569 memset(offset
, 0, sizeof(offset
));
1570 memset(crc
, 0, sizeof(crc
));
1572 if (pdata
->wolopts
& WAKE_BCAST
) {
1573 const u8 bcast
[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1574 netdev_info(dev
->net
, "enabling broadcast detection\n");
1575 filter_mask
[filter
* 4] = 0x003F;
1576 filter_mask
[filter
* 4 + 1] = 0x00;
1577 filter_mask
[filter
* 4 + 2] = 0x00;
1578 filter_mask
[filter
* 4 + 3] = 0x00;
1579 command
[filter
/4] |= 0x05UL
<< ((filter
% 4) * 8);
1580 offset
[filter
/4] |= 0x00 << ((filter
% 4) * 8);
1581 crc
[filter
/2] |= smsc_crc(bcast
, 6, filter
);
1585 if (pdata
->wolopts
& WAKE_MCAST
) {
1586 const u8 mcast
[] = {0x01, 0x00, 0x5E};
1587 netdev_info(dev
->net
, "enabling multicast detection\n");
1588 filter_mask
[filter
* 4] = 0x0007;
1589 filter_mask
[filter
* 4 + 1] = 0x00;
1590 filter_mask
[filter
* 4 + 2] = 0x00;
1591 filter_mask
[filter
* 4 + 3] = 0x00;
1592 command
[filter
/4] |= 0x09UL
<< ((filter
% 4) * 8);
1593 offset
[filter
/4] |= 0x00 << ((filter
% 4) * 8);
1594 crc
[filter
/2] |= smsc_crc(mcast
, 3, filter
);
1598 if (pdata
->wolopts
& WAKE_ARP
) {
1599 const u8 arp
[] = {0x08, 0x06};
1600 netdev_info(dev
->net
, "enabling ARP detection\n");
1601 filter_mask
[filter
* 4] = 0x0003;
1602 filter_mask
[filter
* 4 + 1] = 0x00;
1603 filter_mask
[filter
* 4 + 2] = 0x00;
1604 filter_mask
[filter
* 4 + 3] = 0x00;
1605 command
[filter
/4] |= 0x05UL
<< ((filter
% 4) * 8);
1606 offset
[filter
/4] |= 0x0C << ((filter
% 4) * 8);
1607 crc
[filter
/2] |= smsc_crc(arp
, 2, filter
);
1611 if (pdata
->wolopts
& WAKE_UCAST
) {
1612 netdev_info(dev
->net
, "enabling unicast detection\n");
1613 filter_mask
[filter
* 4] = 0x003F;
1614 filter_mask
[filter
* 4 + 1] = 0x00;
1615 filter_mask
[filter
* 4 + 2] = 0x00;
1616 filter_mask
[filter
* 4 + 3] = 0x00;
1617 command
[filter
/4] |= 0x01UL
<< ((filter
% 4) * 8);
1618 offset
[filter
/4] |= 0x00 << ((filter
% 4) * 8);
1619 crc
[filter
/2] |= smsc_crc(dev
->net
->dev_addr
, ETH_ALEN
, filter
);
1623 for (i
= 0; i
< (wuff_filter_count
* 4); i
++) {
1624 ret
= smsc95xx_write_reg_nopm(dev
, WUFF
, filter_mask
[i
]);
1632 for (i
= 0; i
< (wuff_filter_count
/ 4); i
++) {
1633 ret
= smsc95xx_write_reg_nopm(dev
, WUFF
, command
[i
]);
1638 for (i
= 0; i
< (wuff_filter_count
/ 4); i
++) {
1639 ret
= smsc95xx_write_reg_nopm(dev
, WUFF
, offset
[i
]);
1644 for (i
= 0; i
< (wuff_filter_count
/ 2); i
++) {
1645 ret
= smsc95xx_write_reg_nopm(dev
, WUFF
, crc
[i
]);
1650 /* clear any pending pattern match packet status */
1651 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1657 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1662 if (pdata
->wolopts
& WAKE_MAGIC
) {
1663 /* clear any pending magic packet status */
1664 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1670 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1675 /* enable/disable wakeup sources */
1676 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1680 if (pdata
->wolopts
& (WAKE_BCAST
| WAKE_MCAST
| WAKE_ARP
| WAKE_UCAST
)) {
1681 netdev_info(dev
->net
, "enabling pattern match wakeup\n");
1682 val
|= WUCSR_WAKE_EN_
;
1684 netdev_info(dev
->net
, "disabling pattern match wakeup\n");
1685 val
&= ~WUCSR_WAKE_EN_
;
1688 if (pdata
->wolopts
& WAKE_MAGIC
) {
1689 netdev_info(dev
->net
, "enabling magic packet wakeup\n");
1692 netdev_info(dev
->net
, "disabling magic packet wakeup\n");
1693 val
&= ~WUCSR_MPEN_
;
1696 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1700 /* enable wol wakeup source */
1701 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1705 val
|= PM_CTL_WOL_EN_
;
1707 /* phy energy detect wakeup source */
1708 if (pdata
->wolopts
& WAKE_PHY
)
1709 val
|= PM_CTL_ED_EN_
;
1711 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1715 /* enable receiver to enable frame reception */
1716 smsc95xx_start_rx_path(dev
, 1);
1718 /* some wol options are enabled, so enter SUSPEND0 */
1719 netdev_info(dev
->net
, "entering SUSPEND0 mode\n");
1720 ret
= smsc95xx_enter_suspend0(dev
);
1724 * TODO: resume() might need to handle the suspend failure
1727 if (ret
&& PMSG_IS_AUTO(message
))
1728 usbnet_resume(intf
);
1732 static int smsc95xx_resume(struct usb_interface
*intf
)
1734 struct usbnet
*dev
= usb_get_intfdata(intf
);
1735 struct smsc95xx_priv
*pdata
;
1741 pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1742 suspend_flags
= pdata
->suspend_flags
;
1744 netdev_dbg(dev
->net
, "resume suspend_flags=0x%02x\n", suspend_flags
);
1746 /* do this first to ensure it's cleared even in error case */
1747 pdata
->suspend_flags
= 0;
1748 schedule_delayed_work(&pdata
->carrier_check
, CARRIER_CHECK_DELAY
);
1750 if (suspend_flags
& SUSPEND_ALLMODES
) {
1751 /* clear wake-up sources */
1752 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1756 val
&= ~(WUCSR_WAKE_EN_
| WUCSR_MPEN_
);
1758 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1762 /* clear wake-up status */
1763 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1767 val
&= ~PM_CTL_WOL_EN_
;
1768 val
|= PM_CTL_WUPS_
;
1770 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1775 ret
= usbnet_resume(intf
);
1777 netdev_warn(dev
->net
, "usbnet_resume error\n");
1782 static int smsc95xx_reset_resume(struct usb_interface
*intf
)
1784 struct usbnet
*dev
= usb_get_intfdata(intf
);
1787 ret
= smsc95xx_reset(dev
);
1791 return smsc95xx_resume(intf
);
1794 static void smsc95xx_rx_csum_offload(struct sk_buff
*skb
)
1796 skb
->csum
= *(u16
*)(skb_tail_pointer(skb
) - 2);
1797 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1798 skb_trim(skb
, skb
->len
- 2);
1801 static int smsc95xx_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
1803 /* This check is no longer done by usbnet */
1804 if (skb
->len
< dev
->net
->hard_header_len
)
1807 while (skb
->len
> 0) {
1808 u32 header
, align_count
;
1809 struct sk_buff
*ax_skb
;
1810 unsigned char *packet
;
1813 memcpy(&header
, skb
->data
, sizeof(header
));
1814 le32_to_cpus(&header
);
1815 skb_pull(skb
, 4 + NET_IP_ALIGN
);
1818 /* get the packet length */
1819 size
= (u16
)((header
& RX_STS_FL_
) >> 16);
1820 align_count
= (4 - ((size
+ NET_IP_ALIGN
) % 4)) % 4;
1822 if (unlikely(header
& RX_STS_ES_
)) {
1823 netif_dbg(dev
, rx_err
, dev
->net
,
1824 "Error header=0x%08x\n", header
);
1825 dev
->net
->stats
.rx_errors
++;
1826 dev
->net
->stats
.rx_dropped
++;
1828 if (header
& RX_STS_CRC_
) {
1829 dev
->net
->stats
.rx_crc_errors
++;
1831 if (header
& (RX_STS_TL_
| RX_STS_RF_
))
1832 dev
->net
->stats
.rx_frame_errors
++;
1834 if ((header
& RX_STS_LE_
) &&
1835 (!(header
& RX_STS_FT_
)))
1836 dev
->net
->stats
.rx_length_errors
++;
1839 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1840 if (unlikely(size
> (ETH_FRAME_LEN
+ 12))) {
1841 netif_dbg(dev
, rx_err
, dev
->net
,
1842 "size err header=0x%08x\n", header
);
1846 /* last frame in this batch */
1847 if (skb
->len
== size
) {
1848 if (dev
->net
->features
& NETIF_F_RXCSUM
)
1849 smsc95xx_rx_csum_offload(skb
);
1850 skb_trim(skb
, skb
->len
- 4); /* remove fcs */
1851 skb
->truesize
= size
+ sizeof(struct sk_buff
);
1856 ax_skb
= skb_clone(skb
, GFP_ATOMIC
);
1857 if (unlikely(!ax_skb
)) {
1858 netdev_warn(dev
->net
, "Error allocating skb\n");
1863 ax_skb
->data
= packet
;
1864 skb_set_tail_pointer(ax_skb
, size
);
1866 if (dev
->net
->features
& NETIF_F_RXCSUM
)
1867 smsc95xx_rx_csum_offload(ax_skb
);
1868 skb_trim(ax_skb
, ax_skb
->len
- 4); /* remove fcs */
1869 ax_skb
->truesize
= size
+ sizeof(struct sk_buff
);
1871 usbnet_skb_return(dev
, ax_skb
);
1874 skb_pull(skb
, size
);
1876 /* padding bytes before the next frame starts */
1878 skb_pull(skb
, align_count
);
1884 static u32
smsc95xx_calc_csum_preamble(struct sk_buff
*skb
)
1886 u16 low_16
= (u16
)skb_checksum_start_offset(skb
);
1887 u16 high_16
= low_16
+ skb
->csum_offset
;
1888 return (high_16
<< 16) | low_16
;
1891 static struct sk_buff
*smsc95xx_tx_fixup(struct usbnet
*dev
,
1892 struct sk_buff
*skb
, gfp_t flags
)
1894 bool csum
= skb
->ip_summed
== CHECKSUM_PARTIAL
;
1895 int overhead
= csum
? SMSC95XX_TX_OVERHEAD_CSUM
: SMSC95XX_TX_OVERHEAD
;
1896 u32 tx_cmd_a
, tx_cmd_b
;
1898 /* We do not advertise SG, so skbs should be already linearized */
1899 BUG_ON(skb_shinfo(skb
)->nr_frags
);
1901 if (skb_headroom(skb
) < overhead
) {
1902 struct sk_buff
*skb2
= skb_copy_expand(skb
,
1903 overhead
, 0, flags
);
1904 dev_kfree_skb_any(skb
);
1911 if (skb
->len
<= 45) {
1912 /* workaround - hardware tx checksum does not work
1913 * properly with extremely small packets */
1914 long csstart
= skb_checksum_start_offset(skb
);
1915 __wsum calc
= csum_partial(skb
->data
+ csstart
,
1916 skb
->len
- csstart
, 0);
1917 *((__sum16
*)(skb
->data
+ csstart
1918 + skb
->csum_offset
)) = csum_fold(calc
);
1922 u32 csum_preamble
= smsc95xx_calc_csum_preamble(skb
);
1924 cpu_to_le32s(&csum_preamble
);
1925 memcpy(skb
->data
, &csum_preamble
, 4);
1930 tx_cmd_b
= (u32
)(skb
->len
- 4);
1932 tx_cmd_b
|= TX_CMD_B_CSUM_ENABLE
;
1933 cpu_to_le32s(&tx_cmd_b
);
1934 memcpy(skb
->data
, &tx_cmd_b
, 4);
1937 tx_cmd_a
= (u32
)(skb
->len
- 8) | TX_CMD_A_FIRST_SEG_
|
1939 cpu_to_le32s(&tx_cmd_a
);
1940 memcpy(skb
->data
, &tx_cmd_a
, 4);
1945 static int smsc95xx_manage_power(struct usbnet
*dev
, int on
)
1947 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1949 dev
->intf
->needs_remote_wakeup
= on
;
1951 if (pdata
->features
& FEATURE_REMOTE_WAKEUP
)
1954 /* this chip revision isn't capable of remote wakeup */
1955 netdev_info(dev
->net
, "hardware isn't capable of remote wakeup\n");
1958 usb_autopm_get_interface_no_resume(dev
->intf
);
1960 usb_autopm_put_interface(dev
->intf
);
1965 static const struct driver_info smsc95xx_info
= {
1966 .description
= "smsc95xx USB 2.0 Ethernet",
1967 .bind
= smsc95xx_bind
,
1968 .unbind
= smsc95xx_unbind
,
1969 .link_reset
= smsc95xx_link_reset
,
1970 .reset
= smsc95xx_reset
,
1971 .rx_fixup
= smsc95xx_rx_fixup
,
1972 .tx_fixup
= smsc95xx_tx_fixup
,
1973 .status
= smsc95xx_status
,
1974 .manage_power
= smsc95xx_manage_power
,
1975 .flags
= FLAG_ETHER
| FLAG_SEND_ZLP
| FLAG_LINK_INTR
,
1978 static const struct usb_device_id products
[] = {
1980 /* SMSC9500 USB Ethernet Device */
1981 USB_DEVICE(0x0424, 0x9500),
1982 .driver_info
= (unsigned long) &smsc95xx_info
,
1985 /* SMSC9505 USB Ethernet Device */
1986 USB_DEVICE(0x0424, 0x9505),
1987 .driver_info
= (unsigned long) &smsc95xx_info
,
1990 /* SMSC9500A USB Ethernet Device */
1991 USB_DEVICE(0x0424, 0x9E00),
1992 .driver_info
= (unsigned long) &smsc95xx_info
,
1995 /* SMSC9505A USB Ethernet Device */
1996 USB_DEVICE(0x0424, 0x9E01),
1997 .driver_info
= (unsigned long) &smsc95xx_info
,
2000 /* SMSC9512/9514 USB Hub & Ethernet Device */
2001 USB_DEVICE(0x0424, 0xec00),
2002 .driver_info
= (unsigned long) &smsc95xx_info
,
2005 /* SMSC9500 USB Ethernet Device (SAL10) */
2006 USB_DEVICE(0x0424, 0x9900),
2007 .driver_info
= (unsigned long) &smsc95xx_info
,
2010 /* SMSC9505 USB Ethernet Device (SAL10) */
2011 USB_DEVICE(0x0424, 0x9901),
2012 .driver_info
= (unsigned long) &smsc95xx_info
,
2015 /* SMSC9500A USB Ethernet Device (SAL10) */
2016 USB_DEVICE(0x0424, 0x9902),
2017 .driver_info
= (unsigned long) &smsc95xx_info
,
2020 /* SMSC9505A USB Ethernet Device (SAL10) */
2021 USB_DEVICE(0x0424, 0x9903),
2022 .driver_info
= (unsigned long) &smsc95xx_info
,
2025 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2026 USB_DEVICE(0x0424, 0x9904),
2027 .driver_info
= (unsigned long) &smsc95xx_info
,
2030 /* SMSC9500A USB Ethernet Device (HAL) */
2031 USB_DEVICE(0x0424, 0x9905),
2032 .driver_info
= (unsigned long) &smsc95xx_info
,
2035 /* SMSC9505A USB Ethernet Device (HAL) */
2036 USB_DEVICE(0x0424, 0x9906),
2037 .driver_info
= (unsigned long) &smsc95xx_info
,
2040 /* SMSC9500 USB Ethernet Device (Alternate ID) */
2041 USB_DEVICE(0x0424, 0x9907),
2042 .driver_info
= (unsigned long) &smsc95xx_info
,
2045 /* SMSC9500A USB Ethernet Device (Alternate ID) */
2046 USB_DEVICE(0x0424, 0x9908),
2047 .driver_info
= (unsigned long) &smsc95xx_info
,
2050 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2051 USB_DEVICE(0x0424, 0x9909),
2052 .driver_info
= (unsigned long) &smsc95xx_info
,
2055 /* SMSC LAN9530 USB Ethernet Device */
2056 USB_DEVICE(0x0424, 0x9530),
2057 .driver_info
= (unsigned long) &smsc95xx_info
,
2060 /* SMSC LAN9730 USB Ethernet Device */
2061 USB_DEVICE(0x0424, 0x9730),
2062 .driver_info
= (unsigned long) &smsc95xx_info
,
2065 /* SMSC LAN89530 USB Ethernet Device */
2066 USB_DEVICE(0x0424, 0x9E08),
2067 .driver_info
= (unsigned long) &smsc95xx_info
,
2071 MODULE_DEVICE_TABLE(usb
, products
);
2073 static struct usb_driver smsc95xx_driver
= {
2075 .id_table
= products
,
2076 .probe
= usbnet_probe
,
2077 .suspend
= smsc95xx_suspend
,
2078 .resume
= smsc95xx_resume
,
2079 .reset_resume
= smsc95xx_reset_resume
,
2080 .disconnect
= usbnet_disconnect
,
2081 .disable_hub_initiated_lpm
= 1,
2082 .supports_autosuspend
= 1,
2085 module_usb_driver(smsc95xx_driver
);
2087 MODULE_AUTHOR("Nancy Lin");
2088 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2089 MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2090 MODULE_LICENSE("GPL");