blob: b8bc3eb727219dd1e85184a2d1cdb97ef5757deb [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
2 * Copyright (c) 2013 Realtek Semiconductor Corp. All rights reserved.
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
10#include <linux/init.h>
11#include <linux/signal.h>
12#include <linux/slab.h>
13#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000014#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/mii.h>
17#include <linux/ethtool.h>
18#include <linux/usb.h>
19#include <linux/crc32.h>
20#include <linux/if_vlan.h>
21#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080022#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080023#include <linux/ip.h>
24#include <linux/ipv6.h>
hayeswangac718b62013-05-02 16:01:25 +000025
26/* Version Information */
hayeswang7937f9e2013-11-20 17:30:54 +080027#define DRIVER_VERSION "v1.02.0 (2013/10/28)"
hayeswangac718b62013-05-02 16:01:25 +000028#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
29#define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters"
30#define MODULENAME "r8152"
31
32#define R8152_PHY_ID 32
33
34#define PLA_IDR 0xc000
35#define PLA_RCR 0xc010
36#define PLA_RMS 0xc016
37#define PLA_RXFIFO_CTRL0 0xc0a0
38#define PLA_RXFIFO_CTRL1 0xc0a4
39#define PLA_RXFIFO_CTRL2 0xc0a8
40#define PLA_FMC 0xc0b4
41#define PLA_CFG_WOL 0xc0b6
42#define PLA_MAR 0xcd00
43#define PAL_BDC_CR 0xd1a0
44#define PLA_LEDSEL 0xdd90
45#define PLA_LED_FEATURE 0xdd92
46#define PLA_PHYAR 0xde00
47#define PLA_GPHY_INTR_IMR 0xe022
48#define PLA_EEE_CR 0xe040
49#define PLA_EEEP_CR 0xe080
50#define PLA_MAC_PWR_CTRL 0xe0c0
51#define PLA_TCR0 0xe610
52#define PLA_TCR1 0xe612
53#define PLA_TXFIFO_CTRL 0xe618
54#define PLA_RSTTELLY 0xe800
55#define PLA_CR 0xe813
56#define PLA_CRWECR 0xe81c
57#define PLA_CONFIG5 0xe822
58#define PLA_PHY_PWR 0xe84c
59#define PLA_OOB_CTRL 0xe84f
60#define PLA_CPCR 0xe854
61#define PLA_MISC_0 0xe858
62#define PLA_MISC_1 0xe85a
63#define PLA_OCP_GPHY_BASE 0xe86c
64#define PLA_TELLYCNT 0xe890
65#define PLA_SFF_STS_7 0xe8de
66#define PLA_PHYSTATUS 0xe908
67#define PLA_BP_BA 0xfc26
68#define PLA_BP_0 0xfc28
69#define PLA_BP_1 0xfc2a
70#define PLA_BP_2 0xfc2c
71#define PLA_BP_3 0xfc2e
72#define PLA_BP_4 0xfc30
73#define PLA_BP_5 0xfc32
74#define PLA_BP_6 0xfc34
75#define PLA_BP_7 0xfc36
76
77#define USB_DEV_STAT 0xb808
78#define USB_USB_CTRL 0xd406
79#define USB_PHY_CTRL 0xd408
80#define USB_TX_AGG 0xd40a
81#define USB_RX_BUF_TH 0xd40c
82#define USB_USB_TIMER 0xd428
83#define USB_PM_CTRL_STATUS 0xd432
84#define USB_TX_DMA 0xd434
85#define USB_UPS_CTRL 0xd800
86#define USB_BP_BA 0xfc26
87#define USB_BP_0 0xfc28
88#define USB_BP_1 0xfc2a
89#define USB_BP_2 0xfc2c
90#define USB_BP_3 0xfc2e
91#define USB_BP_4 0xfc30
92#define USB_BP_5 0xfc32
93#define USB_BP_6 0xfc34
94#define USB_BP_7 0xfc36
95
96/* OCP Registers */
97#define OCP_ALDPS_CONFIG 0x2010
98#define OCP_EEE_CONFIG1 0x2080
99#define OCP_EEE_CONFIG2 0x2092
100#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800101#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000102#define OCP_EEE_AR 0xa41a
103#define OCP_EEE_DATA 0xa41c
104
105/* PLA_RCR */
106#define RCR_AAP 0x00000001
107#define RCR_APM 0x00000002
108#define RCR_AM 0x00000004
109#define RCR_AB 0x00000008
110#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
111
112/* PLA_RXFIFO_CTRL0 */
113#define RXFIFO_THR1_NORMAL 0x00080002
114#define RXFIFO_THR1_OOB 0x01800003
115
116/* PLA_RXFIFO_CTRL1 */
117#define RXFIFO_THR2_FULL 0x00000060
118#define RXFIFO_THR2_HIGH 0x00000038
119#define RXFIFO_THR2_OOB 0x0000004a
120
121/* PLA_RXFIFO_CTRL2 */
122#define RXFIFO_THR3_FULL 0x00000078
123#define RXFIFO_THR3_HIGH 0x00000048
124#define RXFIFO_THR3_OOB 0x0000005a
125
126/* PLA_TXFIFO_CTRL */
127#define TXFIFO_THR_NORMAL 0x00400008
128
129/* PLA_FMC */
130#define FMC_FCR_MCU_EN 0x0001
131
132/* PLA_EEEP_CR */
133#define EEEP_CR_EEEP_TX 0x0002
134
135/* PLA_TCR0 */
136#define TCR0_TX_EMPTY 0x0800
137#define TCR0_AUTO_FIFO 0x0080
138
139/* PLA_TCR1 */
140#define VERSION_MASK 0x7cf0
141
142/* PLA_CR */
143#define CR_RST 0x10
144#define CR_RE 0x08
145#define CR_TE 0x04
146
147/* PLA_CRWECR */
148#define CRWECR_NORAML 0x00
149#define CRWECR_CONFIG 0xc0
150
151/* PLA_OOB_CTRL */
152#define NOW_IS_OOB 0x80
153#define TXFIFO_EMPTY 0x20
154#define RXFIFO_EMPTY 0x10
155#define LINK_LIST_READY 0x02
156#define DIS_MCU_CLROOB 0x01
157#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
158
159/* PLA_MISC_1 */
160#define RXDY_GATED_EN 0x0008
161
162/* PLA_SFF_STS_7 */
163#define RE_INIT_LL 0x8000
164#define MCU_BORW_EN 0x4000
165
166/* PLA_CPCR */
167#define CPCR_RX_VLAN 0x0040
168
169/* PLA_CFG_WOL */
170#define MAGIC_EN 0x0001
171
172/* PAL_BDC_CR */
173#define ALDPS_PROXY_MODE 0x0001
174
175/* PLA_CONFIG5 */
176#define LAN_WAKE_EN 0x0002
177
178/* PLA_LED_FEATURE */
179#define LED_MODE_MASK 0x0700
180
181/* PLA_PHY_PWR */
182#define TX_10M_IDLE_EN 0x0080
183#define PFM_PWM_SWITCH 0x0040
184
185/* PLA_MAC_PWR_CTRL */
186#define D3_CLK_GATED_EN 0x00004000
187#define MCU_CLK_RATIO 0x07010f07
188#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
189
190/* PLA_GPHY_INTR_IMR */
191#define GPHY_STS_MSK 0x0001
192#define SPEED_DOWN_MSK 0x0002
193#define SPDWN_RXDV_MSK 0x0004
194#define SPDWN_LINKCHG_MSK 0x0008
195
196/* PLA_PHYAR */
197#define PHYAR_FLAG 0x80000000
198
199/* PLA_EEE_CR */
200#define EEE_RX_EN 0x0001
201#define EEE_TX_EN 0x0002
202
203/* USB_DEV_STAT */
204#define STAT_SPEED_MASK 0x0006
205#define STAT_SPEED_HIGH 0x0000
206#define STAT_SPEED_FULL 0x0001
207
208/* USB_TX_AGG */
209#define TX_AGG_MAX_THRESHOLD 0x03
210
211/* USB_RX_BUF_TH */
212#define RX_BUF_THR 0x7a120180
213
214/* USB_TX_DMA */
215#define TEST_MODE_DISABLE 0x00000001
216#define TX_SIZE_ADJUST1 0x00000100
217
218/* USB_UPS_CTRL */
219#define POWER_CUT 0x0100
220
221/* USB_PM_CTRL_STATUS */
222#define RWSUME_INDICATE 0x0001
223
224/* USB_USB_CTRL */
225#define RX_AGG_DISABLE 0x0010
226
227/* OCP_ALDPS_CONFIG */
228#define ENPWRSAVE 0x8000
229#define ENPDNPS 0x0200
230#define LINKENA 0x0100
231#define DIS_SDSAVE 0x0010
232
233/* OCP_EEE_CONFIG1 */
234#define RG_TXLPI_MSK_HFDUP 0x8000
235#define RG_MATCLR_EN 0x4000
236#define EEE_10_CAP 0x2000
237#define EEE_NWAY_EN 0x1000
238#define TX_QUIET_EN 0x0200
239#define RX_QUIET_EN 0x0100
240#define SDRISETIME 0x0010 /* bit 4 ~ 6 */
241#define RG_RXLPI_MSK_HFDUP 0x0008
242#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
243
244/* OCP_EEE_CONFIG2 */
245#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
246#define RG_DACQUIET_EN 0x0400
247#define RG_LDVQUIET_EN 0x0200
248#define RG_CKRSEL 0x0020
249#define RG_EEEPRG_EN 0x0010
250
251/* OCP_EEE_CONFIG3 */
252#define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
253#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
254#define MSK_PH 0x0006 /* bit 0 ~ 3 */
255
256/* OCP_EEE_AR */
257/* bit[15:14] function */
258#define FUN_ADDR 0x0000
259#define FUN_DATA 0x4000
260/* bit[4:0] device addr */
261#define DEVICE_ADDR 0x0007
262
263/* OCP_EEE_DATA */
264#define EEE_ADDR 0x003C
265#define EEE_DATA 0x0002
266
267enum rtl_register_content {
268 _100bps = 0x08,
269 _10bps = 0x04,
270 LINK_STATUS = 0x02,
271 FULL_DUP = 0x01,
272};
273
hayeswangebc2ec482013-08-14 20:54:38 +0800274#define RTL8152_MAX_TX 10
275#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800276#define INTBUFSIZE 2
277
278#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800279
hayeswangac718b62013-05-02 16:01:25 +0000280#define RTL8152_REQT_READ 0xc0
281#define RTL8152_REQT_WRITE 0x40
282#define RTL8152_REQ_GET_REGS 0x05
283#define RTL8152_REQ_SET_REGS 0x05
284
285#define BYTE_EN_DWORD 0xff
286#define BYTE_EN_WORD 0x33
287#define BYTE_EN_BYTE 0x11
288#define BYTE_EN_SIX_BYTES 0x3f
289#define BYTE_EN_START_MASK 0x0f
290#define BYTE_EN_END_MASK 0xf0
291
292#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
293#define RTL8152_TX_TIMEOUT (HZ)
294
295/* rtl8152 flags */
296enum rtl8152_flags {
297 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000298 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800299 WORK_ENABLE,
300 RTL8152_LINK_CHG,
hayeswangac718b62013-05-02 16:01:25 +0000301};
302
303/* Define these values to match your device */
304#define VENDOR_ID_REALTEK 0x0bda
305#define PRODUCT_ID_RTL8152 0x8152
306
307#define MCU_TYPE_PLA 0x0100
308#define MCU_TYPE_USB 0x0000
309
310struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800311 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000312#define RX_LEN_MASK 0x7fff
hayeswang500b6d72013-11-20 17:30:57 +0800313 __le32 opts2;
314 __le32 opts3;
315 __le32 opts4;
316 __le32 opts5;
317 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000318};
319
320struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800321 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000322#define TX_FS (1 << 31) /* First segment of a packet */
323#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang5bd23882013-08-14 20:54:39 +0800324#define TX_LEN_MASK 0x3ffff
325
hayeswang500b6d72013-11-20 17:30:57 +0800326 __le32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800327#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
328#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
329#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
330#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswangac718b62013-05-02 16:01:25 +0000331};
332
hayeswangdff4e8a2013-08-16 16:09:33 +0800333struct r8152;
334
hayeswangebc2ec482013-08-14 20:54:38 +0800335struct rx_agg {
336 struct list_head list;
337 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800338 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800339 void *buffer;
340 void *head;
341};
342
343struct tx_agg {
344 struct list_head list;
345 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800346 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800347 void *buffer;
348 void *head;
349 u32 skb_num;
350 u32 skb_len;
351};
352
hayeswangac718b62013-05-02 16:01:25 +0000353struct r8152 {
354 unsigned long flags;
355 struct usb_device *udev;
356 struct tasklet_struct tl;
hayeswang40a82912013-08-14 20:54:40 +0800357 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000358 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800359 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800360 struct tx_agg tx_info[RTL8152_MAX_TX];
361 struct rx_agg rx_info[RTL8152_MAX_RX];
362 struct list_head rx_done, tx_free;
363 struct sk_buff_head tx_queue;
364 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000365 struct delayed_work schedule;
366 struct mii_if_info mii;
hayeswang40a82912013-08-14 20:54:40 +0800367 int intr_interval;
hayeswangac718b62013-05-02 16:01:25 +0000368 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800369 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000370 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800371 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000372 u8 version;
373 u8 speed;
374};
375
376enum rtl_version {
377 RTL_VER_UNKNOWN = 0,
378 RTL_VER_01,
379 RTL_VER_02
380};
381
382/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
383 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
384 */
385static const int multicast_filter_limit = 32;
hayeswangebc2ec482013-08-14 20:54:38 +0800386static unsigned int rx_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000387
388static
389int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
390{
hayeswang31787f52013-07-31 17:21:25 +0800391 int ret;
392 void *tmp;
393
394 tmp = kmalloc(size, GFP_KERNEL);
395 if (!tmp)
396 return -ENOMEM;
397
398 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000399 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
hayeswang31787f52013-07-31 17:21:25 +0800400 value, index, tmp, size, 500);
401
402 memcpy(data, tmp, size);
403 kfree(tmp);
404
405 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000406}
407
408static
409int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
410{
hayeswang31787f52013-07-31 17:21:25 +0800411 int ret;
412 void *tmp;
413
414 tmp = kmalloc(size, GFP_KERNEL);
415 if (!tmp)
416 return -ENOMEM;
417
418 memcpy(tmp, data, size);
419
420 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000421 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
hayeswang31787f52013-07-31 17:21:25 +0800422 value, index, tmp, size, 500);
423
424 kfree(tmp);
425 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000426}
427
428static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
429 void *data, u16 type)
430{
431 u16 limit = 64;
432 int ret = 0;
433
434 if (test_bit(RTL8152_UNPLUG, &tp->flags))
435 return -ENODEV;
436
437 /* both size and indix must be 4 bytes align */
438 if ((size & 3) || !size || (index & 3) || !data)
439 return -EPERM;
440
441 if ((u32)index + (u32)size > 0xffff)
442 return -EPERM;
443
444 while (size) {
445 if (size > limit) {
446 ret = get_registers(tp, index, type, limit, data);
447 if (ret < 0)
448 break;
449
450 index += limit;
451 data += limit;
452 size -= limit;
453 } else {
454 ret = get_registers(tp, index, type, size, data);
455 if (ret < 0)
456 break;
457
458 index += size;
459 data += size;
460 size = 0;
461 break;
462 }
463 }
464
465 return ret;
466}
467
468static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
469 u16 size, void *data, u16 type)
470{
471 int ret;
472 u16 byteen_start, byteen_end, byen;
473 u16 limit = 512;
474
475 if (test_bit(RTL8152_UNPLUG, &tp->flags))
476 return -ENODEV;
477
478 /* both size and indix must be 4 bytes align */
479 if ((size & 3) || !size || (index & 3) || !data)
480 return -EPERM;
481
482 if ((u32)index + (u32)size > 0xffff)
483 return -EPERM;
484
485 byteen_start = byteen & BYTE_EN_START_MASK;
486 byteen_end = byteen & BYTE_EN_END_MASK;
487
488 byen = byteen_start | (byteen_start << 4);
489 ret = set_registers(tp, index, type | byen, 4, data);
490 if (ret < 0)
491 goto error1;
492
493 index += 4;
494 data += 4;
495 size -= 4;
496
497 if (size) {
498 size -= 4;
499
500 while (size) {
501 if (size > limit) {
502 ret = set_registers(tp, index,
503 type | BYTE_EN_DWORD,
504 limit, data);
505 if (ret < 0)
506 goto error1;
507
508 index += limit;
509 data += limit;
510 size -= limit;
511 } else {
512 ret = set_registers(tp, index,
513 type | BYTE_EN_DWORD,
514 size, data);
515 if (ret < 0)
516 goto error1;
517
518 index += size;
519 data += size;
520 size = 0;
521 break;
522 }
523 }
524
525 byen = byteen_end | (byteen_end >> 4);
526 ret = set_registers(tp, index, type | byen, 4, data);
527 if (ret < 0)
528 goto error1;
529 }
530
531error1:
532 return ret;
533}
534
535static inline
536int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
537{
538 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
539}
540
541static inline
542int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
543{
544 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
545}
546
547static inline
548int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
549{
550 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
551}
552
553static inline
554int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
555{
556 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
557}
558
559static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
560{
hayeswangc8826de2013-07-31 17:21:26 +0800561 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000562
hayeswangc8826de2013-07-31 17:21:26 +0800563 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000564
565 return __le32_to_cpu(data);
566}
567
568static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
569{
hayeswangc8826de2013-07-31 17:21:26 +0800570 __le32 tmp = __cpu_to_le32(data);
571
572 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000573}
574
575static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
576{
577 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800578 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000579 u8 shift = index & 2;
580
581 index &= ~3;
582
hayeswangc8826de2013-07-31 17:21:26 +0800583 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000584
hayeswangc8826de2013-07-31 17:21:26 +0800585 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000586 data >>= (shift * 8);
587 data &= 0xffff;
588
589 return (u16)data;
590}
591
592static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
593{
hayeswangc8826de2013-07-31 17:21:26 +0800594 u32 mask = 0xffff;
595 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000596 u16 byen = BYTE_EN_WORD;
597 u8 shift = index & 2;
598
599 data &= mask;
600
601 if (index & 2) {
602 byen <<= shift;
603 mask <<= (shift * 8);
604 data <<= (shift * 8);
605 index &= ~3;
606 }
607
hayeswangc8826de2013-07-31 17:21:26 +0800608 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000609
hayeswangc8826de2013-07-31 17:21:26 +0800610 data |= __le32_to_cpu(tmp) & ~mask;
611 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000612
hayeswangc8826de2013-07-31 17:21:26 +0800613 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000614}
615
616static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
617{
618 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800619 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000620 u8 shift = index & 3;
621
622 index &= ~3;
623
hayeswangc8826de2013-07-31 17:21:26 +0800624 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000625
hayeswangc8826de2013-07-31 17:21:26 +0800626 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000627 data >>= (shift * 8);
628 data &= 0xff;
629
630 return (u8)data;
631}
632
633static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
634{
hayeswangc8826de2013-07-31 17:21:26 +0800635 u32 mask = 0xff;
636 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000637 u16 byen = BYTE_EN_BYTE;
638 u8 shift = index & 3;
639
640 data &= mask;
641
642 if (index & 3) {
643 byen <<= shift;
644 mask <<= (shift * 8);
645 data <<= (shift * 8);
646 index &= ~3;
647 }
648
hayeswangc8826de2013-07-31 17:21:26 +0800649 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000650
hayeswangc8826de2013-07-31 17:21:26 +0800651 data |= __le32_to_cpu(tmp) & ~mask;
652 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000653
hayeswangc8826de2013-07-31 17:21:26 +0800654 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000655}
656
hayeswangac244d32014-01-02 11:22:40 +0800657static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
658{
659 u16 ocp_base, ocp_index;
660
661 ocp_base = addr & 0xf000;
662 if (ocp_base != tp->ocp_base) {
663 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
664 tp->ocp_base = ocp_base;
665 }
666
667 ocp_index = (addr & 0x0fff) | 0xb000;
668 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
669}
670
hayeswange3fe0b12014-01-02 11:22:39 +0800671static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
672{
673 u16 ocp_base, ocp_index;
674
675 ocp_base = addr & 0xf000;
676 if (ocp_base != tp->ocp_base) {
677 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
678 tp->ocp_base = ocp_base;
679 }
680
681 ocp_index = (addr & 0x0fff) | 0xb000;
682 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
683}
684
hayeswangac244d32014-01-02 11:22:40 +0800685static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000686{
hayeswangac244d32014-01-02 11:22:40 +0800687 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000688}
689
hayeswangac244d32014-01-02 11:22:40 +0800690static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000691{
hayeswangac244d32014-01-02 11:22:40 +0800692 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000693}
694
695static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
696{
697 struct r8152 *tp = netdev_priv(netdev);
698
699 if (phy_id != R8152_PHY_ID)
700 return -EINVAL;
701
702 return r8152_mdio_read(tp, reg);
703}
704
705static
706void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
707{
708 struct r8152 *tp = netdev_priv(netdev);
709
710 if (phy_id != R8152_PHY_ID)
711 return;
712
713 r8152_mdio_write(tp, reg, val);
714}
715
hayeswangebc2ec482013-08-14 20:54:38 +0800716static
717int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
718
hayeswangac718b62013-05-02 16:01:25 +0000719static inline void set_ethernet_addr(struct r8152 *tp)
720{
721 struct net_device *dev = tp->netdev;
hayeswang31787f52013-07-31 17:21:25 +0800722 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000723
hayeswang31787f52013-07-31 17:21:25 +0800724 if (pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id) < 0)
hayeswangac718b62013-05-02 16:01:25 +0000725 netif_notice(tp, probe, dev, "inet addr fail\n");
726 else {
727 memcpy(dev->dev_addr, node_id, dev->addr_len);
728 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
729 }
hayeswangac718b62013-05-02 16:01:25 +0000730}
731
732static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
733{
734 struct r8152 *tp = netdev_priv(netdev);
735 struct sockaddr *addr = p;
736
737 if (!is_valid_ether_addr(addr->sa_data))
738 return -EADDRNOTAVAIL;
739
740 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
741
742 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
743 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
744 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
745
746 return 0;
747}
748
hayeswangac718b62013-05-02 16:01:25 +0000749static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
750{
751 return &dev->stats;
752}
753
754static void read_bulk_callback(struct urb *urb)
755{
hayeswangac718b62013-05-02 16:01:25 +0000756 struct net_device *netdev;
hayeswanga5a4f462013-08-16 16:09:34 +0800757 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +0000758 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +0800759 struct rx_agg *agg;
760 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000761 int result;
hayeswangac718b62013-05-02 16:01:25 +0000762
hayeswangebc2ec482013-08-14 20:54:38 +0800763 agg = urb->context;
764 if (!agg)
765 return;
766
767 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000768 if (!tp)
769 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800770
hayeswangac718b62013-05-02 16:01:25 +0000771 if (test_bit(RTL8152_UNPLUG, &tp->flags))
772 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800773
774 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000775 return;
776
hayeswangebc2ec482013-08-14 20:54:38 +0800777 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +0800778
779 /* When link down, the driver would cancel all bulks. */
780 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +0800781 if (!netif_carrier_ok(netdev))
782 return;
783
hayeswangac718b62013-05-02 16:01:25 +0000784 switch (status) {
785 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +0800786 if (urb->actual_length < ETH_ZLEN)
787 break;
788
hayeswanga5a4f462013-08-16 16:09:34 +0800789 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800790 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +0800791 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800792 tasklet_schedule(&tp->tl);
793 return;
hayeswangac718b62013-05-02 16:01:25 +0000794 case -ESHUTDOWN:
795 set_bit(RTL8152_UNPLUG, &tp->flags);
796 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +0800797 return;
hayeswangac718b62013-05-02 16:01:25 +0000798 case -ENOENT:
799 return; /* the urb is in unlink state */
800 case -ETIME:
801 pr_warn_ratelimited("may be reset is needed?..\n");
hayeswangebc2ec482013-08-14 20:54:38 +0800802 break;
hayeswangac718b62013-05-02 16:01:25 +0000803 default:
804 pr_warn_ratelimited("Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +0800805 break;
hayeswangac718b62013-05-02 16:01:25 +0000806 }
807
hayeswangebc2ec482013-08-14 20:54:38 +0800808 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +0000809 if (result == -ENODEV) {
810 netif_device_detach(tp->netdev);
811 } else if (result) {
hayeswanga5a4f462013-08-16 16:09:34 +0800812 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800813 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +0800814 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800815 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +0000816 }
hayeswangac718b62013-05-02 16:01:25 +0000817}
818
819static void write_bulk_callback(struct urb *urb)
820{
hayeswangebc2ec482013-08-14 20:54:38 +0800821 struct net_device_stats *stats;
hayeswanga5a4f462013-08-16 16:09:34 +0800822 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +0800823 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +0000824 struct r8152 *tp;
825 int status = urb->status;
826
hayeswangebc2ec482013-08-14 20:54:38 +0800827 agg = urb->context;
828 if (!agg)
829 return;
830
831 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000832 if (!tp)
833 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800834
835 stats = rtl8152_get_stats(tp->netdev);
836 if (status) {
837 pr_warn_ratelimited("Tx status %d\n", status);
838 stats->tx_errors += agg->skb_num;
839 } else {
840 stats->tx_packets += agg->skb_num;
841 stats->tx_bytes += agg->skb_len;
842 }
843
hayeswanga5a4f462013-08-16 16:09:34 +0800844 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800845 list_add_tail(&agg->list, &tp->tx_free);
hayeswanga5a4f462013-08-16 16:09:34 +0800846 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800847
848 if (!netif_carrier_ok(tp->netdev))
hayeswangac718b62013-05-02 16:01:25 +0000849 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800850
851 if (!test_bit(WORK_ENABLE, &tp->flags))
852 return;
853
854 if (test_bit(RTL8152_UNPLUG, &tp->flags))
855 return;
856
857 if (!skb_queue_empty(&tp->tx_queue))
858 tasklet_schedule(&tp->tl);
859}
860
hayeswang40a82912013-08-14 20:54:40 +0800861static void intr_callback(struct urb *urb)
862{
863 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +0800864 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +0800865 int status = urb->status;
866 int res;
867
868 tp = urb->context;
869 if (!tp)
870 return;
871
872 if (!test_bit(WORK_ENABLE, &tp->flags))
873 return;
874
875 if (test_bit(RTL8152_UNPLUG, &tp->flags))
876 return;
877
878 switch (status) {
879 case 0: /* success */
880 break;
881 case -ECONNRESET: /* unlink */
882 case -ESHUTDOWN:
883 netif_device_detach(tp->netdev);
884 case -ENOENT:
885 return;
886 case -EOVERFLOW:
887 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
888 goto resubmit;
889 /* -EPIPE: should clear the halt */
890 default:
891 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
892 goto resubmit;
893 }
894
895 d = urb->transfer_buffer;
896 if (INTR_LINK & __le16_to_cpu(d[0])) {
897 if (!(tp->speed & LINK_STATUS)) {
898 set_bit(RTL8152_LINK_CHG, &tp->flags);
899 schedule_delayed_work(&tp->schedule, 0);
900 }
901 } else {
902 if (tp->speed & LINK_STATUS) {
903 set_bit(RTL8152_LINK_CHG, &tp->flags);
904 schedule_delayed_work(&tp->schedule, 0);
905 }
906 }
907
908resubmit:
909 res = usb_submit_urb(urb, GFP_ATOMIC);
910 if (res == -ENODEV)
911 netif_device_detach(tp->netdev);
912 else if (res)
913 netif_err(tp, intr, tp->netdev,
914 "can't resubmit intr, status %d\n", res);
915}
916
hayeswangebc2ec482013-08-14 20:54:38 +0800917static inline void *rx_agg_align(void *data)
918{
919 return (void *)ALIGN((uintptr_t)data, 8);
920}
921
922static inline void *tx_agg_align(void *data)
923{
924 return (void *)ALIGN((uintptr_t)data, 4);
925}
926
927static void free_all_mem(struct r8152 *tp)
928{
929 int i;
930
931 for (i = 0; i < RTL8152_MAX_RX; i++) {
932 if (tp->rx_info[i].urb) {
933 usb_free_urb(tp->rx_info[i].urb);
934 tp->rx_info[i].urb = NULL;
935 }
936
937 if (tp->rx_info[i].buffer) {
938 kfree(tp->rx_info[i].buffer);
939 tp->rx_info[i].buffer = NULL;
940 tp->rx_info[i].head = NULL;
941 }
942 }
943
944 for (i = 0; i < RTL8152_MAX_TX; i++) {
945 if (tp->tx_info[i].urb) {
946 usb_free_urb(tp->tx_info[i].urb);
947 tp->tx_info[i].urb = NULL;
948 }
949
950 if (tp->tx_info[i].buffer) {
951 kfree(tp->tx_info[i].buffer);
952 tp->tx_info[i].buffer = NULL;
953 tp->tx_info[i].head = NULL;
954 }
955 }
hayeswang40a82912013-08-14 20:54:40 +0800956
957 if (tp->intr_urb) {
958 usb_free_urb(tp->intr_urb);
959 tp->intr_urb = NULL;
960 }
961
962 if (tp->intr_buff) {
963 kfree(tp->intr_buff);
964 tp->intr_buff = NULL;
965 }
hayeswangebc2ec482013-08-14 20:54:38 +0800966}
967
968static int alloc_all_mem(struct r8152 *tp)
969{
970 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +0800971 struct usb_interface *intf = tp->intf;
972 struct usb_host_interface *alt = intf->cur_altsetting;
973 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +0800974 struct urb *urb;
975 int node, i;
976 u8 *buf;
977
978 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
979
980 spin_lock_init(&tp->rx_lock);
981 spin_lock_init(&tp->tx_lock);
982 INIT_LIST_HEAD(&tp->rx_done);
983 INIT_LIST_HEAD(&tp->tx_free);
984 skb_queue_head_init(&tp->tx_queue);
985
986 for (i = 0; i < RTL8152_MAX_RX; i++) {
987 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
988 if (!buf)
989 goto err1;
990
991 if (buf != rx_agg_align(buf)) {
992 kfree(buf);
993 buf = kmalloc_node(rx_buf_sz + 8, GFP_KERNEL, node);
994 if (!buf)
995 goto err1;
996 }
997
998 urb = usb_alloc_urb(0, GFP_KERNEL);
999 if (!urb) {
1000 kfree(buf);
1001 goto err1;
1002 }
1003
1004 INIT_LIST_HEAD(&tp->rx_info[i].list);
1005 tp->rx_info[i].context = tp;
1006 tp->rx_info[i].urb = urb;
1007 tp->rx_info[i].buffer = buf;
1008 tp->rx_info[i].head = rx_agg_align(buf);
1009 }
1010
1011 for (i = 0; i < RTL8152_MAX_TX; i++) {
1012 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1013 if (!buf)
1014 goto err1;
1015
1016 if (buf != tx_agg_align(buf)) {
1017 kfree(buf);
1018 buf = kmalloc_node(rx_buf_sz + 4, GFP_KERNEL, node);
1019 if (!buf)
1020 goto err1;
1021 }
1022
1023 urb = usb_alloc_urb(0, GFP_KERNEL);
1024 if (!urb) {
1025 kfree(buf);
1026 goto err1;
1027 }
1028
1029 INIT_LIST_HEAD(&tp->tx_info[i].list);
1030 tp->tx_info[i].context = tp;
1031 tp->tx_info[i].urb = urb;
1032 tp->tx_info[i].buffer = buf;
1033 tp->tx_info[i].head = tx_agg_align(buf);
1034
1035 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1036 }
1037
hayeswang40a82912013-08-14 20:54:40 +08001038 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1039 if (!tp->intr_urb)
1040 goto err1;
1041
1042 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1043 if (!tp->intr_buff)
1044 goto err1;
1045
1046 tp->intr_interval = (int)ep_intr->desc.bInterval;
1047 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1048 tp->intr_buff, INTBUFSIZE, intr_callback,
1049 tp, tp->intr_interval);
1050
hayeswangebc2ec482013-08-14 20:54:38 +08001051 return 0;
1052
1053err1:
1054 free_all_mem(tp);
1055 return -ENOMEM;
1056}
1057
hayeswang0de98f62013-08-16 16:09:35 +08001058static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1059{
1060 struct tx_agg *agg = NULL;
1061 unsigned long flags;
1062
1063 spin_lock_irqsave(&tp->tx_lock, flags);
1064 if (!list_empty(&tp->tx_free)) {
1065 struct list_head *cursor;
1066
1067 cursor = tp->tx_free.next;
1068 list_del_init(cursor);
1069 agg = list_entry(cursor, struct tx_agg, list);
1070 }
1071 spin_unlock_irqrestore(&tp->tx_lock, flags);
1072
1073 return agg;
1074}
1075
hayeswang5bd23882013-08-14 20:54:39 +08001076static void
1077r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1078{
1079 memset(desc, 0, sizeof(*desc));
1080
1081 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1082
1083 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1084 __be16 protocol;
1085 u8 ip_protocol;
1086 u32 opts2 = 0;
1087
1088 if (skb->protocol == htons(ETH_P_8021Q))
1089 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1090 else
1091 protocol = skb->protocol;
1092
1093 switch (protocol) {
1094 case htons(ETH_P_IP):
1095 opts2 |= IPV4_CS;
1096 ip_protocol = ip_hdr(skb)->protocol;
1097 break;
1098
1099 case htons(ETH_P_IPV6):
1100 opts2 |= IPV6_CS;
1101 ip_protocol = ipv6_hdr(skb)->nexthdr;
1102 break;
1103
1104 default:
1105 ip_protocol = IPPROTO_RAW;
1106 break;
1107 }
1108
1109 if (ip_protocol == IPPROTO_TCP) {
1110 opts2 |= TCP_CS;
1111 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1112 } else if (ip_protocol == IPPROTO_UDP) {
1113 opts2 |= UDP_CS;
1114 } else {
1115 WARN_ON_ONCE(1);
1116 }
1117
1118 desc->opts2 = cpu_to_le32(opts2);
1119 }
1120}
1121
hayeswangb1379d92013-08-16 16:09:37 +08001122static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1123{
hayeswang7937f9e2013-11-20 17:30:54 +08001124 int remain;
hayeswangb1379d92013-08-16 16:09:37 +08001125 u8 *tx_data;
1126
1127 tx_data = agg->head;
1128 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001129 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001130
hayeswang7937f9e2013-11-20 17:30:54 +08001131 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001132 struct tx_desc *tx_desc;
1133 struct sk_buff *skb;
1134 unsigned int len;
1135
1136 skb = skb_dequeue(&tp->tx_queue);
1137 if (!skb)
1138 break;
1139
hayeswang7937f9e2013-11-20 17:30:54 +08001140 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001141 len = skb->len;
1142 if (remain < len) {
1143 skb_queue_head(&tp->tx_queue, skb);
1144 break;
1145 }
1146
hayeswang7937f9e2013-11-20 17:30:54 +08001147 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001148 tx_desc = (struct tx_desc *)tx_data;
1149 tx_data += sizeof(*tx_desc);
1150
1151 r8152_tx_csum(tp, tx_desc, skb);
1152 memcpy(tx_data, skb->data, len);
1153 agg->skb_num++;
1154 agg->skb_len += len;
1155 dev_kfree_skb_any(skb);
1156
hayeswang7937f9e2013-11-20 17:30:54 +08001157 tx_data += len;
1158 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001159 }
1160
hayeswangdd1b1192013-11-20 17:30:56 +08001161 netif_tx_lock(tp->netdev);
1162
1163 if (netif_queue_stopped(tp->netdev) &&
1164 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1165 netif_wake_queue(tp->netdev);
1166
1167 netif_tx_unlock(tp->netdev);
1168
hayeswangb1379d92013-08-16 16:09:37 +08001169 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1170 agg->head, (int)(tx_data - (u8 *)agg->head),
1171 (usb_complete_t)write_bulk_callback, agg);
1172
1173 return usb_submit_urb(agg->urb, GFP_ATOMIC);
1174}
1175
hayeswangebc2ec482013-08-14 20:54:38 +08001176static void rx_bottom(struct r8152 *tp)
1177{
hayeswanga5a4f462013-08-16 16:09:34 +08001178 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001179 struct list_head *cursor, *next;
hayeswangebc2ec482013-08-14 20:54:38 +08001180
hayeswanga5a4f462013-08-16 16:09:34 +08001181 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001182 list_for_each_safe(cursor, next, &tp->rx_done) {
hayeswang43a44782013-08-16 16:09:36 +08001183 struct rx_desc *rx_desc;
1184 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001185 int len_used = 0;
1186 struct urb *urb;
1187 u8 *rx_data;
1188 int ret;
1189
hayeswangebc2ec482013-08-14 20:54:38 +08001190 list_del_init(cursor);
hayeswanga5a4f462013-08-16 16:09:34 +08001191 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001192
1193 agg = list_entry(cursor, struct rx_agg, list);
1194 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001195 if (urb->actual_length < ETH_ZLEN)
1196 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001197
hayeswangebc2ec482013-08-14 20:54:38 +08001198 rx_desc = agg->head;
1199 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001200 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001201
hayeswang7937f9e2013-11-20 17:30:54 +08001202 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001203 struct net_device *netdev = tp->netdev;
1204 struct net_device_stats *stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001205 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001206 struct sk_buff *skb;
1207
hayeswang7937f9e2013-11-20 17:30:54 +08001208 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001209 if (pkt_len < ETH_ZLEN)
1210 break;
1211
hayeswang7937f9e2013-11-20 17:30:54 +08001212 len_used += pkt_len;
1213 if (urb->actual_length < len_used)
1214 break;
1215
hayeswang43a44782013-08-16 16:09:36 +08001216 stats = rtl8152_get_stats(netdev);
1217
hayeswangebc2ec482013-08-14 20:54:38 +08001218 pkt_len -= 4; /* CRC */
1219 rx_data += sizeof(struct rx_desc);
1220
1221 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1222 if (!skb) {
1223 stats->rx_dropped++;
1224 break;
1225 }
1226 memcpy(skb->data, rx_data, pkt_len);
1227 skb_put(skb, pkt_len);
1228 skb->protocol = eth_type_trans(skb, netdev);
1229 netif_rx(skb);
1230 stats->rx_packets++;
1231 stats->rx_bytes += pkt_len;
1232
1233 rx_data = rx_agg_align(rx_data + pkt_len + 4);
1234 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001235 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001236 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001237 }
1238
hayeswang0de98f62013-08-16 16:09:35 +08001239submit:
hayeswangebc2ec482013-08-14 20:54:38 +08001240 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswanga5a4f462013-08-16 16:09:34 +08001241 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001242 if (ret && ret != -ENODEV) {
1243 list_add_tail(&agg->list, next);
1244 tasklet_schedule(&tp->tl);
1245 }
1246 }
hayeswanga5a4f462013-08-16 16:09:34 +08001247 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001248}
1249
1250static void tx_bottom(struct r8152 *tp)
1251{
hayeswangebc2ec482013-08-14 20:54:38 +08001252 int res;
1253
hayeswangb1379d92013-08-16 16:09:37 +08001254 do {
1255 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001256
hayeswangb1379d92013-08-16 16:09:37 +08001257 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001258 break;
1259
hayeswangb1379d92013-08-16 16:09:37 +08001260 agg = r8152_get_tx_agg(tp);
1261 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001262 break;
hayeswangb1379d92013-08-16 16:09:37 +08001263
1264 res = r8152_tx_agg_fill(tp, agg);
1265 if (res) {
1266 struct net_device_stats *stats;
1267 struct net_device *netdev;
1268 unsigned long flags;
1269
1270 netdev = tp->netdev;
1271 stats = rtl8152_get_stats(netdev);
1272
1273 if (res == -ENODEV) {
1274 netif_device_detach(netdev);
1275 } else {
1276 netif_warn(tp, tx_err, netdev,
1277 "failed tx_urb %d\n", res);
1278 stats->tx_dropped += agg->skb_num;
1279 spin_lock_irqsave(&tp->tx_lock, flags);
1280 list_add_tail(&agg->list, &tp->tx_free);
1281 spin_unlock_irqrestore(&tp->tx_lock, flags);
1282 }
hayeswangebc2ec482013-08-14 20:54:38 +08001283 }
hayeswangb1379d92013-08-16 16:09:37 +08001284 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001285}
1286
1287static void bottom_half(unsigned long data)
1288{
1289 struct r8152 *tp;
1290
1291 tp = (struct r8152 *)data;
1292
1293 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1294 return;
1295
1296 if (!test_bit(WORK_ENABLE, &tp->flags))
1297 return;
1298
hayeswang7559fb2f2013-08-16 16:09:38 +08001299 /* When link down, the driver would cancel all bulks. */
1300 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001301 if (!netif_carrier_ok(tp->netdev))
1302 return;
1303
1304 rx_bottom(tp);
1305 tx_bottom(tp);
1306}
1307
1308static
1309int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1310{
1311 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1312 agg->head, rx_buf_sz,
1313 (usb_complete_t)read_bulk_callback, agg);
1314
1315 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001316}
1317
1318static void rtl8152_tx_timeout(struct net_device *netdev)
1319{
1320 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001321 int i;
1322
hayeswangac718b62013-05-02 16:01:25 +00001323 netif_warn(tp, tx_err, netdev, "Tx timeout.\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001324 for (i = 0; i < RTL8152_MAX_TX; i++)
1325 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001326}
1327
1328static void rtl8152_set_rx_mode(struct net_device *netdev)
1329{
1330 struct r8152 *tp = netdev_priv(netdev);
1331
hayeswang40a82912013-08-14 20:54:40 +08001332 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001333 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001334 schedule_delayed_work(&tp->schedule, 0);
1335 }
hayeswangac718b62013-05-02 16:01:25 +00001336}
1337
1338static void _rtl8152_set_rx_mode(struct net_device *netdev)
1339{
1340 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001341 u32 mc_filter[2]; /* Multicast hash filter */
1342 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001343 u32 ocp_data;
1344
hayeswangac718b62013-05-02 16:01:25 +00001345 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1346 netif_stop_queue(netdev);
1347 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1348 ocp_data &= ~RCR_ACPT_ALL;
1349 ocp_data |= RCR_AB | RCR_APM;
1350
1351 if (netdev->flags & IFF_PROMISC) {
1352 /* Unconditionally log net taps. */
1353 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1354 ocp_data |= RCR_AM | RCR_AAP;
1355 mc_filter[1] = mc_filter[0] = 0xffffffff;
1356 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1357 (netdev->flags & IFF_ALLMULTI)) {
1358 /* Too many to filter perfectly -- accept all multicasts. */
1359 ocp_data |= RCR_AM;
1360 mc_filter[1] = mc_filter[0] = 0xffffffff;
1361 } else {
1362 struct netdev_hw_addr *ha;
1363
1364 mc_filter[1] = mc_filter[0] = 0;
1365 netdev_for_each_mc_addr(ha, netdev) {
1366 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1367 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1368 ocp_data |= RCR_AM;
1369 }
1370 }
1371
hayeswang31787f52013-07-31 17:21:25 +08001372 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1373 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001374
hayeswang31787f52013-07-31 17:21:25 +08001375 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001376 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1377 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001378}
1379
1380static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1381 struct net_device *netdev)
1382{
1383 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001384
hayeswangac718b62013-05-02 16:01:25 +00001385 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001386
hayeswang61598782013-11-20 17:30:55 +08001387 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001388
hayeswangdd1b1192013-11-20 17:30:56 +08001389 if (list_empty(&tp->tx_free) &&
1390 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1391 netif_stop_queue(netdev);
1392
hayeswang61598782013-11-20 17:30:55 +08001393 if (!list_empty(&tp->tx_free))
1394 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001395
1396 return NETDEV_TX_OK;
1397}
1398
1399static void r8152b_reset_packet_filter(struct r8152 *tp)
1400{
1401 u32 ocp_data;
1402
1403 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1404 ocp_data &= ~FMC_FCR_MCU_EN;
1405 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1406 ocp_data |= FMC_FCR_MCU_EN;
1407 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1408}
1409
1410static void rtl8152_nic_reset(struct r8152 *tp)
1411{
1412 int i;
1413
1414 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1415
1416 for (i = 0; i < 1000; i++) {
1417 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1418 break;
1419 udelay(100);
1420 }
1421}
1422
hayeswangdd1b1192013-11-20 17:30:56 +08001423static void set_tx_qlen(struct r8152 *tp)
1424{
1425 struct net_device *netdev = tp->netdev;
1426
1427 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1428 sizeof(struct tx_desc));
1429}
1430
hayeswangac718b62013-05-02 16:01:25 +00001431static inline u8 rtl8152_get_speed(struct r8152 *tp)
1432{
1433 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1434}
1435
1436static int rtl8152_enable(struct r8152 *tp)
1437{
hayeswangebc2ec482013-08-14 20:54:38 +08001438 u32 ocp_data;
1439 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001440 u8 speed;
1441
hayeswangdd1b1192013-11-20 17:30:56 +08001442 set_tx_qlen(tp);
hayeswangac718b62013-05-02 16:01:25 +00001443 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001444 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001445 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001446 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001447 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1448 } else {
1449 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001450 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001451 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1452 }
1453
1454 r8152b_reset_packet_filter(tp);
1455
1456 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1457 ocp_data |= CR_RE | CR_TE;
1458 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1459
1460 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1461 ocp_data &= ~RXDY_GATED_EN;
1462 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1463
hayeswangebc2ec482013-08-14 20:54:38 +08001464 INIT_LIST_HEAD(&tp->rx_done);
1465 ret = 0;
1466 for (i = 0; i < RTL8152_MAX_RX; i++) {
1467 INIT_LIST_HEAD(&tp->rx_info[i].list);
1468 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1469 }
hayeswangac718b62013-05-02 16:01:25 +00001470
hayeswangebc2ec482013-08-14 20:54:38 +08001471 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001472}
1473
1474static void rtl8152_disable(struct r8152 *tp)
1475{
hayeswangebc2ec482013-08-14 20:54:38 +08001476 struct net_device_stats *stats = rtl8152_get_stats(tp->netdev);
1477 struct sk_buff *skb;
1478 u32 ocp_data;
1479 int i;
hayeswangac718b62013-05-02 16:01:25 +00001480
1481 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1482 ocp_data &= ~RCR_ACPT_ALL;
1483 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1484
hayeswangebc2ec482013-08-14 20:54:38 +08001485 while ((skb = skb_dequeue(&tp->tx_queue))) {
1486 dev_kfree_skb(skb);
1487 stats->tx_dropped++;
1488 }
1489
1490 for (i = 0; i < RTL8152_MAX_TX; i++)
1491 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001492
1493 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1494 ocp_data |= RXDY_GATED_EN;
1495 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1496
1497 for (i = 0; i < 1000; i++) {
1498 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1499 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1500 break;
1501 mdelay(1);
1502 }
1503
1504 for (i = 0; i < 1000; i++) {
1505 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1506 break;
1507 mdelay(1);
1508 }
1509
hayeswangebc2ec482013-08-14 20:54:38 +08001510 for (i = 0; i < RTL8152_MAX_RX; i++)
1511 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001512
1513 rtl8152_nic_reset(tp);
1514}
1515
1516static void r8152b_exit_oob(struct r8152 *tp)
1517{
1518 u32 ocp_data;
1519 int i;
1520
1521 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1522 ocp_data &= ~RCR_ACPT_ALL;
1523 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1524
1525 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1526 ocp_data |= RXDY_GATED_EN;
1527 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1528
1529 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1530 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1531
1532 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1533 ocp_data &= ~NOW_IS_OOB;
1534 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1535
1536 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1537 ocp_data &= ~MCU_BORW_EN;
1538 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1539
1540 for (i = 0; i < 1000; i++) {
1541 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1542 if (ocp_data & LINK_LIST_READY)
1543 break;
1544 mdelay(1);
1545 }
1546
1547 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1548 ocp_data |= RE_INIT_LL;
1549 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1550
1551 for (i = 0; i < 1000; i++) {
1552 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1553 if (ocp_data & LINK_LIST_READY)
1554 break;
1555 mdelay(1);
1556 }
1557
1558 rtl8152_nic_reset(tp);
1559
1560 /* rx share fifo credit full threshold */
1561 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1562
1563 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1564 ocp_data &= STAT_SPEED_MASK;
1565 if (ocp_data == STAT_SPEED_FULL) {
1566 /* rx share fifo credit near full threshold */
1567 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1568 RXFIFO_THR2_FULL);
1569 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1570 RXFIFO_THR3_FULL);
1571 } else {
1572 /* rx share fifo credit near full threshold */
1573 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1574 RXFIFO_THR2_HIGH);
1575 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1576 RXFIFO_THR3_HIGH);
1577 }
1578
1579 /* TX share fifo free credit full threshold */
1580 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1581
1582 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
1583 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_BUF_THR);
1584 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1585 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1586
1587 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1588 ocp_data &= ~CPCR_RX_VLAN;
1589 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1590
1591 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1592
1593 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1594 ocp_data |= TCR0_AUTO_FIFO;
1595 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1596}
1597
1598static void r8152b_enter_oob(struct r8152 *tp)
1599{
1600 u32 ocp_data;
1601 int i;
1602
1603 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1604 ocp_data &= ~NOW_IS_OOB;
1605 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1606
1607 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1608 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1609 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1610
1611 rtl8152_disable(tp);
1612
1613 for (i = 0; i < 1000; i++) {
1614 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1615 if (ocp_data & LINK_LIST_READY)
1616 break;
1617 mdelay(1);
1618 }
1619
1620 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1621 ocp_data |= RE_INIT_LL;
1622 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1623
1624 for (i = 0; i < 1000; i++) {
1625 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1626 if (ocp_data & LINK_LIST_READY)
1627 break;
1628 mdelay(1);
1629 }
1630
1631 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1632
1633 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1634 ocp_data |= MAGIC_EN;
1635 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1636
1637 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1638 ocp_data |= CPCR_RX_VLAN;
1639 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1640
1641 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
1642 ocp_data |= ALDPS_PROXY_MODE;
1643 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
1644
1645 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1646 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1647 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1648
1649 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
1650
1651 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1652 ocp_data &= ~RXDY_GATED_EN;
1653 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1654
1655 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1656 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1657 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1658}
1659
1660static void r8152b_disable_aldps(struct r8152 *tp)
1661{
1662 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1663 msleep(20);
1664}
1665
1666static inline void r8152b_enable_aldps(struct r8152 *tp)
1667{
1668 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1669 LINKENA | DIS_SDSAVE);
1670}
1671
1672static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
1673{
1674 u16 bmcr, anar;
1675 int ret = 0;
1676
1677 cancel_delayed_work_sync(&tp->schedule);
1678 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1679 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1680 ADVERTISE_100HALF | ADVERTISE_100FULL);
1681
1682 if (autoneg == AUTONEG_DISABLE) {
1683 if (speed == SPEED_10) {
1684 bmcr = 0;
1685 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1686 } else if (speed == SPEED_100) {
1687 bmcr = BMCR_SPEED100;
1688 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1689 } else {
1690 ret = -EINVAL;
1691 goto out;
1692 }
1693
1694 if (duplex == DUPLEX_FULL)
1695 bmcr |= BMCR_FULLDPLX;
1696 } else {
1697 if (speed == SPEED_10) {
1698 if (duplex == DUPLEX_FULL)
1699 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1700 else
1701 anar |= ADVERTISE_10HALF;
1702 } else if (speed == SPEED_100) {
1703 if (duplex == DUPLEX_FULL) {
1704 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1705 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1706 } else {
1707 anar |= ADVERTISE_10HALF;
1708 anar |= ADVERTISE_100HALF;
1709 }
1710 } else {
1711 ret = -EINVAL;
1712 goto out;
1713 }
1714
1715 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1716 }
1717
1718 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1719 r8152_mdio_write(tp, MII_BMCR, bmcr);
1720
1721out:
hayeswangac718b62013-05-02 16:01:25 +00001722
1723 return ret;
1724}
1725
1726static void rtl8152_down(struct r8152 *tp)
1727{
1728 u32 ocp_data;
1729
1730 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1731 ocp_data &= ~POWER_CUT;
1732 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1733
1734 r8152b_disable_aldps(tp);
1735 r8152b_enter_oob(tp);
1736 r8152b_enable_aldps(tp);
1737}
1738
1739static void set_carrier(struct r8152 *tp)
1740{
1741 struct net_device *netdev = tp->netdev;
1742 u8 speed;
1743
hayeswang40a82912013-08-14 20:54:40 +08001744 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00001745 speed = rtl8152_get_speed(tp);
1746
1747 if (speed & LINK_STATUS) {
1748 if (!(tp->speed & LINK_STATUS)) {
1749 rtl8152_enable(tp);
1750 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1751 netif_carrier_on(netdev);
1752 }
1753 } else {
1754 if (tp->speed & LINK_STATUS) {
1755 netif_carrier_off(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001756 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001757 rtl8152_disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001758 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001759 }
1760 }
1761 tp->speed = speed;
1762}
1763
1764static void rtl_work_func_t(struct work_struct *work)
1765{
1766 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
1767
1768 if (!test_bit(WORK_ENABLE, &tp->flags))
1769 goto out1;
1770
1771 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1772 goto out1;
1773
hayeswang40a82912013-08-14 20:54:40 +08001774 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
1775 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00001776
1777 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
1778 _rtl8152_set_rx_mode(tp->netdev);
1779
hayeswangac718b62013-05-02 16:01:25 +00001780out1:
1781 return;
1782}
1783
1784static int rtl8152_open(struct net_device *netdev)
1785{
1786 struct r8152 *tp = netdev_priv(netdev);
1787 int res = 0;
1788
hayeswang40a82912013-08-14 20:54:40 +08001789 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
1790 if (res) {
1791 if (res == -ENODEV)
1792 netif_device_detach(tp->netdev);
1793 netif_warn(tp, ifup, netdev,
1794 "intr_urb submit failed: %d\n", res);
1795 return res;
hayeswangac718b62013-05-02 16:01:25 +00001796 }
1797
1798 rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08001799 tp->speed = 0;
1800 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001801 netif_start_queue(netdev);
1802 set_bit(WORK_ENABLE, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00001803
1804 return res;
1805}
1806
1807static int rtl8152_close(struct net_device *netdev)
1808{
1809 struct r8152 *tp = netdev_priv(netdev);
1810 int res = 0;
1811
hayeswang40a82912013-08-14 20:54:40 +08001812 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00001813 clear_bit(WORK_ENABLE, &tp->flags);
1814 cancel_delayed_work_sync(&tp->schedule);
1815 netif_stop_queue(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001816 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001817 rtl8152_disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001818 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001819
1820 return res;
1821}
1822
1823static void rtl_clear_bp(struct r8152 *tp)
1824{
1825 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1826 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1827 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1828 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1829 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1830 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1831 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1832 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1833 mdelay(3);
1834 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1835 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1836}
1837
1838static void r8152b_enable_eee(struct r8152 *tp)
1839{
1840 u32 ocp_data;
1841
1842 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
1843 ocp_data |= EEE_RX_EN | EEE_TX_EN;
1844 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
1845 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
1846 EEE_10_CAP | EEE_NWAY_EN |
1847 TX_QUIET_EN | RX_QUIET_EN |
1848 SDRISETIME | RG_RXLPI_MSK_HFDUP |
1849 SDFALLTIME);
1850 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
1851 RG_LDVQUIET_EN | RG_CKRSEL |
1852 RG_EEEPRG_EN);
1853 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
1854 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
1855 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
1856 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
1857 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
1858 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
1859}
1860
1861static void r8152b_enable_fc(struct r8152 *tp)
1862{
1863 u16 anar;
1864
1865 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1866 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1867 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1868}
1869
1870static void r8152b_hw_phy_cfg(struct r8152 *tp)
1871{
1872 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1873 r8152b_disable_aldps(tp);
1874}
1875
1876static void r8152b_init(struct r8152 *tp)
1877{
hayeswangebc2ec482013-08-14 20:54:38 +08001878 u32 ocp_data;
1879 int i;
hayeswangac718b62013-05-02 16:01:25 +00001880
1881 rtl_clear_bp(tp);
1882
1883 if (tp->version == RTL_VER_01) {
1884 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
1885 ocp_data &= ~LED_MODE_MASK;
1886 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
1887 }
1888
1889 r8152b_hw_phy_cfg(tp);
1890
1891 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1892 ocp_data &= ~POWER_CUT;
1893 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1894
1895 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1896 ocp_data &= ~RWSUME_INDICATE;
1897 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1898
1899 r8152b_exit_oob(tp);
1900
1901 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1902 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
1903 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1904 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
1905 ocp_data &= ~MCU_CLK_RATIO_MASK;
1906 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
1907 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
1908 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
1909 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
1910 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
1911
1912 r8152b_enable_eee(tp);
1913 r8152b_enable_aldps(tp);
1914 r8152b_enable_fc(tp);
1915
1916 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
1917 BMCR_ANRESTART);
1918 for (i = 0; i < 100; i++) {
1919 udelay(100);
1920 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
1921 break;
1922 }
1923
hayeswangebc2ec482013-08-14 20:54:38 +08001924 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00001925 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08001926 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00001927 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
1928}
1929
1930static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
1931{
1932 struct r8152 *tp = usb_get_intfdata(intf);
1933
1934 netif_device_detach(tp->netdev);
1935
1936 if (netif_running(tp->netdev)) {
1937 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001938 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00001939 cancel_delayed_work_sync(&tp->schedule);
hayeswangebc2ec482013-08-14 20:54:38 +08001940 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001941 }
1942
1943 rtl8152_down(tp);
1944
1945 return 0;
1946}
1947
1948static int rtl8152_resume(struct usb_interface *intf)
1949{
1950 struct r8152 *tp = usb_get_intfdata(intf);
1951
1952 r8152b_init(tp);
1953 netif_device_attach(tp->netdev);
1954 if (netif_running(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001955 rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL);
1956 tp->speed = 0;
1957 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00001958 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001959 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangebc2ec482013-08-14 20:54:38 +08001960 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001961 }
1962
1963 return 0;
1964}
1965
1966static void rtl8152_get_drvinfo(struct net_device *netdev,
1967 struct ethtool_drvinfo *info)
1968{
1969 struct r8152 *tp = netdev_priv(netdev);
1970
1971 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
1972 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
1973 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
1974}
1975
1976static
1977int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1978{
1979 struct r8152 *tp = netdev_priv(netdev);
1980
1981 if (!tp->mii.mdio_read)
1982 return -EOPNOTSUPP;
1983
1984 return mii_ethtool_gset(&tp->mii, cmd);
1985}
1986
1987static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1988{
1989 struct r8152 *tp = netdev_priv(dev);
1990
1991 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
1992}
1993
1994static struct ethtool_ops ops = {
1995 .get_drvinfo = rtl8152_get_drvinfo,
1996 .get_settings = rtl8152_get_settings,
1997 .set_settings = rtl8152_set_settings,
1998 .get_link = ethtool_op_get_link,
1999};
2000
2001static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2002{
2003 struct r8152 *tp = netdev_priv(netdev);
2004 struct mii_ioctl_data *data = if_mii(rq);
2005 int res = 0;
2006
2007 switch (cmd) {
2008 case SIOCGMIIPHY:
2009 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2010 break;
2011
2012 case SIOCGMIIREG:
2013 data->val_out = r8152_mdio_read(tp, data->reg_num);
2014 break;
2015
2016 case SIOCSMIIREG:
2017 if (!capable(CAP_NET_ADMIN)) {
2018 res = -EPERM;
2019 break;
2020 }
2021 r8152_mdio_write(tp, data->reg_num, data->val_in);
2022 break;
2023
2024 default:
2025 res = -EOPNOTSUPP;
2026 }
2027
2028 return res;
2029}
2030
2031static const struct net_device_ops rtl8152_netdev_ops = {
2032 .ndo_open = rtl8152_open,
2033 .ndo_stop = rtl8152_close,
2034 .ndo_do_ioctl = rtl8152_ioctl,
2035 .ndo_start_xmit = rtl8152_start_xmit,
2036 .ndo_tx_timeout = rtl8152_tx_timeout,
2037 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2038 .ndo_set_mac_address = rtl8152_set_mac_address,
2039
2040 .ndo_change_mtu = eth_change_mtu,
2041 .ndo_validate_addr = eth_validate_addr,
2042};
2043
2044static void r8152b_get_version(struct r8152 *tp)
2045{
2046 u32 ocp_data;
2047 u16 version;
2048
2049 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2050 version = (u16)(ocp_data & VERSION_MASK);
2051
2052 switch (version) {
2053 case 0x4c00:
2054 tp->version = RTL_VER_01;
2055 break;
2056 case 0x4c10:
2057 tp->version = RTL_VER_02;
2058 break;
2059 default:
2060 netif_info(tp, probe, tp->netdev,
2061 "Unknown version 0x%04x\n", version);
2062 break;
2063 }
2064}
2065
hayeswange3fe0b12014-01-02 11:22:39 +08002066static void rtl8152_unload(struct r8152 *tp)
2067{
2068 u32 ocp_data;
2069
2070 if (tp->version != RTL_VER_01) {
2071 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2072 ocp_data |= POWER_CUT;
2073 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2074 }
2075
2076 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2077 ocp_data &= ~RWSUME_INDICATE;
2078 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2079}
2080
hayeswangac718b62013-05-02 16:01:25 +00002081static int rtl8152_probe(struct usb_interface *intf,
2082 const struct usb_device_id *id)
2083{
2084 struct usb_device *udev = interface_to_usbdev(intf);
2085 struct r8152 *tp;
2086 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08002087 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002088
2089 if (udev->actconfig->desc.bConfigurationValue != 1) {
2090 usb_driver_set_configuration(udev, 1);
2091 return -ENODEV;
2092 }
2093
2094 netdev = alloc_etherdev(sizeof(struct r8152));
2095 if (!netdev) {
2096 dev_err(&intf->dev, "Out of memory");
2097 return -ENOMEM;
2098 }
2099
hayeswangebc2ec482013-08-14 20:54:38 +08002100 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00002101 tp = netdev_priv(netdev);
2102 tp->msg_enable = 0x7FFF;
2103
hayeswangebc2ec482013-08-14 20:54:38 +08002104 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00002105 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
2106
2107 tp->udev = udev;
2108 tp->netdev = netdev;
hayeswang40a82912013-08-14 20:54:40 +08002109 tp->intf = intf;
hayeswangac718b62013-05-02 16:01:25 +00002110 netdev->netdev_ops = &rtl8152_netdev_ops;
2111 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08002112
2113 netdev->features |= NETIF_F_IP_CSUM;
2114 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangac718b62013-05-02 16:01:25 +00002115 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00002116
2117 tp->mii.dev = netdev;
2118 tp->mii.mdio_read = read_mii_word;
2119 tp->mii.mdio_write = write_mii_word;
2120 tp->mii.phy_id_mask = 0x3f;
2121 tp->mii.reg_num_mask = 0x1f;
2122 tp->mii.phy_id = R8152_PHY_ID;
2123 tp->mii.supports_gmii = 0;
2124
2125 r8152b_get_version(tp);
2126 r8152b_init(tp);
2127 set_ethernet_addr(tp);
2128
hayeswangebc2ec482013-08-14 20:54:38 +08002129 ret = alloc_all_mem(tp);
2130 if (ret)
hayeswangac718b62013-05-02 16:01:25 +00002131 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002132
2133 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00002134
hayeswangebc2ec482013-08-14 20:54:38 +08002135 ret = register_netdev(netdev);
2136 if (ret != 0) {
hayeswangac718b62013-05-02 16:01:25 +00002137 netif_err(tp, probe, netdev, "couldn't register the device");
hayeswangebc2ec482013-08-14 20:54:38 +08002138 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00002139 }
2140
2141 netif_info(tp, probe, netdev, "%s", DRIVER_VERSION);
2142
2143 return 0;
2144
hayeswangac718b62013-05-02 16:01:25 +00002145out1:
hayeswangebc2ec482013-08-14 20:54:38 +08002146 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00002147out:
2148 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002149 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002150}
2151
hayeswangac718b62013-05-02 16:01:25 +00002152static void rtl8152_disconnect(struct usb_interface *intf)
2153{
2154 struct r8152 *tp = usb_get_intfdata(intf);
2155
2156 usb_set_intfdata(intf, NULL);
2157 if (tp) {
2158 set_bit(RTL8152_UNPLUG, &tp->flags);
2159 tasklet_kill(&tp->tl);
2160 unregister_netdev(tp->netdev);
2161 rtl8152_unload(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002162 free_all_mem(tp);
hayeswangac718b62013-05-02 16:01:25 +00002163 free_netdev(tp->netdev);
2164 }
2165}
2166
2167/* table of devices that work with this driver */
2168static struct usb_device_id rtl8152_table[] = {
2169 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
2170 {}
2171};
2172
2173MODULE_DEVICE_TABLE(usb, rtl8152_table);
2174
2175static struct usb_driver rtl8152_driver = {
2176 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08002177 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00002178 .probe = rtl8152_probe,
2179 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00002180 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08002181 .resume = rtl8152_resume,
2182 .reset_resume = rtl8152_resume,
hayeswangac718b62013-05-02 16:01:25 +00002183};
2184
Sachin Kamatb4236daa2013-05-16 17:48:08 +00002185module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00002186
2187MODULE_AUTHOR(DRIVER_AUTHOR);
2188MODULE_DESCRIPTION(DRIVER_DESC);
2189MODULE_LICENSE("GPL");