]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/usb/r8152.c
r8152: adjust rx_bottom
[mirror_ubuntu-artful-kernel.git] / drivers / net / usb / r8152.c
CommitLineData
ac718b69 1/*
c7de7dec 2 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
ac718b69 3 *
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
ac718b69 10#include <linux/signal.h>
11#include <linux/slab.h>
12#include <linux/module.h>
ac718b69 13#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>
ebc2ec48 21#include <linux/list.h>
5bd23881 22#include <linux/ip.h>
23#include <linux/ipv6.h>
6128d1bb 24#include <net/ip6_checksum.h>
4c4a6b1b 25#include <uapi/linux/mdio.h>
26#include <linux/mdio.h>
d9a28c5b 27#include <linux/usb/cdc.h>
ac718b69 28
29/* Version Information */
d823ab68 30#define DRIVER_VERSION "v1.08.0 (2015/01/13)"
ac718b69 31#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
44d942a9 32#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
ac718b69 33#define MODULENAME "r8152"
34
35#define R8152_PHY_ID 32
36
37#define PLA_IDR 0xc000
38#define PLA_RCR 0xc010
39#define PLA_RMS 0xc016
40#define PLA_RXFIFO_CTRL0 0xc0a0
41#define PLA_RXFIFO_CTRL1 0xc0a4
42#define PLA_RXFIFO_CTRL2 0xc0a8
43#define PLA_FMC 0xc0b4
44#define PLA_CFG_WOL 0xc0b6
43779f8d 45#define PLA_TEREDO_CFG 0xc0bc
ac718b69 46#define PLA_MAR 0xcd00
43779f8d 47#define PLA_BACKUP 0xd000
ac718b69 48#define PAL_BDC_CR 0xd1a0
43779f8d 49#define PLA_TEREDO_TIMER 0xd2cc
50#define PLA_REALWOW_TIMER 0xd2e8
ac718b69 51#define PLA_LEDSEL 0xdd90
52#define PLA_LED_FEATURE 0xdd92
53#define PLA_PHYAR 0xde00
43779f8d 54#define PLA_BOOT_CTRL 0xe004
ac718b69 55#define PLA_GPHY_INTR_IMR 0xe022
56#define PLA_EEE_CR 0xe040
57#define PLA_EEEP_CR 0xe080
58#define PLA_MAC_PWR_CTRL 0xe0c0
43779f8d 59#define PLA_MAC_PWR_CTRL2 0xe0ca
60#define PLA_MAC_PWR_CTRL3 0xe0cc
61#define PLA_MAC_PWR_CTRL4 0xe0ce
62#define PLA_WDT6_CTRL 0xe428
ac718b69 63#define PLA_TCR0 0xe610
64#define PLA_TCR1 0xe612
69b4b7a4 65#define PLA_MTPS 0xe615
ac718b69 66#define PLA_TXFIFO_CTRL 0xe618
4f1d4d54 67#define PLA_RSTTALLY 0xe800
ac718b69 68#define PLA_CR 0xe813
69#define PLA_CRWECR 0xe81c
21ff2e89 70#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
71#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
ac718b69 72#define PLA_CONFIG5 0xe822
73#define PLA_PHY_PWR 0xe84c
74#define PLA_OOB_CTRL 0xe84f
75#define PLA_CPCR 0xe854
76#define PLA_MISC_0 0xe858
77#define PLA_MISC_1 0xe85a
78#define PLA_OCP_GPHY_BASE 0xe86c
4f1d4d54 79#define PLA_TALLYCNT 0xe890
ac718b69 80#define PLA_SFF_STS_7 0xe8de
81#define PLA_PHYSTATUS 0xe908
82#define PLA_BP_BA 0xfc26
83#define PLA_BP_0 0xfc28
84#define PLA_BP_1 0xfc2a
85#define PLA_BP_2 0xfc2c
86#define PLA_BP_3 0xfc2e
87#define PLA_BP_4 0xfc30
88#define PLA_BP_5 0xfc32
89#define PLA_BP_6 0xfc34
90#define PLA_BP_7 0xfc36
43779f8d 91#define PLA_BP_EN 0xfc38
ac718b69 92
43779f8d 93#define USB_U2P3_CTRL 0xb460
ac718b69 94#define USB_DEV_STAT 0xb808
95#define USB_USB_CTRL 0xd406
96#define USB_PHY_CTRL 0xd408
97#define USB_TX_AGG 0xd40a
98#define USB_RX_BUF_TH 0xd40c
99#define USB_USB_TIMER 0xd428
43779f8d 100#define USB_RX_EARLY_AGG 0xd42c
ac718b69 101#define USB_PM_CTRL_STATUS 0xd432
102#define USB_TX_DMA 0xd434
43779f8d 103#define USB_TOLERANCE 0xd490
104#define USB_LPM_CTRL 0xd41a
ac718b69 105#define USB_UPS_CTRL 0xd800
43779f8d 106#define USB_MISC_0 0xd81a
107#define USB_POWER_CUT 0xd80a
108#define USB_AFE_CTRL2 0xd824
109#define USB_WDT11_CTRL 0xe43c
ac718b69 110#define USB_BP_BA 0xfc26
111#define USB_BP_0 0xfc28
112#define USB_BP_1 0xfc2a
113#define USB_BP_2 0xfc2c
114#define USB_BP_3 0xfc2e
115#define USB_BP_4 0xfc30
116#define USB_BP_5 0xfc32
117#define USB_BP_6 0xfc34
118#define USB_BP_7 0xfc36
43779f8d 119#define USB_BP_EN 0xfc38
ac718b69 120
121/* OCP Registers */
122#define OCP_ALDPS_CONFIG 0x2010
123#define OCP_EEE_CONFIG1 0x2080
124#define OCP_EEE_CONFIG2 0x2092
125#define OCP_EEE_CONFIG3 0x2094
ac244d3e 126#define OCP_BASE_MII 0xa400
ac718b69 127#define OCP_EEE_AR 0xa41a
128#define OCP_EEE_DATA 0xa41c
43779f8d 129#define OCP_PHY_STATUS 0xa420
130#define OCP_POWER_CFG 0xa430
131#define OCP_EEE_CFG 0xa432
132#define OCP_SRAM_ADDR 0xa436
133#define OCP_SRAM_DATA 0xa438
134#define OCP_DOWN_SPEED 0xa442
df35d283 135#define OCP_EEE_ABLE 0xa5c4
4c4a6b1b 136#define OCP_EEE_ADV 0xa5d0
df35d283 137#define OCP_EEE_LPABLE 0xa5d2
43779f8d 138#define OCP_ADC_CFG 0xbc06
139
140/* SRAM Register */
141#define SRAM_LPF_CFG 0x8012
142#define SRAM_10M_AMP1 0x8080
143#define SRAM_10M_AMP2 0x8082
144#define SRAM_IMPEDANCE 0x8084
ac718b69 145
146/* PLA_RCR */
147#define RCR_AAP 0x00000001
148#define RCR_APM 0x00000002
149#define RCR_AM 0x00000004
150#define RCR_AB 0x00000008
151#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
152
153/* PLA_RXFIFO_CTRL0 */
154#define RXFIFO_THR1_NORMAL 0x00080002
155#define RXFIFO_THR1_OOB 0x01800003
156
157/* PLA_RXFIFO_CTRL1 */
158#define RXFIFO_THR2_FULL 0x00000060
159#define RXFIFO_THR2_HIGH 0x00000038
160#define RXFIFO_THR2_OOB 0x0000004a
43779f8d 161#define RXFIFO_THR2_NORMAL 0x00a0
ac718b69 162
163/* PLA_RXFIFO_CTRL2 */
164#define RXFIFO_THR3_FULL 0x00000078
165#define RXFIFO_THR3_HIGH 0x00000048
166#define RXFIFO_THR3_OOB 0x0000005a
43779f8d 167#define RXFIFO_THR3_NORMAL 0x0110
ac718b69 168
169/* PLA_TXFIFO_CTRL */
170#define TXFIFO_THR_NORMAL 0x00400008
43779f8d 171#define TXFIFO_THR_NORMAL2 0x01000008
ac718b69 172
173/* PLA_FMC */
174#define FMC_FCR_MCU_EN 0x0001
175
176/* PLA_EEEP_CR */
177#define EEEP_CR_EEEP_TX 0x0002
178
43779f8d 179/* PLA_WDT6_CTRL */
180#define WDT6_SET_MODE 0x0010
181
ac718b69 182/* PLA_TCR0 */
183#define TCR0_TX_EMPTY 0x0800
184#define TCR0_AUTO_FIFO 0x0080
185
186/* PLA_TCR1 */
187#define VERSION_MASK 0x7cf0
188
69b4b7a4 189/* PLA_MTPS */
190#define MTPS_JUMBO (12 * 1024 / 64)
191#define MTPS_DEFAULT (6 * 1024 / 64)
192
4f1d4d54 193/* PLA_RSTTALLY */
194#define TALLY_RESET 0x0001
195
ac718b69 196/* PLA_CR */
197#define CR_RST 0x10
198#define CR_RE 0x08
199#define CR_TE 0x04
200
201/* PLA_CRWECR */
202#define CRWECR_NORAML 0x00
203#define CRWECR_CONFIG 0xc0
204
205/* PLA_OOB_CTRL */
206#define NOW_IS_OOB 0x80
207#define TXFIFO_EMPTY 0x20
208#define RXFIFO_EMPTY 0x10
209#define LINK_LIST_READY 0x02
210#define DIS_MCU_CLROOB 0x01
211#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
212
213/* PLA_MISC_1 */
214#define RXDY_GATED_EN 0x0008
215
216/* PLA_SFF_STS_7 */
217#define RE_INIT_LL 0x8000
218#define MCU_BORW_EN 0x4000
219
220/* PLA_CPCR */
221#define CPCR_RX_VLAN 0x0040
222
223/* PLA_CFG_WOL */
224#define MAGIC_EN 0x0001
225
43779f8d 226/* PLA_TEREDO_CFG */
227#define TEREDO_SEL 0x8000
228#define TEREDO_WAKE_MASK 0x7f00
229#define TEREDO_RS_EVENT_MASK 0x00fe
230#define OOB_TEREDO_EN 0x0001
231
ac718b69 232/* PAL_BDC_CR */
233#define ALDPS_PROXY_MODE 0x0001
234
21ff2e89 235/* PLA_CONFIG34 */
236#define LINK_ON_WAKE_EN 0x0010
237#define LINK_OFF_WAKE_EN 0x0008
238
ac718b69 239/* PLA_CONFIG5 */
21ff2e89 240#define BWF_EN 0x0040
241#define MWF_EN 0x0020
242#define UWF_EN 0x0010
ac718b69 243#define LAN_WAKE_EN 0x0002
244
245/* PLA_LED_FEATURE */
246#define LED_MODE_MASK 0x0700
247
248/* PLA_PHY_PWR */
249#define TX_10M_IDLE_EN 0x0080
250#define PFM_PWM_SWITCH 0x0040
251
252/* PLA_MAC_PWR_CTRL */
253#define D3_CLK_GATED_EN 0x00004000
254#define MCU_CLK_RATIO 0x07010f07
255#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
43779f8d 256#define ALDPS_SPDWN_RATIO 0x0f87
257
258/* PLA_MAC_PWR_CTRL2 */
259#define EEE_SPDWN_RATIO 0x8007
260
261/* PLA_MAC_PWR_CTRL3 */
262#define PKT_AVAIL_SPDWN_EN 0x0100
263#define SUSPEND_SPDWN_EN 0x0004
264#define U1U2_SPDWN_EN 0x0002
265#define L1_SPDWN_EN 0x0001
266
267/* PLA_MAC_PWR_CTRL4 */
268#define PWRSAVE_SPDWN_EN 0x1000
269#define RXDV_SPDWN_EN 0x0800
270#define TX10MIDLE_EN 0x0100
271#define TP100_SPDWN_EN 0x0020
272#define TP500_SPDWN_EN 0x0010
273#define TP1000_SPDWN_EN 0x0008
274#define EEE_SPDWN_EN 0x0001
ac718b69 275
276/* PLA_GPHY_INTR_IMR */
277#define GPHY_STS_MSK 0x0001
278#define SPEED_DOWN_MSK 0x0002
279#define SPDWN_RXDV_MSK 0x0004
280#define SPDWN_LINKCHG_MSK 0x0008
281
282/* PLA_PHYAR */
283#define PHYAR_FLAG 0x80000000
284
285/* PLA_EEE_CR */
286#define EEE_RX_EN 0x0001
287#define EEE_TX_EN 0x0002
288
43779f8d 289/* PLA_BOOT_CTRL */
290#define AUTOLOAD_DONE 0x0002
291
ac718b69 292/* USB_DEV_STAT */
293#define STAT_SPEED_MASK 0x0006
294#define STAT_SPEED_HIGH 0x0000
a3cc465d 295#define STAT_SPEED_FULL 0x0002
ac718b69 296
297/* USB_TX_AGG */
298#define TX_AGG_MAX_THRESHOLD 0x03
299
300/* USB_RX_BUF_TH */
43779f8d 301#define RX_THR_SUPPER 0x0c350180
8e1f51bd 302#define RX_THR_HIGH 0x7a120180
43779f8d 303#define RX_THR_SLOW 0xffff0180
ac718b69 304
305/* USB_TX_DMA */
306#define TEST_MODE_DISABLE 0x00000001
307#define TX_SIZE_ADJUST1 0x00000100
308
309/* USB_UPS_CTRL */
310#define POWER_CUT 0x0100
311
312/* USB_PM_CTRL_STATUS */
8e1f51bd 313#define RESUME_INDICATE 0x0001
ac718b69 314
315/* USB_USB_CTRL */
316#define RX_AGG_DISABLE 0x0010
317
43779f8d 318/* USB_U2P3_CTRL */
319#define U2P3_ENABLE 0x0001
320
321/* USB_POWER_CUT */
322#define PWR_EN 0x0001
323#define PHASE2_EN 0x0008
324
325/* USB_MISC_0 */
326#define PCUT_STATUS 0x0001
327
328/* USB_RX_EARLY_AGG */
329#define EARLY_AGG_SUPPER 0x0e832981
330#define EARLY_AGG_HIGH 0x0e837a12
331#define EARLY_AGG_SLOW 0x0e83ffff
332
333/* USB_WDT11_CTRL */
334#define TIMER11_EN 0x0001
335
336/* USB_LPM_CTRL */
337#define LPM_TIMER_MASK 0x0c
338#define LPM_TIMER_500MS 0x04 /* 500 ms */
339#define LPM_TIMER_500US 0x0c /* 500 us */
340
341/* USB_AFE_CTRL2 */
342#define SEN_VAL_MASK 0xf800
343#define SEN_VAL_NORMAL 0xa000
344#define SEL_RXIDLE 0x0100
345
ac718b69 346/* OCP_ALDPS_CONFIG */
347#define ENPWRSAVE 0x8000
348#define ENPDNPS 0x0200
349#define LINKENA 0x0100
350#define DIS_SDSAVE 0x0010
351
43779f8d 352/* OCP_PHY_STATUS */
353#define PHY_STAT_MASK 0x0007
354#define PHY_STAT_LAN_ON 3
355#define PHY_STAT_PWRDN 5
356
357/* OCP_POWER_CFG */
358#define EEE_CLKDIV_EN 0x8000
359#define EN_ALDPS 0x0004
360#define EN_10M_PLLOFF 0x0001
361
ac718b69 362/* OCP_EEE_CONFIG1 */
363#define RG_TXLPI_MSK_HFDUP 0x8000
364#define RG_MATCLR_EN 0x4000
365#define EEE_10_CAP 0x2000
366#define EEE_NWAY_EN 0x1000
367#define TX_QUIET_EN 0x0200
368#define RX_QUIET_EN 0x0100
d24f6134 369#define sd_rise_time_mask 0x0070
4c4a6b1b 370#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
ac718b69 371#define RG_RXLPI_MSK_HFDUP 0x0008
372#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
373
374/* OCP_EEE_CONFIG2 */
375#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
376#define RG_DACQUIET_EN 0x0400
377#define RG_LDVQUIET_EN 0x0200
378#define RG_CKRSEL 0x0020
379#define RG_EEEPRG_EN 0x0010
380
381/* OCP_EEE_CONFIG3 */
d24f6134 382#define fast_snr_mask 0xff80
4c4a6b1b 383#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
ac718b69 384#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
385#define MSK_PH 0x0006 /* bit 0 ~ 3 */
386
387/* OCP_EEE_AR */
388/* bit[15:14] function */
389#define FUN_ADDR 0x0000
390#define FUN_DATA 0x4000
391/* bit[4:0] device addr */
ac718b69 392
43779f8d 393/* OCP_EEE_CFG */
394#define CTAP_SHORT_EN 0x0040
395#define EEE10_EN 0x0010
396
397/* OCP_DOWN_SPEED */
398#define EN_10M_BGOFF 0x0080
399
43779f8d 400/* OCP_ADC_CFG */
401#define CKADSEL_L 0x0100
402#define ADC_EN 0x0080
403#define EN_EMI_L 0x0040
404
405/* SRAM_LPF_CFG */
406#define LPF_AUTO_TUNE 0x8000
407
408/* SRAM_10M_AMP1 */
409#define GDAC_IB_UPALL 0x0008
410
411/* SRAM_10M_AMP2 */
412#define AMP_DN 0x0200
413
414/* SRAM_IMPEDANCE */
415#define RX_DRIVING_MASK 0x6000
416
ac718b69 417enum rtl_register_content {
43779f8d 418 _1000bps = 0x10,
ac718b69 419 _100bps = 0x08,
420 _10bps = 0x04,
421 LINK_STATUS = 0x02,
422 FULL_DUP = 0x01,
423};
424
1764bcd9 425#define RTL8152_MAX_TX 4
ebc2ec48 426#define RTL8152_MAX_RX 10
40a82917 427#define INTBUFSIZE 2
8e1f51bd 428#define CRC_SIZE 4
429#define TX_ALIGN 4
430#define RX_ALIGN 8
40a82917 431
432#define INTR_LINK 0x0004
ebc2ec48 433
ac718b69 434#define RTL8152_REQT_READ 0xc0
435#define RTL8152_REQT_WRITE 0x40
436#define RTL8152_REQ_GET_REGS 0x05
437#define RTL8152_REQ_SET_REGS 0x05
438
439#define BYTE_EN_DWORD 0xff
440#define BYTE_EN_WORD 0x33
441#define BYTE_EN_BYTE 0x11
442#define BYTE_EN_SIX_BYTES 0x3f
443#define BYTE_EN_START_MASK 0x0f
444#define BYTE_EN_END_MASK 0xf0
445
69b4b7a4 446#define RTL8153_MAX_PACKET 9216 /* 9K */
447#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
ac718b69 448#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
69b4b7a4 449#define RTL8153_RMS RTL8153_MAX_PACKET
b8125404 450#define RTL8152_TX_TIMEOUT (5 * HZ)
d823ab68 451#define RTL8152_NAPI_WEIGHT 64
ac718b69 452
453/* rtl8152 flags */
454enum rtl8152_flags {
455 RTL8152_UNPLUG = 0,
ac718b69 456 RTL8152_SET_RX_MODE,
40a82917 457 WORK_ENABLE,
458 RTL8152_LINK_CHG,
9a4be1bd 459 SELECTIVE_SUSPEND,
aa66a5f1 460 PHY_RESET,
d823ab68 461 SCHEDULE_NAPI,
ac718b69 462};
463
464/* Define these values to match your device */
465#define VENDOR_ID_REALTEK 0x0bda
43779f8d 466#define VENDOR_ID_SAMSUNG 0x04e8
ac718b69 467
468#define MCU_TYPE_PLA 0x0100
469#define MCU_TYPE_USB 0x0000
470
4f1d4d54 471struct tally_counter {
472 __le64 tx_packets;
473 __le64 rx_packets;
474 __le64 tx_errors;
475 __le32 rx_errors;
476 __le16 rx_missed;
477 __le16 align_errors;
478 __le32 tx_one_collision;
479 __le32 tx_multi_collision;
480 __le64 rx_unicast;
481 __le64 rx_broadcast;
482 __le32 rx_multicast;
483 __le16 tx_aborted;
f37119c5 484 __le16 tx_underrun;
4f1d4d54 485};
486
ac718b69 487struct rx_desc {
500b6d7e 488 __le32 opts1;
ac718b69 489#define RX_LEN_MASK 0x7fff
565cab0a 490
500b6d7e 491 __le32 opts2;
565cab0a 492#define RD_UDP_CS (1 << 23)
493#define RD_TCP_CS (1 << 22)
6128d1bb 494#define RD_IPV6_CS (1 << 20)
565cab0a 495#define RD_IPV4_CS (1 << 19)
496
500b6d7e 497 __le32 opts3;
565cab0a 498#define IPF (1 << 23) /* IP checksum fail */
499#define UDPF (1 << 22) /* UDP checksum fail */
500#define TCPF (1 << 21) /* TCP checksum fail */
c5554298 501#define RX_VLAN_TAG (1 << 16)
565cab0a 502
500b6d7e 503 __le32 opts4;
504 __le32 opts5;
505 __le32 opts6;
ac718b69 506};
507
508struct tx_desc {
500b6d7e 509 __le32 opts1;
ac718b69 510#define TX_FS (1 << 31) /* First segment of a packet */
511#define TX_LS (1 << 30) /* Final segment of a packet */
60c89071 512#define GTSENDV4 (1 << 28)
6128d1bb 513#define GTSENDV6 (1 << 27)
60c89071 514#define GTTCPHO_SHIFT 18
6128d1bb 515#define GTTCPHO_MAX 0x7fU
60c89071 516#define TX_LEN_MAX 0x3ffffU
5bd23881 517
500b6d7e 518 __le32 opts2;
5bd23881 519#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
520#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
521#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
522#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
60c89071 523#define MSS_SHIFT 17
524#define MSS_MAX 0x7ffU
525#define TCPHO_SHIFT 17
6128d1bb 526#define TCPHO_MAX 0x7ffU
c5554298 527#define TX_VLAN_TAG (1 << 16)
ac718b69 528};
529
dff4e8ad 530struct r8152;
531
ebc2ec48 532struct rx_agg {
533 struct list_head list;
534 struct urb *urb;
dff4e8ad 535 struct r8152 *context;
ebc2ec48 536 void *buffer;
537 void *head;
538};
539
540struct tx_agg {
541 struct list_head list;
542 struct urb *urb;
dff4e8ad 543 struct r8152 *context;
ebc2ec48 544 void *buffer;
545 void *head;
546 u32 skb_num;
547 u32 skb_len;
548};
549
ac718b69 550struct r8152 {
551 unsigned long flags;
552 struct usb_device *udev;
d823ab68 553 struct napi_struct napi;
40a82917 554 struct usb_interface *intf;
ac718b69 555 struct net_device *netdev;
40a82917 556 struct urb *intr_urb;
ebc2ec48 557 struct tx_agg tx_info[RTL8152_MAX_TX];
558 struct rx_agg rx_info[RTL8152_MAX_RX];
559 struct list_head rx_done, tx_free;
d823ab68 560 struct sk_buff_head tx_queue, rx_queue;
ebc2ec48 561 spinlock_t rx_lock, tx_lock;
ac718b69 562 struct delayed_work schedule;
563 struct mii_if_info mii;
b5403273 564 struct mutex control; /* use for hw setting */
c81229c9 565
566 struct rtl_ops {
567 void (*init)(struct r8152 *);
568 int (*enable)(struct r8152 *);
569 void (*disable)(struct r8152 *);
7e9da481 570 void (*up)(struct r8152 *);
c81229c9 571 void (*down)(struct r8152 *);
572 void (*unload)(struct r8152 *);
df35d283 573 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
574 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
c81229c9 575 } rtl_ops;
576
40a82917 577 int intr_interval;
21ff2e89 578 u32 saved_wolopts;
ac718b69 579 u32 msg_enable;
dd1b119c 580 u32 tx_qlen;
ac718b69 581 u16 ocp_base;
40a82917 582 u8 *intr_buff;
ac718b69 583 u8 version;
584 u8 speed;
585};
586
587enum rtl_version {
588 RTL_VER_UNKNOWN = 0,
589 RTL_VER_01,
43779f8d 590 RTL_VER_02,
591 RTL_VER_03,
592 RTL_VER_04,
593 RTL_VER_05,
594 RTL_VER_MAX
ac718b69 595};
596
60c89071 597enum tx_csum_stat {
598 TX_CSUM_SUCCESS = 0,
599 TX_CSUM_TSO,
600 TX_CSUM_NONE
601};
602
ac718b69 603/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
604 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
605 */
606static const int multicast_filter_limit = 32;
52aec126 607static unsigned int agg_buf_sz = 16384;
ac718b69 608
52aec126 609#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
60c89071 610 VLAN_ETH_HLEN - VLAN_HLEN)
611
ac718b69 612static
613int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
614{
31787f53 615 int ret;
616 void *tmp;
617
618 tmp = kmalloc(size, GFP_KERNEL);
619 if (!tmp)
620 return -ENOMEM;
621
622 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
b209af99 623 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
624 value, index, tmp, size, 500);
31787f53 625
626 memcpy(data, tmp, size);
627 kfree(tmp);
628
629 return ret;
ac718b69 630}
631
632static
633int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
634{
31787f53 635 int ret;
636 void *tmp;
637
c4438f03 638 tmp = kmemdup(data, size, GFP_KERNEL);
31787f53 639 if (!tmp)
640 return -ENOMEM;
641
31787f53 642 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
b209af99 643 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
644 value, index, tmp, size, 500);
31787f53 645
646 kfree(tmp);
db8515ef 647
31787f53 648 return ret;
ac718b69 649}
650
651static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
b209af99 652 void *data, u16 type)
ac718b69 653{
45f4a19f 654 u16 limit = 64;
655 int ret = 0;
ac718b69 656
657 if (test_bit(RTL8152_UNPLUG, &tp->flags))
658 return -ENODEV;
659
660 /* both size and indix must be 4 bytes align */
661 if ((size & 3) || !size || (index & 3) || !data)
662 return -EPERM;
663
664 if ((u32)index + (u32)size > 0xffff)
665 return -EPERM;
666
667 while (size) {
668 if (size > limit) {
669 ret = get_registers(tp, index, type, limit, data);
670 if (ret < 0)
671 break;
672
673 index += limit;
674 data += limit;
675 size -= limit;
676 } else {
677 ret = get_registers(tp, index, type, size, data);
678 if (ret < 0)
679 break;
680
681 index += size;
682 data += size;
683 size = 0;
684 break;
685 }
686 }
687
67610496 688 if (ret == -ENODEV)
689 set_bit(RTL8152_UNPLUG, &tp->flags);
690
ac718b69 691 return ret;
692}
693
694static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
b209af99 695 u16 size, void *data, u16 type)
ac718b69 696{
45f4a19f 697 int ret;
698 u16 byteen_start, byteen_end, byen;
699 u16 limit = 512;
ac718b69 700
701 if (test_bit(RTL8152_UNPLUG, &tp->flags))
702 return -ENODEV;
703
704 /* both size and indix must be 4 bytes align */
705 if ((size & 3) || !size || (index & 3) || !data)
706 return -EPERM;
707
708 if ((u32)index + (u32)size > 0xffff)
709 return -EPERM;
710
711 byteen_start = byteen & BYTE_EN_START_MASK;
712 byteen_end = byteen & BYTE_EN_END_MASK;
713
714 byen = byteen_start | (byteen_start << 4);
715 ret = set_registers(tp, index, type | byen, 4, data);
716 if (ret < 0)
717 goto error1;
718
719 index += 4;
720 data += 4;
721 size -= 4;
722
723 if (size) {
724 size -= 4;
725
726 while (size) {
727 if (size > limit) {
728 ret = set_registers(tp, index,
b209af99 729 type | BYTE_EN_DWORD,
730 limit, data);
ac718b69 731 if (ret < 0)
732 goto error1;
733
734 index += limit;
735 data += limit;
736 size -= limit;
737 } else {
738 ret = set_registers(tp, index,
b209af99 739 type | BYTE_EN_DWORD,
740 size, data);
ac718b69 741 if (ret < 0)
742 goto error1;
743
744 index += size;
745 data += size;
746 size = 0;
747 break;
748 }
749 }
750
751 byen = byteen_end | (byteen_end >> 4);
752 ret = set_registers(tp, index, type | byen, 4, data);
753 if (ret < 0)
754 goto error1;
755 }
756
757error1:
67610496 758 if (ret == -ENODEV)
759 set_bit(RTL8152_UNPLUG, &tp->flags);
760
ac718b69 761 return ret;
762}
763
764static inline
765int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
766{
767 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
768}
769
770static inline
771int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
772{
773 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
774}
775
776static inline
777int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
778{
779 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
780}
781
782static inline
783int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
784{
785 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
786}
787
788static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
789{
c8826de8 790 __le32 data;
ac718b69 791
c8826de8 792 generic_ocp_read(tp, index, sizeof(data), &data, type);
ac718b69 793
794 return __le32_to_cpu(data);
795}
796
797static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
798{
c8826de8 799 __le32 tmp = __cpu_to_le32(data);
800
801 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
ac718b69 802}
803
804static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
805{
806 u32 data;
c8826de8 807 __le32 tmp;
ac718b69 808 u8 shift = index & 2;
809
810 index &= ~3;
811
c8826de8 812 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
ac718b69 813
c8826de8 814 data = __le32_to_cpu(tmp);
ac718b69 815 data >>= (shift * 8);
816 data &= 0xffff;
817
818 return (u16)data;
819}
820
821static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
822{
c8826de8 823 u32 mask = 0xffff;
824 __le32 tmp;
ac718b69 825 u16 byen = BYTE_EN_WORD;
826 u8 shift = index & 2;
827
828 data &= mask;
829
830 if (index & 2) {
831 byen <<= shift;
832 mask <<= (shift * 8);
833 data <<= (shift * 8);
834 index &= ~3;
835 }
836
c8826de8 837 tmp = __cpu_to_le32(data);
ac718b69 838
c8826de8 839 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
ac718b69 840}
841
842static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
843{
844 u32 data;
c8826de8 845 __le32 tmp;
ac718b69 846 u8 shift = index & 3;
847
848 index &= ~3;
849
c8826de8 850 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
ac718b69 851
c8826de8 852 data = __le32_to_cpu(tmp);
ac718b69 853 data >>= (shift * 8);
854 data &= 0xff;
855
856 return (u8)data;
857}
858
859static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
860{
c8826de8 861 u32 mask = 0xff;
862 __le32 tmp;
ac718b69 863 u16 byen = BYTE_EN_BYTE;
864 u8 shift = index & 3;
865
866 data &= mask;
867
868 if (index & 3) {
869 byen <<= shift;
870 mask <<= (shift * 8);
871 data <<= (shift * 8);
872 index &= ~3;
873 }
874
c8826de8 875 tmp = __cpu_to_le32(data);
ac718b69 876
c8826de8 877 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
ac718b69 878}
879
ac244d3e 880static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
e3fe0b1a 881{
882 u16 ocp_base, ocp_index;
883
884 ocp_base = addr & 0xf000;
885 if (ocp_base != tp->ocp_base) {
886 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
887 tp->ocp_base = ocp_base;
888 }
889
890 ocp_index = (addr & 0x0fff) | 0xb000;
ac244d3e 891 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
e3fe0b1a 892}
893
ac244d3e 894static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
ac718b69 895{
ac244d3e 896 u16 ocp_base, ocp_index;
ac718b69 897
ac244d3e 898 ocp_base = addr & 0xf000;
899 if (ocp_base != tp->ocp_base) {
900 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
901 tp->ocp_base = ocp_base;
ac718b69 902 }
ac244d3e 903
904 ocp_index = (addr & 0x0fff) | 0xb000;
905 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
ac718b69 906}
907
ac244d3e 908static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
ac718b69 909{
ac244d3e 910 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
911}
ac718b69 912
ac244d3e 913static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
914{
915 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
ac718b69 916}
917
43779f8d 918static void sram_write(struct r8152 *tp, u16 addr, u16 data)
919{
920 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
921 ocp_reg_write(tp, OCP_SRAM_DATA, data);
922}
923
ac718b69 924static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
925{
926 struct r8152 *tp = netdev_priv(netdev);
9a4be1bd 927 int ret;
ac718b69 928
6871438c 929 if (test_bit(RTL8152_UNPLUG, &tp->flags))
930 return -ENODEV;
931
ac718b69 932 if (phy_id != R8152_PHY_ID)
933 return -EINVAL;
934
9a4be1bd 935 ret = r8152_mdio_read(tp, reg);
936
9a4be1bd 937 return ret;
ac718b69 938}
939
940static
941void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
942{
943 struct r8152 *tp = netdev_priv(netdev);
944
6871438c 945 if (test_bit(RTL8152_UNPLUG, &tp->flags))
946 return;
947
ac718b69 948 if (phy_id != R8152_PHY_ID)
949 return;
950
951 r8152_mdio_write(tp, reg, val);
952}
953
b209af99 954static int
955r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
ebc2ec48 956
8ba789ab 957static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
958{
959 struct r8152 *tp = netdev_priv(netdev);
960 struct sockaddr *addr = p;
ea6a7112 961 int ret = -EADDRNOTAVAIL;
8ba789ab 962
963 if (!is_valid_ether_addr(addr->sa_data))
ea6a7112 964 goto out1;
965
966 ret = usb_autopm_get_interface(tp->intf);
967 if (ret < 0)
968 goto out1;
8ba789ab 969
b5403273 970 mutex_lock(&tp->control);
971
8ba789ab 972 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
973
974 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
975 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
976 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
977
b5403273 978 mutex_unlock(&tp->control);
979
ea6a7112 980 usb_autopm_put_interface(tp->intf);
981out1:
982 return ret;
8ba789ab 983}
984
179bb6d7 985static int set_ethernet_addr(struct r8152 *tp)
ac718b69 986{
987 struct net_device *dev = tp->netdev;
179bb6d7 988 struct sockaddr sa;
8a91c824 989 int ret;
ac718b69 990
8a91c824 991 if (tp->version == RTL_VER_01)
179bb6d7 992 ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
8a91c824 993 else
179bb6d7 994 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
8a91c824 995
996 if (ret < 0) {
179bb6d7 997 netif_err(tp, probe, dev, "Get ether addr fail\n");
998 } else if (!is_valid_ether_addr(sa.sa_data)) {
999 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1000 sa.sa_data);
1001 eth_hw_addr_random(dev);
1002 ether_addr_copy(sa.sa_data, dev->dev_addr);
1003 ret = rtl8152_set_mac_address(dev, &sa);
1004 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1005 sa.sa_data);
8a91c824 1006 } else {
179bb6d7 1007 if (tp->version == RTL_VER_01)
1008 ether_addr_copy(dev->dev_addr, sa.sa_data);
1009 else
1010 ret = rtl8152_set_mac_address(dev, &sa);
ac718b69 1011 }
179bb6d7 1012
1013 return ret;
ac718b69 1014}
1015
ac718b69 1016static void read_bulk_callback(struct urb *urb)
1017{
ac718b69 1018 struct net_device *netdev;
ac718b69 1019 int status = urb->status;
ebc2ec48 1020 struct rx_agg *agg;
1021 struct r8152 *tp;
ac718b69 1022
ebc2ec48 1023 agg = urb->context;
1024 if (!agg)
1025 return;
1026
1027 tp = agg->context;
ac718b69 1028 if (!tp)
1029 return;
ebc2ec48 1030
ac718b69 1031 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1032 return;
ebc2ec48 1033
1034 if (!test_bit(WORK_ENABLE, &tp->flags))
1035 return;
1036
ac718b69 1037 netdev = tp->netdev;
7559fb2f 1038
1039 /* When link down, the driver would cancel all bulks. */
1040 /* This avoid the re-submitting bulk */
ebc2ec48 1041 if (!netif_carrier_ok(netdev))
ac718b69 1042 return;
1043
9a4be1bd 1044 usb_mark_last_busy(tp->udev);
1045
ac718b69 1046 switch (status) {
1047 case 0:
ebc2ec48 1048 if (urb->actual_length < ETH_ZLEN)
1049 break;
1050
2685d410 1051 spin_lock(&tp->rx_lock);
ebc2ec48 1052 list_add_tail(&agg->list, &tp->rx_done);
2685d410 1053 spin_unlock(&tp->rx_lock);
d823ab68 1054 napi_schedule(&tp->napi);
ebc2ec48 1055 return;
ac718b69 1056 case -ESHUTDOWN:
1057 set_bit(RTL8152_UNPLUG, &tp->flags);
1058 netif_device_detach(tp->netdev);
ebc2ec48 1059 return;
ac718b69 1060 case -ENOENT:
1061 return; /* the urb is in unlink state */
1062 case -ETIME:
4a8deae2
HW
1063 if (net_ratelimit())
1064 netdev_warn(netdev, "maybe reset is needed?\n");
ebc2ec48 1065 break;
ac718b69 1066 default:
4a8deae2
HW
1067 if (net_ratelimit())
1068 netdev_warn(netdev, "Rx status %d\n", status);
ebc2ec48 1069 break;
ac718b69 1070 }
1071
a0fccd48 1072 r8152_submit_rx(tp, agg, GFP_ATOMIC);
ac718b69 1073}
1074
ebc2ec48 1075static void write_bulk_callback(struct urb *urb)
ac718b69 1076{
ebc2ec48 1077 struct net_device_stats *stats;
d104eafa 1078 struct net_device *netdev;
ebc2ec48 1079 struct tx_agg *agg;
ac718b69 1080 struct r8152 *tp;
ebc2ec48 1081 int status = urb->status;
ac718b69 1082
ebc2ec48 1083 agg = urb->context;
1084 if (!agg)
ac718b69 1085 return;
1086
ebc2ec48 1087 tp = agg->context;
1088 if (!tp)
1089 return;
1090
d104eafa 1091 netdev = tp->netdev;
05e0f1aa 1092 stats = &netdev->stats;
ebc2ec48 1093 if (status) {
4a8deae2 1094 if (net_ratelimit())
d104eafa 1095 netdev_warn(netdev, "Tx status %d\n", status);
ebc2ec48 1096 stats->tx_errors += agg->skb_num;
ac718b69 1097 } else {
ebc2ec48 1098 stats->tx_packets += agg->skb_num;
1099 stats->tx_bytes += agg->skb_len;
ac718b69 1100 }
1101
2685d410 1102 spin_lock(&tp->tx_lock);
ebc2ec48 1103 list_add_tail(&agg->list, &tp->tx_free);
2685d410 1104 spin_unlock(&tp->tx_lock);
ebc2ec48 1105
9a4be1bd 1106 usb_autopm_put_interface_async(tp->intf);
1107
d104eafa 1108 if (!netif_carrier_ok(netdev))
ebc2ec48 1109 return;
1110
1111 if (!test_bit(WORK_ENABLE, &tp->flags))
1112 return;
1113
1114 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1115 return;
1116
1117 if (!skb_queue_empty(&tp->tx_queue))
d823ab68 1118 napi_schedule(&tp->napi);
ac718b69 1119}
1120
40a82917 1121static void intr_callback(struct urb *urb)
1122{
1123 struct r8152 *tp;
500b6d7e 1124 __le16 *d;
40a82917 1125 int status = urb->status;
1126 int res;
1127
1128 tp = urb->context;
1129 if (!tp)
1130 return;
1131
1132 if (!test_bit(WORK_ENABLE, &tp->flags))
1133 return;
1134
1135 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1136 return;
1137
1138 switch (status) {
1139 case 0: /* success */
1140 break;
1141 case -ECONNRESET: /* unlink */
1142 case -ESHUTDOWN:
1143 netif_device_detach(tp->netdev);
1144 case -ENOENT:
d59c876d 1145 case -EPROTO:
1146 netif_info(tp, intr, tp->netdev,
1147 "Stop submitting intr, status %d\n", status);
40a82917 1148 return;
1149 case -EOVERFLOW:
1150 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1151 goto resubmit;
1152 /* -EPIPE: should clear the halt */
1153 default:
1154 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1155 goto resubmit;
1156 }
1157
1158 d = urb->transfer_buffer;
1159 if (INTR_LINK & __le16_to_cpu(d[0])) {
1160 if (!(tp->speed & LINK_STATUS)) {
1161 set_bit(RTL8152_LINK_CHG, &tp->flags);
1162 schedule_delayed_work(&tp->schedule, 0);
1163 }
1164 } else {
1165 if (tp->speed & LINK_STATUS) {
1166 set_bit(RTL8152_LINK_CHG, &tp->flags);
1167 schedule_delayed_work(&tp->schedule, 0);
1168 }
1169 }
1170
1171resubmit:
1172 res = usb_submit_urb(urb, GFP_ATOMIC);
67610496 1173 if (res == -ENODEV) {
1174 set_bit(RTL8152_UNPLUG, &tp->flags);
40a82917 1175 netif_device_detach(tp->netdev);
67610496 1176 } else if (res) {
40a82917 1177 netif_err(tp, intr, tp->netdev,
4a8deae2 1178 "can't resubmit intr, status %d\n", res);
67610496 1179 }
40a82917 1180}
1181
ebc2ec48 1182static inline void *rx_agg_align(void *data)
1183{
8e1f51bd 1184 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
ebc2ec48 1185}
1186
1187static inline void *tx_agg_align(void *data)
1188{
8e1f51bd 1189 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
ebc2ec48 1190}
1191
1192static void free_all_mem(struct r8152 *tp)
1193{
1194 int i;
1195
1196 for (i = 0; i < RTL8152_MAX_RX; i++) {
9629e3c0 1197 usb_free_urb(tp->rx_info[i].urb);
1198 tp->rx_info[i].urb = NULL;
ebc2ec48 1199
9629e3c0 1200 kfree(tp->rx_info[i].buffer);
1201 tp->rx_info[i].buffer = NULL;
1202 tp->rx_info[i].head = NULL;
ebc2ec48 1203 }
1204
1205 for (i = 0; i < RTL8152_MAX_TX; i++) {
9629e3c0 1206 usb_free_urb(tp->tx_info[i].urb);
1207 tp->tx_info[i].urb = NULL;
ebc2ec48 1208
9629e3c0 1209 kfree(tp->tx_info[i].buffer);
1210 tp->tx_info[i].buffer = NULL;
1211 tp->tx_info[i].head = NULL;
ebc2ec48 1212 }
40a82917 1213
9629e3c0 1214 usb_free_urb(tp->intr_urb);
1215 tp->intr_urb = NULL;
40a82917 1216
9629e3c0 1217 kfree(tp->intr_buff);
1218 tp->intr_buff = NULL;
ebc2ec48 1219}
1220
1221static int alloc_all_mem(struct r8152 *tp)
1222{
1223 struct net_device *netdev = tp->netdev;
40a82917 1224 struct usb_interface *intf = tp->intf;
1225 struct usb_host_interface *alt = intf->cur_altsetting;
1226 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
ebc2ec48 1227 struct urb *urb;
1228 int node, i;
1229 u8 *buf;
1230
1231 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1232
1233 spin_lock_init(&tp->rx_lock);
1234 spin_lock_init(&tp->tx_lock);
ebc2ec48 1235 INIT_LIST_HEAD(&tp->tx_free);
1236 skb_queue_head_init(&tp->tx_queue);
d823ab68 1237 skb_queue_head_init(&tp->rx_queue);
ebc2ec48 1238
1239 for (i = 0; i < RTL8152_MAX_RX; i++) {
52aec126 1240 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
ebc2ec48 1241 if (!buf)
1242 goto err1;
1243
1244 if (buf != rx_agg_align(buf)) {
1245 kfree(buf);
52aec126 1246 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
8e1f51bd 1247 node);
ebc2ec48 1248 if (!buf)
1249 goto err1;
1250 }
1251
1252 urb = usb_alloc_urb(0, GFP_KERNEL);
1253 if (!urb) {
1254 kfree(buf);
1255 goto err1;
1256 }
1257
1258 INIT_LIST_HEAD(&tp->rx_info[i].list);
1259 tp->rx_info[i].context = tp;
1260 tp->rx_info[i].urb = urb;
1261 tp->rx_info[i].buffer = buf;
1262 tp->rx_info[i].head = rx_agg_align(buf);
1263 }
1264
1265 for (i = 0; i < RTL8152_MAX_TX; i++) {
52aec126 1266 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
ebc2ec48 1267 if (!buf)
1268 goto err1;
1269
1270 if (buf != tx_agg_align(buf)) {
1271 kfree(buf);
52aec126 1272 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
8e1f51bd 1273 node);
ebc2ec48 1274 if (!buf)
1275 goto err1;
1276 }
1277
1278 urb = usb_alloc_urb(0, GFP_KERNEL);
1279 if (!urb) {
1280 kfree(buf);
1281 goto err1;
1282 }
1283
1284 INIT_LIST_HEAD(&tp->tx_info[i].list);
1285 tp->tx_info[i].context = tp;
1286 tp->tx_info[i].urb = urb;
1287 tp->tx_info[i].buffer = buf;
1288 tp->tx_info[i].head = tx_agg_align(buf);
1289
1290 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1291 }
1292
40a82917 1293 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1294 if (!tp->intr_urb)
1295 goto err1;
1296
1297 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1298 if (!tp->intr_buff)
1299 goto err1;
1300
1301 tp->intr_interval = (int)ep_intr->desc.bInterval;
1302 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
b209af99 1303 tp->intr_buff, INTBUFSIZE, intr_callback,
1304 tp, tp->intr_interval);
40a82917 1305
ebc2ec48 1306 return 0;
1307
1308err1:
1309 free_all_mem(tp);
1310 return -ENOMEM;
1311}
1312
0de98f6c 1313static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1314{
1315 struct tx_agg *agg = NULL;
1316 unsigned long flags;
1317
21949ab7 1318 if (list_empty(&tp->tx_free))
1319 return NULL;
1320
0de98f6c 1321 spin_lock_irqsave(&tp->tx_lock, flags);
1322 if (!list_empty(&tp->tx_free)) {
1323 struct list_head *cursor;
1324
1325 cursor = tp->tx_free.next;
1326 list_del_init(cursor);
1327 agg = list_entry(cursor, struct tx_agg, list);
1328 }
1329 spin_unlock_irqrestore(&tp->tx_lock, flags);
1330
1331 return agg;
1332}
1333
60c89071 1334static inline __be16 get_protocol(struct sk_buff *skb)
5bd23881 1335{
60c89071 1336 __be16 protocol;
5bd23881 1337
60c89071 1338 if (skb->protocol == htons(ETH_P_8021Q))
1339 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1340 else
1341 protocol = skb->protocol;
5bd23881 1342
60c89071 1343 return protocol;
1344}
5bd23881 1345
b209af99 1346/* r8152_csum_workaround()
6128d1bb 1347 * The hw limites the value the transport offset. When the offset is out of the
1348 * range, calculate the checksum by sw.
1349 */
1350static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1351 struct sk_buff_head *list)
1352{
1353 if (skb_shinfo(skb)->gso_size) {
1354 netdev_features_t features = tp->netdev->features;
1355 struct sk_buff_head seg_list;
1356 struct sk_buff *segs, *nskb;
1357
a91d45f1 1358 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6128d1bb 1359 segs = skb_gso_segment(skb, features);
1360 if (IS_ERR(segs) || !segs)
1361 goto drop;
1362
1363 __skb_queue_head_init(&seg_list);
1364
1365 do {
1366 nskb = segs;
1367 segs = segs->next;
1368 nskb->next = NULL;
1369 __skb_queue_tail(&seg_list, nskb);
1370 } while (segs);
1371
1372 skb_queue_splice(&seg_list, list);
1373 dev_kfree_skb(skb);
1374 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1375 if (skb_checksum_help(skb) < 0)
1376 goto drop;
1377
1378 __skb_queue_head(list, skb);
1379 } else {
1380 struct net_device_stats *stats;
1381
1382drop:
1383 stats = &tp->netdev->stats;
1384 stats->tx_dropped++;
1385 dev_kfree_skb(skb);
1386 }
1387}
1388
b209af99 1389/* msdn_giant_send_check()
6128d1bb 1390 * According to the document of microsoft, the TCP Pseudo Header excludes the
1391 * packet length for IPv6 TCP large packets.
1392 */
1393static int msdn_giant_send_check(struct sk_buff *skb)
1394{
1395 const struct ipv6hdr *ipv6h;
1396 struct tcphdr *th;
fcb308d5 1397 int ret;
1398
1399 ret = skb_cow_head(skb, 0);
1400 if (ret)
1401 return ret;
6128d1bb 1402
1403 ipv6h = ipv6_hdr(skb);
1404 th = tcp_hdr(skb);
1405
1406 th->check = 0;
1407 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1408
fcb308d5 1409 return ret;
6128d1bb 1410}
1411
c5554298 1412static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1413{
df8a39de 1414 if (skb_vlan_tag_present(skb)) {
c5554298 1415 u32 opts2;
1416
df8a39de 1417 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
c5554298 1418 desc->opts2 |= cpu_to_le32(opts2);
1419 }
1420}
1421
1422static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1423{
1424 u32 opts2 = le32_to_cpu(desc->opts2);
1425
1426 if (opts2 & RX_VLAN_TAG)
1427 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1428 swab16(opts2 & 0xffff));
1429}
1430
60c89071 1431static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1432 struct sk_buff *skb, u32 len, u32 transport_offset)
1433{
1434 u32 mss = skb_shinfo(skb)->gso_size;
1435 u32 opts1, opts2 = 0;
1436 int ret = TX_CSUM_SUCCESS;
1437
1438 WARN_ON_ONCE(len > TX_LEN_MAX);
1439
1440 opts1 = len | TX_FS | TX_LS;
1441
1442 if (mss) {
6128d1bb 1443 if (transport_offset > GTTCPHO_MAX) {
1444 netif_warn(tp, tx_err, tp->netdev,
1445 "Invalid transport offset 0x%x for TSO\n",
1446 transport_offset);
1447 ret = TX_CSUM_TSO;
1448 goto unavailable;
1449 }
1450
60c89071 1451 switch (get_protocol(skb)) {
1452 case htons(ETH_P_IP):
1453 opts1 |= GTSENDV4;
1454 break;
1455
6128d1bb 1456 case htons(ETH_P_IPV6):
fcb308d5 1457 if (msdn_giant_send_check(skb)) {
1458 ret = TX_CSUM_TSO;
1459 goto unavailable;
1460 }
6128d1bb 1461 opts1 |= GTSENDV6;
6128d1bb 1462 break;
1463
60c89071 1464 default:
1465 WARN_ON_ONCE(1);
1466 break;
1467 }
1468
1469 opts1 |= transport_offset << GTTCPHO_SHIFT;
1470 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1471 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1472 u8 ip_protocol;
5bd23881 1473
6128d1bb 1474 if (transport_offset > TCPHO_MAX) {
1475 netif_warn(tp, tx_err, tp->netdev,
1476 "Invalid transport offset 0x%x\n",
1477 transport_offset);
1478 ret = TX_CSUM_NONE;
1479 goto unavailable;
1480 }
1481
60c89071 1482 switch (get_protocol(skb)) {
5bd23881 1483 case htons(ETH_P_IP):
1484 opts2 |= IPV4_CS;
1485 ip_protocol = ip_hdr(skb)->protocol;
1486 break;
1487
1488 case htons(ETH_P_IPV6):
1489 opts2 |= IPV6_CS;
1490 ip_protocol = ipv6_hdr(skb)->nexthdr;
1491 break;
1492
1493 default:
1494 ip_protocol = IPPROTO_RAW;
1495 break;
1496 }
1497
60c89071 1498 if (ip_protocol == IPPROTO_TCP)
5bd23881 1499 opts2 |= TCP_CS;
60c89071 1500 else if (ip_protocol == IPPROTO_UDP)
5bd23881 1501 opts2 |= UDP_CS;
60c89071 1502 else
5bd23881 1503 WARN_ON_ONCE(1);
5bd23881 1504
60c89071 1505 opts2 |= transport_offset << TCPHO_SHIFT;
5bd23881 1506 }
60c89071 1507
1508 desc->opts2 = cpu_to_le32(opts2);
1509 desc->opts1 = cpu_to_le32(opts1);
1510
6128d1bb 1511unavailable:
60c89071 1512 return ret;
5bd23881 1513}
1514
b1379d9a 1515static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1516{
d84130a1 1517 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
9a4be1bd 1518 int remain, ret;
b1379d9a 1519 u8 *tx_data;
1520
d84130a1 1521 __skb_queue_head_init(&skb_head);
0c3121fc 1522 spin_lock(&tx_queue->lock);
d84130a1 1523 skb_queue_splice_init(tx_queue, &skb_head);
0c3121fc 1524 spin_unlock(&tx_queue->lock);
d84130a1 1525
b1379d9a 1526 tx_data = agg->head;
b209af99 1527 agg->skb_num = 0;
1528 agg->skb_len = 0;
52aec126 1529 remain = agg_buf_sz;
b1379d9a 1530
7937f9e5 1531 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
b1379d9a 1532 struct tx_desc *tx_desc;
1533 struct sk_buff *skb;
1534 unsigned int len;
60c89071 1535 u32 offset;
b1379d9a 1536
d84130a1 1537 skb = __skb_dequeue(&skb_head);
b1379d9a 1538 if (!skb)
1539 break;
1540
60c89071 1541 len = skb->len + sizeof(*tx_desc);
1542
1543 if (len > remain) {
d84130a1 1544 __skb_queue_head(&skb_head, skb);
b1379d9a 1545 break;
1546 }
1547
7937f9e5 1548 tx_data = tx_agg_align(tx_data);
b1379d9a 1549 tx_desc = (struct tx_desc *)tx_data;
60c89071 1550
1551 offset = (u32)skb_transport_offset(skb);
1552
6128d1bb 1553 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1554 r8152_csum_workaround(tp, skb, &skb_head);
1555 continue;
1556 }
60c89071 1557
c5554298 1558 rtl_tx_vlan_tag(tx_desc, skb);
1559
b1379d9a 1560 tx_data += sizeof(*tx_desc);
1561
60c89071 1562 len = skb->len;
1563 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1564 struct net_device_stats *stats = &tp->netdev->stats;
1565
1566 stats->tx_dropped++;
1567 dev_kfree_skb_any(skb);
1568 tx_data -= sizeof(*tx_desc);
1569 continue;
1570 }
1571
1572 tx_data += len;
b1379d9a 1573 agg->skb_len += len;
60c89071 1574 agg->skb_num++;
1575
b1379d9a 1576 dev_kfree_skb_any(skb);
1577
52aec126 1578 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
b1379d9a 1579 }
1580
d84130a1 1581 if (!skb_queue_empty(&skb_head)) {
0c3121fc 1582 spin_lock(&tx_queue->lock);
d84130a1 1583 skb_queue_splice(&skb_head, tx_queue);
0c3121fc 1584 spin_unlock(&tx_queue->lock);
d84130a1 1585 }
1586
0c3121fc 1587 netif_tx_lock(tp->netdev);
dd1b119c 1588
1589 if (netif_queue_stopped(tp->netdev) &&
1590 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1591 netif_wake_queue(tp->netdev);
1592
0c3121fc 1593 netif_tx_unlock(tp->netdev);
9a4be1bd 1594
0c3121fc 1595 ret = usb_autopm_get_interface_async(tp->intf);
9a4be1bd 1596 if (ret < 0)
1597 goto out_tx_fill;
dd1b119c 1598
b1379d9a 1599 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1600 agg->head, (int)(tx_data - (u8 *)agg->head),
1601 (usb_complete_t)write_bulk_callback, agg);
1602
0c3121fc 1603 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
9a4be1bd 1604 if (ret < 0)
0c3121fc 1605 usb_autopm_put_interface_async(tp->intf);
9a4be1bd 1606
1607out_tx_fill:
1608 return ret;
b1379d9a 1609}
1610
565cab0a 1611static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1612{
1613 u8 checksum = CHECKSUM_NONE;
1614 u32 opts2, opts3;
1615
1616 if (tp->version == RTL_VER_01)
1617 goto return_result;
1618
1619 opts2 = le32_to_cpu(rx_desc->opts2);
1620 opts3 = le32_to_cpu(rx_desc->opts3);
1621
1622 if (opts2 & RD_IPV4_CS) {
1623 if (opts3 & IPF)
1624 checksum = CHECKSUM_NONE;
1625 else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
1626 checksum = CHECKSUM_NONE;
1627 else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
1628 checksum = CHECKSUM_NONE;
1629 else
1630 checksum = CHECKSUM_UNNECESSARY;
6128d1bb 1631 } else if (RD_IPV6_CS) {
1632 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1633 checksum = CHECKSUM_UNNECESSARY;
1634 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1635 checksum = CHECKSUM_UNNECESSARY;
565cab0a 1636 }
1637
1638return_result:
1639 return checksum;
1640}
1641
d823ab68 1642static int rx_bottom(struct r8152 *tp, int budget)
ebc2ec48 1643{
a5a4f468 1644 unsigned long flags;
d84130a1 1645 struct list_head *cursor, *next, rx_queue;
e1a2ca92 1646 int ret = 0, work_done = 0;
d823ab68 1647
1648 if (!skb_queue_empty(&tp->rx_queue)) {
1649 while (work_done < budget) {
1650 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1651 struct net_device *netdev = tp->netdev;
1652 struct net_device_stats *stats = &netdev->stats;
1653 unsigned int pkt_len;
1654
1655 if (!skb)
1656 break;
1657
1658 pkt_len = skb->len;
1659 napi_gro_receive(&tp->napi, skb);
1660 work_done++;
1661 stats->rx_packets++;
1662 stats->rx_bytes += pkt_len;
1663 }
1664 }
ebc2ec48 1665
d84130a1 1666 if (list_empty(&tp->rx_done))
d823ab68 1667 goto out1;
d84130a1 1668
1669 INIT_LIST_HEAD(&rx_queue);
a5a4f468 1670 spin_lock_irqsave(&tp->rx_lock, flags);
d84130a1 1671 list_splice_init(&tp->rx_done, &rx_queue);
1672 spin_unlock_irqrestore(&tp->rx_lock, flags);
1673
1674 list_for_each_safe(cursor, next, &rx_queue) {
43a4478d 1675 struct rx_desc *rx_desc;
1676 struct rx_agg *agg;
43a4478d 1677 int len_used = 0;
1678 struct urb *urb;
1679 u8 *rx_data;
43a4478d 1680
ebc2ec48 1681 list_del_init(cursor);
ebc2ec48 1682
1683 agg = list_entry(cursor, struct rx_agg, list);
1684 urb = agg->urb;
0de98f6c 1685 if (urb->actual_length < ETH_ZLEN)
1686 goto submit;
ebc2ec48 1687
ebc2ec48 1688 rx_desc = agg->head;
1689 rx_data = agg->head;
7937f9e5 1690 len_used += sizeof(struct rx_desc);
ebc2ec48 1691
7937f9e5 1692 while (urb->actual_length > len_used) {
43a4478d 1693 struct net_device *netdev = tp->netdev;
05e0f1aa 1694 struct net_device_stats *stats = &netdev->stats;
7937f9e5 1695 unsigned int pkt_len;
43a4478d 1696 struct sk_buff *skb;
1697
7937f9e5 1698 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
ebc2ec48 1699 if (pkt_len < ETH_ZLEN)
1700 break;
1701
7937f9e5 1702 len_used += pkt_len;
1703 if (urb->actual_length < len_used)
1704 break;
1705
8e1f51bd 1706 pkt_len -= CRC_SIZE;
ebc2ec48 1707 rx_data += sizeof(struct rx_desc);
1708
1709 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1710 if (!skb) {
1711 stats->rx_dropped++;
5e2f7485 1712 goto find_next_rx;
ebc2ec48 1713 }
565cab0a 1714
1715 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
ebc2ec48 1716 memcpy(skb->data, rx_data, pkt_len);
1717 skb_put(skb, pkt_len);
1718 skb->protocol = eth_type_trans(skb, netdev);
c5554298 1719 rtl_rx_vlan_tag(rx_desc, skb);
d823ab68 1720 if (work_done < budget) {
1721 napi_gro_receive(&tp->napi, skb);
1722 work_done++;
1723 stats->rx_packets++;
1724 stats->rx_bytes += pkt_len;
1725 } else {
1726 __skb_queue_tail(&tp->rx_queue, skb);
1727 }
ebc2ec48 1728
5e2f7485 1729find_next_rx:
8e1f51bd 1730 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
ebc2ec48 1731 rx_desc = (struct rx_desc *)rx_data;
ebc2ec48 1732 len_used = (int)(rx_data - (u8 *)agg->head);
7937f9e5 1733 len_used += sizeof(struct rx_desc);
ebc2ec48 1734 }
1735
0de98f6c 1736submit:
e1a2ca92 1737 if (!ret) {
1738 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1739 } else {
1740 urb->actual_length = 0;
1741 list_add_tail(&agg->list, next);
1742 }
1743 }
1744
1745 if (!list_empty(&rx_queue)) {
1746 spin_lock_irqsave(&tp->rx_lock, flags);
1747 list_splice_tail(&rx_queue, &tp->rx_done);
1748 spin_unlock_irqrestore(&tp->rx_lock, flags);
ebc2ec48 1749 }
d823ab68 1750
1751out1:
1752 return work_done;
ebc2ec48 1753}
1754
1755static void tx_bottom(struct r8152 *tp)
1756{
ebc2ec48 1757 int res;
1758
b1379d9a 1759 do {
1760 struct tx_agg *agg;
ebc2ec48 1761
b1379d9a 1762 if (skb_queue_empty(&tp->tx_queue))
ebc2ec48 1763 break;
1764
b1379d9a 1765 agg = r8152_get_tx_agg(tp);
1766 if (!agg)
ebc2ec48 1767 break;
ebc2ec48 1768
b1379d9a 1769 res = r8152_tx_agg_fill(tp, agg);
1770 if (res) {
05e0f1aa 1771 struct net_device *netdev = tp->netdev;
ebc2ec48 1772
b1379d9a 1773 if (res == -ENODEV) {
67610496 1774 set_bit(RTL8152_UNPLUG, &tp->flags);
b1379d9a 1775 netif_device_detach(netdev);
1776 } else {
05e0f1aa 1777 struct net_device_stats *stats = &netdev->stats;
1778 unsigned long flags;
1779
b1379d9a 1780 netif_warn(tp, tx_err, netdev,
1781 "failed tx_urb %d\n", res);
1782 stats->tx_dropped += agg->skb_num;
db8515ef 1783
b1379d9a 1784 spin_lock_irqsave(&tp->tx_lock, flags);
1785 list_add_tail(&agg->list, &tp->tx_free);
1786 spin_unlock_irqrestore(&tp->tx_lock, flags);
1787 }
ebc2ec48 1788 }
b1379d9a 1789 } while (res == 0);
ebc2ec48 1790}
1791
d823ab68 1792static void bottom_half(struct r8152 *tp)
ac718b69 1793{
ebc2ec48 1794 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1795 return;
1796
1797 if (!test_bit(WORK_ENABLE, &tp->flags))
ac718b69 1798 return;
ebc2ec48 1799
7559fb2f 1800 /* When link down, the driver would cancel all bulks. */
1801 /* This avoid the re-submitting bulk */
ebc2ec48 1802 if (!netif_carrier_ok(tp->netdev))
ac718b69 1803 return;
ebc2ec48 1804
d823ab68 1805 clear_bit(SCHEDULE_NAPI, &tp->flags);
9451a11c 1806
0c3121fc 1807 tx_bottom(tp);
ebc2ec48 1808}
1809
d823ab68 1810static int r8152_poll(struct napi_struct *napi, int budget)
1811{
1812 struct r8152 *tp = container_of(napi, struct r8152, napi);
1813 int work_done;
1814
1815 work_done = rx_bottom(tp, budget);
1816 bottom_half(tp);
1817
1818 if (work_done < budget) {
1819 napi_complete(napi);
1820 if (!list_empty(&tp->rx_done))
1821 napi_schedule(napi);
1822 }
1823
1824 return work_done;
1825}
1826
ebc2ec48 1827static
1828int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1829{
a0fccd48 1830 int ret;
1831
ef827a5b 1832 /* The rx would be stopped, so skip submitting */
1833 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
1834 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
1835 return 0;
1836
ebc2ec48 1837 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
52aec126 1838 agg->head, agg_buf_sz,
b209af99 1839 (usb_complete_t)read_bulk_callback, agg);
ebc2ec48 1840
a0fccd48 1841 ret = usb_submit_urb(agg->urb, mem_flags);
1842 if (ret == -ENODEV) {
1843 set_bit(RTL8152_UNPLUG, &tp->flags);
1844 netif_device_detach(tp->netdev);
1845 } else if (ret) {
1846 struct urb *urb = agg->urb;
1847 unsigned long flags;
1848
1849 urb->actual_length = 0;
1850 spin_lock_irqsave(&tp->rx_lock, flags);
1851 list_add_tail(&agg->list, &tp->rx_done);
1852 spin_unlock_irqrestore(&tp->rx_lock, flags);
d823ab68 1853
1854 netif_err(tp, rx_err, tp->netdev,
1855 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
1856
1857 napi_schedule(&tp->napi);
a0fccd48 1858 }
1859
1860 return ret;
ac718b69 1861}
1862
00a5e360 1863static void rtl_drop_queued_tx(struct r8152 *tp)
1864{
1865 struct net_device_stats *stats = &tp->netdev->stats;
d84130a1 1866 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
00a5e360 1867 struct sk_buff *skb;
1868
d84130a1 1869 if (skb_queue_empty(tx_queue))
1870 return;
1871
1872 __skb_queue_head_init(&skb_head);
2685d410 1873 spin_lock_bh(&tx_queue->lock);
d84130a1 1874 skb_queue_splice_init(tx_queue, &skb_head);
2685d410 1875 spin_unlock_bh(&tx_queue->lock);
d84130a1 1876
1877 while ((skb = __skb_dequeue(&skb_head))) {
00a5e360 1878 dev_kfree_skb(skb);
1879 stats->tx_dropped++;
1880 }
1881}
1882
ac718b69 1883static void rtl8152_tx_timeout(struct net_device *netdev)
1884{
1885 struct r8152 *tp = netdev_priv(netdev);
ebc2ec48 1886 int i;
1887
4a8deae2 1888 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
ebc2ec48 1889 for (i = 0; i < RTL8152_MAX_TX; i++)
1890 usb_unlink_urb(tp->tx_info[i].urb);
ac718b69 1891}
1892
1893static void rtl8152_set_rx_mode(struct net_device *netdev)
1894{
1895 struct r8152 *tp = netdev_priv(netdev);
1896
40a82917 1897 if (tp->speed & LINK_STATUS) {
ac718b69 1898 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
40a82917 1899 schedule_delayed_work(&tp->schedule, 0);
1900 }
ac718b69 1901}
1902
1903static void _rtl8152_set_rx_mode(struct net_device *netdev)
1904{
1905 struct r8152 *tp = netdev_priv(netdev);
31787f53 1906 u32 mc_filter[2]; /* Multicast hash filter */
1907 __le32 tmp[2];
ac718b69 1908 u32 ocp_data;
1909
ac718b69 1910 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1911 netif_stop_queue(netdev);
1912 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1913 ocp_data &= ~RCR_ACPT_ALL;
1914 ocp_data |= RCR_AB | RCR_APM;
1915
1916 if (netdev->flags & IFF_PROMISC) {
1917 /* Unconditionally log net taps. */
1918 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1919 ocp_data |= RCR_AM | RCR_AAP;
b209af99 1920 mc_filter[1] = 0xffffffff;
1921 mc_filter[0] = 0xffffffff;
ac718b69 1922 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1923 (netdev->flags & IFF_ALLMULTI)) {
1924 /* Too many to filter perfectly -- accept all multicasts. */
1925 ocp_data |= RCR_AM;
b209af99 1926 mc_filter[1] = 0xffffffff;
1927 mc_filter[0] = 0xffffffff;
ac718b69 1928 } else {
1929 struct netdev_hw_addr *ha;
1930
b209af99 1931 mc_filter[1] = 0;
1932 mc_filter[0] = 0;
ac718b69 1933 netdev_for_each_mc_addr(ha, netdev) {
1934 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
b209af99 1935
ac718b69 1936 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1937 ocp_data |= RCR_AM;
1938 }
1939 }
1940
31787f53 1941 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1942 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
ac718b69 1943
31787f53 1944 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
ac718b69 1945 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1946 netif_wake_queue(netdev);
ac718b69 1947}
1948
a5e31255 1949static netdev_features_t
1950rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
1951 netdev_features_t features)
1952{
1953 u32 mss = skb_shinfo(skb)->gso_size;
1954 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
1955 int offset = skb_transport_offset(skb);
1956
1957 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
1958 features &= ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
1959 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
1960 features &= ~NETIF_F_GSO_MASK;
1961
1962 return features;
1963}
1964
ac718b69 1965static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
b209af99 1966 struct net_device *netdev)
ac718b69 1967{
1968 struct r8152 *tp = netdev_priv(netdev);
ac718b69 1969
ebc2ec48 1970 skb_tx_timestamp(skb);
ac718b69 1971
61598788 1972 skb_queue_tail(&tp->tx_queue, skb);
ebc2ec48 1973
0c3121fc 1974 if (!list_empty(&tp->tx_free)) {
1975 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
d823ab68 1976 set_bit(SCHEDULE_NAPI, &tp->flags);
0c3121fc 1977 schedule_delayed_work(&tp->schedule, 0);
1978 } else {
1979 usb_mark_last_busy(tp->udev);
d823ab68 1980 napi_schedule(&tp->napi);
0c3121fc 1981 }
b209af99 1982 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
dd1b119c 1983 netif_stop_queue(netdev);
b209af99 1984 }
dd1b119c 1985
ac718b69 1986 return NETDEV_TX_OK;
1987}
1988
1989static void r8152b_reset_packet_filter(struct r8152 *tp)
1990{
1991 u32 ocp_data;
1992
1993 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1994 ocp_data &= ~FMC_FCR_MCU_EN;
1995 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1996 ocp_data |= FMC_FCR_MCU_EN;
1997 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1998}
1999
2000static void rtl8152_nic_reset(struct r8152 *tp)
2001{
2002 int i;
2003
2004 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2005
2006 for (i = 0; i < 1000; i++) {
2007 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2008 break;
b209af99 2009 usleep_range(100, 400);
ac718b69 2010 }
2011}
2012
dd1b119c 2013static void set_tx_qlen(struct r8152 *tp)
2014{
2015 struct net_device *netdev = tp->netdev;
2016
52aec126 2017 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
2018 sizeof(struct tx_desc));
dd1b119c 2019}
2020
ac718b69 2021static inline u8 rtl8152_get_speed(struct r8152 *tp)
2022{
2023 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2024}
2025
507605a8 2026static void rtl_set_eee_plus(struct r8152 *tp)
ac718b69 2027{
ebc2ec48 2028 u32 ocp_data;
ac718b69 2029 u8 speed;
2030
2031 speed = rtl8152_get_speed(tp);
ebc2ec48 2032 if (speed & _10bps) {
ac718b69 2033 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
ebc2ec48 2034 ocp_data |= EEEP_CR_EEEP_TX;
ac718b69 2035 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2036 } else {
2037 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
ebc2ec48 2038 ocp_data &= ~EEEP_CR_EEEP_TX;
ac718b69 2039 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2040 }
507605a8 2041}
2042
00a5e360 2043static void rxdy_gated_en(struct r8152 *tp, bool enable)
2044{
2045 u32 ocp_data;
2046
2047 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2048 if (enable)
2049 ocp_data |= RXDY_GATED_EN;
2050 else
2051 ocp_data &= ~RXDY_GATED_EN;
2052 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2053}
2054
445f7f4d 2055static int rtl_start_rx(struct r8152 *tp)
2056{
2057 int i, ret = 0;
2058
d823ab68 2059 napi_disable(&tp->napi);
445f7f4d 2060 INIT_LIST_HEAD(&tp->rx_done);
2061 for (i = 0; i < RTL8152_MAX_RX; i++) {
2062 INIT_LIST_HEAD(&tp->rx_info[i].list);
2063 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2064 if (ret)
2065 break;
2066 }
d823ab68 2067 napi_enable(&tp->napi);
445f7f4d 2068
7bcf4f60 2069 if (ret && ++i < RTL8152_MAX_RX) {
2070 struct list_head rx_queue;
2071 unsigned long flags;
2072
2073 INIT_LIST_HEAD(&rx_queue);
2074
2075 do {
2076 struct rx_agg *agg = &tp->rx_info[i++];
2077 struct urb *urb = agg->urb;
2078
2079 urb->actual_length = 0;
2080 list_add_tail(&agg->list, &rx_queue);
2081 } while (i < RTL8152_MAX_RX);
2082
2083 spin_lock_irqsave(&tp->rx_lock, flags);
2084 list_splice_tail(&rx_queue, &tp->rx_done);
2085 spin_unlock_irqrestore(&tp->rx_lock, flags);
2086 }
2087
445f7f4d 2088 return ret;
2089}
2090
2091static int rtl_stop_rx(struct r8152 *tp)
2092{
2093 int i;
2094
2095 for (i = 0; i < RTL8152_MAX_RX; i++)
2096 usb_kill_urb(tp->rx_info[i].urb);
2097
d823ab68 2098 while (!skb_queue_empty(&tp->rx_queue))
2099 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2100
445f7f4d 2101 return 0;
2102}
2103
507605a8 2104static int rtl_enable(struct r8152 *tp)
2105{
2106 u32 ocp_data;
ac718b69 2107
2108 r8152b_reset_packet_filter(tp);
2109
2110 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2111 ocp_data |= CR_RE | CR_TE;
2112 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2113
00a5e360 2114 rxdy_gated_en(tp, false);
ac718b69 2115
aa2e0926 2116 return 0;
ac718b69 2117}
2118
507605a8 2119static int rtl8152_enable(struct r8152 *tp)
2120{
6871438c 2121 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2122 return -ENODEV;
2123
507605a8 2124 set_tx_qlen(tp);
2125 rtl_set_eee_plus(tp);
2126
2127 return rtl_enable(tp);
2128}
2129
43779f8d 2130static void r8153_set_rx_agg(struct r8152 *tp)
2131{
2132 u8 speed;
2133
2134 speed = rtl8152_get_speed(tp);
2135 if (speed & _1000bps) {
2136 if (tp->udev->speed == USB_SPEED_SUPER) {
2137 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
2138 RX_THR_SUPPER);
2139 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2140 EARLY_AGG_SUPPER);
2141 } else {
2142 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
2143 RX_THR_HIGH);
2144 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2145 EARLY_AGG_HIGH);
2146 }
2147 } else {
2148 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
2149 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2150 EARLY_AGG_SLOW);
2151 }
2152}
2153
2154static int rtl8153_enable(struct r8152 *tp)
2155{
6871438c 2156 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2157 return -ENODEV;
2158
43779f8d 2159 set_tx_qlen(tp);
2160 rtl_set_eee_plus(tp);
2161 r8153_set_rx_agg(tp);
2162
2163 return rtl_enable(tp);
2164}
2165
d70b1137 2166static void rtl_disable(struct r8152 *tp)
ac718b69 2167{
ebc2ec48 2168 u32 ocp_data;
2169 int i;
ac718b69 2170
6871438c 2171 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2172 rtl_drop_queued_tx(tp);
2173 return;
2174 }
2175
ac718b69 2176 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2177 ocp_data &= ~RCR_ACPT_ALL;
2178 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2179
00a5e360 2180 rtl_drop_queued_tx(tp);
ebc2ec48 2181
2182 for (i = 0; i < RTL8152_MAX_TX; i++)
2183 usb_kill_urb(tp->tx_info[i].urb);
ac718b69 2184
00a5e360 2185 rxdy_gated_en(tp, true);
ac718b69 2186
2187 for (i = 0; i < 1000; i++) {
2188 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2189 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2190 break;
8ddfa077 2191 usleep_range(1000, 2000);
ac718b69 2192 }
2193
2194 for (i = 0; i < 1000; i++) {
2195 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2196 break;
8ddfa077 2197 usleep_range(1000, 2000);
ac718b69 2198 }
2199
445f7f4d 2200 rtl_stop_rx(tp);
ac718b69 2201
2202 rtl8152_nic_reset(tp);
2203}
2204
00a5e360 2205static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2206{
2207 u32 ocp_data;
2208
2209 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2210 if (enable)
2211 ocp_data |= POWER_CUT;
2212 else
2213 ocp_data &= ~POWER_CUT;
2214 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2215
2216 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2217 ocp_data &= ~RESUME_INDICATE;
2218 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
00a5e360 2219}
2220
c5554298 2221static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2222{
2223 u32 ocp_data;
2224
2225 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2226 if (enable)
2227 ocp_data |= CPCR_RX_VLAN;
2228 else
2229 ocp_data &= ~CPCR_RX_VLAN;
2230 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2231}
2232
2233static int rtl8152_set_features(struct net_device *dev,
2234 netdev_features_t features)
2235{
2236 netdev_features_t changed = features ^ dev->features;
2237 struct r8152 *tp = netdev_priv(dev);
405f8a0e 2238 int ret;
2239
2240 ret = usb_autopm_get_interface(tp->intf);
2241 if (ret < 0)
2242 goto out;
c5554298 2243
b5403273 2244 mutex_lock(&tp->control);
2245
c5554298 2246 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2247 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2248 rtl_rx_vlan_en(tp, true);
2249 else
2250 rtl_rx_vlan_en(tp, false);
2251 }
2252
b5403273 2253 mutex_unlock(&tp->control);
2254
405f8a0e 2255 usb_autopm_put_interface(tp->intf);
2256
2257out:
2258 return ret;
c5554298 2259}
2260
21ff2e89 2261#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2262
2263static u32 __rtl_get_wol(struct r8152 *tp)
2264{
2265 u32 ocp_data;
2266 u32 wolopts = 0;
2267
2268 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2269 if (!(ocp_data & LAN_WAKE_EN))
2270 return 0;
2271
2272 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2273 if (ocp_data & LINK_ON_WAKE_EN)
2274 wolopts |= WAKE_PHY;
2275
2276 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2277 if (ocp_data & UWF_EN)
2278 wolopts |= WAKE_UCAST;
2279 if (ocp_data & BWF_EN)
2280 wolopts |= WAKE_BCAST;
2281 if (ocp_data & MWF_EN)
2282 wolopts |= WAKE_MCAST;
2283
2284 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2285 if (ocp_data & MAGIC_EN)
2286 wolopts |= WAKE_MAGIC;
2287
2288 return wolopts;
2289}
2290
2291static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2292{
2293 u32 ocp_data;
2294
2295 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2296
2297 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2298 ocp_data &= ~LINK_ON_WAKE_EN;
2299 if (wolopts & WAKE_PHY)
2300 ocp_data |= LINK_ON_WAKE_EN;
2301 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2302
2303 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2304 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
2305 if (wolopts & WAKE_UCAST)
2306 ocp_data |= UWF_EN;
2307 if (wolopts & WAKE_BCAST)
2308 ocp_data |= BWF_EN;
2309 if (wolopts & WAKE_MCAST)
2310 ocp_data |= MWF_EN;
2311 if (wolopts & WAKE_ANY)
2312 ocp_data |= LAN_WAKE_EN;
2313 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2314
2315 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2316
2317 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2318 ocp_data &= ~MAGIC_EN;
2319 if (wolopts & WAKE_MAGIC)
2320 ocp_data |= MAGIC_EN;
2321 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2322
2323 if (wolopts & WAKE_ANY)
2324 device_set_wakeup_enable(&tp->udev->dev, true);
2325 else
2326 device_set_wakeup_enable(&tp->udev->dev, false);
2327}
2328
9a4be1bd 2329static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2330{
2331 if (enable) {
2332 u32 ocp_data;
2333
2334 __rtl_set_wol(tp, WAKE_ANY);
2335
2336 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2337
2338 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2339 ocp_data |= LINK_OFF_WAKE_EN;
2340 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2341
2342 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2343 } else {
2344 __rtl_set_wol(tp, tp->saved_wolopts);
2345 }
2346}
2347
aa66a5f1 2348static void rtl_phy_reset(struct r8152 *tp)
2349{
2350 u16 data;
2351 int i;
2352
2353 clear_bit(PHY_RESET, &tp->flags);
2354
2355 data = r8152_mdio_read(tp, MII_BMCR);
2356
2357 /* don't reset again before the previous one complete */
2358 if (data & BMCR_RESET)
2359 return;
2360
2361 data |= BMCR_RESET;
2362 r8152_mdio_write(tp, MII_BMCR, data);
2363
2364 for (i = 0; i < 50; i++) {
2365 msleep(20);
2366 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2367 break;
2368 }
2369}
2370
4349968a 2371static void r8153_teredo_off(struct r8152 *tp)
2372{
2373 u32 ocp_data;
2374
2375 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2376 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
2377 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2378
2379 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2380 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2381 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2382}
2383
2384static void r8152b_disable_aldps(struct r8152 *tp)
2385{
2386 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
2387 msleep(20);
2388}
2389
2390static inline void r8152b_enable_aldps(struct r8152 *tp)
2391{
2392 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2393 LINKENA | DIS_SDSAVE);
2394}
2395
d70b1137 2396static void rtl8152_disable(struct r8152 *tp)
2397{
2398 r8152b_disable_aldps(tp);
2399 rtl_disable(tp);
2400 r8152b_enable_aldps(tp);
2401}
2402
4349968a 2403static void r8152b_hw_phy_cfg(struct r8152 *tp)
2404{
f0cbe0ac 2405 u16 data;
2406
2407 data = r8152_mdio_read(tp, MII_BMCR);
2408 if (data & BMCR_PDOWN) {
2409 data &= ~BMCR_PDOWN;
2410 r8152_mdio_write(tp, MII_BMCR, data);
2411 }
2412
aa66a5f1 2413 set_bit(PHY_RESET, &tp->flags);
4349968a 2414}
2415
ac718b69 2416static void r8152b_exit_oob(struct r8152 *tp)
2417{
db8515ef 2418 u32 ocp_data;
2419 int i;
ac718b69 2420
2421 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2422 ocp_data &= ~RCR_ACPT_ALL;
2423 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2424
00a5e360 2425 rxdy_gated_en(tp, true);
da9bd117 2426 r8153_teredo_off(tp);
7e9da481 2427 r8152b_hw_phy_cfg(tp);
ac718b69 2428
2429 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2430 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2431
2432 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2433 ocp_data &= ~NOW_IS_OOB;
2434 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2435
2436 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2437 ocp_data &= ~MCU_BORW_EN;
2438 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2439
2440 for (i = 0; i < 1000; i++) {
2441 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2442 if (ocp_data & LINK_LIST_READY)
2443 break;
8ddfa077 2444 usleep_range(1000, 2000);
ac718b69 2445 }
2446
2447 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2448 ocp_data |= RE_INIT_LL;
2449 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2450
2451 for (i = 0; i < 1000; i++) {
2452 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2453 if (ocp_data & LINK_LIST_READY)
2454 break;
8ddfa077 2455 usleep_range(1000, 2000);
ac718b69 2456 }
2457
2458 rtl8152_nic_reset(tp);
2459
2460 /* rx share fifo credit full threshold */
2461 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2462
a3cc465d 2463 if (tp->udev->speed == USB_SPEED_FULL ||
2464 tp->udev->speed == USB_SPEED_LOW) {
ac718b69 2465 /* rx share fifo credit near full threshold */
2466 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2467 RXFIFO_THR2_FULL);
2468 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2469 RXFIFO_THR3_FULL);
2470 } else {
2471 /* rx share fifo credit near full threshold */
2472 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2473 RXFIFO_THR2_HIGH);
2474 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2475 RXFIFO_THR3_HIGH);
2476 }
2477
2478 /* TX share fifo free credit full threshold */
2479 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2480
2481 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
8e1f51bd 2482 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
ac718b69 2483 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2484 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2485
c5554298 2486 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
ac718b69 2487
2488 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2489
2490 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2491 ocp_data |= TCR0_AUTO_FIFO;
2492 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2493}
2494
2495static void r8152b_enter_oob(struct r8152 *tp)
2496{
45f4a19f 2497 u32 ocp_data;
2498 int i;
ac718b69 2499
2500 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2501 ocp_data &= ~NOW_IS_OOB;
2502 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2503
2504 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2505 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2506 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2507
d70b1137 2508 rtl_disable(tp);
ac718b69 2509
2510 for (i = 0; i < 1000; i++) {
2511 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2512 if (ocp_data & LINK_LIST_READY)
2513 break;
8ddfa077 2514 usleep_range(1000, 2000);
ac718b69 2515 }
2516
2517 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2518 ocp_data |= RE_INIT_LL;
2519 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2520
2521 for (i = 0; i < 1000; i++) {
2522 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2523 if (ocp_data & LINK_LIST_READY)
2524 break;
8ddfa077 2525 usleep_range(1000, 2000);
ac718b69 2526 }
2527
2528 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2529
c5554298 2530 rtl_rx_vlan_en(tp, true);
ac718b69 2531
2532 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2533 ocp_data |= ALDPS_PROXY_MODE;
2534 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2535
2536 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2537 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2538 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2539
00a5e360 2540 rxdy_gated_en(tp, false);
ac718b69 2541
2542 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2543 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2544 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2545}
2546
43779f8d 2547static void r8153_hw_phy_cfg(struct r8152 *tp)
2548{
2549 u32 ocp_data;
2550 u16 data;
2551
2552 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
f0cbe0ac 2553 data = r8152_mdio_read(tp, MII_BMCR);
2554 if (data & BMCR_PDOWN) {
2555 data &= ~BMCR_PDOWN;
2556 r8152_mdio_write(tp, MII_BMCR, data);
2557 }
43779f8d 2558
2559 if (tp->version == RTL_VER_03) {
2560 data = ocp_reg_read(tp, OCP_EEE_CFG);
2561 data &= ~CTAP_SHORT_EN;
2562 ocp_reg_write(tp, OCP_EEE_CFG, data);
2563 }
2564
2565 data = ocp_reg_read(tp, OCP_POWER_CFG);
2566 data |= EEE_CLKDIV_EN;
2567 ocp_reg_write(tp, OCP_POWER_CFG, data);
2568
2569 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2570 data |= EN_10M_BGOFF;
2571 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2572 data = ocp_reg_read(tp, OCP_POWER_CFG);
2573 data |= EN_10M_PLLOFF;
2574 ocp_reg_write(tp, OCP_POWER_CFG, data);
b4d99def 2575 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
43779f8d 2576
2577 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2578 ocp_data |= PFM_PWM_SWITCH;
2579 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2580
b4d99def 2581 /* Enable LPF corner auto tune */
2582 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
43779f8d 2583
b4d99def 2584 /* Adjust 10M Amplitude */
2585 sram_write(tp, SRAM_10M_AMP1, 0x00af);
2586 sram_write(tp, SRAM_10M_AMP2, 0x0208);
aa66a5f1 2587
2588 set_bit(PHY_RESET, &tp->flags);
43779f8d 2589}
2590
b9702723 2591static void r8153_u1u2en(struct r8152 *tp, bool enable)
43779f8d 2592{
2593 u8 u1u2[8];
2594
2595 if (enable)
2596 memset(u1u2, 0xff, sizeof(u1u2));
2597 else
2598 memset(u1u2, 0x00, sizeof(u1u2));
2599
2600 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2601}
2602
b9702723 2603static void r8153_u2p3en(struct r8152 *tp, bool enable)
43779f8d 2604{
2605 u32 ocp_data;
2606
2607 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2608 if (enable)
2609 ocp_data |= U2P3_ENABLE;
2610 else
2611 ocp_data &= ~U2P3_ENABLE;
2612 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2613}
2614
b9702723 2615static void r8153_power_cut_en(struct r8152 *tp, bool enable)
43779f8d 2616{
2617 u32 ocp_data;
2618
2619 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2620 if (enable)
2621 ocp_data |= PWR_EN | PHASE2_EN;
2622 else
2623 ocp_data &= ~(PWR_EN | PHASE2_EN);
2624 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2625
2626 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2627 ocp_data &= ~PCUT_STATUS;
2628 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2629}
2630
43779f8d 2631static void r8153_first_init(struct r8152 *tp)
2632{
2633 u32 ocp_data;
2634 int i;
2635
00a5e360 2636 rxdy_gated_en(tp, true);
43779f8d 2637 r8153_teredo_off(tp);
2638
2639 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2640 ocp_data &= ~RCR_ACPT_ALL;
2641 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2642
2643 r8153_hw_phy_cfg(tp);
2644
2645 rtl8152_nic_reset(tp);
2646
2647 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2648 ocp_data &= ~NOW_IS_OOB;
2649 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2650
2651 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2652 ocp_data &= ~MCU_BORW_EN;
2653 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2654
2655 for (i = 0; i < 1000; i++) {
2656 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2657 if (ocp_data & LINK_LIST_READY)
2658 break;
8ddfa077 2659 usleep_range(1000, 2000);
43779f8d 2660 }
2661
2662 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2663 ocp_data |= RE_INIT_LL;
2664 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2665
2666 for (i = 0; i < 1000; i++) {
2667 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2668 if (ocp_data & LINK_LIST_READY)
2669 break;
8ddfa077 2670 usleep_range(1000, 2000);
43779f8d 2671 }
2672
c5554298 2673 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
43779f8d 2674
69b4b7a4 2675 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
2676 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
43779f8d 2677
2678 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2679 ocp_data |= TCR0_AUTO_FIFO;
2680 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2681
2682 rtl8152_nic_reset(tp);
2683
2684 /* rx share fifo credit full threshold */
2685 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2686 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2687 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2688 /* TX share fifo free credit full threshold */
2689 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2690
9629e3c0 2691 /* rx aggregation */
43779f8d 2692 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2693 ocp_data &= ~RX_AGG_DISABLE;
2694 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2695}
2696
2697static void r8153_enter_oob(struct r8152 *tp)
2698{
2699 u32 ocp_data;
2700 int i;
2701
2702 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2703 ocp_data &= ~NOW_IS_OOB;
2704 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2705
d70b1137 2706 rtl_disable(tp);
43779f8d 2707
2708 for (i = 0; i < 1000; i++) {
2709 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2710 if (ocp_data & LINK_LIST_READY)
2711 break;
8ddfa077 2712 usleep_range(1000, 2000);
43779f8d 2713 }
2714
2715 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2716 ocp_data |= RE_INIT_LL;
2717 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2718
2719 for (i = 0; i < 1000; i++) {
2720 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2721 if (ocp_data & LINK_LIST_READY)
2722 break;
8ddfa077 2723 usleep_range(1000, 2000);
43779f8d 2724 }
2725
69b4b7a4 2726 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
43779f8d 2727
43779f8d 2728 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2729 ocp_data &= ~TEREDO_WAKE_MASK;
2730 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2731
c5554298 2732 rtl_rx_vlan_en(tp, true);
43779f8d 2733
2734 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2735 ocp_data |= ALDPS_PROXY_MODE;
2736 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2737
2738 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2739 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2740 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2741
00a5e360 2742 rxdy_gated_en(tp, false);
43779f8d 2743
2744 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2745 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2746 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2747}
2748
2749static void r8153_disable_aldps(struct r8152 *tp)
2750{
2751 u16 data;
2752
2753 data = ocp_reg_read(tp, OCP_POWER_CFG);
2754 data &= ~EN_ALDPS;
2755 ocp_reg_write(tp, OCP_POWER_CFG, data);
2756 msleep(20);
2757}
2758
2759static void r8153_enable_aldps(struct r8152 *tp)
2760{
2761 u16 data;
2762
2763 data = ocp_reg_read(tp, OCP_POWER_CFG);
2764 data |= EN_ALDPS;
2765 ocp_reg_write(tp, OCP_POWER_CFG, data);
2766}
2767
d70b1137 2768static void rtl8153_disable(struct r8152 *tp)
2769{
2770 r8153_disable_aldps(tp);
2771 rtl_disable(tp);
2772 r8153_enable_aldps(tp);
2773}
2774
ac718b69 2775static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2776{
43779f8d 2777 u16 bmcr, anar, gbcr;
ac718b69 2778 int ret = 0;
2779
2780 cancel_delayed_work_sync(&tp->schedule);
2781 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2782 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2783 ADVERTISE_100HALF | ADVERTISE_100FULL);
43779f8d 2784 if (tp->mii.supports_gmii) {
2785 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2786 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2787 } else {
2788 gbcr = 0;
2789 }
ac718b69 2790
2791 if (autoneg == AUTONEG_DISABLE) {
2792 if (speed == SPEED_10) {
2793 bmcr = 0;
2794 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2795 } else if (speed == SPEED_100) {
2796 bmcr = BMCR_SPEED100;
2797 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
43779f8d 2798 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2799 bmcr = BMCR_SPEED1000;
2800 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
ac718b69 2801 } else {
2802 ret = -EINVAL;
2803 goto out;
2804 }
2805
2806 if (duplex == DUPLEX_FULL)
2807 bmcr |= BMCR_FULLDPLX;
2808 } else {
2809 if (speed == SPEED_10) {
2810 if (duplex == DUPLEX_FULL)
2811 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2812 else
2813 anar |= ADVERTISE_10HALF;
2814 } else if (speed == SPEED_100) {
2815 if (duplex == DUPLEX_FULL) {
2816 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2817 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2818 } else {
2819 anar |= ADVERTISE_10HALF;
2820 anar |= ADVERTISE_100HALF;
2821 }
43779f8d 2822 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2823 if (duplex == DUPLEX_FULL) {
2824 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2825 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2826 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2827 } else {
2828 anar |= ADVERTISE_10HALF;
2829 anar |= ADVERTISE_100HALF;
2830 gbcr |= ADVERTISE_1000HALF;
2831 }
ac718b69 2832 } else {
2833 ret = -EINVAL;
2834 goto out;
2835 }
2836
2837 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2838 }
2839
aa66a5f1 2840 if (test_bit(PHY_RESET, &tp->flags))
2841 bmcr |= BMCR_RESET;
2842
43779f8d 2843 if (tp->mii.supports_gmii)
2844 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2845
ac718b69 2846 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2847 r8152_mdio_write(tp, MII_BMCR, bmcr);
2848
aa66a5f1 2849 if (test_bit(PHY_RESET, &tp->flags)) {
2850 int i;
2851
2852 clear_bit(PHY_RESET, &tp->flags);
2853 for (i = 0; i < 50; i++) {
2854 msleep(20);
2855 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2856 break;
2857 }
2858 }
2859
ac718b69 2860out:
ac718b69 2861
2862 return ret;
2863}
2864
d70b1137 2865static void rtl8152_up(struct r8152 *tp)
2866{
2867 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2868 return;
2869
2870 r8152b_disable_aldps(tp);
2871 r8152b_exit_oob(tp);
2872 r8152b_enable_aldps(tp);
2873}
2874
ac718b69 2875static void rtl8152_down(struct r8152 *tp)
2876{
6871438c 2877 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2878 rtl_drop_queued_tx(tp);
2879 return;
2880 }
2881
00a5e360 2882 r8152_power_cut_en(tp, false);
ac718b69 2883 r8152b_disable_aldps(tp);
2884 r8152b_enter_oob(tp);
2885 r8152b_enable_aldps(tp);
2886}
2887
d70b1137 2888static void rtl8153_up(struct r8152 *tp)
2889{
2890 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2891 return;
2892
2893 r8153_disable_aldps(tp);
2894 r8153_first_init(tp);
2895 r8153_enable_aldps(tp);
2896}
2897
43779f8d 2898static void rtl8153_down(struct r8152 *tp)
2899{
6871438c 2900 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2901 rtl_drop_queued_tx(tp);
2902 return;
2903 }
2904
b9702723 2905 r8153_u1u2en(tp, false);
2906 r8153_power_cut_en(tp, false);
43779f8d 2907 r8153_disable_aldps(tp);
2908 r8153_enter_oob(tp);
2909 r8153_enable_aldps(tp);
2910}
2911
ac718b69 2912static void set_carrier(struct r8152 *tp)
2913{
2914 struct net_device *netdev = tp->netdev;
2915 u8 speed;
2916
40a82917 2917 clear_bit(RTL8152_LINK_CHG, &tp->flags);
ac718b69 2918 speed = rtl8152_get_speed(tp);
2919
2920 if (speed & LINK_STATUS) {
2921 if (!(tp->speed & LINK_STATUS)) {
c81229c9 2922 tp->rtl_ops.enable(tp);
ac718b69 2923 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2924 netif_carrier_on(netdev);
aa2e0926 2925 rtl_start_rx(tp);
ac718b69 2926 }
2927 } else {
2928 if (tp->speed & LINK_STATUS) {
2929 netif_carrier_off(netdev);
d823ab68 2930 napi_disable(&tp->napi);
c81229c9 2931 tp->rtl_ops.disable(tp);
d823ab68 2932 napi_enable(&tp->napi);
ac718b69 2933 }
2934 }
2935 tp->speed = speed;
2936}
2937
2938static void rtl_work_func_t(struct work_struct *work)
2939{
2940 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2941
a1f83fee 2942 /* If the device is unplugged or !netif_running(), the workqueue
2943 * doesn't need to wake the device, and could return directly.
2944 */
2945 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
2946 return;
2947
9a4be1bd 2948 if (usb_autopm_get_interface(tp->intf) < 0)
2949 return;
2950
ac718b69 2951 if (!test_bit(WORK_ENABLE, &tp->flags))
2952 goto out1;
2953
b5403273 2954 if (!mutex_trylock(&tp->control)) {
2955 schedule_delayed_work(&tp->schedule, 0);
2956 goto out1;
2957 }
2958
40a82917 2959 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2960 set_carrier(tp);
ac718b69 2961
2962 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2963 _rtl8152_set_rx_mode(tp->netdev);
2964
d823ab68 2965 /* don't schedule napi before linking */
2966 if (test_bit(SCHEDULE_NAPI, &tp->flags) &&
0c3121fc 2967 (tp->speed & LINK_STATUS)) {
d823ab68 2968 clear_bit(SCHEDULE_NAPI, &tp->flags);
2969 napi_schedule(&tp->napi);
0c3121fc 2970 }
aa66a5f1 2971
2972 if (test_bit(PHY_RESET, &tp->flags))
2973 rtl_phy_reset(tp);
2974
b5403273 2975 mutex_unlock(&tp->control);
2976
ac718b69 2977out1:
9a4be1bd 2978 usb_autopm_put_interface(tp->intf);
ac718b69 2979}
2980
2981static int rtl8152_open(struct net_device *netdev)
2982{
2983 struct r8152 *tp = netdev_priv(netdev);
2984 int res = 0;
2985
7e9da481 2986 res = alloc_all_mem(tp);
2987 if (res)
2988 goto out;
2989
f4c7476b 2990 /* set speed to 0 to avoid autoresume try to submit rx */
2991 tp->speed = 0;
2992
9a4be1bd 2993 res = usb_autopm_get_interface(tp->intf);
2994 if (res < 0) {
2995 free_all_mem(tp);
2996 goto out;
2997 }
2998
b5403273 2999 mutex_lock(&tp->control);
3000
9a4be1bd 3001 /* The WORK_ENABLE may be set when autoresume occurs */
3002 if (test_bit(WORK_ENABLE, &tp->flags)) {
3003 clear_bit(WORK_ENABLE, &tp->flags);
3004 usb_kill_urb(tp->intr_urb);
3005 cancel_delayed_work_sync(&tp->schedule);
f4c7476b 3006
3007 /* disable the tx/rx, if the workqueue has enabled them. */
9a4be1bd 3008 if (tp->speed & LINK_STATUS)
3009 tp->rtl_ops.disable(tp);
3010 }
3011
7e9da481 3012 tp->rtl_ops.up(tp);
3013
3d55f44f 3014 rtl8152_set_speed(tp, AUTONEG_ENABLE,
3015 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
3016 DUPLEX_FULL);
3017 tp->speed = 0;
3018 netif_carrier_off(netdev);
3019 netif_start_queue(netdev);
3020 set_bit(WORK_ENABLE, &tp->flags);
db8515ef 3021
40a82917 3022 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3023 if (res) {
3024 if (res == -ENODEV)
3025 netif_device_detach(tp->netdev);
4a8deae2
HW
3026 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3027 res);
7e9da481 3028 free_all_mem(tp);
93ffbeab 3029 } else {
d823ab68 3030 napi_enable(&tp->napi);
ac718b69 3031 }
3032
b5403273 3033 mutex_unlock(&tp->control);
3034
9a4be1bd 3035 usb_autopm_put_interface(tp->intf);
ac718b69 3036
7e9da481 3037out:
ac718b69 3038 return res;
3039}
3040
3041static int rtl8152_close(struct net_device *netdev)
3042{
3043 struct r8152 *tp = netdev_priv(netdev);
3044 int res = 0;
3045
d823ab68 3046 napi_disable(&tp->napi);
ac718b69 3047 clear_bit(WORK_ENABLE, &tp->flags);
3d55f44f 3048 usb_kill_urb(tp->intr_urb);
ac718b69 3049 cancel_delayed_work_sync(&tp->schedule);
3050 netif_stop_queue(netdev);
9a4be1bd 3051
3052 res = usb_autopm_get_interface(tp->intf);
3053 if (res < 0) {
3054 rtl_drop_queued_tx(tp);
d823ab68 3055 rtl_stop_rx(tp);
9a4be1bd 3056 } else {
b5403273 3057 mutex_lock(&tp->control);
3058
b209af99 3059 /* The autosuspend may have been enabled and wouldn't
9a4be1bd 3060 * be disable when autoresume occurs, because the
3061 * netif_running() would be false.
3062 */
923e1ee3 3063 rtl_runtime_suspend_enable(tp, false);
9a4be1bd 3064
9a4be1bd 3065 tp->rtl_ops.down(tp);
b5403273 3066
3067 mutex_unlock(&tp->control);
3068
9a4be1bd 3069 usb_autopm_put_interface(tp->intf);
3070 }
ac718b69 3071
7e9da481 3072 free_all_mem(tp);
3073
ac718b69 3074 return res;
3075}
3076
d24f6134 3077static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
3078{
3079 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3080 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3081 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3082}
3083
3084static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3085{
3086 u16 data;
3087
3088 r8152_mmd_indirect(tp, dev, reg);
3089 data = ocp_reg_read(tp, OCP_EEE_DATA);
3090 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3091
3092 return data;
3093}
3094
3095static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
ac718b69 3096{
d24f6134 3097 r8152_mmd_indirect(tp, dev, reg);
3098 ocp_reg_write(tp, OCP_EEE_DATA, data);
3099 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3100}
3101
3102static void r8152_eee_en(struct r8152 *tp, bool enable)
3103{
3104 u16 config1, config2, config3;
45f4a19f 3105 u32 ocp_data;
ac718b69 3106
3107 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
d24f6134 3108 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3109 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3110 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3111
3112 if (enable) {
3113 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3114 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3115 config1 |= sd_rise_time(1);
3116 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3117 config3 |= fast_snr(42);
3118 } else {
3119 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3120 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3121 RX_QUIET_EN);
3122 config1 |= sd_rise_time(7);
3123 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3124 config3 |= fast_snr(511);
3125 }
3126
ac718b69 3127 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
d24f6134 3128 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3129 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3130 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
ac718b69 3131}
3132
d24f6134 3133static void r8152b_enable_eee(struct r8152 *tp)
3134{
3135 r8152_eee_en(tp, true);
3136 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3137}
3138
3139static void r8153_eee_en(struct r8152 *tp, bool enable)
43779f8d 3140{
3141 u32 ocp_data;
d24f6134 3142 u16 config;
43779f8d 3143
3144 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
d24f6134 3145 config = ocp_reg_read(tp, OCP_EEE_CFG);
3146
3147 if (enable) {
3148 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3149 config |= EEE10_EN;
3150 } else {
3151 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3152 config &= ~EEE10_EN;
3153 }
3154
43779f8d 3155 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
d24f6134 3156 ocp_reg_write(tp, OCP_EEE_CFG, config);
3157}
3158
3159static void r8153_enable_eee(struct r8152 *tp)
3160{
3161 r8153_eee_en(tp, true);
3162 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
43779f8d 3163}
3164
ac718b69 3165static void r8152b_enable_fc(struct r8152 *tp)
3166{
3167 u16 anar;
3168
3169 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3170 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3171 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3172}
3173
4f1d4d54 3174static void rtl_tally_reset(struct r8152 *tp)
3175{
3176 u32 ocp_data;
3177
3178 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
3179 ocp_data |= TALLY_RESET;
3180 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
3181}
3182
ac718b69 3183static void r8152b_init(struct r8152 *tp)
3184{
ebc2ec48 3185 u32 ocp_data;
ac718b69 3186
6871438c 3187 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3188 return;
3189
d70b1137 3190 r8152b_disable_aldps(tp);
3191
ac718b69 3192 if (tp->version == RTL_VER_01) {
3193 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3194 ocp_data &= ~LED_MODE_MASK;
3195 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3196 }
3197
00a5e360 3198 r8152_power_cut_en(tp, false);
ac718b69 3199
ac718b69 3200 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3201 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
3202 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3203 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
3204 ocp_data &= ~MCU_CLK_RATIO_MASK;
3205 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
3206 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
3207 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
3208 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
3209 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
3210
3211 r8152b_enable_eee(tp);
3212 r8152b_enable_aldps(tp);
3213 r8152b_enable_fc(tp);
4f1d4d54 3214 rtl_tally_reset(tp);
ac718b69 3215
ebc2ec48 3216 /* enable rx aggregation */
ac718b69 3217 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
ebc2ec48 3218 ocp_data &= ~RX_AGG_DISABLE;
ac718b69 3219 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
3220}
3221
43779f8d 3222static void r8153_init(struct r8152 *tp)
3223{
3224 u32 ocp_data;
3225 int i;
3226
6871438c 3227 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3228 return;
3229
d70b1137 3230 r8153_disable_aldps(tp);
b9702723 3231 r8153_u1u2en(tp, false);
43779f8d 3232
3233 for (i = 0; i < 500; i++) {
3234 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3235 AUTOLOAD_DONE)
3236 break;
3237 msleep(20);
3238 }
3239
3240 for (i = 0; i < 500; i++) {
3241 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
3242 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
3243 break;
3244 msleep(20);
3245 }
3246
b9702723 3247 r8153_u2p3en(tp, false);
43779f8d 3248
3249 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
3250 ocp_data &= ~TIMER11_EN;
3251 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
3252
43779f8d 3253 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3254 ocp_data &= ~LED_MODE_MASK;
3255 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3256
3257 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
3258 ocp_data &= ~LPM_TIMER_MASK;
3259 if (tp->udev->speed == USB_SPEED_SUPER)
3260 ocp_data |= LPM_TIMER_500US;
3261 else
3262 ocp_data |= LPM_TIMER_500MS;
3263 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
3264
3265 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
3266 ocp_data &= ~SEN_VAL_MASK;
3267 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
3268 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
3269
b9702723 3270 r8153_power_cut_en(tp, false);
3271 r8153_u1u2en(tp, true);
43779f8d 3272
43779f8d 3273 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
3274 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
3275 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3276 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3277 U1U2_SPDWN_EN | L1_SPDWN_EN);
3278 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3279 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3280 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
3281 EEE_SPDWN_EN);
3282
3283 r8153_enable_eee(tp);
3284 r8153_enable_aldps(tp);
3285 r8152b_enable_fc(tp);
4f1d4d54 3286 rtl_tally_reset(tp);
43779f8d 3287}
3288
ac718b69 3289static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
3290{
3291 struct r8152 *tp = usb_get_intfdata(intf);
6cc69f2a 3292 struct net_device *netdev = tp->netdev;
3293 int ret = 0;
ac718b69 3294
b5403273 3295 mutex_lock(&tp->control);
3296
6cc69f2a 3297 if (PMSG_IS_AUTO(message)) {
3298 if (netif_running(netdev) && work_busy(&tp->schedule.work)) {
3299 ret = -EBUSY;
3300 goto out1;
3301 }
3302
9a4be1bd 3303 set_bit(SELECTIVE_SUSPEND, &tp->flags);
6cc69f2a 3304 } else {
3305 netif_device_detach(netdev);
3306 }
ac718b69 3307
e3bd1a81 3308 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
ac718b69 3309 clear_bit(WORK_ENABLE, &tp->flags);
40a82917 3310 usb_kill_urb(tp->intr_urb);
d823ab68 3311 napi_disable(&tp->napi);
9a4be1bd 3312 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
445f7f4d 3313 rtl_stop_rx(tp);
9a4be1bd 3314 rtl_runtime_suspend_enable(tp, true);
3315 } else {
6cc69f2a 3316 cancel_delayed_work_sync(&tp->schedule);
9a4be1bd 3317 tp->rtl_ops.down(tp);
9a4be1bd 3318 }
d823ab68 3319 napi_enable(&tp->napi);
ac718b69 3320 }
6cc69f2a 3321out1:
b5403273 3322 mutex_unlock(&tp->control);
3323
6cc69f2a 3324 return ret;
ac718b69 3325}
3326
3327static int rtl8152_resume(struct usb_interface *intf)
3328{
3329 struct r8152 *tp = usb_get_intfdata(intf);
3330
b5403273 3331 mutex_lock(&tp->control);
3332
9a4be1bd 3333 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3334 tp->rtl_ops.init(tp);
3335 netif_device_attach(tp->netdev);
3336 }
3337
ac718b69 3338 if (netif_running(tp->netdev)) {
9a4be1bd 3339 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3340 rtl_runtime_suspend_enable(tp, false);
3341 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
445f7f4d 3342 set_bit(WORK_ENABLE, &tp->flags);
9a4be1bd 3343 if (tp->speed & LINK_STATUS)
445f7f4d 3344 rtl_start_rx(tp);
9a4be1bd 3345 } else {
3346 tp->rtl_ops.up(tp);
3347 rtl8152_set_speed(tp, AUTONEG_ENABLE,
b209af99 3348 tp->mii.supports_gmii ?
3349 SPEED_1000 : SPEED_100,
3350 DUPLEX_FULL);
445f7f4d 3351 tp->speed = 0;
3352 netif_carrier_off(tp->netdev);
3353 set_bit(WORK_ENABLE, &tp->flags);
9a4be1bd 3354 }
40a82917 3355 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
923e1ee3 3356 } else if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3357 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
ac718b69 3358 }
3359
b5403273 3360 mutex_unlock(&tp->control);
3361
ac718b69 3362 return 0;
3363}
3364
21ff2e89 3365static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3366{
3367 struct r8152 *tp = netdev_priv(dev);
3368
9a4be1bd 3369 if (usb_autopm_get_interface(tp->intf) < 0)
3370 return;
3371
b5403273 3372 mutex_lock(&tp->control);
3373
21ff2e89 3374 wol->supported = WAKE_ANY;
3375 wol->wolopts = __rtl_get_wol(tp);
9a4be1bd 3376
b5403273 3377 mutex_unlock(&tp->control);
3378
9a4be1bd 3379 usb_autopm_put_interface(tp->intf);
21ff2e89 3380}
3381
3382static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3383{
3384 struct r8152 *tp = netdev_priv(dev);
9a4be1bd 3385 int ret;
3386
3387 ret = usb_autopm_get_interface(tp->intf);
3388 if (ret < 0)
3389 goto out_set_wol;
21ff2e89 3390
b5403273 3391 mutex_lock(&tp->control);
3392
21ff2e89 3393 __rtl_set_wol(tp, wol->wolopts);
3394 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
3395
b5403273 3396 mutex_unlock(&tp->control);
3397
9a4be1bd 3398 usb_autopm_put_interface(tp->intf);
3399
3400out_set_wol:
3401 return ret;
21ff2e89 3402}
3403
a5ec27c1 3404static u32 rtl8152_get_msglevel(struct net_device *dev)
3405{
3406 struct r8152 *tp = netdev_priv(dev);
3407
3408 return tp->msg_enable;
3409}
3410
3411static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
3412{
3413 struct r8152 *tp = netdev_priv(dev);
3414
3415 tp->msg_enable = value;
3416}
3417
ac718b69 3418static void rtl8152_get_drvinfo(struct net_device *netdev,
3419 struct ethtool_drvinfo *info)
3420{
3421 struct r8152 *tp = netdev_priv(netdev);
3422
b0b46c77 3423 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
3424 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
ac718b69 3425 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
3426}
3427
3428static
3429int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
3430{
3431 struct r8152 *tp = netdev_priv(netdev);
8d4a4d72 3432 int ret;
ac718b69 3433
3434 if (!tp->mii.mdio_read)
3435 return -EOPNOTSUPP;
3436
8d4a4d72 3437 ret = usb_autopm_get_interface(tp->intf);
3438 if (ret < 0)
3439 goto out;
3440
b5403273 3441 mutex_lock(&tp->control);
3442
8d4a4d72 3443 ret = mii_ethtool_gset(&tp->mii, cmd);
3444
b5403273 3445 mutex_unlock(&tp->control);
3446
8d4a4d72 3447 usb_autopm_put_interface(tp->intf);
3448
3449out:
3450 return ret;
ac718b69 3451}
3452
3453static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3454{
3455 struct r8152 *tp = netdev_priv(dev);
9a4be1bd 3456 int ret;
3457
3458 ret = usb_autopm_get_interface(tp->intf);
3459 if (ret < 0)
3460 goto out;
ac718b69 3461
b5403273 3462 mutex_lock(&tp->control);
3463
9a4be1bd 3464 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
3465
b5403273 3466 mutex_unlock(&tp->control);
3467
9a4be1bd 3468 usb_autopm_put_interface(tp->intf);
3469
3470out:
3471 return ret;
ac718b69 3472}
3473
4f1d4d54 3474static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
3475 "tx_packets",
3476 "rx_packets",
3477 "tx_errors",
3478 "rx_errors",
3479 "rx_missed",
3480 "align_errors",
3481 "tx_single_collisions",
3482 "tx_multi_collisions",
3483 "rx_unicast",
3484 "rx_broadcast",
3485 "rx_multicast",
3486 "tx_aborted",
3487 "tx_underrun",
3488};
3489
3490static int rtl8152_get_sset_count(struct net_device *dev, int sset)
3491{
3492 switch (sset) {
3493 case ETH_SS_STATS:
3494 return ARRAY_SIZE(rtl8152_gstrings);
3495 default:
3496 return -EOPNOTSUPP;
3497 }
3498}
3499
3500static void rtl8152_get_ethtool_stats(struct net_device *dev,
3501 struct ethtool_stats *stats, u64 *data)
3502{
3503 struct r8152 *tp = netdev_priv(dev);
3504 struct tally_counter tally;
3505
0b030244 3506 if (usb_autopm_get_interface(tp->intf) < 0)
3507 return;
3508
4f1d4d54 3509 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
3510
0b030244 3511 usb_autopm_put_interface(tp->intf);
3512
4f1d4d54 3513 data[0] = le64_to_cpu(tally.tx_packets);
3514 data[1] = le64_to_cpu(tally.rx_packets);
3515 data[2] = le64_to_cpu(tally.tx_errors);
3516 data[3] = le32_to_cpu(tally.rx_errors);
3517 data[4] = le16_to_cpu(tally.rx_missed);
3518 data[5] = le16_to_cpu(tally.align_errors);
3519 data[6] = le32_to_cpu(tally.tx_one_collision);
3520 data[7] = le32_to_cpu(tally.tx_multi_collision);
3521 data[8] = le64_to_cpu(tally.rx_unicast);
3522 data[9] = le64_to_cpu(tally.rx_broadcast);
3523 data[10] = le32_to_cpu(tally.rx_multicast);
3524 data[11] = le16_to_cpu(tally.tx_aborted);
f37119c5 3525 data[12] = le16_to_cpu(tally.tx_underrun);
4f1d4d54 3526}
3527
3528static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3529{
3530 switch (stringset) {
3531 case ETH_SS_STATS:
3532 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
3533 break;
3534 }
3535}
3536
df35d283 3537static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3538{
3539 u32 ocp_data, lp, adv, supported = 0;
3540 u16 val;
3541
3542 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
3543 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3544
3545 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
3546 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3547
3548 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
3549 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3550
3551 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3552 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3553
3554 eee->eee_enabled = !!ocp_data;
3555 eee->eee_active = !!(supported & adv & lp);
3556 eee->supported = supported;
3557 eee->advertised = adv;
3558 eee->lp_advertised = lp;
3559
3560 return 0;
3561}
3562
3563static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3564{
3565 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3566
3567 r8152_eee_en(tp, eee->eee_enabled);
3568
3569 if (!eee->eee_enabled)
3570 val = 0;
3571
3572 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
3573
3574 return 0;
3575}
3576
3577static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3578{
3579 u32 ocp_data, lp, adv, supported = 0;
3580 u16 val;
3581
3582 val = ocp_reg_read(tp, OCP_EEE_ABLE);
3583 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3584
3585 val = ocp_reg_read(tp, OCP_EEE_ADV);
3586 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3587
3588 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
3589 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3590
3591 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3592 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3593
3594 eee->eee_enabled = !!ocp_data;
3595 eee->eee_active = !!(supported & adv & lp);
3596 eee->supported = supported;
3597 eee->advertised = adv;
3598 eee->lp_advertised = lp;
3599
3600 return 0;
3601}
3602
3603static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3604{
3605 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3606
3607 r8153_eee_en(tp, eee->eee_enabled);
3608
3609 if (!eee->eee_enabled)
3610 val = 0;
3611
3612 ocp_reg_write(tp, OCP_EEE_ADV, val);
3613
3614 return 0;
3615}
3616
3617static int
3618rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
3619{
3620 struct r8152 *tp = netdev_priv(net);
3621 int ret;
3622
3623 ret = usb_autopm_get_interface(tp->intf);
3624 if (ret < 0)
3625 goto out;
3626
b5403273 3627 mutex_lock(&tp->control);
3628
df35d283 3629 ret = tp->rtl_ops.eee_get(tp, edata);
3630
b5403273 3631 mutex_unlock(&tp->control);
3632
df35d283 3633 usb_autopm_put_interface(tp->intf);
3634
3635out:
3636 return ret;
3637}
3638
3639static int
3640rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
3641{
3642 struct r8152 *tp = netdev_priv(net);
3643 int ret;
3644
3645 ret = usb_autopm_get_interface(tp->intf);
3646 if (ret < 0)
3647 goto out;
3648
b5403273 3649 mutex_lock(&tp->control);
3650
df35d283 3651 ret = tp->rtl_ops.eee_set(tp, edata);
9d31a7b9 3652 if (!ret)
3653 ret = mii_nway_restart(&tp->mii);
df35d283 3654
b5403273 3655 mutex_unlock(&tp->control);
3656
df35d283 3657 usb_autopm_put_interface(tp->intf);
3658
3659out:
3660 return ret;
3661}
3662
8884f507 3663static int rtl8152_nway_reset(struct net_device *dev)
3664{
3665 struct r8152 *tp = netdev_priv(dev);
3666 int ret;
3667
3668 ret = usb_autopm_get_interface(tp->intf);
3669 if (ret < 0)
3670 goto out;
3671
3672 mutex_lock(&tp->control);
3673
3674 ret = mii_nway_restart(&tp->mii);
3675
3676 mutex_unlock(&tp->control);
3677
3678 usb_autopm_put_interface(tp->intf);
3679
3680out:
3681 return ret;
3682}
3683
ac718b69 3684static struct ethtool_ops ops = {
3685 .get_drvinfo = rtl8152_get_drvinfo,
3686 .get_settings = rtl8152_get_settings,
3687 .set_settings = rtl8152_set_settings,
3688 .get_link = ethtool_op_get_link,
8884f507 3689 .nway_reset = rtl8152_nway_reset,
a5ec27c1 3690 .get_msglevel = rtl8152_get_msglevel,
3691 .set_msglevel = rtl8152_set_msglevel,
21ff2e89 3692 .get_wol = rtl8152_get_wol,
3693 .set_wol = rtl8152_set_wol,
4f1d4d54 3694 .get_strings = rtl8152_get_strings,
3695 .get_sset_count = rtl8152_get_sset_count,
3696 .get_ethtool_stats = rtl8152_get_ethtool_stats,
df35d283 3697 .get_eee = rtl_ethtool_get_eee,
3698 .set_eee = rtl_ethtool_set_eee,
ac718b69 3699};
3700
3701static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
3702{
3703 struct r8152 *tp = netdev_priv(netdev);
3704 struct mii_ioctl_data *data = if_mii(rq);
9a4be1bd 3705 int res;
3706
6871438c 3707 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3708 return -ENODEV;
3709
9a4be1bd 3710 res = usb_autopm_get_interface(tp->intf);
3711 if (res < 0)
3712 goto out;
ac718b69 3713
3714 switch (cmd) {
3715 case SIOCGMIIPHY:
3716 data->phy_id = R8152_PHY_ID; /* Internal PHY */
3717 break;
3718
3719 case SIOCGMIIREG:
b5403273 3720 mutex_lock(&tp->control);
ac718b69 3721 data->val_out = r8152_mdio_read(tp, data->reg_num);
b5403273 3722 mutex_unlock(&tp->control);
ac718b69 3723 break;
3724
3725 case SIOCSMIIREG:
3726 if (!capable(CAP_NET_ADMIN)) {
3727 res = -EPERM;
3728 break;
3729 }
b5403273 3730 mutex_lock(&tp->control);
ac718b69 3731 r8152_mdio_write(tp, data->reg_num, data->val_in);
b5403273 3732 mutex_unlock(&tp->control);
ac718b69 3733 break;
3734
3735 default:
3736 res = -EOPNOTSUPP;
3737 }
3738
9a4be1bd 3739 usb_autopm_put_interface(tp->intf);
3740
3741out:
ac718b69 3742 return res;
3743}
3744
69b4b7a4 3745static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
3746{
3747 struct r8152 *tp = netdev_priv(dev);
3748
3749 switch (tp->version) {
3750 case RTL_VER_01:
3751 case RTL_VER_02:
3752 return eth_change_mtu(dev, new_mtu);
3753 default:
3754 break;
3755 }
3756
3757 if (new_mtu < 68 || new_mtu > RTL8153_MAX_MTU)
3758 return -EINVAL;
3759
3760 dev->mtu = new_mtu;
3761
3762 return 0;
3763}
3764
ac718b69 3765static const struct net_device_ops rtl8152_netdev_ops = {
3766 .ndo_open = rtl8152_open,
3767 .ndo_stop = rtl8152_close,
3768 .ndo_do_ioctl = rtl8152_ioctl,
3769 .ndo_start_xmit = rtl8152_start_xmit,
3770 .ndo_tx_timeout = rtl8152_tx_timeout,
c5554298 3771 .ndo_set_features = rtl8152_set_features,
ac718b69 3772 .ndo_set_rx_mode = rtl8152_set_rx_mode,
3773 .ndo_set_mac_address = rtl8152_set_mac_address,
69b4b7a4 3774 .ndo_change_mtu = rtl8152_change_mtu,
ac718b69 3775 .ndo_validate_addr = eth_validate_addr,
a5e31255 3776 .ndo_features_check = rtl8152_features_check,
ac718b69 3777};
3778
3779static void r8152b_get_version(struct r8152 *tp)
3780{
3781 u32 ocp_data;
3782 u16 version;
3783
3784 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3785 version = (u16)(ocp_data & VERSION_MASK);
3786
3787 switch (version) {
3788 case 0x4c00:
3789 tp->version = RTL_VER_01;
3790 break;
3791 case 0x4c10:
3792 tp->version = RTL_VER_02;
3793 break;
43779f8d 3794 case 0x5c00:
3795 tp->version = RTL_VER_03;
3796 tp->mii.supports_gmii = 1;
3797 break;
3798 case 0x5c10:
3799 tp->version = RTL_VER_04;
3800 tp->mii.supports_gmii = 1;
3801 break;
3802 case 0x5c20:
3803 tp->version = RTL_VER_05;
3804 tp->mii.supports_gmii = 1;
3805 break;
ac718b69 3806 default:
3807 netif_info(tp, probe, tp->netdev,
3808 "Unknown version 0x%04x\n", version);
3809 break;
3810 }
3811}
3812
e3fe0b1a 3813static void rtl8152_unload(struct r8152 *tp)
3814{
6871438c 3815 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3816 return;
3817
00a5e360 3818 if (tp->version != RTL_VER_01)
3819 r8152_power_cut_en(tp, true);
e3fe0b1a 3820}
3821
43779f8d 3822static void rtl8153_unload(struct r8152 *tp)
3823{
6871438c 3824 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3825 return;
3826
49be1723 3827 r8153_power_cut_en(tp, false);
43779f8d 3828}
3829
55b65475 3830static int rtl_ops_init(struct r8152 *tp)
c81229c9 3831{
3832 struct rtl_ops *ops = &tp->rtl_ops;
55b65475 3833 int ret = 0;
3834
3835 switch (tp->version) {
3836 case RTL_VER_01:
3837 case RTL_VER_02:
3838 ops->init = r8152b_init;
3839 ops->enable = rtl8152_enable;
3840 ops->disable = rtl8152_disable;
3841 ops->up = rtl8152_up;
3842 ops->down = rtl8152_down;
3843 ops->unload = rtl8152_unload;
3844 ops->eee_get = r8152_get_eee;
3845 ops->eee_set = r8152_set_eee;
43779f8d 3846 break;
3847
55b65475 3848 case RTL_VER_03:
3849 case RTL_VER_04:
3850 case RTL_VER_05:
3851 ops->init = r8153_init;
3852 ops->enable = rtl8153_enable;
3853 ops->disable = rtl8153_disable;
3854 ops->up = rtl8153_up;
3855 ops->down = rtl8153_down;
3856 ops->unload = rtl8153_unload;
3857 ops->eee_get = r8153_get_eee;
3858 ops->eee_set = r8153_set_eee;
c81229c9 3859 break;
3860
3861 default:
55b65475 3862 ret = -ENODEV;
3863 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
c81229c9 3864 break;
3865 }
3866
3867 return ret;
3868}
3869
ac718b69 3870static int rtl8152_probe(struct usb_interface *intf,
3871 const struct usb_device_id *id)
3872{
3873 struct usb_device *udev = interface_to_usbdev(intf);
3874 struct r8152 *tp;
3875 struct net_device *netdev;
ebc2ec48 3876 int ret;
ac718b69 3877
10c32717 3878 if (udev->actconfig->desc.bConfigurationValue != 1) {
3879 usb_driver_set_configuration(udev, 1);
3880 return -ENODEV;
3881 }
3882
3883 usb_reset_device(udev);
ac718b69 3884 netdev = alloc_etherdev(sizeof(struct r8152));
3885 if (!netdev) {
4a8deae2 3886 dev_err(&intf->dev, "Out of memory\n");
ac718b69 3887 return -ENOMEM;
3888 }
3889
ebc2ec48 3890 SET_NETDEV_DEV(netdev, &intf->dev);
ac718b69 3891 tp = netdev_priv(netdev);
3892 tp->msg_enable = 0x7FFF;
3893
e3ad412a 3894 tp->udev = udev;
3895 tp->netdev = netdev;
3896 tp->intf = intf;
3897
82cf94cb 3898 r8152b_get_version(tp);
55b65475 3899 ret = rtl_ops_init(tp);
31ca1dec 3900 if (ret)
3901 goto out;
c81229c9 3902
b5403273 3903 mutex_init(&tp->control);
ac718b69 3904 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3905
ac718b69 3906 netdev->netdev_ops = &rtl8152_netdev_ops;
3907 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
5bd23881 3908
60c89071 3909 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
6128d1bb 3910 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
c5554298 3911 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
3912 NETIF_F_HW_VLAN_CTAG_TX;
60c89071 3913 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
6128d1bb 3914 NETIF_F_TSO | NETIF_F_FRAGLIST |
c5554298 3915 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
3916 NETIF_F_HW_VLAN_CTAG_RX |
3917 NETIF_F_HW_VLAN_CTAG_TX;
3918 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3919 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
3920 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
db8515ef 3921
7ad24ea4 3922 netdev->ethtool_ops = &ops;
60c89071 3923 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
ac718b69 3924
3925 tp->mii.dev = netdev;
3926 tp->mii.mdio_read = read_mii_word;
3927 tp->mii.mdio_write = write_mii_word;
3928 tp->mii.phy_id_mask = 0x3f;
3929 tp->mii.reg_num_mask = 0x1f;
3930 tp->mii.phy_id = R8152_PHY_ID;
ac718b69 3931
9a4be1bd 3932 intf->needs_remote_wakeup = 1;
3933
c81229c9 3934 tp->rtl_ops.init(tp);
ac718b69 3935 set_ethernet_addr(tp);
3936
ac718b69 3937 usb_set_intfdata(intf, tp);
d823ab68 3938 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
ac718b69 3939
ebc2ec48 3940 ret = register_netdev(netdev);
3941 if (ret != 0) {
4a8deae2 3942 netif_err(tp, probe, netdev, "couldn't register the device\n");
ebc2ec48 3943 goto out1;
ac718b69 3944 }
3945
21ff2e89 3946 tp->saved_wolopts = __rtl_get_wol(tp);
3947 if (tp->saved_wolopts)
3948 device_set_wakeup_enable(&udev->dev, true);
3949 else
3950 device_set_wakeup_enable(&udev->dev, false);
3951
4a8deae2 3952 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
ac718b69 3953
3954 return 0;
3955
ac718b69 3956out1:
d823ab68 3957 netif_napi_del(&tp->napi);
ebc2ec48 3958 usb_set_intfdata(intf, NULL);
ac718b69 3959out:
3960 free_netdev(netdev);
ebc2ec48 3961 return ret;
ac718b69 3962}
3963
ac718b69 3964static void rtl8152_disconnect(struct usb_interface *intf)
3965{
3966 struct r8152 *tp = usb_get_intfdata(intf);
3967
3968 usb_set_intfdata(intf, NULL);
3969 if (tp) {
f561de33 3970 struct usb_device *udev = tp->udev;
3971
3972 if (udev->state == USB_STATE_NOTATTACHED)
3973 set_bit(RTL8152_UNPLUG, &tp->flags);
3974
d823ab68 3975 netif_napi_del(&tp->napi);
ac718b69 3976 unregister_netdev(tp->netdev);
c81229c9 3977 tp->rtl_ops.unload(tp);
ac718b69 3978 free_netdev(tp->netdev);
3979 }
3980}
3981
d9a28c5b 3982#define REALTEK_USB_DEVICE(vend, prod) \
3983 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
3984 USB_DEVICE_ID_MATCH_INT_CLASS, \
3985 .idVendor = (vend), \
3986 .idProduct = (prod), \
3987 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
3988}, \
3989{ \
3990 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
3991 USB_DEVICE_ID_MATCH_DEVICE, \
3992 .idVendor = (vend), \
3993 .idProduct = (prod), \
3994 .bInterfaceClass = USB_CLASS_COMM, \
3995 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
3996 .bInterfaceProtocol = USB_CDC_PROTO_NONE
3997
ac718b69 3998/* table of devices that work with this driver */
3999static struct usb_device_id rtl8152_table[] = {
d9a28c5b 4000 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
4001 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
4002 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
ac718b69 4003 {}
4004};
4005
4006MODULE_DEVICE_TABLE(usb, rtl8152_table);
4007
4008static struct usb_driver rtl8152_driver = {
4009 .name = MODULENAME,
ebc2ec48 4010 .id_table = rtl8152_table,
ac718b69 4011 .probe = rtl8152_probe,
4012 .disconnect = rtl8152_disconnect,
ac718b69 4013 .suspend = rtl8152_suspend,
ebc2ec48 4014 .resume = rtl8152_resume,
4015 .reset_resume = rtl8152_resume,
9a4be1bd 4016 .supports_autosuspend = 1,
a634782f 4017 .disable_hub_initiated_lpm = 1,
ac718b69 4018};
4019
b4236daa 4020module_usb_driver(rtl8152_driver);
ac718b69 4021
4022MODULE_AUTHOR(DRIVER_AUTHOR);
4023MODULE_DESCRIPTION(DRIVER_DESC);
4024MODULE_LICENSE("GPL");