Linux 3.16.62
[linux/fpc-iii.git] / drivers / net / usb / smsc75xx.c
blob157d4a9a0b3acaf6227e088715455119991492c7
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, 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 "smsc75xx.h"
34 #define SMSC_CHIPNAME "smsc75xx"
35 #define SMSC_DRIVER_VERSION "1.0.0"
36 #define HS_USB_PKT_SIZE (512)
37 #define FS_USB_PKT_SIZE (64)
38 #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
39 #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
40 #define DEFAULT_BULK_IN_DELAY (0x00002000)
41 #define MAX_SINGLE_PACKET_SIZE (9000)
42 #define LAN75XX_EEPROM_MAGIC (0x7500)
43 #define EEPROM_MAC_OFFSET (0x01)
44 #define DEFAULT_TX_CSUM_ENABLE (true)
45 #define DEFAULT_RX_CSUM_ENABLE (true)
46 #define SMSC75XX_INTERNAL_PHY_ID (1)
47 #define SMSC75XX_TX_OVERHEAD (8)
48 #define MAX_RX_FIFO_SIZE (20 * 1024)
49 #define MAX_TX_FIFO_SIZE (12 * 1024)
50 #define USB_VENDOR_ID_SMSC (0x0424)
51 #define USB_PRODUCT_ID_LAN7500 (0x7500)
52 #define USB_PRODUCT_ID_LAN7505 (0x7505)
53 #define RXW_PADDING 2
54 #define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
55 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
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 struct smsc75xx_priv {
65 struct usbnet *dev;
66 u32 rfe_ctl;
67 u32 wolopts;
68 u32 multicast_hash_table[DP_SEL_VHF_HASH_LEN];
69 struct mutex dataport_mutex;
70 spinlock_t rfe_ctl_lock;
71 struct work_struct set_multicast;
72 u8 suspend_flags;
75 struct usb_context {
76 struct usb_ctrlrequest req;
77 struct usbnet *dev;
80 static bool turbo_mode = true;
81 module_param(turbo_mode, bool, 0644);
82 MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
84 static int smsc75xx_link_ok_nopm(struct usbnet *dev);
85 static int smsc75xx_phy_gig_workaround(struct usbnet *dev);
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 if (wolinfo->wolopts & ~SUPPORTED_WAKE)
732 return -EINVAL;
734 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
736 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
737 if (ret < 0)
738 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
740 return ret;
743 static const struct ethtool_ops smsc75xx_ethtool_ops = {
744 .get_link = usbnet_get_link,
745 .nway_reset = usbnet_nway_reset,
746 .get_drvinfo = usbnet_get_drvinfo,
747 .get_msglevel = usbnet_get_msglevel,
748 .set_msglevel = usbnet_set_msglevel,
749 .get_settings = usbnet_get_settings,
750 .set_settings = usbnet_set_settings,
751 .get_eeprom_len = smsc75xx_ethtool_get_eeprom_len,
752 .get_eeprom = smsc75xx_ethtool_get_eeprom,
753 .set_eeprom = smsc75xx_ethtool_set_eeprom,
754 .get_wol = smsc75xx_ethtool_get_wol,
755 .set_wol = smsc75xx_ethtool_set_wol,
758 static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
760 struct usbnet *dev = netdev_priv(netdev);
762 if (!netif_running(netdev))
763 return -EINVAL;
765 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
768 static void smsc75xx_init_mac_address(struct usbnet *dev)
770 /* try reading mac address from EEPROM */
771 if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
772 dev->net->dev_addr) == 0) {
773 if (is_valid_ether_addr(dev->net->dev_addr)) {
774 /* eeprom values are valid so use them */
775 netif_dbg(dev, ifup, dev->net,
776 "MAC address read from EEPROM\n");
777 return;
781 /* no eeprom, or eeprom values are invalid. generate random MAC */
782 eth_hw_addr_random(dev->net);
783 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
786 static int smsc75xx_set_mac_address(struct usbnet *dev)
788 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
789 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
790 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
792 int ret = smsc75xx_write_reg(dev, RX_ADDRH, addr_hi);
793 if (ret < 0) {
794 netdev_warn(dev->net, "Failed to write RX_ADDRH: %d\n", ret);
795 return ret;
798 ret = smsc75xx_write_reg(dev, RX_ADDRL, addr_lo);
799 if (ret < 0) {
800 netdev_warn(dev->net, "Failed to write RX_ADDRL: %d\n", ret);
801 return ret;
804 addr_hi |= ADDR_FILTX_FB_VALID;
805 ret = smsc75xx_write_reg(dev, ADDR_FILTX, addr_hi);
806 if (ret < 0) {
807 netdev_warn(dev->net, "Failed to write ADDR_FILTX: %d\n", ret);
808 return ret;
811 ret = smsc75xx_write_reg(dev, ADDR_FILTX + 4, addr_lo);
812 if (ret < 0)
813 netdev_warn(dev->net, "Failed to write ADDR_FILTX+4: %d\n", ret);
815 return ret;
818 static int smsc75xx_phy_initialize(struct usbnet *dev)
820 int bmcr, ret, timeout = 0;
822 /* Initialize MII structure */
823 dev->mii.dev = dev->net;
824 dev->mii.mdio_read = smsc75xx_mdio_read;
825 dev->mii.mdio_write = smsc75xx_mdio_write;
826 dev->mii.phy_id_mask = 0x1f;
827 dev->mii.reg_num_mask = 0x1f;
828 dev->mii.supports_gmii = 1;
829 dev->mii.phy_id = SMSC75XX_INTERNAL_PHY_ID;
831 /* reset phy and wait for reset to complete */
832 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
834 do {
835 msleep(10);
836 bmcr = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
837 if (bmcr < 0) {
838 netdev_warn(dev->net, "Error reading MII_BMCR\n");
839 return bmcr;
841 timeout++;
842 } while ((bmcr & BMCR_RESET) && (timeout < 100));
844 if (timeout >= 100) {
845 netdev_warn(dev->net, "timeout on PHY Reset\n");
846 return -EIO;
849 /* phy workaround for gig link */
850 smsc75xx_phy_gig_workaround(dev);
852 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
853 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
854 ADVERTISE_PAUSE_ASYM);
855 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
856 ADVERTISE_1000FULL);
858 /* read and write to clear phy interrupt status */
859 ret = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
860 if (ret < 0) {
861 netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
862 return ret;
865 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_SRC, 0xffff);
867 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
868 PHY_INT_MASK_DEFAULT);
869 mii_nway_restart(&dev->mii);
871 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
872 return 0;
875 static int smsc75xx_set_rx_max_frame_length(struct usbnet *dev, int size)
877 int ret = 0;
878 u32 buf;
879 bool rxenabled;
881 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
882 if (ret < 0) {
883 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
884 return ret;
887 rxenabled = ((buf & MAC_RX_RXEN) != 0);
889 if (rxenabled) {
890 buf &= ~MAC_RX_RXEN;
891 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
892 if (ret < 0) {
893 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
894 return ret;
898 /* add 4 to size for FCS */
899 buf &= ~MAC_RX_MAX_SIZE;
900 buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT) & MAC_RX_MAX_SIZE);
902 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
903 if (ret < 0) {
904 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
905 return ret;
908 if (rxenabled) {
909 buf |= MAC_RX_RXEN;
910 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
911 if (ret < 0) {
912 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
913 return ret;
917 return 0;
920 static int smsc75xx_change_mtu(struct net_device *netdev, int new_mtu)
922 struct usbnet *dev = netdev_priv(netdev);
923 int ret;
925 if (new_mtu > MAX_SINGLE_PACKET_SIZE)
926 return -EINVAL;
928 ret = smsc75xx_set_rx_max_frame_length(dev, new_mtu + ETH_HLEN);
929 if (ret < 0) {
930 netdev_warn(dev->net, "Failed to set mac rx frame length\n");
931 return ret;
934 return usbnet_change_mtu(netdev, new_mtu);
937 /* Enable or disable Rx checksum offload engine */
938 static int smsc75xx_set_features(struct net_device *netdev,
939 netdev_features_t features)
941 struct usbnet *dev = netdev_priv(netdev);
942 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
943 unsigned long flags;
944 int ret;
946 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
948 if (features & NETIF_F_RXCSUM)
949 pdata->rfe_ctl |= RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM;
950 else
951 pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM);
953 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
954 /* it's racing here! */
956 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
957 if (ret < 0)
958 netdev_warn(dev->net, "Error writing RFE_CTL\n");
960 return ret;
963 static int smsc75xx_wait_ready(struct usbnet *dev, int in_pm)
965 int timeout = 0;
967 do {
968 u32 buf;
969 int ret;
971 ret = __smsc75xx_read_reg(dev, PMT_CTL, &buf, in_pm);
973 if (ret < 0) {
974 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
975 return ret;
978 if (buf & PMT_CTL_DEV_RDY)
979 return 0;
981 msleep(10);
982 timeout++;
983 } while (timeout < 100);
985 netdev_warn(dev->net, "timeout waiting for device ready\n");
986 return -EIO;
989 static int smsc75xx_phy_gig_workaround(struct usbnet *dev)
991 struct mii_if_info *mii = &dev->mii;
992 int ret = 0, timeout = 0;
993 u32 buf, link_up = 0;
995 /* Set the phy in Gig loopback */
996 smsc75xx_mdio_write(dev->net, mii->phy_id, MII_BMCR, 0x4040);
998 /* Wait for the link up */
999 do {
1000 link_up = smsc75xx_link_ok_nopm(dev);
1001 usleep_range(10000, 20000);
1002 timeout++;
1003 } while ((!link_up) && (timeout < 1000));
1005 if (timeout >= 1000) {
1006 netdev_warn(dev->net, "Timeout waiting for PHY link up\n");
1007 return -EIO;
1010 /* phy reset */
1011 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1012 if (ret < 0) {
1013 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1014 return ret;
1017 buf |= PMT_CTL_PHY_RST;
1019 ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
1020 if (ret < 0) {
1021 netdev_warn(dev->net, "Failed to write PMT_CTL: %d\n", ret);
1022 return ret;
1025 timeout = 0;
1026 do {
1027 usleep_range(10000, 20000);
1028 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1029 if (ret < 0) {
1030 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n",
1031 ret);
1032 return ret;
1034 timeout++;
1035 } while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
1037 if (timeout >= 100) {
1038 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
1039 return -EIO;
1042 return 0;
1045 static int smsc75xx_reset(struct usbnet *dev)
1047 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1048 u32 buf;
1049 int ret = 0, timeout;
1051 netif_dbg(dev, ifup, dev->net, "entering smsc75xx_reset\n");
1053 ret = smsc75xx_wait_ready(dev, 0);
1054 if (ret < 0) {
1055 netdev_warn(dev->net, "device not ready in smsc75xx_reset\n");
1056 return ret;
1059 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1060 if (ret < 0) {
1061 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1062 return ret;
1065 buf |= HW_CFG_LRST;
1067 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1068 if (ret < 0) {
1069 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1070 return ret;
1073 timeout = 0;
1074 do {
1075 msleep(10);
1076 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1077 if (ret < 0) {
1078 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1079 return ret;
1081 timeout++;
1082 } while ((buf & HW_CFG_LRST) && (timeout < 100));
1084 if (timeout >= 100) {
1085 netdev_warn(dev->net, "timeout on completion of Lite Reset\n");
1086 return -EIO;
1089 netif_dbg(dev, ifup, dev->net, "Lite reset complete, resetting PHY\n");
1091 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1092 if (ret < 0) {
1093 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1094 return ret;
1097 buf |= PMT_CTL_PHY_RST;
1099 ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
1100 if (ret < 0) {
1101 netdev_warn(dev->net, "Failed to write PMT_CTL: %d\n", ret);
1102 return ret;
1105 timeout = 0;
1106 do {
1107 msleep(10);
1108 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1109 if (ret < 0) {
1110 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1111 return ret;
1113 timeout++;
1114 } while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
1116 if (timeout >= 100) {
1117 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
1118 return -EIO;
1121 netif_dbg(dev, ifup, dev->net, "PHY reset complete\n");
1123 ret = smsc75xx_set_mac_address(dev);
1124 if (ret < 0) {
1125 netdev_warn(dev->net, "Failed to set mac address\n");
1126 return ret;
1129 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
1130 dev->net->dev_addr);
1132 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1133 if (ret < 0) {
1134 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1135 return ret;
1138 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
1139 buf);
1141 buf |= HW_CFG_BIR;
1143 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1144 if (ret < 0) {
1145 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1146 return ret;
1149 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1150 if (ret < 0) {
1151 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1152 return ret;
1155 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG after writing HW_CFG_BIR: 0x%08x\n",
1156 buf);
1158 if (!turbo_mode) {
1159 buf = 0;
1160 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1161 } else if (dev->udev->speed == USB_SPEED_HIGH) {
1162 buf = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1163 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1164 } else {
1165 buf = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1166 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1169 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1170 (ulong)dev->rx_urb_size);
1172 ret = smsc75xx_write_reg(dev, BURST_CAP, buf);
1173 if (ret < 0) {
1174 netdev_warn(dev->net, "Failed to write BURST_CAP: %d\n", ret);
1175 return ret;
1178 ret = smsc75xx_read_reg(dev, BURST_CAP, &buf);
1179 if (ret < 0) {
1180 netdev_warn(dev->net, "Failed to read BURST_CAP: %d\n", ret);
1181 return ret;
1184 netif_dbg(dev, ifup, dev->net,
1185 "Read Value from BURST_CAP after writing: 0x%08x\n", buf);
1187 ret = smsc75xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1188 if (ret < 0) {
1189 netdev_warn(dev->net, "Failed to write BULK_IN_DLY: %d\n", ret);
1190 return ret;
1193 ret = smsc75xx_read_reg(dev, BULK_IN_DLY, &buf);
1194 if (ret < 0) {
1195 netdev_warn(dev->net, "Failed to read BULK_IN_DLY: %d\n", ret);
1196 return ret;
1199 netif_dbg(dev, ifup, dev->net,
1200 "Read Value from BULK_IN_DLY after writing: 0x%08x\n", buf);
1202 if (turbo_mode) {
1203 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1204 if (ret < 0) {
1205 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1206 return ret;
1209 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
1211 buf |= (HW_CFG_MEF | HW_CFG_BCE);
1213 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1214 if (ret < 0) {
1215 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1216 return ret;
1219 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1220 if (ret < 0) {
1221 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1222 return ret;
1225 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
1228 /* set FIFO sizes */
1229 buf = (MAX_RX_FIFO_SIZE - 512) / 512;
1230 ret = smsc75xx_write_reg(dev, FCT_RX_FIFO_END, buf);
1231 if (ret < 0) {
1232 netdev_warn(dev->net, "Failed to write FCT_RX_FIFO_END: %d\n", ret);
1233 return ret;
1236 netif_dbg(dev, ifup, dev->net, "FCT_RX_FIFO_END set to 0x%08x\n", buf);
1238 buf = (MAX_TX_FIFO_SIZE - 512) / 512;
1239 ret = smsc75xx_write_reg(dev, FCT_TX_FIFO_END, buf);
1240 if (ret < 0) {
1241 netdev_warn(dev->net, "Failed to write FCT_TX_FIFO_END: %d\n", ret);
1242 return ret;
1245 netif_dbg(dev, ifup, dev->net, "FCT_TX_FIFO_END set to 0x%08x\n", buf);
1247 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
1248 if (ret < 0) {
1249 netdev_warn(dev->net, "Failed to write INT_STS: %d\n", ret);
1250 return ret;
1253 ret = smsc75xx_read_reg(dev, ID_REV, &buf);
1254 if (ret < 0) {
1255 netdev_warn(dev->net, "Failed to read ID_REV: %d\n", ret);
1256 return ret;
1259 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", buf);
1261 ret = smsc75xx_read_reg(dev, E2P_CMD, &buf);
1262 if (ret < 0) {
1263 netdev_warn(dev->net, "Failed to read E2P_CMD: %d\n", ret);
1264 return ret;
1267 /* only set default GPIO/LED settings if no EEPROM is detected */
1268 if (!(buf & E2P_CMD_LOADED)) {
1269 ret = smsc75xx_read_reg(dev, LED_GPIO_CFG, &buf);
1270 if (ret < 0) {
1271 netdev_warn(dev->net, "Failed to read LED_GPIO_CFG: %d\n", ret);
1272 return ret;
1275 buf &= ~(LED_GPIO_CFG_LED2_FUN_SEL | LED_GPIO_CFG_LED10_FUN_SEL);
1276 buf |= LED_GPIO_CFG_LEDGPIO_EN | LED_GPIO_CFG_LED2_FUN_SEL;
1278 ret = smsc75xx_write_reg(dev, LED_GPIO_CFG, buf);
1279 if (ret < 0) {
1280 netdev_warn(dev->net, "Failed to write LED_GPIO_CFG: %d\n", ret);
1281 return ret;
1285 ret = smsc75xx_write_reg(dev, FLOW, 0);
1286 if (ret < 0) {
1287 netdev_warn(dev->net, "Failed to write FLOW: %d\n", ret);
1288 return ret;
1291 ret = smsc75xx_write_reg(dev, FCT_FLOW, 0);
1292 if (ret < 0) {
1293 netdev_warn(dev->net, "Failed to write FCT_FLOW: %d\n", ret);
1294 return ret;
1297 /* Don't need rfe_ctl_lock during initialisation */
1298 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
1299 if (ret < 0) {
1300 netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1301 return ret;
1304 pdata->rfe_ctl |= RFE_CTL_AB | RFE_CTL_DPF;
1306 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
1307 if (ret < 0) {
1308 netdev_warn(dev->net, "Failed to write RFE_CTL: %d\n", ret);
1309 return ret;
1312 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
1313 if (ret < 0) {
1314 netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1315 return ret;
1318 netif_dbg(dev, ifup, dev->net, "RFE_CTL set to 0x%08x\n",
1319 pdata->rfe_ctl);
1321 /* Enable or disable checksum offload engines */
1322 smsc75xx_set_features(dev->net, dev->net->features);
1324 smsc75xx_set_multicast(dev->net);
1326 ret = smsc75xx_phy_initialize(dev);
1327 if (ret < 0) {
1328 netdev_warn(dev->net, "Failed to initialize PHY: %d\n", ret);
1329 return ret;
1332 ret = smsc75xx_read_reg(dev, INT_EP_CTL, &buf);
1333 if (ret < 0) {
1334 netdev_warn(dev->net, "Failed to read INT_EP_CTL: %d\n", ret);
1335 return ret;
1338 /* enable PHY interrupts */
1339 buf |= INT_ENP_PHY_INT;
1341 ret = smsc75xx_write_reg(dev, INT_EP_CTL, buf);
1342 if (ret < 0) {
1343 netdev_warn(dev->net, "Failed to write INT_EP_CTL: %d\n", ret);
1344 return ret;
1347 /* allow mac to detect speed and duplex from phy */
1348 ret = smsc75xx_read_reg(dev, MAC_CR, &buf);
1349 if (ret < 0) {
1350 netdev_warn(dev->net, "Failed to read MAC_CR: %d\n", ret);
1351 return ret;
1354 buf |= (MAC_CR_ADD | MAC_CR_ASD);
1355 ret = smsc75xx_write_reg(dev, MAC_CR, buf);
1356 if (ret < 0) {
1357 netdev_warn(dev->net, "Failed to write MAC_CR: %d\n", ret);
1358 return ret;
1361 ret = smsc75xx_read_reg(dev, MAC_TX, &buf);
1362 if (ret < 0) {
1363 netdev_warn(dev->net, "Failed to read MAC_TX: %d\n", ret);
1364 return ret;
1367 buf |= MAC_TX_TXEN;
1369 ret = smsc75xx_write_reg(dev, MAC_TX, buf);
1370 if (ret < 0) {
1371 netdev_warn(dev->net, "Failed to write MAC_TX: %d\n", ret);
1372 return ret;
1375 netif_dbg(dev, ifup, dev->net, "MAC_TX set to 0x%08x\n", buf);
1377 ret = smsc75xx_read_reg(dev, FCT_TX_CTL, &buf);
1378 if (ret < 0) {
1379 netdev_warn(dev->net, "Failed to read FCT_TX_CTL: %d\n", ret);
1380 return ret;
1383 buf |= FCT_TX_CTL_EN;
1385 ret = smsc75xx_write_reg(dev, FCT_TX_CTL, buf);
1386 if (ret < 0) {
1387 netdev_warn(dev->net, "Failed to write FCT_TX_CTL: %d\n", ret);
1388 return ret;
1391 netif_dbg(dev, ifup, dev->net, "FCT_TX_CTL set to 0x%08x\n", buf);
1393 ret = smsc75xx_set_rx_max_frame_length(dev, dev->net->mtu + ETH_HLEN);
1394 if (ret < 0) {
1395 netdev_warn(dev->net, "Failed to set max rx frame length\n");
1396 return ret;
1399 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
1400 if (ret < 0) {
1401 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
1402 return ret;
1405 buf |= MAC_RX_RXEN;
1407 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
1408 if (ret < 0) {
1409 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
1410 return ret;
1413 netif_dbg(dev, ifup, dev->net, "MAC_RX set to 0x%08x\n", buf);
1415 ret = smsc75xx_read_reg(dev, FCT_RX_CTL, &buf);
1416 if (ret < 0) {
1417 netdev_warn(dev->net, "Failed to read FCT_RX_CTL: %d\n", ret);
1418 return ret;
1421 buf |= FCT_RX_CTL_EN;
1423 ret = smsc75xx_write_reg(dev, FCT_RX_CTL, buf);
1424 if (ret < 0) {
1425 netdev_warn(dev->net, "Failed to write FCT_RX_CTL: %d\n", ret);
1426 return ret;
1429 netif_dbg(dev, ifup, dev->net, "FCT_RX_CTL set to 0x%08x\n", buf);
1431 netif_dbg(dev, ifup, dev->net, "smsc75xx_reset, return 0\n");
1432 return 0;
1435 static const struct net_device_ops smsc75xx_netdev_ops = {
1436 .ndo_open = usbnet_open,
1437 .ndo_stop = usbnet_stop,
1438 .ndo_start_xmit = usbnet_start_xmit,
1439 .ndo_tx_timeout = usbnet_tx_timeout,
1440 .ndo_change_mtu = smsc75xx_change_mtu,
1441 .ndo_set_mac_address = eth_mac_addr,
1442 .ndo_validate_addr = eth_validate_addr,
1443 .ndo_do_ioctl = smsc75xx_ioctl,
1444 .ndo_set_rx_mode = smsc75xx_set_multicast,
1445 .ndo_set_features = smsc75xx_set_features,
1448 static int smsc75xx_bind(struct usbnet *dev, struct usb_interface *intf)
1450 struct smsc75xx_priv *pdata = NULL;
1451 int ret;
1453 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1455 ret = usbnet_get_endpoints(dev, intf);
1456 if (ret < 0) {
1457 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1458 return ret;
1461 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc75xx_priv),
1462 GFP_KERNEL);
1464 pdata = (struct smsc75xx_priv *)(dev->data[0]);
1465 if (!pdata)
1466 return -ENOMEM;
1468 pdata->dev = dev;
1470 spin_lock_init(&pdata->rfe_ctl_lock);
1471 mutex_init(&pdata->dataport_mutex);
1473 INIT_WORK(&pdata->set_multicast, smsc75xx_deferred_multicast_write);
1475 if (DEFAULT_TX_CSUM_ENABLE)
1476 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1478 if (DEFAULT_RX_CSUM_ENABLE)
1479 dev->net->features |= NETIF_F_RXCSUM;
1481 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1482 NETIF_F_RXCSUM;
1484 ret = smsc75xx_wait_ready(dev, 0);
1485 if (ret < 0) {
1486 netdev_warn(dev->net, "device not ready in smsc75xx_bind\n");
1487 return ret;
1490 smsc75xx_init_mac_address(dev);
1492 /* Init all registers */
1493 ret = smsc75xx_reset(dev);
1494 if (ret < 0) {
1495 netdev_warn(dev->net, "smsc75xx_reset error %d\n", ret);
1496 return ret;
1499 dev->net->netdev_ops = &smsc75xx_netdev_ops;
1500 dev->net->ethtool_ops = &smsc75xx_ethtool_ops;
1501 dev->net->flags |= IFF_MULTICAST;
1502 dev->net->hard_header_len += SMSC75XX_TX_OVERHEAD;
1503 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1504 return 0;
1507 static void smsc75xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1509 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1510 if (pdata) {
1511 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1512 kfree(pdata);
1513 pdata = NULL;
1514 dev->data[0] = 0;
1518 static u16 smsc_crc(const u8 *buffer, size_t len)
1520 return bitrev16(crc16(0xFFFF, buffer, len));
1523 static int smsc75xx_write_wuff(struct usbnet *dev, int filter, u32 wuf_cfg,
1524 u32 wuf_mask1)
1526 int cfg_base = WUF_CFGX + filter * 4;
1527 int mask_base = WUF_MASKX + filter * 16;
1528 int ret;
1530 ret = smsc75xx_write_reg(dev, cfg_base, wuf_cfg);
1531 if (ret < 0) {
1532 netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1533 return ret;
1536 ret = smsc75xx_write_reg(dev, mask_base, wuf_mask1);
1537 if (ret < 0) {
1538 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1539 return ret;
1542 ret = smsc75xx_write_reg(dev, mask_base + 4, 0);
1543 if (ret < 0) {
1544 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1545 return ret;
1548 ret = smsc75xx_write_reg(dev, mask_base + 8, 0);
1549 if (ret < 0) {
1550 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1551 return ret;
1554 ret = smsc75xx_write_reg(dev, mask_base + 12, 0);
1555 if (ret < 0) {
1556 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1557 return ret;
1560 return 0;
1563 static int smsc75xx_enter_suspend0(struct usbnet *dev)
1565 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1566 u32 val;
1567 int ret;
1569 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1570 if (ret < 0) {
1571 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1572 return ret;
1575 val &= (~(PMT_CTL_SUS_MODE | PMT_CTL_PHY_RST));
1576 val |= PMT_CTL_SUS_MODE_0 | PMT_CTL_WOL_EN | PMT_CTL_WUPS;
1578 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1579 if (ret < 0) {
1580 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1581 return ret;
1584 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1586 return 0;
1589 static int smsc75xx_enter_suspend1(struct usbnet *dev)
1591 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1592 u32 val;
1593 int ret;
1595 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1596 if (ret < 0) {
1597 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1598 return ret;
1601 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1602 val |= PMT_CTL_SUS_MODE_1;
1604 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1605 if (ret < 0) {
1606 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1607 return ret;
1610 /* clear wol status, enable energy detection */
1611 val &= ~PMT_CTL_WUPS;
1612 val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
1614 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1615 if (ret < 0) {
1616 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1617 return ret;
1620 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1622 return 0;
1625 static int smsc75xx_enter_suspend2(struct usbnet *dev)
1627 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1628 u32 val;
1629 int ret;
1631 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1632 if (ret < 0) {
1633 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1634 return ret;
1637 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1638 val |= PMT_CTL_SUS_MODE_2;
1640 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1641 if (ret < 0) {
1642 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1643 return ret;
1646 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1648 return 0;
1651 static int smsc75xx_enter_suspend3(struct usbnet *dev)
1653 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1654 u32 val;
1655 int ret;
1657 ret = smsc75xx_read_reg_nopm(dev, FCT_RX_CTL, &val);
1658 if (ret < 0) {
1659 netdev_warn(dev->net, "Error reading FCT_RX_CTL\n");
1660 return ret;
1663 if (val & FCT_RX_CTL_RXUSED) {
1664 netdev_dbg(dev->net, "rx fifo not empty in autosuspend\n");
1665 return -EBUSY;
1668 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1669 if (ret < 0) {
1670 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1671 return ret;
1674 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1675 val |= PMT_CTL_SUS_MODE_3 | PMT_CTL_RES_CLR_WKP_EN;
1677 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1678 if (ret < 0) {
1679 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1680 return ret;
1683 /* clear wol status */
1684 val &= ~PMT_CTL_WUPS;
1685 val |= PMT_CTL_WUPS_WOL;
1687 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1688 if (ret < 0) {
1689 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1690 return ret;
1693 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1695 return 0;
1698 static int smsc75xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1700 struct mii_if_info *mii = &dev->mii;
1701 int ret;
1703 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1705 /* read to clear */
1706 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1707 if (ret < 0) {
1708 netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
1709 return ret;
1712 /* enable interrupt source */
1713 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1714 if (ret < 0) {
1715 netdev_warn(dev->net, "Error reading PHY_INT_MASK\n");
1716 return ret;
1719 ret |= mask;
1721 smsc75xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1723 return 0;
1726 static int smsc75xx_link_ok_nopm(struct usbnet *dev)
1728 struct mii_if_info *mii = &dev->mii;
1729 int ret;
1731 /* first, a dummy read, needed to latch some MII phys */
1732 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1733 if (ret < 0) {
1734 netdev_warn(dev->net, "Error reading MII_BMSR\n");
1735 return ret;
1738 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1739 if (ret < 0) {
1740 netdev_warn(dev->net, "Error reading MII_BMSR\n");
1741 return ret;
1744 return !!(ret & BMSR_LSTATUS);
1747 static int smsc75xx_autosuspend(struct usbnet *dev, u32 link_up)
1749 int ret;
1751 if (!netif_running(dev->net)) {
1752 /* interface is ifconfig down so fully power down hw */
1753 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1754 return smsc75xx_enter_suspend2(dev);
1757 if (!link_up) {
1758 /* link is down so enter EDPD mode */
1759 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1761 /* enable PHY wakeup events for if cable is attached */
1762 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1763 PHY_INT_MASK_ANEG_COMP);
1764 if (ret < 0) {
1765 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1766 return ret;
1769 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1770 return smsc75xx_enter_suspend1(dev);
1773 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1774 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1775 PHY_INT_MASK_LINK_DOWN);
1776 if (ret < 0) {
1777 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1778 return ret;
1781 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1782 return smsc75xx_enter_suspend3(dev);
1785 static int smsc75xx_suspend(struct usb_interface *intf, pm_message_t message)
1787 struct usbnet *dev = usb_get_intfdata(intf);
1788 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1789 u32 val, link_up;
1790 int ret;
1792 ret = usbnet_suspend(intf, message);
1793 if (ret < 0) {
1794 netdev_warn(dev->net, "usbnet_suspend error\n");
1795 return ret;
1798 if (pdata->suspend_flags) {
1799 netdev_warn(dev->net, "error during last resume\n");
1800 pdata->suspend_flags = 0;
1803 /* determine if link is up using only _nopm functions */
1804 link_up = smsc75xx_link_ok_nopm(dev);
1806 if (message.event == PM_EVENT_AUTO_SUSPEND) {
1807 ret = smsc75xx_autosuspend(dev, link_up);
1808 goto done;
1811 /* if we get this far we're not autosuspending */
1812 /* if no wol options set, or if link is down and we're not waking on
1813 * PHY activity, enter lowest power SUSPEND2 mode
1815 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1816 !(link_up || (pdata->wolopts & WAKE_PHY))) {
1817 netdev_info(dev->net, "entering SUSPEND2 mode\n");
1819 /* disable energy detect (link up) & wake up events */
1820 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1821 if (ret < 0) {
1822 netdev_warn(dev->net, "Error reading WUCSR\n");
1823 goto done;
1826 val &= ~(WUCSR_MPEN | WUCSR_WUEN);
1828 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1829 if (ret < 0) {
1830 netdev_warn(dev->net, "Error writing WUCSR\n");
1831 goto done;
1834 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1835 if (ret < 0) {
1836 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1837 goto done;
1840 val &= ~(PMT_CTL_ED_EN | PMT_CTL_WOL_EN);
1842 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1843 if (ret < 0) {
1844 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1845 goto done;
1848 ret = smsc75xx_enter_suspend2(dev);
1849 goto done;
1852 if (pdata->wolopts & WAKE_PHY) {
1853 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1854 (PHY_INT_MASK_ANEG_COMP | PHY_INT_MASK_LINK_DOWN));
1855 if (ret < 0) {
1856 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1857 goto done;
1860 /* if link is down then configure EDPD and enter SUSPEND1,
1861 * otherwise enter SUSPEND0 below
1863 if (!link_up) {
1864 struct mii_if_info *mii = &dev->mii;
1865 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1867 /* enable energy detect power-down mode */
1868 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id,
1869 PHY_MODE_CTRL_STS);
1870 if (ret < 0) {
1871 netdev_warn(dev->net, "Error reading PHY_MODE_CTRL_STS\n");
1872 goto done;
1875 ret |= MODE_CTRL_STS_EDPWRDOWN;
1877 smsc75xx_mdio_write_nopm(dev->net, mii->phy_id,
1878 PHY_MODE_CTRL_STS, ret);
1880 /* enter SUSPEND1 mode */
1881 ret = smsc75xx_enter_suspend1(dev);
1882 goto done;
1886 if (pdata->wolopts & (WAKE_MCAST | WAKE_ARP)) {
1887 int i, filter = 0;
1889 /* disable all filters */
1890 for (i = 0; i < WUF_NUM; i++) {
1891 ret = smsc75xx_write_reg_nopm(dev, WUF_CFGX + i * 4, 0);
1892 if (ret < 0) {
1893 netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1894 goto done;
1898 if (pdata->wolopts & WAKE_MCAST) {
1899 const u8 mcast[] = {0x01, 0x00, 0x5E};
1900 netdev_info(dev->net, "enabling multicast detection\n");
1902 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_MULTICAST
1903 | smsc_crc(mcast, 3);
1904 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0007);
1905 if (ret < 0) {
1906 netdev_warn(dev->net, "Error writing wakeup filter\n");
1907 goto done;
1911 if (pdata->wolopts & WAKE_ARP) {
1912 const u8 arp[] = {0x08, 0x06};
1913 netdev_info(dev->net, "enabling ARP detection\n");
1915 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_ALL | (0x0C << 16)
1916 | smsc_crc(arp, 2);
1917 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0003);
1918 if (ret < 0) {
1919 netdev_warn(dev->net, "Error writing wakeup filter\n");
1920 goto done;
1924 /* clear any pending pattern match packet status */
1925 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1926 if (ret < 0) {
1927 netdev_warn(dev->net, "Error reading WUCSR\n");
1928 goto done;
1931 val |= WUCSR_WUFR;
1933 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1934 if (ret < 0) {
1935 netdev_warn(dev->net, "Error writing WUCSR\n");
1936 goto done;
1939 netdev_info(dev->net, "enabling packet match detection\n");
1940 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1941 if (ret < 0) {
1942 netdev_warn(dev->net, "Error reading WUCSR\n");
1943 goto done;
1946 val |= WUCSR_WUEN;
1948 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1949 if (ret < 0) {
1950 netdev_warn(dev->net, "Error writing WUCSR\n");
1951 goto done;
1953 } else {
1954 netdev_info(dev->net, "disabling packet match detection\n");
1955 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1956 if (ret < 0) {
1957 netdev_warn(dev->net, "Error reading WUCSR\n");
1958 goto done;
1961 val &= ~WUCSR_WUEN;
1963 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1964 if (ret < 0) {
1965 netdev_warn(dev->net, "Error writing WUCSR\n");
1966 goto done;
1970 /* disable magic, bcast & unicast wakeup sources */
1971 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1972 if (ret < 0) {
1973 netdev_warn(dev->net, "Error reading WUCSR\n");
1974 goto done;
1977 val &= ~(WUCSR_MPEN | WUCSR_BCST_EN | WUCSR_PFDA_EN);
1979 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1980 if (ret < 0) {
1981 netdev_warn(dev->net, "Error writing WUCSR\n");
1982 goto done;
1985 if (pdata->wolopts & WAKE_PHY) {
1986 netdev_info(dev->net, "enabling PHY wakeup\n");
1988 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1989 if (ret < 0) {
1990 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1991 goto done;
1994 /* clear wol status, enable energy detection */
1995 val &= ~PMT_CTL_WUPS;
1996 val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
1998 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1999 if (ret < 0) {
2000 netdev_warn(dev->net, "Error writing PMT_CTL\n");
2001 goto done;
2005 if (pdata->wolopts & WAKE_MAGIC) {
2006 netdev_info(dev->net, "enabling magic packet wakeup\n");
2007 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2008 if (ret < 0) {
2009 netdev_warn(dev->net, "Error reading WUCSR\n");
2010 goto done;
2013 /* clear any pending magic packet status */
2014 val |= WUCSR_MPR | WUCSR_MPEN;
2016 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2017 if (ret < 0) {
2018 netdev_warn(dev->net, "Error writing WUCSR\n");
2019 goto done;
2023 if (pdata->wolopts & WAKE_BCAST) {
2024 netdev_info(dev->net, "enabling broadcast detection\n");
2025 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2026 if (ret < 0) {
2027 netdev_warn(dev->net, "Error reading WUCSR\n");
2028 goto done;
2031 val |= WUCSR_BCAST_FR | WUCSR_BCST_EN;
2033 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2034 if (ret < 0) {
2035 netdev_warn(dev->net, "Error writing WUCSR\n");
2036 goto done;
2040 if (pdata->wolopts & WAKE_UCAST) {
2041 netdev_info(dev->net, "enabling unicast detection\n");
2042 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2043 if (ret < 0) {
2044 netdev_warn(dev->net, "Error reading WUCSR\n");
2045 goto done;
2048 val |= WUCSR_WUFR | WUCSR_PFDA_EN;
2050 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2051 if (ret < 0) {
2052 netdev_warn(dev->net, "Error writing WUCSR\n");
2053 goto done;
2057 /* enable receiver to enable frame reception */
2058 ret = smsc75xx_read_reg_nopm(dev, MAC_RX, &val);
2059 if (ret < 0) {
2060 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
2061 goto done;
2064 val |= MAC_RX_RXEN;
2066 ret = smsc75xx_write_reg_nopm(dev, MAC_RX, val);
2067 if (ret < 0) {
2068 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
2069 goto done;
2072 /* some wol options are enabled, so enter SUSPEND0 */
2073 netdev_info(dev->net, "entering SUSPEND0 mode\n");
2074 ret = smsc75xx_enter_suspend0(dev);
2076 done:
2078 * TODO: resume() might need to handle the suspend failure
2079 * in system sleep
2081 if (ret && PMSG_IS_AUTO(message))
2082 usbnet_resume(intf);
2083 return ret;
2086 static int smsc75xx_resume(struct usb_interface *intf)
2088 struct usbnet *dev = usb_get_intfdata(intf);
2089 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
2090 u8 suspend_flags = pdata->suspend_flags;
2091 int ret;
2092 u32 val;
2094 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
2096 /* do this first to ensure it's cleared even in error case */
2097 pdata->suspend_flags = 0;
2099 if (suspend_flags & SUSPEND_ALLMODES) {
2100 /* Disable wakeup sources */
2101 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2102 if (ret < 0) {
2103 netdev_warn(dev->net, "Error reading WUCSR\n");
2104 return ret;
2107 val &= ~(WUCSR_WUEN | WUCSR_MPEN | WUCSR_PFDA_EN
2108 | WUCSR_BCST_EN);
2110 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2111 if (ret < 0) {
2112 netdev_warn(dev->net, "Error writing WUCSR\n");
2113 return ret;
2116 /* clear wake-up status */
2117 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
2118 if (ret < 0) {
2119 netdev_warn(dev->net, "Error reading PMT_CTL\n");
2120 return ret;
2123 val &= ~PMT_CTL_WOL_EN;
2124 val |= PMT_CTL_WUPS;
2126 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
2127 if (ret < 0) {
2128 netdev_warn(dev->net, "Error writing PMT_CTL\n");
2129 return ret;
2133 if (suspend_flags & SUSPEND_SUSPEND2) {
2134 netdev_info(dev->net, "resuming from SUSPEND2\n");
2136 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
2137 if (ret < 0) {
2138 netdev_warn(dev->net, "Error reading PMT_CTL\n");
2139 return ret;
2142 val |= PMT_CTL_PHY_PWRUP;
2144 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
2145 if (ret < 0) {
2146 netdev_warn(dev->net, "Error writing PMT_CTL\n");
2147 return ret;
2151 ret = smsc75xx_wait_ready(dev, 1);
2152 if (ret < 0) {
2153 netdev_warn(dev->net, "device not ready in smsc75xx_resume\n");
2154 return ret;
2157 return usbnet_resume(intf);
2160 static void smsc75xx_rx_csum_offload(struct usbnet *dev, struct sk_buff *skb,
2161 u32 rx_cmd_a, u32 rx_cmd_b)
2163 if (!(dev->net->features & NETIF_F_RXCSUM) ||
2164 unlikely(rx_cmd_a & RX_CMD_A_LCSM)) {
2165 skb->ip_summed = CHECKSUM_NONE;
2166 } else {
2167 skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT));
2168 skb->ip_summed = CHECKSUM_COMPLETE;
2172 static int smsc75xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
2174 /* This check is no longer done by usbnet */
2175 if (skb->len < dev->net->hard_header_len)
2176 return 0;
2178 while (skb->len > 0) {
2179 u32 rx_cmd_a, rx_cmd_b, align_count, size;
2180 struct sk_buff *ax_skb;
2181 unsigned char *packet;
2183 memcpy(&rx_cmd_a, skb->data, sizeof(rx_cmd_a));
2184 le32_to_cpus(&rx_cmd_a);
2185 skb_pull(skb, 4);
2187 memcpy(&rx_cmd_b, skb->data, sizeof(rx_cmd_b));
2188 le32_to_cpus(&rx_cmd_b);
2189 skb_pull(skb, 4 + RXW_PADDING);
2191 packet = skb->data;
2193 /* get the packet length */
2194 size = (rx_cmd_a & RX_CMD_A_LEN) - RXW_PADDING;
2195 align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
2197 if (unlikely(rx_cmd_a & RX_CMD_A_RED)) {
2198 netif_dbg(dev, rx_err, dev->net,
2199 "Error rx_cmd_a=0x%08x\n", rx_cmd_a);
2200 dev->net->stats.rx_errors++;
2201 dev->net->stats.rx_dropped++;
2203 if (rx_cmd_a & RX_CMD_A_FCS)
2204 dev->net->stats.rx_crc_errors++;
2205 else if (rx_cmd_a & (RX_CMD_A_LONG | RX_CMD_A_RUNT))
2206 dev->net->stats.rx_frame_errors++;
2207 } else {
2208 /* MAX_SINGLE_PACKET_SIZE + 4(CRC) + 2(COE) + 4(Vlan) */
2209 if (unlikely(size > (MAX_SINGLE_PACKET_SIZE + ETH_HLEN + 12))) {
2210 netif_dbg(dev, rx_err, dev->net,
2211 "size err rx_cmd_a=0x%08x\n",
2212 rx_cmd_a);
2213 return 0;
2216 /* last frame in this batch */
2217 if (skb->len == size) {
2218 smsc75xx_rx_csum_offload(dev, skb, rx_cmd_a,
2219 rx_cmd_b);
2221 skb_trim(skb, skb->len - 4); /* remove fcs */
2222 skb->truesize = size + sizeof(struct sk_buff);
2224 return 1;
2227 ax_skb = skb_clone(skb, GFP_ATOMIC);
2228 if (unlikely(!ax_skb)) {
2229 netdev_warn(dev->net, "Error allocating skb\n");
2230 return 0;
2233 ax_skb->len = size;
2234 ax_skb->data = packet;
2235 skb_set_tail_pointer(ax_skb, size);
2237 smsc75xx_rx_csum_offload(dev, ax_skb, rx_cmd_a,
2238 rx_cmd_b);
2240 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
2241 ax_skb->truesize = size + sizeof(struct sk_buff);
2243 usbnet_skb_return(dev, ax_skb);
2246 skb_pull(skb, size);
2248 /* padding bytes before the next frame starts */
2249 if (skb->len)
2250 skb_pull(skb, align_count);
2253 if (unlikely(skb->len < 0)) {
2254 netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
2255 return 0;
2258 return 1;
2261 static struct sk_buff *smsc75xx_tx_fixup(struct usbnet *dev,
2262 struct sk_buff *skb, gfp_t flags)
2264 u32 tx_cmd_a, tx_cmd_b;
2266 if (skb_cow_head(skb, SMSC75XX_TX_OVERHEAD)) {
2267 dev_kfree_skb_any(skb);
2268 return NULL;
2271 tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN) | TX_CMD_A_FCS;
2273 if (skb->ip_summed == CHECKSUM_PARTIAL)
2274 tx_cmd_a |= TX_CMD_A_IPE | TX_CMD_A_TPE;
2276 if (skb_is_gso(skb)) {
2277 u16 mss = max(skb_shinfo(skb)->gso_size, TX_MSS_MIN);
2278 tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT) & TX_CMD_B_MSS;
2280 tx_cmd_a |= TX_CMD_A_LSO;
2281 } else {
2282 tx_cmd_b = 0;
2285 skb_push(skb, 4);
2286 cpu_to_le32s(&tx_cmd_b);
2287 memcpy(skb->data, &tx_cmd_b, 4);
2289 skb_push(skb, 4);
2290 cpu_to_le32s(&tx_cmd_a);
2291 memcpy(skb->data, &tx_cmd_a, 4);
2293 return skb;
2296 static int smsc75xx_manage_power(struct usbnet *dev, int on)
2298 dev->intf->needs_remote_wakeup = on;
2299 return 0;
2302 static const struct driver_info smsc75xx_info = {
2303 .description = "smsc75xx USB 2.0 Gigabit Ethernet",
2304 .bind = smsc75xx_bind,
2305 .unbind = smsc75xx_unbind,
2306 .link_reset = smsc75xx_link_reset,
2307 .reset = smsc75xx_reset,
2308 .rx_fixup = smsc75xx_rx_fixup,
2309 .tx_fixup = smsc75xx_tx_fixup,
2310 .status = smsc75xx_status,
2311 .manage_power = smsc75xx_manage_power,
2312 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
2315 static const struct usb_device_id products[] = {
2317 /* SMSC7500 USB Gigabit Ethernet Device */
2318 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7500),
2319 .driver_info = (unsigned long) &smsc75xx_info,
2322 /* SMSC7500 USB Gigabit Ethernet Device */
2323 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7505),
2324 .driver_info = (unsigned long) &smsc75xx_info,
2326 { }, /* END */
2328 MODULE_DEVICE_TABLE(usb, products);
2330 static struct usb_driver smsc75xx_driver = {
2331 .name = SMSC_CHIPNAME,
2332 .id_table = products,
2333 .probe = usbnet_probe,
2334 .suspend = smsc75xx_suspend,
2335 .resume = smsc75xx_resume,
2336 .reset_resume = smsc75xx_resume,
2337 .disconnect = usbnet_disconnect,
2338 .disable_hub_initiated_lpm = 1,
2339 .supports_autosuspend = 1,
2342 module_usb_driver(smsc75xx_driver);
2344 MODULE_AUTHOR("Nancy Lin");
2345 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2346 MODULE_DESCRIPTION("SMSC75XX USB 2.0 Gigabit Ethernet Devices");
2347 MODULE_LICENSE("GPL");