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 /* Version Information */
25 #define DRIVER_VERSION "v1.01.0 (2013/08/12)"
26 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
27 #define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters"
28 #define MODULENAME "r8152"
30 #define R8152_PHY_ID 32
32 #define PLA_IDR 0xc000
33 #define PLA_RCR 0xc010
34 #define PLA_RMS 0xc016
35 #define PLA_RXFIFO_CTRL0 0xc0a0
36 #define PLA_RXFIFO_CTRL1 0xc0a4
37 #define PLA_RXFIFO_CTRL2 0xc0a8
38 #define PLA_FMC 0xc0b4
39 #define PLA_CFG_WOL 0xc0b6
40 #define PLA_MAR 0xcd00
41 #define PAL_BDC_CR 0xd1a0
42 #define PLA_LEDSEL 0xdd90
43 #define PLA_LED_FEATURE 0xdd92
44 #define PLA_PHYAR 0xde00
45 #define PLA_GPHY_INTR_IMR 0xe022
46 #define PLA_EEE_CR 0xe040
47 #define PLA_EEEP_CR 0xe080
48 #define PLA_MAC_PWR_CTRL 0xe0c0
49 #define PLA_TCR0 0xe610
50 #define PLA_TCR1 0xe612
51 #define PLA_TXFIFO_CTRL 0xe618
52 #define PLA_RSTTELLY 0xe800
54 #define PLA_CRWECR 0xe81c
55 #define PLA_CONFIG5 0xe822
56 #define PLA_PHY_PWR 0xe84c
57 #define PLA_OOB_CTRL 0xe84f
58 #define PLA_CPCR 0xe854
59 #define PLA_MISC_0 0xe858
60 #define PLA_MISC_1 0xe85a
61 #define PLA_OCP_GPHY_BASE 0xe86c
62 #define PLA_TELLYCNT 0xe890
63 #define PLA_SFF_STS_7 0xe8de
64 #define PLA_PHYSTATUS 0xe908
65 #define PLA_BP_BA 0xfc26
66 #define PLA_BP_0 0xfc28
67 #define PLA_BP_1 0xfc2a
68 #define PLA_BP_2 0xfc2c
69 #define PLA_BP_3 0xfc2e
70 #define PLA_BP_4 0xfc30
71 #define PLA_BP_5 0xfc32
72 #define PLA_BP_6 0xfc34
73 #define PLA_BP_7 0xfc36
75 #define USB_DEV_STAT 0xb808
76 #define USB_USB_CTRL 0xd406
77 #define USB_PHY_CTRL 0xd408
78 #define USB_TX_AGG 0xd40a
79 #define USB_RX_BUF_TH 0xd40c
80 #define USB_USB_TIMER 0xd428
81 #define USB_PM_CTRL_STATUS 0xd432
82 #define USB_TX_DMA 0xd434
83 #define USB_UPS_CTRL 0xd800
84 #define USB_BP_BA 0xfc26
85 #define USB_BP_0 0xfc28
86 #define USB_BP_1 0xfc2a
87 #define USB_BP_2 0xfc2c
88 #define USB_BP_3 0xfc2e
89 #define USB_BP_4 0xfc30
90 #define USB_BP_5 0xfc32
91 #define USB_BP_6 0xfc34
92 #define USB_BP_7 0xfc36
95 #define OCP_ALDPS_CONFIG 0x2010
96 #define OCP_EEE_CONFIG1 0x2080
97 #define OCP_EEE_CONFIG2 0x2092
98 #define OCP_EEE_CONFIG3 0x2094
99 #define OCP_EEE_AR 0xa41a
100 #define OCP_EEE_DATA 0xa41c
103 #define RCR_AAP 0x00000001
104 #define RCR_APM 0x00000002
105 #define RCR_AM 0x00000004
106 #define RCR_AB 0x00000008
107 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
109 /* PLA_RXFIFO_CTRL0 */
110 #define RXFIFO_THR1_NORMAL 0x00080002
111 #define RXFIFO_THR1_OOB 0x01800003
113 /* PLA_RXFIFO_CTRL1 */
114 #define RXFIFO_THR2_FULL 0x00000060
115 #define RXFIFO_THR2_HIGH 0x00000038
116 #define RXFIFO_THR2_OOB 0x0000004a
118 /* PLA_RXFIFO_CTRL2 */
119 #define RXFIFO_THR3_FULL 0x00000078
120 #define RXFIFO_THR3_HIGH 0x00000048
121 #define RXFIFO_THR3_OOB 0x0000005a
123 /* PLA_TXFIFO_CTRL */
124 #define TXFIFO_THR_NORMAL 0x00400008
127 #define FMC_FCR_MCU_EN 0x0001
130 #define EEEP_CR_EEEP_TX 0x0002
133 #define TCR0_TX_EMPTY 0x0800
134 #define TCR0_AUTO_FIFO 0x0080
137 #define VERSION_MASK 0x7cf0
145 #define CRWECR_NORAML 0x00
146 #define CRWECR_CONFIG 0xc0
149 #define NOW_IS_OOB 0x80
150 #define TXFIFO_EMPTY 0x20
151 #define RXFIFO_EMPTY 0x10
152 #define LINK_LIST_READY 0x02
153 #define DIS_MCU_CLROOB 0x01
154 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
157 #define RXDY_GATED_EN 0x0008
160 #define RE_INIT_LL 0x8000
161 #define MCU_BORW_EN 0x4000
164 #define CPCR_RX_VLAN 0x0040
167 #define MAGIC_EN 0x0001
170 #define ALDPS_PROXY_MODE 0x0001
173 #define LAN_WAKE_EN 0x0002
175 /* PLA_LED_FEATURE */
176 #define LED_MODE_MASK 0x0700
179 #define TX_10M_IDLE_EN 0x0080
180 #define PFM_PWM_SWITCH 0x0040
182 /* PLA_MAC_PWR_CTRL */
183 #define D3_CLK_GATED_EN 0x00004000
184 #define MCU_CLK_RATIO 0x07010f07
185 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
187 /* PLA_GPHY_INTR_IMR */
188 #define GPHY_STS_MSK 0x0001
189 #define SPEED_DOWN_MSK 0x0002
190 #define SPDWN_RXDV_MSK 0x0004
191 #define SPDWN_LINKCHG_MSK 0x0008
194 #define PHYAR_FLAG 0x80000000
197 #define EEE_RX_EN 0x0001
198 #define EEE_TX_EN 0x0002
201 #define STAT_SPEED_MASK 0x0006
202 #define STAT_SPEED_HIGH 0x0000
203 #define STAT_SPEED_FULL 0x0001
206 #define TX_AGG_MAX_THRESHOLD 0x03
209 #define RX_BUF_THR 0x7a120180
212 #define TEST_MODE_DISABLE 0x00000001
213 #define TX_SIZE_ADJUST1 0x00000100
216 #define POWER_CUT 0x0100
218 /* USB_PM_CTRL_STATUS */
219 #define RWSUME_INDICATE 0x0001
222 #define RX_AGG_DISABLE 0x0010
224 /* OCP_ALDPS_CONFIG */
225 #define ENPWRSAVE 0x8000
226 #define ENPDNPS 0x0200
227 #define LINKENA 0x0100
228 #define DIS_SDSAVE 0x0010
230 /* OCP_EEE_CONFIG1 */
231 #define RG_TXLPI_MSK_HFDUP 0x8000
232 #define RG_MATCLR_EN 0x4000
233 #define EEE_10_CAP 0x2000
234 #define EEE_NWAY_EN 0x1000
235 #define TX_QUIET_EN 0x0200
236 #define RX_QUIET_EN 0x0100
237 #define SDRISETIME 0x0010 /* bit 4 ~ 6 */
238 #define RG_RXLPI_MSK_HFDUP 0x0008
239 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
241 /* OCP_EEE_CONFIG2 */
242 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
243 #define RG_DACQUIET_EN 0x0400
244 #define RG_LDVQUIET_EN 0x0200
245 #define RG_CKRSEL 0x0020
246 #define RG_EEEPRG_EN 0x0010
248 /* OCP_EEE_CONFIG3 */
249 #define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
250 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
251 #define MSK_PH 0x0006 /* bit 0 ~ 3 */
254 /* bit[15:14] function */
255 #define FUN_ADDR 0x0000
256 #define FUN_DATA 0x4000
257 /* bit[4:0] device addr */
258 #define DEVICE_ADDR 0x0007
261 #define EEE_ADDR 0x003C
262 #define EEE_DATA 0x0002
264 enum rtl_register_content {
271 #define RTL8152_MAX_TX 10
272 #define RTL8152_MAX_RX 10
274 #define RTL8152_REQT_READ 0xc0
275 #define RTL8152_REQT_WRITE 0x40
276 #define RTL8152_REQ_GET_REGS 0x05
277 #define RTL8152_REQ_SET_REGS 0x05
279 #define BYTE_EN_DWORD 0xff
280 #define BYTE_EN_WORD 0x33
281 #define BYTE_EN_BYTE 0x11
282 #define BYTE_EN_SIX_BYTES 0x3f
283 #define BYTE_EN_START_MASK 0x0f
284 #define BYTE_EN_END_MASK 0xf0
286 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
287 #define RTL8152_TX_TIMEOUT (HZ)
296 /* Define these values to match your device */
297 #define VENDOR_ID_REALTEK 0x0bda
298 #define PRODUCT_ID_RTL8152 0x8152
300 #define MCU_TYPE_PLA 0x0100
301 #define MCU_TYPE_USB 0x0000
305 #define RX_LEN_MASK 0x7fff
315 #define TX_FS (1 << 31) /* First segment of a packet */
316 #define TX_LS (1 << 30) /* Final segment of a packet */
317 #define TX_LEN_MASK 0xffff
322 struct list_head list;
330 struct list_head list;
341 struct usb_device *udev;
342 struct tasklet_struct tl;
343 struct net_device *netdev;
344 struct tx_agg tx_info[RTL8152_MAX_TX];
345 struct rx_agg rx_info[RTL8152_MAX_RX];
346 struct list_head rx_done, tx_free;
347 struct sk_buff_head tx_queue;
348 spinlock_t rx_lock, tx_lock;
349 struct delayed_work schedule;
350 struct mii_if_info mii;
363 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
364 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
366 static const int multicast_filter_limit = 32;
367 static unsigned int rx_buf_sz = 16384;
370 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
375 tmp = kmalloc(size, GFP_KERNEL);
379 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
380 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
381 value, index, tmp, size, 500);
383 memcpy(data, tmp, size);
390 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
395 tmp = kmalloc(size, GFP_KERNEL);
399 memcpy(tmp, data, size);
401 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
402 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
403 value, index, tmp, size, 500);
409 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
410 void *data, u16 type)
415 if (test_bit(RTL8152_UNPLUG, &tp->flags))
418 /* both size and indix must be 4 bytes align */
419 if ((size & 3) || !size || (index & 3) || !data)
422 if ((u32)index + (u32)size > 0xffff)
427 ret = get_registers(tp, index, type, limit, data);
435 ret = get_registers(tp, index, type, size, data);
449 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
450 u16 size, void *data, u16 type)
453 u16 byteen_start, byteen_end, byen;
456 if (test_bit(RTL8152_UNPLUG, &tp->flags))
459 /* both size and indix must be 4 bytes align */
460 if ((size & 3) || !size || (index & 3) || !data)
463 if ((u32)index + (u32)size > 0xffff)
466 byteen_start = byteen & BYTE_EN_START_MASK;
467 byteen_end = byteen & BYTE_EN_END_MASK;
469 byen = byteen_start | (byteen_start << 4);
470 ret = set_registers(tp, index, type | byen, 4, data);
483 ret = set_registers(tp, index,
484 type | BYTE_EN_DWORD,
493 ret = set_registers(tp, index,
494 type | BYTE_EN_DWORD,
506 byen = byteen_end | (byteen_end >> 4);
507 ret = set_registers(tp, index, type | byen, 4, data);
517 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
519 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
523 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
525 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
529 int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
531 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
535 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
537 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
540 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
544 generic_ocp_read(tp, index, sizeof(data), &data, type);
546 return __le32_to_cpu(data);
549 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
551 __le32 tmp = __cpu_to_le32(data);
553 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
556 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
560 u8 shift = index & 2;
564 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
566 data = __le32_to_cpu(tmp);
567 data >>= (shift * 8);
573 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
577 u16 byen = BYTE_EN_WORD;
578 u8 shift = index & 2;
584 mask <<= (shift * 8);
585 data <<= (shift * 8);
589 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
591 data |= __le32_to_cpu(tmp) & ~mask;
592 tmp = __cpu_to_le32(data);
594 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
597 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
601 u8 shift = index & 3;
605 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
607 data = __le32_to_cpu(tmp);
608 data >>= (shift * 8);
614 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
618 u16 byen = BYTE_EN_BYTE;
619 u8 shift = index & 3;
625 mask <<= (shift * 8);
626 data <<= (shift * 8);
630 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
632 data |= __le32_to_cpu(tmp) & ~mask;
633 tmp = __cpu_to_le32(data);
635 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
638 static void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
643 ocp_data = PHYAR_FLAG | ((reg_addr & 0x1f) << 16) |
646 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
648 for (i = 20; i > 0; i--) {
650 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
651 if (!(ocp_data & PHYAR_FLAG))
657 static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
662 ocp_data = (reg_addr & 0x1f) << 16;
663 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
665 for (i = 20; i > 0; i--) {
667 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
668 if (ocp_data & PHYAR_FLAG)
673 if (!(ocp_data & PHYAR_FLAG))
676 return (u16)(ocp_data & 0xffff);
679 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
681 struct r8152 *tp = netdev_priv(netdev);
683 if (phy_id != R8152_PHY_ID)
686 return r8152_mdio_read(tp, reg);
690 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
692 struct r8152 *tp = netdev_priv(netdev);
694 if (phy_id != R8152_PHY_ID)
697 r8152_mdio_write(tp, reg, val);
700 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
702 u16 ocp_base, ocp_index;
704 ocp_base = addr & 0xf000;
705 if (ocp_base != tp->ocp_base) {
706 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
707 tp->ocp_base = ocp_base;
710 ocp_index = (addr & 0x0fff) | 0xb000;
711 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
715 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
717 static inline void set_ethernet_addr(struct r8152 *tp)
719 struct net_device *dev = tp->netdev;
722 if (pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id) < 0)
723 netif_notice(tp, probe, dev, "inet addr fail\n");
725 memcpy(dev->dev_addr, node_id, dev->addr_len);
726 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
730 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
732 struct r8152 *tp = netdev_priv(netdev);
733 struct sockaddr *addr = p;
735 if (!is_valid_ether_addr(addr->sa_data))
736 return -EADDRNOTAVAIL;
738 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
740 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
741 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
742 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
747 static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
752 static void read_bulk_callback(struct urb *urb)
754 struct net_device *netdev;
755 unsigned long lockflags;
756 int status = urb->status;
769 if (test_bit(RTL8152_UNPLUG, &tp->flags))
772 if (!test_bit(WORK_ENABLE, &tp->flags))
776 if (!netif_carrier_ok(netdev))
781 if (urb->actual_length < ETH_ZLEN)
784 spin_lock_irqsave(&tp->rx_lock, lockflags);
785 list_add_tail(&agg->list, &tp->rx_done);
786 spin_unlock_irqrestore(&tp->rx_lock, lockflags);
787 tasklet_schedule(&tp->tl);
790 set_bit(RTL8152_UNPLUG, &tp->flags);
791 netif_device_detach(tp->netdev);
794 return; /* the urb is in unlink state */
796 pr_warn_ratelimited("may be reset is needed?..\n");
799 pr_warn_ratelimited("Rx status %d\n", status);
803 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
804 if (result == -ENODEV) {
805 netif_device_detach(tp->netdev);
807 spin_lock_irqsave(&tp->rx_lock, lockflags);
808 list_add_tail(&agg->list, &tp->rx_done);
809 spin_unlock_irqrestore(&tp->rx_lock, lockflags);
810 tasklet_schedule(&tp->tl);
814 static void write_bulk_callback(struct urb *urb)
816 struct net_device_stats *stats;
817 unsigned long lockflags;
820 int status = urb->status;
830 stats = rtl8152_get_stats(tp->netdev);
832 pr_warn_ratelimited("Tx status %d\n", status);
833 stats->tx_errors += agg->skb_num;
835 stats->tx_packets += agg->skb_num;
836 stats->tx_bytes += agg->skb_len;
839 spin_lock_irqsave(&tp->tx_lock, lockflags);
840 list_add_tail(&agg->list, &tp->tx_free);
841 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
843 if (!netif_carrier_ok(tp->netdev))
846 if (!test_bit(WORK_ENABLE, &tp->flags))
849 if (test_bit(RTL8152_UNPLUG, &tp->flags))
852 if (!skb_queue_empty(&tp->tx_queue))
853 tasklet_schedule(&tp->tl);
856 static inline void *rx_agg_align(void *data)
858 return (void *)ALIGN((uintptr_t)data, 8);
861 static inline void *tx_agg_align(void *data)
863 return (void *)ALIGN((uintptr_t)data, 4);
866 static void free_all_mem(struct r8152 *tp)
870 for (i = 0; i < RTL8152_MAX_RX; i++) {
871 if (tp->rx_info[i].urb) {
872 usb_free_urb(tp->rx_info[i].urb);
873 tp->rx_info[i].urb = NULL;
876 if (tp->rx_info[i].buffer) {
877 kfree(tp->rx_info[i].buffer);
878 tp->rx_info[i].buffer = NULL;
879 tp->rx_info[i].head = NULL;
883 for (i = 0; i < RTL8152_MAX_TX; i++) {
884 if (tp->tx_info[i].urb) {
885 usb_free_urb(tp->tx_info[i].urb);
886 tp->tx_info[i].urb = NULL;
889 if (tp->tx_info[i].buffer) {
890 kfree(tp->tx_info[i].buffer);
891 tp->tx_info[i].buffer = NULL;
892 tp->tx_info[i].head = NULL;
897 static int alloc_all_mem(struct r8152 *tp)
899 struct net_device *netdev = tp->netdev;
904 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
906 spin_lock_init(&tp->rx_lock);
907 spin_lock_init(&tp->tx_lock);
908 INIT_LIST_HEAD(&tp->rx_done);
909 INIT_LIST_HEAD(&tp->tx_free);
910 skb_queue_head_init(&tp->tx_queue);
912 for (i = 0; i < RTL8152_MAX_RX; i++) {
913 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
917 if (buf != rx_agg_align(buf)) {
919 buf = kmalloc_node(rx_buf_sz + 8, GFP_KERNEL, node);
924 urb = usb_alloc_urb(0, GFP_KERNEL);
930 INIT_LIST_HEAD(&tp->rx_info[i].list);
931 tp->rx_info[i].context = tp;
932 tp->rx_info[i].urb = urb;
933 tp->rx_info[i].buffer = buf;
934 tp->rx_info[i].head = rx_agg_align(buf);
937 for (i = 0; i < RTL8152_MAX_TX; i++) {
938 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
942 if (buf != tx_agg_align(buf)) {
944 buf = kmalloc_node(rx_buf_sz + 4, GFP_KERNEL, node);
949 urb = usb_alloc_urb(0, GFP_KERNEL);
955 INIT_LIST_HEAD(&tp->tx_info[i].list);
956 tp->tx_info[i].context = tp;
957 tp->tx_info[i].urb = urb;
958 tp->tx_info[i].buffer = buf;
959 tp->tx_info[i].head = tx_agg_align(buf);
961 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
971 static void rx_bottom(struct r8152 *tp)
973 struct net_device_stats *stats;
974 struct net_device *netdev;
976 struct rx_desc *rx_desc;
977 unsigned long lockflags;
978 struct list_head *cursor, *next;
988 stats = rtl8152_get_stats(netdev);
990 spin_lock_irqsave(&tp->rx_lock, lockflags);
991 list_for_each_safe(cursor, next, &tp->rx_done) {
992 list_del_init(cursor);
993 spin_unlock_irqrestore(&tp->rx_lock, lockflags);
995 agg = list_entry(cursor, struct rx_agg, list);
997 if (urb->actual_length < ETH_ZLEN) {
998 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
999 spin_lock_irqsave(&tp->rx_lock, lockflags);
1000 if (ret && ret != -ENODEV) {
1001 list_add_tail(&agg->list, next);
1002 tasklet_schedule(&tp->tl);
1008 rx_desc = agg->head;
1009 rx_data = agg->head;
1010 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
1011 len_used += sizeof(struct rx_desc) + pkt_len;
1013 while (urb->actual_length >= len_used) {
1014 if (pkt_len < ETH_ZLEN)
1017 pkt_len -= 4; /* CRC */
1018 rx_data += sizeof(struct rx_desc);
1020 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1022 stats->rx_dropped++;
1025 memcpy(skb->data, rx_data, pkt_len);
1026 skb_put(skb, pkt_len);
1027 skb->protocol = eth_type_trans(skb, netdev);
1029 stats->rx_packets++;
1030 stats->rx_bytes += pkt_len;
1032 rx_data = rx_agg_align(rx_data + pkt_len + 4);
1033 rx_desc = (struct rx_desc *)rx_data;
1034 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
1035 len_used = (int)(rx_data - (u8 *)agg->head);
1036 len_used += sizeof(struct rx_desc) + pkt_len;
1039 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1040 spin_lock_irqsave(&tp->rx_lock, lockflags);
1041 if (ret && ret != -ENODEV) {
1042 list_add_tail(&agg->list, next);
1043 tasklet_schedule(&tp->tl);
1046 spin_unlock_irqrestore(&tp->rx_lock, lockflags);
1049 static void tx_bottom(struct r8152 *tp)
1051 struct net_device_stats *stats;
1052 struct net_device *netdev;
1054 unsigned long lockflags;
1059 netdev = tp->netdev;
1063 spin_lock_irqsave(&tp->tx_lock, lockflags);
1064 if (!skb_queue_empty(&tp->tx_queue) && !list_empty(&tp->tx_free)) {
1065 struct list_head *cursor;
1067 cursor = tp->tx_free.next;
1068 list_del_init(cursor);
1069 agg = list_entry(cursor, struct tx_agg, list);
1071 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
1076 tx_data = agg->head;
1077 agg->skb_num = agg->skb_len = 0;
1078 remain = rx_buf_sz - sizeof(struct tx_desc);
1081 while (remain >= ETH_ZLEN) {
1082 struct tx_desc *tx_desc;
1083 struct sk_buff *skb;
1086 skb = skb_dequeue(&tp->tx_queue);
1092 skb_queue_head(&tp->tx_queue, skb);
1096 tx_data = tx_agg_align(tx_data);
1097 tx_desc = (struct tx_desc *)tx_data;
1098 tx_data += sizeof(*tx_desc);
1100 tx_desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS |
1102 memcpy(tx_data, skb->data, len);
1104 agg->skb_len += len;
1105 dev_kfree_skb_any(skb);
1108 remain = rx_buf_sz - sizeof(*tx_desc) -
1109 (u32)(tx_agg_align(tx_data) - agg->head);
1112 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1113 agg->head, (int)(tx_data - (u8 *)agg->head),
1114 (usb_complete_t)write_bulk_callback, agg);
1115 res = usb_submit_urb(agg->urb, GFP_ATOMIC);
1117 stats = rtl8152_get_stats(netdev);
1120 /* Can we get/handle EPIPE here? */
1121 if (res == -ENODEV) {
1122 netif_device_detach(netdev);
1124 netif_warn(tp, tx_err, netdev,
1125 "failed tx_urb %d\n", res);
1126 stats->tx_dropped += agg->skb_num;
1127 spin_lock_irqsave(&tp->tx_lock, lockflags);
1128 list_add_tail(&agg->list, &tp->tx_free);
1129 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
1136 static void bottom_half(unsigned long data)
1140 tp = (struct r8152 *)data;
1142 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1145 if (!test_bit(WORK_ENABLE, &tp->flags))
1148 if (!netif_carrier_ok(tp->netdev))
1156 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1158 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1159 agg->head, rx_buf_sz,
1160 (usb_complete_t)read_bulk_callback, agg);
1162 return usb_submit_urb(agg->urb, mem_flags);
1165 static void rtl8152_tx_timeout(struct net_device *netdev)
1167 struct r8152 *tp = netdev_priv(netdev);
1170 netif_warn(tp, tx_err, netdev, "Tx timeout.\n");
1171 for (i = 0; i < RTL8152_MAX_TX; i++)
1172 usb_unlink_urb(tp->tx_info[i].urb);
1175 static void rtl8152_set_rx_mode(struct net_device *netdev)
1177 struct r8152 *tp = netdev_priv(netdev);
1179 if (tp->speed & LINK_STATUS)
1180 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1183 static void _rtl8152_set_rx_mode(struct net_device *netdev)
1185 struct r8152 *tp = netdev_priv(netdev);
1186 u32 mc_filter[2]; /* Multicast hash filter */
1190 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1191 netif_stop_queue(netdev);
1192 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1193 ocp_data &= ~RCR_ACPT_ALL;
1194 ocp_data |= RCR_AB | RCR_APM;
1196 if (netdev->flags & IFF_PROMISC) {
1197 /* Unconditionally log net taps. */
1198 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1199 ocp_data |= RCR_AM | RCR_AAP;
1200 mc_filter[1] = mc_filter[0] = 0xffffffff;
1201 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1202 (netdev->flags & IFF_ALLMULTI)) {
1203 /* Too many to filter perfectly -- accept all multicasts. */
1205 mc_filter[1] = mc_filter[0] = 0xffffffff;
1207 struct netdev_hw_addr *ha;
1209 mc_filter[1] = mc_filter[0] = 0;
1210 netdev_for_each_mc_addr(ha, netdev) {
1211 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1212 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1217 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1218 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
1220 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
1221 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1222 netif_wake_queue(netdev);
1225 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1226 struct net_device *netdev)
1228 struct r8152 *tp = netdev_priv(netdev);
1229 struct net_device_stats *stats = rtl8152_get_stats(netdev);
1230 unsigned long lockflags;
1231 struct tx_agg *agg = NULL;
1232 struct tx_desc *tx_desc;
1237 skb_tx_timestamp(skb);
1239 spin_lock_irqsave(&tp->tx_lock, lockflags);
1240 if (!list_empty(&tp->tx_free) && skb_queue_empty(&tp->tx_queue)) {
1241 struct list_head *cursor;
1243 cursor = tp->tx_free.next;
1244 list_del_init(cursor);
1245 agg = list_entry(cursor, struct tx_agg, list);
1247 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
1250 skb_queue_tail(&tp->tx_queue, skb);
1251 return NETDEV_TX_OK;
1254 tx_desc = (struct tx_desc *)agg->head;
1255 tx_data = agg->head + sizeof(*tx_desc);
1256 agg->skb_num = agg->skb_len = 0;
1259 tx_desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1260 memcpy(tx_data, skb->data, len);
1261 dev_kfree_skb_any(skb);
1263 agg->skb_len += len;
1264 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1265 agg->head, len + sizeof(*tx_desc),
1266 (usb_complete_t)write_bulk_callback, agg);
1267 res = usb_submit_urb(agg->urb, GFP_ATOMIC);
1269 /* Can we get/handle EPIPE here? */
1270 if (res == -ENODEV) {
1271 netif_device_detach(tp->netdev);
1273 netif_warn(tp, tx_err, netdev,
1274 "failed tx_urb %d\n", res);
1275 stats->tx_dropped++;
1276 spin_lock_irqsave(&tp->tx_lock, lockflags);
1277 list_add_tail(&agg->list, &tp->tx_free);
1278 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
1282 return NETDEV_TX_OK;
1285 static void r8152b_reset_packet_filter(struct r8152 *tp)
1289 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1290 ocp_data &= ~FMC_FCR_MCU_EN;
1291 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1292 ocp_data |= FMC_FCR_MCU_EN;
1293 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1296 static void rtl8152_nic_reset(struct r8152 *tp)
1300 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1302 for (i = 0; i < 1000; i++) {
1303 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1309 static inline u8 rtl8152_get_speed(struct r8152 *tp)
1311 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1314 static int rtl8152_enable(struct r8152 *tp)
1320 speed = rtl8152_get_speed(tp);
1321 if (speed & _10bps) {
1322 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
1323 ocp_data |= EEEP_CR_EEEP_TX;
1324 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1326 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
1327 ocp_data &= ~EEEP_CR_EEEP_TX;
1328 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1331 r8152b_reset_packet_filter(tp);
1333 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1334 ocp_data |= CR_RE | CR_TE;
1335 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1337 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1338 ocp_data &= ~RXDY_GATED_EN;
1339 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1341 INIT_LIST_HEAD(&tp->rx_done);
1343 for (i = 0; i < RTL8152_MAX_RX; i++) {
1344 INIT_LIST_HEAD(&tp->rx_info[i].list);
1345 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1351 static void rtl8152_disable(struct r8152 *tp)
1353 struct net_device_stats *stats = rtl8152_get_stats(tp->netdev);
1354 struct sk_buff *skb;
1358 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1359 ocp_data &= ~RCR_ACPT_ALL;
1360 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1362 while ((skb = skb_dequeue(&tp->tx_queue))) {
1364 stats->tx_dropped++;
1367 for (i = 0; i < RTL8152_MAX_TX; i++)
1368 usb_kill_urb(tp->tx_info[i].urb);
1370 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1371 ocp_data |= RXDY_GATED_EN;
1372 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1374 for (i = 0; i < 1000; i++) {
1375 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1376 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1381 for (i = 0; i < 1000; i++) {
1382 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1387 for (i = 0; i < RTL8152_MAX_RX; i++)
1388 usb_kill_urb(tp->rx_info[i].urb);
1390 rtl8152_nic_reset(tp);
1393 static void r8152b_exit_oob(struct r8152 *tp)
1398 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1399 ocp_data &= ~RCR_ACPT_ALL;
1400 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1402 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1403 ocp_data |= RXDY_GATED_EN;
1404 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1406 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1407 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1409 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1410 ocp_data &= ~NOW_IS_OOB;
1411 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1413 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1414 ocp_data &= ~MCU_BORW_EN;
1415 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1417 for (i = 0; i < 1000; i++) {
1418 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1419 if (ocp_data & LINK_LIST_READY)
1424 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1425 ocp_data |= RE_INIT_LL;
1426 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1428 for (i = 0; i < 1000; i++) {
1429 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1430 if (ocp_data & LINK_LIST_READY)
1435 rtl8152_nic_reset(tp);
1437 /* rx share fifo credit full threshold */
1438 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1440 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1441 ocp_data &= STAT_SPEED_MASK;
1442 if (ocp_data == STAT_SPEED_FULL) {
1443 /* rx share fifo credit near full threshold */
1444 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1446 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1449 /* rx share fifo credit near full threshold */
1450 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1452 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1456 /* TX share fifo free credit full threshold */
1457 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1459 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
1460 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_BUF_THR);
1461 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1462 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1464 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1465 ocp_data &= ~CPCR_RX_VLAN;
1466 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1468 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1470 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1471 ocp_data |= TCR0_AUTO_FIFO;
1472 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1475 static void r8152b_enter_oob(struct r8152 *tp)
1480 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1481 ocp_data &= ~NOW_IS_OOB;
1482 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1484 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1485 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1486 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1488 rtl8152_disable(tp);
1490 for (i = 0; i < 1000; i++) {
1491 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1492 if (ocp_data & LINK_LIST_READY)
1497 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1498 ocp_data |= RE_INIT_LL;
1499 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1501 for (i = 0; i < 1000; i++) {
1502 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1503 if (ocp_data & LINK_LIST_READY)
1508 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1510 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1511 ocp_data |= MAGIC_EN;
1512 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1514 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1515 ocp_data |= CPCR_RX_VLAN;
1516 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1518 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
1519 ocp_data |= ALDPS_PROXY_MODE;
1520 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
1522 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1523 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1524 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1526 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
1528 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1529 ocp_data &= ~RXDY_GATED_EN;
1530 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1532 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1533 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1534 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1537 static void r8152b_disable_aldps(struct r8152 *tp)
1539 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1543 static inline void r8152b_enable_aldps(struct r8152 *tp)
1545 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1546 LINKENA | DIS_SDSAVE);
1549 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
1554 cancel_delayed_work_sync(&tp->schedule);
1555 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1556 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1557 ADVERTISE_100HALF | ADVERTISE_100FULL);
1559 if (autoneg == AUTONEG_DISABLE) {
1560 if (speed == SPEED_10) {
1562 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1563 } else if (speed == SPEED_100) {
1564 bmcr = BMCR_SPEED100;
1565 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1571 if (duplex == DUPLEX_FULL)
1572 bmcr |= BMCR_FULLDPLX;
1574 if (speed == SPEED_10) {
1575 if (duplex == DUPLEX_FULL)
1576 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1578 anar |= ADVERTISE_10HALF;
1579 } else if (speed == SPEED_100) {
1580 if (duplex == DUPLEX_FULL) {
1581 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1582 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1584 anar |= ADVERTISE_10HALF;
1585 anar |= ADVERTISE_100HALF;
1592 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1595 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1596 r8152_mdio_write(tp, MII_BMCR, bmcr);
1599 schedule_delayed_work(&tp->schedule, 5 * HZ);
1604 static void rtl8152_down(struct r8152 *tp)
1608 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1609 ocp_data &= ~POWER_CUT;
1610 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1612 r8152b_disable_aldps(tp);
1613 r8152b_enter_oob(tp);
1614 r8152b_enable_aldps(tp);
1617 static void set_carrier(struct r8152 *tp)
1619 struct net_device *netdev = tp->netdev;
1622 speed = rtl8152_get_speed(tp);
1624 if (speed & LINK_STATUS) {
1625 if (!(tp->speed & LINK_STATUS)) {
1627 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1628 netif_carrier_on(netdev);
1631 if (tp->speed & LINK_STATUS) {
1632 netif_carrier_off(netdev);
1633 tasklet_disable(&tp->tl);
1634 rtl8152_disable(tp);
1635 tasklet_enable(&tp->tl);
1641 static void rtl_work_func_t(struct work_struct *work)
1643 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
1645 if (!test_bit(WORK_ENABLE, &tp->flags))
1648 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1653 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
1654 _rtl8152_set_rx_mode(tp->netdev);
1656 schedule_delayed_work(&tp->schedule, HZ);
1662 static int rtl8152_open(struct net_device *netdev)
1664 struct r8152 *tp = netdev_priv(netdev);
1667 tp->speed = rtl8152_get_speed(tp);
1668 if (tp->speed & LINK_STATUS) {
1669 res = rtl8152_enable(tp);
1672 netif_device_detach(tp->netdev);
1674 netif_err(tp, ifup, netdev,
1675 "rtl8152_open failed: %d\n", res);
1679 netif_carrier_on(netdev);
1681 netif_stop_queue(netdev);
1682 netif_carrier_off(netdev);
1685 rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL);
1686 netif_start_queue(netdev);
1687 set_bit(WORK_ENABLE, &tp->flags);
1688 schedule_delayed_work(&tp->schedule, 0);
1693 static int rtl8152_close(struct net_device *netdev)
1695 struct r8152 *tp = netdev_priv(netdev);
1698 clear_bit(WORK_ENABLE, &tp->flags);
1699 cancel_delayed_work_sync(&tp->schedule);
1700 netif_stop_queue(netdev);
1701 tasklet_disable(&tp->tl);
1702 rtl8152_disable(tp);
1703 tasklet_enable(&tp->tl);
1708 static void rtl_clear_bp(struct r8152 *tp)
1710 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1711 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1712 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1713 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1714 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1715 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1716 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1717 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1719 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1720 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1723 static void r8152b_enable_eee(struct r8152 *tp)
1727 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
1728 ocp_data |= EEE_RX_EN | EEE_TX_EN;
1729 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
1730 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
1731 EEE_10_CAP | EEE_NWAY_EN |
1732 TX_QUIET_EN | RX_QUIET_EN |
1733 SDRISETIME | RG_RXLPI_MSK_HFDUP |
1735 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
1736 RG_LDVQUIET_EN | RG_CKRSEL |
1738 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
1739 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
1740 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
1741 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
1742 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
1743 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
1746 static void r8152b_enable_fc(struct r8152 *tp)
1750 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1751 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1752 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1755 static void r8152b_hw_phy_cfg(struct r8152 *tp)
1757 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1758 r8152b_disable_aldps(tp);
1761 static void r8152b_init(struct r8152 *tp)
1768 if (tp->version == RTL_VER_01) {
1769 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
1770 ocp_data &= ~LED_MODE_MASK;
1771 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
1774 r8152b_hw_phy_cfg(tp);
1776 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1777 ocp_data &= ~POWER_CUT;
1778 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1780 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1781 ocp_data &= ~RWSUME_INDICATE;
1782 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1784 r8152b_exit_oob(tp);
1786 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1787 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
1788 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1789 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
1790 ocp_data &= ~MCU_CLK_RATIO_MASK;
1791 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
1792 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
1793 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
1794 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
1795 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
1797 r8152b_enable_eee(tp);
1798 r8152b_enable_aldps(tp);
1799 r8152b_enable_fc(tp);
1801 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
1803 for (i = 0; i < 100; i++) {
1805 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
1809 /* enable rx aggregation */
1810 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
1811 ocp_data &= ~RX_AGG_DISABLE;
1812 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
1815 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
1817 struct r8152 *tp = usb_get_intfdata(intf);
1819 netif_device_detach(tp->netdev);
1821 if (netif_running(tp->netdev)) {
1822 clear_bit(WORK_ENABLE, &tp->flags);
1823 cancel_delayed_work_sync(&tp->schedule);
1824 tasklet_disable(&tp->tl);
1832 static int rtl8152_resume(struct usb_interface *intf)
1834 struct r8152 *tp = usb_get_intfdata(intf);
1837 netif_device_attach(tp->netdev);
1838 if (netif_running(tp->netdev)) {
1840 set_bit(WORK_ENABLE, &tp->flags);
1841 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1842 schedule_delayed_work(&tp->schedule, 0);
1843 tasklet_enable(&tp->tl);
1849 static void rtl8152_get_drvinfo(struct net_device *netdev,
1850 struct ethtool_drvinfo *info)
1852 struct r8152 *tp = netdev_priv(netdev);
1854 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
1855 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
1856 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
1860 int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1862 struct r8152 *tp = netdev_priv(netdev);
1864 if (!tp->mii.mdio_read)
1867 return mii_ethtool_gset(&tp->mii, cmd);
1870 static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1872 struct r8152 *tp = netdev_priv(dev);
1874 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
1877 static struct ethtool_ops ops = {
1878 .get_drvinfo = rtl8152_get_drvinfo,
1879 .get_settings = rtl8152_get_settings,
1880 .set_settings = rtl8152_set_settings,
1881 .get_link = ethtool_op_get_link,
1884 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
1886 struct r8152 *tp = netdev_priv(netdev);
1887 struct mii_ioctl_data *data = if_mii(rq);
1892 data->phy_id = R8152_PHY_ID; /* Internal PHY */
1896 data->val_out = r8152_mdio_read(tp, data->reg_num);
1900 if (!capable(CAP_NET_ADMIN)) {
1904 r8152_mdio_write(tp, data->reg_num, data->val_in);
1914 static const struct net_device_ops rtl8152_netdev_ops = {
1915 .ndo_open = rtl8152_open,
1916 .ndo_stop = rtl8152_close,
1917 .ndo_do_ioctl = rtl8152_ioctl,
1918 .ndo_start_xmit = rtl8152_start_xmit,
1919 .ndo_tx_timeout = rtl8152_tx_timeout,
1920 .ndo_set_rx_mode = rtl8152_set_rx_mode,
1921 .ndo_set_mac_address = rtl8152_set_mac_address,
1923 .ndo_change_mtu = eth_change_mtu,
1924 .ndo_validate_addr = eth_validate_addr,
1927 static void r8152b_get_version(struct r8152 *tp)
1932 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
1933 version = (u16)(ocp_data & VERSION_MASK);
1937 tp->version = RTL_VER_01;
1940 tp->version = RTL_VER_02;
1943 netif_info(tp, probe, tp->netdev,
1944 "Unknown version 0x%04x\n", version);
1949 static int rtl8152_probe(struct usb_interface *intf,
1950 const struct usb_device_id *id)
1952 struct usb_device *udev = interface_to_usbdev(intf);
1954 struct net_device *netdev;
1957 if (udev->actconfig->desc.bConfigurationValue != 1) {
1958 usb_driver_set_configuration(udev, 1);
1962 netdev = alloc_etherdev(sizeof(struct r8152));
1964 dev_err(&intf->dev, "Out of memory");
1968 SET_NETDEV_DEV(netdev, &intf->dev);
1969 tp = netdev_priv(netdev);
1970 memset(tp, 0, sizeof(*tp));
1971 tp->msg_enable = 0x7FFF;
1973 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
1974 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
1977 tp->netdev = netdev;
1978 netdev->netdev_ops = &rtl8152_netdev_ops;
1979 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
1980 netdev->features &= ~NETIF_F_IP_CSUM;
1981 SET_ETHTOOL_OPS(netdev, &ops);
1984 tp->mii.dev = netdev;
1985 tp->mii.mdio_read = read_mii_word;
1986 tp->mii.mdio_write = write_mii_word;
1987 tp->mii.phy_id_mask = 0x3f;
1988 tp->mii.reg_num_mask = 0x1f;
1989 tp->mii.phy_id = R8152_PHY_ID;
1990 tp->mii.supports_gmii = 0;
1992 r8152b_get_version(tp);
1994 set_ethernet_addr(tp);
1996 ret = alloc_all_mem(tp);
2000 usb_set_intfdata(intf, tp);
2002 ret = register_netdev(netdev);
2004 netif_err(tp, probe, netdev, "couldn't register the device");
2008 netif_info(tp, probe, netdev, "%s", DRIVER_VERSION);
2013 usb_set_intfdata(intf, NULL);
2015 free_netdev(netdev);
2019 static void rtl8152_unload(struct r8152 *tp)
2023 if (tp->version != RTL_VER_01) {
2024 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2025 ocp_data |= POWER_CUT;
2026 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2029 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2030 ocp_data &= ~RWSUME_INDICATE;
2031 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2034 static void rtl8152_disconnect(struct usb_interface *intf)
2036 struct r8152 *tp = usb_get_intfdata(intf);
2038 usb_set_intfdata(intf, NULL);
2040 set_bit(RTL8152_UNPLUG, &tp->flags);
2041 tasklet_kill(&tp->tl);
2042 unregister_netdev(tp->netdev);
2045 free_netdev(tp->netdev);
2049 /* table of devices that work with this driver */
2050 static struct usb_device_id rtl8152_table[] = {
2051 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
2055 MODULE_DEVICE_TABLE(usb, rtl8152_table);
2057 static struct usb_driver rtl8152_driver = {
2059 .id_table = rtl8152_table,
2060 .probe = rtl8152_probe,
2061 .disconnect = rtl8152_disconnect,
2062 .suspend = rtl8152_suspend,
2063 .resume = rtl8152_resume,
2064 .reset_resume = rtl8152_resume,
2067 module_usb_driver(rtl8152_driver);
2069 MODULE_AUTHOR(DRIVER_AUTHOR);
2070 MODULE_DESCRIPTION(DRIVER_DESC);
2071 MODULE_LICENSE("GPL");