blob: 828572a5b24a30963f8deccf8f213d62a2d5665c [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
hayeswangc7de7de2014-01-15 10:42:16 +08002 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00003 *
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.
7 *
8 */
9
hayeswangac718b62013-05-02 16:01:25 +000010#include <linux/signal.h>
11#include <linux/slab.h>
12#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000013#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/mii.h>
16#include <linux/ethtool.h>
17#include <linux/usb.h>
18#include <linux/crc32.h>
19#include <linux/if_vlan.h>
20#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080021#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080022#include <linux/ip.h>
23#include <linux/ipv6.h>
hayeswangac718b62013-05-02 16:01:25 +000024
25/* Version Information */
hayeswangc7de7de2014-01-15 10:42:16 +080026#define DRIVER_VERSION "v1.04.0 (2014/01/15)"
hayeswangac718b62013-05-02 16:01:25 +000027#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080028#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000029#define MODULENAME "r8152"
30
31#define R8152_PHY_ID 32
32
33#define PLA_IDR 0xc000
34#define PLA_RCR 0xc010
35#define PLA_RMS 0xc016
36#define PLA_RXFIFO_CTRL0 0xc0a0
37#define PLA_RXFIFO_CTRL1 0xc0a4
38#define PLA_RXFIFO_CTRL2 0xc0a8
39#define PLA_FMC 0xc0b4
40#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080041#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000042#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080043#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000044#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080045#define PLA_TEREDO_TIMER 0xd2cc
46#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000047#define PLA_LEDSEL 0xdd90
48#define PLA_LED_FEATURE 0xdd92
49#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080050#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000051#define PLA_GPHY_INTR_IMR 0xe022
52#define PLA_EEE_CR 0xe040
53#define PLA_EEEP_CR 0xe080
54#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_MAC_PWR_CTRL2 0xe0ca
56#define PLA_MAC_PWR_CTRL3 0xe0cc
57#define PLA_MAC_PWR_CTRL4 0xe0ce
58#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000059#define PLA_TCR0 0xe610
60#define PLA_TCR1 0xe612
61#define PLA_TXFIFO_CTRL 0xe618
62#define PLA_RSTTELLY 0xe800
63#define PLA_CR 0xe813
64#define PLA_CRWECR 0xe81c
65#define PLA_CONFIG5 0xe822
66#define PLA_PHY_PWR 0xe84c
67#define PLA_OOB_CTRL 0xe84f
68#define PLA_CPCR 0xe854
69#define PLA_MISC_0 0xe858
70#define PLA_MISC_1 0xe85a
71#define PLA_OCP_GPHY_BASE 0xe86c
72#define PLA_TELLYCNT 0xe890
73#define PLA_SFF_STS_7 0xe8de
74#define PLA_PHYSTATUS 0xe908
75#define PLA_BP_BA 0xfc26
76#define PLA_BP_0 0xfc28
77#define PLA_BP_1 0xfc2a
78#define PLA_BP_2 0xfc2c
79#define PLA_BP_3 0xfc2e
80#define PLA_BP_4 0xfc30
81#define PLA_BP_5 0xfc32
82#define PLA_BP_6 0xfc34
83#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080084#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000085
hayeswang43779f82014-01-02 11:25:10 +080086#define USB_U2P3_CTRL 0xb460
hayeswangac718b62013-05-02 16:01:25 +000087#define USB_DEV_STAT 0xb808
88#define USB_USB_CTRL 0xd406
89#define USB_PHY_CTRL 0xd408
90#define USB_TX_AGG 0xd40a
91#define USB_RX_BUF_TH 0xd40c
92#define USB_USB_TIMER 0xd428
hayeswang43779f82014-01-02 11:25:10 +080093#define USB_RX_EARLY_AGG 0xd42c
hayeswangac718b62013-05-02 16:01:25 +000094#define USB_PM_CTRL_STATUS 0xd432
95#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +080096#define USB_TOLERANCE 0xd490
97#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +000098#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +080099#define USB_MISC_0 0xd81a
100#define USB_POWER_CUT 0xd80a
101#define USB_AFE_CTRL2 0xd824
102#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000103#define USB_BP_BA 0xfc26
104#define USB_BP_0 0xfc28
105#define USB_BP_1 0xfc2a
106#define USB_BP_2 0xfc2c
107#define USB_BP_3 0xfc2e
108#define USB_BP_4 0xfc30
109#define USB_BP_5 0xfc32
110#define USB_BP_6 0xfc34
111#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800112#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000113
114/* OCP Registers */
115#define OCP_ALDPS_CONFIG 0x2010
116#define OCP_EEE_CONFIG1 0x2080
117#define OCP_EEE_CONFIG2 0x2092
118#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800119#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000120#define OCP_EEE_AR 0xa41a
121#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800122#define OCP_PHY_STATUS 0xa420
123#define OCP_POWER_CFG 0xa430
124#define OCP_EEE_CFG 0xa432
125#define OCP_SRAM_ADDR 0xa436
126#define OCP_SRAM_DATA 0xa438
127#define OCP_DOWN_SPEED 0xa442
128#define OCP_EEE_CFG2 0xa5d0
129#define OCP_ADC_CFG 0xbc06
130
131/* SRAM Register */
132#define SRAM_LPF_CFG 0x8012
133#define SRAM_10M_AMP1 0x8080
134#define SRAM_10M_AMP2 0x8082
135#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000136
137/* PLA_RCR */
138#define RCR_AAP 0x00000001
139#define RCR_APM 0x00000002
140#define RCR_AM 0x00000004
141#define RCR_AB 0x00000008
142#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
143
144/* PLA_RXFIFO_CTRL0 */
145#define RXFIFO_THR1_NORMAL 0x00080002
146#define RXFIFO_THR1_OOB 0x01800003
147
148/* PLA_RXFIFO_CTRL1 */
149#define RXFIFO_THR2_FULL 0x00000060
150#define RXFIFO_THR2_HIGH 0x00000038
151#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800152#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000153
154/* PLA_RXFIFO_CTRL2 */
155#define RXFIFO_THR3_FULL 0x00000078
156#define RXFIFO_THR3_HIGH 0x00000048
157#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800158#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000159
160/* PLA_TXFIFO_CTRL */
161#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800162#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000163
164/* PLA_FMC */
165#define FMC_FCR_MCU_EN 0x0001
166
167/* PLA_EEEP_CR */
168#define EEEP_CR_EEEP_TX 0x0002
169
hayeswang43779f82014-01-02 11:25:10 +0800170/* PLA_WDT6_CTRL */
171#define WDT6_SET_MODE 0x0010
172
hayeswangac718b62013-05-02 16:01:25 +0000173/* PLA_TCR0 */
174#define TCR0_TX_EMPTY 0x0800
175#define TCR0_AUTO_FIFO 0x0080
176
177/* PLA_TCR1 */
178#define VERSION_MASK 0x7cf0
179
180/* PLA_CR */
181#define CR_RST 0x10
182#define CR_RE 0x08
183#define CR_TE 0x04
184
185/* PLA_CRWECR */
186#define CRWECR_NORAML 0x00
187#define CRWECR_CONFIG 0xc0
188
189/* PLA_OOB_CTRL */
190#define NOW_IS_OOB 0x80
191#define TXFIFO_EMPTY 0x20
192#define RXFIFO_EMPTY 0x10
193#define LINK_LIST_READY 0x02
194#define DIS_MCU_CLROOB 0x01
195#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
196
197/* PLA_MISC_1 */
198#define RXDY_GATED_EN 0x0008
199
200/* PLA_SFF_STS_7 */
201#define RE_INIT_LL 0x8000
202#define MCU_BORW_EN 0x4000
203
204/* PLA_CPCR */
205#define CPCR_RX_VLAN 0x0040
206
207/* PLA_CFG_WOL */
208#define MAGIC_EN 0x0001
209
hayeswang43779f82014-01-02 11:25:10 +0800210/* PLA_TEREDO_CFG */
211#define TEREDO_SEL 0x8000
212#define TEREDO_WAKE_MASK 0x7f00
213#define TEREDO_RS_EVENT_MASK 0x00fe
214#define OOB_TEREDO_EN 0x0001
215
hayeswangac718b62013-05-02 16:01:25 +0000216/* PAL_BDC_CR */
217#define ALDPS_PROXY_MODE 0x0001
218
219/* PLA_CONFIG5 */
220#define LAN_WAKE_EN 0x0002
221
222/* PLA_LED_FEATURE */
223#define LED_MODE_MASK 0x0700
224
225/* PLA_PHY_PWR */
226#define TX_10M_IDLE_EN 0x0080
227#define PFM_PWM_SWITCH 0x0040
228
229/* PLA_MAC_PWR_CTRL */
230#define D3_CLK_GATED_EN 0x00004000
231#define MCU_CLK_RATIO 0x07010f07
232#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800233#define ALDPS_SPDWN_RATIO 0x0f87
234
235/* PLA_MAC_PWR_CTRL2 */
236#define EEE_SPDWN_RATIO 0x8007
237
238/* PLA_MAC_PWR_CTRL3 */
239#define PKT_AVAIL_SPDWN_EN 0x0100
240#define SUSPEND_SPDWN_EN 0x0004
241#define U1U2_SPDWN_EN 0x0002
242#define L1_SPDWN_EN 0x0001
243
244/* PLA_MAC_PWR_CTRL4 */
245#define PWRSAVE_SPDWN_EN 0x1000
246#define RXDV_SPDWN_EN 0x0800
247#define TX10MIDLE_EN 0x0100
248#define TP100_SPDWN_EN 0x0020
249#define TP500_SPDWN_EN 0x0010
250#define TP1000_SPDWN_EN 0x0008
251#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000252
253/* PLA_GPHY_INTR_IMR */
254#define GPHY_STS_MSK 0x0001
255#define SPEED_DOWN_MSK 0x0002
256#define SPDWN_RXDV_MSK 0x0004
257#define SPDWN_LINKCHG_MSK 0x0008
258
259/* PLA_PHYAR */
260#define PHYAR_FLAG 0x80000000
261
262/* PLA_EEE_CR */
263#define EEE_RX_EN 0x0001
264#define EEE_TX_EN 0x0002
265
hayeswang43779f82014-01-02 11:25:10 +0800266/* PLA_BOOT_CTRL */
267#define AUTOLOAD_DONE 0x0002
268
hayeswangac718b62013-05-02 16:01:25 +0000269/* USB_DEV_STAT */
270#define STAT_SPEED_MASK 0x0006
271#define STAT_SPEED_HIGH 0x0000
272#define STAT_SPEED_FULL 0x0001
273
274/* USB_TX_AGG */
275#define TX_AGG_MAX_THRESHOLD 0x03
276
277/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800278#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800279#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800280#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000281
282/* USB_TX_DMA */
283#define TEST_MODE_DISABLE 0x00000001
284#define TX_SIZE_ADJUST1 0x00000100
285
286/* USB_UPS_CTRL */
287#define POWER_CUT 0x0100
288
289/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800290#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000291
292/* USB_USB_CTRL */
293#define RX_AGG_DISABLE 0x0010
294
hayeswang43779f82014-01-02 11:25:10 +0800295/* USB_U2P3_CTRL */
296#define U2P3_ENABLE 0x0001
297
298/* USB_POWER_CUT */
299#define PWR_EN 0x0001
300#define PHASE2_EN 0x0008
301
302/* USB_MISC_0 */
303#define PCUT_STATUS 0x0001
304
305/* USB_RX_EARLY_AGG */
306#define EARLY_AGG_SUPPER 0x0e832981
307#define EARLY_AGG_HIGH 0x0e837a12
308#define EARLY_AGG_SLOW 0x0e83ffff
309
310/* USB_WDT11_CTRL */
311#define TIMER11_EN 0x0001
312
313/* USB_LPM_CTRL */
314#define LPM_TIMER_MASK 0x0c
315#define LPM_TIMER_500MS 0x04 /* 500 ms */
316#define LPM_TIMER_500US 0x0c /* 500 us */
317
318/* USB_AFE_CTRL2 */
319#define SEN_VAL_MASK 0xf800
320#define SEN_VAL_NORMAL 0xa000
321#define SEL_RXIDLE 0x0100
322
hayeswangac718b62013-05-02 16:01:25 +0000323/* OCP_ALDPS_CONFIG */
324#define ENPWRSAVE 0x8000
325#define ENPDNPS 0x0200
326#define LINKENA 0x0100
327#define DIS_SDSAVE 0x0010
328
hayeswang43779f82014-01-02 11:25:10 +0800329/* OCP_PHY_STATUS */
330#define PHY_STAT_MASK 0x0007
331#define PHY_STAT_LAN_ON 3
332#define PHY_STAT_PWRDN 5
333
334/* OCP_POWER_CFG */
335#define EEE_CLKDIV_EN 0x8000
336#define EN_ALDPS 0x0004
337#define EN_10M_PLLOFF 0x0001
338
hayeswangac718b62013-05-02 16:01:25 +0000339/* OCP_EEE_CONFIG1 */
340#define RG_TXLPI_MSK_HFDUP 0x8000
341#define RG_MATCLR_EN 0x4000
342#define EEE_10_CAP 0x2000
343#define EEE_NWAY_EN 0x1000
344#define TX_QUIET_EN 0x0200
345#define RX_QUIET_EN 0x0100
346#define SDRISETIME 0x0010 /* bit 4 ~ 6 */
347#define RG_RXLPI_MSK_HFDUP 0x0008
348#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
349
350/* OCP_EEE_CONFIG2 */
351#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
352#define RG_DACQUIET_EN 0x0400
353#define RG_LDVQUIET_EN 0x0200
354#define RG_CKRSEL 0x0020
355#define RG_EEEPRG_EN 0x0010
356
357/* OCP_EEE_CONFIG3 */
358#define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
359#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
360#define MSK_PH 0x0006 /* bit 0 ~ 3 */
361
362/* OCP_EEE_AR */
363/* bit[15:14] function */
364#define FUN_ADDR 0x0000
365#define FUN_DATA 0x4000
366/* bit[4:0] device addr */
367#define DEVICE_ADDR 0x0007
368
369/* OCP_EEE_DATA */
370#define EEE_ADDR 0x003C
371#define EEE_DATA 0x0002
372
hayeswang43779f82014-01-02 11:25:10 +0800373/* OCP_EEE_CFG */
374#define CTAP_SHORT_EN 0x0040
375#define EEE10_EN 0x0010
376
377/* OCP_DOWN_SPEED */
378#define EN_10M_BGOFF 0x0080
379
380/* OCP_EEE_CFG2 */
381#define MY1000_EEE 0x0004
382#define MY100_EEE 0x0002
383
384/* OCP_ADC_CFG */
385#define CKADSEL_L 0x0100
386#define ADC_EN 0x0080
387#define EN_EMI_L 0x0040
388
389/* SRAM_LPF_CFG */
390#define LPF_AUTO_TUNE 0x8000
391
392/* SRAM_10M_AMP1 */
393#define GDAC_IB_UPALL 0x0008
394
395/* SRAM_10M_AMP2 */
396#define AMP_DN 0x0200
397
398/* SRAM_IMPEDANCE */
399#define RX_DRIVING_MASK 0x6000
400
hayeswangac718b62013-05-02 16:01:25 +0000401enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800402 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000403 _100bps = 0x08,
404 _10bps = 0x04,
405 LINK_STATUS = 0x02,
406 FULL_DUP = 0x01,
407};
408
hayeswangebc2ec482013-08-14 20:54:38 +0800409#define RTL8152_MAX_TX 10
410#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800411#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800412#define CRC_SIZE 4
413#define TX_ALIGN 4
414#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800415
416#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800417
hayeswangac718b62013-05-02 16:01:25 +0000418#define RTL8152_REQT_READ 0xc0
419#define RTL8152_REQT_WRITE 0x40
420#define RTL8152_REQ_GET_REGS 0x05
421#define RTL8152_REQ_SET_REGS 0x05
422
423#define BYTE_EN_DWORD 0xff
424#define BYTE_EN_WORD 0x33
425#define BYTE_EN_BYTE 0x11
426#define BYTE_EN_SIX_BYTES 0x3f
427#define BYTE_EN_START_MASK 0x0f
428#define BYTE_EN_END_MASK 0xf0
429
430#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
431#define RTL8152_TX_TIMEOUT (HZ)
432
433/* rtl8152 flags */
434enum rtl8152_flags {
435 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000436 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800437 WORK_ENABLE,
438 RTL8152_LINK_CHG,
hayeswangaa66a5f2014-02-18 21:49:04 +0800439 PHY_RESET,
hayeswangac718b62013-05-02 16:01:25 +0000440};
441
442/* Define these values to match your device */
443#define VENDOR_ID_REALTEK 0x0bda
444#define PRODUCT_ID_RTL8152 0x8152
hayeswang43779f82014-01-02 11:25:10 +0800445#define PRODUCT_ID_RTL8153 0x8153
446
447#define VENDOR_ID_SAMSUNG 0x04e8
448#define PRODUCT_ID_SAMSUNG 0xa101
hayeswangac718b62013-05-02 16:01:25 +0000449
450#define MCU_TYPE_PLA 0x0100
451#define MCU_TYPE_USB 0x0000
452
hayeswangc7de7de2014-01-15 10:42:16 +0800453#define REALTEK_USB_DEVICE(vend, prod) \
454 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)
455
hayeswangac718b62013-05-02 16:01:25 +0000456struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800457 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000458#define RX_LEN_MASK 0x7fff
hayeswang500b6d72013-11-20 17:30:57 +0800459 __le32 opts2;
460 __le32 opts3;
461 __le32 opts4;
462 __le32 opts5;
463 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000464};
465
466struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800467 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000468#define TX_FS (1 << 31) /* First segment of a packet */
469#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang5bd23882013-08-14 20:54:39 +0800470#define TX_LEN_MASK 0x3ffff
471
hayeswang500b6d72013-11-20 17:30:57 +0800472 __le32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800473#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
474#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
475#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
476#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswangac718b62013-05-02 16:01:25 +0000477};
478
hayeswangdff4e8a2013-08-16 16:09:33 +0800479struct r8152;
480
hayeswangebc2ec482013-08-14 20:54:38 +0800481struct rx_agg {
482 struct list_head list;
483 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800484 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800485 void *buffer;
486 void *head;
487};
488
489struct tx_agg {
490 struct list_head list;
491 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800492 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800493 void *buffer;
494 void *head;
495 u32 skb_num;
496 u32 skb_len;
497};
498
hayeswangac718b62013-05-02 16:01:25 +0000499struct r8152 {
500 unsigned long flags;
501 struct usb_device *udev;
502 struct tasklet_struct tl;
hayeswang40a82912013-08-14 20:54:40 +0800503 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000504 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800505 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800506 struct tx_agg tx_info[RTL8152_MAX_TX];
507 struct rx_agg rx_info[RTL8152_MAX_RX];
508 struct list_head rx_done, tx_free;
509 struct sk_buff_head tx_queue;
510 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000511 struct delayed_work schedule;
512 struct mii_if_info mii;
hayeswangc81229c2014-01-02 11:22:42 +0800513
514 struct rtl_ops {
515 void (*init)(struct r8152 *);
516 int (*enable)(struct r8152 *);
517 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800518 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800519 void (*down)(struct r8152 *);
520 void (*unload)(struct r8152 *);
521 } rtl_ops;
522
hayeswang40a82912013-08-14 20:54:40 +0800523 int intr_interval;
hayeswangac718b62013-05-02 16:01:25 +0000524 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800525 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000526 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800527 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000528 u8 version;
529 u8 speed;
530};
531
532enum rtl_version {
533 RTL_VER_UNKNOWN = 0,
534 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800535 RTL_VER_02,
536 RTL_VER_03,
537 RTL_VER_04,
538 RTL_VER_05,
539 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000540};
541
542/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
543 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
544 */
545static const int multicast_filter_limit = 32;
hayeswangebc2ec482013-08-14 20:54:38 +0800546static unsigned int rx_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000547
548static
549int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
550{
hayeswang31787f52013-07-31 17:21:25 +0800551 int ret;
552 void *tmp;
553
554 tmp = kmalloc(size, GFP_KERNEL);
555 if (!tmp)
556 return -ENOMEM;
557
558 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000559 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
hayeswang31787f52013-07-31 17:21:25 +0800560 value, index, tmp, size, 500);
561
562 memcpy(data, tmp, size);
563 kfree(tmp);
564
565 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000566}
567
568static
569int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
570{
hayeswang31787f52013-07-31 17:21:25 +0800571 int ret;
572 void *tmp;
573
574 tmp = kmalloc(size, GFP_KERNEL);
575 if (!tmp)
576 return -ENOMEM;
577
578 memcpy(tmp, data, size);
579
580 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000581 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
hayeswang31787f52013-07-31 17:21:25 +0800582 value, index, tmp, size, 500);
583
584 kfree(tmp);
585 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000586}
587
588static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
589 void *data, u16 type)
590{
hayeswang45f4a192014-01-06 17:08:41 +0800591 u16 limit = 64;
592 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000593
594 if (test_bit(RTL8152_UNPLUG, &tp->flags))
595 return -ENODEV;
596
597 /* both size and indix must be 4 bytes align */
598 if ((size & 3) || !size || (index & 3) || !data)
599 return -EPERM;
600
601 if ((u32)index + (u32)size > 0xffff)
602 return -EPERM;
603
604 while (size) {
605 if (size > limit) {
606 ret = get_registers(tp, index, type, limit, data);
607 if (ret < 0)
608 break;
609
610 index += limit;
611 data += limit;
612 size -= limit;
613 } else {
614 ret = get_registers(tp, index, type, size, data);
615 if (ret < 0)
616 break;
617
618 index += size;
619 data += size;
620 size = 0;
621 break;
622 }
623 }
624
625 return ret;
626}
627
628static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
629 u16 size, void *data, u16 type)
630{
hayeswang45f4a192014-01-06 17:08:41 +0800631 int ret;
632 u16 byteen_start, byteen_end, byen;
633 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000634
635 if (test_bit(RTL8152_UNPLUG, &tp->flags))
636 return -ENODEV;
637
638 /* both size and indix must be 4 bytes align */
639 if ((size & 3) || !size || (index & 3) || !data)
640 return -EPERM;
641
642 if ((u32)index + (u32)size > 0xffff)
643 return -EPERM;
644
645 byteen_start = byteen & BYTE_EN_START_MASK;
646 byteen_end = byteen & BYTE_EN_END_MASK;
647
648 byen = byteen_start | (byteen_start << 4);
649 ret = set_registers(tp, index, type | byen, 4, data);
650 if (ret < 0)
651 goto error1;
652
653 index += 4;
654 data += 4;
655 size -= 4;
656
657 if (size) {
658 size -= 4;
659
660 while (size) {
661 if (size > limit) {
662 ret = set_registers(tp, index,
663 type | BYTE_EN_DWORD,
664 limit, data);
665 if (ret < 0)
666 goto error1;
667
668 index += limit;
669 data += limit;
670 size -= limit;
671 } else {
672 ret = set_registers(tp, index,
673 type | BYTE_EN_DWORD,
674 size, data);
675 if (ret < 0)
676 goto error1;
677
678 index += size;
679 data += size;
680 size = 0;
681 break;
682 }
683 }
684
685 byen = byteen_end | (byteen_end >> 4);
686 ret = set_registers(tp, index, type | byen, 4, data);
687 if (ret < 0)
688 goto error1;
689 }
690
691error1:
692 return ret;
693}
694
695static inline
696int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
697{
698 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
699}
700
701static inline
702int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
703{
704 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
705}
706
707static inline
708int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
709{
710 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
711}
712
713static inline
714int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
715{
716 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
717}
718
719static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
720{
hayeswangc8826de2013-07-31 17:21:26 +0800721 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000722
hayeswangc8826de2013-07-31 17:21:26 +0800723 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000724
725 return __le32_to_cpu(data);
726}
727
728static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
729{
hayeswangc8826de2013-07-31 17:21:26 +0800730 __le32 tmp = __cpu_to_le32(data);
731
732 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000733}
734
735static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
736{
737 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800738 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000739 u8 shift = index & 2;
740
741 index &= ~3;
742
hayeswangc8826de2013-07-31 17:21:26 +0800743 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000744
hayeswangc8826de2013-07-31 17:21:26 +0800745 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000746 data >>= (shift * 8);
747 data &= 0xffff;
748
749 return (u16)data;
750}
751
752static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
753{
hayeswangc8826de2013-07-31 17:21:26 +0800754 u32 mask = 0xffff;
755 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000756 u16 byen = BYTE_EN_WORD;
757 u8 shift = index & 2;
758
759 data &= mask;
760
761 if (index & 2) {
762 byen <<= shift;
763 mask <<= (shift * 8);
764 data <<= (shift * 8);
765 index &= ~3;
766 }
767
hayeswangc8826de2013-07-31 17:21:26 +0800768 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000769
hayeswangc8826de2013-07-31 17:21:26 +0800770 data |= __le32_to_cpu(tmp) & ~mask;
771 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000772
hayeswangc8826de2013-07-31 17:21:26 +0800773 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000774}
775
776static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
777{
778 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800779 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000780 u8 shift = index & 3;
781
782 index &= ~3;
783
hayeswangc8826de2013-07-31 17:21:26 +0800784 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000785
hayeswangc8826de2013-07-31 17:21:26 +0800786 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000787 data >>= (shift * 8);
788 data &= 0xff;
789
790 return (u8)data;
791}
792
793static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
794{
hayeswangc8826de2013-07-31 17:21:26 +0800795 u32 mask = 0xff;
796 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000797 u16 byen = BYTE_EN_BYTE;
798 u8 shift = index & 3;
799
800 data &= mask;
801
802 if (index & 3) {
803 byen <<= shift;
804 mask <<= (shift * 8);
805 data <<= (shift * 8);
806 index &= ~3;
807 }
808
hayeswangc8826de2013-07-31 17:21:26 +0800809 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000810
hayeswangc8826de2013-07-31 17:21:26 +0800811 data |= __le32_to_cpu(tmp) & ~mask;
812 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000813
hayeswangc8826de2013-07-31 17:21:26 +0800814 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000815}
816
hayeswangac244d32014-01-02 11:22:40 +0800817static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
818{
819 u16 ocp_base, ocp_index;
820
821 ocp_base = addr & 0xf000;
822 if (ocp_base != tp->ocp_base) {
823 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
824 tp->ocp_base = ocp_base;
825 }
826
827 ocp_index = (addr & 0x0fff) | 0xb000;
828 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
829}
830
hayeswange3fe0b12014-01-02 11:22:39 +0800831static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
832{
833 u16 ocp_base, ocp_index;
834
835 ocp_base = addr & 0xf000;
836 if (ocp_base != tp->ocp_base) {
837 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
838 tp->ocp_base = ocp_base;
839 }
840
841 ocp_index = (addr & 0x0fff) | 0xb000;
842 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
843}
844
hayeswangac244d32014-01-02 11:22:40 +0800845static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000846{
hayeswangac244d32014-01-02 11:22:40 +0800847 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000848}
849
hayeswangac244d32014-01-02 11:22:40 +0800850static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000851{
hayeswangac244d32014-01-02 11:22:40 +0800852 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000853}
854
hayeswang43779f82014-01-02 11:25:10 +0800855static void sram_write(struct r8152 *tp, u16 addr, u16 data)
856{
857 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
858 ocp_reg_write(tp, OCP_SRAM_DATA, data);
859}
860
861static u16 sram_read(struct r8152 *tp, u16 addr)
862{
863 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
864 return ocp_reg_read(tp, OCP_SRAM_DATA);
865}
866
hayeswangac718b62013-05-02 16:01:25 +0000867static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
868{
869 struct r8152 *tp = netdev_priv(netdev);
870
871 if (phy_id != R8152_PHY_ID)
872 return -EINVAL;
873
874 return r8152_mdio_read(tp, reg);
875}
876
877static
878void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
879{
880 struct r8152 *tp = netdev_priv(netdev);
881
882 if (phy_id != R8152_PHY_ID)
883 return;
884
885 r8152_mdio_write(tp, reg, val);
886}
887
hayeswangebc2ec482013-08-14 20:54:38 +0800888static
889int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
890
hayeswangac718b62013-05-02 16:01:25 +0000891static inline void set_ethernet_addr(struct r8152 *tp)
892{
893 struct net_device *dev = tp->netdev;
hayeswang8a91c822014-02-18 21:49:01 +0800894 int ret;
hayeswang31787f52013-07-31 17:21:25 +0800895 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000896
hayeswang8a91c822014-02-18 21:49:01 +0800897 if (tp->version == RTL_VER_01)
898 ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id);
899 else
900 ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id);
901
902 if (ret < 0) {
hayeswangac718b62013-05-02 16:01:25 +0000903 netif_notice(tp, probe, dev, "inet addr fail\n");
hayeswang8a91c822014-02-18 21:49:01 +0800904 } else {
905 if (tp->version != RTL_VER_01) {
906 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
907 CRWECR_CONFIG);
908 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES,
909 sizeof(node_id), node_id);
910 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
911 CRWECR_NORAML);
912 }
913
hayeswangac718b62013-05-02 16:01:25 +0000914 memcpy(dev->dev_addr, node_id, dev->addr_len);
915 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
916 }
hayeswangac718b62013-05-02 16:01:25 +0000917}
918
919static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
920{
921 struct r8152 *tp = netdev_priv(netdev);
922 struct sockaddr *addr = p;
923
924 if (!is_valid_ether_addr(addr->sa_data))
925 return -EADDRNOTAVAIL;
926
927 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
928
929 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
930 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
931 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
932
933 return 0;
934}
935
hayeswangac718b62013-05-02 16:01:25 +0000936static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
937{
938 return &dev->stats;
939}
940
941static void read_bulk_callback(struct urb *urb)
942{
hayeswangac718b62013-05-02 16:01:25 +0000943 struct net_device *netdev;
hayeswanga5a4f462013-08-16 16:09:34 +0800944 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +0000945 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +0800946 struct rx_agg *agg;
947 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000948 int result;
hayeswangac718b62013-05-02 16:01:25 +0000949
hayeswangebc2ec482013-08-14 20:54:38 +0800950 agg = urb->context;
951 if (!agg)
952 return;
953
954 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000955 if (!tp)
956 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800957
hayeswangac718b62013-05-02 16:01:25 +0000958 if (test_bit(RTL8152_UNPLUG, &tp->flags))
959 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800960
961 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000962 return;
963
hayeswangebc2ec482013-08-14 20:54:38 +0800964 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +0800965
966 /* When link down, the driver would cancel all bulks. */
967 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +0800968 if (!netif_carrier_ok(netdev))
969 return;
970
hayeswangac718b62013-05-02 16:01:25 +0000971 switch (status) {
972 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +0800973 if (urb->actual_length < ETH_ZLEN)
974 break;
975
hayeswanga5a4f462013-08-16 16:09:34 +0800976 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800977 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +0800978 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800979 tasklet_schedule(&tp->tl);
980 return;
hayeswangac718b62013-05-02 16:01:25 +0000981 case -ESHUTDOWN:
982 set_bit(RTL8152_UNPLUG, &tp->flags);
983 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +0800984 return;
hayeswangac718b62013-05-02 16:01:25 +0000985 case -ENOENT:
986 return; /* the urb is in unlink state */
987 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +0800988 if (net_ratelimit())
989 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +0800990 break;
hayeswangac718b62013-05-02 16:01:25 +0000991 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +0800992 if (net_ratelimit())
993 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +0800994 break;
hayeswangac718b62013-05-02 16:01:25 +0000995 }
996
hayeswangebc2ec482013-08-14 20:54:38 +0800997 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +0000998 if (result == -ENODEV) {
999 netif_device_detach(tp->netdev);
1000 } else if (result) {
hayeswanga5a4f462013-08-16 16:09:34 +08001001 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001002 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +08001003 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001004 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001005 }
hayeswangac718b62013-05-02 16:01:25 +00001006}
1007
1008static void write_bulk_callback(struct urb *urb)
1009{
hayeswangebc2ec482013-08-14 20:54:38 +08001010 struct net_device_stats *stats;
hayeswanga5a4f462013-08-16 16:09:34 +08001011 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001012 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001013 struct r8152 *tp;
1014 int status = urb->status;
1015
hayeswangebc2ec482013-08-14 20:54:38 +08001016 agg = urb->context;
1017 if (!agg)
1018 return;
1019
1020 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001021 if (!tp)
1022 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001023
1024 stats = rtl8152_get_stats(tp->netdev);
1025 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001026 if (net_ratelimit())
1027 netdev_warn(tp->netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001028 stats->tx_errors += agg->skb_num;
1029 } else {
1030 stats->tx_packets += agg->skb_num;
1031 stats->tx_bytes += agg->skb_len;
1032 }
1033
hayeswanga5a4f462013-08-16 16:09:34 +08001034 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001035 list_add_tail(&agg->list, &tp->tx_free);
hayeswanga5a4f462013-08-16 16:09:34 +08001036 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001037
1038 if (!netif_carrier_ok(tp->netdev))
hayeswangac718b62013-05-02 16:01:25 +00001039 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001040
1041 if (!test_bit(WORK_ENABLE, &tp->flags))
1042 return;
1043
1044 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1045 return;
1046
1047 if (!skb_queue_empty(&tp->tx_queue))
1048 tasklet_schedule(&tp->tl);
1049}
1050
hayeswang40a82912013-08-14 20:54:40 +08001051static void intr_callback(struct urb *urb)
1052{
1053 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001054 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001055 int status = urb->status;
1056 int res;
1057
1058 tp = urb->context;
1059 if (!tp)
1060 return;
1061
1062 if (!test_bit(WORK_ENABLE, &tp->flags))
1063 return;
1064
1065 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1066 return;
1067
1068 switch (status) {
1069 case 0: /* success */
1070 break;
1071 case -ECONNRESET: /* unlink */
1072 case -ESHUTDOWN:
1073 netif_device_detach(tp->netdev);
1074 case -ENOENT:
1075 return;
1076 case -EOVERFLOW:
1077 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1078 goto resubmit;
1079 /* -EPIPE: should clear the halt */
1080 default:
1081 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1082 goto resubmit;
1083 }
1084
1085 d = urb->transfer_buffer;
1086 if (INTR_LINK & __le16_to_cpu(d[0])) {
1087 if (!(tp->speed & LINK_STATUS)) {
1088 set_bit(RTL8152_LINK_CHG, &tp->flags);
1089 schedule_delayed_work(&tp->schedule, 0);
1090 }
1091 } else {
1092 if (tp->speed & LINK_STATUS) {
1093 set_bit(RTL8152_LINK_CHG, &tp->flags);
1094 schedule_delayed_work(&tp->schedule, 0);
1095 }
1096 }
1097
1098resubmit:
1099 res = usb_submit_urb(urb, GFP_ATOMIC);
1100 if (res == -ENODEV)
1101 netif_device_detach(tp->netdev);
1102 else if (res)
1103 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001104 "can't resubmit intr, status %d\n", res);
hayeswang40a82912013-08-14 20:54:40 +08001105}
1106
hayeswangebc2ec482013-08-14 20:54:38 +08001107static inline void *rx_agg_align(void *data)
1108{
hayeswang8e1f51b2014-01-02 11:22:41 +08001109 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001110}
1111
1112static inline void *tx_agg_align(void *data)
1113{
hayeswang8e1f51b2014-01-02 11:22:41 +08001114 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001115}
1116
1117static void free_all_mem(struct r8152 *tp)
1118{
1119 int i;
1120
1121 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001122 usb_free_urb(tp->rx_info[i].urb);
1123 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001124
hayeswang9629e3c2014-01-15 10:42:15 +08001125 kfree(tp->rx_info[i].buffer);
1126 tp->rx_info[i].buffer = NULL;
1127 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001128 }
1129
1130 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001131 usb_free_urb(tp->tx_info[i].urb);
1132 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001133
hayeswang9629e3c2014-01-15 10:42:15 +08001134 kfree(tp->tx_info[i].buffer);
1135 tp->tx_info[i].buffer = NULL;
1136 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001137 }
hayeswang40a82912013-08-14 20:54:40 +08001138
hayeswang9629e3c2014-01-15 10:42:15 +08001139 usb_free_urb(tp->intr_urb);
1140 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001141
hayeswang9629e3c2014-01-15 10:42:15 +08001142 kfree(tp->intr_buff);
1143 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001144}
1145
1146static int alloc_all_mem(struct r8152 *tp)
1147{
1148 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001149 struct usb_interface *intf = tp->intf;
1150 struct usb_host_interface *alt = intf->cur_altsetting;
1151 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001152 struct urb *urb;
1153 int node, i;
1154 u8 *buf;
1155
1156 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1157
1158 spin_lock_init(&tp->rx_lock);
1159 spin_lock_init(&tp->tx_lock);
1160 INIT_LIST_HEAD(&tp->rx_done);
1161 INIT_LIST_HEAD(&tp->tx_free);
1162 skb_queue_head_init(&tp->tx_queue);
1163
1164 for (i = 0; i < RTL8152_MAX_RX; i++) {
1165 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1166 if (!buf)
1167 goto err1;
1168
1169 if (buf != rx_agg_align(buf)) {
1170 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001171 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1172 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001173 if (!buf)
1174 goto err1;
1175 }
1176
1177 urb = usb_alloc_urb(0, GFP_KERNEL);
1178 if (!urb) {
1179 kfree(buf);
1180 goto err1;
1181 }
1182
1183 INIT_LIST_HEAD(&tp->rx_info[i].list);
1184 tp->rx_info[i].context = tp;
1185 tp->rx_info[i].urb = urb;
1186 tp->rx_info[i].buffer = buf;
1187 tp->rx_info[i].head = rx_agg_align(buf);
1188 }
1189
1190 for (i = 0; i < RTL8152_MAX_TX; i++) {
1191 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1192 if (!buf)
1193 goto err1;
1194
1195 if (buf != tx_agg_align(buf)) {
1196 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001197 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1198 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001199 if (!buf)
1200 goto err1;
1201 }
1202
1203 urb = usb_alloc_urb(0, GFP_KERNEL);
1204 if (!urb) {
1205 kfree(buf);
1206 goto err1;
1207 }
1208
1209 INIT_LIST_HEAD(&tp->tx_info[i].list);
1210 tp->tx_info[i].context = tp;
1211 tp->tx_info[i].urb = urb;
1212 tp->tx_info[i].buffer = buf;
1213 tp->tx_info[i].head = tx_agg_align(buf);
1214
1215 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1216 }
1217
hayeswang40a82912013-08-14 20:54:40 +08001218 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1219 if (!tp->intr_urb)
1220 goto err1;
1221
1222 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1223 if (!tp->intr_buff)
1224 goto err1;
1225
1226 tp->intr_interval = (int)ep_intr->desc.bInterval;
1227 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1228 tp->intr_buff, INTBUFSIZE, intr_callback,
1229 tp, tp->intr_interval);
1230
hayeswangebc2ec482013-08-14 20:54:38 +08001231 return 0;
1232
1233err1:
1234 free_all_mem(tp);
1235 return -ENOMEM;
1236}
1237
hayeswang0de98f62013-08-16 16:09:35 +08001238static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1239{
1240 struct tx_agg *agg = NULL;
1241 unsigned long flags;
1242
1243 spin_lock_irqsave(&tp->tx_lock, flags);
1244 if (!list_empty(&tp->tx_free)) {
1245 struct list_head *cursor;
1246
1247 cursor = tp->tx_free.next;
1248 list_del_init(cursor);
1249 agg = list_entry(cursor, struct tx_agg, list);
1250 }
1251 spin_unlock_irqrestore(&tp->tx_lock, flags);
1252
1253 return agg;
1254}
1255
hayeswang5bd23882013-08-14 20:54:39 +08001256static void
1257r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1258{
1259 memset(desc, 0, sizeof(*desc));
1260
1261 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1262
1263 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1264 __be16 protocol;
1265 u8 ip_protocol;
1266 u32 opts2 = 0;
1267
1268 if (skb->protocol == htons(ETH_P_8021Q))
1269 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1270 else
1271 protocol = skb->protocol;
1272
1273 switch (protocol) {
1274 case htons(ETH_P_IP):
1275 opts2 |= IPV4_CS;
1276 ip_protocol = ip_hdr(skb)->protocol;
1277 break;
1278
1279 case htons(ETH_P_IPV6):
1280 opts2 |= IPV6_CS;
1281 ip_protocol = ipv6_hdr(skb)->nexthdr;
1282 break;
1283
1284 default:
1285 ip_protocol = IPPROTO_RAW;
1286 break;
1287 }
1288
1289 if (ip_protocol == IPPROTO_TCP) {
1290 opts2 |= TCP_CS;
1291 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1292 } else if (ip_protocol == IPPROTO_UDP) {
1293 opts2 |= UDP_CS;
1294 } else {
1295 WARN_ON_ONCE(1);
1296 }
1297
1298 desc->opts2 = cpu_to_le32(opts2);
1299 }
1300}
1301
hayeswangb1379d92013-08-16 16:09:37 +08001302static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1303{
hayeswangd84130a2014-02-18 21:49:02 +08001304 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1305 unsigned long flags;
hayeswang7937f9e2013-11-20 17:30:54 +08001306 int remain;
hayeswangb1379d92013-08-16 16:09:37 +08001307 u8 *tx_data;
1308
hayeswangd84130a2014-02-18 21:49:02 +08001309 __skb_queue_head_init(&skb_head);
1310 spin_lock_irqsave(&tx_queue->lock, flags);
1311 skb_queue_splice_init(tx_queue, &skb_head);
1312 spin_unlock_irqrestore(&tx_queue->lock, flags);
1313
hayeswangb1379d92013-08-16 16:09:37 +08001314 tx_data = agg->head;
1315 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001316 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001317
hayeswang7937f9e2013-11-20 17:30:54 +08001318 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001319 struct tx_desc *tx_desc;
1320 struct sk_buff *skb;
1321 unsigned int len;
1322
hayeswangd84130a2014-02-18 21:49:02 +08001323 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001324 if (!skb)
1325 break;
1326
hayeswang7937f9e2013-11-20 17:30:54 +08001327 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001328 len = skb->len;
1329 if (remain < len) {
hayeswangd84130a2014-02-18 21:49:02 +08001330 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001331 break;
1332 }
1333
hayeswang7937f9e2013-11-20 17:30:54 +08001334 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001335 tx_desc = (struct tx_desc *)tx_data;
1336 tx_data += sizeof(*tx_desc);
1337
1338 r8152_tx_csum(tp, tx_desc, skb);
1339 memcpy(tx_data, skb->data, len);
1340 agg->skb_num++;
1341 agg->skb_len += len;
1342 dev_kfree_skb_any(skb);
1343
hayeswang7937f9e2013-11-20 17:30:54 +08001344 tx_data += len;
1345 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001346 }
1347
hayeswangd84130a2014-02-18 21:49:02 +08001348 if (!skb_queue_empty(&skb_head)) {
1349 spin_lock_irqsave(&tx_queue->lock, flags);
1350 skb_queue_splice(&skb_head, tx_queue);
1351 spin_unlock_irqrestore(&tx_queue->lock, flags);
1352 }
1353
hayeswangdd1b1192013-11-20 17:30:56 +08001354 netif_tx_lock(tp->netdev);
1355
1356 if (netif_queue_stopped(tp->netdev) &&
1357 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1358 netif_wake_queue(tp->netdev);
1359
1360 netif_tx_unlock(tp->netdev);
1361
hayeswangb1379d92013-08-16 16:09:37 +08001362 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1363 agg->head, (int)(tx_data - (u8 *)agg->head),
1364 (usb_complete_t)write_bulk_callback, agg);
1365
1366 return usb_submit_urb(agg->urb, GFP_ATOMIC);
1367}
1368
hayeswangebc2ec482013-08-14 20:54:38 +08001369static void rx_bottom(struct r8152 *tp)
1370{
hayeswanga5a4f462013-08-16 16:09:34 +08001371 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001372 struct list_head *cursor, *next, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +08001373
hayeswangd84130a2014-02-18 21:49:02 +08001374 if (list_empty(&tp->rx_done))
1375 return;
1376
1377 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001378 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001379 list_splice_init(&tp->rx_done, &rx_queue);
1380 spin_unlock_irqrestore(&tp->rx_lock, flags);
1381
1382 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001383 struct rx_desc *rx_desc;
1384 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001385 int len_used = 0;
1386 struct urb *urb;
1387 u8 *rx_data;
1388 int ret;
1389
hayeswangebc2ec482013-08-14 20:54:38 +08001390 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001391
1392 agg = list_entry(cursor, struct rx_agg, list);
1393 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001394 if (urb->actual_length < ETH_ZLEN)
1395 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001396
hayeswangebc2ec482013-08-14 20:54:38 +08001397 rx_desc = agg->head;
1398 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001399 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001400
hayeswang7937f9e2013-11-20 17:30:54 +08001401 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001402 struct net_device *netdev = tp->netdev;
1403 struct net_device_stats *stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001404 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001405 struct sk_buff *skb;
1406
hayeswang7937f9e2013-11-20 17:30:54 +08001407 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001408 if (pkt_len < ETH_ZLEN)
1409 break;
1410
hayeswang7937f9e2013-11-20 17:30:54 +08001411 len_used += pkt_len;
1412 if (urb->actual_length < len_used)
1413 break;
1414
hayeswang43a44782013-08-16 16:09:36 +08001415 stats = rtl8152_get_stats(netdev);
1416
hayeswang8e1f51b2014-01-02 11:22:41 +08001417 pkt_len -= CRC_SIZE;
hayeswangebc2ec482013-08-14 20:54:38 +08001418 rx_data += sizeof(struct rx_desc);
1419
1420 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1421 if (!skb) {
1422 stats->rx_dropped++;
1423 break;
1424 }
1425 memcpy(skb->data, rx_data, pkt_len);
1426 skb_put(skb, pkt_len);
1427 skb->protocol = eth_type_trans(skb, netdev);
1428 netif_rx(skb);
1429 stats->rx_packets++;
1430 stats->rx_bytes += pkt_len;
1431
hayeswang8e1f51b2014-01-02 11:22:41 +08001432 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec482013-08-14 20:54:38 +08001433 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001434 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001435 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001436 }
1437
hayeswang0de98f62013-08-16 16:09:35 +08001438submit:
hayeswangebc2ec482013-08-14 20:54:38 +08001439 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangebc2ec482013-08-14 20:54:38 +08001440 if (ret && ret != -ENODEV) {
hayeswangd84130a2014-02-18 21:49:02 +08001441 spin_lock_irqsave(&tp->rx_lock, flags);
1442 list_add_tail(&agg->list, &tp->rx_done);
1443 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001444 tasklet_schedule(&tp->tl);
1445 }
1446 }
hayeswangebc2ec482013-08-14 20:54:38 +08001447}
1448
1449static void tx_bottom(struct r8152 *tp)
1450{
hayeswangebc2ec482013-08-14 20:54:38 +08001451 int res;
1452
hayeswangb1379d92013-08-16 16:09:37 +08001453 do {
1454 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001455
hayeswangb1379d92013-08-16 16:09:37 +08001456 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001457 break;
1458
hayeswangb1379d92013-08-16 16:09:37 +08001459 agg = r8152_get_tx_agg(tp);
1460 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001461 break;
hayeswangb1379d92013-08-16 16:09:37 +08001462
1463 res = r8152_tx_agg_fill(tp, agg);
1464 if (res) {
1465 struct net_device_stats *stats;
1466 struct net_device *netdev;
1467 unsigned long flags;
1468
1469 netdev = tp->netdev;
1470 stats = rtl8152_get_stats(netdev);
1471
1472 if (res == -ENODEV) {
1473 netif_device_detach(netdev);
1474 } else {
1475 netif_warn(tp, tx_err, netdev,
1476 "failed tx_urb %d\n", res);
1477 stats->tx_dropped += agg->skb_num;
1478 spin_lock_irqsave(&tp->tx_lock, flags);
1479 list_add_tail(&agg->list, &tp->tx_free);
1480 spin_unlock_irqrestore(&tp->tx_lock, flags);
1481 }
hayeswangebc2ec482013-08-14 20:54:38 +08001482 }
hayeswangb1379d92013-08-16 16:09:37 +08001483 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001484}
1485
1486static void bottom_half(unsigned long data)
1487{
1488 struct r8152 *tp;
1489
1490 tp = (struct r8152 *)data;
1491
1492 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1493 return;
1494
1495 if (!test_bit(WORK_ENABLE, &tp->flags))
1496 return;
1497
hayeswang7559fb2f2013-08-16 16:09:38 +08001498 /* When link down, the driver would cancel all bulks. */
1499 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001500 if (!netif_carrier_ok(tp->netdev))
1501 return;
1502
1503 rx_bottom(tp);
1504 tx_bottom(tp);
1505}
1506
1507static
1508int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1509{
1510 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1511 agg->head, rx_buf_sz,
1512 (usb_complete_t)read_bulk_callback, agg);
1513
1514 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001515}
1516
hayeswang00a5e362014-02-18 21:48:59 +08001517static void rtl_drop_queued_tx(struct r8152 *tp)
1518{
1519 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001520 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1521 unsigned long flags;
hayeswang00a5e362014-02-18 21:48:59 +08001522 struct sk_buff *skb;
1523
hayeswangd84130a2014-02-18 21:49:02 +08001524 if (skb_queue_empty(tx_queue))
1525 return;
1526
1527 __skb_queue_head_init(&skb_head);
1528 spin_lock_irqsave(&tx_queue->lock, flags);
1529 skb_queue_splice_init(tx_queue, &skb_head);
1530 spin_unlock_irqrestore(&tx_queue->lock, flags);
1531
1532 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001533 dev_kfree_skb(skb);
1534 stats->tx_dropped++;
1535 }
1536}
1537
hayeswangac718b62013-05-02 16:01:25 +00001538static void rtl8152_tx_timeout(struct net_device *netdev)
1539{
1540 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001541 int i;
1542
Hayes Wang4a8deae2014-01-07 11:18:22 +08001543 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001544 for (i = 0; i < RTL8152_MAX_TX; i++)
1545 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001546}
1547
1548static void rtl8152_set_rx_mode(struct net_device *netdev)
1549{
1550 struct r8152 *tp = netdev_priv(netdev);
1551
hayeswang40a82912013-08-14 20:54:40 +08001552 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001553 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001554 schedule_delayed_work(&tp->schedule, 0);
1555 }
hayeswangac718b62013-05-02 16:01:25 +00001556}
1557
1558static void _rtl8152_set_rx_mode(struct net_device *netdev)
1559{
1560 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001561 u32 mc_filter[2]; /* Multicast hash filter */
1562 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001563 u32 ocp_data;
1564
hayeswangac718b62013-05-02 16:01:25 +00001565 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1566 netif_stop_queue(netdev);
1567 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1568 ocp_data &= ~RCR_ACPT_ALL;
1569 ocp_data |= RCR_AB | RCR_APM;
1570
1571 if (netdev->flags & IFF_PROMISC) {
1572 /* Unconditionally log net taps. */
1573 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1574 ocp_data |= RCR_AM | RCR_AAP;
1575 mc_filter[1] = mc_filter[0] = 0xffffffff;
1576 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1577 (netdev->flags & IFF_ALLMULTI)) {
1578 /* Too many to filter perfectly -- accept all multicasts. */
1579 ocp_data |= RCR_AM;
1580 mc_filter[1] = mc_filter[0] = 0xffffffff;
1581 } else {
1582 struct netdev_hw_addr *ha;
1583
1584 mc_filter[1] = mc_filter[0] = 0;
1585 netdev_for_each_mc_addr(ha, netdev) {
1586 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1587 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1588 ocp_data |= RCR_AM;
1589 }
1590 }
1591
hayeswang31787f52013-07-31 17:21:25 +08001592 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1593 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001594
hayeswang31787f52013-07-31 17:21:25 +08001595 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001596 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1597 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001598}
1599
1600static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1601 struct net_device *netdev)
1602{
1603 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001604
hayeswangac718b62013-05-02 16:01:25 +00001605 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001606
hayeswang61598782013-11-20 17:30:55 +08001607 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001608
hayeswangdd1b1192013-11-20 17:30:56 +08001609 if (list_empty(&tp->tx_free) &&
1610 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1611 netif_stop_queue(netdev);
1612
hayeswang61598782013-11-20 17:30:55 +08001613 if (!list_empty(&tp->tx_free))
1614 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001615
1616 return NETDEV_TX_OK;
1617}
1618
1619static void r8152b_reset_packet_filter(struct r8152 *tp)
1620{
1621 u32 ocp_data;
1622
1623 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1624 ocp_data &= ~FMC_FCR_MCU_EN;
1625 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1626 ocp_data |= FMC_FCR_MCU_EN;
1627 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1628}
1629
1630static void rtl8152_nic_reset(struct r8152 *tp)
1631{
1632 int i;
1633
1634 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1635
1636 for (i = 0; i < 1000; i++) {
1637 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1638 break;
1639 udelay(100);
1640 }
1641}
1642
hayeswangdd1b1192013-11-20 17:30:56 +08001643static void set_tx_qlen(struct r8152 *tp)
1644{
1645 struct net_device *netdev = tp->netdev;
1646
1647 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1648 sizeof(struct tx_desc));
1649}
1650
hayeswangac718b62013-05-02 16:01:25 +00001651static inline u8 rtl8152_get_speed(struct r8152 *tp)
1652{
1653 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1654}
1655
hayeswang507605a2014-01-02 11:22:43 +08001656static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001657{
hayeswangebc2ec482013-08-14 20:54:38 +08001658 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001659 u8 speed;
1660
1661 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001662 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001663 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001664 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001665 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1666 } else {
1667 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001668 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001669 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1670 }
hayeswang507605a2014-01-02 11:22:43 +08001671}
1672
hayeswang00a5e362014-02-18 21:48:59 +08001673static void rxdy_gated_en(struct r8152 *tp, bool enable)
1674{
1675 u32 ocp_data;
1676
1677 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1678 if (enable)
1679 ocp_data |= RXDY_GATED_EN;
1680 else
1681 ocp_data &= ~RXDY_GATED_EN;
1682 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1683}
1684
hayeswang507605a2014-01-02 11:22:43 +08001685static int rtl_enable(struct r8152 *tp)
1686{
1687 u32 ocp_data;
1688 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001689
1690 r8152b_reset_packet_filter(tp);
1691
1692 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1693 ocp_data |= CR_RE | CR_TE;
1694 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1695
hayeswang00a5e362014-02-18 21:48:59 +08001696 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001697
hayeswangebc2ec482013-08-14 20:54:38 +08001698 INIT_LIST_HEAD(&tp->rx_done);
1699 ret = 0;
1700 for (i = 0; i < RTL8152_MAX_RX; i++) {
1701 INIT_LIST_HEAD(&tp->rx_info[i].list);
1702 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1703 }
hayeswangac718b62013-05-02 16:01:25 +00001704
hayeswangebc2ec482013-08-14 20:54:38 +08001705 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001706}
1707
hayeswang507605a2014-01-02 11:22:43 +08001708static int rtl8152_enable(struct r8152 *tp)
1709{
1710 set_tx_qlen(tp);
1711 rtl_set_eee_plus(tp);
1712
1713 return rtl_enable(tp);
1714}
1715
hayeswang43779f82014-01-02 11:25:10 +08001716static void r8153_set_rx_agg(struct r8152 *tp)
1717{
1718 u8 speed;
1719
1720 speed = rtl8152_get_speed(tp);
1721 if (speed & _1000bps) {
1722 if (tp->udev->speed == USB_SPEED_SUPER) {
1723 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1724 RX_THR_SUPPER);
1725 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1726 EARLY_AGG_SUPPER);
1727 } else {
1728 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1729 RX_THR_HIGH);
1730 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1731 EARLY_AGG_HIGH);
1732 }
1733 } else {
1734 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1735 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1736 EARLY_AGG_SLOW);
1737 }
1738}
1739
1740static int rtl8153_enable(struct r8152 *tp)
1741{
1742 set_tx_qlen(tp);
1743 rtl_set_eee_plus(tp);
1744 r8153_set_rx_agg(tp);
1745
1746 return rtl_enable(tp);
1747}
1748
hayeswangac718b62013-05-02 16:01:25 +00001749static void rtl8152_disable(struct r8152 *tp)
1750{
hayeswangebc2ec482013-08-14 20:54:38 +08001751 u32 ocp_data;
1752 int i;
hayeswangac718b62013-05-02 16:01:25 +00001753
1754 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1755 ocp_data &= ~RCR_ACPT_ALL;
1756 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1757
hayeswang00a5e362014-02-18 21:48:59 +08001758 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001759
1760 for (i = 0; i < RTL8152_MAX_TX; i++)
1761 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001762
hayeswang00a5e362014-02-18 21:48:59 +08001763 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00001764
1765 for (i = 0; i < 1000; i++) {
1766 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1767 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1768 break;
1769 mdelay(1);
1770 }
1771
1772 for (i = 0; i < 1000; i++) {
1773 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1774 break;
1775 mdelay(1);
1776 }
1777
hayeswangebc2ec482013-08-14 20:54:38 +08001778 for (i = 0; i < RTL8152_MAX_RX; i++)
1779 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001780
1781 rtl8152_nic_reset(tp);
1782}
1783
hayeswang00a5e362014-02-18 21:48:59 +08001784static void r8152_power_cut_en(struct r8152 *tp, bool enable)
1785{
1786 u32 ocp_data;
1787
1788 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1789 if (enable)
1790 ocp_data |= POWER_CUT;
1791 else
1792 ocp_data &= ~POWER_CUT;
1793 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1794
1795 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1796 ocp_data &= ~RESUME_INDICATE;
1797 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1798
1799}
1800
hayeswangaa66a5f2014-02-18 21:49:04 +08001801static void rtl_phy_reset(struct r8152 *tp)
1802{
1803 u16 data;
1804 int i;
1805
1806 clear_bit(PHY_RESET, &tp->flags);
1807
1808 data = r8152_mdio_read(tp, MII_BMCR);
1809
1810 /* don't reset again before the previous one complete */
1811 if (data & BMCR_RESET)
1812 return;
1813
1814 data |= BMCR_RESET;
1815 r8152_mdio_write(tp, MII_BMCR, data);
1816
1817 for (i = 0; i < 50; i++) {
1818 msleep(20);
1819 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
1820 break;
1821 }
1822}
1823
hayeswang43499682014-02-18 21:48:58 +08001824static void rtl_clear_bp(struct r8152 *tp)
1825{
1826 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1827 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1828 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1829 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1830 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1831 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1832 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1833 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1834 mdelay(3);
1835 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1836 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1837}
1838
1839static void r8153_clear_bp(struct r8152 *tp)
1840{
1841 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
1842 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
1843 rtl_clear_bp(tp);
1844}
1845
1846static void r8153_teredo_off(struct r8152 *tp)
1847{
1848 u32 ocp_data;
1849
1850 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
1851 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
1852 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
1853
1854 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
1855 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
1856 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
1857}
1858
1859static void r8152b_disable_aldps(struct r8152 *tp)
1860{
1861 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1862 msleep(20);
1863}
1864
1865static inline void r8152b_enable_aldps(struct r8152 *tp)
1866{
1867 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1868 LINKENA | DIS_SDSAVE);
1869}
1870
1871static void r8152b_hw_phy_cfg(struct r8152 *tp)
1872{
hayeswangf0cbe0a2014-02-18 21:49:03 +08001873 u16 data;
1874
1875 data = r8152_mdio_read(tp, MII_BMCR);
1876 if (data & BMCR_PDOWN) {
1877 data &= ~BMCR_PDOWN;
1878 r8152_mdio_write(tp, MII_BMCR, data);
1879 }
1880
hayeswang43499682014-02-18 21:48:58 +08001881 r8152b_disable_aldps(tp);
hayeswang7e9da482014-02-18 21:49:05 +08001882
1883 rtl_clear_bp(tp);
1884
1885 r8152b_enable_aldps(tp);
hayeswangaa66a5f2014-02-18 21:49:04 +08001886 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08001887}
1888
hayeswangac718b62013-05-02 16:01:25 +00001889static void r8152b_exit_oob(struct r8152 *tp)
1890{
1891 u32 ocp_data;
1892 int i;
1893
1894 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1895 ocp_data &= ~RCR_ACPT_ALL;
1896 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1897
hayeswang00a5e362014-02-18 21:48:59 +08001898 rxdy_gated_en(tp, true);
hayeswang7e9da482014-02-18 21:49:05 +08001899 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00001900
1901 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1902 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1903
1904 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1905 ocp_data &= ~NOW_IS_OOB;
1906 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1907
1908 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1909 ocp_data &= ~MCU_BORW_EN;
1910 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1911
1912 for (i = 0; i < 1000; i++) {
1913 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1914 if (ocp_data & LINK_LIST_READY)
1915 break;
1916 mdelay(1);
1917 }
1918
1919 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1920 ocp_data |= RE_INIT_LL;
1921 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1922
1923 for (i = 0; i < 1000; i++) {
1924 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1925 if (ocp_data & LINK_LIST_READY)
1926 break;
1927 mdelay(1);
1928 }
1929
1930 rtl8152_nic_reset(tp);
1931
1932 /* rx share fifo credit full threshold */
1933 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1934
1935 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1936 ocp_data &= STAT_SPEED_MASK;
1937 if (ocp_data == STAT_SPEED_FULL) {
1938 /* rx share fifo credit near full threshold */
1939 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1940 RXFIFO_THR2_FULL);
1941 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1942 RXFIFO_THR3_FULL);
1943 } else {
1944 /* rx share fifo credit near full threshold */
1945 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1946 RXFIFO_THR2_HIGH);
1947 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1948 RXFIFO_THR3_HIGH);
1949 }
1950
1951 /* TX share fifo free credit full threshold */
1952 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1953
1954 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08001955 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00001956 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1957 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1958
1959 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1960 ocp_data &= ~CPCR_RX_VLAN;
1961 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1962
1963 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1964
1965 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1966 ocp_data |= TCR0_AUTO_FIFO;
1967 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1968}
1969
1970static void r8152b_enter_oob(struct r8152 *tp)
1971{
hayeswang45f4a192014-01-06 17:08:41 +08001972 u32 ocp_data;
1973 int i;
hayeswangac718b62013-05-02 16:01:25 +00001974
1975 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1976 ocp_data &= ~NOW_IS_OOB;
1977 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1978
1979 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1980 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1981 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1982
1983 rtl8152_disable(tp);
1984
1985 for (i = 0; i < 1000; i++) {
1986 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1987 if (ocp_data & LINK_LIST_READY)
1988 break;
1989 mdelay(1);
1990 }
1991
1992 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1993 ocp_data |= RE_INIT_LL;
1994 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1995
1996 for (i = 0; i < 1000; i++) {
1997 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1998 if (ocp_data & LINK_LIST_READY)
1999 break;
2000 mdelay(1);
2001 }
2002
2003 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2004
2005 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2006 ocp_data |= MAGIC_EN;
2007 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2008
2009 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2010 ocp_data |= CPCR_RX_VLAN;
2011 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2012
2013 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2014 ocp_data |= ALDPS_PROXY_MODE;
2015 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2016
2017 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2018 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2019 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2020
2021 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
2022
hayeswang00a5e362014-02-18 21:48:59 +08002023 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002024
2025 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2026 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2027 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2028}
2029
hayeswang43779f82014-01-02 11:25:10 +08002030static void r8153_hw_phy_cfg(struct r8152 *tp)
2031{
2032 u32 ocp_data;
2033 u16 data;
2034
2035 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002036 data = r8152_mdio_read(tp, MII_BMCR);
2037 if (data & BMCR_PDOWN) {
2038 data &= ~BMCR_PDOWN;
2039 r8152_mdio_write(tp, MII_BMCR, data);
2040 }
hayeswang43779f82014-01-02 11:25:10 +08002041
hayeswang7e9da482014-02-18 21:49:05 +08002042 r8153_clear_bp(tp);
2043
hayeswang43779f82014-01-02 11:25:10 +08002044 if (tp->version == RTL_VER_03) {
2045 data = ocp_reg_read(tp, OCP_EEE_CFG);
2046 data &= ~CTAP_SHORT_EN;
2047 ocp_reg_write(tp, OCP_EEE_CFG, data);
2048 }
2049
2050 data = ocp_reg_read(tp, OCP_POWER_CFG);
2051 data |= EEE_CLKDIV_EN;
2052 ocp_reg_write(tp, OCP_POWER_CFG, data);
2053
2054 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2055 data |= EN_10M_BGOFF;
2056 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2057 data = ocp_reg_read(tp, OCP_POWER_CFG);
2058 data |= EN_10M_PLLOFF;
2059 ocp_reg_write(tp, OCP_POWER_CFG, data);
2060 data = sram_read(tp, SRAM_IMPEDANCE);
2061 data &= ~RX_DRIVING_MASK;
2062 sram_write(tp, SRAM_IMPEDANCE, data);
2063
2064 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2065 ocp_data |= PFM_PWM_SWITCH;
2066 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2067
2068 data = sram_read(tp, SRAM_LPF_CFG);
2069 data |= LPF_AUTO_TUNE;
2070 sram_write(tp, SRAM_LPF_CFG, data);
2071
2072 data = sram_read(tp, SRAM_10M_AMP1);
2073 data |= GDAC_IB_UPALL;
2074 sram_write(tp, SRAM_10M_AMP1, data);
2075 data = sram_read(tp, SRAM_10M_AMP2);
2076 data |= AMP_DN;
2077 sram_write(tp, SRAM_10M_AMP2, data);
hayeswangaa66a5f2014-02-18 21:49:04 +08002078
2079 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002080}
2081
hayeswangb9702722014-02-18 21:49:00 +08002082static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002083{
2084 u8 u1u2[8];
2085
2086 if (enable)
2087 memset(u1u2, 0xff, sizeof(u1u2));
2088 else
2089 memset(u1u2, 0x00, sizeof(u1u2));
2090
2091 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2092}
2093
hayeswangb9702722014-02-18 21:49:00 +08002094static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002095{
2096 u32 ocp_data;
2097
2098 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2099 if (enable)
2100 ocp_data |= U2P3_ENABLE;
2101 else
2102 ocp_data &= ~U2P3_ENABLE;
2103 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2104}
2105
hayeswangb9702722014-02-18 21:49:00 +08002106static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002107{
2108 u32 ocp_data;
2109
2110 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2111 if (enable)
2112 ocp_data |= PWR_EN | PHASE2_EN;
2113 else
2114 ocp_data &= ~(PWR_EN | PHASE2_EN);
2115 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2116
2117 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2118 ocp_data &= ~PCUT_STATUS;
2119 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2120}
2121
hayeswang43779f82014-01-02 11:25:10 +08002122static void r8153_first_init(struct r8152 *tp)
2123{
2124 u32 ocp_data;
2125 int i;
2126
hayeswang00a5e362014-02-18 21:48:59 +08002127 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002128 r8153_teredo_off(tp);
2129
2130 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2131 ocp_data &= ~RCR_ACPT_ALL;
2132 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2133
2134 r8153_hw_phy_cfg(tp);
2135
2136 rtl8152_nic_reset(tp);
2137
2138 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2139 ocp_data &= ~NOW_IS_OOB;
2140 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2141
2142 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2143 ocp_data &= ~MCU_BORW_EN;
2144 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2145
2146 for (i = 0; i < 1000; i++) {
2147 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2148 if (ocp_data & LINK_LIST_READY)
2149 break;
2150 mdelay(1);
2151 }
2152
2153 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2154 ocp_data |= RE_INIT_LL;
2155 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2156
2157 for (i = 0; i < 1000; i++) {
2158 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2159 if (ocp_data & LINK_LIST_READY)
2160 break;
2161 mdelay(1);
2162 }
2163
2164 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2165 ocp_data &= ~CPCR_RX_VLAN;
2166 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2167
2168 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2169
2170 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2171 ocp_data |= TCR0_AUTO_FIFO;
2172 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2173
2174 rtl8152_nic_reset(tp);
2175
2176 /* rx share fifo credit full threshold */
2177 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2178 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2179 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2180 /* TX share fifo free credit full threshold */
2181 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2182
hayeswang9629e3c2014-01-15 10:42:15 +08002183 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002184 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2185 ocp_data &= ~RX_AGG_DISABLE;
2186 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2187}
2188
2189static void r8153_enter_oob(struct r8152 *tp)
2190{
2191 u32 ocp_data;
2192 int i;
2193
2194 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2195 ocp_data &= ~NOW_IS_OOB;
2196 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2197
2198 rtl8152_disable(tp);
2199
2200 for (i = 0; i < 1000; i++) {
2201 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2202 if (ocp_data & LINK_LIST_READY)
2203 break;
2204 mdelay(1);
2205 }
2206
2207 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2208 ocp_data |= RE_INIT_LL;
2209 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2210
2211 for (i = 0; i < 1000; i++) {
2212 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2213 if (ocp_data & LINK_LIST_READY)
2214 break;
2215 mdelay(1);
2216 }
2217
2218 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2219
2220 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2221 ocp_data |= MAGIC_EN;
2222 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2223
2224 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2225 ocp_data &= ~TEREDO_WAKE_MASK;
2226 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2227
2228 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2229 ocp_data |= CPCR_RX_VLAN;
2230 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2231
2232 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2233 ocp_data |= ALDPS_PROXY_MODE;
2234 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2235
2236 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2237 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2238 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2239
2240 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
2241
hayeswang00a5e362014-02-18 21:48:59 +08002242 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002243
2244 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2245 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2246 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2247}
2248
2249static void r8153_disable_aldps(struct r8152 *tp)
2250{
2251 u16 data;
2252
2253 data = ocp_reg_read(tp, OCP_POWER_CFG);
2254 data &= ~EN_ALDPS;
2255 ocp_reg_write(tp, OCP_POWER_CFG, data);
2256 msleep(20);
2257}
2258
2259static void r8153_enable_aldps(struct r8152 *tp)
2260{
2261 u16 data;
2262
2263 data = ocp_reg_read(tp, OCP_POWER_CFG);
2264 data |= EN_ALDPS;
2265 ocp_reg_write(tp, OCP_POWER_CFG, data);
2266}
2267
hayeswangac718b62013-05-02 16:01:25 +00002268static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2269{
hayeswang43779f82014-01-02 11:25:10 +08002270 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002271 int ret = 0;
2272
2273 cancel_delayed_work_sync(&tp->schedule);
2274 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2275 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2276 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002277 if (tp->mii.supports_gmii) {
2278 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2279 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2280 } else {
2281 gbcr = 0;
2282 }
hayeswangac718b62013-05-02 16:01:25 +00002283
2284 if (autoneg == AUTONEG_DISABLE) {
2285 if (speed == SPEED_10) {
2286 bmcr = 0;
2287 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2288 } else if (speed == SPEED_100) {
2289 bmcr = BMCR_SPEED100;
2290 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002291 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2292 bmcr = BMCR_SPEED1000;
2293 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002294 } else {
2295 ret = -EINVAL;
2296 goto out;
2297 }
2298
2299 if (duplex == DUPLEX_FULL)
2300 bmcr |= BMCR_FULLDPLX;
2301 } else {
2302 if (speed == SPEED_10) {
2303 if (duplex == DUPLEX_FULL)
2304 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2305 else
2306 anar |= ADVERTISE_10HALF;
2307 } else if (speed == SPEED_100) {
2308 if (duplex == DUPLEX_FULL) {
2309 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2310 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2311 } else {
2312 anar |= ADVERTISE_10HALF;
2313 anar |= ADVERTISE_100HALF;
2314 }
hayeswang43779f82014-01-02 11:25:10 +08002315 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2316 if (duplex == DUPLEX_FULL) {
2317 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2318 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2319 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2320 } else {
2321 anar |= ADVERTISE_10HALF;
2322 anar |= ADVERTISE_100HALF;
2323 gbcr |= ADVERTISE_1000HALF;
2324 }
hayeswangac718b62013-05-02 16:01:25 +00002325 } else {
2326 ret = -EINVAL;
2327 goto out;
2328 }
2329
2330 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2331 }
2332
hayeswangaa66a5f2014-02-18 21:49:04 +08002333 if (test_bit(PHY_RESET, &tp->flags))
2334 bmcr |= BMCR_RESET;
2335
hayeswang43779f82014-01-02 11:25:10 +08002336 if (tp->mii.supports_gmii)
2337 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2338
hayeswangac718b62013-05-02 16:01:25 +00002339 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2340 r8152_mdio_write(tp, MII_BMCR, bmcr);
2341
hayeswangaa66a5f2014-02-18 21:49:04 +08002342 if (test_bit(PHY_RESET, &tp->flags)) {
2343 int i;
2344
2345 clear_bit(PHY_RESET, &tp->flags);
2346 for (i = 0; i < 50; i++) {
2347 msleep(20);
2348 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2349 break;
2350 }
2351 }
2352
hayeswangac718b62013-05-02 16:01:25 +00002353out:
hayeswangac718b62013-05-02 16:01:25 +00002354
2355 return ret;
2356}
2357
2358static void rtl8152_down(struct r8152 *tp)
2359{
hayeswang00a5e362014-02-18 21:48:59 +08002360 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002361 r8152b_disable_aldps(tp);
2362 r8152b_enter_oob(tp);
2363 r8152b_enable_aldps(tp);
2364}
2365
hayeswang43779f82014-01-02 11:25:10 +08002366static void rtl8153_down(struct r8152 *tp)
2367{
hayeswangb9702722014-02-18 21:49:00 +08002368 r8153_u1u2en(tp, false);
2369 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002370 r8153_disable_aldps(tp);
2371 r8153_enter_oob(tp);
2372 r8153_enable_aldps(tp);
2373}
2374
hayeswangac718b62013-05-02 16:01:25 +00002375static void set_carrier(struct r8152 *tp)
2376{
2377 struct net_device *netdev = tp->netdev;
2378 u8 speed;
2379
hayeswang40a82912013-08-14 20:54:40 +08002380 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002381 speed = rtl8152_get_speed(tp);
2382
2383 if (speed & LINK_STATUS) {
2384 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002385 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002386 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2387 netif_carrier_on(netdev);
2388 }
2389 } else {
2390 if (tp->speed & LINK_STATUS) {
2391 netif_carrier_off(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002392 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002393 tp->rtl_ops.disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002394 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002395 }
2396 }
2397 tp->speed = speed;
2398}
2399
2400static void rtl_work_func_t(struct work_struct *work)
2401{
2402 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2403
2404 if (!test_bit(WORK_ENABLE, &tp->flags))
2405 goto out1;
2406
2407 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2408 goto out1;
2409
hayeswang40a82912013-08-14 20:54:40 +08002410 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2411 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002412
2413 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2414 _rtl8152_set_rx_mode(tp->netdev);
2415
hayeswangaa66a5f2014-02-18 21:49:04 +08002416
2417 if (test_bit(PHY_RESET, &tp->flags))
2418 rtl_phy_reset(tp);
2419
hayeswangac718b62013-05-02 16:01:25 +00002420out1:
2421 return;
2422}
2423
2424static int rtl8152_open(struct net_device *netdev)
2425{
2426 struct r8152 *tp = netdev_priv(netdev);
2427 int res = 0;
2428
hayeswang7e9da482014-02-18 21:49:05 +08002429 res = alloc_all_mem(tp);
2430 if (res)
2431 goto out;
2432
2433 tp->rtl_ops.up(tp);
2434
hayeswang43779f82014-01-02 11:25:10 +08002435 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2436 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2437 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002438 tp->speed = 0;
2439 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002440 netif_start_queue(netdev);
2441 set_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002442 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2443 if (res) {
2444 if (res == -ENODEV)
2445 netif_device_detach(tp->netdev);
2446 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2447 res);
hayeswang7e9da482014-02-18 21:49:05 +08002448 free_all_mem(tp);
hayeswang3d55f442014-02-06 11:55:48 +08002449 }
2450
hayeswangac718b62013-05-02 16:01:25 +00002451
hayeswang7e9da482014-02-18 21:49:05 +08002452out:
hayeswangac718b62013-05-02 16:01:25 +00002453 return res;
2454}
2455
2456static int rtl8152_close(struct net_device *netdev)
2457{
2458 struct r8152 *tp = netdev_priv(netdev);
2459 int res = 0;
2460
2461 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002462 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002463 cancel_delayed_work_sync(&tp->schedule);
2464 netif_stop_queue(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002465 tasklet_disable(&tp->tl);
hayeswang7e9da482014-02-18 21:49:05 +08002466 tp->rtl_ops.down(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002467 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002468
hayeswang7e9da482014-02-18 21:49:05 +08002469 free_all_mem(tp);
2470
hayeswangac718b62013-05-02 16:01:25 +00002471 return res;
2472}
2473
hayeswangac718b62013-05-02 16:01:25 +00002474static void r8152b_enable_eee(struct r8152 *tp)
2475{
hayeswang45f4a192014-01-06 17:08:41 +08002476 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002477
2478 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2479 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2480 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2481 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2482 EEE_10_CAP | EEE_NWAY_EN |
2483 TX_QUIET_EN | RX_QUIET_EN |
2484 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2485 SDFALLTIME);
2486 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2487 RG_LDVQUIET_EN | RG_CKRSEL |
2488 RG_EEEPRG_EN);
2489 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2490 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2491 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2492 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2493 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2494 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2495}
2496
hayeswang43779f82014-01-02 11:25:10 +08002497static void r8153_enable_eee(struct r8152 *tp)
2498{
2499 u32 ocp_data;
2500 u16 data;
2501
2502 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2503 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2504 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2505 data = ocp_reg_read(tp, OCP_EEE_CFG);
2506 data |= EEE10_EN;
2507 ocp_reg_write(tp, OCP_EEE_CFG, data);
2508 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2509 data |= MY1000_EEE | MY100_EEE;
2510 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2511}
2512
hayeswangac718b62013-05-02 16:01:25 +00002513static void r8152b_enable_fc(struct r8152 *tp)
2514{
2515 u16 anar;
2516
2517 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2518 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2519 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2520}
2521
hayeswangac718b62013-05-02 16:01:25 +00002522static void r8152b_init(struct r8152 *tp)
2523{
hayeswangebc2ec482013-08-14 20:54:38 +08002524 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002525
hayeswangac718b62013-05-02 16:01:25 +00002526 if (tp->version == RTL_VER_01) {
2527 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2528 ocp_data &= ~LED_MODE_MASK;
2529 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2530 }
2531
hayeswang00a5e362014-02-18 21:48:59 +08002532 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002533
hayeswangac718b62013-05-02 16:01:25 +00002534 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2535 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2536 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2537 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2538 ocp_data &= ~MCU_CLK_RATIO_MASK;
2539 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2540 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2541 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2542 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2543 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2544
2545 r8152b_enable_eee(tp);
2546 r8152b_enable_aldps(tp);
2547 r8152b_enable_fc(tp);
2548
hayeswangebc2ec482013-08-14 20:54:38 +08002549 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002550 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08002551 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002552 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2553}
2554
hayeswang43779f82014-01-02 11:25:10 +08002555static void r8153_init(struct r8152 *tp)
2556{
2557 u32 ocp_data;
2558 int i;
2559
hayeswangb9702722014-02-18 21:49:00 +08002560 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002561
2562 for (i = 0; i < 500; i++) {
2563 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2564 AUTOLOAD_DONE)
2565 break;
2566 msleep(20);
2567 }
2568
2569 for (i = 0; i < 500; i++) {
2570 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2571 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2572 break;
2573 msleep(20);
2574 }
2575
hayeswangb9702722014-02-18 21:49:00 +08002576 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002577
2578 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2579 ocp_data &= ~TIMER11_EN;
2580 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2581
hayeswang43779f82014-01-02 11:25:10 +08002582 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2583 ocp_data &= ~LED_MODE_MASK;
2584 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2585
2586 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2587 ocp_data &= ~LPM_TIMER_MASK;
2588 if (tp->udev->speed == USB_SPEED_SUPER)
2589 ocp_data |= LPM_TIMER_500US;
2590 else
2591 ocp_data |= LPM_TIMER_500MS;
2592 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2593
2594 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2595 ocp_data &= ~SEN_VAL_MASK;
2596 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2597 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2598
hayeswangb9702722014-02-18 21:49:00 +08002599 r8153_power_cut_en(tp, false);
2600 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002601
hayeswang43779f82014-01-02 11:25:10 +08002602 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2603 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2604 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2605 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2606 U1U2_SPDWN_EN | L1_SPDWN_EN);
2607 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2608 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2609 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2610 EEE_SPDWN_EN);
2611
2612 r8153_enable_eee(tp);
2613 r8153_enable_aldps(tp);
2614 r8152b_enable_fc(tp);
hayeswang43779f82014-01-02 11:25:10 +08002615}
2616
hayeswangac718b62013-05-02 16:01:25 +00002617static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2618{
2619 struct r8152 *tp = usb_get_intfdata(intf);
2620
2621 netif_device_detach(tp->netdev);
2622
2623 if (netif_running(tp->netdev)) {
2624 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002625 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002626 cancel_delayed_work_sync(&tp->schedule);
hayeswangebc2ec482013-08-14 20:54:38 +08002627 tasklet_disable(&tp->tl);
hayeswang7e9da482014-02-18 21:49:05 +08002628 tp->rtl_ops.down(tp);
2629 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002630 }
2631
hayeswangac718b62013-05-02 16:01:25 +00002632 return 0;
2633}
2634
2635static int rtl8152_resume(struct usb_interface *intf)
2636{
2637 struct r8152 *tp = usb_get_intfdata(intf);
2638
hayeswangc81229c2014-01-02 11:22:42 +08002639 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002640 netif_device_attach(tp->netdev);
2641 if (netif_running(tp->netdev)) {
hayeswang7e9da482014-02-18 21:49:05 +08002642 tp->rtl_ops.up(tp);
hayeswang43779f82014-01-02 11:25:10 +08002643 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2644 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2645 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002646 tp->speed = 0;
2647 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002648 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002649 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangac718b62013-05-02 16:01:25 +00002650 }
2651
2652 return 0;
2653}
2654
2655static void rtl8152_get_drvinfo(struct net_device *netdev,
2656 struct ethtool_drvinfo *info)
2657{
2658 struct r8152 *tp = netdev_priv(netdev);
2659
2660 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
2661 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
2662 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
2663}
2664
2665static
2666int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
2667{
2668 struct r8152 *tp = netdev_priv(netdev);
2669
2670 if (!tp->mii.mdio_read)
2671 return -EOPNOTSUPP;
2672
2673 return mii_ethtool_gset(&tp->mii, cmd);
2674}
2675
2676static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2677{
2678 struct r8152 *tp = netdev_priv(dev);
2679
2680 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2681}
2682
2683static struct ethtool_ops ops = {
2684 .get_drvinfo = rtl8152_get_drvinfo,
2685 .get_settings = rtl8152_get_settings,
2686 .set_settings = rtl8152_set_settings,
2687 .get_link = ethtool_op_get_link,
2688};
2689
2690static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2691{
2692 struct r8152 *tp = netdev_priv(netdev);
2693 struct mii_ioctl_data *data = if_mii(rq);
2694 int res = 0;
2695
2696 switch (cmd) {
2697 case SIOCGMIIPHY:
2698 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2699 break;
2700
2701 case SIOCGMIIREG:
2702 data->val_out = r8152_mdio_read(tp, data->reg_num);
2703 break;
2704
2705 case SIOCSMIIREG:
2706 if (!capable(CAP_NET_ADMIN)) {
2707 res = -EPERM;
2708 break;
2709 }
2710 r8152_mdio_write(tp, data->reg_num, data->val_in);
2711 break;
2712
2713 default:
2714 res = -EOPNOTSUPP;
2715 }
2716
2717 return res;
2718}
2719
2720static const struct net_device_ops rtl8152_netdev_ops = {
2721 .ndo_open = rtl8152_open,
2722 .ndo_stop = rtl8152_close,
2723 .ndo_do_ioctl = rtl8152_ioctl,
2724 .ndo_start_xmit = rtl8152_start_xmit,
2725 .ndo_tx_timeout = rtl8152_tx_timeout,
2726 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2727 .ndo_set_mac_address = rtl8152_set_mac_address,
2728
2729 .ndo_change_mtu = eth_change_mtu,
2730 .ndo_validate_addr = eth_validate_addr,
2731};
2732
2733static void r8152b_get_version(struct r8152 *tp)
2734{
2735 u32 ocp_data;
2736 u16 version;
2737
2738 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2739 version = (u16)(ocp_data & VERSION_MASK);
2740
2741 switch (version) {
2742 case 0x4c00:
2743 tp->version = RTL_VER_01;
2744 break;
2745 case 0x4c10:
2746 tp->version = RTL_VER_02;
2747 break;
hayeswang43779f82014-01-02 11:25:10 +08002748 case 0x5c00:
2749 tp->version = RTL_VER_03;
2750 tp->mii.supports_gmii = 1;
2751 break;
2752 case 0x5c10:
2753 tp->version = RTL_VER_04;
2754 tp->mii.supports_gmii = 1;
2755 break;
2756 case 0x5c20:
2757 tp->version = RTL_VER_05;
2758 tp->mii.supports_gmii = 1;
2759 break;
hayeswangac718b62013-05-02 16:01:25 +00002760 default:
2761 netif_info(tp, probe, tp->netdev,
2762 "Unknown version 0x%04x\n", version);
2763 break;
2764 }
2765}
2766
hayeswange3fe0b12014-01-02 11:22:39 +08002767static void rtl8152_unload(struct r8152 *tp)
2768{
hayeswang00a5e362014-02-18 21:48:59 +08002769 if (tp->version != RTL_VER_01)
2770 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08002771}
2772
hayeswang43779f82014-01-02 11:25:10 +08002773static void rtl8153_unload(struct r8152 *tp)
2774{
hayeswangb9702722014-02-18 21:49:00 +08002775 r8153_power_cut_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002776}
2777
hayeswang31ca1de2014-01-06 17:08:43 +08002778static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
hayeswangc81229c2014-01-02 11:22:42 +08002779{
2780 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang31ca1de2014-01-06 17:08:43 +08002781 int ret = -ENODEV;
hayeswangc81229c2014-01-02 11:22:42 +08002782
2783 switch (id->idVendor) {
2784 case VENDOR_ID_REALTEK:
2785 switch (id->idProduct) {
2786 case PRODUCT_ID_RTL8152:
2787 ops->init = r8152b_init;
2788 ops->enable = rtl8152_enable;
2789 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08002790 ops->up = r8152b_exit_oob;
hayeswangc81229c2014-01-02 11:22:42 +08002791 ops->down = rtl8152_down;
2792 ops->unload = rtl8152_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002793 ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08002794 break;
hayeswang43779f82014-01-02 11:25:10 +08002795 case PRODUCT_ID_RTL8153:
2796 ops->init = r8153_init;
2797 ops->enable = rtl8153_enable;
2798 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08002799 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08002800 ops->down = rtl8153_down;
2801 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002802 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08002803 break;
2804 default:
hayeswang43779f82014-01-02 11:25:10 +08002805 break;
2806 }
2807 break;
2808
2809 case VENDOR_ID_SAMSUNG:
2810 switch (id->idProduct) {
2811 case PRODUCT_ID_SAMSUNG:
2812 ops->init = r8153_init;
2813 ops->enable = rtl8153_enable;
2814 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08002815 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08002816 ops->down = rtl8153_down;
2817 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002818 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08002819 break;
hayeswangc81229c2014-01-02 11:22:42 +08002820 default:
hayeswangc81229c2014-01-02 11:22:42 +08002821 break;
2822 }
2823 break;
2824
2825 default:
hayeswangc81229c2014-01-02 11:22:42 +08002826 break;
2827 }
2828
hayeswang31ca1de2014-01-06 17:08:43 +08002829 if (ret)
2830 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
2831
hayeswangc81229c2014-01-02 11:22:42 +08002832 return ret;
2833}
2834
hayeswangac718b62013-05-02 16:01:25 +00002835static int rtl8152_probe(struct usb_interface *intf,
2836 const struct usb_device_id *id)
2837{
2838 struct usb_device *udev = interface_to_usbdev(intf);
2839 struct r8152 *tp;
2840 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08002841 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002842
hayeswangac718b62013-05-02 16:01:25 +00002843 netdev = alloc_etherdev(sizeof(struct r8152));
2844 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08002845 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00002846 return -ENOMEM;
2847 }
2848
hayeswangebc2ec482013-08-14 20:54:38 +08002849 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00002850 tp = netdev_priv(netdev);
2851 tp->msg_enable = 0x7FFF;
2852
hayeswange3ad4122014-01-06 17:08:42 +08002853 tp->udev = udev;
2854 tp->netdev = netdev;
2855 tp->intf = intf;
2856
hayeswang31ca1de2014-01-06 17:08:43 +08002857 ret = rtl_ops_init(tp, id);
2858 if (ret)
2859 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08002860
hayeswangebc2ec482013-08-14 20:54:38 +08002861 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00002862 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
2863
hayeswangac718b62013-05-02 16:01:25 +00002864 netdev->netdev_ops = &rtl8152_netdev_ops;
2865 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08002866
2867 netdev->features |= NETIF_F_IP_CSUM;
2868 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangac718b62013-05-02 16:01:25 +00002869 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00002870
2871 tp->mii.dev = netdev;
2872 tp->mii.mdio_read = read_mii_word;
2873 tp->mii.mdio_write = write_mii_word;
2874 tp->mii.phy_id_mask = 0x3f;
2875 tp->mii.reg_num_mask = 0x1f;
2876 tp->mii.phy_id = R8152_PHY_ID;
2877 tp->mii.supports_gmii = 0;
2878
2879 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08002880 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002881 set_ethernet_addr(tp);
2882
hayeswangac718b62013-05-02 16:01:25 +00002883 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00002884
hayeswangebc2ec482013-08-14 20:54:38 +08002885 ret = register_netdev(netdev);
2886 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08002887 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08002888 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00002889 }
2890
Hayes Wang4a8deae2014-01-07 11:18:22 +08002891 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00002892
2893 return 0;
2894
hayeswangac718b62013-05-02 16:01:25 +00002895out1:
hayeswangebc2ec482013-08-14 20:54:38 +08002896 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00002897out:
2898 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002899 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002900}
2901
hayeswangac718b62013-05-02 16:01:25 +00002902static void rtl8152_disconnect(struct usb_interface *intf)
2903{
2904 struct r8152 *tp = usb_get_intfdata(intf);
2905
2906 usb_set_intfdata(intf, NULL);
2907 if (tp) {
2908 set_bit(RTL8152_UNPLUG, &tp->flags);
2909 tasklet_kill(&tp->tl);
2910 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08002911 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00002912 free_netdev(tp->netdev);
2913 }
2914}
2915
2916/* table of devices that work with this driver */
2917static struct usb_device_id rtl8152_table[] = {
hayeswangc7de7de2014-01-15 10:42:16 +08002918 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
2919 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
2920 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00002921 {}
2922};
2923
2924MODULE_DEVICE_TABLE(usb, rtl8152_table);
2925
2926static struct usb_driver rtl8152_driver = {
2927 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08002928 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00002929 .probe = rtl8152_probe,
2930 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00002931 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08002932 .resume = rtl8152_resume,
2933 .reset_resume = rtl8152_resume,
hayeswangac718b62013-05-02 16:01:25 +00002934};
2935
Sachin Kamatb4236daa2013-05-16 17:48:08 +00002936module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00002937
2938MODULE_AUTHOR(DRIVER_AUTHOR);
2939MODULE_DESCRIPTION(DRIVER_DESC);
2940MODULE_LICENSE("GPL");