blob: 2a778faa96307888fd53e9a8b76b845dce8853cb [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,
hayeswangac718b62013-05-02 16:01:25 +0000439};
440
441/* Define these values to match your device */
442#define VENDOR_ID_REALTEK 0x0bda
443#define PRODUCT_ID_RTL8152 0x8152
hayeswang43779f82014-01-02 11:25:10 +0800444#define PRODUCT_ID_RTL8153 0x8153
445
446#define VENDOR_ID_SAMSUNG 0x04e8
447#define PRODUCT_ID_SAMSUNG 0xa101
hayeswangac718b62013-05-02 16:01:25 +0000448
449#define MCU_TYPE_PLA 0x0100
450#define MCU_TYPE_USB 0x0000
451
hayeswangc7de7de2014-01-15 10:42:16 +0800452#define REALTEK_USB_DEVICE(vend, prod) \
453 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)
454
hayeswangac718b62013-05-02 16:01:25 +0000455struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800456 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000457#define RX_LEN_MASK 0x7fff
hayeswang500b6d72013-11-20 17:30:57 +0800458 __le32 opts2;
459 __le32 opts3;
460 __le32 opts4;
461 __le32 opts5;
462 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000463};
464
465struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800466 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000467#define TX_FS (1 << 31) /* First segment of a packet */
468#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang5bd23882013-08-14 20:54:39 +0800469#define TX_LEN_MASK 0x3ffff
470
hayeswang500b6d72013-11-20 17:30:57 +0800471 __le32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800472#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
473#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
474#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
475#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswangac718b62013-05-02 16:01:25 +0000476};
477
hayeswangdff4e8a2013-08-16 16:09:33 +0800478struct r8152;
479
hayeswangebc2ec482013-08-14 20:54:38 +0800480struct rx_agg {
481 struct list_head list;
482 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800483 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800484 void *buffer;
485 void *head;
486};
487
488struct tx_agg {
489 struct list_head list;
490 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800491 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800492 void *buffer;
493 void *head;
494 u32 skb_num;
495 u32 skb_len;
496};
497
hayeswangac718b62013-05-02 16:01:25 +0000498struct r8152 {
499 unsigned long flags;
500 struct usb_device *udev;
501 struct tasklet_struct tl;
hayeswang40a82912013-08-14 20:54:40 +0800502 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000503 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800504 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800505 struct tx_agg tx_info[RTL8152_MAX_TX];
506 struct rx_agg rx_info[RTL8152_MAX_RX];
507 struct list_head rx_done, tx_free;
508 struct sk_buff_head tx_queue;
509 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000510 struct delayed_work schedule;
511 struct mii_if_info mii;
hayeswangc81229c2014-01-02 11:22:42 +0800512
513 struct rtl_ops {
514 void (*init)(struct r8152 *);
515 int (*enable)(struct r8152 *);
516 void (*disable)(struct r8152 *);
517 void (*down)(struct r8152 *);
518 void (*unload)(struct r8152 *);
519 } rtl_ops;
520
hayeswang40a82912013-08-14 20:54:40 +0800521 int intr_interval;
hayeswangac718b62013-05-02 16:01:25 +0000522 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800523 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000524 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800525 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000526 u8 version;
527 u8 speed;
528};
529
530enum rtl_version {
531 RTL_VER_UNKNOWN = 0,
532 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800533 RTL_VER_02,
534 RTL_VER_03,
535 RTL_VER_04,
536 RTL_VER_05,
537 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000538};
539
540/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
541 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
542 */
543static const int multicast_filter_limit = 32;
hayeswangebc2ec482013-08-14 20:54:38 +0800544static unsigned int rx_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000545
546static
547int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
548{
hayeswang31787f52013-07-31 17:21:25 +0800549 int ret;
550 void *tmp;
551
552 tmp = kmalloc(size, GFP_KERNEL);
553 if (!tmp)
554 return -ENOMEM;
555
556 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000557 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
hayeswang31787f52013-07-31 17:21:25 +0800558 value, index, tmp, size, 500);
559
560 memcpy(data, tmp, size);
561 kfree(tmp);
562
563 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000564}
565
566static
567int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
568{
hayeswang31787f52013-07-31 17:21:25 +0800569 int ret;
570 void *tmp;
571
572 tmp = kmalloc(size, GFP_KERNEL);
573 if (!tmp)
574 return -ENOMEM;
575
576 memcpy(tmp, data, size);
577
578 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000579 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
hayeswang31787f52013-07-31 17:21:25 +0800580 value, index, tmp, size, 500);
581
582 kfree(tmp);
583 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000584}
585
586static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
587 void *data, u16 type)
588{
hayeswang45f4a192014-01-06 17:08:41 +0800589 u16 limit = 64;
590 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000591
592 if (test_bit(RTL8152_UNPLUG, &tp->flags))
593 return -ENODEV;
594
595 /* both size and indix must be 4 bytes align */
596 if ((size & 3) || !size || (index & 3) || !data)
597 return -EPERM;
598
599 if ((u32)index + (u32)size > 0xffff)
600 return -EPERM;
601
602 while (size) {
603 if (size > limit) {
604 ret = get_registers(tp, index, type, limit, data);
605 if (ret < 0)
606 break;
607
608 index += limit;
609 data += limit;
610 size -= limit;
611 } else {
612 ret = get_registers(tp, index, type, size, data);
613 if (ret < 0)
614 break;
615
616 index += size;
617 data += size;
618 size = 0;
619 break;
620 }
621 }
622
623 return ret;
624}
625
626static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
627 u16 size, void *data, u16 type)
628{
hayeswang45f4a192014-01-06 17:08:41 +0800629 int ret;
630 u16 byteen_start, byteen_end, byen;
631 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000632
633 if (test_bit(RTL8152_UNPLUG, &tp->flags))
634 return -ENODEV;
635
636 /* both size and indix must be 4 bytes align */
637 if ((size & 3) || !size || (index & 3) || !data)
638 return -EPERM;
639
640 if ((u32)index + (u32)size > 0xffff)
641 return -EPERM;
642
643 byteen_start = byteen & BYTE_EN_START_MASK;
644 byteen_end = byteen & BYTE_EN_END_MASK;
645
646 byen = byteen_start | (byteen_start << 4);
647 ret = set_registers(tp, index, type | byen, 4, data);
648 if (ret < 0)
649 goto error1;
650
651 index += 4;
652 data += 4;
653 size -= 4;
654
655 if (size) {
656 size -= 4;
657
658 while (size) {
659 if (size > limit) {
660 ret = set_registers(tp, index,
661 type | BYTE_EN_DWORD,
662 limit, data);
663 if (ret < 0)
664 goto error1;
665
666 index += limit;
667 data += limit;
668 size -= limit;
669 } else {
670 ret = set_registers(tp, index,
671 type | BYTE_EN_DWORD,
672 size, data);
673 if (ret < 0)
674 goto error1;
675
676 index += size;
677 data += size;
678 size = 0;
679 break;
680 }
681 }
682
683 byen = byteen_end | (byteen_end >> 4);
684 ret = set_registers(tp, index, type | byen, 4, data);
685 if (ret < 0)
686 goto error1;
687 }
688
689error1:
690 return ret;
691}
692
693static inline
694int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
695{
696 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
697}
698
699static inline
700int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
701{
702 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
703}
704
705static inline
706int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
707{
708 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
709}
710
711static inline
712int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
713{
714 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
715}
716
717static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
718{
hayeswangc8826de2013-07-31 17:21:26 +0800719 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000720
hayeswangc8826de2013-07-31 17:21:26 +0800721 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000722
723 return __le32_to_cpu(data);
724}
725
726static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
727{
hayeswangc8826de2013-07-31 17:21:26 +0800728 __le32 tmp = __cpu_to_le32(data);
729
730 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000731}
732
733static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
734{
735 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800736 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000737 u8 shift = index & 2;
738
739 index &= ~3;
740
hayeswangc8826de2013-07-31 17:21:26 +0800741 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000742
hayeswangc8826de2013-07-31 17:21:26 +0800743 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000744 data >>= (shift * 8);
745 data &= 0xffff;
746
747 return (u16)data;
748}
749
750static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
751{
hayeswangc8826de2013-07-31 17:21:26 +0800752 u32 mask = 0xffff;
753 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000754 u16 byen = BYTE_EN_WORD;
755 u8 shift = index & 2;
756
757 data &= mask;
758
759 if (index & 2) {
760 byen <<= shift;
761 mask <<= (shift * 8);
762 data <<= (shift * 8);
763 index &= ~3;
764 }
765
hayeswangc8826de2013-07-31 17:21:26 +0800766 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000767
hayeswangc8826de2013-07-31 17:21:26 +0800768 data |= __le32_to_cpu(tmp) & ~mask;
769 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000770
hayeswangc8826de2013-07-31 17:21:26 +0800771 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000772}
773
774static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
775{
776 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800777 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000778 u8 shift = index & 3;
779
780 index &= ~3;
781
hayeswangc8826de2013-07-31 17:21:26 +0800782 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000783
hayeswangc8826de2013-07-31 17:21:26 +0800784 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000785 data >>= (shift * 8);
786 data &= 0xff;
787
788 return (u8)data;
789}
790
791static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
792{
hayeswangc8826de2013-07-31 17:21:26 +0800793 u32 mask = 0xff;
794 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000795 u16 byen = BYTE_EN_BYTE;
796 u8 shift = index & 3;
797
798 data &= mask;
799
800 if (index & 3) {
801 byen <<= shift;
802 mask <<= (shift * 8);
803 data <<= (shift * 8);
804 index &= ~3;
805 }
806
hayeswangc8826de2013-07-31 17:21:26 +0800807 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000808
hayeswangc8826de2013-07-31 17:21:26 +0800809 data |= __le32_to_cpu(tmp) & ~mask;
810 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000811
hayeswangc8826de2013-07-31 17:21:26 +0800812 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000813}
814
hayeswangac244d32014-01-02 11:22:40 +0800815static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
816{
817 u16 ocp_base, ocp_index;
818
819 ocp_base = addr & 0xf000;
820 if (ocp_base != tp->ocp_base) {
821 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
822 tp->ocp_base = ocp_base;
823 }
824
825 ocp_index = (addr & 0x0fff) | 0xb000;
826 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
827}
828
hayeswange3fe0b12014-01-02 11:22:39 +0800829static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
830{
831 u16 ocp_base, ocp_index;
832
833 ocp_base = addr & 0xf000;
834 if (ocp_base != tp->ocp_base) {
835 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
836 tp->ocp_base = ocp_base;
837 }
838
839 ocp_index = (addr & 0x0fff) | 0xb000;
840 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
841}
842
hayeswangac244d32014-01-02 11:22:40 +0800843static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000844{
hayeswangac244d32014-01-02 11:22:40 +0800845 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000846}
847
hayeswangac244d32014-01-02 11:22:40 +0800848static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000849{
hayeswangac244d32014-01-02 11:22:40 +0800850 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000851}
852
hayeswang43779f82014-01-02 11:25:10 +0800853static void sram_write(struct r8152 *tp, u16 addr, u16 data)
854{
855 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
856 ocp_reg_write(tp, OCP_SRAM_DATA, data);
857}
858
859static u16 sram_read(struct r8152 *tp, u16 addr)
860{
861 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
862 return ocp_reg_read(tp, OCP_SRAM_DATA);
863}
864
hayeswangac718b62013-05-02 16:01:25 +0000865static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
866{
867 struct r8152 *tp = netdev_priv(netdev);
868
869 if (phy_id != R8152_PHY_ID)
870 return -EINVAL;
871
872 return r8152_mdio_read(tp, reg);
873}
874
875static
876void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
877{
878 struct r8152 *tp = netdev_priv(netdev);
879
880 if (phy_id != R8152_PHY_ID)
881 return;
882
883 r8152_mdio_write(tp, reg, val);
884}
885
hayeswangebc2ec482013-08-14 20:54:38 +0800886static
887int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
888
hayeswangac718b62013-05-02 16:01:25 +0000889static inline void set_ethernet_addr(struct r8152 *tp)
890{
891 struct net_device *dev = tp->netdev;
hayeswang8a91c822014-02-18 21:49:01 +0800892 int ret;
hayeswang31787f52013-07-31 17:21:25 +0800893 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000894
hayeswang8a91c822014-02-18 21:49:01 +0800895 if (tp->version == RTL_VER_01)
896 ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id);
897 else
898 ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id);
899
900 if (ret < 0) {
hayeswangac718b62013-05-02 16:01:25 +0000901 netif_notice(tp, probe, dev, "inet addr fail\n");
hayeswang8a91c822014-02-18 21:49:01 +0800902 } else {
903 if (tp->version != RTL_VER_01) {
904 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
905 CRWECR_CONFIG);
906 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES,
907 sizeof(node_id), node_id);
908 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
909 CRWECR_NORAML);
910 }
911
hayeswangac718b62013-05-02 16:01:25 +0000912 memcpy(dev->dev_addr, node_id, dev->addr_len);
913 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
914 }
hayeswangac718b62013-05-02 16:01:25 +0000915}
916
917static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
918{
919 struct r8152 *tp = netdev_priv(netdev);
920 struct sockaddr *addr = p;
921
922 if (!is_valid_ether_addr(addr->sa_data))
923 return -EADDRNOTAVAIL;
924
925 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
926
927 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
928 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
929 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
930
931 return 0;
932}
933
hayeswangac718b62013-05-02 16:01:25 +0000934static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
935{
936 return &dev->stats;
937}
938
939static void read_bulk_callback(struct urb *urb)
940{
hayeswangac718b62013-05-02 16:01:25 +0000941 struct net_device *netdev;
hayeswanga5a4f462013-08-16 16:09:34 +0800942 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +0000943 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +0800944 struct rx_agg *agg;
945 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000946 int result;
hayeswangac718b62013-05-02 16:01:25 +0000947
hayeswangebc2ec482013-08-14 20:54:38 +0800948 agg = urb->context;
949 if (!agg)
950 return;
951
952 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000953 if (!tp)
954 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800955
hayeswangac718b62013-05-02 16:01:25 +0000956 if (test_bit(RTL8152_UNPLUG, &tp->flags))
957 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800958
959 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000960 return;
961
hayeswangebc2ec482013-08-14 20:54:38 +0800962 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +0800963
964 /* When link down, the driver would cancel all bulks. */
965 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +0800966 if (!netif_carrier_ok(netdev))
967 return;
968
hayeswangac718b62013-05-02 16:01:25 +0000969 switch (status) {
970 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +0800971 if (urb->actual_length < ETH_ZLEN)
972 break;
973
hayeswanga5a4f462013-08-16 16:09:34 +0800974 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800975 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +0800976 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800977 tasklet_schedule(&tp->tl);
978 return;
hayeswangac718b62013-05-02 16:01:25 +0000979 case -ESHUTDOWN:
980 set_bit(RTL8152_UNPLUG, &tp->flags);
981 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +0800982 return;
hayeswangac718b62013-05-02 16:01:25 +0000983 case -ENOENT:
984 return; /* the urb is in unlink state */
985 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +0800986 if (net_ratelimit())
987 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +0800988 break;
hayeswangac718b62013-05-02 16:01:25 +0000989 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +0800990 if (net_ratelimit())
991 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +0800992 break;
hayeswangac718b62013-05-02 16:01:25 +0000993 }
994
hayeswangebc2ec482013-08-14 20:54:38 +0800995 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +0000996 if (result == -ENODEV) {
997 netif_device_detach(tp->netdev);
998 } else if (result) {
hayeswanga5a4f462013-08-16 16:09:34 +0800999 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001000 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +08001001 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001002 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001003 }
hayeswangac718b62013-05-02 16:01:25 +00001004}
1005
1006static void write_bulk_callback(struct urb *urb)
1007{
hayeswangebc2ec482013-08-14 20:54:38 +08001008 struct net_device_stats *stats;
hayeswanga5a4f462013-08-16 16:09:34 +08001009 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001010 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001011 struct r8152 *tp;
1012 int status = urb->status;
1013
hayeswangebc2ec482013-08-14 20:54:38 +08001014 agg = urb->context;
1015 if (!agg)
1016 return;
1017
1018 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001019 if (!tp)
1020 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001021
1022 stats = rtl8152_get_stats(tp->netdev);
1023 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001024 if (net_ratelimit())
1025 netdev_warn(tp->netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001026 stats->tx_errors += agg->skb_num;
1027 } else {
1028 stats->tx_packets += agg->skb_num;
1029 stats->tx_bytes += agg->skb_len;
1030 }
1031
hayeswanga5a4f462013-08-16 16:09:34 +08001032 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001033 list_add_tail(&agg->list, &tp->tx_free);
hayeswanga5a4f462013-08-16 16:09:34 +08001034 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001035
1036 if (!netif_carrier_ok(tp->netdev))
hayeswangac718b62013-05-02 16:01:25 +00001037 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001038
1039 if (!test_bit(WORK_ENABLE, &tp->flags))
1040 return;
1041
1042 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1043 return;
1044
1045 if (!skb_queue_empty(&tp->tx_queue))
1046 tasklet_schedule(&tp->tl);
1047}
1048
hayeswang40a82912013-08-14 20:54:40 +08001049static void intr_callback(struct urb *urb)
1050{
1051 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001052 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001053 int status = urb->status;
1054 int res;
1055
1056 tp = urb->context;
1057 if (!tp)
1058 return;
1059
1060 if (!test_bit(WORK_ENABLE, &tp->flags))
1061 return;
1062
1063 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1064 return;
1065
1066 switch (status) {
1067 case 0: /* success */
1068 break;
1069 case -ECONNRESET: /* unlink */
1070 case -ESHUTDOWN:
1071 netif_device_detach(tp->netdev);
1072 case -ENOENT:
1073 return;
1074 case -EOVERFLOW:
1075 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1076 goto resubmit;
1077 /* -EPIPE: should clear the halt */
1078 default:
1079 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1080 goto resubmit;
1081 }
1082
1083 d = urb->transfer_buffer;
1084 if (INTR_LINK & __le16_to_cpu(d[0])) {
1085 if (!(tp->speed & LINK_STATUS)) {
1086 set_bit(RTL8152_LINK_CHG, &tp->flags);
1087 schedule_delayed_work(&tp->schedule, 0);
1088 }
1089 } else {
1090 if (tp->speed & LINK_STATUS) {
1091 set_bit(RTL8152_LINK_CHG, &tp->flags);
1092 schedule_delayed_work(&tp->schedule, 0);
1093 }
1094 }
1095
1096resubmit:
1097 res = usb_submit_urb(urb, GFP_ATOMIC);
1098 if (res == -ENODEV)
1099 netif_device_detach(tp->netdev);
1100 else if (res)
1101 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001102 "can't resubmit intr, status %d\n", res);
hayeswang40a82912013-08-14 20:54:40 +08001103}
1104
hayeswangebc2ec482013-08-14 20:54:38 +08001105static inline void *rx_agg_align(void *data)
1106{
hayeswang8e1f51b2014-01-02 11:22:41 +08001107 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001108}
1109
1110static inline void *tx_agg_align(void *data)
1111{
hayeswang8e1f51b2014-01-02 11:22:41 +08001112 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001113}
1114
1115static void free_all_mem(struct r8152 *tp)
1116{
1117 int i;
1118
1119 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001120 usb_free_urb(tp->rx_info[i].urb);
1121 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001122
hayeswang9629e3c2014-01-15 10:42:15 +08001123 kfree(tp->rx_info[i].buffer);
1124 tp->rx_info[i].buffer = NULL;
1125 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001126 }
1127
1128 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001129 usb_free_urb(tp->tx_info[i].urb);
1130 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001131
hayeswang9629e3c2014-01-15 10:42:15 +08001132 kfree(tp->tx_info[i].buffer);
1133 tp->tx_info[i].buffer = NULL;
1134 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001135 }
hayeswang40a82912013-08-14 20:54:40 +08001136
hayeswang9629e3c2014-01-15 10:42:15 +08001137 usb_free_urb(tp->intr_urb);
1138 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001139
hayeswang9629e3c2014-01-15 10:42:15 +08001140 kfree(tp->intr_buff);
1141 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001142}
1143
1144static int alloc_all_mem(struct r8152 *tp)
1145{
1146 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001147 struct usb_interface *intf = tp->intf;
1148 struct usb_host_interface *alt = intf->cur_altsetting;
1149 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001150 struct urb *urb;
1151 int node, i;
1152 u8 *buf;
1153
1154 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1155
1156 spin_lock_init(&tp->rx_lock);
1157 spin_lock_init(&tp->tx_lock);
1158 INIT_LIST_HEAD(&tp->rx_done);
1159 INIT_LIST_HEAD(&tp->tx_free);
1160 skb_queue_head_init(&tp->tx_queue);
1161
1162 for (i = 0; i < RTL8152_MAX_RX; i++) {
1163 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1164 if (!buf)
1165 goto err1;
1166
1167 if (buf != rx_agg_align(buf)) {
1168 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001169 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1170 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001171 if (!buf)
1172 goto err1;
1173 }
1174
1175 urb = usb_alloc_urb(0, GFP_KERNEL);
1176 if (!urb) {
1177 kfree(buf);
1178 goto err1;
1179 }
1180
1181 INIT_LIST_HEAD(&tp->rx_info[i].list);
1182 tp->rx_info[i].context = tp;
1183 tp->rx_info[i].urb = urb;
1184 tp->rx_info[i].buffer = buf;
1185 tp->rx_info[i].head = rx_agg_align(buf);
1186 }
1187
1188 for (i = 0; i < RTL8152_MAX_TX; i++) {
1189 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1190 if (!buf)
1191 goto err1;
1192
1193 if (buf != tx_agg_align(buf)) {
1194 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001195 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1196 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001197 if (!buf)
1198 goto err1;
1199 }
1200
1201 urb = usb_alloc_urb(0, GFP_KERNEL);
1202 if (!urb) {
1203 kfree(buf);
1204 goto err1;
1205 }
1206
1207 INIT_LIST_HEAD(&tp->tx_info[i].list);
1208 tp->tx_info[i].context = tp;
1209 tp->tx_info[i].urb = urb;
1210 tp->tx_info[i].buffer = buf;
1211 tp->tx_info[i].head = tx_agg_align(buf);
1212
1213 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1214 }
1215
hayeswang40a82912013-08-14 20:54:40 +08001216 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1217 if (!tp->intr_urb)
1218 goto err1;
1219
1220 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1221 if (!tp->intr_buff)
1222 goto err1;
1223
1224 tp->intr_interval = (int)ep_intr->desc.bInterval;
1225 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1226 tp->intr_buff, INTBUFSIZE, intr_callback,
1227 tp, tp->intr_interval);
1228
hayeswangebc2ec482013-08-14 20:54:38 +08001229 return 0;
1230
1231err1:
1232 free_all_mem(tp);
1233 return -ENOMEM;
1234}
1235
hayeswang0de98f62013-08-16 16:09:35 +08001236static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1237{
1238 struct tx_agg *agg = NULL;
1239 unsigned long flags;
1240
1241 spin_lock_irqsave(&tp->tx_lock, flags);
1242 if (!list_empty(&tp->tx_free)) {
1243 struct list_head *cursor;
1244
1245 cursor = tp->tx_free.next;
1246 list_del_init(cursor);
1247 agg = list_entry(cursor, struct tx_agg, list);
1248 }
1249 spin_unlock_irqrestore(&tp->tx_lock, flags);
1250
1251 return agg;
1252}
1253
hayeswang5bd23882013-08-14 20:54:39 +08001254static void
1255r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1256{
1257 memset(desc, 0, sizeof(*desc));
1258
1259 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1260
1261 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1262 __be16 protocol;
1263 u8 ip_protocol;
1264 u32 opts2 = 0;
1265
1266 if (skb->protocol == htons(ETH_P_8021Q))
1267 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1268 else
1269 protocol = skb->protocol;
1270
1271 switch (protocol) {
1272 case htons(ETH_P_IP):
1273 opts2 |= IPV4_CS;
1274 ip_protocol = ip_hdr(skb)->protocol;
1275 break;
1276
1277 case htons(ETH_P_IPV6):
1278 opts2 |= IPV6_CS;
1279 ip_protocol = ipv6_hdr(skb)->nexthdr;
1280 break;
1281
1282 default:
1283 ip_protocol = IPPROTO_RAW;
1284 break;
1285 }
1286
1287 if (ip_protocol == IPPROTO_TCP) {
1288 opts2 |= TCP_CS;
1289 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1290 } else if (ip_protocol == IPPROTO_UDP) {
1291 opts2 |= UDP_CS;
1292 } else {
1293 WARN_ON_ONCE(1);
1294 }
1295
1296 desc->opts2 = cpu_to_le32(opts2);
1297 }
1298}
1299
hayeswangb1379d92013-08-16 16:09:37 +08001300static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1301{
hayeswangd84130a2014-02-18 21:49:02 +08001302 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1303 unsigned long flags;
hayeswang7937f9e2013-11-20 17:30:54 +08001304 int remain;
hayeswangb1379d92013-08-16 16:09:37 +08001305 u8 *tx_data;
1306
hayeswangd84130a2014-02-18 21:49:02 +08001307 __skb_queue_head_init(&skb_head);
1308 spin_lock_irqsave(&tx_queue->lock, flags);
1309 skb_queue_splice_init(tx_queue, &skb_head);
1310 spin_unlock_irqrestore(&tx_queue->lock, flags);
1311
hayeswangb1379d92013-08-16 16:09:37 +08001312 tx_data = agg->head;
1313 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001314 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001315
hayeswang7937f9e2013-11-20 17:30:54 +08001316 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001317 struct tx_desc *tx_desc;
1318 struct sk_buff *skb;
1319 unsigned int len;
1320
hayeswangd84130a2014-02-18 21:49:02 +08001321 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001322 if (!skb)
1323 break;
1324
hayeswang7937f9e2013-11-20 17:30:54 +08001325 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001326 len = skb->len;
1327 if (remain < len) {
hayeswangd84130a2014-02-18 21:49:02 +08001328 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001329 break;
1330 }
1331
hayeswang7937f9e2013-11-20 17:30:54 +08001332 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001333 tx_desc = (struct tx_desc *)tx_data;
1334 tx_data += sizeof(*tx_desc);
1335
1336 r8152_tx_csum(tp, tx_desc, skb);
1337 memcpy(tx_data, skb->data, len);
1338 agg->skb_num++;
1339 agg->skb_len += len;
1340 dev_kfree_skb_any(skb);
1341
hayeswang7937f9e2013-11-20 17:30:54 +08001342 tx_data += len;
1343 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001344 }
1345
hayeswangd84130a2014-02-18 21:49:02 +08001346 if (!skb_queue_empty(&skb_head)) {
1347 spin_lock_irqsave(&tx_queue->lock, flags);
1348 skb_queue_splice(&skb_head, tx_queue);
1349 spin_unlock_irqrestore(&tx_queue->lock, flags);
1350 }
1351
hayeswangdd1b1192013-11-20 17:30:56 +08001352 netif_tx_lock(tp->netdev);
1353
1354 if (netif_queue_stopped(tp->netdev) &&
1355 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1356 netif_wake_queue(tp->netdev);
1357
1358 netif_tx_unlock(tp->netdev);
1359
hayeswangb1379d92013-08-16 16:09:37 +08001360 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1361 agg->head, (int)(tx_data - (u8 *)agg->head),
1362 (usb_complete_t)write_bulk_callback, agg);
1363
1364 return usb_submit_urb(agg->urb, GFP_ATOMIC);
1365}
1366
hayeswangebc2ec482013-08-14 20:54:38 +08001367static void rx_bottom(struct r8152 *tp)
1368{
hayeswanga5a4f462013-08-16 16:09:34 +08001369 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001370 struct list_head *cursor, *next, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +08001371
hayeswangd84130a2014-02-18 21:49:02 +08001372 if (list_empty(&tp->rx_done))
1373 return;
1374
1375 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001376 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001377 list_splice_init(&tp->rx_done, &rx_queue);
1378 spin_unlock_irqrestore(&tp->rx_lock, flags);
1379
1380 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001381 struct rx_desc *rx_desc;
1382 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001383 int len_used = 0;
1384 struct urb *urb;
1385 u8 *rx_data;
1386 int ret;
1387
hayeswangebc2ec482013-08-14 20:54:38 +08001388 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001389
1390 agg = list_entry(cursor, struct rx_agg, list);
1391 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001392 if (urb->actual_length < ETH_ZLEN)
1393 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001394
hayeswangebc2ec482013-08-14 20:54:38 +08001395 rx_desc = agg->head;
1396 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001397 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001398
hayeswang7937f9e2013-11-20 17:30:54 +08001399 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001400 struct net_device *netdev = tp->netdev;
1401 struct net_device_stats *stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001402 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001403 struct sk_buff *skb;
1404
hayeswang7937f9e2013-11-20 17:30:54 +08001405 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001406 if (pkt_len < ETH_ZLEN)
1407 break;
1408
hayeswang7937f9e2013-11-20 17:30:54 +08001409 len_used += pkt_len;
1410 if (urb->actual_length < len_used)
1411 break;
1412
hayeswang43a44782013-08-16 16:09:36 +08001413 stats = rtl8152_get_stats(netdev);
1414
hayeswang8e1f51b2014-01-02 11:22:41 +08001415 pkt_len -= CRC_SIZE;
hayeswangebc2ec482013-08-14 20:54:38 +08001416 rx_data += sizeof(struct rx_desc);
1417
1418 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1419 if (!skb) {
1420 stats->rx_dropped++;
1421 break;
1422 }
1423 memcpy(skb->data, rx_data, pkt_len);
1424 skb_put(skb, pkt_len);
1425 skb->protocol = eth_type_trans(skb, netdev);
1426 netif_rx(skb);
1427 stats->rx_packets++;
1428 stats->rx_bytes += pkt_len;
1429
hayeswang8e1f51b2014-01-02 11:22:41 +08001430 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec482013-08-14 20:54:38 +08001431 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001432 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001433 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001434 }
1435
hayeswang0de98f62013-08-16 16:09:35 +08001436submit:
hayeswangebc2ec482013-08-14 20:54:38 +08001437 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangebc2ec482013-08-14 20:54:38 +08001438 if (ret && ret != -ENODEV) {
hayeswangd84130a2014-02-18 21:49:02 +08001439 spin_lock_irqsave(&tp->rx_lock, flags);
1440 list_add_tail(&agg->list, &tp->rx_done);
1441 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001442 tasklet_schedule(&tp->tl);
1443 }
1444 }
hayeswangebc2ec482013-08-14 20:54:38 +08001445}
1446
1447static void tx_bottom(struct r8152 *tp)
1448{
hayeswangebc2ec482013-08-14 20:54:38 +08001449 int res;
1450
hayeswangb1379d92013-08-16 16:09:37 +08001451 do {
1452 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001453
hayeswangb1379d92013-08-16 16:09:37 +08001454 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001455 break;
1456
hayeswangb1379d92013-08-16 16:09:37 +08001457 agg = r8152_get_tx_agg(tp);
1458 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001459 break;
hayeswangb1379d92013-08-16 16:09:37 +08001460
1461 res = r8152_tx_agg_fill(tp, agg);
1462 if (res) {
1463 struct net_device_stats *stats;
1464 struct net_device *netdev;
1465 unsigned long flags;
1466
1467 netdev = tp->netdev;
1468 stats = rtl8152_get_stats(netdev);
1469
1470 if (res == -ENODEV) {
1471 netif_device_detach(netdev);
1472 } else {
1473 netif_warn(tp, tx_err, netdev,
1474 "failed tx_urb %d\n", res);
1475 stats->tx_dropped += agg->skb_num;
1476 spin_lock_irqsave(&tp->tx_lock, flags);
1477 list_add_tail(&agg->list, &tp->tx_free);
1478 spin_unlock_irqrestore(&tp->tx_lock, flags);
1479 }
hayeswangebc2ec482013-08-14 20:54:38 +08001480 }
hayeswangb1379d92013-08-16 16:09:37 +08001481 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001482}
1483
1484static void bottom_half(unsigned long data)
1485{
1486 struct r8152 *tp;
1487
1488 tp = (struct r8152 *)data;
1489
1490 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1491 return;
1492
1493 if (!test_bit(WORK_ENABLE, &tp->flags))
1494 return;
1495
hayeswang7559fb2f2013-08-16 16:09:38 +08001496 /* When link down, the driver would cancel all bulks. */
1497 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001498 if (!netif_carrier_ok(tp->netdev))
1499 return;
1500
1501 rx_bottom(tp);
1502 tx_bottom(tp);
1503}
1504
1505static
1506int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1507{
1508 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1509 agg->head, rx_buf_sz,
1510 (usb_complete_t)read_bulk_callback, agg);
1511
1512 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001513}
1514
hayeswang00a5e362014-02-18 21:48:59 +08001515static void rtl_drop_queued_tx(struct r8152 *tp)
1516{
1517 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001518 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1519 unsigned long flags;
hayeswang00a5e362014-02-18 21:48:59 +08001520 struct sk_buff *skb;
1521
hayeswangd84130a2014-02-18 21:49:02 +08001522 if (skb_queue_empty(tx_queue))
1523 return;
1524
1525 __skb_queue_head_init(&skb_head);
1526 spin_lock_irqsave(&tx_queue->lock, flags);
1527 skb_queue_splice_init(tx_queue, &skb_head);
1528 spin_unlock_irqrestore(&tx_queue->lock, flags);
1529
1530 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001531 dev_kfree_skb(skb);
1532 stats->tx_dropped++;
1533 }
1534}
1535
hayeswangac718b62013-05-02 16:01:25 +00001536static void rtl8152_tx_timeout(struct net_device *netdev)
1537{
1538 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001539 int i;
1540
Hayes Wang4a8deae2014-01-07 11:18:22 +08001541 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001542 for (i = 0; i < RTL8152_MAX_TX; i++)
1543 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001544}
1545
1546static void rtl8152_set_rx_mode(struct net_device *netdev)
1547{
1548 struct r8152 *tp = netdev_priv(netdev);
1549
hayeswang40a82912013-08-14 20:54:40 +08001550 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001551 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001552 schedule_delayed_work(&tp->schedule, 0);
1553 }
hayeswangac718b62013-05-02 16:01:25 +00001554}
1555
1556static void _rtl8152_set_rx_mode(struct net_device *netdev)
1557{
1558 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001559 u32 mc_filter[2]; /* Multicast hash filter */
1560 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001561 u32 ocp_data;
1562
hayeswangac718b62013-05-02 16:01:25 +00001563 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1564 netif_stop_queue(netdev);
1565 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1566 ocp_data &= ~RCR_ACPT_ALL;
1567 ocp_data |= RCR_AB | RCR_APM;
1568
1569 if (netdev->flags & IFF_PROMISC) {
1570 /* Unconditionally log net taps. */
1571 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1572 ocp_data |= RCR_AM | RCR_AAP;
1573 mc_filter[1] = mc_filter[0] = 0xffffffff;
1574 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1575 (netdev->flags & IFF_ALLMULTI)) {
1576 /* Too many to filter perfectly -- accept all multicasts. */
1577 ocp_data |= RCR_AM;
1578 mc_filter[1] = mc_filter[0] = 0xffffffff;
1579 } else {
1580 struct netdev_hw_addr *ha;
1581
1582 mc_filter[1] = mc_filter[0] = 0;
1583 netdev_for_each_mc_addr(ha, netdev) {
1584 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1585 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1586 ocp_data |= RCR_AM;
1587 }
1588 }
1589
hayeswang31787f52013-07-31 17:21:25 +08001590 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1591 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001592
hayeswang31787f52013-07-31 17:21:25 +08001593 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001594 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1595 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001596}
1597
1598static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1599 struct net_device *netdev)
1600{
1601 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001602
hayeswangac718b62013-05-02 16:01:25 +00001603 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001604
hayeswang61598782013-11-20 17:30:55 +08001605 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001606
hayeswangdd1b1192013-11-20 17:30:56 +08001607 if (list_empty(&tp->tx_free) &&
1608 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1609 netif_stop_queue(netdev);
1610
hayeswang61598782013-11-20 17:30:55 +08001611 if (!list_empty(&tp->tx_free))
1612 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001613
1614 return NETDEV_TX_OK;
1615}
1616
1617static void r8152b_reset_packet_filter(struct r8152 *tp)
1618{
1619 u32 ocp_data;
1620
1621 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1622 ocp_data &= ~FMC_FCR_MCU_EN;
1623 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1624 ocp_data |= FMC_FCR_MCU_EN;
1625 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1626}
1627
1628static void rtl8152_nic_reset(struct r8152 *tp)
1629{
1630 int i;
1631
1632 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1633
1634 for (i = 0; i < 1000; i++) {
1635 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1636 break;
1637 udelay(100);
1638 }
1639}
1640
hayeswangdd1b1192013-11-20 17:30:56 +08001641static void set_tx_qlen(struct r8152 *tp)
1642{
1643 struct net_device *netdev = tp->netdev;
1644
1645 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1646 sizeof(struct tx_desc));
1647}
1648
hayeswangac718b62013-05-02 16:01:25 +00001649static inline u8 rtl8152_get_speed(struct r8152 *tp)
1650{
1651 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1652}
1653
hayeswang507605a2014-01-02 11:22:43 +08001654static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001655{
hayeswangebc2ec482013-08-14 20:54:38 +08001656 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001657 u8 speed;
1658
1659 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001660 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001661 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001662 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001663 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1664 } else {
1665 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001666 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001667 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1668 }
hayeswang507605a2014-01-02 11:22:43 +08001669}
1670
hayeswang00a5e362014-02-18 21:48:59 +08001671static void rxdy_gated_en(struct r8152 *tp, bool enable)
1672{
1673 u32 ocp_data;
1674
1675 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1676 if (enable)
1677 ocp_data |= RXDY_GATED_EN;
1678 else
1679 ocp_data &= ~RXDY_GATED_EN;
1680 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1681}
1682
hayeswang507605a2014-01-02 11:22:43 +08001683static int rtl_enable(struct r8152 *tp)
1684{
1685 u32 ocp_data;
1686 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001687
1688 r8152b_reset_packet_filter(tp);
1689
1690 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1691 ocp_data |= CR_RE | CR_TE;
1692 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1693
hayeswang00a5e362014-02-18 21:48:59 +08001694 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001695
hayeswangebc2ec482013-08-14 20:54:38 +08001696 INIT_LIST_HEAD(&tp->rx_done);
1697 ret = 0;
1698 for (i = 0; i < RTL8152_MAX_RX; i++) {
1699 INIT_LIST_HEAD(&tp->rx_info[i].list);
1700 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1701 }
hayeswangac718b62013-05-02 16:01:25 +00001702
hayeswangebc2ec482013-08-14 20:54:38 +08001703 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001704}
1705
hayeswang507605a2014-01-02 11:22:43 +08001706static int rtl8152_enable(struct r8152 *tp)
1707{
1708 set_tx_qlen(tp);
1709 rtl_set_eee_plus(tp);
1710
1711 return rtl_enable(tp);
1712}
1713
hayeswang43779f82014-01-02 11:25:10 +08001714static void r8153_set_rx_agg(struct r8152 *tp)
1715{
1716 u8 speed;
1717
1718 speed = rtl8152_get_speed(tp);
1719 if (speed & _1000bps) {
1720 if (tp->udev->speed == USB_SPEED_SUPER) {
1721 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1722 RX_THR_SUPPER);
1723 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1724 EARLY_AGG_SUPPER);
1725 } else {
1726 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1727 RX_THR_HIGH);
1728 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1729 EARLY_AGG_HIGH);
1730 }
1731 } else {
1732 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1733 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1734 EARLY_AGG_SLOW);
1735 }
1736}
1737
1738static int rtl8153_enable(struct r8152 *tp)
1739{
1740 set_tx_qlen(tp);
1741 rtl_set_eee_plus(tp);
1742 r8153_set_rx_agg(tp);
1743
1744 return rtl_enable(tp);
1745}
1746
hayeswangac718b62013-05-02 16:01:25 +00001747static void rtl8152_disable(struct r8152 *tp)
1748{
hayeswangebc2ec482013-08-14 20:54:38 +08001749 u32 ocp_data;
1750 int i;
hayeswangac718b62013-05-02 16:01:25 +00001751
1752 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1753 ocp_data &= ~RCR_ACPT_ALL;
1754 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1755
hayeswang00a5e362014-02-18 21:48:59 +08001756 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001757
1758 for (i = 0; i < RTL8152_MAX_TX; i++)
1759 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001760
hayeswang00a5e362014-02-18 21:48:59 +08001761 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00001762
1763 for (i = 0; i < 1000; i++) {
1764 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1765 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1766 break;
1767 mdelay(1);
1768 }
1769
1770 for (i = 0; i < 1000; i++) {
1771 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1772 break;
1773 mdelay(1);
1774 }
1775
hayeswangebc2ec482013-08-14 20:54:38 +08001776 for (i = 0; i < RTL8152_MAX_RX; i++)
1777 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001778
1779 rtl8152_nic_reset(tp);
1780}
1781
hayeswang00a5e362014-02-18 21:48:59 +08001782static void r8152_power_cut_en(struct r8152 *tp, bool enable)
1783{
1784 u32 ocp_data;
1785
1786 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1787 if (enable)
1788 ocp_data |= POWER_CUT;
1789 else
1790 ocp_data &= ~POWER_CUT;
1791 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1792
1793 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1794 ocp_data &= ~RESUME_INDICATE;
1795 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1796
1797}
1798
hayeswang43499682014-02-18 21:48:58 +08001799static void rtl_clear_bp(struct r8152 *tp)
1800{
1801 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1802 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1803 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1804 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1805 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1806 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1807 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1808 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1809 mdelay(3);
1810 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1811 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1812}
1813
1814static void r8153_clear_bp(struct r8152 *tp)
1815{
1816 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
1817 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
1818 rtl_clear_bp(tp);
1819}
1820
1821static void r8153_teredo_off(struct r8152 *tp)
1822{
1823 u32 ocp_data;
1824
1825 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
1826 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
1827 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
1828
1829 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
1830 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
1831 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
1832}
1833
1834static void r8152b_disable_aldps(struct r8152 *tp)
1835{
1836 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1837 msleep(20);
1838}
1839
1840static inline void r8152b_enable_aldps(struct r8152 *tp)
1841{
1842 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1843 LINKENA | DIS_SDSAVE);
1844}
1845
1846static void r8152b_hw_phy_cfg(struct r8152 *tp)
1847{
1848 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1849 r8152b_disable_aldps(tp);
1850}
1851
hayeswangac718b62013-05-02 16:01:25 +00001852static void r8152b_exit_oob(struct r8152 *tp)
1853{
1854 u32 ocp_data;
1855 int i;
1856
1857 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1858 ocp_data &= ~RCR_ACPT_ALL;
1859 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1860
hayeswang00a5e362014-02-18 21:48:59 +08001861 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00001862
1863 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1864 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1865
1866 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1867 ocp_data &= ~NOW_IS_OOB;
1868 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1869
1870 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1871 ocp_data &= ~MCU_BORW_EN;
1872 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1873
1874 for (i = 0; i < 1000; i++) {
1875 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1876 if (ocp_data & LINK_LIST_READY)
1877 break;
1878 mdelay(1);
1879 }
1880
1881 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1882 ocp_data |= RE_INIT_LL;
1883 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1884
1885 for (i = 0; i < 1000; i++) {
1886 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1887 if (ocp_data & LINK_LIST_READY)
1888 break;
1889 mdelay(1);
1890 }
1891
1892 rtl8152_nic_reset(tp);
1893
1894 /* rx share fifo credit full threshold */
1895 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1896
1897 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1898 ocp_data &= STAT_SPEED_MASK;
1899 if (ocp_data == STAT_SPEED_FULL) {
1900 /* rx share fifo credit near full threshold */
1901 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1902 RXFIFO_THR2_FULL);
1903 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1904 RXFIFO_THR3_FULL);
1905 } else {
1906 /* rx share fifo credit near full threshold */
1907 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1908 RXFIFO_THR2_HIGH);
1909 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1910 RXFIFO_THR3_HIGH);
1911 }
1912
1913 /* TX share fifo free credit full threshold */
1914 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1915
1916 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08001917 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00001918 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1919 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1920
1921 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1922 ocp_data &= ~CPCR_RX_VLAN;
1923 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1924
1925 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1926
1927 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1928 ocp_data |= TCR0_AUTO_FIFO;
1929 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1930}
1931
1932static void r8152b_enter_oob(struct r8152 *tp)
1933{
hayeswang45f4a192014-01-06 17:08:41 +08001934 u32 ocp_data;
1935 int i;
hayeswangac718b62013-05-02 16:01:25 +00001936
1937 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1938 ocp_data &= ~NOW_IS_OOB;
1939 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1940
1941 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1942 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1943 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1944
1945 rtl8152_disable(tp);
1946
1947 for (i = 0; i < 1000; i++) {
1948 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1949 if (ocp_data & LINK_LIST_READY)
1950 break;
1951 mdelay(1);
1952 }
1953
1954 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1955 ocp_data |= RE_INIT_LL;
1956 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1957
1958 for (i = 0; i < 1000; i++) {
1959 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1960 if (ocp_data & LINK_LIST_READY)
1961 break;
1962 mdelay(1);
1963 }
1964
1965 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1966
1967 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1968 ocp_data |= MAGIC_EN;
1969 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1970
1971 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1972 ocp_data |= CPCR_RX_VLAN;
1973 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1974
1975 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
1976 ocp_data |= ALDPS_PROXY_MODE;
1977 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
1978
1979 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1980 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1981 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1982
1983 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
1984
hayeswang00a5e362014-02-18 21:48:59 +08001985 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001986
1987 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1988 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1989 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1990}
1991
hayeswang43779f82014-01-02 11:25:10 +08001992static void r8153_hw_phy_cfg(struct r8152 *tp)
1993{
1994 u32 ocp_data;
1995 u16 data;
1996
1997 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
1998 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1999
2000 if (tp->version == RTL_VER_03) {
2001 data = ocp_reg_read(tp, OCP_EEE_CFG);
2002 data &= ~CTAP_SHORT_EN;
2003 ocp_reg_write(tp, OCP_EEE_CFG, data);
2004 }
2005
2006 data = ocp_reg_read(tp, OCP_POWER_CFG);
2007 data |= EEE_CLKDIV_EN;
2008 ocp_reg_write(tp, OCP_POWER_CFG, data);
2009
2010 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2011 data |= EN_10M_BGOFF;
2012 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2013 data = ocp_reg_read(tp, OCP_POWER_CFG);
2014 data |= EN_10M_PLLOFF;
2015 ocp_reg_write(tp, OCP_POWER_CFG, data);
2016 data = sram_read(tp, SRAM_IMPEDANCE);
2017 data &= ~RX_DRIVING_MASK;
2018 sram_write(tp, SRAM_IMPEDANCE, data);
2019
2020 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2021 ocp_data |= PFM_PWM_SWITCH;
2022 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2023
2024 data = sram_read(tp, SRAM_LPF_CFG);
2025 data |= LPF_AUTO_TUNE;
2026 sram_write(tp, SRAM_LPF_CFG, data);
2027
2028 data = sram_read(tp, SRAM_10M_AMP1);
2029 data |= GDAC_IB_UPALL;
2030 sram_write(tp, SRAM_10M_AMP1, data);
2031 data = sram_read(tp, SRAM_10M_AMP2);
2032 data |= AMP_DN;
2033 sram_write(tp, SRAM_10M_AMP2, data);
2034}
2035
hayeswangb9702722014-02-18 21:49:00 +08002036static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002037{
2038 u8 u1u2[8];
2039
2040 if (enable)
2041 memset(u1u2, 0xff, sizeof(u1u2));
2042 else
2043 memset(u1u2, 0x00, sizeof(u1u2));
2044
2045 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2046}
2047
hayeswangb9702722014-02-18 21:49:00 +08002048static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002049{
2050 u32 ocp_data;
2051
2052 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2053 if (enable)
2054 ocp_data |= U2P3_ENABLE;
2055 else
2056 ocp_data &= ~U2P3_ENABLE;
2057 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2058}
2059
hayeswangb9702722014-02-18 21:49:00 +08002060static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002061{
2062 u32 ocp_data;
2063
2064 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2065 if (enable)
2066 ocp_data |= PWR_EN | PHASE2_EN;
2067 else
2068 ocp_data &= ~(PWR_EN | PHASE2_EN);
2069 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2070
2071 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2072 ocp_data &= ~PCUT_STATUS;
2073 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2074}
2075
hayeswang43779f82014-01-02 11:25:10 +08002076static void r8153_first_init(struct r8152 *tp)
2077{
2078 u32 ocp_data;
2079 int i;
2080
hayeswang00a5e362014-02-18 21:48:59 +08002081 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002082 r8153_teredo_off(tp);
2083
2084 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2085 ocp_data &= ~RCR_ACPT_ALL;
2086 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2087
2088 r8153_hw_phy_cfg(tp);
2089
2090 rtl8152_nic_reset(tp);
2091
2092 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2093 ocp_data &= ~NOW_IS_OOB;
2094 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2095
2096 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2097 ocp_data &= ~MCU_BORW_EN;
2098 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2099
2100 for (i = 0; i < 1000; i++) {
2101 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2102 if (ocp_data & LINK_LIST_READY)
2103 break;
2104 mdelay(1);
2105 }
2106
2107 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2108 ocp_data |= RE_INIT_LL;
2109 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2110
2111 for (i = 0; i < 1000; i++) {
2112 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2113 if (ocp_data & LINK_LIST_READY)
2114 break;
2115 mdelay(1);
2116 }
2117
2118 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2119 ocp_data &= ~CPCR_RX_VLAN;
2120 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2121
2122 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2123
2124 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2125 ocp_data |= TCR0_AUTO_FIFO;
2126 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2127
2128 rtl8152_nic_reset(tp);
2129
2130 /* rx share fifo credit full threshold */
2131 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2132 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2133 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2134 /* TX share fifo free credit full threshold */
2135 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2136
hayeswang9629e3c2014-01-15 10:42:15 +08002137 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002138 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2139 ocp_data &= ~RX_AGG_DISABLE;
2140 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2141}
2142
2143static void r8153_enter_oob(struct r8152 *tp)
2144{
2145 u32 ocp_data;
2146 int i;
2147
2148 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2149 ocp_data &= ~NOW_IS_OOB;
2150 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2151
2152 rtl8152_disable(tp);
2153
2154 for (i = 0; i < 1000; i++) {
2155 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2156 if (ocp_data & LINK_LIST_READY)
2157 break;
2158 mdelay(1);
2159 }
2160
2161 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2162 ocp_data |= RE_INIT_LL;
2163 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2164
2165 for (i = 0; i < 1000; i++) {
2166 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2167 if (ocp_data & LINK_LIST_READY)
2168 break;
2169 mdelay(1);
2170 }
2171
2172 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2173
2174 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2175 ocp_data |= MAGIC_EN;
2176 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2177
2178 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2179 ocp_data &= ~TEREDO_WAKE_MASK;
2180 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2181
2182 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2183 ocp_data |= CPCR_RX_VLAN;
2184 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2185
2186 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2187 ocp_data |= ALDPS_PROXY_MODE;
2188 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2189
2190 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2191 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2192 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2193
2194 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
2195
hayeswang00a5e362014-02-18 21:48:59 +08002196 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002197
2198 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2199 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2200 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2201}
2202
2203static void r8153_disable_aldps(struct r8152 *tp)
2204{
2205 u16 data;
2206
2207 data = ocp_reg_read(tp, OCP_POWER_CFG);
2208 data &= ~EN_ALDPS;
2209 ocp_reg_write(tp, OCP_POWER_CFG, data);
2210 msleep(20);
2211}
2212
2213static void r8153_enable_aldps(struct r8152 *tp)
2214{
2215 u16 data;
2216
2217 data = ocp_reg_read(tp, OCP_POWER_CFG);
2218 data |= EN_ALDPS;
2219 ocp_reg_write(tp, OCP_POWER_CFG, data);
2220}
2221
hayeswangac718b62013-05-02 16:01:25 +00002222static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2223{
hayeswang43779f82014-01-02 11:25:10 +08002224 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002225 int ret = 0;
2226
2227 cancel_delayed_work_sync(&tp->schedule);
2228 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2229 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2230 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002231 if (tp->mii.supports_gmii) {
2232 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2233 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2234 } else {
2235 gbcr = 0;
2236 }
hayeswangac718b62013-05-02 16:01:25 +00002237
2238 if (autoneg == AUTONEG_DISABLE) {
2239 if (speed == SPEED_10) {
2240 bmcr = 0;
2241 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2242 } else if (speed == SPEED_100) {
2243 bmcr = BMCR_SPEED100;
2244 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002245 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2246 bmcr = BMCR_SPEED1000;
2247 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002248 } else {
2249 ret = -EINVAL;
2250 goto out;
2251 }
2252
2253 if (duplex == DUPLEX_FULL)
2254 bmcr |= BMCR_FULLDPLX;
2255 } else {
2256 if (speed == SPEED_10) {
2257 if (duplex == DUPLEX_FULL)
2258 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2259 else
2260 anar |= ADVERTISE_10HALF;
2261 } else if (speed == SPEED_100) {
2262 if (duplex == DUPLEX_FULL) {
2263 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2264 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2265 } else {
2266 anar |= ADVERTISE_10HALF;
2267 anar |= ADVERTISE_100HALF;
2268 }
hayeswang43779f82014-01-02 11:25:10 +08002269 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2270 if (duplex == DUPLEX_FULL) {
2271 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2272 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2273 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2274 } else {
2275 anar |= ADVERTISE_10HALF;
2276 anar |= ADVERTISE_100HALF;
2277 gbcr |= ADVERTISE_1000HALF;
2278 }
hayeswangac718b62013-05-02 16:01:25 +00002279 } else {
2280 ret = -EINVAL;
2281 goto out;
2282 }
2283
2284 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2285 }
2286
hayeswang43779f82014-01-02 11:25:10 +08002287 if (tp->mii.supports_gmii)
2288 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2289
hayeswangac718b62013-05-02 16:01:25 +00002290 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2291 r8152_mdio_write(tp, MII_BMCR, bmcr);
2292
2293out:
hayeswangac718b62013-05-02 16:01:25 +00002294
2295 return ret;
2296}
2297
2298static void rtl8152_down(struct r8152 *tp)
2299{
hayeswang00a5e362014-02-18 21:48:59 +08002300 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002301 r8152b_disable_aldps(tp);
2302 r8152b_enter_oob(tp);
2303 r8152b_enable_aldps(tp);
2304}
2305
hayeswang43779f82014-01-02 11:25:10 +08002306static void rtl8153_down(struct r8152 *tp)
2307{
hayeswangb9702722014-02-18 21:49:00 +08002308 r8153_u1u2en(tp, false);
2309 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002310 r8153_disable_aldps(tp);
2311 r8153_enter_oob(tp);
2312 r8153_enable_aldps(tp);
2313}
2314
hayeswangac718b62013-05-02 16:01:25 +00002315static void set_carrier(struct r8152 *tp)
2316{
2317 struct net_device *netdev = tp->netdev;
2318 u8 speed;
2319
hayeswang40a82912013-08-14 20:54:40 +08002320 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002321 speed = rtl8152_get_speed(tp);
2322
2323 if (speed & LINK_STATUS) {
2324 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002325 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002326 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2327 netif_carrier_on(netdev);
2328 }
2329 } else {
2330 if (tp->speed & LINK_STATUS) {
2331 netif_carrier_off(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002332 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002333 tp->rtl_ops.disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002334 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002335 }
2336 }
2337 tp->speed = speed;
2338}
2339
2340static void rtl_work_func_t(struct work_struct *work)
2341{
2342 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2343
2344 if (!test_bit(WORK_ENABLE, &tp->flags))
2345 goto out1;
2346
2347 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2348 goto out1;
2349
hayeswang40a82912013-08-14 20:54:40 +08002350 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2351 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002352
2353 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2354 _rtl8152_set_rx_mode(tp->netdev);
2355
hayeswangac718b62013-05-02 16:01:25 +00002356out1:
2357 return;
2358}
2359
2360static int rtl8152_open(struct net_device *netdev)
2361{
2362 struct r8152 *tp = netdev_priv(netdev);
2363 int res = 0;
2364
hayeswang43779f82014-01-02 11:25:10 +08002365 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2366 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2367 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002368 tp->speed = 0;
2369 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002370 netif_start_queue(netdev);
2371 set_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002372 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2373 if (res) {
2374 if (res == -ENODEV)
2375 netif_device_detach(tp->netdev);
2376 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2377 res);
2378 }
2379
hayeswangac718b62013-05-02 16:01:25 +00002380
2381 return res;
2382}
2383
2384static int rtl8152_close(struct net_device *netdev)
2385{
2386 struct r8152 *tp = netdev_priv(netdev);
2387 int res = 0;
2388
2389 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002390 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002391 cancel_delayed_work_sync(&tp->schedule);
2392 netif_stop_queue(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002393 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002394 tp->rtl_ops.disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002395 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002396
2397 return res;
2398}
2399
hayeswangac718b62013-05-02 16:01:25 +00002400static void r8152b_enable_eee(struct r8152 *tp)
2401{
hayeswang45f4a192014-01-06 17:08:41 +08002402 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002403
2404 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2405 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2406 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2407 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2408 EEE_10_CAP | EEE_NWAY_EN |
2409 TX_QUIET_EN | RX_QUIET_EN |
2410 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2411 SDFALLTIME);
2412 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2413 RG_LDVQUIET_EN | RG_CKRSEL |
2414 RG_EEEPRG_EN);
2415 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2416 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2417 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2418 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2419 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2420 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2421}
2422
hayeswang43779f82014-01-02 11:25:10 +08002423static void r8153_enable_eee(struct r8152 *tp)
2424{
2425 u32 ocp_data;
2426 u16 data;
2427
2428 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2429 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2430 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2431 data = ocp_reg_read(tp, OCP_EEE_CFG);
2432 data |= EEE10_EN;
2433 ocp_reg_write(tp, OCP_EEE_CFG, data);
2434 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2435 data |= MY1000_EEE | MY100_EEE;
2436 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2437}
2438
hayeswangac718b62013-05-02 16:01:25 +00002439static void r8152b_enable_fc(struct r8152 *tp)
2440{
2441 u16 anar;
2442
2443 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2444 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2445 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2446}
2447
hayeswangac718b62013-05-02 16:01:25 +00002448static void r8152b_init(struct r8152 *tp)
2449{
hayeswangebc2ec482013-08-14 20:54:38 +08002450 u32 ocp_data;
2451 int i;
hayeswangac718b62013-05-02 16:01:25 +00002452
2453 rtl_clear_bp(tp);
2454
2455 if (tp->version == RTL_VER_01) {
2456 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2457 ocp_data &= ~LED_MODE_MASK;
2458 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2459 }
2460
2461 r8152b_hw_phy_cfg(tp);
2462
hayeswang00a5e362014-02-18 21:48:59 +08002463 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002464
hayeswangac718b62013-05-02 16:01:25 +00002465
2466 r8152b_exit_oob(tp);
2467
2468 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2469 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2470 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2471 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2472 ocp_data &= ~MCU_CLK_RATIO_MASK;
2473 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2474 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2475 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2476 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2477 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2478
2479 r8152b_enable_eee(tp);
2480 r8152b_enable_aldps(tp);
2481 r8152b_enable_fc(tp);
2482
2483 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
2484 BMCR_ANRESTART);
2485 for (i = 0; i < 100; i++) {
2486 udelay(100);
2487 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
2488 break;
2489 }
2490
hayeswangebc2ec482013-08-14 20:54:38 +08002491 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002492 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08002493 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002494 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2495}
2496
hayeswang43779f82014-01-02 11:25:10 +08002497static void r8153_init(struct r8152 *tp)
2498{
2499 u32 ocp_data;
2500 int i;
2501
hayeswangb9702722014-02-18 21:49:00 +08002502 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002503
2504 for (i = 0; i < 500; i++) {
2505 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2506 AUTOLOAD_DONE)
2507 break;
2508 msleep(20);
2509 }
2510
2511 for (i = 0; i < 500; i++) {
2512 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2513 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2514 break;
2515 msleep(20);
2516 }
2517
hayeswangb9702722014-02-18 21:49:00 +08002518 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002519
2520 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2521 ocp_data &= ~TIMER11_EN;
2522 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2523
2524 r8153_clear_bp(tp);
2525
2526 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2527 ocp_data &= ~LED_MODE_MASK;
2528 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2529
2530 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2531 ocp_data &= ~LPM_TIMER_MASK;
2532 if (tp->udev->speed == USB_SPEED_SUPER)
2533 ocp_data |= LPM_TIMER_500US;
2534 else
2535 ocp_data |= LPM_TIMER_500MS;
2536 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2537
2538 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2539 ocp_data &= ~SEN_VAL_MASK;
2540 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2541 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2542
hayeswangb9702722014-02-18 21:49:00 +08002543 r8153_power_cut_en(tp, false);
2544 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002545
2546 r8153_first_init(tp);
2547
2548 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2549 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2550 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2551 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2552 U1U2_SPDWN_EN | L1_SPDWN_EN);
2553 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2554 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2555 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2556 EEE_SPDWN_EN);
2557
2558 r8153_enable_eee(tp);
2559 r8153_enable_aldps(tp);
2560 r8152b_enable_fc(tp);
2561
2562 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
2563 BMCR_ANRESTART);
2564}
2565
hayeswangac718b62013-05-02 16:01:25 +00002566static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2567{
2568 struct r8152 *tp = usb_get_intfdata(intf);
2569
2570 netif_device_detach(tp->netdev);
2571
2572 if (netif_running(tp->netdev)) {
2573 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002574 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002575 cancel_delayed_work_sync(&tp->schedule);
hayeswangebc2ec482013-08-14 20:54:38 +08002576 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002577 }
2578
hayeswangc81229c2014-01-02 11:22:42 +08002579 tp->rtl_ops.down(tp);
hayeswangac718b62013-05-02 16:01:25 +00002580
2581 return 0;
2582}
2583
2584static int rtl8152_resume(struct usb_interface *intf)
2585{
2586 struct r8152 *tp = usb_get_intfdata(intf);
2587
hayeswangc81229c2014-01-02 11:22:42 +08002588 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002589 netif_device_attach(tp->netdev);
2590 if (netif_running(tp->netdev)) {
hayeswang43779f82014-01-02 11:25:10 +08002591 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2592 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2593 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002594 tp->speed = 0;
2595 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002596 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002597 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangebc2ec482013-08-14 20:54:38 +08002598 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002599 }
2600
2601 return 0;
2602}
2603
2604static void rtl8152_get_drvinfo(struct net_device *netdev,
2605 struct ethtool_drvinfo *info)
2606{
2607 struct r8152 *tp = netdev_priv(netdev);
2608
2609 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
2610 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
2611 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
2612}
2613
2614static
2615int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
2616{
2617 struct r8152 *tp = netdev_priv(netdev);
2618
2619 if (!tp->mii.mdio_read)
2620 return -EOPNOTSUPP;
2621
2622 return mii_ethtool_gset(&tp->mii, cmd);
2623}
2624
2625static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2626{
2627 struct r8152 *tp = netdev_priv(dev);
2628
2629 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2630}
2631
2632static struct ethtool_ops ops = {
2633 .get_drvinfo = rtl8152_get_drvinfo,
2634 .get_settings = rtl8152_get_settings,
2635 .set_settings = rtl8152_set_settings,
2636 .get_link = ethtool_op_get_link,
2637};
2638
2639static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2640{
2641 struct r8152 *tp = netdev_priv(netdev);
2642 struct mii_ioctl_data *data = if_mii(rq);
2643 int res = 0;
2644
2645 switch (cmd) {
2646 case SIOCGMIIPHY:
2647 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2648 break;
2649
2650 case SIOCGMIIREG:
2651 data->val_out = r8152_mdio_read(tp, data->reg_num);
2652 break;
2653
2654 case SIOCSMIIREG:
2655 if (!capable(CAP_NET_ADMIN)) {
2656 res = -EPERM;
2657 break;
2658 }
2659 r8152_mdio_write(tp, data->reg_num, data->val_in);
2660 break;
2661
2662 default:
2663 res = -EOPNOTSUPP;
2664 }
2665
2666 return res;
2667}
2668
2669static const struct net_device_ops rtl8152_netdev_ops = {
2670 .ndo_open = rtl8152_open,
2671 .ndo_stop = rtl8152_close,
2672 .ndo_do_ioctl = rtl8152_ioctl,
2673 .ndo_start_xmit = rtl8152_start_xmit,
2674 .ndo_tx_timeout = rtl8152_tx_timeout,
2675 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2676 .ndo_set_mac_address = rtl8152_set_mac_address,
2677
2678 .ndo_change_mtu = eth_change_mtu,
2679 .ndo_validate_addr = eth_validate_addr,
2680};
2681
2682static void r8152b_get_version(struct r8152 *tp)
2683{
2684 u32 ocp_data;
2685 u16 version;
2686
2687 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2688 version = (u16)(ocp_data & VERSION_MASK);
2689
2690 switch (version) {
2691 case 0x4c00:
2692 tp->version = RTL_VER_01;
2693 break;
2694 case 0x4c10:
2695 tp->version = RTL_VER_02;
2696 break;
hayeswang43779f82014-01-02 11:25:10 +08002697 case 0x5c00:
2698 tp->version = RTL_VER_03;
2699 tp->mii.supports_gmii = 1;
2700 break;
2701 case 0x5c10:
2702 tp->version = RTL_VER_04;
2703 tp->mii.supports_gmii = 1;
2704 break;
2705 case 0x5c20:
2706 tp->version = RTL_VER_05;
2707 tp->mii.supports_gmii = 1;
2708 break;
hayeswangac718b62013-05-02 16:01:25 +00002709 default:
2710 netif_info(tp, probe, tp->netdev,
2711 "Unknown version 0x%04x\n", version);
2712 break;
2713 }
2714}
2715
hayeswange3fe0b12014-01-02 11:22:39 +08002716static void rtl8152_unload(struct r8152 *tp)
2717{
hayeswang00a5e362014-02-18 21:48:59 +08002718 if (tp->version != RTL_VER_01)
2719 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08002720}
2721
hayeswang43779f82014-01-02 11:25:10 +08002722static void rtl8153_unload(struct r8152 *tp)
2723{
hayeswangb9702722014-02-18 21:49:00 +08002724 r8153_power_cut_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002725}
2726
hayeswang31ca1de2014-01-06 17:08:43 +08002727static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
hayeswangc81229c2014-01-02 11:22:42 +08002728{
2729 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang31ca1de2014-01-06 17:08:43 +08002730 int ret = -ENODEV;
hayeswangc81229c2014-01-02 11:22:42 +08002731
2732 switch (id->idVendor) {
2733 case VENDOR_ID_REALTEK:
2734 switch (id->idProduct) {
2735 case PRODUCT_ID_RTL8152:
2736 ops->init = r8152b_init;
2737 ops->enable = rtl8152_enable;
2738 ops->disable = rtl8152_disable;
2739 ops->down = rtl8152_down;
2740 ops->unload = rtl8152_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002741 ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08002742 break;
hayeswang43779f82014-01-02 11:25:10 +08002743 case PRODUCT_ID_RTL8153:
2744 ops->init = r8153_init;
2745 ops->enable = rtl8153_enable;
2746 ops->disable = rtl8152_disable;
2747 ops->down = rtl8153_down;
2748 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002749 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08002750 break;
2751 default:
hayeswang43779f82014-01-02 11:25:10 +08002752 break;
2753 }
2754 break;
2755
2756 case VENDOR_ID_SAMSUNG:
2757 switch (id->idProduct) {
2758 case PRODUCT_ID_SAMSUNG:
2759 ops->init = r8153_init;
2760 ops->enable = rtl8153_enable;
2761 ops->disable = rtl8152_disable;
2762 ops->down = rtl8153_down;
2763 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002764 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08002765 break;
hayeswangc81229c2014-01-02 11:22:42 +08002766 default:
hayeswangc81229c2014-01-02 11:22:42 +08002767 break;
2768 }
2769 break;
2770
2771 default:
hayeswangc81229c2014-01-02 11:22:42 +08002772 break;
2773 }
2774
hayeswang31ca1de2014-01-06 17:08:43 +08002775 if (ret)
2776 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
2777
hayeswangc81229c2014-01-02 11:22:42 +08002778 return ret;
2779}
2780
hayeswangac718b62013-05-02 16:01:25 +00002781static int rtl8152_probe(struct usb_interface *intf,
2782 const struct usb_device_id *id)
2783{
2784 struct usb_device *udev = interface_to_usbdev(intf);
2785 struct r8152 *tp;
2786 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08002787 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002788
hayeswangac718b62013-05-02 16:01:25 +00002789 netdev = alloc_etherdev(sizeof(struct r8152));
2790 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08002791 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00002792 return -ENOMEM;
2793 }
2794
hayeswangebc2ec482013-08-14 20:54:38 +08002795 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00002796 tp = netdev_priv(netdev);
2797 tp->msg_enable = 0x7FFF;
2798
hayeswange3ad4122014-01-06 17:08:42 +08002799 tp->udev = udev;
2800 tp->netdev = netdev;
2801 tp->intf = intf;
2802
hayeswang31ca1de2014-01-06 17:08:43 +08002803 ret = rtl_ops_init(tp, id);
2804 if (ret)
2805 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08002806
hayeswangebc2ec482013-08-14 20:54:38 +08002807 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00002808 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
2809
hayeswangac718b62013-05-02 16:01:25 +00002810 netdev->netdev_ops = &rtl8152_netdev_ops;
2811 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08002812
2813 netdev->features |= NETIF_F_IP_CSUM;
2814 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangac718b62013-05-02 16:01:25 +00002815 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00002816
2817 tp->mii.dev = netdev;
2818 tp->mii.mdio_read = read_mii_word;
2819 tp->mii.mdio_write = write_mii_word;
2820 tp->mii.phy_id_mask = 0x3f;
2821 tp->mii.reg_num_mask = 0x1f;
2822 tp->mii.phy_id = R8152_PHY_ID;
2823 tp->mii.supports_gmii = 0;
2824
2825 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08002826 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002827 set_ethernet_addr(tp);
2828
hayeswangebc2ec482013-08-14 20:54:38 +08002829 ret = alloc_all_mem(tp);
2830 if (ret)
hayeswangac718b62013-05-02 16:01:25 +00002831 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002832
2833 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00002834
hayeswangebc2ec482013-08-14 20:54:38 +08002835 ret = register_netdev(netdev);
2836 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08002837 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08002838 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00002839 }
2840
Hayes Wang4a8deae2014-01-07 11:18:22 +08002841 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00002842
2843 return 0;
2844
hayeswangac718b62013-05-02 16:01:25 +00002845out1:
hayeswangebc2ec482013-08-14 20:54:38 +08002846 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00002847out:
2848 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002849 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002850}
2851
hayeswangac718b62013-05-02 16:01:25 +00002852static void rtl8152_disconnect(struct usb_interface *intf)
2853{
2854 struct r8152 *tp = usb_get_intfdata(intf);
2855
2856 usb_set_intfdata(intf, NULL);
2857 if (tp) {
2858 set_bit(RTL8152_UNPLUG, &tp->flags);
2859 tasklet_kill(&tp->tl);
2860 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08002861 tp->rtl_ops.unload(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002862 free_all_mem(tp);
hayeswangac718b62013-05-02 16:01:25 +00002863 free_netdev(tp->netdev);
2864 }
2865}
2866
2867/* table of devices that work with this driver */
2868static struct usb_device_id rtl8152_table[] = {
hayeswangc7de7de2014-01-15 10:42:16 +08002869 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
2870 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
2871 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00002872 {}
2873};
2874
2875MODULE_DEVICE_TABLE(usb, rtl8152_table);
2876
2877static struct usb_driver rtl8152_driver = {
2878 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08002879 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00002880 .probe = rtl8152_probe,
2881 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00002882 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08002883 .resume = rtl8152_resume,
2884 .reset_resume = rtl8152_resume,
hayeswangac718b62013-05-02 16:01:25 +00002885};
2886
Sachin Kamatb4236daa2013-05-16 17:48:08 +00002887module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00002888
2889MODULE_AUTHOR(DRIVER_AUTHOR);
2890MODULE_DESCRIPTION(DRIVER_DESC);
2891MODULE_LICENSE("GPL");