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>
23 /* Version Information */
24 #define DRIVER_VERSION "v1.0.0 (2013/05/03)"
25 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
26 #define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters"
27 #define MODULENAME "r8152"
29 #define R8152_PHY_ID 32
31 #define PLA_IDR 0xc000
32 #define PLA_RCR 0xc010
33 #define PLA_RMS 0xc016
34 #define PLA_RXFIFO_CTRL0 0xc0a0
35 #define PLA_RXFIFO_CTRL1 0xc0a4
36 #define PLA_RXFIFO_CTRL2 0xc0a8
37 #define PLA_FMC 0xc0b4
38 #define PLA_CFG_WOL 0xc0b6
39 #define PLA_MAR 0xcd00
40 #define PAL_BDC_CR 0xd1a0
41 #define PLA_LEDSEL 0xdd90
42 #define PLA_LED_FEATURE 0xdd92
43 #define PLA_PHYAR 0xde00
44 #define PLA_GPHY_INTR_IMR 0xe022
45 #define PLA_EEE_CR 0xe040
46 #define PLA_EEEP_CR 0xe080
47 #define PLA_MAC_PWR_CTRL 0xe0c0
48 #define PLA_TCR0 0xe610
49 #define PLA_TCR1 0xe612
50 #define PLA_TXFIFO_CTRL 0xe618
51 #define PLA_RSTTELLY 0xe800
53 #define PLA_CRWECR 0xe81c
54 #define PLA_CONFIG5 0xe822
55 #define PLA_PHY_PWR 0xe84c
56 #define PLA_OOB_CTRL 0xe84f
57 #define PLA_CPCR 0xe854
58 #define PLA_MISC_0 0xe858
59 #define PLA_MISC_1 0xe85a
60 #define PLA_OCP_GPHY_BASE 0xe86c
61 #define PLA_TELLYCNT 0xe890
62 #define PLA_SFF_STS_7 0xe8de
63 #define PLA_PHYSTATUS 0xe908
64 #define PLA_BP_BA 0xfc26
65 #define PLA_BP_0 0xfc28
66 #define PLA_BP_1 0xfc2a
67 #define PLA_BP_2 0xfc2c
68 #define PLA_BP_3 0xfc2e
69 #define PLA_BP_4 0xfc30
70 #define PLA_BP_5 0xfc32
71 #define PLA_BP_6 0xfc34
72 #define PLA_BP_7 0xfc36
74 #define USB_DEV_STAT 0xb808
75 #define USB_USB_CTRL 0xd406
76 #define USB_PHY_CTRL 0xd408
77 #define USB_TX_AGG 0xd40a
78 #define USB_RX_BUF_TH 0xd40c
79 #define USB_USB_TIMER 0xd428
80 #define USB_PM_CTRL_STATUS 0xd432
81 #define USB_TX_DMA 0xd434
82 #define USB_UPS_CTRL 0xd800
83 #define USB_BP_BA 0xfc26
84 #define USB_BP_0 0xfc28
85 #define USB_BP_1 0xfc2a
86 #define USB_BP_2 0xfc2c
87 #define USB_BP_3 0xfc2e
88 #define USB_BP_4 0xfc30
89 #define USB_BP_5 0xfc32
90 #define USB_BP_6 0xfc34
91 #define USB_BP_7 0xfc36
94 #define OCP_ALDPS_CONFIG 0x2010
95 #define OCP_EEE_CONFIG1 0x2080
96 #define OCP_EEE_CONFIG2 0x2092
97 #define OCP_EEE_CONFIG3 0x2094
98 #define OCP_EEE_AR 0xa41a
99 #define OCP_EEE_DATA 0xa41c
102 #define RCR_AAP 0x00000001
103 #define RCR_APM 0x00000002
104 #define RCR_AM 0x00000004
105 #define RCR_AB 0x00000008
106 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
108 /* PLA_RXFIFO_CTRL0 */
109 #define RXFIFO_THR1_NORMAL 0x00080002
110 #define RXFIFO_THR1_OOB 0x01800003
112 /* PLA_RXFIFO_CTRL1 */
113 #define RXFIFO_THR2_FULL 0x00000060
114 #define RXFIFO_THR2_HIGH 0x00000038
115 #define RXFIFO_THR2_OOB 0x0000004a
117 /* PLA_RXFIFO_CTRL2 */
118 #define RXFIFO_THR3_FULL 0x00000078
119 #define RXFIFO_THR3_HIGH 0x00000048
120 #define RXFIFO_THR3_OOB 0x0000005a
122 /* PLA_TXFIFO_CTRL */
123 #define TXFIFO_THR_NORMAL 0x00400008
126 #define FMC_FCR_MCU_EN 0x0001
129 #define EEEP_CR_EEEP_TX 0x0002
132 #define TCR0_TX_EMPTY 0x0800
133 #define TCR0_AUTO_FIFO 0x0080
136 #define VERSION_MASK 0x7cf0
144 #define CRWECR_NORAML 0x00
145 #define CRWECR_CONFIG 0xc0
148 #define NOW_IS_OOB 0x80
149 #define TXFIFO_EMPTY 0x20
150 #define RXFIFO_EMPTY 0x10
151 #define LINK_LIST_READY 0x02
152 #define DIS_MCU_CLROOB 0x01
153 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
156 #define RXDY_GATED_EN 0x0008
159 #define RE_INIT_LL 0x8000
160 #define MCU_BORW_EN 0x4000
163 #define CPCR_RX_VLAN 0x0040
166 #define MAGIC_EN 0x0001
169 #define ALDPS_PROXY_MODE 0x0001
172 #define LAN_WAKE_EN 0x0002
174 /* PLA_LED_FEATURE */
175 #define LED_MODE_MASK 0x0700
178 #define TX_10M_IDLE_EN 0x0080
179 #define PFM_PWM_SWITCH 0x0040
181 /* PLA_MAC_PWR_CTRL */
182 #define D3_CLK_GATED_EN 0x00004000
183 #define MCU_CLK_RATIO 0x07010f07
184 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
186 /* PLA_GPHY_INTR_IMR */
187 #define GPHY_STS_MSK 0x0001
188 #define SPEED_DOWN_MSK 0x0002
189 #define SPDWN_RXDV_MSK 0x0004
190 #define SPDWN_LINKCHG_MSK 0x0008
193 #define PHYAR_FLAG 0x80000000
196 #define EEE_RX_EN 0x0001
197 #define EEE_TX_EN 0x0002
200 #define STAT_SPEED_MASK 0x0006
201 #define STAT_SPEED_HIGH 0x0000
202 #define STAT_SPEED_FULL 0x0001
205 #define TX_AGG_MAX_THRESHOLD 0x03
208 #define RX_BUF_THR 0x7a120180
211 #define TEST_MODE_DISABLE 0x00000001
212 #define TX_SIZE_ADJUST1 0x00000100
215 #define POWER_CUT 0x0100
217 /* USB_PM_CTRL_STATUS */
218 #define RWSUME_INDICATE 0x0001
221 #define RX_AGG_DISABLE 0x0010
223 /* OCP_ALDPS_CONFIG */
224 #define ENPWRSAVE 0x8000
225 #define ENPDNPS 0x0200
226 #define LINKENA 0x0100
227 #define DIS_SDSAVE 0x0010
229 /* OCP_EEE_CONFIG1 */
230 #define RG_TXLPI_MSK_HFDUP 0x8000
231 #define RG_MATCLR_EN 0x4000
232 #define EEE_10_CAP 0x2000
233 #define EEE_NWAY_EN 0x1000
234 #define TX_QUIET_EN 0x0200
235 #define RX_QUIET_EN 0x0100
236 #define SDRISETIME 0x0010 /* bit 4 ~ 6 */
237 #define RG_RXLPI_MSK_HFDUP 0x0008
238 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
240 /* OCP_EEE_CONFIG2 */
241 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
242 #define RG_DACQUIET_EN 0x0400
243 #define RG_LDVQUIET_EN 0x0200
244 #define RG_CKRSEL 0x0020
245 #define RG_EEEPRG_EN 0x0010
247 /* OCP_EEE_CONFIG3 */
248 #define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
249 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
250 #define MSK_PH 0x0006 /* bit 0 ~ 3 */
253 /* bit[15:14] function */
254 #define FUN_ADDR 0x0000
255 #define FUN_DATA 0x4000
256 /* bit[4:0] device addr */
257 #define DEVICE_ADDR 0x0007
260 #define EEE_ADDR 0x003C
261 #define EEE_DATA 0x0002
263 enum rtl_register_content {
270 #define RTL8152_REQT_READ 0xc0
271 #define RTL8152_REQT_WRITE 0x40
272 #define RTL8152_REQ_GET_REGS 0x05
273 #define RTL8152_REQ_SET_REGS 0x05
275 #define BYTE_EN_DWORD 0xff
276 #define BYTE_EN_WORD 0x33
277 #define BYTE_EN_BYTE 0x11
278 #define BYTE_EN_SIX_BYTES 0x3f
279 #define BYTE_EN_START_MASK 0x0f
280 #define BYTE_EN_END_MASK 0xf0
282 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
283 #define RTL8152_TX_TIMEOUT (HZ)
293 /* Define these values to match your device */
294 #define VENDOR_ID_REALTEK 0x0bda
295 #define PRODUCT_ID_RTL8152 0x8152
297 #define MCU_TYPE_PLA 0x0100
298 #define MCU_TYPE_USB 0x0000
302 #define RX_LEN_MASK 0x7fff
312 #define TX_FS (1 << 31) /* First segment of a packet */
313 #define TX_LS (1 << 30) /* Final segment of a packet */
314 #define TX_LEN_MASK 0xffff
320 struct usb_device *udev;
321 struct tasklet_struct tl;
322 struct net_device *netdev;
323 struct urb *rx_urb, *tx_urb;
324 struct sk_buff *tx_skb, *rx_skb;
325 struct delayed_work schedule;
326 struct mii_if_info mii;
339 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
340 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
342 static const int multicast_filter_limit = 32;
345 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
350 tmp = kmalloc(size, GFP_KERNEL);
354 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
355 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
356 value, index, tmp, size, 500);
358 memcpy(data, tmp, size);
365 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
370 tmp = kmalloc(size, GFP_KERNEL);
374 memcpy(tmp, data, size);
376 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
377 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
378 value, index, tmp, size, 500);
384 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
385 void *data, u16 type)
390 if (test_bit(RTL8152_UNPLUG, &tp->flags))
393 /* both size and indix must be 4 bytes align */
394 if ((size & 3) || !size || (index & 3) || !data)
397 if ((u32)index + (u32)size > 0xffff)
402 ret = get_registers(tp, index, type, limit, data);
410 ret = get_registers(tp, index, type, size, data);
424 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
425 u16 size, void *data, u16 type)
428 u16 byteen_start, byteen_end, byen;
431 if (test_bit(RTL8152_UNPLUG, &tp->flags))
434 /* both size and indix must be 4 bytes align */
435 if ((size & 3) || !size || (index & 3) || !data)
438 if ((u32)index + (u32)size > 0xffff)
441 byteen_start = byteen & BYTE_EN_START_MASK;
442 byteen_end = byteen & BYTE_EN_END_MASK;
444 byen = byteen_start | (byteen_start << 4);
445 ret = set_registers(tp, index, type | byen, 4, data);
458 ret = set_registers(tp, index,
459 type | BYTE_EN_DWORD,
468 ret = set_registers(tp, index,
469 type | BYTE_EN_DWORD,
481 byen = byteen_end | (byteen_end >> 4);
482 ret = set_registers(tp, index, type | byen, 4, data);
492 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
494 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
498 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
500 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
504 int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
506 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
510 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
512 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
515 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
519 if (type == MCU_TYPE_PLA)
520 pla_ocp_read(tp, index, sizeof(data), &data);
522 usb_ocp_read(tp, index, sizeof(data), &data);
524 return __le32_to_cpu(data);
527 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
529 if (type == MCU_TYPE_PLA)
530 pla_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(data), &data);
532 usb_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(data), &data);
535 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
538 u8 shift = index & 2;
542 if (type == MCU_TYPE_PLA)
543 pla_ocp_read(tp, index, sizeof(data), &data);
545 usb_ocp_read(tp, index, sizeof(data), &data);
547 data = __le32_to_cpu(data);
548 data >>= (shift * 8);
554 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
556 u32 tmp, mask = 0xffff;
557 u16 byen = BYTE_EN_WORD;
558 u8 shift = index & 2;
564 mask <<= (shift * 8);
565 data <<= (shift * 8);
569 if (type == MCU_TYPE_PLA)
570 pla_ocp_read(tp, index, sizeof(tmp), &tmp);
572 usb_ocp_read(tp, index, sizeof(tmp), &tmp);
574 tmp = __le32_to_cpu(tmp) & ~mask;
576 tmp = __cpu_to_le32(tmp);
578 if (type == MCU_TYPE_PLA)
579 pla_ocp_write(tp, index, byen, sizeof(tmp), &tmp);
581 usb_ocp_write(tp, index, byen, sizeof(tmp), &tmp);
584 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
587 u8 shift = index & 3;
591 if (type == MCU_TYPE_PLA)
592 pla_ocp_read(tp, index, sizeof(data), &data);
594 usb_ocp_read(tp, index, sizeof(data), &data);
596 data = __le32_to_cpu(data);
597 data >>= (shift * 8);
603 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
605 u32 tmp, mask = 0xff;
606 u16 byen = BYTE_EN_BYTE;
607 u8 shift = index & 3;
613 mask <<= (shift * 8);
614 data <<= (shift * 8);
618 if (type == MCU_TYPE_PLA)
619 pla_ocp_read(tp, index, sizeof(tmp), &tmp);
621 usb_ocp_read(tp, index, sizeof(tmp), &tmp);
623 tmp = __le32_to_cpu(tmp) & ~mask;
625 tmp = __cpu_to_le32(tmp);
627 if (type == MCU_TYPE_PLA)
628 pla_ocp_write(tp, index, byen, sizeof(tmp), &tmp);
630 usb_ocp_write(tp, index, byen, sizeof(tmp), &tmp);
633 static void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
638 ocp_data = PHYAR_FLAG | ((reg_addr & 0x1f) << 16) |
641 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
643 for (i = 20; i > 0; i--) {
645 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
646 if (!(ocp_data & PHYAR_FLAG))
652 static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
657 ocp_data = (reg_addr & 0x1f) << 16;
658 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
660 for (i = 20; i > 0; i--) {
662 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
663 if (ocp_data & PHYAR_FLAG)
668 if (!(ocp_data & PHYAR_FLAG))
671 return (u16)(ocp_data & 0xffff);
674 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
676 struct r8152 *tp = netdev_priv(netdev);
678 if (phy_id != R8152_PHY_ID)
681 return r8152_mdio_read(tp, reg);
685 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
687 struct r8152 *tp = netdev_priv(netdev);
689 if (phy_id != R8152_PHY_ID)
692 r8152_mdio_write(tp, reg, val);
695 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
697 u16 ocp_base, ocp_index;
699 ocp_base = addr & 0xf000;
700 if (ocp_base != tp->ocp_base) {
701 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
702 tp->ocp_base = ocp_base;
705 ocp_index = (addr & 0x0fff) | 0xb000;
706 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
709 static inline void set_ethernet_addr(struct r8152 *tp)
711 struct net_device *dev = tp->netdev;
714 if (pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id) < 0)
715 netif_notice(tp, probe, dev, "inet addr fail\n");
717 memcpy(dev->dev_addr, node_id, dev->addr_len);
718 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
722 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
724 struct r8152 *tp = netdev_priv(netdev);
725 struct sockaddr *addr = p;
727 if (!is_valid_ether_addr(addr->sa_data))
728 return -EADDRNOTAVAIL;
730 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
732 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
733 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
734 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
739 static int alloc_all_urbs(struct r8152 *tp)
741 tp->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
744 tp->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
746 usb_free_urb(tp->rx_urb);
753 static void free_all_urbs(struct r8152 *tp)
755 usb_free_urb(tp->rx_urb);
756 usb_free_urb(tp->tx_urb);
759 static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
764 static void read_bulk_callback(struct urb *urb)
769 struct net_device *netdev;
770 struct net_device_stats *stats;
771 int status = urb->status;
773 struct rx_desc *rx_desc;
778 if (test_bit(RTL8152_UNPLUG, &tp->flags))
781 if (!netif_device_present(netdev))
784 stats = rtl8152_get_stats(netdev);
789 set_bit(RTL8152_UNPLUG, &tp->flags);
790 netif_device_detach(tp->netdev);
792 return; /* the urb is in unlink state */
794 pr_warn_ratelimited("may be reset is needed?..\n");
797 pr_warn_ratelimited("Rx status %d\n", status);
801 /* protect against short packets (tell me why we got some?!?) */
802 if (urb->actual_length < sizeof(*rx_desc))
806 rx_desc = (struct rx_desc *)urb->transfer_buffer;
807 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
808 if (urb->actual_length < sizeof(struct rx_desc) + pkt_len)
811 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
815 memcpy(skb->data, tp->rx_skb->data + sizeof(struct rx_desc), pkt_len);
816 skb_put(skb, pkt_len);
817 skb->protocol = eth_type_trans(skb, netdev);
820 stats->rx_bytes += pkt_len;
822 usb_fill_bulk_urb(tp->rx_urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
823 tp->rx_skb->data, RTL8152_RMS + sizeof(struct rx_desc),
824 (usb_complete_t)read_bulk_callback, tp);
825 result = usb_submit_urb(tp->rx_urb, GFP_ATOMIC);
826 if (result == -ENODEV) {
827 netif_device_detach(tp->netdev);
829 set_bit(RX_URB_FAIL, &tp->flags);
832 clear_bit(RX_URB_FAIL, &tp->flags);
837 tasklet_schedule(&tp->tl);
840 static void rx_fixup(unsigned long data)
845 tp = (struct r8152 *)data;
846 if (!test_bit(WORK_ENABLE, &tp->flags))
849 status = usb_submit_urb(tp->rx_urb, GFP_ATOMIC);
850 if (status == -ENODEV) {
851 netif_device_detach(tp->netdev);
853 set_bit(RX_URB_FAIL, &tp->flags);
856 clear_bit(RX_URB_FAIL, &tp->flags);
861 tasklet_schedule(&tp->tl);
864 static void write_bulk_callback(struct urb *urb)
867 int status = urb->status;
872 dev_kfree_skb_irq(tp->tx_skb);
873 if (!netif_device_present(tp->netdev))
876 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
877 tp->netdev->name, status);
878 tp->netdev->trans_start = jiffies;
879 netif_wake_queue(tp->netdev);
882 static void rtl8152_tx_timeout(struct net_device *netdev)
884 struct r8152 *tp = netdev_priv(netdev);
885 struct net_device_stats *stats = rtl8152_get_stats(netdev);
886 netif_warn(tp, tx_err, netdev, "Tx timeout.\n");
887 usb_unlink_urb(tp->tx_urb);
891 static void rtl8152_set_rx_mode(struct net_device *netdev)
893 struct r8152 *tp = netdev_priv(netdev);
895 if (tp->speed & LINK_STATUS)
896 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
899 static void _rtl8152_set_rx_mode(struct net_device *netdev)
901 struct r8152 *tp = netdev_priv(netdev);
902 u32 mc_filter[2]; /* Multicast hash filter */
906 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
907 netif_stop_queue(netdev);
908 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
909 ocp_data &= ~RCR_ACPT_ALL;
910 ocp_data |= RCR_AB | RCR_APM;
912 if (netdev->flags & IFF_PROMISC) {
913 /* Unconditionally log net taps. */
914 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
915 ocp_data |= RCR_AM | RCR_AAP;
916 mc_filter[1] = mc_filter[0] = 0xffffffff;
917 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
918 (netdev->flags & IFF_ALLMULTI)) {
919 /* Too many to filter perfectly -- accept all multicasts. */
921 mc_filter[1] = mc_filter[0] = 0xffffffff;
923 struct netdev_hw_addr *ha;
925 mc_filter[1] = mc_filter[0] = 0;
926 netdev_for_each_mc_addr(ha, netdev) {
927 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
928 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
933 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
934 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
936 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
937 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
938 netif_wake_queue(netdev);
941 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
942 struct net_device *netdev)
944 struct r8152 *tp = netdev_priv(netdev);
945 struct net_device_stats *stats = rtl8152_get_stats(netdev);
946 struct tx_desc *tx_desc;
950 netif_stop_queue(netdev);
952 if (skb_header_cloned(skb) || skb_headroom(skb) < sizeof(*tx_desc)) {
953 struct sk_buff *tx_skb;
955 tx_skb = skb_copy_expand(skb, sizeof(*tx_desc), 0, GFP_ATOMIC);
956 dev_kfree_skb_any(skb);
959 netif_wake_queue(netdev);
964 tx_desc = (struct tx_desc *)skb_push(skb, sizeof(*tx_desc));
965 memset(tx_desc, 0, sizeof(*tx_desc));
966 tx_desc->opts1 = cpu_to_le32((len & TX_LEN_MASK) | TX_FS | TX_LS);
968 skb_tx_timestamp(skb);
969 usb_fill_bulk_urb(tp->tx_urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
971 (usb_complete_t)write_bulk_callback, tp);
972 res = usb_submit_urb(tp->tx_urb, GFP_ATOMIC);
974 /* Can we get/handle EPIPE here? */
975 if (res == -ENODEV) {
976 netif_device_detach(tp->netdev);
978 netif_warn(tp, tx_err, netdev,
979 "failed tx_urb %d\n", res);
981 netif_start_queue(netdev);
985 stats->tx_bytes += skb->len;
991 static void r8152b_reset_packet_filter(struct r8152 *tp)
995 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
996 ocp_data &= ~FMC_FCR_MCU_EN;
997 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
998 ocp_data |= FMC_FCR_MCU_EN;
999 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1002 static void rtl8152_nic_reset(struct r8152 *tp)
1006 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1008 for (i = 0; i < 1000; i++) {
1009 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1015 static inline u8 rtl8152_get_speed(struct r8152 *tp)
1017 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1020 static int rtl8152_enable(struct r8152 *tp)
1025 speed = rtl8152_get_speed(tp);
1026 if (speed & _100bps) {
1027 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
1028 ocp_data &= ~EEEP_CR_EEEP_TX;
1029 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1031 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
1032 ocp_data |= EEEP_CR_EEEP_TX;
1033 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1036 r8152b_reset_packet_filter(tp);
1038 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1039 ocp_data |= CR_RE | CR_TE;
1040 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1042 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1043 ocp_data &= ~RXDY_GATED_EN;
1044 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1046 usb_fill_bulk_urb(tp->rx_urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1047 tp->rx_skb->data, RTL8152_RMS + sizeof(struct rx_desc),
1048 (usb_complete_t)read_bulk_callback, tp);
1050 return usb_submit_urb(tp->rx_urb, GFP_KERNEL);
1053 static void rtl8152_disable(struct r8152 *tp)
1058 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1059 ocp_data &= ~RCR_ACPT_ALL;
1060 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1062 usb_kill_urb(tp->tx_urb);
1064 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1065 ocp_data |= RXDY_GATED_EN;
1066 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1068 for (i = 0; i < 1000; i++) {
1069 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1070 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1075 for (i = 0; i < 1000; i++) {
1076 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1081 usb_kill_urb(tp->rx_urb);
1083 rtl8152_nic_reset(tp);
1086 static void r8152b_exit_oob(struct r8152 *tp)
1091 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1092 ocp_data &= ~RCR_ACPT_ALL;
1093 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1095 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1096 ocp_data |= RXDY_GATED_EN;
1097 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1099 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1100 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1102 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1103 ocp_data &= ~NOW_IS_OOB;
1104 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1106 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1107 ocp_data &= ~MCU_BORW_EN;
1108 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1110 for (i = 0; i < 1000; i++) {
1111 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1112 if (ocp_data & LINK_LIST_READY)
1117 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1118 ocp_data |= RE_INIT_LL;
1119 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1121 for (i = 0; i < 1000; i++) {
1122 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1123 if (ocp_data & LINK_LIST_READY)
1128 rtl8152_nic_reset(tp);
1130 /* rx share fifo credit full threshold */
1131 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1133 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1134 ocp_data &= STAT_SPEED_MASK;
1135 if (ocp_data == STAT_SPEED_FULL) {
1136 /* rx share fifo credit near full threshold */
1137 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1139 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1142 /* rx share fifo credit near full threshold */
1143 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1145 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1149 /* TX share fifo free credit full threshold */
1150 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1152 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
1153 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_BUF_THR);
1154 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1155 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1157 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1158 ocp_data &= ~CPCR_RX_VLAN;
1159 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1161 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1163 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1164 ocp_data |= TCR0_AUTO_FIFO;
1165 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1168 static void r8152b_enter_oob(struct r8152 *tp)
1173 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1174 ocp_data &= ~NOW_IS_OOB;
1175 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1177 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1178 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1179 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1181 rtl8152_disable(tp);
1183 for (i = 0; i < 1000; i++) {
1184 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1185 if (ocp_data & LINK_LIST_READY)
1190 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1191 ocp_data |= RE_INIT_LL;
1192 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1194 for (i = 0; i < 1000; i++) {
1195 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1196 if (ocp_data & LINK_LIST_READY)
1201 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1203 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1204 ocp_data |= MAGIC_EN;
1205 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1207 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1208 ocp_data |= CPCR_RX_VLAN;
1209 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1211 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
1212 ocp_data |= ALDPS_PROXY_MODE;
1213 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
1215 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1216 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1217 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1219 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
1221 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1222 ocp_data &= ~RXDY_GATED_EN;
1223 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1225 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1226 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1227 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1230 static void r8152b_disable_aldps(struct r8152 *tp)
1232 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1236 static inline void r8152b_enable_aldps(struct r8152 *tp)
1238 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1239 LINKENA | DIS_SDSAVE);
1242 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
1247 cancel_delayed_work_sync(&tp->schedule);
1248 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1249 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1250 ADVERTISE_100HALF | ADVERTISE_100FULL);
1252 if (autoneg == AUTONEG_DISABLE) {
1253 if (speed == SPEED_10) {
1255 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1256 } else if (speed == SPEED_100) {
1257 bmcr = BMCR_SPEED100;
1258 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1264 if (duplex == DUPLEX_FULL)
1265 bmcr |= BMCR_FULLDPLX;
1267 if (speed == SPEED_10) {
1268 if (duplex == DUPLEX_FULL)
1269 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1271 anar |= ADVERTISE_10HALF;
1272 } else if (speed == SPEED_100) {
1273 if (duplex == DUPLEX_FULL) {
1274 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1275 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1277 anar |= ADVERTISE_10HALF;
1278 anar |= ADVERTISE_100HALF;
1285 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1288 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1289 r8152_mdio_write(tp, MII_BMCR, bmcr);
1292 schedule_delayed_work(&tp->schedule, 5 * HZ);
1297 static void rtl8152_down(struct r8152 *tp)
1301 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1302 ocp_data &= ~POWER_CUT;
1303 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1305 r8152b_disable_aldps(tp);
1306 r8152b_enter_oob(tp);
1307 r8152b_enable_aldps(tp);
1310 static void set_carrier(struct r8152 *tp)
1312 struct net_device *netdev = tp->netdev;
1315 speed = rtl8152_get_speed(tp);
1317 if (speed & LINK_STATUS) {
1318 if (!(tp->speed & LINK_STATUS)) {
1320 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1321 netif_carrier_on(netdev);
1324 if (tp->speed & LINK_STATUS) {
1325 netif_carrier_off(netdev);
1326 rtl8152_disable(tp);
1332 static void rtl_work_func_t(struct work_struct *work)
1334 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
1336 if (!test_bit(WORK_ENABLE, &tp->flags))
1339 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1344 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
1345 _rtl8152_set_rx_mode(tp->netdev);
1347 schedule_delayed_work(&tp->schedule, HZ);
1353 static int rtl8152_open(struct net_device *netdev)
1355 struct r8152 *tp = netdev_priv(netdev);
1358 tp->speed = rtl8152_get_speed(tp);
1359 if (tp->speed & LINK_STATUS) {
1360 res = rtl8152_enable(tp);
1363 netif_device_detach(tp->netdev);
1365 netif_err(tp, ifup, netdev,
1366 "rtl8152_open failed: %d\n", res);
1370 netif_carrier_on(netdev);
1372 netif_stop_queue(netdev);
1373 netif_carrier_off(netdev);
1376 rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL);
1377 netif_start_queue(netdev);
1378 set_bit(WORK_ENABLE, &tp->flags);
1379 schedule_delayed_work(&tp->schedule, 0);
1384 static int rtl8152_close(struct net_device *netdev)
1386 struct r8152 *tp = netdev_priv(netdev);
1389 clear_bit(WORK_ENABLE, &tp->flags);
1390 cancel_delayed_work_sync(&tp->schedule);
1391 netif_stop_queue(netdev);
1392 rtl8152_disable(tp);
1397 static void rtl_clear_bp(struct r8152 *tp)
1399 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1400 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1401 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1402 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1403 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1404 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1405 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1406 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1408 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1409 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1412 static void r8152b_enable_eee(struct r8152 *tp)
1416 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
1417 ocp_data |= EEE_RX_EN | EEE_TX_EN;
1418 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
1419 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
1420 EEE_10_CAP | EEE_NWAY_EN |
1421 TX_QUIET_EN | RX_QUIET_EN |
1422 SDRISETIME | RG_RXLPI_MSK_HFDUP |
1424 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
1425 RG_LDVQUIET_EN | RG_CKRSEL |
1427 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
1428 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
1429 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
1430 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
1431 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
1432 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
1435 static void r8152b_enable_fc(struct r8152 *tp)
1439 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1440 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1441 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1444 static void r8152b_hw_phy_cfg(struct r8152 *tp)
1446 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1447 r8152b_disable_aldps(tp);
1450 static void r8152b_init(struct r8152 *tp)
1457 if (tp->version == RTL_VER_01) {
1458 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
1459 ocp_data &= ~LED_MODE_MASK;
1460 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
1463 r8152b_hw_phy_cfg(tp);
1465 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1466 ocp_data &= ~POWER_CUT;
1467 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1469 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1470 ocp_data &= ~RWSUME_INDICATE;
1471 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1473 r8152b_exit_oob(tp);
1475 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1476 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
1477 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1478 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
1479 ocp_data &= ~MCU_CLK_RATIO_MASK;
1480 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
1481 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
1482 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
1483 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
1484 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
1486 r8152b_enable_eee(tp);
1487 r8152b_enable_aldps(tp);
1488 r8152b_enable_fc(tp);
1490 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
1492 for (i = 0; i < 100; i++) {
1494 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
1498 /* disable rx aggregation */
1499 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
1500 ocp_data |= RX_AGG_DISABLE;
1501 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
1504 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
1506 struct r8152 *tp = usb_get_intfdata(intf);
1508 netif_device_detach(tp->netdev);
1510 if (netif_running(tp->netdev)) {
1511 clear_bit(WORK_ENABLE, &tp->flags);
1512 cancel_delayed_work_sync(&tp->schedule);
1520 static int rtl8152_resume(struct usb_interface *intf)
1522 struct r8152 *tp = usb_get_intfdata(intf);
1525 netif_device_attach(tp->netdev);
1526 if (netif_running(tp->netdev)) {
1528 set_bit(WORK_ENABLE, &tp->flags);
1529 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1530 schedule_delayed_work(&tp->schedule, 0);
1536 static void rtl8152_get_drvinfo(struct net_device *netdev,
1537 struct ethtool_drvinfo *info)
1539 struct r8152 *tp = netdev_priv(netdev);
1541 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
1542 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
1543 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
1547 int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1549 struct r8152 *tp = netdev_priv(netdev);
1551 if (!tp->mii.mdio_read)
1554 return mii_ethtool_gset(&tp->mii, cmd);
1557 static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1559 struct r8152 *tp = netdev_priv(dev);
1561 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
1564 static struct ethtool_ops ops = {
1565 .get_drvinfo = rtl8152_get_drvinfo,
1566 .get_settings = rtl8152_get_settings,
1567 .set_settings = rtl8152_set_settings,
1568 .get_link = ethtool_op_get_link,
1571 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
1573 struct r8152 *tp = netdev_priv(netdev);
1574 struct mii_ioctl_data *data = if_mii(rq);
1579 data->phy_id = R8152_PHY_ID; /* Internal PHY */
1583 data->val_out = r8152_mdio_read(tp, data->reg_num);
1587 if (!capable(CAP_NET_ADMIN)) {
1591 r8152_mdio_write(tp, data->reg_num, data->val_in);
1601 static const struct net_device_ops rtl8152_netdev_ops = {
1602 .ndo_open = rtl8152_open,
1603 .ndo_stop = rtl8152_close,
1604 .ndo_do_ioctl = rtl8152_ioctl,
1605 .ndo_start_xmit = rtl8152_start_xmit,
1606 .ndo_tx_timeout = rtl8152_tx_timeout,
1607 .ndo_set_rx_mode = rtl8152_set_rx_mode,
1608 .ndo_set_mac_address = rtl8152_set_mac_address,
1610 .ndo_change_mtu = eth_change_mtu,
1611 .ndo_validate_addr = eth_validate_addr,
1614 static void r8152b_get_version(struct r8152 *tp)
1619 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
1620 version = (u16)(ocp_data & VERSION_MASK);
1624 tp->version = RTL_VER_01;
1627 tp->version = RTL_VER_02;
1630 netif_info(tp, probe, tp->netdev,
1631 "Unknown version 0x%04x\n", version);
1636 static int rtl8152_probe(struct usb_interface *intf,
1637 const struct usb_device_id *id)
1639 struct usb_device *udev = interface_to_usbdev(intf);
1641 struct net_device *netdev;
1643 if (udev->actconfig->desc.bConfigurationValue != 1) {
1644 usb_driver_set_configuration(udev, 1);
1648 netdev = alloc_etherdev(sizeof(struct r8152));
1650 dev_err(&intf->dev, "Out of memory");
1654 tp = netdev_priv(netdev);
1655 tp->msg_enable = 0x7FFF;
1657 tasklet_init(&tp->tl, rx_fixup, (unsigned long)tp);
1658 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
1661 tp->netdev = netdev;
1662 netdev->netdev_ops = &rtl8152_netdev_ops;
1663 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
1664 netdev->features &= ~NETIF_F_IP_CSUM;
1665 SET_ETHTOOL_OPS(netdev, &ops);
1668 tp->mii.dev = netdev;
1669 tp->mii.mdio_read = read_mii_word;
1670 tp->mii.mdio_write = write_mii_word;
1671 tp->mii.phy_id_mask = 0x3f;
1672 tp->mii.reg_num_mask = 0x1f;
1673 tp->mii.phy_id = R8152_PHY_ID;
1674 tp->mii.supports_gmii = 0;
1676 r8152b_get_version(tp);
1678 set_ethernet_addr(tp);
1680 if (!alloc_all_urbs(tp)) {
1681 netif_err(tp, probe, netdev, "out of memory");
1685 tp->rx_skb = netdev_alloc_skb(netdev,
1686 RTL8152_RMS + sizeof(struct rx_desc));
1690 usb_set_intfdata(intf, tp);
1691 SET_NETDEV_DEV(netdev, &intf->dev);
1694 if (register_netdev(netdev) != 0) {
1695 netif_err(tp, probe, netdev, "couldn't register the device");
1699 netif_info(tp, probe, netdev, "%s", DRIVER_VERSION);
1704 usb_set_intfdata(intf, NULL);
1705 dev_kfree_skb(tp->rx_skb);
1709 free_netdev(netdev);
1713 static void rtl8152_unload(struct r8152 *tp)
1717 if (tp->version != RTL_VER_01) {
1718 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1719 ocp_data |= POWER_CUT;
1720 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1723 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1724 ocp_data &= ~RWSUME_INDICATE;
1725 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1728 static void rtl8152_disconnect(struct usb_interface *intf)
1730 struct r8152 *tp = usb_get_intfdata(intf);
1732 usb_set_intfdata(intf, NULL);
1734 set_bit(RTL8152_UNPLUG, &tp->flags);
1735 tasklet_kill(&tp->tl);
1736 unregister_netdev(tp->netdev);
1740 dev_kfree_skb(tp->rx_skb);
1741 free_netdev(tp->netdev);
1745 /* table of devices that work with this driver */
1746 static struct usb_device_id rtl8152_table[] = {
1747 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
1751 MODULE_DEVICE_TABLE(usb, rtl8152_table);
1753 static struct usb_driver rtl8152_driver = {
1755 .probe = rtl8152_probe,
1756 .disconnect = rtl8152_disconnect,
1757 .id_table = rtl8152_table,
1758 .suspend = rtl8152_suspend,
1759 .resume = rtl8152_resume
1762 module_usb_driver(rtl8152_driver);
1764 MODULE_AUTHOR(DRIVER_AUTHOR);
1765 MODULE_DESCRIPTION(DRIVER_DESC);
1766 MODULE_LICENSE("GPL");