2 * Copyright (c) 2013 Realtek Semiconductor Corp. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
10 #include <linux/init.h>
11 #include <linux/signal.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/mii.h>
17 #include <linux/ethtool.h>
18 #include <linux/usb.h>
19 #include <linux/crc32.h>
20 #include <linux/if_vlan.h>
21 #include <linux/uaccess.h>
22 #include <linux/list.h>
24 #include <linux/ipv6.h>
26 /* Version Information */
27 #define DRIVER_VERSION "v1.02.0 (2013/10/28)"
29 #define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters"
30 #define MODULENAME "r8152"
32 #define R8152_PHY_ID 32
34 #define PLA_IDR 0xc000
35 #define PLA_RCR 0xc010
36 #define PLA_RMS 0xc016
37 #define PLA_RXFIFO_CTRL0 0xc0a0
38 #define PLA_RXFIFO_CTRL1 0xc0a4
39 #define PLA_RXFIFO_CTRL2 0xc0a8
40 #define PLA_FMC 0xc0b4
41 #define PLA_CFG_WOL 0xc0b6
42 #define PLA_MAR 0xcd00
43 #define PAL_BDC_CR 0xd1a0
44 #define PLA_LEDSEL 0xdd90
45 #define PLA_LED_FEATURE 0xdd92
46 #define PLA_PHYAR 0xde00
47 #define PLA_GPHY_INTR_IMR 0xe022
48 #define PLA_EEE_CR 0xe040
49 #define PLA_EEEP_CR 0xe080
50 #define PLA_MAC_PWR_CTRL 0xe0c0
51 #define PLA_TCR0 0xe610
52 #define PLA_TCR1 0xe612
53 #define PLA_TXFIFO_CTRL 0xe618
54 #define PLA_RSTTELLY 0xe800
56 #define PLA_CRWECR 0xe81c
57 #define PLA_CONFIG5 0xe822
58 #define PLA_PHY_PWR 0xe84c
59 #define PLA_OOB_CTRL 0xe84f
60 #define PLA_CPCR 0xe854
61 #define PLA_MISC_0 0xe858
62 #define PLA_MISC_1 0xe85a
63 #define PLA_OCP_GPHY_BASE 0xe86c
64 #define PLA_TELLYCNT 0xe890
65 #define PLA_SFF_STS_7 0xe8de
66 #define PLA_PHYSTATUS 0xe908
67 #define PLA_BP_BA 0xfc26
68 #define PLA_BP_0 0xfc28
69 #define PLA_BP_1 0xfc2a
70 #define PLA_BP_2 0xfc2c
71 #define PLA_BP_3 0xfc2e
72 #define PLA_BP_4 0xfc30
73 #define PLA_BP_5 0xfc32
74 #define PLA_BP_6 0xfc34
75 #define PLA_BP_7 0xfc36
77 #define USB_DEV_STAT 0xb808
78 #define USB_USB_CTRL 0xd406
79 #define USB_PHY_CTRL 0xd408
80 #define USB_TX_AGG 0xd40a
81 #define USB_RX_BUF_TH 0xd40c
82 #define USB_USB_TIMER 0xd428
83 #define USB_PM_CTRL_STATUS 0xd432
84 #define USB_TX_DMA 0xd434
85 #define USB_UPS_CTRL 0xd800
86 #define USB_BP_BA 0xfc26
87 #define USB_BP_0 0xfc28
88 #define USB_BP_1 0xfc2a
89 #define USB_BP_2 0xfc2c
90 #define USB_BP_3 0xfc2e
91 #define USB_BP_4 0xfc30
92 #define USB_BP_5 0xfc32
93 #define USB_BP_6 0xfc34
94 #define USB_BP_7 0xfc36
97 #define OCP_ALDPS_CONFIG 0x2010
98 #define OCP_EEE_CONFIG1 0x2080
99 #define OCP_EEE_CONFIG2 0x2092
100 #define OCP_EEE_CONFIG3 0x2094
101 #define OCP_EEE_AR 0xa41a
102 #define OCP_EEE_DATA 0xa41c
105 #define RCR_AAP 0x00000001
106 #define RCR_APM 0x00000002
107 #define RCR_AM 0x00000004
108 #define RCR_AB 0x00000008
109 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
111 /* PLA_RXFIFO_CTRL0 */
112 #define RXFIFO_THR1_NORMAL 0x00080002
113 #define RXFIFO_THR1_OOB 0x01800003
115 /* PLA_RXFIFO_CTRL1 */
116 #define RXFIFO_THR2_FULL 0x00000060
117 #define RXFIFO_THR2_HIGH 0x00000038
118 #define RXFIFO_THR2_OOB 0x0000004a
120 /* PLA_RXFIFO_CTRL2 */
121 #define RXFIFO_THR3_FULL 0x00000078
122 #define RXFIFO_THR3_HIGH 0x00000048
123 #define RXFIFO_THR3_OOB 0x0000005a
125 /* PLA_TXFIFO_CTRL */
126 #define TXFIFO_THR_NORMAL 0x00400008
129 #define FMC_FCR_MCU_EN 0x0001
132 #define EEEP_CR_EEEP_TX 0x0002
135 #define TCR0_TX_EMPTY 0x0800
136 #define TCR0_AUTO_FIFO 0x0080
139 #define VERSION_MASK 0x7cf0
147 #define CRWECR_NORAML 0x00
148 #define CRWECR_CONFIG 0xc0
151 #define NOW_IS_OOB 0x80
152 #define TXFIFO_EMPTY 0x20
153 #define RXFIFO_EMPTY 0x10
154 #define LINK_LIST_READY 0x02
155 #define DIS_MCU_CLROOB 0x01
156 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
159 #define RXDY_GATED_EN 0x0008
162 #define RE_INIT_LL 0x8000
163 #define MCU_BORW_EN 0x4000
166 #define CPCR_RX_VLAN 0x0040
169 #define MAGIC_EN 0x0001
172 #define ALDPS_PROXY_MODE 0x0001
175 #define LAN_WAKE_EN 0x0002
177 /* PLA_LED_FEATURE */
178 #define LED_MODE_MASK 0x0700
181 #define TX_10M_IDLE_EN 0x0080
182 #define PFM_PWM_SWITCH 0x0040
184 /* PLA_MAC_PWR_CTRL */
185 #define D3_CLK_GATED_EN 0x00004000
186 #define MCU_CLK_RATIO 0x07010f07
187 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
189 /* PLA_GPHY_INTR_IMR */
190 #define GPHY_STS_MSK 0x0001
191 #define SPEED_DOWN_MSK 0x0002
192 #define SPDWN_RXDV_MSK 0x0004
193 #define SPDWN_LINKCHG_MSK 0x0008
196 #define PHYAR_FLAG 0x80000000
199 #define EEE_RX_EN 0x0001
200 #define EEE_TX_EN 0x0002
203 #define STAT_SPEED_MASK 0x0006
204 #define STAT_SPEED_HIGH 0x0000
205 #define STAT_SPEED_FULL 0x0001
208 #define TX_AGG_MAX_THRESHOLD 0x03
211 #define RX_BUF_THR 0x7a120180
214 #define TEST_MODE_DISABLE 0x00000001
215 #define TX_SIZE_ADJUST1 0x00000100
218 #define POWER_CUT 0x0100
220 /* USB_PM_CTRL_STATUS */
221 #define RWSUME_INDICATE 0x0001
224 #define RX_AGG_DISABLE 0x0010
226 /* OCP_ALDPS_CONFIG */
227 #define ENPWRSAVE 0x8000
228 #define ENPDNPS 0x0200
229 #define LINKENA 0x0100
230 #define DIS_SDSAVE 0x0010
232 /* OCP_EEE_CONFIG1 */
233 #define RG_TXLPI_MSK_HFDUP 0x8000
234 #define RG_MATCLR_EN 0x4000
235 #define EEE_10_CAP 0x2000
236 #define EEE_NWAY_EN 0x1000
237 #define TX_QUIET_EN 0x0200
238 #define RX_QUIET_EN 0x0100
239 #define SDRISETIME 0x0010 /* bit 4 ~ 6 */
240 #define RG_RXLPI_MSK_HFDUP 0x0008
241 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
243 /* OCP_EEE_CONFIG2 */
244 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
245 #define RG_DACQUIET_EN 0x0400
246 #define RG_LDVQUIET_EN 0x0200
247 #define RG_CKRSEL 0x0020
248 #define RG_EEEPRG_EN 0x0010
250 /* OCP_EEE_CONFIG3 */
251 #define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
252 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
253 #define MSK_PH 0x0006 /* bit 0 ~ 3 */
256 /* bit[15:14] function */
257 #define FUN_ADDR 0x0000
258 #define FUN_DATA 0x4000
259 /* bit[4:0] device addr */
260 #define DEVICE_ADDR 0x0007
263 #define EEE_ADDR 0x003C
264 #define EEE_DATA 0x0002
266 enum rtl_register_content {
273 #define RTL8152_MAX_TX 10
274 #define RTL8152_MAX_RX 10
277 #define INTR_LINK 0x0004
279 #define RTL8152_REQT_READ 0xc0
280 #define RTL8152_REQT_WRITE 0x40
281 #define RTL8152_REQ_GET_REGS 0x05
282 #define RTL8152_REQ_SET_REGS 0x05
284 #define BYTE_EN_DWORD 0xff
285 #define BYTE_EN_WORD 0x33
286 #define BYTE_EN_BYTE 0x11
287 #define BYTE_EN_SIX_BYTES 0x3f
288 #define BYTE_EN_START_MASK 0x0f
289 #define BYTE_EN_END_MASK 0xf0
291 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
292 #define RTL8152_TX_TIMEOUT (HZ)
302 /* Define these values to match your device */
303 #define VENDOR_ID_REALTEK 0x0bda
304 #define PRODUCT_ID_RTL8152 0x8152
306 #define MCU_TYPE_PLA 0x0100
307 #define MCU_TYPE_USB 0x0000
311 #define RX_LEN_MASK 0x7fff
321 #define TX_FS (1 << 31) /* First segment of a packet */
322 #define TX_LS (1 << 30) /* Final segment of a packet */
323 #define TX_LEN_MASK 0x3ffff
326 #define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
327 #define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
328 #define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
329 #define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
335 struct list_head list;
337 struct r8152 *context;
343 struct list_head list;
345 struct r8152 *context;
354 struct usb_device *udev;
355 struct tasklet_struct tl;
356 struct usb_interface *intf;
357 struct net_device *netdev;
358 struct urb *intr_urb;
359 struct tx_agg tx_info[RTL8152_MAX_TX];
360 struct rx_agg rx_info[RTL8152_MAX_RX];
361 struct list_head rx_done, tx_free;
362 struct sk_buff_head tx_queue;
363 spinlock_t rx_lock, tx_lock;
364 struct delayed_work schedule;
365 struct mii_if_info mii;
381 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
382 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
384 static const int multicast_filter_limit = 32;
385 static unsigned int rx_buf_sz = 16384;
388 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
393 tmp = kmalloc(size, GFP_KERNEL);
397 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
398 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
399 value, index, tmp, size, 500);
401 memcpy(data, tmp, size);
408 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
413 tmp = kmalloc(size, GFP_KERNEL);
417 memcpy(tmp, data, size);
419 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
420 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
421 value, index, tmp, size, 500);
427 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
428 void *data, u16 type)
433 if (test_bit(RTL8152_UNPLUG, &tp->flags))
436 /* both size and indix must be 4 bytes align */
437 if ((size & 3) || !size || (index & 3) || !data)
440 if ((u32)index + (u32)size > 0xffff)
445 ret = get_registers(tp, index, type, limit, data);
453 ret = get_registers(tp, index, type, size, data);
467 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
468 u16 size, void *data, u16 type)
471 u16 byteen_start, byteen_end, byen;
474 if (test_bit(RTL8152_UNPLUG, &tp->flags))
477 /* both size and indix must be 4 bytes align */
478 if ((size & 3) || !size || (index & 3) || !data)
481 if ((u32)index + (u32)size > 0xffff)
484 byteen_start = byteen & BYTE_EN_START_MASK;
485 byteen_end = byteen & BYTE_EN_END_MASK;
487 byen = byteen_start | (byteen_start << 4);
488 ret = set_registers(tp, index, type | byen, 4, data);
501 ret = set_registers(tp, index,
502 type | BYTE_EN_DWORD,
511 ret = set_registers(tp, index,
512 type | BYTE_EN_DWORD,
524 byen = byteen_end | (byteen_end >> 4);
525 ret = set_registers(tp, index, type | byen, 4, data);
535 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
537 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
541 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
543 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
547 int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
549 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
553 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
555 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
558 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
562 generic_ocp_read(tp, index, sizeof(data), &data, type);
564 return __le32_to_cpu(data);
567 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
569 __le32 tmp = __cpu_to_le32(data);
571 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
574 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
578 u8 shift = index & 2;
582 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
584 data = __le32_to_cpu(tmp);
585 data >>= (shift * 8);
591 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
595 u16 byen = BYTE_EN_WORD;
596 u8 shift = index & 2;
602 mask <<= (shift * 8);
603 data <<= (shift * 8);
607 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
609 data |= __le32_to_cpu(tmp) & ~mask;
610 tmp = __cpu_to_le32(data);
612 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
615 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
619 u8 shift = index & 3;
623 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
625 data = __le32_to_cpu(tmp);
626 data >>= (shift * 8);
632 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
636 u16 byen = BYTE_EN_BYTE;
637 u8 shift = index & 3;
643 mask <<= (shift * 8);
644 data <<= (shift * 8);
648 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
650 data |= __le32_to_cpu(tmp) & ~mask;
651 tmp = __cpu_to_le32(data);
653 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
656 static void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
661 ocp_data = PHYAR_FLAG | ((reg_addr & 0x1f) << 16) |
664 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
666 for (i = 20; i > 0; i--) {
668 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
669 if (!(ocp_data & PHYAR_FLAG))
675 static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
680 ocp_data = (reg_addr & 0x1f) << 16;
681 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
683 for (i = 20; i > 0; i--) {
685 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
686 if (ocp_data & PHYAR_FLAG)
691 if (!(ocp_data & PHYAR_FLAG))
694 return (u16)(ocp_data & 0xffff);
697 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
699 struct r8152 *tp = netdev_priv(netdev);
701 if (phy_id != R8152_PHY_ID)
704 return r8152_mdio_read(tp, reg);
708 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
710 struct r8152 *tp = netdev_priv(netdev);
712 if (phy_id != R8152_PHY_ID)
715 r8152_mdio_write(tp, reg, val);
718 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
720 u16 ocp_base, ocp_index;
722 ocp_base = addr & 0xf000;
723 if (ocp_base != tp->ocp_base) {
724 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
725 tp->ocp_base = ocp_base;
728 ocp_index = (addr & 0x0fff) | 0xb000;
729 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
733 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
735 static inline void set_ethernet_addr(struct r8152 *tp)
737 struct net_device *dev = tp->netdev;
740 if (pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id) < 0)
741 netif_notice(tp, probe, dev, "inet addr fail\n");
743 memcpy(dev->dev_addr, node_id, dev->addr_len);
744 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
748 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
750 struct r8152 *tp = netdev_priv(netdev);
751 struct sockaddr *addr = p;
753 if (!is_valid_ether_addr(addr->sa_data))
754 return -EADDRNOTAVAIL;
756 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
758 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
759 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
760 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
765 static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
770 static void read_bulk_callback(struct urb *urb)
772 struct net_device *netdev;
774 int status = urb->status;
787 if (test_bit(RTL8152_UNPLUG, &tp->flags))
790 if (!test_bit(WORK_ENABLE, &tp->flags))
795 /* When link down, the driver would cancel all bulks. */
796 /* This avoid the re-submitting bulk */
797 if (!netif_carrier_ok(netdev))
802 if (urb->actual_length < ETH_ZLEN)
805 spin_lock_irqsave(&tp->rx_lock, flags);
806 list_add_tail(&agg->list, &tp->rx_done);
807 spin_unlock_irqrestore(&tp->rx_lock, flags);
808 tasklet_schedule(&tp->tl);
811 set_bit(RTL8152_UNPLUG, &tp->flags);
812 netif_device_detach(tp->netdev);
815 return; /* the urb is in unlink state */
817 pr_warn_ratelimited("may be reset is needed?..\n");
820 pr_warn_ratelimited("Rx status %d\n", status);
824 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
825 if (result == -ENODEV) {
826 netif_device_detach(tp->netdev);
828 spin_lock_irqsave(&tp->rx_lock, flags);
829 list_add_tail(&agg->list, &tp->rx_done);
830 spin_unlock_irqrestore(&tp->rx_lock, flags);
831 tasklet_schedule(&tp->tl);
835 static void write_bulk_callback(struct urb *urb)
837 struct net_device_stats *stats;
841 int status = urb->status;
851 stats = rtl8152_get_stats(tp->netdev);
853 pr_warn_ratelimited("Tx status %d\n", status);
854 stats->tx_errors += agg->skb_num;
856 stats->tx_packets += agg->skb_num;
857 stats->tx_bytes += agg->skb_len;
860 spin_lock_irqsave(&tp->tx_lock, flags);
861 list_add_tail(&agg->list, &tp->tx_free);
862 spin_unlock_irqrestore(&tp->tx_lock, flags);
864 if (!netif_carrier_ok(tp->netdev))
867 if (!test_bit(WORK_ENABLE, &tp->flags))
870 if (test_bit(RTL8152_UNPLUG, &tp->flags))
873 if (!skb_queue_empty(&tp->tx_queue))
874 tasklet_schedule(&tp->tl);
877 static void intr_callback(struct urb *urb)
881 int status = urb->status;
888 if (!test_bit(WORK_ENABLE, &tp->flags))
891 if (test_bit(RTL8152_UNPLUG, &tp->flags))
895 case 0: /* success */
897 case -ECONNRESET: /* unlink */
899 netif_device_detach(tp->netdev);
903 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
905 /* -EPIPE: should clear the halt */
907 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
911 d = urb->transfer_buffer;
912 if (INTR_LINK & __le16_to_cpu(d[0])) {
913 if (!(tp->speed & LINK_STATUS)) {
914 set_bit(RTL8152_LINK_CHG, &tp->flags);
915 schedule_delayed_work(&tp->schedule, 0);
918 if (tp->speed & LINK_STATUS) {
919 set_bit(RTL8152_LINK_CHG, &tp->flags);
920 schedule_delayed_work(&tp->schedule, 0);
925 res = usb_submit_urb(urb, GFP_ATOMIC);
927 netif_device_detach(tp->netdev);
929 netif_err(tp, intr, tp->netdev,
930 "can't resubmit intr, status %d\n", res);
933 static inline void *rx_agg_align(void *data)
935 return (void *)ALIGN((uintptr_t)data, 8);
938 static inline void *tx_agg_align(void *data)
940 return (void *)ALIGN((uintptr_t)data, 4);
943 static void free_all_mem(struct r8152 *tp)
947 for (i = 0; i < RTL8152_MAX_RX; i++) {
948 if (tp->rx_info[i].urb) {
949 usb_free_urb(tp->rx_info[i].urb);
950 tp->rx_info[i].urb = NULL;
953 if (tp->rx_info[i].buffer) {
954 kfree(tp->rx_info[i].buffer);
955 tp->rx_info[i].buffer = NULL;
956 tp->rx_info[i].head = NULL;
960 for (i = 0; i < RTL8152_MAX_TX; i++) {
961 if (tp->tx_info[i].urb) {
962 usb_free_urb(tp->tx_info[i].urb);
963 tp->tx_info[i].urb = NULL;
966 if (tp->tx_info[i].buffer) {
967 kfree(tp->tx_info[i].buffer);
968 tp->tx_info[i].buffer = NULL;
969 tp->tx_info[i].head = NULL;
974 usb_free_urb(tp->intr_urb);
979 kfree(tp->intr_buff);
980 tp->intr_buff = NULL;
984 static int alloc_all_mem(struct r8152 *tp)
986 struct net_device *netdev = tp->netdev;
987 struct usb_interface *intf = tp->intf;
988 struct usb_host_interface *alt = intf->cur_altsetting;
989 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
994 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
996 spin_lock_init(&tp->rx_lock);
997 spin_lock_init(&tp->tx_lock);
998 INIT_LIST_HEAD(&tp->rx_done);
999 INIT_LIST_HEAD(&tp->tx_free);
1000 skb_queue_head_init(&tp->tx_queue);
1002 for (i = 0; i < RTL8152_MAX_RX; i++) {
1003 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1007 if (buf != rx_agg_align(buf)) {
1009 buf = kmalloc_node(rx_buf_sz + 8, GFP_KERNEL, node);
1014 urb = usb_alloc_urb(0, GFP_KERNEL);
1020 INIT_LIST_HEAD(&tp->rx_info[i].list);
1021 tp->rx_info[i].context = tp;
1022 tp->rx_info[i].urb = urb;
1023 tp->rx_info[i].buffer = buf;
1024 tp->rx_info[i].head = rx_agg_align(buf);
1027 for (i = 0; i < RTL8152_MAX_TX; i++) {
1028 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1032 if (buf != tx_agg_align(buf)) {
1034 buf = kmalloc_node(rx_buf_sz + 4, GFP_KERNEL, node);
1039 urb = usb_alloc_urb(0, GFP_KERNEL);
1045 INIT_LIST_HEAD(&tp->tx_info[i].list);
1046 tp->tx_info[i].context = tp;
1047 tp->tx_info[i].urb = urb;
1048 tp->tx_info[i].buffer = buf;
1049 tp->tx_info[i].head = tx_agg_align(buf);
1051 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1054 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1058 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1062 tp->intr_interval = (int)ep_intr->desc.bInterval;
1063 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1064 tp->intr_buff, INTBUFSIZE, intr_callback,
1065 tp, tp->intr_interval);
1074 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1076 struct tx_agg *agg = NULL;
1077 unsigned long flags;
1079 spin_lock_irqsave(&tp->tx_lock, flags);
1080 if (!list_empty(&tp->tx_free)) {
1081 struct list_head *cursor;
1083 cursor = tp->tx_free.next;
1084 list_del_init(cursor);
1085 agg = list_entry(cursor, struct tx_agg, list);
1087 spin_unlock_irqrestore(&tp->tx_lock, flags);
1093 r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1095 memset(desc, 0, sizeof(*desc));
1097 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1099 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1104 if (skb->protocol == htons(ETH_P_8021Q))
1105 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1107 protocol = skb->protocol;
1110 case htons(ETH_P_IP):
1112 ip_protocol = ip_hdr(skb)->protocol;
1115 case htons(ETH_P_IPV6):
1117 ip_protocol = ipv6_hdr(skb)->nexthdr;
1121 ip_protocol = IPPROTO_RAW;
1125 if (ip_protocol == IPPROTO_TCP) {
1127 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1128 } else if (ip_protocol == IPPROTO_UDP) {
1134 desc->opts2 = cpu_to_le32(opts2);
1138 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1143 tx_data = agg->head;
1144 agg->skb_num = agg->skb_len = 0;
1147 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
1148 struct tx_desc *tx_desc;
1149 struct sk_buff *skb;
1152 skb = skb_dequeue(&tp->tx_queue);
1156 remain -= sizeof(*tx_desc);
1159 skb_queue_head(&tp->tx_queue, skb);
1163 tx_data = tx_agg_align(tx_data);
1164 tx_desc = (struct tx_desc *)tx_data;
1165 tx_data += sizeof(*tx_desc);
1167 r8152_tx_csum(tp, tx_desc, skb);
1168 memcpy(tx_data, skb->data, len);
1170 agg->skb_len += len;
1171 dev_kfree_skb_any(skb);
1174 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
1177 netif_tx_lock(tp->netdev);
1179 if (netif_queue_stopped(tp->netdev) &&
1180 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1181 netif_wake_queue(tp->netdev);
1183 netif_tx_unlock(tp->netdev);
1185 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1186 agg->head, (int)(tx_data - (u8 *)agg->head),
1187 (usb_complete_t)write_bulk_callback, agg);
1189 return usb_submit_urb(agg->urb, GFP_ATOMIC);
1192 static void rx_bottom(struct r8152 *tp)
1194 unsigned long flags;
1195 struct list_head *cursor, *next;
1197 spin_lock_irqsave(&tp->rx_lock, flags);
1198 list_for_each_safe(cursor, next, &tp->rx_done) {
1199 struct rx_desc *rx_desc;
1206 list_del_init(cursor);
1207 spin_unlock_irqrestore(&tp->rx_lock, flags);
1209 agg = list_entry(cursor, struct rx_agg, list);
1211 if (urb->actual_length < ETH_ZLEN)
1214 rx_desc = agg->head;
1215 rx_data = agg->head;
1216 len_used += sizeof(struct rx_desc);
1218 while (urb->actual_length > len_used) {
1219 struct net_device *netdev = tp->netdev;
1220 struct net_device_stats *stats;
1221 unsigned int pkt_len;
1222 struct sk_buff *skb;
1224 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
1225 if (pkt_len < ETH_ZLEN)
1228 len_used += pkt_len;
1229 if (urb->actual_length < len_used)
1232 stats = rtl8152_get_stats(netdev);
1234 pkt_len -= 4; /* CRC */
1235 rx_data += sizeof(struct rx_desc);
1237 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1239 stats->rx_dropped++;
1242 memcpy(skb->data, rx_data, pkt_len);
1243 skb_put(skb, pkt_len);
1244 skb->protocol = eth_type_trans(skb, netdev);
1246 stats->rx_packets++;
1247 stats->rx_bytes += pkt_len;
1249 rx_data = rx_agg_align(rx_data + pkt_len + 4);
1250 rx_desc = (struct rx_desc *)rx_data;
1251 len_used = (int)(rx_data - (u8 *)agg->head);
1252 len_used += sizeof(struct rx_desc);
1256 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1257 spin_lock_irqsave(&tp->rx_lock, flags);
1258 if (ret && ret != -ENODEV) {
1259 list_add_tail(&agg->list, next);
1260 tasklet_schedule(&tp->tl);
1263 spin_unlock_irqrestore(&tp->rx_lock, flags);
1266 static void tx_bottom(struct r8152 *tp)
1273 if (skb_queue_empty(&tp->tx_queue))
1276 agg = r8152_get_tx_agg(tp);
1280 res = r8152_tx_agg_fill(tp, agg);
1282 struct net_device_stats *stats;
1283 struct net_device *netdev;
1284 unsigned long flags;
1286 netdev = tp->netdev;
1287 stats = rtl8152_get_stats(netdev);
1289 if (res == -ENODEV) {
1290 netif_device_detach(netdev);
1292 netif_warn(tp, tx_err, netdev,
1293 "failed tx_urb %d\n", res);
1294 stats->tx_dropped += agg->skb_num;
1295 spin_lock_irqsave(&tp->tx_lock, flags);
1296 list_add_tail(&agg->list, &tp->tx_free);
1297 spin_unlock_irqrestore(&tp->tx_lock, flags);
1303 static void bottom_half(unsigned long data)
1307 tp = (struct r8152 *)data;
1309 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1312 if (!test_bit(WORK_ENABLE, &tp->flags))
1315 /* When link down, the driver would cancel all bulks. */
1316 /* This avoid the re-submitting bulk */
1317 if (!netif_carrier_ok(tp->netdev))
1325 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1327 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1328 agg->head, rx_buf_sz,
1329 (usb_complete_t)read_bulk_callback, agg);
1331 return usb_submit_urb(agg->urb, mem_flags);
1334 static void rtl8152_tx_timeout(struct net_device *netdev)
1336 struct r8152 *tp = netdev_priv(netdev);
1339 netif_warn(tp, tx_err, netdev, "Tx timeout.\n");
1340 for (i = 0; i < RTL8152_MAX_TX; i++)
1341 usb_unlink_urb(tp->tx_info[i].urb);
1344 static void rtl8152_set_rx_mode(struct net_device *netdev)
1346 struct r8152 *tp = netdev_priv(netdev);
1348 if (tp->speed & LINK_STATUS) {
1349 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1350 schedule_delayed_work(&tp->schedule, 0);
1354 static void _rtl8152_set_rx_mode(struct net_device *netdev)
1356 struct r8152 *tp = netdev_priv(netdev);
1357 u32 mc_filter[2]; /* Multicast hash filter */
1361 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1362 netif_stop_queue(netdev);
1363 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1364 ocp_data &= ~RCR_ACPT_ALL;
1365 ocp_data |= RCR_AB | RCR_APM;
1367 if (netdev->flags & IFF_PROMISC) {
1368 /* Unconditionally log net taps. */
1369 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1370 ocp_data |= RCR_AM | RCR_AAP;
1371 mc_filter[1] = mc_filter[0] = 0xffffffff;
1372 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1373 (netdev->flags & IFF_ALLMULTI)) {
1374 /* Too many to filter perfectly -- accept all multicasts. */
1376 mc_filter[1] = mc_filter[0] = 0xffffffff;
1378 struct netdev_hw_addr *ha;
1380 mc_filter[1] = mc_filter[0] = 0;
1381 netdev_for_each_mc_addr(ha, netdev) {
1382 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1383 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1388 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1389 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
1391 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
1392 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1393 netif_wake_queue(netdev);
1396 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1397 struct net_device *netdev)
1399 struct r8152 *tp = netdev_priv(netdev);
1401 skb_tx_timestamp(skb);
1403 skb_queue_tail(&tp->tx_queue, skb);
1405 if (list_empty(&tp->tx_free) &&
1406 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1407 netif_stop_queue(netdev);
1409 if (!list_empty(&tp->tx_free))
1410 tasklet_schedule(&tp->tl);
1412 return NETDEV_TX_OK;
1415 static void r8152b_reset_packet_filter(struct r8152 *tp)
1419 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1420 ocp_data &= ~FMC_FCR_MCU_EN;
1421 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1422 ocp_data |= FMC_FCR_MCU_EN;
1423 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1426 static void rtl8152_nic_reset(struct r8152 *tp)
1430 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1432 for (i = 0; i < 1000; i++) {
1433 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1439 static void set_tx_qlen(struct r8152 *tp)
1441 struct net_device *netdev = tp->netdev;
1443 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1444 sizeof(struct tx_desc));
1447 static inline u8 rtl8152_get_speed(struct r8152 *tp)
1449 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1452 static int rtl8152_enable(struct r8152 *tp)
1459 speed = rtl8152_get_speed(tp);
1460 if (speed & _10bps) {
1461 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
1462 ocp_data |= EEEP_CR_EEEP_TX;
1463 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1465 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
1466 ocp_data &= ~EEEP_CR_EEEP_TX;
1467 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1470 r8152b_reset_packet_filter(tp);
1472 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1473 ocp_data |= CR_RE | CR_TE;
1474 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1476 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1477 ocp_data &= ~RXDY_GATED_EN;
1478 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1480 INIT_LIST_HEAD(&tp->rx_done);
1482 for (i = 0; i < RTL8152_MAX_RX; i++) {
1483 INIT_LIST_HEAD(&tp->rx_info[i].list);
1484 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1490 static void rtl8152_disable(struct r8152 *tp)
1492 struct net_device_stats *stats = rtl8152_get_stats(tp->netdev);
1493 struct sk_buff *skb;
1497 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1498 ocp_data &= ~RCR_ACPT_ALL;
1499 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1501 while ((skb = skb_dequeue(&tp->tx_queue))) {
1503 stats->tx_dropped++;
1506 for (i = 0; i < RTL8152_MAX_TX; i++)
1507 usb_kill_urb(tp->tx_info[i].urb);
1509 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1510 ocp_data |= RXDY_GATED_EN;
1511 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1513 for (i = 0; i < 1000; i++) {
1514 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1515 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1520 for (i = 0; i < 1000; i++) {
1521 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1526 for (i = 0; i < RTL8152_MAX_RX; i++)
1527 usb_kill_urb(tp->rx_info[i].urb);
1529 rtl8152_nic_reset(tp);
1532 static void r8152b_exit_oob(struct r8152 *tp)
1537 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1538 ocp_data &= ~RCR_ACPT_ALL;
1539 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1541 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1542 ocp_data |= RXDY_GATED_EN;
1543 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1545 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1546 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1548 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1549 ocp_data &= ~NOW_IS_OOB;
1550 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1552 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1553 ocp_data &= ~MCU_BORW_EN;
1554 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1556 for (i = 0; i < 1000; i++) {
1557 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1558 if (ocp_data & LINK_LIST_READY)
1563 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1564 ocp_data |= RE_INIT_LL;
1565 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1567 for (i = 0; i < 1000; i++) {
1568 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1569 if (ocp_data & LINK_LIST_READY)
1574 rtl8152_nic_reset(tp);
1576 /* rx share fifo credit full threshold */
1577 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1579 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1580 ocp_data &= STAT_SPEED_MASK;
1581 if (ocp_data == STAT_SPEED_FULL) {
1582 /* rx share fifo credit near full threshold */
1583 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1585 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1588 /* rx share fifo credit near full threshold */
1589 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1591 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1595 /* TX share fifo free credit full threshold */
1596 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1598 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
1599 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_BUF_THR);
1600 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1601 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1603 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1604 ocp_data &= ~CPCR_RX_VLAN;
1605 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1607 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1609 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1610 ocp_data |= TCR0_AUTO_FIFO;
1611 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1614 static void r8152b_enter_oob(struct r8152 *tp)
1619 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1620 ocp_data &= ~NOW_IS_OOB;
1621 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1623 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1624 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1625 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1627 rtl8152_disable(tp);
1629 for (i = 0; i < 1000; i++) {
1630 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1631 if (ocp_data & LINK_LIST_READY)
1636 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1637 ocp_data |= RE_INIT_LL;
1638 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1640 for (i = 0; i < 1000; i++) {
1641 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1642 if (ocp_data & LINK_LIST_READY)
1647 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1649 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1650 ocp_data |= MAGIC_EN;
1651 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1653 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1654 ocp_data |= CPCR_RX_VLAN;
1655 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1657 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
1658 ocp_data |= ALDPS_PROXY_MODE;
1659 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
1661 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1662 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1663 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1665 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
1667 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1668 ocp_data &= ~RXDY_GATED_EN;
1669 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1671 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1672 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1673 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1676 static void r8152b_disable_aldps(struct r8152 *tp)
1678 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1682 static inline void r8152b_enable_aldps(struct r8152 *tp)
1684 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1685 LINKENA | DIS_SDSAVE);
1688 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
1693 cancel_delayed_work_sync(&tp->schedule);
1694 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1695 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1696 ADVERTISE_100HALF | ADVERTISE_100FULL);
1698 if (autoneg == AUTONEG_DISABLE) {
1699 if (speed == SPEED_10) {
1701 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1702 } else if (speed == SPEED_100) {
1703 bmcr = BMCR_SPEED100;
1704 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1710 if (duplex == DUPLEX_FULL)
1711 bmcr |= BMCR_FULLDPLX;
1713 if (speed == SPEED_10) {
1714 if (duplex == DUPLEX_FULL)
1715 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1717 anar |= ADVERTISE_10HALF;
1718 } else if (speed == SPEED_100) {
1719 if (duplex == DUPLEX_FULL) {
1720 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1721 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1723 anar |= ADVERTISE_10HALF;
1724 anar |= ADVERTISE_100HALF;
1731 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1734 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1735 r8152_mdio_write(tp, MII_BMCR, bmcr);
1742 static void rtl8152_down(struct r8152 *tp)
1746 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1747 ocp_data &= ~POWER_CUT;
1748 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1750 r8152b_disable_aldps(tp);
1751 r8152b_enter_oob(tp);
1752 r8152b_enable_aldps(tp);
1755 static void set_carrier(struct r8152 *tp)
1757 struct net_device *netdev = tp->netdev;
1760 clear_bit(RTL8152_LINK_CHG, &tp->flags);
1761 speed = rtl8152_get_speed(tp);
1763 if (speed & LINK_STATUS) {
1764 if (!(tp->speed & LINK_STATUS)) {
1766 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1767 netif_carrier_on(netdev);
1770 if (tp->speed & LINK_STATUS) {
1771 netif_carrier_off(netdev);
1772 tasklet_disable(&tp->tl);
1773 rtl8152_disable(tp);
1774 tasklet_enable(&tp->tl);
1780 static void rtl_work_func_t(struct work_struct *work)
1782 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
1784 if (!test_bit(WORK_ENABLE, &tp->flags))
1787 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1790 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
1793 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
1794 _rtl8152_set_rx_mode(tp->netdev);
1800 static int rtl8152_open(struct net_device *netdev)
1802 struct r8152 *tp = netdev_priv(netdev);
1805 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
1808 netif_device_detach(tp->netdev);
1809 netif_warn(tp, ifup, netdev,
1810 "intr_urb submit failed: %d\n", res);
1814 rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL);
1816 netif_carrier_off(netdev);
1817 netif_start_queue(netdev);
1818 set_bit(WORK_ENABLE, &tp->flags);
1823 static int rtl8152_close(struct net_device *netdev)
1825 struct r8152 *tp = netdev_priv(netdev);
1828 usb_kill_urb(tp->intr_urb);
1829 clear_bit(WORK_ENABLE, &tp->flags);
1830 cancel_delayed_work_sync(&tp->schedule);
1831 netif_stop_queue(netdev);
1832 tasklet_disable(&tp->tl);
1833 rtl8152_disable(tp);
1834 tasklet_enable(&tp->tl);
1839 static void rtl_clear_bp(struct r8152 *tp)
1841 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1842 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1843 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1844 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1845 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1846 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1847 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1848 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1850 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1851 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1854 static void r8152b_enable_eee(struct r8152 *tp)
1858 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
1859 ocp_data |= EEE_RX_EN | EEE_TX_EN;
1860 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
1861 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
1862 EEE_10_CAP | EEE_NWAY_EN |
1863 TX_QUIET_EN | RX_QUIET_EN |
1864 SDRISETIME | RG_RXLPI_MSK_HFDUP |
1866 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
1867 RG_LDVQUIET_EN | RG_CKRSEL |
1869 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
1870 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
1871 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
1872 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
1873 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
1874 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
1877 static void r8152b_enable_fc(struct r8152 *tp)
1881 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1882 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1883 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1886 static void r8152b_hw_phy_cfg(struct r8152 *tp)
1888 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1889 r8152b_disable_aldps(tp);
1892 static void r8152b_init(struct r8152 *tp)
1899 if (tp->version == RTL_VER_01) {
1900 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
1901 ocp_data &= ~LED_MODE_MASK;
1902 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
1905 r8152b_hw_phy_cfg(tp);
1907 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1908 ocp_data &= ~POWER_CUT;
1909 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1911 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1912 ocp_data &= ~RWSUME_INDICATE;
1913 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1915 r8152b_exit_oob(tp);
1917 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1918 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
1919 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1920 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
1921 ocp_data &= ~MCU_CLK_RATIO_MASK;
1922 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
1923 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
1924 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
1925 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
1926 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
1928 r8152b_enable_eee(tp);
1929 r8152b_enable_aldps(tp);
1930 r8152b_enable_fc(tp);
1932 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
1934 for (i = 0; i < 100; i++) {
1936 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
1940 /* enable rx aggregation */
1941 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
1942 ocp_data &= ~RX_AGG_DISABLE;
1943 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
1946 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
1948 struct r8152 *tp = usb_get_intfdata(intf);
1950 netif_device_detach(tp->netdev);
1952 if (netif_running(tp->netdev)) {
1953 clear_bit(WORK_ENABLE, &tp->flags);
1954 usb_kill_urb(tp->intr_urb);
1955 cancel_delayed_work_sync(&tp->schedule);
1956 tasklet_disable(&tp->tl);
1964 static int rtl8152_resume(struct usb_interface *intf)
1966 struct r8152 *tp = usb_get_intfdata(intf);
1969 netif_device_attach(tp->netdev);
1970 if (netif_running(tp->netdev)) {
1971 rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL);
1973 netif_carrier_off(tp->netdev);
1974 set_bit(WORK_ENABLE, &tp->flags);
1975 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
1976 tasklet_enable(&tp->tl);
1982 static void rtl8152_get_drvinfo(struct net_device *netdev,
1983 struct ethtool_drvinfo *info)
1985 struct r8152 *tp = netdev_priv(netdev);
1987 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
1988 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
1989 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
1993 int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1995 struct r8152 *tp = netdev_priv(netdev);
1997 if (!tp->mii.mdio_read)
2000 return mii_ethtool_gset(&tp->mii, cmd);
2003 static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2005 struct r8152 *tp = netdev_priv(dev);
2007 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2010 static struct ethtool_ops ops = {
2011 .get_drvinfo = rtl8152_get_drvinfo,
2012 .get_settings = rtl8152_get_settings,
2013 .set_settings = rtl8152_set_settings,
2014 .get_link = ethtool_op_get_link,
2017 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2019 struct r8152 *tp = netdev_priv(netdev);
2020 struct mii_ioctl_data *data = if_mii(rq);
2025 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2029 data->val_out = r8152_mdio_read(tp, data->reg_num);
2033 if (!capable(CAP_NET_ADMIN)) {
2037 r8152_mdio_write(tp, data->reg_num, data->val_in);
2047 static const struct net_device_ops rtl8152_netdev_ops = {
2048 .ndo_open = rtl8152_open,
2049 .ndo_stop = rtl8152_close,
2050 .ndo_do_ioctl = rtl8152_ioctl,
2051 .ndo_start_xmit = rtl8152_start_xmit,
2052 .ndo_tx_timeout = rtl8152_tx_timeout,
2053 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2054 .ndo_set_mac_address = rtl8152_set_mac_address,
2056 .ndo_change_mtu = eth_change_mtu,
2057 .ndo_validate_addr = eth_validate_addr,
2060 static void r8152b_get_version(struct r8152 *tp)
2065 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2066 version = (u16)(ocp_data & VERSION_MASK);
2070 tp->version = RTL_VER_01;
2073 tp->version = RTL_VER_02;
2076 netif_info(tp, probe, tp->netdev,
2077 "Unknown version 0x%04x\n", version);
2082 static int rtl8152_probe(struct usb_interface *intf,
2083 const struct usb_device_id *id)
2085 struct usb_device *udev = interface_to_usbdev(intf);
2087 struct net_device *netdev;
2090 if (udev->actconfig->desc.bConfigurationValue != 1) {
2091 usb_driver_set_configuration(udev, 1);
2095 netdev = alloc_etherdev(sizeof(struct r8152));
2097 dev_err(&intf->dev, "Out of memory");
2101 SET_NETDEV_DEV(netdev, &intf->dev);
2102 tp = netdev_priv(netdev);
2103 tp->msg_enable = 0x7FFF;
2105 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
2106 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
2109 tp->netdev = netdev;
2111 netdev->netdev_ops = &rtl8152_netdev_ops;
2112 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
2114 netdev->features |= NETIF_F_IP_CSUM;
2115 netdev->hw_features = NETIF_F_IP_CSUM;
2116 SET_ETHTOOL_OPS(netdev, &ops);
2118 tp->mii.dev = netdev;
2119 tp->mii.mdio_read = read_mii_word;
2120 tp->mii.mdio_write = write_mii_word;
2121 tp->mii.phy_id_mask = 0x3f;
2122 tp->mii.reg_num_mask = 0x1f;
2123 tp->mii.phy_id = R8152_PHY_ID;
2124 tp->mii.supports_gmii = 0;
2126 r8152b_get_version(tp);
2128 set_ethernet_addr(tp);
2130 ret = alloc_all_mem(tp);
2134 usb_set_intfdata(intf, tp);
2136 ret = register_netdev(netdev);
2138 netif_err(tp, probe, netdev, "couldn't register the device");
2142 netif_info(tp, probe, netdev, "%s", DRIVER_VERSION);
2147 usb_set_intfdata(intf, NULL);
2149 free_netdev(netdev);
2153 static void rtl8152_unload(struct r8152 *tp)
2157 if (tp->version != RTL_VER_01) {
2158 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2159 ocp_data |= POWER_CUT;
2160 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2163 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2164 ocp_data &= ~RWSUME_INDICATE;
2165 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2168 static void rtl8152_disconnect(struct usb_interface *intf)
2170 struct r8152 *tp = usb_get_intfdata(intf);
2172 usb_set_intfdata(intf, NULL);
2174 set_bit(RTL8152_UNPLUG, &tp->flags);
2175 tasklet_kill(&tp->tl);
2176 unregister_netdev(tp->netdev);
2179 free_netdev(tp->netdev);
2183 /* table of devices that work with this driver */
2184 static struct usb_device_id rtl8152_table[] = {
2185 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
2189 MODULE_DEVICE_TABLE(usb, rtl8152_table);
2191 static struct usb_driver rtl8152_driver = {
2193 .id_table = rtl8152_table,
2194 .probe = rtl8152_probe,
2195 .disconnect = rtl8152_disconnect,
2196 .suspend = rtl8152_suspend,
2197 .resume = rtl8152_resume,
2198 .reset_resume = rtl8152_resume,
2201 module_usb_driver(rtl8152_driver);
2203 MODULE_AUTHOR(DRIVER_AUTHOR);
2204 MODULE_DESCRIPTION(DRIVER_DESC);
2205 MODULE_LICENSE("GPL");