r8152.c 41 KB

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