r8152.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. /*
  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. #include <linux/init.h>
  10. #include <linux/signal.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  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>
  21. /* Version Information */
  22. #define DRIVER_VERSION "v1.0.0 (2013/05/03)"
  23. #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
  24. #define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters"
  25. #define MODULENAME "r8152"
  26. #define R8152_PHY_ID 32
  27. #define PLA_IDR 0xc000
  28. #define PLA_RCR 0xc010
  29. #define PLA_RMS 0xc016
  30. #define PLA_RXFIFO_CTRL0 0xc0a0
  31. #define PLA_RXFIFO_CTRL1 0xc0a4
  32. #define PLA_RXFIFO_CTRL2 0xc0a8
  33. #define PLA_FMC 0xc0b4
  34. #define PLA_CFG_WOL 0xc0b6
  35. #define PLA_MAR 0xcd00
  36. #define PAL_BDC_CR 0xd1a0
  37. #define PLA_LEDSEL 0xdd90
  38. #define PLA_LED_FEATURE 0xdd92
  39. #define PLA_PHYAR 0xde00
  40. #define PLA_GPHY_INTR_IMR 0xe022
  41. #define PLA_EEE_CR 0xe040
  42. #define PLA_EEEP_CR 0xe080
  43. #define PLA_MAC_PWR_CTRL 0xe0c0
  44. #define PLA_TCR0 0xe610
  45. #define PLA_TCR1 0xe612
  46. #define PLA_TXFIFO_CTRL 0xe618
  47. #define PLA_RSTTELLY 0xe800
  48. #define PLA_CR 0xe813
  49. #define PLA_CRWECR 0xe81c
  50. #define PLA_CONFIG5 0xe822
  51. #define PLA_PHY_PWR 0xe84c
  52. #define PLA_OOB_CTRL 0xe84f
  53. #define PLA_CPCR 0xe854
  54. #define PLA_MISC_0 0xe858
  55. #define PLA_MISC_1 0xe85a
  56. #define PLA_OCP_GPHY_BASE 0xe86c
  57. #define PLA_TELLYCNT 0xe890
  58. #define PLA_SFF_STS_7 0xe8de
  59. #define PLA_PHYSTATUS 0xe908
  60. #define PLA_BP_BA 0xfc26
  61. #define PLA_BP_0 0xfc28
  62. #define PLA_BP_1 0xfc2a
  63. #define PLA_BP_2 0xfc2c
  64. #define PLA_BP_3 0xfc2e
  65. #define PLA_BP_4 0xfc30
  66. #define PLA_BP_5 0xfc32
  67. #define PLA_BP_6 0xfc34
  68. #define PLA_BP_7 0xfc36
  69. #define USB_DEV_STAT 0xb808
  70. #define USB_USB_CTRL 0xd406
  71. #define USB_PHY_CTRL 0xd408
  72. #define USB_TX_AGG 0xd40a
  73. #define USB_RX_BUF_TH 0xd40c
  74. #define USB_USB_TIMER 0xd428
  75. #define USB_PM_CTRL_STATUS 0xd432
  76. #define USB_TX_DMA 0xd434
  77. #define USB_UPS_CTRL 0xd800
  78. #define USB_BP_BA 0xfc26
  79. #define USB_BP_0 0xfc28
  80. #define USB_BP_1 0xfc2a
  81. #define USB_BP_2 0xfc2c
  82. #define USB_BP_3 0xfc2e
  83. #define USB_BP_4 0xfc30
  84. #define USB_BP_5 0xfc32
  85. #define USB_BP_6 0xfc34
  86. #define USB_BP_7 0xfc36
  87. /* OCP Registers */
  88. #define OCP_ALDPS_CONFIG 0x2010
  89. #define OCP_EEE_CONFIG1 0x2080
  90. #define OCP_EEE_CONFIG2 0x2092
  91. #define OCP_EEE_CONFIG3 0x2094
  92. #define OCP_EEE_AR 0xa41a
  93. #define OCP_EEE_DATA 0xa41c
  94. /* PLA_RCR */
  95. #define RCR_AAP 0x00000001
  96. #define RCR_APM 0x00000002
  97. #define RCR_AM 0x00000004
  98. #define RCR_AB 0x00000008
  99. #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
  100. /* PLA_RXFIFO_CTRL0 */
  101. #define RXFIFO_THR1_NORMAL 0x00080002
  102. #define RXFIFO_THR1_OOB 0x01800003
  103. /* PLA_RXFIFO_CTRL1 */
  104. #define RXFIFO_THR2_FULL 0x00000060
  105. #define RXFIFO_THR2_HIGH 0x00000038
  106. #define RXFIFO_THR2_OOB 0x0000004a
  107. /* PLA_RXFIFO_CTRL2 */
  108. #define RXFIFO_THR3_FULL 0x00000078
  109. #define RXFIFO_THR3_HIGH 0x00000048
  110. #define RXFIFO_THR3_OOB 0x0000005a
  111. /* PLA_TXFIFO_CTRL */
  112. #define TXFIFO_THR_NORMAL 0x00400008
  113. /* PLA_FMC */
  114. #define FMC_FCR_MCU_EN 0x0001
  115. /* PLA_EEEP_CR */
  116. #define EEEP_CR_EEEP_TX 0x0002
  117. /* PLA_TCR0 */
  118. #define TCR0_TX_EMPTY 0x0800
  119. #define TCR0_AUTO_FIFO 0x0080
  120. /* PLA_TCR1 */
  121. #define VERSION_MASK 0x7cf0
  122. /* PLA_CR */
  123. #define CR_RST 0x10
  124. #define CR_RE 0x08
  125. #define CR_TE 0x04
  126. /* PLA_CRWECR */
  127. #define CRWECR_NORAML 0x00
  128. #define CRWECR_CONFIG 0xc0
  129. /* PLA_OOB_CTRL */
  130. #define NOW_IS_OOB 0x80
  131. #define TXFIFO_EMPTY 0x20
  132. #define RXFIFO_EMPTY 0x10
  133. #define LINK_LIST_READY 0x02
  134. #define DIS_MCU_CLROOB 0x01
  135. #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
  136. /* PLA_MISC_1 */
  137. #define RXDY_GATED_EN 0x0008
  138. /* PLA_SFF_STS_7 */
  139. #define RE_INIT_LL 0x8000
  140. #define MCU_BORW_EN 0x4000
  141. /* PLA_CPCR */
  142. #define CPCR_RX_VLAN 0x0040
  143. /* PLA_CFG_WOL */
  144. #define MAGIC_EN 0x0001
  145. /* PAL_BDC_CR */
  146. #define ALDPS_PROXY_MODE 0x0001
  147. /* PLA_CONFIG5 */
  148. #define LAN_WAKE_EN 0x0002
  149. /* PLA_LED_FEATURE */
  150. #define LED_MODE_MASK 0x0700
  151. /* PLA_PHY_PWR */
  152. #define TX_10M_IDLE_EN 0x0080
  153. #define PFM_PWM_SWITCH 0x0040
  154. /* PLA_MAC_PWR_CTRL */
  155. #define D3_CLK_GATED_EN 0x00004000
  156. #define MCU_CLK_RATIO 0x07010f07
  157. #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
  158. /* PLA_GPHY_INTR_IMR */
  159. #define GPHY_STS_MSK 0x0001
  160. #define SPEED_DOWN_MSK 0x0002
  161. #define SPDWN_RXDV_MSK 0x0004
  162. #define SPDWN_LINKCHG_MSK 0x0008
  163. /* PLA_PHYAR */
  164. #define PHYAR_FLAG 0x80000000
  165. /* PLA_EEE_CR */
  166. #define EEE_RX_EN 0x0001
  167. #define EEE_TX_EN 0x0002
  168. /* USB_DEV_STAT */
  169. #define STAT_SPEED_MASK 0x0006
  170. #define STAT_SPEED_HIGH 0x0000
  171. #define STAT_SPEED_FULL 0x0001
  172. /* USB_TX_AGG */
  173. #define TX_AGG_MAX_THRESHOLD 0x03
  174. /* USB_RX_BUF_TH */
  175. #define RX_BUF_THR 0x7a120180
  176. /* USB_TX_DMA */
  177. #define TEST_MODE_DISABLE 0x00000001
  178. #define TX_SIZE_ADJUST1 0x00000100
  179. /* USB_UPS_CTRL */
  180. #define POWER_CUT 0x0100
  181. /* USB_PM_CTRL_STATUS */
  182. #define RWSUME_INDICATE 0x0001
  183. /* USB_USB_CTRL */
  184. #define RX_AGG_DISABLE 0x0010
  185. /* OCP_ALDPS_CONFIG */
  186. #define ENPWRSAVE 0x8000
  187. #define ENPDNPS 0x0200
  188. #define LINKENA 0x0100
  189. #define DIS_SDSAVE 0x0010
  190. /* OCP_EEE_CONFIG1 */
  191. #define RG_TXLPI_MSK_HFDUP 0x8000
  192. #define RG_MATCLR_EN 0x4000
  193. #define EEE_10_CAP 0x2000
  194. #define EEE_NWAY_EN 0x1000
  195. #define TX_QUIET_EN 0x0200
  196. #define RX_QUIET_EN 0x0100
  197. #define SDRISETIME 0x0010 /* bit 4 ~ 6 */
  198. #define RG_RXLPI_MSK_HFDUP 0x0008
  199. #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
  200. /* OCP_EEE_CONFIG2 */
  201. #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
  202. #define RG_DACQUIET_EN 0x0400
  203. #define RG_LDVQUIET_EN 0x0200
  204. #define RG_CKRSEL 0x0020
  205. #define RG_EEEPRG_EN 0x0010
  206. /* OCP_EEE_CONFIG3 */
  207. #define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
  208. #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
  209. #define MSK_PH 0x0006 /* bit 0 ~ 3 */
  210. /* OCP_EEE_AR */
  211. /* bit[15:14] function */
  212. #define FUN_ADDR 0x0000
  213. #define FUN_DATA 0x4000
  214. /* bit[4:0] device addr */
  215. #define DEVICE_ADDR 0x0007
  216. /* OCP_EEE_DATA */
  217. #define EEE_ADDR 0x003C
  218. #define EEE_DATA 0x0002
  219. enum rtl_register_content {
  220. _100bps = 0x08,
  221. _10bps = 0x04,
  222. LINK_STATUS = 0x02,
  223. FULL_DUP = 0x01,
  224. };
  225. #define RTL8152_REQT_READ 0xc0
  226. #define RTL8152_REQT_WRITE 0x40
  227. #define RTL8152_REQ_GET_REGS 0x05
  228. #define RTL8152_REQ_SET_REGS 0x05
  229. #define BYTE_EN_DWORD 0xff
  230. #define BYTE_EN_WORD 0x33
  231. #define BYTE_EN_BYTE 0x11
  232. #define BYTE_EN_SIX_BYTES 0x3f
  233. #define BYTE_EN_START_MASK 0x0f
  234. #define BYTE_EN_END_MASK 0xf0
  235. #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
  236. #define RTL8152_TX_TIMEOUT (HZ)
  237. /* rtl8152 flags */
  238. enum rtl8152_flags {
  239. RTL8152_UNPLUG = 0,
  240. RX_URB_FAIL,
  241. RTL8152_SET_RX_MODE,
  242. WORK_ENABLE
  243. };
  244. /* Define these values to match your device */
  245. #define VENDOR_ID_REALTEK 0x0bda
  246. #define PRODUCT_ID_RTL8152 0x8152
  247. #define MCU_TYPE_PLA 0x0100
  248. #define MCU_TYPE_USB 0x0000
  249. struct rx_desc {
  250. u32 opts1;
  251. #define RX_LEN_MASK 0x7fff
  252. u32 opts2;
  253. u32 opts3;
  254. u32 opts4;
  255. u32 opts5;
  256. u32 opts6;
  257. };
  258. struct tx_desc {
  259. u32 opts1;
  260. #define TX_FS (1 << 31) /* First segment of a packet */
  261. #define TX_LS (1 << 30) /* Final segment of a packet */
  262. #define TX_LEN_MASK 0xffff
  263. u32 opts2;
  264. };
  265. struct r8152 {
  266. unsigned long flags;
  267. struct usb_device *udev;
  268. struct tasklet_struct tl;
  269. struct net_device *netdev;
  270. struct urb *rx_urb, *tx_urb;
  271. struct sk_buff *tx_skb, *rx_skb;
  272. struct delayed_work schedule;
  273. struct mii_if_info mii;
  274. u32 msg_enable;
  275. u16 ocp_base;
  276. u8 version;
  277. u8 speed;
  278. };
  279. enum rtl_version {
  280. RTL_VER_UNKNOWN = 0,
  281. RTL_VER_01,
  282. RTL_VER_02
  283. };
  284. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  285. * The RTL chips use a 64 element hash table based on the Ethernet CRC.
  286. */
  287. static const int multicast_filter_limit = 32;
  288. static
  289. int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  290. {
  291. return usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
  292. RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
  293. value, index, data, size, 500);
  294. }
  295. static
  296. int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  297. {
  298. return usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
  299. RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
  300. value, index, data, size, 500);
  301. }
  302. static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
  303. void *data, u16 type)
  304. {
  305. u16 limit = 64;
  306. int ret = 0;
  307. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  308. return -ENODEV;
  309. /* both size and indix must be 4 bytes align */
  310. if ((size & 3) || !size || (index & 3) || !data)
  311. return -EPERM;
  312. if ((u32)index + (u32)size > 0xffff)
  313. return -EPERM;
  314. while (size) {
  315. if (size > limit) {
  316. ret = get_registers(tp, index, type, limit, data);
  317. if (ret < 0)
  318. break;
  319. index += limit;
  320. data += limit;
  321. size -= limit;
  322. } else {
  323. ret = get_registers(tp, index, type, size, data);
  324. if (ret < 0)
  325. break;
  326. index += size;
  327. data += size;
  328. size = 0;
  329. break;
  330. }
  331. }
  332. return ret;
  333. }
  334. static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
  335. u16 size, void *data, u16 type)
  336. {
  337. int ret;
  338. u16 byteen_start, byteen_end, byen;
  339. u16 limit = 512;
  340. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  341. return -ENODEV;
  342. /* both size and indix must be 4 bytes align */
  343. if ((size & 3) || !size || (index & 3) || !data)
  344. return -EPERM;
  345. if ((u32)index + (u32)size > 0xffff)
  346. return -EPERM;
  347. byteen_start = byteen & BYTE_EN_START_MASK;
  348. byteen_end = byteen & BYTE_EN_END_MASK;
  349. byen = byteen_start | (byteen_start << 4);
  350. ret = set_registers(tp, index, type | byen, 4, data);
  351. if (ret < 0)
  352. goto error1;
  353. index += 4;
  354. data += 4;
  355. size -= 4;
  356. if (size) {
  357. size -= 4;
  358. while (size) {
  359. if (size > limit) {
  360. ret = set_registers(tp, index,
  361. type | BYTE_EN_DWORD,
  362. limit, data);
  363. if (ret < 0)
  364. goto error1;
  365. index += limit;
  366. data += limit;
  367. size -= limit;
  368. } else {
  369. ret = set_registers(tp, index,
  370. type | BYTE_EN_DWORD,
  371. size, data);
  372. if (ret < 0)
  373. goto error1;
  374. index += size;
  375. data += size;
  376. size = 0;
  377. break;
  378. }
  379. }
  380. byen = byteen_end | (byteen_end >> 4);
  381. ret = set_registers(tp, index, type | byen, 4, data);
  382. if (ret < 0)
  383. goto error1;
  384. }
  385. error1:
  386. return ret;
  387. }
  388. static inline
  389. int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  390. {
  391. return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
  392. }
  393. static inline
  394. int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  395. {
  396. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
  397. }
  398. static inline
  399. int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  400. {
  401. return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
  402. }
  403. static inline
  404. int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  405. {
  406. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
  407. }
  408. static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
  409. {
  410. u32 data;
  411. if (type == MCU_TYPE_PLA)
  412. pla_ocp_read(tp, index, sizeof(data), &data);
  413. else
  414. usb_ocp_read(tp, index, sizeof(data), &data);
  415. return __le32_to_cpu(data);
  416. }
  417. static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
  418. {
  419. if (type == MCU_TYPE_PLA)
  420. pla_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(data), &data);
  421. else
  422. usb_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(data), &data);
  423. }
  424. static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
  425. {
  426. u32 data;
  427. u8 shift = index & 2;
  428. index &= ~3;
  429. if (type == MCU_TYPE_PLA)
  430. pla_ocp_read(tp, index, sizeof(data), &data);
  431. else
  432. usb_ocp_read(tp, index, sizeof(data), &data);
  433. data = __le32_to_cpu(data);
  434. data >>= (shift * 8);
  435. data &= 0xffff;
  436. return (u16)data;
  437. }
  438. static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
  439. {
  440. u32 tmp, mask = 0xffff;
  441. u16 byen = BYTE_EN_WORD;
  442. u8 shift = index & 2;
  443. data &= mask;
  444. if (index & 2) {
  445. byen <<= shift;
  446. mask <<= (shift * 8);
  447. data <<= (shift * 8);
  448. index &= ~3;
  449. }
  450. if (type == MCU_TYPE_PLA)
  451. pla_ocp_read(tp, index, sizeof(tmp), &tmp);
  452. else
  453. usb_ocp_read(tp, index, sizeof(tmp), &tmp);
  454. tmp = __le32_to_cpu(tmp) & ~mask;
  455. tmp |= data;
  456. tmp = __cpu_to_le32(tmp);
  457. if (type == MCU_TYPE_PLA)
  458. pla_ocp_write(tp, index, byen, sizeof(tmp), &tmp);
  459. else
  460. usb_ocp_write(tp, index, byen, sizeof(tmp), &tmp);
  461. }
  462. static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
  463. {
  464. u32 data;
  465. u8 shift = index & 3;
  466. index &= ~3;
  467. if (type == MCU_TYPE_PLA)
  468. pla_ocp_read(tp, index, sizeof(data), &data);
  469. else
  470. usb_ocp_read(tp, index, sizeof(data), &data);
  471. data = __le32_to_cpu(data);
  472. data >>= (shift * 8);
  473. data &= 0xff;
  474. return (u8)data;
  475. }
  476. static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
  477. {
  478. u32 tmp, mask = 0xff;
  479. u16 byen = BYTE_EN_BYTE;
  480. u8 shift = index & 3;
  481. data &= mask;
  482. if (index & 3) {
  483. byen <<= shift;
  484. mask <<= (shift * 8);
  485. data <<= (shift * 8);
  486. index &= ~3;
  487. }
  488. if (type == MCU_TYPE_PLA)
  489. pla_ocp_read(tp, index, sizeof(tmp), &tmp);
  490. else
  491. usb_ocp_read(tp, index, sizeof(tmp), &tmp);
  492. tmp = __le32_to_cpu(tmp) & ~mask;
  493. tmp |= data;
  494. tmp = __cpu_to_le32(tmp);
  495. if (type == MCU_TYPE_PLA)
  496. pla_ocp_write(tp, index, byen, sizeof(tmp), &tmp);
  497. else
  498. usb_ocp_write(tp, index, byen, sizeof(tmp), &tmp);
  499. }
  500. static void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
  501. {
  502. u32 ocp_data;
  503. int i;
  504. ocp_data = PHYAR_FLAG | ((reg_addr & 0x1f) << 16) |
  505. (value & 0xffff);
  506. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
  507. for (i = 20; i > 0; i--) {
  508. udelay(25);
  509. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
  510. if (!(ocp_data & PHYAR_FLAG))
  511. break;
  512. }
  513. udelay(20);
  514. }
  515. static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
  516. {
  517. u32 ocp_data;
  518. int i;
  519. ocp_data = (reg_addr & 0x1f) << 16;
  520. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
  521. for (i = 20; i > 0; i--) {
  522. udelay(25);
  523. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
  524. if (ocp_data & PHYAR_FLAG)
  525. break;
  526. }
  527. udelay(20);
  528. if (!(ocp_data & PHYAR_FLAG))
  529. return -EAGAIN;
  530. return (u16)(ocp_data & 0xffff);
  531. }
  532. static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
  533. {
  534. struct r8152 *tp = netdev_priv(netdev);
  535. if (phy_id != R8152_PHY_ID)
  536. return -EINVAL;
  537. return r8152_mdio_read(tp, reg);
  538. }
  539. static
  540. void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
  541. {
  542. struct r8152 *tp = netdev_priv(netdev);
  543. if (phy_id != R8152_PHY_ID)
  544. return;
  545. r8152_mdio_write(tp, reg, val);
  546. }
  547. static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
  548. {
  549. u16 ocp_base, ocp_index;
  550. ocp_base = addr & 0xf000;
  551. if (ocp_base != tp->ocp_base) {
  552. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  553. tp->ocp_base = ocp_base;
  554. }
  555. ocp_index = (addr & 0x0fff) | 0xb000;
  556. ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
  557. }
  558. static inline void set_ethernet_addr(struct r8152 *tp)
  559. {
  560. struct net_device *dev = tp->netdev;
  561. u8 *node_id;
  562. node_id = kmalloc(sizeof(u8) * 8, GFP_KERNEL);
  563. if (!node_id) {
  564. netif_err(tp, probe, dev, "out of memory");
  565. return;
  566. }
  567. if (pla_ocp_read(tp, PLA_IDR, sizeof(u8) * 8, node_id) < 0)
  568. netif_notice(tp, probe, dev, "inet addr fail\n");
  569. else {
  570. memcpy(dev->dev_addr, node_id, dev->addr_len);
  571. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  572. }
  573. kfree(node_id);
  574. }
  575. static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
  576. {
  577. struct r8152 *tp = netdev_priv(netdev);
  578. struct sockaddr *addr = p;
  579. if (!is_valid_ether_addr(addr->sa_data))
  580. return -EADDRNOTAVAIL;
  581. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  582. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  583. pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
  584. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  585. return 0;
  586. }
  587. static int alloc_all_urbs(struct r8152 *tp)
  588. {
  589. tp->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  590. if (!tp->rx_urb)
  591. return 0;
  592. tp->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  593. if (!tp->tx_urb) {
  594. usb_free_urb(tp->rx_urb);
  595. return 0;
  596. }
  597. return 1;
  598. }
  599. static void free_all_urbs(struct r8152 *tp)
  600. {
  601. usb_free_urb(tp->rx_urb);
  602. usb_free_urb(tp->tx_urb);
  603. }
  604. static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
  605. {
  606. return &dev->stats;
  607. }
  608. static void read_bulk_callback(struct urb *urb)
  609. {
  610. struct r8152 *tp;
  611. unsigned pkt_len;
  612. struct sk_buff *skb;
  613. struct net_device *netdev;
  614. struct net_device_stats *stats;
  615. int status = urb->status;
  616. int result;
  617. struct rx_desc *rx_desc;
  618. tp = urb->context;
  619. if (!tp)
  620. return;
  621. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  622. return;
  623. netdev = tp->netdev;
  624. if (!netif_device_present(netdev))
  625. return;
  626. stats = rtl8152_get_stats(netdev);
  627. switch (status) {
  628. case 0:
  629. break;
  630. case -ESHUTDOWN:
  631. set_bit(RTL8152_UNPLUG, &tp->flags);
  632. netif_device_detach(tp->netdev);
  633. case -ENOENT:
  634. return; /* the urb is in unlink state */
  635. case -ETIME:
  636. pr_warn_ratelimited("may be reset is needed?..\n");
  637. goto goon;
  638. default:
  639. pr_warn_ratelimited("Rx status %d\n", status);
  640. goto goon;
  641. }
  642. /* protect against short packets (tell me why we got some?!?) */
  643. if (urb->actual_length < sizeof(*rx_desc))
  644. goto goon;
  645. rx_desc = (struct rx_desc *)urb->transfer_buffer;
  646. pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
  647. if (urb->actual_length < sizeof(struct rx_desc) + pkt_len)
  648. goto goon;
  649. skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
  650. if (!skb)
  651. goto goon;
  652. memcpy(skb->data, tp->rx_skb->data + sizeof(struct rx_desc), pkt_len);
  653. skb_put(skb, pkt_len);
  654. skb->protocol = eth_type_trans(skb, netdev);
  655. netif_rx(skb);
  656. stats->rx_packets++;
  657. stats->rx_bytes += pkt_len;
  658. goon:
  659. usb_fill_bulk_urb(tp->rx_urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
  660. tp->rx_skb->data, RTL8152_RMS + sizeof(struct rx_desc),
  661. (usb_complete_t)read_bulk_callback, tp);
  662. result = usb_submit_urb(tp->rx_urb, GFP_ATOMIC);
  663. if (result == -ENODEV) {
  664. netif_device_detach(tp->netdev);
  665. } else if (result) {
  666. set_bit(RX_URB_FAIL, &tp->flags);
  667. goto resched;
  668. } else {
  669. clear_bit(RX_URB_FAIL, &tp->flags);
  670. }
  671. return;
  672. resched:
  673. tasklet_schedule(&tp->tl);
  674. }
  675. static void rx_fixup(unsigned long data)
  676. {
  677. struct r8152 *tp;
  678. int status;
  679. tp = (struct r8152 *)data;
  680. if (!test_bit(WORK_ENABLE, &tp->flags))
  681. return;
  682. status = usb_submit_urb(tp->rx_urb, GFP_ATOMIC);
  683. if (status == -ENODEV) {
  684. netif_device_detach(tp->netdev);
  685. } else if (status) {
  686. set_bit(RX_URB_FAIL, &tp->flags);
  687. goto tlsched;
  688. } else {
  689. clear_bit(RX_URB_FAIL, &tp->flags);
  690. }
  691. return;
  692. tlsched:
  693. tasklet_schedule(&tp->tl);
  694. }
  695. static void write_bulk_callback(struct urb *urb)
  696. {
  697. struct r8152 *tp;
  698. int status = urb->status;
  699. tp = urb->context;
  700. if (!tp)
  701. return;
  702. dev_kfree_skb_irq(tp->tx_skb);
  703. if (!netif_device_present(tp->netdev))
  704. return;
  705. if (status)
  706. dev_info(&urb->dev->dev, "%s: Tx status %d\n",
  707. tp->netdev->name, status);
  708. tp->netdev->trans_start = jiffies;
  709. netif_wake_queue(tp->netdev);
  710. }
  711. static void rtl8152_tx_timeout(struct net_device *netdev)
  712. {
  713. struct r8152 *tp = netdev_priv(netdev);
  714. struct net_device_stats *stats = rtl8152_get_stats(netdev);
  715. netif_warn(tp, tx_err, netdev, "Tx timeout.\n");
  716. usb_unlink_urb(tp->tx_urb);
  717. stats->tx_errors++;
  718. }
  719. static void rtl8152_set_rx_mode(struct net_device *netdev)
  720. {
  721. struct r8152 *tp = netdev_priv(netdev);
  722. if (tp->speed & LINK_STATUS)
  723. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  724. }
  725. static void _rtl8152_set_rx_mode(struct net_device *netdev)
  726. {
  727. struct r8152 *tp = netdev_priv(netdev);
  728. u32 tmp, *mc_filter; /* Multicast hash filter */
  729. u32 ocp_data;
  730. mc_filter = kmalloc(sizeof(u32) * 2, GFP_KERNEL);
  731. if (!mc_filter) {
  732. netif_err(tp, link, netdev, "out of memory");
  733. return;
  734. }
  735. clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
  736. netif_stop_queue(netdev);
  737. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  738. ocp_data &= ~RCR_ACPT_ALL;
  739. ocp_data |= RCR_AB | RCR_APM;
  740. if (netdev->flags & IFF_PROMISC) {
  741. /* Unconditionally log net taps. */
  742. netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
  743. ocp_data |= RCR_AM | RCR_AAP;
  744. mc_filter[1] = mc_filter[0] = 0xffffffff;
  745. } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
  746. (netdev->flags & IFF_ALLMULTI)) {
  747. /* Too many to filter perfectly -- accept all multicasts. */
  748. ocp_data |= RCR_AM;
  749. mc_filter[1] = mc_filter[0] = 0xffffffff;
  750. } else {
  751. struct netdev_hw_addr *ha;
  752. mc_filter[1] = mc_filter[0] = 0;
  753. netdev_for_each_mc_addr(ha, netdev) {
  754. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  755. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  756. ocp_data |= RCR_AM;
  757. }
  758. }
  759. tmp = mc_filter[0];
  760. mc_filter[0] = __cpu_to_le32(swab32(mc_filter[1]));
  761. mc_filter[1] = __cpu_to_le32(swab32(tmp));
  762. pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(u32) * 2, mc_filter);
  763. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  764. netif_wake_queue(netdev);
  765. kfree(mc_filter);
  766. }
  767. static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
  768. struct net_device *netdev)
  769. {
  770. struct r8152 *tp = netdev_priv(netdev);
  771. struct net_device_stats *stats = rtl8152_get_stats(netdev);
  772. struct tx_desc *tx_desc;
  773. int len, res;
  774. netif_stop_queue(netdev);
  775. len = skb->len;
  776. if (skb_header_cloned(skb) || skb_headroom(skb) < sizeof(*tx_desc)) {
  777. struct sk_buff *tx_skb;
  778. tx_skb = skb_copy_expand(skb, sizeof(*tx_desc), 0, GFP_ATOMIC);
  779. dev_kfree_skb_any(skb);
  780. if (!tx_skb) {
  781. stats->tx_dropped++;
  782. netif_wake_queue(netdev);
  783. return NETDEV_TX_OK;
  784. }
  785. skb = tx_skb;
  786. }
  787. tx_desc = (struct tx_desc *)skb_push(skb, sizeof(*tx_desc));
  788. memset(tx_desc, 0, sizeof(*tx_desc));
  789. tx_desc->opts1 = cpu_to_le32((len & TX_LEN_MASK) | TX_FS | TX_LS);
  790. tp->tx_skb = skb;
  791. skb_tx_timestamp(skb);
  792. usb_fill_bulk_urb(tp->tx_urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
  793. skb->data, skb->len,
  794. (usb_complete_t)write_bulk_callback, tp);
  795. res = usb_submit_urb(tp->tx_urb, GFP_ATOMIC);
  796. if (res) {
  797. /* Can we get/handle EPIPE here? */
  798. if (res == -ENODEV) {
  799. netif_device_detach(tp->netdev);
  800. } else {
  801. netif_warn(tp, tx_err, netdev,
  802. "failed tx_urb %d\n", res);
  803. stats->tx_errors++;
  804. netif_start_queue(netdev);
  805. }
  806. } else {
  807. stats->tx_packets++;
  808. stats->tx_bytes += skb->len;
  809. }
  810. return NETDEV_TX_OK;
  811. }
  812. static void r8152b_reset_packet_filter(struct r8152 *tp)
  813. {
  814. u32 ocp_data;
  815. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
  816. ocp_data &= ~FMC_FCR_MCU_EN;
  817. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  818. ocp_data |= FMC_FCR_MCU_EN;
  819. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  820. }
  821. static void rtl8152_nic_reset(struct r8152 *tp)
  822. {
  823. int i;
  824. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
  825. for (i = 0; i < 1000; i++) {
  826. if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
  827. break;
  828. udelay(100);
  829. }
  830. }
  831. static inline u8 rtl8152_get_speed(struct r8152 *tp)
  832. {
  833. return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
  834. }
  835. static int rtl8152_enable(struct r8152 *tp)
  836. {
  837. u32 ocp_data;
  838. u8 speed;
  839. speed = rtl8152_get_speed(tp);
  840. if (speed & _100bps) {
  841. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  842. ocp_data &= ~EEEP_CR_EEEP_TX;
  843. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  844. } else {
  845. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  846. ocp_data |= EEEP_CR_EEEP_TX;
  847. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  848. }
  849. r8152b_reset_packet_filter(tp);
  850. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
  851. ocp_data |= CR_RE | CR_TE;
  852. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
  853. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  854. ocp_data &= ~RXDY_GATED_EN;
  855. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  856. usb_fill_bulk_urb(tp->rx_urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
  857. tp->rx_skb->data, RTL8152_RMS + sizeof(struct rx_desc),
  858. (usb_complete_t)read_bulk_callback, tp);
  859. return usb_submit_urb(tp->rx_urb, GFP_KERNEL);
  860. }
  861. static void rtl8152_disable(struct r8152 *tp)
  862. {
  863. u32 ocp_data;
  864. int i;
  865. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  866. ocp_data &= ~RCR_ACPT_ALL;
  867. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  868. usb_kill_urb(tp->tx_urb);
  869. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  870. ocp_data |= RXDY_GATED_EN;
  871. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  872. for (i = 0; i < 1000; i++) {
  873. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  874. if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
  875. break;
  876. mdelay(1);
  877. }
  878. for (i = 0; i < 1000; i++) {
  879. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
  880. break;
  881. mdelay(1);
  882. }
  883. usb_kill_urb(tp->rx_urb);
  884. rtl8152_nic_reset(tp);
  885. }
  886. static void r8152b_exit_oob(struct r8152 *tp)
  887. {
  888. u32 ocp_data;
  889. int i;
  890. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  891. ocp_data &= ~RCR_ACPT_ALL;
  892. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  893. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  894. ocp_data |= RXDY_GATED_EN;
  895. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  896. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  897. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
  898. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  899. ocp_data &= ~NOW_IS_OOB;
  900. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  901. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  902. ocp_data &= ~MCU_BORW_EN;
  903. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  904. for (i = 0; i < 1000; i++) {
  905. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  906. if (ocp_data & LINK_LIST_READY)
  907. break;
  908. mdelay(1);
  909. }
  910. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  911. ocp_data |= RE_INIT_LL;
  912. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  913. for (i = 0; i < 1000; i++) {
  914. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  915. if (ocp_data & LINK_LIST_READY)
  916. break;
  917. mdelay(1);
  918. }
  919. rtl8152_nic_reset(tp);
  920. /* rx share fifo credit full threshold */
  921. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  922. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
  923. ocp_data &= STAT_SPEED_MASK;
  924. if (ocp_data == STAT_SPEED_FULL) {
  925. /* rx share fifo credit near full threshold */
  926. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  927. RXFIFO_THR2_FULL);
  928. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  929. RXFIFO_THR3_FULL);
  930. } else {
  931. /* rx share fifo credit near full threshold */
  932. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  933. RXFIFO_THR2_HIGH);
  934. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  935. RXFIFO_THR3_HIGH);
  936. }
  937. /* TX share fifo free credit full threshold */
  938. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
  939. ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
  940. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_BUF_THR);
  941. ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
  942. TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
  943. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  944. ocp_data &= ~CPCR_RX_VLAN;
  945. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  946. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  947. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  948. ocp_data |= TCR0_AUTO_FIFO;
  949. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  950. }
  951. static void r8152b_enter_oob(struct r8152 *tp)
  952. {
  953. u32 ocp_data;
  954. int i;
  955. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  956. ocp_data &= ~NOW_IS_OOB;
  957. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  958. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
  959. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
  960. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
  961. rtl8152_disable(tp);
  962. for (i = 0; i < 1000; i++) {
  963. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  964. if (ocp_data & LINK_LIST_READY)
  965. break;
  966. mdelay(1);
  967. }
  968. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  969. ocp_data |= RE_INIT_LL;
  970. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  971. for (i = 0; i < 1000; i++) {
  972. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  973. if (ocp_data & LINK_LIST_READY)
  974. break;
  975. mdelay(1);
  976. }
  977. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  978. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  979. ocp_data |= MAGIC_EN;
  980. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
  981. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  982. ocp_data |= CPCR_RX_VLAN;
  983. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  984. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  985. ocp_data |= ALDPS_PROXY_MODE;
  986. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  987. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  988. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  989. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  990. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
  991. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  992. ocp_data &= ~RXDY_GATED_EN;
  993. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  994. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  995. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  996. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  997. }
  998. static void r8152b_disable_aldps(struct r8152 *tp)
  999. {
  1000. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
  1001. msleep(20);
  1002. }
  1003. static inline void r8152b_enable_aldps(struct r8152 *tp)
  1004. {
  1005. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
  1006. LINKENA | DIS_SDSAVE);
  1007. }
  1008. static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
  1009. {
  1010. u16 bmcr, anar;
  1011. int ret = 0;
  1012. cancel_delayed_work_sync(&tp->schedule);
  1013. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  1014. anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  1015. ADVERTISE_100HALF | ADVERTISE_100FULL);
  1016. if (autoneg == AUTONEG_DISABLE) {
  1017. if (speed == SPEED_10) {
  1018. bmcr = 0;
  1019. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1020. } else if (speed == SPEED_100) {
  1021. bmcr = BMCR_SPEED100;
  1022. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  1023. } else {
  1024. ret = -EINVAL;
  1025. goto out;
  1026. }
  1027. if (duplex == DUPLEX_FULL)
  1028. bmcr |= BMCR_FULLDPLX;
  1029. } else {
  1030. if (speed == SPEED_10) {
  1031. if (duplex == DUPLEX_FULL)
  1032. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1033. else
  1034. anar |= ADVERTISE_10HALF;
  1035. } else if (speed == SPEED_100) {
  1036. if (duplex == DUPLEX_FULL) {
  1037. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1038. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  1039. } else {
  1040. anar |= ADVERTISE_10HALF;
  1041. anar |= ADVERTISE_100HALF;
  1042. }
  1043. } else {
  1044. ret = -EINVAL;
  1045. goto out;
  1046. }
  1047. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  1048. }
  1049. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  1050. r8152_mdio_write(tp, MII_BMCR, bmcr);
  1051. out:
  1052. schedule_delayed_work(&tp->schedule, 5 * HZ);
  1053. return ret;
  1054. }
  1055. static void rtl8152_down(struct r8152 *tp)
  1056. {
  1057. u32 ocp_data;
  1058. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1059. ocp_data &= ~POWER_CUT;
  1060. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1061. r8152b_disable_aldps(tp);
  1062. r8152b_enter_oob(tp);
  1063. r8152b_enable_aldps(tp);
  1064. }
  1065. static void set_carrier(struct r8152 *tp)
  1066. {
  1067. struct net_device *netdev = tp->netdev;
  1068. u8 speed;
  1069. speed = rtl8152_get_speed(tp);
  1070. if (speed & LINK_STATUS) {
  1071. if (!(tp->speed & LINK_STATUS)) {
  1072. rtl8152_enable(tp);
  1073. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1074. netif_carrier_on(netdev);
  1075. }
  1076. } else {
  1077. if (tp->speed & LINK_STATUS) {
  1078. netif_carrier_off(netdev);
  1079. rtl8152_disable(tp);
  1080. }
  1081. }
  1082. tp->speed = speed;
  1083. }
  1084. static void rtl_work_func_t(struct work_struct *work)
  1085. {
  1086. struct r8152 *tp = container_of(work, struct r8152, schedule.work);
  1087. if (!test_bit(WORK_ENABLE, &tp->flags))
  1088. goto out1;
  1089. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1090. goto out1;
  1091. set_carrier(tp);
  1092. if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
  1093. _rtl8152_set_rx_mode(tp->netdev);
  1094. schedule_delayed_work(&tp->schedule, HZ);
  1095. out1:
  1096. return;
  1097. }
  1098. static int rtl8152_open(struct net_device *netdev)
  1099. {
  1100. struct r8152 *tp = netdev_priv(netdev);
  1101. int res = 0;
  1102. tp->speed = rtl8152_get_speed(tp);
  1103. if (tp->speed & LINK_STATUS) {
  1104. res = rtl8152_enable(tp);
  1105. if (res) {
  1106. if (res == -ENODEV)
  1107. netif_device_detach(tp->netdev);
  1108. netif_err(tp, ifup, netdev,
  1109. "rtl8152_open failed: %d\n", res);
  1110. return res;
  1111. }
  1112. netif_carrier_on(netdev);
  1113. } else {
  1114. netif_stop_queue(netdev);
  1115. netif_carrier_off(netdev);
  1116. }
  1117. rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL);
  1118. netif_start_queue(netdev);
  1119. set_bit(WORK_ENABLE, &tp->flags);
  1120. schedule_delayed_work(&tp->schedule, 0);
  1121. return res;
  1122. }
  1123. static int rtl8152_close(struct net_device *netdev)
  1124. {
  1125. struct r8152 *tp = netdev_priv(netdev);
  1126. int res = 0;
  1127. clear_bit(WORK_ENABLE, &tp->flags);
  1128. cancel_delayed_work_sync(&tp->schedule);
  1129. netif_stop_queue(netdev);
  1130. rtl8152_disable(tp);
  1131. return res;
  1132. }
  1133. static void rtl_clear_bp(struct r8152 *tp)
  1134. {
  1135. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
  1136. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
  1137. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
  1138. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
  1139. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
  1140. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
  1141. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
  1142. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
  1143. mdelay(3);
  1144. ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
  1145. ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
  1146. }
  1147. static void r8152b_enable_eee(struct r8152 *tp)
  1148. {
  1149. u32 ocp_data;
  1150. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  1151. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  1152. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  1153. ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
  1154. EEE_10_CAP | EEE_NWAY_EN |
  1155. TX_QUIET_EN | RX_QUIET_EN |
  1156. SDRISETIME | RG_RXLPI_MSK_HFDUP |
  1157. SDFALLTIME);
  1158. ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
  1159. RG_LDVQUIET_EN | RG_CKRSEL |
  1160. RG_EEEPRG_EN);
  1161. ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
  1162. ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
  1163. ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
  1164. ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
  1165. ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
  1166. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  1167. }
  1168. static void r8152b_enable_fc(struct r8152 *tp)
  1169. {
  1170. u16 anar;
  1171. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  1172. anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1173. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  1174. }
  1175. static void r8152b_hw_phy_cfg(struct r8152 *tp)
  1176. {
  1177. r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
  1178. r8152b_disable_aldps(tp);
  1179. }
  1180. static void r8152b_init(struct r8152 *tp)
  1181. {
  1182. u32 ocp_data;
  1183. int i;
  1184. rtl_clear_bp(tp);
  1185. if (tp->version == RTL_VER_01) {
  1186. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  1187. ocp_data &= ~LED_MODE_MASK;
  1188. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  1189. }
  1190. r8152b_hw_phy_cfg(tp);
  1191. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1192. ocp_data &= ~POWER_CUT;
  1193. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1194. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  1195. ocp_data &= ~RWSUME_INDICATE;
  1196. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  1197. r8152b_exit_oob(tp);
  1198. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  1199. ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
  1200. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  1201. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
  1202. ocp_data &= ~MCU_CLK_RATIO_MASK;
  1203. ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
  1204. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
  1205. ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
  1206. SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
  1207. ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
  1208. r8152b_enable_eee(tp);
  1209. r8152b_enable_aldps(tp);
  1210. r8152b_enable_fc(tp);
  1211. r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
  1212. BMCR_ANRESTART);
  1213. for (i = 0; i < 100; i++) {
  1214. udelay(100);
  1215. if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
  1216. break;
  1217. }
  1218. /* disable rx aggregation */
  1219. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  1220. ocp_data |= RX_AGG_DISABLE;
  1221. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  1222. }
  1223. static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
  1224. {
  1225. struct r8152 *tp = usb_get_intfdata(intf);
  1226. netif_device_detach(tp->netdev);
  1227. if (netif_running(tp->netdev)) {
  1228. clear_bit(WORK_ENABLE, &tp->flags);
  1229. cancel_delayed_work_sync(&tp->schedule);
  1230. }
  1231. rtl8152_down(tp);
  1232. return 0;
  1233. }
  1234. static int rtl8152_resume(struct usb_interface *intf)
  1235. {
  1236. struct r8152 *tp = usb_get_intfdata(intf);
  1237. r8152b_init(tp);
  1238. netif_device_attach(tp->netdev);
  1239. if (netif_running(tp->netdev)) {
  1240. rtl8152_enable(tp);
  1241. set_bit(WORK_ENABLE, &tp->flags);
  1242. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1243. schedule_delayed_work(&tp->schedule, 0);
  1244. }
  1245. return 0;
  1246. }
  1247. static void rtl8152_get_drvinfo(struct net_device *netdev,
  1248. struct ethtool_drvinfo *info)
  1249. {
  1250. struct r8152 *tp = netdev_priv(netdev);
  1251. strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
  1252. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  1253. usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
  1254. }
  1255. static
  1256. int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  1257. {
  1258. struct r8152 *tp = netdev_priv(netdev);
  1259. if (!tp->mii.mdio_read)
  1260. return -EOPNOTSUPP;
  1261. return mii_ethtool_gset(&tp->mii, cmd);
  1262. }
  1263. static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1264. {
  1265. struct r8152 *tp = netdev_priv(dev);
  1266. return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
  1267. }
  1268. static struct ethtool_ops ops = {
  1269. .get_drvinfo = rtl8152_get_drvinfo,
  1270. .get_settings = rtl8152_get_settings,
  1271. .set_settings = rtl8152_set_settings,
  1272. .get_link = ethtool_op_get_link,
  1273. };
  1274. static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  1275. {
  1276. struct r8152 *tp = netdev_priv(netdev);
  1277. struct mii_ioctl_data *data = if_mii(rq);
  1278. int res = 0;
  1279. switch (cmd) {
  1280. case SIOCGMIIPHY:
  1281. data->phy_id = R8152_PHY_ID; /* Internal PHY */
  1282. break;
  1283. case SIOCGMIIREG:
  1284. data->val_out = r8152_mdio_read(tp, data->reg_num);
  1285. break;
  1286. case SIOCSMIIREG:
  1287. if (!capable(CAP_NET_ADMIN)) {
  1288. res = -EPERM;
  1289. break;
  1290. }
  1291. r8152_mdio_write(tp, data->reg_num, data->val_in);
  1292. break;
  1293. default:
  1294. res = -EOPNOTSUPP;
  1295. }
  1296. return res;
  1297. }
  1298. static const struct net_device_ops rtl8152_netdev_ops = {
  1299. .ndo_open = rtl8152_open,
  1300. .ndo_stop = rtl8152_close,
  1301. .ndo_do_ioctl = rtl8152_ioctl,
  1302. .ndo_start_xmit = rtl8152_start_xmit,
  1303. .ndo_tx_timeout = rtl8152_tx_timeout,
  1304. .ndo_set_rx_mode = rtl8152_set_rx_mode,
  1305. .ndo_set_mac_address = rtl8152_set_mac_address,
  1306. .ndo_change_mtu = eth_change_mtu,
  1307. .ndo_validate_addr = eth_validate_addr,
  1308. };
  1309. static void r8152b_get_version(struct r8152 *tp)
  1310. {
  1311. u32 ocp_data;
  1312. u16 version;
  1313. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
  1314. version = (u16)(ocp_data & VERSION_MASK);
  1315. switch (version) {
  1316. case 0x4c00:
  1317. tp->version = RTL_VER_01;
  1318. break;
  1319. case 0x4c10:
  1320. tp->version = RTL_VER_02;
  1321. break;
  1322. default:
  1323. netif_info(tp, probe, tp->netdev,
  1324. "Unknown version 0x%04x\n", version);
  1325. break;
  1326. }
  1327. }
  1328. static int rtl8152_probe(struct usb_interface *intf,
  1329. const struct usb_device_id *id)
  1330. {
  1331. struct usb_device *udev = interface_to_usbdev(intf);
  1332. struct r8152 *tp;
  1333. struct net_device *netdev;
  1334. if (udev->actconfig->desc.bConfigurationValue != 1) {
  1335. usb_driver_set_configuration(udev, 1);
  1336. return -ENODEV;
  1337. }
  1338. netdev = alloc_etherdev(sizeof(struct r8152));
  1339. if (!netdev) {
  1340. dev_err(&intf->dev, "Out of memory");
  1341. return -ENOMEM;
  1342. }
  1343. tp = netdev_priv(netdev);
  1344. tp->msg_enable = 0x7FFF;
  1345. tasklet_init(&tp->tl, rx_fixup, (unsigned long)tp);
  1346. INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
  1347. tp->udev = udev;
  1348. tp->netdev = netdev;
  1349. netdev->netdev_ops = &rtl8152_netdev_ops;
  1350. netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
  1351. netdev->features &= ~NETIF_F_IP_CSUM;
  1352. SET_ETHTOOL_OPS(netdev, &ops);
  1353. tp->speed = 0;
  1354. tp->mii.dev = netdev;
  1355. tp->mii.mdio_read = read_mii_word;
  1356. tp->mii.mdio_write = write_mii_word;
  1357. tp->mii.phy_id_mask = 0x3f;
  1358. tp->mii.reg_num_mask = 0x1f;
  1359. tp->mii.phy_id = R8152_PHY_ID;
  1360. tp->mii.supports_gmii = 0;
  1361. r8152b_get_version(tp);
  1362. r8152b_init(tp);
  1363. set_ethernet_addr(tp);
  1364. if (!alloc_all_urbs(tp)) {
  1365. netif_err(tp, probe, netdev, "out of memory");
  1366. goto out;
  1367. }
  1368. tp->rx_skb = netdev_alloc_skb(netdev,
  1369. RTL8152_RMS + sizeof(struct rx_desc));
  1370. if (!tp->rx_skb)
  1371. goto out1;
  1372. usb_set_intfdata(intf, tp);
  1373. SET_NETDEV_DEV(netdev, &intf->dev);
  1374. if (register_netdev(netdev) != 0) {
  1375. netif_err(tp, probe, netdev, "couldn't register the device");
  1376. goto out2;
  1377. }
  1378. netif_info(tp, probe, netdev, "%s", DRIVER_VERSION);
  1379. return 0;
  1380. out2:
  1381. usb_set_intfdata(intf, NULL);
  1382. dev_kfree_skb(tp->rx_skb);
  1383. out1:
  1384. free_all_urbs(tp);
  1385. out:
  1386. free_netdev(netdev);
  1387. return -EIO;
  1388. }
  1389. static void rtl8152_unload(struct r8152 *tp)
  1390. {
  1391. u32 ocp_data;
  1392. if (tp->version != RTL_VER_01) {
  1393. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1394. ocp_data |= POWER_CUT;
  1395. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1396. }
  1397. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  1398. ocp_data &= ~RWSUME_INDICATE;
  1399. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  1400. }
  1401. static void rtl8152_disconnect(struct usb_interface *intf)
  1402. {
  1403. struct r8152 *tp = usb_get_intfdata(intf);
  1404. usb_set_intfdata(intf, NULL);
  1405. if (tp) {
  1406. set_bit(RTL8152_UNPLUG, &tp->flags);
  1407. tasklet_kill(&tp->tl);
  1408. unregister_netdev(tp->netdev);
  1409. rtl8152_unload(tp);
  1410. free_all_urbs(tp);
  1411. if (tp->rx_skb)
  1412. dev_kfree_skb(tp->rx_skb);
  1413. free_netdev(tp->netdev);
  1414. }
  1415. }
  1416. /* table of devices that work with this driver */
  1417. static struct usb_device_id rtl8152_table[] = {
  1418. {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
  1419. {}
  1420. };
  1421. MODULE_DEVICE_TABLE(usb, rtl8152_table);
  1422. static struct usb_driver rtl8152_driver = {
  1423. .name = MODULENAME,
  1424. .probe = rtl8152_probe,
  1425. .disconnect = rtl8152_disconnect,
  1426. .id_table = rtl8152_table,
  1427. .suspend = rtl8152_suspend,
  1428. .resume = rtl8152_resume
  1429. };
  1430. module_usb_driver(rtl8152_driver);
  1431. MODULE_AUTHOR(DRIVER_AUTHOR);
  1432. MODULE_DESCRIPTION(DRIVER_DESC);
  1433. MODULE_LICENSE("GPL");