Linux 3.11-rc3
[cris-mirror.git] / drivers / net / usb / smsc75xx.c
blob75409748c77470da8cc5bcc7390e9f4c5a94ea6a
1 /***************************************************************************
3 * Copyright (C) 2007-2010 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/bitrev.h>
30 #include <linux/crc16.h>
31 #include <linux/crc32.h>
32 #include <linux/usb/usbnet.h>
33 #include <linux/slab.h>
34 #include "smsc75xx.h"
36 #define SMSC_CHIPNAME "smsc75xx"
37 #define SMSC_DRIVER_VERSION "1.0.0"
38 #define HS_USB_PKT_SIZE (512)
39 #define FS_USB_PKT_SIZE (64)
40 #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
41 #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
42 #define DEFAULT_BULK_IN_DELAY (0x00002000)
43 #define MAX_SINGLE_PACKET_SIZE (9000)
44 #define LAN75XX_EEPROM_MAGIC (0x7500)
45 #define EEPROM_MAC_OFFSET (0x01)
46 #define DEFAULT_TX_CSUM_ENABLE (true)
47 #define DEFAULT_RX_CSUM_ENABLE (true)
48 #define DEFAULT_TSO_ENABLE (true)
49 #define SMSC75XX_INTERNAL_PHY_ID (1)
50 #define SMSC75XX_TX_OVERHEAD (8)
51 #define MAX_RX_FIFO_SIZE (20 * 1024)
52 #define MAX_TX_FIFO_SIZE (12 * 1024)
53 #define USB_VENDOR_ID_SMSC (0x0424)
54 #define USB_PRODUCT_ID_LAN7500 (0x7500)
55 #define USB_PRODUCT_ID_LAN7505 (0x7505)
56 #define RXW_PADDING 2
57 #define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
58 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
60 #define SUSPEND_SUSPEND0 (0x01)
61 #define SUSPEND_SUSPEND1 (0x02)
62 #define SUSPEND_SUSPEND2 (0x04)
63 #define SUSPEND_SUSPEND3 (0x08)
64 #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
65 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
67 struct smsc75xx_priv {
68 struct usbnet *dev;
69 u32 rfe_ctl;
70 u32 wolopts;
71 u32 multicast_hash_table[DP_SEL_VHF_HASH_LEN];
72 struct mutex dataport_mutex;
73 spinlock_t rfe_ctl_lock;
74 struct work_struct set_multicast;
75 u8 suspend_flags;
78 struct usb_context {
79 struct usb_ctrlrequest req;
80 struct usbnet *dev;
83 static bool turbo_mode = true;
84 module_param(turbo_mode, bool, 0644);
85 MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
87 static int __must_check __smsc75xx_read_reg(struct usbnet *dev, u32 index,
88 u32 *data, int in_pm)
90 u32 buf;
91 int ret;
92 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
94 BUG_ON(!dev);
96 if (!in_pm)
97 fn = usbnet_read_cmd;
98 else
99 fn = usbnet_read_cmd_nopm;
101 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
102 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
103 0, index, &buf, 4);
104 if (unlikely(ret < 0))
105 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
106 index, ret);
108 le32_to_cpus(&buf);
109 *data = buf;
111 return ret;
114 static int __must_check __smsc75xx_write_reg(struct usbnet *dev, u32 index,
115 u32 data, int in_pm)
117 u32 buf;
118 int ret;
119 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
121 BUG_ON(!dev);
123 if (!in_pm)
124 fn = usbnet_write_cmd;
125 else
126 fn = usbnet_write_cmd_nopm;
128 buf = data;
129 cpu_to_le32s(&buf);
131 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
132 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
133 0, index, &buf, 4);
134 if (unlikely(ret < 0))
135 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
136 index, ret);
138 return ret;
141 static int __must_check smsc75xx_read_reg_nopm(struct usbnet *dev, u32 index,
142 u32 *data)
144 return __smsc75xx_read_reg(dev, index, data, 1);
147 static int __must_check smsc75xx_write_reg_nopm(struct usbnet *dev, u32 index,
148 u32 data)
150 return __smsc75xx_write_reg(dev, index, data, 1);
153 static int __must_check smsc75xx_read_reg(struct usbnet *dev, u32 index,
154 u32 *data)
156 return __smsc75xx_read_reg(dev, index, data, 0);
159 static int __must_check smsc75xx_write_reg(struct usbnet *dev, u32 index,
160 u32 data)
162 return __smsc75xx_write_reg(dev, index, data, 0);
165 /* Loop until the read is completed with timeout
166 * called with phy_mutex held */
167 static __must_check int __smsc75xx_phy_wait_not_busy(struct usbnet *dev,
168 int in_pm)
170 unsigned long start_time = jiffies;
171 u32 val;
172 int ret;
174 do {
175 ret = __smsc75xx_read_reg(dev, MII_ACCESS, &val, in_pm);
176 if (ret < 0) {
177 netdev_warn(dev->net, "Error reading MII_ACCESS\n");
178 return ret;
181 if (!(val & MII_ACCESS_BUSY))
182 return 0;
183 } while (!time_after(jiffies, start_time + HZ));
185 return -EIO;
188 static int __smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
189 int in_pm)
191 struct usbnet *dev = netdev_priv(netdev);
192 u32 val, addr;
193 int ret;
195 mutex_lock(&dev->phy_mutex);
197 /* confirm MII not busy */
198 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
199 if (ret < 0) {
200 netdev_warn(dev->net, "MII is busy in smsc75xx_mdio_read\n");
201 goto done;
204 /* set the address, index & direction (read from PHY) */
205 phy_id &= dev->mii.phy_id_mask;
206 idx &= dev->mii.reg_num_mask;
207 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
208 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
209 | MII_ACCESS_READ | MII_ACCESS_BUSY;
210 ret = __smsc75xx_write_reg(dev, MII_ACCESS, addr, in_pm);
211 if (ret < 0) {
212 netdev_warn(dev->net, "Error writing MII_ACCESS\n");
213 goto done;
216 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
217 if (ret < 0) {
218 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
219 goto done;
222 ret = __smsc75xx_read_reg(dev, MII_DATA, &val, in_pm);
223 if (ret < 0) {
224 netdev_warn(dev->net, "Error reading MII_DATA\n");
225 goto done;
228 ret = (u16)(val & 0xFFFF);
230 done:
231 mutex_unlock(&dev->phy_mutex);
232 return ret;
235 static void __smsc75xx_mdio_write(struct net_device *netdev, int phy_id,
236 int idx, int regval, int in_pm)
238 struct usbnet *dev = netdev_priv(netdev);
239 u32 val, addr;
240 int ret;
242 mutex_lock(&dev->phy_mutex);
244 /* confirm MII not busy */
245 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
246 if (ret < 0) {
247 netdev_warn(dev->net, "MII is busy in smsc75xx_mdio_write\n");
248 goto done;
251 val = regval;
252 ret = __smsc75xx_write_reg(dev, MII_DATA, val, in_pm);
253 if (ret < 0) {
254 netdev_warn(dev->net, "Error writing MII_DATA\n");
255 goto done;
258 /* set the address, index & direction (write to PHY) */
259 phy_id &= dev->mii.phy_id_mask;
260 idx &= dev->mii.reg_num_mask;
261 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
262 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
263 | MII_ACCESS_WRITE | MII_ACCESS_BUSY;
264 ret = __smsc75xx_write_reg(dev, MII_ACCESS, addr, in_pm);
265 if (ret < 0) {
266 netdev_warn(dev->net, "Error writing MII_ACCESS\n");
267 goto done;
270 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
271 if (ret < 0) {
272 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
273 goto done;
276 done:
277 mutex_unlock(&dev->phy_mutex);
280 static int smsc75xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
281 int idx)
283 return __smsc75xx_mdio_read(netdev, phy_id, idx, 1);
286 static void smsc75xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
287 int idx, int regval)
289 __smsc75xx_mdio_write(netdev, phy_id, idx, regval, 1);
292 static int smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
294 return __smsc75xx_mdio_read(netdev, phy_id, idx, 0);
297 static void smsc75xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
298 int regval)
300 __smsc75xx_mdio_write(netdev, phy_id, idx, regval, 0);
303 static int smsc75xx_wait_eeprom(struct usbnet *dev)
305 unsigned long start_time = jiffies;
306 u32 val;
307 int ret;
309 do {
310 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
311 if (ret < 0) {
312 netdev_warn(dev->net, "Error reading E2P_CMD\n");
313 return ret;
316 if (!(val & E2P_CMD_BUSY) || (val & E2P_CMD_TIMEOUT))
317 break;
318 udelay(40);
319 } while (!time_after(jiffies, start_time + HZ));
321 if (val & (E2P_CMD_TIMEOUT | E2P_CMD_BUSY)) {
322 netdev_warn(dev->net, "EEPROM read operation timeout\n");
323 return -EIO;
326 return 0;
329 static int smsc75xx_eeprom_confirm_not_busy(struct usbnet *dev)
331 unsigned long start_time = jiffies;
332 u32 val;
333 int ret;
335 do {
336 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
337 if (ret < 0) {
338 netdev_warn(dev->net, "Error reading E2P_CMD\n");
339 return ret;
342 if (!(val & E2P_CMD_BUSY))
343 return 0;
345 udelay(40);
346 } while (!time_after(jiffies, start_time + HZ));
348 netdev_warn(dev->net, "EEPROM is busy\n");
349 return -EIO;
352 static int smsc75xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
353 u8 *data)
355 u32 val;
356 int i, ret;
358 BUG_ON(!dev);
359 BUG_ON(!data);
361 ret = smsc75xx_eeprom_confirm_not_busy(dev);
362 if (ret)
363 return ret;
365 for (i = 0; i < length; i++) {
366 val = E2P_CMD_BUSY | E2P_CMD_READ | (offset & E2P_CMD_ADDR);
367 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
368 if (ret < 0) {
369 netdev_warn(dev->net, "Error writing E2P_CMD\n");
370 return ret;
373 ret = smsc75xx_wait_eeprom(dev);
374 if (ret < 0)
375 return ret;
377 ret = smsc75xx_read_reg(dev, E2P_DATA, &val);
378 if (ret < 0) {
379 netdev_warn(dev->net, "Error reading E2P_DATA\n");
380 return ret;
383 data[i] = val & 0xFF;
384 offset++;
387 return 0;
390 static int smsc75xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
391 u8 *data)
393 u32 val;
394 int i, ret;
396 BUG_ON(!dev);
397 BUG_ON(!data);
399 ret = smsc75xx_eeprom_confirm_not_busy(dev);
400 if (ret)
401 return ret;
403 /* Issue write/erase enable command */
404 val = E2P_CMD_BUSY | E2P_CMD_EWEN;
405 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
406 if (ret < 0) {
407 netdev_warn(dev->net, "Error writing E2P_CMD\n");
408 return ret;
411 ret = smsc75xx_wait_eeprom(dev);
412 if (ret < 0)
413 return ret;
415 for (i = 0; i < length; i++) {
417 /* Fill data register */
418 val = data[i];
419 ret = smsc75xx_write_reg(dev, E2P_DATA, val);
420 if (ret < 0) {
421 netdev_warn(dev->net, "Error writing E2P_DATA\n");
422 return ret;
425 /* Send "write" command */
426 val = E2P_CMD_BUSY | E2P_CMD_WRITE | (offset & E2P_CMD_ADDR);
427 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
428 if (ret < 0) {
429 netdev_warn(dev->net, "Error writing E2P_CMD\n");
430 return ret;
433 ret = smsc75xx_wait_eeprom(dev);
434 if (ret < 0)
435 return ret;
437 offset++;
440 return 0;
443 static int smsc75xx_dataport_wait_not_busy(struct usbnet *dev)
445 int i, ret;
447 for (i = 0; i < 100; i++) {
448 u32 dp_sel;
449 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
450 if (ret < 0) {
451 netdev_warn(dev->net, "Error reading DP_SEL\n");
452 return ret;
455 if (dp_sel & DP_SEL_DPRDY)
456 return 0;
458 udelay(40);
461 netdev_warn(dev->net, "smsc75xx_dataport_wait_not_busy timed out\n");
463 return -EIO;
466 static int smsc75xx_dataport_write(struct usbnet *dev, u32 ram_select, u32 addr,
467 u32 length, u32 *buf)
469 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
470 u32 dp_sel;
471 int i, ret;
473 mutex_lock(&pdata->dataport_mutex);
475 ret = smsc75xx_dataport_wait_not_busy(dev);
476 if (ret < 0) {
477 netdev_warn(dev->net, "smsc75xx_dataport_write busy on entry\n");
478 goto done;
481 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
482 if (ret < 0) {
483 netdev_warn(dev->net, "Error reading DP_SEL\n");
484 goto done;
487 dp_sel &= ~DP_SEL_RSEL;
488 dp_sel |= ram_select;
489 ret = smsc75xx_write_reg(dev, DP_SEL, dp_sel);
490 if (ret < 0) {
491 netdev_warn(dev->net, "Error writing DP_SEL\n");
492 goto done;
495 for (i = 0; i < length; i++) {
496 ret = smsc75xx_write_reg(dev, DP_ADDR, addr + i);
497 if (ret < 0) {
498 netdev_warn(dev->net, "Error writing DP_ADDR\n");
499 goto done;
502 ret = smsc75xx_write_reg(dev, DP_DATA, buf[i]);
503 if (ret < 0) {
504 netdev_warn(dev->net, "Error writing DP_DATA\n");
505 goto done;
508 ret = smsc75xx_write_reg(dev, DP_CMD, DP_CMD_WRITE);
509 if (ret < 0) {
510 netdev_warn(dev->net, "Error writing DP_CMD\n");
511 goto done;
514 ret = smsc75xx_dataport_wait_not_busy(dev);
515 if (ret < 0) {
516 netdev_warn(dev->net, "smsc75xx_dataport_write timeout\n");
517 goto done;
521 done:
522 mutex_unlock(&pdata->dataport_mutex);
523 return ret;
526 /* returns hash bit number for given MAC address */
527 static u32 smsc75xx_hash(char addr[ETH_ALEN])
529 return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
532 static void smsc75xx_deferred_multicast_write(struct work_struct *param)
534 struct smsc75xx_priv *pdata =
535 container_of(param, struct smsc75xx_priv, set_multicast);
536 struct usbnet *dev = pdata->dev;
537 int ret;
539 netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x\n",
540 pdata->rfe_ctl);
542 smsc75xx_dataport_write(dev, DP_SEL_VHF, DP_SEL_VHF_VLAN_LEN,
543 DP_SEL_VHF_HASH_LEN, pdata->multicast_hash_table);
545 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
546 if (ret < 0)
547 netdev_warn(dev->net, "Error writing RFE_CRL\n");
550 static void smsc75xx_set_multicast(struct net_device *netdev)
552 struct usbnet *dev = netdev_priv(netdev);
553 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
554 unsigned long flags;
555 int i;
557 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
559 pdata->rfe_ctl &=
560 ~(RFE_CTL_AU | RFE_CTL_AM | RFE_CTL_DPF | RFE_CTL_MHF);
561 pdata->rfe_ctl |= RFE_CTL_AB;
563 for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
564 pdata->multicast_hash_table[i] = 0;
566 if (dev->net->flags & IFF_PROMISC) {
567 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
568 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_AU;
569 } else if (dev->net->flags & IFF_ALLMULTI) {
570 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
571 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_DPF;
572 } else if (!netdev_mc_empty(dev->net)) {
573 struct netdev_hw_addr *ha;
575 netif_dbg(dev, drv, dev->net, "receive multicast hash filter\n");
577 pdata->rfe_ctl |= RFE_CTL_MHF | RFE_CTL_DPF;
579 netdev_for_each_mc_addr(ha, netdev) {
580 u32 bitnum = smsc75xx_hash(ha->addr);
581 pdata->multicast_hash_table[bitnum / 32] |=
582 (1 << (bitnum % 32));
584 } else {
585 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
586 pdata->rfe_ctl |= RFE_CTL_DPF;
589 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
591 /* defer register writes to a sleepable context */
592 schedule_work(&pdata->set_multicast);
595 static int smsc75xx_update_flowcontrol(struct usbnet *dev, u8 duplex,
596 u16 lcladv, u16 rmtadv)
598 u32 flow = 0, fct_flow = 0;
599 int ret;
601 if (duplex == DUPLEX_FULL) {
602 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
604 if (cap & FLOW_CTRL_TX) {
605 flow = (FLOW_TX_FCEN | 0xFFFF);
606 /* set fct_flow thresholds to 20% and 80% */
607 fct_flow = (8 << 8) | 32;
610 if (cap & FLOW_CTRL_RX)
611 flow |= FLOW_RX_FCEN;
613 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
614 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
615 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
616 } else {
617 netif_dbg(dev, link, dev->net, "half duplex\n");
620 ret = smsc75xx_write_reg(dev, FLOW, flow);
621 if (ret < 0) {
622 netdev_warn(dev->net, "Error writing FLOW\n");
623 return ret;
626 ret = smsc75xx_write_reg(dev, FCT_FLOW, fct_flow);
627 if (ret < 0) {
628 netdev_warn(dev->net, "Error writing FCT_FLOW\n");
629 return ret;
632 return 0;
635 static int smsc75xx_link_reset(struct usbnet *dev)
637 struct mii_if_info *mii = &dev->mii;
638 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
639 u16 lcladv, rmtadv;
640 int ret;
642 /* write to clear phy interrupt status */
643 smsc75xx_mdio_write(dev->net, mii->phy_id, PHY_INT_SRC,
644 PHY_INT_SRC_CLEAR_ALL);
646 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
647 if (ret < 0) {
648 netdev_warn(dev->net, "Error writing INT_STS\n");
649 return ret;
652 mii_check_media(mii, 1, 1);
653 mii_ethtool_gset(&dev->mii, &ecmd);
654 lcladv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
655 rmtadv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
657 netif_dbg(dev, link, dev->net, "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
658 ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
660 return smsc75xx_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
663 static void smsc75xx_status(struct usbnet *dev, struct urb *urb)
665 u32 intdata;
667 if (urb->actual_length != 4) {
668 netdev_warn(dev->net, "unexpected urb length %d\n",
669 urb->actual_length);
670 return;
673 memcpy(&intdata, urb->transfer_buffer, 4);
674 le32_to_cpus(&intdata);
676 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
678 if (intdata & INT_ENP_PHY_INT)
679 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
680 else
681 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
682 intdata);
685 static int smsc75xx_ethtool_get_eeprom_len(struct net_device *net)
687 return MAX_EEPROM_SIZE;
690 static int smsc75xx_ethtool_get_eeprom(struct net_device *netdev,
691 struct ethtool_eeprom *ee, u8 *data)
693 struct usbnet *dev = netdev_priv(netdev);
695 ee->magic = LAN75XX_EEPROM_MAGIC;
697 return smsc75xx_read_eeprom(dev, ee->offset, ee->len, data);
700 static int smsc75xx_ethtool_set_eeprom(struct net_device *netdev,
701 struct ethtool_eeprom *ee, u8 *data)
703 struct usbnet *dev = netdev_priv(netdev);
705 if (ee->magic != LAN75XX_EEPROM_MAGIC) {
706 netdev_warn(dev->net, "EEPROM: magic value mismatch: 0x%x\n",
707 ee->magic);
708 return -EINVAL;
711 return smsc75xx_write_eeprom(dev, ee->offset, ee->len, data);
714 static void smsc75xx_ethtool_get_wol(struct net_device *net,
715 struct ethtool_wolinfo *wolinfo)
717 struct usbnet *dev = netdev_priv(net);
718 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
720 wolinfo->supported = SUPPORTED_WAKE;
721 wolinfo->wolopts = pdata->wolopts;
724 static int smsc75xx_ethtool_set_wol(struct net_device *net,
725 struct ethtool_wolinfo *wolinfo)
727 struct usbnet *dev = netdev_priv(net);
728 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
729 int ret;
731 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
733 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
734 if (ret < 0)
735 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
737 return ret;
740 static const struct ethtool_ops smsc75xx_ethtool_ops = {
741 .get_link = usbnet_get_link,
742 .nway_reset = usbnet_nway_reset,
743 .get_drvinfo = usbnet_get_drvinfo,
744 .get_msglevel = usbnet_get_msglevel,
745 .set_msglevel = usbnet_set_msglevel,
746 .get_settings = usbnet_get_settings,
747 .set_settings = usbnet_set_settings,
748 .get_eeprom_len = smsc75xx_ethtool_get_eeprom_len,
749 .get_eeprom = smsc75xx_ethtool_get_eeprom,
750 .set_eeprom = smsc75xx_ethtool_set_eeprom,
751 .get_wol = smsc75xx_ethtool_get_wol,
752 .set_wol = smsc75xx_ethtool_set_wol,
755 static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
757 struct usbnet *dev = netdev_priv(netdev);
759 if (!netif_running(netdev))
760 return -EINVAL;
762 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
765 static void smsc75xx_init_mac_address(struct usbnet *dev)
767 /* try reading mac address from EEPROM */
768 if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
769 dev->net->dev_addr) == 0) {
770 if (is_valid_ether_addr(dev->net->dev_addr)) {
771 /* eeprom values are valid so use them */
772 netif_dbg(dev, ifup, dev->net,
773 "MAC address read from EEPROM\n");
774 return;
778 /* no eeprom, or eeprom values are invalid. generate random MAC */
779 eth_hw_addr_random(dev->net);
780 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
783 static int smsc75xx_set_mac_address(struct usbnet *dev)
785 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
786 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
787 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
789 int ret = smsc75xx_write_reg(dev, RX_ADDRH, addr_hi);
790 if (ret < 0) {
791 netdev_warn(dev->net, "Failed to write RX_ADDRH: %d\n", ret);
792 return ret;
795 ret = smsc75xx_write_reg(dev, RX_ADDRL, addr_lo);
796 if (ret < 0) {
797 netdev_warn(dev->net, "Failed to write RX_ADDRL: %d\n", ret);
798 return ret;
801 addr_hi |= ADDR_FILTX_FB_VALID;
802 ret = smsc75xx_write_reg(dev, ADDR_FILTX, addr_hi);
803 if (ret < 0) {
804 netdev_warn(dev->net, "Failed to write ADDR_FILTX: %d\n", ret);
805 return ret;
808 ret = smsc75xx_write_reg(dev, ADDR_FILTX + 4, addr_lo);
809 if (ret < 0)
810 netdev_warn(dev->net, "Failed to write ADDR_FILTX+4: %d\n", ret);
812 return ret;
815 static int smsc75xx_phy_initialize(struct usbnet *dev)
817 int bmcr, ret, timeout = 0;
819 /* Initialize MII structure */
820 dev->mii.dev = dev->net;
821 dev->mii.mdio_read = smsc75xx_mdio_read;
822 dev->mii.mdio_write = smsc75xx_mdio_write;
823 dev->mii.phy_id_mask = 0x1f;
824 dev->mii.reg_num_mask = 0x1f;
825 dev->mii.supports_gmii = 1;
826 dev->mii.phy_id = SMSC75XX_INTERNAL_PHY_ID;
828 /* reset phy and wait for reset to complete */
829 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
831 do {
832 msleep(10);
833 bmcr = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
834 if (bmcr < 0) {
835 netdev_warn(dev->net, "Error reading MII_BMCR\n");
836 return bmcr;
838 timeout++;
839 } while ((bmcr & BMCR_RESET) && (timeout < 100));
841 if (timeout >= 100) {
842 netdev_warn(dev->net, "timeout on PHY Reset\n");
843 return -EIO;
846 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
847 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
848 ADVERTISE_PAUSE_ASYM);
849 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
850 ADVERTISE_1000FULL);
852 /* read and write to clear phy interrupt status */
853 ret = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
854 if (ret < 0) {
855 netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
856 return ret;
859 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_SRC, 0xffff);
861 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
862 PHY_INT_MASK_DEFAULT);
863 mii_nway_restart(&dev->mii);
865 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
866 return 0;
869 static int smsc75xx_set_rx_max_frame_length(struct usbnet *dev, int size)
871 int ret = 0;
872 u32 buf;
873 bool rxenabled;
875 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
876 if (ret < 0) {
877 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
878 return ret;
881 rxenabled = ((buf & MAC_RX_RXEN) != 0);
883 if (rxenabled) {
884 buf &= ~MAC_RX_RXEN;
885 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
886 if (ret < 0) {
887 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
888 return ret;
892 /* add 4 to size for FCS */
893 buf &= ~MAC_RX_MAX_SIZE;
894 buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT) & MAC_RX_MAX_SIZE);
896 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
897 if (ret < 0) {
898 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
899 return ret;
902 if (rxenabled) {
903 buf |= MAC_RX_RXEN;
904 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
905 if (ret < 0) {
906 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
907 return ret;
911 return 0;
914 static int smsc75xx_change_mtu(struct net_device *netdev, int new_mtu)
916 struct usbnet *dev = netdev_priv(netdev);
917 int ret;
919 if (new_mtu > MAX_SINGLE_PACKET_SIZE)
920 return -EINVAL;
922 ret = smsc75xx_set_rx_max_frame_length(dev, new_mtu + ETH_HLEN);
923 if (ret < 0) {
924 netdev_warn(dev->net, "Failed to set mac rx frame length\n");
925 return ret;
928 return usbnet_change_mtu(netdev, new_mtu);
931 /* Enable or disable Rx checksum offload engine */
932 static int smsc75xx_set_features(struct net_device *netdev,
933 netdev_features_t features)
935 struct usbnet *dev = netdev_priv(netdev);
936 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
937 unsigned long flags;
938 int ret;
940 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
942 if (features & NETIF_F_RXCSUM)
943 pdata->rfe_ctl |= RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM;
944 else
945 pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM);
947 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
948 /* it's racing here! */
950 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
951 if (ret < 0)
952 netdev_warn(dev->net, "Error writing RFE_CTL\n");
954 return ret;
957 static int smsc75xx_wait_ready(struct usbnet *dev, int in_pm)
959 int timeout = 0;
961 do {
962 u32 buf;
963 int ret;
965 ret = __smsc75xx_read_reg(dev, PMT_CTL, &buf, in_pm);
967 if (ret < 0) {
968 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
969 return ret;
972 if (buf & PMT_CTL_DEV_RDY)
973 return 0;
975 msleep(10);
976 timeout++;
977 } while (timeout < 100);
979 netdev_warn(dev->net, "timeout waiting for device ready\n");
980 return -EIO;
983 static int smsc75xx_reset(struct usbnet *dev)
985 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
986 u32 buf;
987 int ret = 0, timeout;
989 netif_dbg(dev, ifup, dev->net, "entering smsc75xx_reset\n");
991 ret = smsc75xx_wait_ready(dev, 0);
992 if (ret < 0) {
993 netdev_warn(dev->net, "device not ready in smsc75xx_reset\n");
994 return ret;
997 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
998 if (ret < 0) {
999 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1000 return ret;
1003 buf |= HW_CFG_LRST;
1005 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1006 if (ret < 0) {
1007 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1008 return ret;
1011 timeout = 0;
1012 do {
1013 msleep(10);
1014 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1015 if (ret < 0) {
1016 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1017 return ret;
1019 timeout++;
1020 } while ((buf & HW_CFG_LRST) && (timeout < 100));
1022 if (timeout >= 100) {
1023 netdev_warn(dev->net, "timeout on completion of Lite Reset\n");
1024 return -EIO;
1027 netif_dbg(dev, ifup, dev->net, "Lite reset complete, resetting PHY\n");
1029 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1030 if (ret < 0) {
1031 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1032 return ret;
1035 buf |= PMT_CTL_PHY_RST;
1037 ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
1038 if (ret < 0) {
1039 netdev_warn(dev->net, "Failed to write PMT_CTL: %d\n", ret);
1040 return ret;
1043 timeout = 0;
1044 do {
1045 msleep(10);
1046 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1047 if (ret < 0) {
1048 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1049 return ret;
1051 timeout++;
1052 } while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
1054 if (timeout >= 100) {
1055 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
1056 return -EIO;
1059 netif_dbg(dev, ifup, dev->net, "PHY reset complete\n");
1061 ret = smsc75xx_set_mac_address(dev);
1062 if (ret < 0) {
1063 netdev_warn(dev->net, "Failed to set mac address\n");
1064 return ret;
1067 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
1068 dev->net->dev_addr);
1070 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1071 if (ret < 0) {
1072 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1073 return ret;
1076 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
1077 buf);
1079 buf |= HW_CFG_BIR;
1081 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1082 if (ret < 0) {
1083 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1084 return ret;
1087 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1088 if (ret < 0) {
1089 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1090 return ret;
1093 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG after writing HW_CFG_BIR: 0x%08x\n",
1094 buf);
1096 if (!turbo_mode) {
1097 buf = 0;
1098 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1099 } else if (dev->udev->speed == USB_SPEED_HIGH) {
1100 buf = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1101 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1102 } else {
1103 buf = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1104 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1107 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1108 (ulong)dev->rx_urb_size);
1110 ret = smsc75xx_write_reg(dev, BURST_CAP, buf);
1111 if (ret < 0) {
1112 netdev_warn(dev->net, "Failed to write BURST_CAP: %d\n", ret);
1113 return ret;
1116 ret = smsc75xx_read_reg(dev, BURST_CAP, &buf);
1117 if (ret < 0) {
1118 netdev_warn(dev->net, "Failed to read BURST_CAP: %d\n", ret);
1119 return ret;
1122 netif_dbg(dev, ifup, dev->net,
1123 "Read Value from BURST_CAP after writing: 0x%08x\n", buf);
1125 ret = smsc75xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1126 if (ret < 0) {
1127 netdev_warn(dev->net, "Failed to write BULK_IN_DLY: %d\n", ret);
1128 return ret;
1131 ret = smsc75xx_read_reg(dev, BULK_IN_DLY, &buf);
1132 if (ret < 0) {
1133 netdev_warn(dev->net, "Failed to read BULK_IN_DLY: %d\n", ret);
1134 return ret;
1137 netif_dbg(dev, ifup, dev->net,
1138 "Read Value from BULK_IN_DLY after writing: 0x%08x\n", buf);
1140 if (turbo_mode) {
1141 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1142 if (ret < 0) {
1143 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1144 return ret;
1147 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
1149 buf |= (HW_CFG_MEF | HW_CFG_BCE);
1151 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1152 if (ret < 0) {
1153 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1154 return ret;
1157 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1158 if (ret < 0) {
1159 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1160 return ret;
1163 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
1166 /* set FIFO sizes */
1167 buf = (MAX_RX_FIFO_SIZE - 512) / 512;
1168 ret = smsc75xx_write_reg(dev, FCT_RX_FIFO_END, buf);
1169 if (ret < 0) {
1170 netdev_warn(dev->net, "Failed to write FCT_RX_FIFO_END: %d\n", ret);
1171 return ret;
1174 netif_dbg(dev, ifup, dev->net, "FCT_RX_FIFO_END set to 0x%08x\n", buf);
1176 buf = (MAX_TX_FIFO_SIZE - 512) / 512;
1177 ret = smsc75xx_write_reg(dev, FCT_TX_FIFO_END, buf);
1178 if (ret < 0) {
1179 netdev_warn(dev->net, "Failed to write FCT_TX_FIFO_END: %d\n", ret);
1180 return ret;
1183 netif_dbg(dev, ifup, dev->net, "FCT_TX_FIFO_END set to 0x%08x\n", buf);
1185 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
1186 if (ret < 0) {
1187 netdev_warn(dev->net, "Failed to write INT_STS: %d\n", ret);
1188 return ret;
1191 ret = smsc75xx_read_reg(dev, ID_REV, &buf);
1192 if (ret < 0) {
1193 netdev_warn(dev->net, "Failed to read ID_REV: %d\n", ret);
1194 return ret;
1197 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", buf);
1199 ret = smsc75xx_read_reg(dev, E2P_CMD, &buf);
1200 if (ret < 0) {
1201 netdev_warn(dev->net, "Failed to read E2P_CMD: %d\n", ret);
1202 return ret;
1205 /* only set default GPIO/LED settings if no EEPROM is detected */
1206 if (!(buf & E2P_CMD_LOADED)) {
1207 ret = smsc75xx_read_reg(dev, LED_GPIO_CFG, &buf);
1208 if (ret < 0) {
1209 netdev_warn(dev->net, "Failed to read LED_GPIO_CFG: %d\n", ret);
1210 return ret;
1213 buf &= ~(LED_GPIO_CFG_LED2_FUN_SEL | LED_GPIO_CFG_LED10_FUN_SEL);
1214 buf |= LED_GPIO_CFG_LEDGPIO_EN | LED_GPIO_CFG_LED2_FUN_SEL;
1216 ret = smsc75xx_write_reg(dev, LED_GPIO_CFG, buf);
1217 if (ret < 0) {
1218 netdev_warn(dev->net, "Failed to write LED_GPIO_CFG: %d\n", ret);
1219 return ret;
1223 ret = smsc75xx_write_reg(dev, FLOW, 0);
1224 if (ret < 0) {
1225 netdev_warn(dev->net, "Failed to write FLOW: %d\n", ret);
1226 return ret;
1229 ret = smsc75xx_write_reg(dev, FCT_FLOW, 0);
1230 if (ret < 0) {
1231 netdev_warn(dev->net, "Failed to write FCT_FLOW: %d\n", ret);
1232 return ret;
1235 /* Don't need rfe_ctl_lock during initialisation */
1236 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
1237 if (ret < 0) {
1238 netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1239 return ret;
1242 pdata->rfe_ctl |= RFE_CTL_AB | RFE_CTL_DPF;
1244 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
1245 if (ret < 0) {
1246 netdev_warn(dev->net, "Failed to write RFE_CTL: %d\n", ret);
1247 return ret;
1250 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
1251 if (ret < 0) {
1252 netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1253 return ret;
1256 netif_dbg(dev, ifup, dev->net, "RFE_CTL set to 0x%08x\n",
1257 pdata->rfe_ctl);
1259 /* Enable or disable checksum offload engines */
1260 smsc75xx_set_features(dev->net, dev->net->features);
1262 smsc75xx_set_multicast(dev->net);
1264 ret = smsc75xx_phy_initialize(dev);
1265 if (ret < 0) {
1266 netdev_warn(dev->net, "Failed to initialize PHY: %d\n", ret);
1267 return ret;
1270 ret = smsc75xx_read_reg(dev, INT_EP_CTL, &buf);
1271 if (ret < 0) {
1272 netdev_warn(dev->net, "Failed to read INT_EP_CTL: %d\n", ret);
1273 return ret;
1276 /* enable PHY interrupts */
1277 buf |= INT_ENP_PHY_INT;
1279 ret = smsc75xx_write_reg(dev, INT_EP_CTL, buf);
1280 if (ret < 0) {
1281 netdev_warn(dev->net, "Failed to write INT_EP_CTL: %d\n", ret);
1282 return ret;
1285 /* allow mac to detect speed and duplex from phy */
1286 ret = smsc75xx_read_reg(dev, MAC_CR, &buf);
1287 if (ret < 0) {
1288 netdev_warn(dev->net, "Failed to read MAC_CR: %d\n", ret);
1289 return ret;
1292 buf |= (MAC_CR_ADD | MAC_CR_ASD);
1293 ret = smsc75xx_write_reg(dev, MAC_CR, buf);
1294 if (ret < 0) {
1295 netdev_warn(dev->net, "Failed to write MAC_CR: %d\n", ret);
1296 return ret;
1299 ret = smsc75xx_read_reg(dev, MAC_TX, &buf);
1300 if (ret < 0) {
1301 netdev_warn(dev->net, "Failed to read MAC_TX: %d\n", ret);
1302 return ret;
1305 buf |= MAC_TX_TXEN;
1307 ret = smsc75xx_write_reg(dev, MAC_TX, buf);
1308 if (ret < 0) {
1309 netdev_warn(dev->net, "Failed to write MAC_TX: %d\n", ret);
1310 return ret;
1313 netif_dbg(dev, ifup, dev->net, "MAC_TX set to 0x%08x\n", buf);
1315 ret = smsc75xx_read_reg(dev, FCT_TX_CTL, &buf);
1316 if (ret < 0) {
1317 netdev_warn(dev->net, "Failed to read FCT_TX_CTL: %d\n", ret);
1318 return ret;
1321 buf |= FCT_TX_CTL_EN;
1323 ret = smsc75xx_write_reg(dev, FCT_TX_CTL, buf);
1324 if (ret < 0) {
1325 netdev_warn(dev->net, "Failed to write FCT_TX_CTL: %d\n", ret);
1326 return ret;
1329 netif_dbg(dev, ifup, dev->net, "FCT_TX_CTL set to 0x%08x\n", buf);
1331 ret = smsc75xx_set_rx_max_frame_length(dev, dev->net->mtu + ETH_HLEN);
1332 if (ret < 0) {
1333 netdev_warn(dev->net, "Failed to set max rx frame length\n");
1334 return ret;
1337 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
1338 if (ret < 0) {
1339 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
1340 return ret;
1343 buf |= MAC_RX_RXEN;
1345 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
1346 if (ret < 0) {
1347 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
1348 return ret;
1351 netif_dbg(dev, ifup, dev->net, "MAC_RX set to 0x%08x\n", buf);
1353 ret = smsc75xx_read_reg(dev, FCT_RX_CTL, &buf);
1354 if (ret < 0) {
1355 netdev_warn(dev->net, "Failed to read FCT_RX_CTL: %d\n", ret);
1356 return ret;
1359 buf |= FCT_RX_CTL_EN;
1361 ret = smsc75xx_write_reg(dev, FCT_RX_CTL, buf);
1362 if (ret < 0) {
1363 netdev_warn(dev->net, "Failed to write FCT_RX_CTL: %d\n", ret);
1364 return ret;
1367 netif_dbg(dev, ifup, dev->net, "FCT_RX_CTL set to 0x%08x\n", buf);
1369 netif_dbg(dev, ifup, dev->net, "smsc75xx_reset, return 0\n");
1370 return 0;
1373 static const struct net_device_ops smsc75xx_netdev_ops = {
1374 .ndo_open = usbnet_open,
1375 .ndo_stop = usbnet_stop,
1376 .ndo_start_xmit = usbnet_start_xmit,
1377 .ndo_tx_timeout = usbnet_tx_timeout,
1378 .ndo_change_mtu = smsc75xx_change_mtu,
1379 .ndo_set_mac_address = eth_mac_addr,
1380 .ndo_validate_addr = eth_validate_addr,
1381 .ndo_do_ioctl = smsc75xx_ioctl,
1382 .ndo_set_rx_mode = smsc75xx_set_multicast,
1383 .ndo_set_features = smsc75xx_set_features,
1386 static int smsc75xx_bind(struct usbnet *dev, struct usb_interface *intf)
1388 struct smsc75xx_priv *pdata = NULL;
1389 int ret;
1391 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1393 ret = usbnet_get_endpoints(dev, intf);
1394 if (ret < 0) {
1395 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1396 return ret;
1399 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc75xx_priv),
1400 GFP_KERNEL);
1402 pdata = (struct smsc75xx_priv *)(dev->data[0]);
1403 if (!pdata)
1404 return -ENOMEM;
1406 pdata->dev = dev;
1408 spin_lock_init(&pdata->rfe_ctl_lock);
1409 mutex_init(&pdata->dataport_mutex);
1411 INIT_WORK(&pdata->set_multicast, smsc75xx_deferred_multicast_write);
1413 if (DEFAULT_TX_CSUM_ENABLE) {
1414 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1415 if (DEFAULT_TSO_ENABLE)
1416 dev->net->features |= NETIF_F_SG |
1417 NETIF_F_TSO | NETIF_F_TSO6;
1419 if (DEFAULT_RX_CSUM_ENABLE)
1420 dev->net->features |= NETIF_F_RXCSUM;
1422 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1423 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_RXCSUM;
1425 ret = smsc75xx_wait_ready(dev, 0);
1426 if (ret < 0) {
1427 netdev_warn(dev->net, "device not ready in smsc75xx_bind\n");
1428 return ret;
1431 smsc75xx_init_mac_address(dev);
1433 /* Init all registers */
1434 ret = smsc75xx_reset(dev);
1435 if (ret < 0) {
1436 netdev_warn(dev->net, "smsc75xx_reset error %d\n", ret);
1437 return ret;
1440 dev->net->netdev_ops = &smsc75xx_netdev_ops;
1441 dev->net->ethtool_ops = &smsc75xx_ethtool_ops;
1442 dev->net->flags |= IFF_MULTICAST;
1443 dev->net->hard_header_len += SMSC75XX_TX_OVERHEAD;
1444 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1445 return 0;
1448 static void smsc75xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1450 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1451 if (pdata) {
1452 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1453 kfree(pdata);
1454 pdata = NULL;
1455 dev->data[0] = 0;
1459 static u16 smsc_crc(const u8 *buffer, size_t len)
1461 return bitrev16(crc16(0xFFFF, buffer, len));
1464 static int smsc75xx_write_wuff(struct usbnet *dev, int filter, u32 wuf_cfg,
1465 u32 wuf_mask1)
1467 int cfg_base = WUF_CFGX + filter * 4;
1468 int mask_base = WUF_MASKX + filter * 16;
1469 int ret;
1471 ret = smsc75xx_write_reg(dev, cfg_base, wuf_cfg);
1472 if (ret < 0) {
1473 netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1474 return ret;
1477 ret = smsc75xx_write_reg(dev, mask_base, wuf_mask1);
1478 if (ret < 0) {
1479 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1480 return ret;
1483 ret = smsc75xx_write_reg(dev, mask_base + 4, 0);
1484 if (ret < 0) {
1485 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1486 return ret;
1489 ret = smsc75xx_write_reg(dev, mask_base + 8, 0);
1490 if (ret < 0) {
1491 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1492 return ret;
1495 ret = smsc75xx_write_reg(dev, mask_base + 12, 0);
1496 if (ret < 0) {
1497 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1498 return ret;
1501 return 0;
1504 static int smsc75xx_enter_suspend0(struct usbnet *dev)
1506 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1507 u32 val;
1508 int ret;
1510 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1511 if (ret < 0) {
1512 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1513 return ret;
1516 val &= (~(PMT_CTL_SUS_MODE | PMT_CTL_PHY_RST));
1517 val |= PMT_CTL_SUS_MODE_0 | PMT_CTL_WOL_EN | PMT_CTL_WUPS;
1519 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1520 if (ret < 0) {
1521 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1522 return ret;
1525 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1527 return 0;
1530 static int smsc75xx_enter_suspend1(struct usbnet *dev)
1532 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1533 u32 val;
1534 int ret;
1536 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1537 if (ret < 0) {
1538 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1539 return ret;
1542 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1543 val |= PMT_CTL_SUS_MODE_1;
1545 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1546 if (ret < 0) {
1547 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1548 return ret;
1551 /* clear wol status, enable energy detection */
1552 val &= ~PMT_CTL_WUPS;
1553 val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
1555 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1556 if (ret < 0) {
1557 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1558 return ret;
1561 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1563 return 0;
1566 static int smsc75xx_enter_suspend2(struct usbnet *dev)
1568 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1569 u32 val;
1570 int ret;
1572 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1573 if (ret < 0) {
1574 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1575 return ret;
1578 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1579 val |= PMT_CTL_SUS_MODE_2;
1581 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1582 if (ret < 0) {
1583 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1584 return ret;
1587 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1589 return 0;
1592 static int smsc75xx_enter_suspend3(struct usbnet *dev)
1594 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1595 u32 val;
1596 int ret;
1598 ret = smsc75xx_read_reg_nopm(dev, FCT_RX_CTL, &val);
1599 if (ret < 0) {
1600 netdev_warn(dev->net, "Error reading FCT_RX_CTL\n");
1601 return ret;
1604 if (val & FCT_RX_CTL_RXUSED) {
1605 netdev_dbg(dev->net, "rx fifo not empty in autosuspend\n");
1606 return -EBUSY;
1609 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1610 if (ret < 0) {
1611 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1612 return ret;
1615 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1616 val |= PMT_CTL_SUS_MODE_3 | PMT_CTL_RES_CLR_WKP_EN;
1618 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1619 if (ret < 0) {
1620 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1621 return ret;
1624 /* clear wol status */
1625 val &= ~PMT_CTL_WUPS;
1626 val |= PMT_CTL_WUPS_WOL;
1628 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1629 if (ret < 0) {
1630 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1631 return ret;
1634 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1636 return 0;
1639 static int smsc75xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1641 struct mii_if_info *mii = &dev->mii;
1642 int ret;
1644 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1646 /* read to clear */
1647 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1648 if (ret < 0) {
1649 netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
1650 return ret;
1653 /* enable interrupt source */
1654 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1655 if (ret < 0) {
1656 netdev_warn(dev->net, "Error reading PHY_INT_MASK\n");
1657 return ret;
1660 ret |= mask;
1662 smsc75xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1664 return 0;
1667 static int smsc75xx_link_ok_nopm(struct usbnet *dev)
1669 struct mii_if_info *mii = &dev->mii;
1670 int ret;
1672 /* first, a dummy read, needed to latch some MII phys */
1673 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1674 if (ret < 0) {
1675 netdev_warn(dev->net, "Error reading MII_BMSR\n");
1676 return ret;
1679 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1680 if (ret < 0) {
1681 netdev_warn(dev->net, "Error reading MII_BMSR\n");
1682 return ret;
1685 return !!(ret & BMSR_LSTATUS);
1688 static int smsc75xx_autosuspend(struct usbnet *dev, u32 link_up)
1690 int ret;
1692 if (!netif_running(dev->net)) {
1693 /* interface is ifconfig down so fully power down hw */
1694 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1695 return smsc75xx_enter_suspend2(dev);
1698 if (!link_up) {
1699 /* link is down so enter EDPD mode */
1700 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1702 /* enable PHY wakeup events for if cable is attached */
1703 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1704 PHY_INT_MASK_ANEG_COMP);
1705 if (ret < 0) {
1706 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1707 return ret;
1710 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1711 return smsc75xx_enter_suspend1(dev);
1714 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1715 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1716 PHY_INT_MASK_LINK_DOWN);
1717 if (ret < 0) {
1718 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1719 return ret;
1722 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1723 return smsc75xx_enter_suspend3(dev);
1726 static int smsc75xx_suspend(struct usb_interface *intf, pm_message_t message)
1728 struct usbnet *dev = usb_get_intfdata(intf);
1729 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1730 u32 val, link_up;
1731 int ret;
1733 ret = usbnet_suspend(intf, message);
1734 if (ret < 0) {
1735 netdev_warn(dev->net, "usbnet_suspend error\n");
1736 return ret;
1739 if (pdata->suspend_flags) {
1740 netdev_warn(dev->net, "error during last resume\n");
1741 pdata->suspend_flags = 0;
1744 /* determine if link is up using only _nopm functions */
1745 link_up = smsc75xx_link_ok_nopm(dev);
1747 if (message.event == PM_EVENT_AUTO_SUSPEND) {
1748 ret = smsc75xx_autosuspend(dev, link_up);
1749 goto done;
1752 /* if we get this far we're not autosuspending */
1753 /* if no wol options set, or if link is down and we're not waking on
1754 * PHY activity, enter lowest power SUSPEND2 mode
1756 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1757 !(link_up || (pdata->wolopts & WAKE_PHY))) {
1758 netdev_info(dev->net, "entering SUSPEND2 mode\n");
1760 /* disable energy detect (link up) & wake up events */
1761 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1762 if (ret < 0) {
1763 netdev_warn(dev->net, "Error reading WUCSR\n");
1764 goto done;
1767 val &= ~(WUCSR_MPEN | WUCSR_WUEN);
1769 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1770 if (ret < 0) {
1771 netdev_warn(dev->net, "Error writing WUCSR\n");
1772 goto done;
1775 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1776 if (ret < 0) {
1777 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1778 goto done;
1781 val &= ~(PMT_CTL_ED_EN | PMT_CTL_WOL_EN);
1783 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1784 if (ret < 0) {
1785 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1786 goto done;
1789 ret = smsc75xx_enter_suspend2(dev);
1790 goto done;
1793 if (pdata->wolopts & WAKE_PHY) {
1794 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1795 (PHY_INT_MASK_ANEG_COMP | PHY_INT_MASK_LINK_DOWN));
1796 if (ret < 0) {
1797 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1798 goto done;
1801 /* if link is down then configure EDPD and enter SUSPEND1,
1802 * otherwise enter SUSPEND0 below
1804 if (!link_up) {
1805 struct mii_if_info *mii = &dev->mii;
1806 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1808 /* enable energy detect power-down mode */
1809 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id,
1810 PHY_MODE_CTRL_STS);
1811 if (ret < 0) {
1812 netdev_warn(dev->net, "Error reading PHY_MODE_CTRL_STS\n");
1813 goto done;
1816 ret |= MODE_CTRL_STS_EDPWRDOWN;
1818 smsc75xx_mdio_write_nopm(dev->net, mii->phy_id,
1819 PHY_MODE_CTRL_STS, ret);
1821 /* enter SUSPEND1 mode */
1822 ret = smsc75xx_enter_suspend1(dev);
1823 goto done;
1827 if (pdata->wolopts & (WAKE_MCAST | WAKE_ARP)) {
1828 int i, filter = 0;
1830 /* disable all filters */
1831 for (i = 0; i < WUF_NUM; i++) {
1832 ret = smsc75xx_write_reg_nopm(dev, WUF_CFGX + i * 4, 0);
1833 if (ret < 0) {
1834 netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1835 goto done;
1839 if (pdata->wolopts & WAKE_MCAST) {
1840 const u8 mcast[] = {0x01, 0x00, 0x5E};
1841 netdev_info(dev->net, "enabling multicast detection\n");
1843 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_MULTICAST
1844 | smsc_crc(mcast, 3);
1845 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0007);
1846 if (ret < 0) {
1847 netdev_warn(dev->net, "Error writing wakeup filter\n");
1848 goto done;
1852 if (pdata->wolopts & WAKE_ARP) {
1853 const u8 arp[] = {0x08, 0x06};
1854 netdev_info(dev->net, "enabling ARP detection\n");
1856 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_ALL | (0x0C << 16)
1857 | smsc_crc(arp, 2);
1858 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0003);
1859 if (ret < 0) {
1860 netdev_warn(dev->net, "Error writing wakeup filter\n");
1861 goto done;
1865 /* clear any pending pattern match packet status */
1866 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1867 if (ret < 0) {
1868 netdev_warn(dev->net, "Error reading WUCSR\n");
1869 goto done;
1872 val |= WUCSR_WUFR;
1874 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1875 if (ret < 0) {
1876 netdev_warn(dev->net, "Error writing WUCSR\n");
1877 goto done;
1880 netdev_info(dev->net, "enabling packet match detection\n");
1881 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1882 if (ret < 0) {
1883 netdev_warn(dev->net, "Error reading WUCSR\n");
1884 goto done;
1887 val |= WUCSR_WUEN;
1889 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1890 if (ret < 0) {
1891 netdev_warn(dev->net, "Error writing WUCSR\n");
1892 goto done;
1894 } else {
1895 netdev_info(dev->net, "disabling packet match detection\n");
1896 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1897 if (ret < 0) {
1898 netdev_warn(dev->net, "Error reading WUCSR\n");
1899 goto done;
1902 val &= ~WUCSR_WUEN;
1904 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1905 if (ret < 0) {
1906 netdev_warn(dev->net, "Error writing WUCSR\n");
1907 goto done;
1911 /* disable magic, bcast & unicast wakeup sources */
1912 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1913 if (ret < 0) {
1914 netdev_warn(dev->net, "Error reading WUCSR\n");
1915 goto done;
1918 val &= ~(WUCSR_MPEN | WUCSR_BCST_EN | WUCSR_PFDA_EN);
1920 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1921 if (ret < 0) {
1922 netdev_warn(dev->net, "Error writing WUCSR\n");
1923 goto done;
1926 if (pdata->wolopts & WAKE_PHY) {
1927 netdev_info(dev->net, "enabling PHY wakeup\n");
1929 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1930 if (ret < 0) {
1931 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1932 goto done;
1935 /* clear wol status, enable energy detection */
1936 val &= ~PMT_CTL_WUPS;
1937 val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
1939 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1940 if (ret < 0) {
1941 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1942 goto done;
1946 if (pdata->wolopts & WAKE_MAGIC) {
1947 netdev_info(dev->net, "enabling magic packet wakeup\n");
1948 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1949 if (ret < 0) {
1950 netdev_warn(dev->net, "Error reading WUCSR\n");
1951 goto done;
1954 /* clear any pending magic packet status */
1955 val |= WUCSR_MPR | WUCSR_MPEN;
1957 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1958 if (ret < 0) {
1959 netdev_warn(dev->net, "Error writing WUCSR\n");
1960 goto done;
1964 if (pdata->wolopts & WAKE_BCAST) {
1965 netdev_info(dev->net, "enabling broadcast detection\n");
1966 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1967 if (ret < 0) {
1968 netdev_warn(dev->net, "Error reading WUCSR\n");
1969 goto done;
1972 val |= WUCSR_BCAST_FR | WUCSR_BCST_EN;
1974 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1975 if (ret < 0) {
1976 netdev_warn(dev->net, "Error writing WUCSR\n");
1977 goto done;
1981 if (pdata->wolopts & WAKE_UCAST) {
1982 netdev_info(dev->net, "enabling unicast detection\n");
1983 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1984 if (ret < 0) {
1985 netdev_warn(dev->net, "Error reading WUCSR\n");
1986 goto done;
1989 val |= WUCSR_WUFR | WUCSR_PFDA_EN;
1991 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1992 if (ret < 0) {
1993 netdev_warn(dev->net, "Error writing WUCSR\n");
1994 goto done;
1998 /* enable receiver to enable frame reception */
1999 ret = smsc75xx_read_reg_nopm(dev, MAC_RX, &val);
2000 if (ret < 0) {
2001 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
2002 goto done;
2005 val |= MAC_RX_RXEN;
2007 ret = smsc75xx_write_reg_nopm(dev, MAC_RX, val);
2008 if (ret < 0) {
2009 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
2010 goto done;
2013 /* some wol options are enabled, so enter SUSPEND0 */
2014 netdev_info(dev->net, "entering SUSPEND0 mode\n");
2015 ret = smsc75xx_enter_suspend0(dev);
2017 done:
2019 * TODO: resume() might need to handle the suspend failure
2020 * in system sleep
2022 if (ret && PMSG_IS_AUTO(message))
2023 usbnet_resume(intf);
2024 return ret;
2027 static int smsc75xx_resume(struct usb_interface *intf)
2029 struct usbnet *dev = usb_get_intfdata(intf);
2030 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
2031 u8 suspend_flags = pdata->suspend_flags;
2032 int ret;
2033 u32 val;
2035 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
2037 /* do this first to ensure it's cleared even in error case */
2038 pdata->suspend_flags = 0;
2040 if (suspend_flags & SUSPEND_ALLMODES) {
2041 /* Disable wakeup sources */
2042 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2043 if (ret < 0) {
2044 netdev_warn(dev->net, "Error reading WUCSR\n");
2045 return ret;
2048 val &= ~(WUCSR_WUEN | WUCSR_MPEN | WUCSR_PFDA_EN
2049 | WUCSR_BCST_EN);
2051 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2052 if (ret < 0) {
2053 netdev_warn(dev->net, "Error writing WUCSR\n");
2054 return ret;
2057 /* clear wake-up status */
2058 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
2059 if (ret < 0) {
2060 netdev_warn(dev->net, "Error reading PMT_CTL\n");
2061 return ret;
2064 val &= ~PMT_CTL_WOL_EN;
2065 val |= PMT_CTL_WUPS;
2067 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
2068 if (ret < 0) {
2069 netdev_warn(dev->net, "Error writing PMT_CTL\n");
2070 return ret;
2074 if (suspend_flags & SUSPEND_SUSPEND2) {
2075 netdev_info(dev->net, "resuming from SUSPEND2\n");
2077 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
2078 if (ret < 0) {
2079 netdev_warn(dev->net, "Error reading PMT_CTL\n");
2080 return ret;
2083 val |= PMT_CTL_PHY_PWRUP;
2085 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
2086 if (ret < 0) {
2087 netdev_warn(dev->net, "Error writing PMT_CTL\n");
2088 return ret;
2092 ret = smsc75xx_wait_ready(dev, 1);
2093 if (ret < 0) {
2094 netdev_warn(dev->net, "device not ready in smsc75xx_resume\n");
2095 return ret;
2098 return usbnet_resume(intf);
2101 static void smsc75xx_rx_csum_offload(struct usbnet *dev, struct sk_buff *skb,
2102 u32 rx_cmd_a, u32 rx_cmd_b)
2104 if (!(dev->net->features & NETIF_F_RXCSUM) ||
2105 unlikely(rx_cmd_a & RX_CMD_A_LCSM)) {
2106 skb->ip_summed = CHECKSUM_NONE;
2107 } else {
2108 skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT));
2109 skb->ip_summed = CHECKSUM_COMPLETE;
2113 static int smsc75xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
2115 while (skb->len > 0) {
2116 u32 rx_cmd_a, rx_cmd_b, align_count, size;
2117 struct sk_buff *ax_skb;
2118 unsigned char *packet;
2120 memcpy(&rx_cmd_a, skb->data, sizeof(rx_cmd_a));
2121 le32_to_cpus(&rx_cmd_a);
2122 skb_pull(skb, 4);
2124 memcpy(&rx_cmd_b, skb->data, sizeof(rx_cmd_b));
2125 le32_to_cpus(&rx_cmd_b);
2126 skb_pull(skb, 4 + RXW_PADDING);
2128 packet = skb->data;
2130 /* get the packet length */
2131 size = (rx_cmd_a & RX_CMD_A_LEN) - RXW_PADDING;
2132 align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
2134 if (unlikely(rx_cmd_a & RX_CMD_A_RED)) {
2135 netif_dbg(dev, rx_err, dev->net,
2136 "Error rx_cmd_a=0x%08x\n", rx_cmd_a);
2137 dev->net->stats.rx_errors++;
2138 dev->net->stats.rx_dropped++;
2140 if (rx_cmd_a & RX_CMD_A_FCS)
2141 dev->net->stats.rx_crc_errors++;
2142 else if (rx_cmd_a & (RX_CMD_A_LONG | RX_CMD_A_RUNT))
2143 dev->net->stats.rx_frame_errors++;
2144 } else {
2145 /* MAX_SINGLE_PACKET_SIZE + 4(CRC) + 2(COE) + 4(Vlan) */
2146 if (unlikely(size > (MAX_SINGLE_PACKET_SIZE + ETH_HLEN + 12))) {
2147 netif_dbg(dev, rx_err, dev->net,
2148 "size err rx_cmd_a=0x%08x\n",
2149 rx_cmd_a);
2150 return 0;
2153 /* last frame in this batch */
2154 if (skb->len == size) {
2155 smsc75xx_rx_csum_offload(dev, skb, rx_cmd_a,
2156 rx_cmd_b);
2158 skb_trim(skb, skb->len - 4); /* remove fcs */
2159 skb->truesize = size + sizeof(struct sk_buff);
2161 return 1;
2164 ax_skb = skb_clone(skb, GFP_ATOMIC);
2165 if (unlikely(!ax_skb)) {
2166 netdev_warn(dev->net, "Error allocating skb\n");
2167 return 0;
2170 ax_skb->len = size;
2171 ax_skb->data = packet;
2172 skb_set_tail_pointer(ax_skb, size);
2174 smsc75xx_rx_csum_offload(dev, ax_skb, rx_cmd_a,
2175 rx_cmd_b);
2177 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
2178 ax_skb->truesize = size + sizeof(struct sk_buff);
2180 usbnet_skb_return(dev, ax_skb);
2183 skb_pull(skb, size);
2185 /* padding bytes before the next frame starts */
2186 if (skb->len)
2187 skb_pull(skb, align_count);
2190 if (unlikely(skb->len < 0)) {
2191 netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
2192 return 0;
2195 return 1;
2198 static struct sk_buff *smsc75xx_tx_fixup(struct usbnet *dev,
2199 struct sk_buff *skb, gfp_t flags)
2201 u32 tx_cmd_a, tx_cmd_b;
2203 skb_linearize(skb);
2205 if (skb_headroom(skb) < SMSC75XX_TX_OVERHEAD) {
2206 struct sk_buff *skb2 =
2207 skb_copy_expand(skb, SMSC75XX_TX_OVERHEAD, 0, flags);
2208 dev_kfree_skb_any(skb);
2209 skb = skb2;
2210 if (!skb)
2211 return NULL;
2214 tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN) | TX_CMD_A_FCS;
2216 if (skb->ip_summed == CHECKSUM_PARTIAL)
2217 tx_cmd_a |= TX_CMD_A_IPE | TX_CMD_A_TPE;
2219 if (skb_is_gso(skb)) {
2220 u16 mss = max(skb_shinfo(skb)->gso_size, TX_MSS_MIN);
2221 tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT) & TX_CMD_B_MSS;
2223 tx_cmd_a |= TX_CMD_A_LSO;
2224 } else {
2225 tx_cmd_b = 0;
2228 skb_push(skb, 4);
2229 cpu_to_le32s(&tx_cmd_b);
2230 memcpy(skb->data, &tx_cmd_b, 4);
2232 skb_push(skb, 4);
2233 cpu_to_le32s(&tx_cmd_a);
2234 memcpy(skb->data, &tx_cmd_a, 4);
2236 return skb;
2239 static int smsc75xx_manage_power(struct usbnet *dev, int on)
2241 dev->intf->needs_remote_wakeup = on;
2242 return 0;
2245 static const struct driver_info smsc75xx_info = {
2246 .description = "smsc75xx USB 2.0 Gigabit Ethernet",
2247 .bind = smsc75xx_bind,
2248 .unbind = smsc75xx_unbind,
2249 .link_reset = smsc75xx_link_reset,
2250 .reset = smsc75xx_reset,
2251 .rx_fixup = smsc75xx_rx_fixup,
2252 .tx_fixup = smsc75xx_tx_fixup,
2253 .status = smsc75xx_status,
2254 .manage_power = smsc75xx_manage_power,
2255 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
2258 static const struct usb_device_id products[] = {
2260 /* SMSC7500 USB Gigabit Ethernet Device */
2261 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7500),
2262 .driver_info = (unsigned long) &smsc75xx_info,
2265 /* SMSC7500 USB Gigabit Ethernet Device */
2266 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7505),
2267 .driver_info = (unsigned long) &smsc75xx_info,
2269 { }, /* END */
2271 MODULE_DEVICE_TABLE(usb, products);
2273 static struct usb_driver smsc75xx_driver = {
2274 .name = SMSC_CHIPNAME,
2275 .id_table = products,
2276 .probe = usbnet_probe,
2277 .suspend = smsc75xx_suspend,
2278 .resume = smsc75xx_resume,
2279 .reset_resume = smsc75xx_resume,
2280 .disconnect = usbnet_disconnect,
2281 .disable_hub_initiated_lpm = 1,
2282 .supports_autosuspend = 1,
2285 module_usb_driver(smsc75xx_driver);
2287 MODULE_AUTHOR("Nancy Lin");
2288 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2289 MODULE_DESCRIPTION("SMSC75XX USB 2.0 Gigabit Ethernet Devices");
2290 MODULE_LICENSE("GPL");