r8152.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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. int ret;
  292. void *tmp;
  293. tmp = kmalloc(size, GFP_KERNEL);
  294. if (!tmp)
  295. return -ENOMEM;
  296. ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
  297. RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
  298. value, index, tmp, size, 500);
  299. memcpy(data, tmp, size);
  300. kfree(tmp);
  301. return ret;
  302. }
  303. static
  304. int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  305. {
  306. int ret;
  307. void *tmp;
  308. tmp = kmalloc(size, GFP_KERNEL);
  309. if (!tmp)
  310. return -ENOMEM;
  311. memcpy(tmp, data, size);
  312. ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
  313. RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
  314. value, index, tmp, size, 500);
  315. kfree(tmp);
  316. return ret;
  317. }
  318. static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
  319. void *data, u16 type)
  320. {
  321. u16 limit = 64;
  322. int ret = 0;
  323. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  324. return -ENODEV;
  325. /* both size and indix must be 4 bytes align */
  326. if ((size & 3) || !size || (index & 3) || !data)
  327. return -EPERM;
  328. if ((u32)index + (u32)size > 0xffff)
  329. return -EPERM;
  330. while (size) {
  331. if (size > limit) {
  332. ret = get_registers(tp, index, type, limit, data);
  333. if (ret < 0)
  334. break;
  335. index += limit;
  336. data += limit;
  337. size -= limit;
  338. } else {
  339. ret = get_registers(tp, index, type, size, data);
  340. if (ret < 0)
  341. break;
  342. index += size;
  343. data += size;
  344. size = 0;
  345. break;
  346. }
  347. }
  348. return ret;
  349. }
  350. static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
  351. u16 size, void *data, u16 type)
  352. {
  353. int ret;
  354. u16 byteen_start, byteen_end, byen;
  355. u16 limit = 512;
  356. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  357. return -ENODEV;
  358. /* both size and indix must be 4 bytes align */
  359. if ((size & 3) || !size || (index & 3) || !data)
  360. return -EPERM;
  361. if ((u32)index + (u32)size > 0xffff)
  362. return -EPERM;
  363. byteen_start = byteen & BYTE_EN_START_MASK;
  364. byteen_end = byteen & BYTE_EN_END_MASK;
  365. byen = byteen_start | (byteen_start << 4);
  366. ret = set_registers(tp, index, type | byen, 4, data);
  367. if (ret < 0)
  368. goto error1;
  369. index += 4;
  370. data += 4;
  371. size -= 4;
  372. if (size) {
  373. size -= 4;
  374. while (size) {
  375. if (size > limit) {
  376. ret = set_registers(tp, index,
  377. type | BYTE_EN_DWORD,
  378. limit, data);
  379. if (ret < 0)
  380. goto error1;
  381. index += limit;
  382. data += limit;
  383. size -= limit;
  384. } else {
  385. ret = set_registers(tp, index,
  386. type | BYTE_EN_DWORD,
  387. size, data);
  388. if (ret < 0)
  389. goto error1;
  390. index += size;
  391. data += size;
  392. size = 0;
  393. break;
  394. }
  395. }
  396. byen = byteen_end | (byteen_end >> 4);
  397. ret = set_registers(tp, index, type | byen, 4, data);
  398. if (ret < 0)
  399. goto error1;
  400. }
  401. error1:
  402. return ret;
  403. }
  404. static inline
  405. int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  406. {
  407. return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
  408. }
  409. static inline
  410. int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  411. {
  412. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
  413. }
  414. static inline
  415. int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  416. {
  417. return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
  418. }
  419. static inline
  420. int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  421. {
  422. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
  423. }
  424. static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
  425. {
  426. __le32 data;
  427. generic_ocp_read(tp, index, sizeof(data), &data, type);
  428. return __le32_to_cpu(data);
  429. }
  430. static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
  431. {
  432. __le32 tmp = __cpu_to_le32(data);
  433. generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
  434. }
  435. static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
  436. {
  437. u32 data;
  438. __le32 tmp;
  439. u8 shift = index & 2;
  440. index &= ~3;
  441. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  442. data = __le32_to_cpu(tmp);
  443. data >>= (shift * 8);
  444. data &= 0xffff;
  445. return (u16)data;
  446. }
  447. static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
  448. {
  449. u32 mask = 0xffff;
  450. __le32 tmp;
  451. u16 byen = BYTE_EN_WORD;
  452. u8 shift = index & 2;
  453. data &= mask;
  454. if (index & 2) {
  455. byen <<= shift;
  456. mask <<= (shift * 8);
  457. data <<= (shift * 8);
  458. index &= ~3;
  459. }
  460. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  461. data |= __le32_to_cpu(tmp) & ~mask;
  462. tmp = __cpu_to_le32(data);
  463. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  464. }
  465. static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
  466. {
  467. u32 data;
  468. __le32 tmp;
  469. u8 shift = index & 3;
  470. index &= ~3;
  471. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  472. data = __le32_to_cpu(tmp);
  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 mask = 0xff;
  480. __le32 tmp;
  481. u16 byen = BYTE_EN_BYTE;
  482. u8 shift = index & 3;
  483. data &= mask;
  484. if (index & 3) {
  485. byen <<= shift;
  486. mask <<= (shift * 8);
  487. data <<= (shift * 8);
  488. index &= ~3;
  489. }
  490. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  491. data |= __le32_to_cpu(tmp) & ~mask;
  492. tmp = __cpu_to_le32(data);
  493. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  494. }
  495. static void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
  496. {
  497. u32 ocp_data;
  498. int i;
  499. ocp_data = PHYAR_FLAG | ((reg_addr & 0x1f) << 16) |
  500. (value & 0xffff);
  501. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
  502. for (i = 20; i > 0; i--) {
  503. udelay(25);
  504. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
  505. if (!(ocp_data & PHYAR_FLAG))
  506. break;
  507. }
  508. udelay(20);
  509. }
  510. static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
  511. {
  512. u32 ocp_data;
  513. int i;
  514. ocp_data = (reg_addr & 0x1f) << 16;
  515. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
  516. for (i = 20; i > 0; i--) {
  517. udelay(25);
  518. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
  519. if (ocp_data & PHYAR_FLAG)
  520. break;
  521. }
  522. udelay(20);
  523. if (!(ocp_data & PHYAR_FLAG))
  524. return -EAGAIN;
  525. return (u16)(ocp_data & 0xffff);
  526. }
  527. static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
  528. {
  529. struct r8152 *tp = netdev_priv(netdev);
  530. if (phy_id != R8152_PHY_ID)
  531. return -EINVAL;
  532. return r8152_mdio_read(tp, reg);
  533. }
  534. static
  535. void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
  536. {
  537. struct r8152 *tp = netdev_priv(netdev);
  538. if (phy_id != R8152_PHY_ID)
  539. return;
  540. r8152_mdio_write(tp, reg, val);
  541. }
  542. static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
  543. {
  544. u16 ocp_base, ocp_index;
  545. ocp_base = addr & 0xf000;
  546. if (ocp_base != tp->ocp_base) {
  547. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  548. tp->ocp_base = ocp_base;
  549. }
  550. ocp_index = (addr & 0x0fff) | 0xb000;
  551. ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
  552. }
  553. static inline void set_ethernet_addr(struct r8152 *tp)
  554. {
  555. struct net_device *dev = tp->netdev;
  556. u8 node_id[8] = {0};
  557. if (pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id) < 0)
  558. netif_notice(tp, probe, dev, "inet addr fail\n");
  559. else {
  560. memcpy(dev->dev_addr, node_id, dev->addr_len);
  561. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  562. }
  563. }
  564. static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
  565. {
  566. struct r8152 *tp = netdev_priv(netdev);
  567. struct sockaddr *addr = p;
  568. if (!is_valid_ether_addr(addr->sa_data))
  569. return -EADDRNOTAVAIL;
  570. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  571. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  572. pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
  573. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  574. return 0;
  575. }
  576. static int alloc_all_urbs(struct r8152 *tp)
  577. {
  578. tp->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  579. if (!tp->rx_urb)
  580. return 0;
  581. tp->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  582. if (!tp->tx_urb) {
  583. usb_free_urb(tp->rx_urb);
  584. return 0;
  585. }
  586. return 1;
  587. }
  588. static void free_all_urbs(struct r8152 *tp)
  589. {
  590. usb_free_urb(tp->rx_urb);
  591. usb_free_urb(tp->tx_urb);
  592. }
  593. static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
  594. {
  595. return &dev->stats;
  596. }
  597. static void read_bulk_callback(struct urb *urb)
  598. {
  599. struct r8152 *tp;
  600. unsigned pkt_len;
  601. struct sk_buff *skb;
  602. struct net_device *netdev;
  603. struct net_device_stats *stats;
  604. int status = urb->status;
  605. int result;
  606. struct rx_desc *rx_desc;
  607. tp = urb->context;
  608. if (!tp)
  609. return;
  610. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  611. return;
  612. netdev = tp->netdev;
  613. if (!netif_device_present(netdev))
  614. return;
  615. stats = rtl8152_get_stats(netdev);
  616. switch (status) {
  617. case 0:
  618. break;
  619. case -ESHUTDOWN:
  620. set_bit(RTL8152_UNPLUG, &tp->flags);
  621. netif_device_detach(tp->netdev);
  622. case -ENOENT:
  623. return; /* the urb is in unlink state */
  624. case -ETIME:
  625. pr_warn_ratelimited("may be reset is needed?..\n");
  626. goto goon;
  627. default:
  628. pr_warn_ratelimited("Rx status %d\n", status);
  629. goto goon;
  630. }
  631. /* protect against short packets (tell me why we got some?!?) */
  632. if (urb->actual_length < sizeof(*rx_desc))
  633. goto goon;
  634. rx_desc = (struct rx_desc *)urb->transfer_buffer;
  635. pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
  636. if (urb->actual_length < sizeof(struct rx_desc) + pkt_len)
  637. goto goon;
  638. skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
  639. if (!skb)
  640. goto goon;
  641. memcpy(skb->data, tp->rx_skb->data + sizeof(struct rx_desc), pkt_len);
  642. skb_put(skb, pkt_len);
  643. skb->protocol = eth_type_trans(skb, netdev);
  644. netif_rx(skb);
  645. stats->rx_packets++;
  646. stats->rx_bytes += pkt_len;
  647. goon:
  648. usb_fill_bulk_urb(tp->rx_urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
  649. tp->rx_skb->data, RTL8152_RMS + sizeof(struct rx_desc),
  650. (usb_complete_t)read_bulk_callback, tp);
  651. result = usb_submit_urb(tp->rx_urb, GFP_ATOMIC);
  652. if (result == -ENODEV) {
  653. netif_device_detach(tp->netdev);
  654. } else if (result) {
  655. set_bit(RX_URB_FAIL, &tp->flags);
  656. goto resched;
  657. } else {
  658. clear_bit(RX_URB_FAIL, &tp->flags);
  659. }
  660. return;
  661. resched:
  662. tasklet_schedule(&tp->tl);
  663. }
  664. static void rx_fixup(unsigned long data)
  665. {
  666. struct r8152 *tp;
  667. int status;
  668. tp = (struct r8152 *)data;
  669. if (!test_bit(WORK_ENABLE, &tp->flags))
  670. return;
  671. status = usb_submit_urb(tp->rx_urb, GFP_ATOMIC);
  672. if (status == -ENODEV) {
  673. netif_device_detach(tp->netdev);
  674. } else if (status) {
  675. set_bit(RX_URB_FAIL, &tp->flags);
  676. goto tlsched;
  677. } else {
  678. clear_bit(RX_URB_FAIL, &tp->flags);
  679. }
  680. return;
  681. tlsched:
  682. tasklet_schedule(&tp->tl);
  683. }
  684. static void write_bulk_callback(struct urb *urb)
  685. {
  686. struct r8152 *tp;
  687. int status = urb->status;
  688. tp = urb->context;
  689. if (!tp)
  690. return;
  691. dev_kfree_skb_irq(tp->tx_skb);
  692. if (!netif_device_present(tp->netdev))
  693. return;
  694. if (status)
  695. dev_info(&urb->dev->dev, "%s: Tx status %d\n",
  696. tp->netdev->name, status);
  697. tp->netdev->trans_start = jiffies;
  698. netif_wake_queue(tp->netdev);
  699. }
  700. static void rtl8152_tx_timeout(struct net_device *netdev)
  701. {
  702. struct r8152 *tp = netdev_priv(netdev);
  703. struct net_device_stats *stats = rtl8152_get_stats(netdev);
  704. netif_warn(tp, tx_err, netdev, "Tx timeout.\n");
  705. usb_unlink_urb(tp->tx_urb);
  706. stats->tx_errors++;
  707. }
  708. static void rtl8152_set_rx_mode(struct net_device *netdev)
  709. {
  710. struct r8152 *tp = netdev_priv(netdev);
  711. if (tp->speed & LINK_STATUS)
  712. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  713. }
  714. static void _rtl8152_set_rx_mode(struct net_device *netdev)
  715. {
  716. struct r8152 *tp = netdev_priv(netdev);
  717. u32 mc_filter[2]; /* Multicast hash filter */
  718. __le32 tmp[2];
  719. u32 ocp_data;
  720. clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
  721. netif_stop_queue(netdev);
  722. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  723. ocp_data &= ~RCR_ACPT_ALL;
  724. ocp_data |= RCR_AB | RCR_APM;
  725. if (netdev->flags & IFF_PROMISC) {
  726. /* Unconditionally log net taps. */
  727. netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
  728. ocp_data |= RCR_AM | RCR_AAP;
  729. mc_filter[1] = mc_filter[0] = 0xffffffff;
  730. } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
  731. (netdev->flags & IFF_ALLMULTI)) {
  732. /* Too many to filter perfectly -- accept all multicasts. */
  733. ocp_data |= RCR_AM;
  734. mc_filter[1] = mc_filter[0] = 0xffffffff;
  735. } else {
  736. struct netdev_hw_addr *ha;
  737. mc_filter[1] = mc_filter[0] = 0;
  738. netdev_for_each_mc_addr(ha, netdev) {
  739. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  740. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  741. ocp_data |= RCR_AM;
  742. }
  743. }
  744. tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
  745. tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
  746. pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
  747. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  748. netif_wake_queue(netdev);
  749. }
  750. static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
  751. struct net_device *netdev)
  752. {
  753. struct r8152 *tp = netdev_priv(netdev);
  754. struct net_device_stats *stats = rtl8152_get_stats(netdev);
  755. struct tx_desc *tx_desc;
  756. unsigned int len;
  757. int res;
  758. netif_stop_queue(netdev);
  759. len = skb->len;
  760. if (skb_header_cloned(skb) || skb_headroom(skb) < sizeof(*tx_desc)) {
  761. struct sk_buff *tx_skb;
  762. tx_skb = skb_copy_expand(skb, sizeof(*tx_desc), 0, GFP_ATOMIC);
  763. dev_kfree_skb_any(skb);
  764. if (!tx_skb) {
  765. stats->tx_dropped++;
  766. netif_wake_queue(netdev);
  767. return NETDEV_TX_OK;
  768. }
  769. skb = tx_skb;
  770. }
  771. tx_desc = (struct tx_desc *)skb_push(skb, sizeof(*tx_desc));
  772. memset(tx_desc, 0, sizeof(*tx_desc));
  773. tx_desc->opts1 = cpu_to_le32((len & TX_LEN_MASK) | TX_FS | TX_LS);
  774. tp->tx_skb = skb;
  775. skb_tx_timestamp(skb);
  776. usb_fill_bulk_urb(tp->tx_urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
  777. skb->data, skb->len,
  778. (usb_complete_t)write_bulk_callback, tp);
  779. res = usb_submit_urb(tp->tx_urb, GFP_ATOMIC);
  780. if (res) {
  781. /* Can we get/handle EPIPE here? */
  782. if (res == -ENODEV) {
  783. netif_device_detach(tp->netdev);
  784. } else {
  785. netif_warn(tp, tx_err, netdev,
  786. "failed tx_urb %d\n", res);
  787. stats->tx_errors++;
  788. netif_start_queue(netdev);
  789. }
  790. } else {
  791. stats->tx_packets++;
  792. stats->tx_bytes += skb->len;
  793. }
  794. return NETDEV_TX_OK;
  795. }
  796. static void r8152b_reset_packet_filter(struct r8152 *tp)
  797. {
  798. u32 ocp_data;
  799. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
  800. ocp_data &= ~FMC_FCR_MCU_EN;
  801. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  802. ocp_data |= FMC_FCR_MCU_EN;
  803. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  804. }
  805. static void rtl8152_nic_reset(struct r8152 *tp)
  806. {
  807. int i;
  808. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
  809. for (i = 0; i < 1000; i++) {
  810. if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
  811. break;
  812. udelay(100);
  813. }
  814. }
  815. static inline u8 rtl8152_get_speed(struct r8152 *tp)
  816. {
  817. return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
  818. }
  819. static int rtl8152_enable(struct r8152 *tp)
  820. {
  821. u32 ocp_data;
  822. u8 speed;
  823. speed = rtl8152_get_speed(tp);
  824. if (speed & _100bps) {
  825. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  826. ocp_data &= ~EEEP_CR_EEEP_TX;
  827. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  828. } else {
  829. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  830. ocp_data |= EEEP_CR_EEEP_TX;
  831. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  832. }
  833. r8152b_reset_packet_filter(tp);
  834. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
  835. ocp_data |= CR_RE | CR_TE;
  836. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
  837. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  838. ocp_data &= ~RXDY_GATED_EN;
  839. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  840. usb_fill_bulk_urb(tp->rx_urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
  841. tp->rx_skb->data, RTL8152_RMS + sizeof(struct rx_desc),
  842. (usb_complete_t)read_bulk_callback, tp);
  843. return usb_submit_urb(tp->rx_urb, GFP_KERNEL);
  844. }
  845. static void rtl8152_disable(struct r8152 *tp)
  846. {
  847. u32 ocp_data;
  848. int i;
  849. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  850. ocp_data &= ~RCR_ACPT_ALL;
  851. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  852. usb_kill_urb(tp->tx_urb);
  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. for (i = 0; i < 1000; i++) {
  857. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  858. if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
  859. break;
  860. mdelay(1);
  861. }
  862. for (i = 0; i < 1000; i++) {
  863. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
  864. break;
  865. mdelay(1);
  866. }
  867. usb_kill_urb(tp->rx_urb);
  868. rtl8152_nic_reset(tp);
  869. }
  870. static void r8152b_exit_oob(struct r8152 *tp)
  871. {
  872. u32 ocp_data;
  873. int i;
  874. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  875. ocp_data &= ~RCR_ACPT_ALL;
  876. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  877. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  878. ocp_data |= RXDY_GATED_EN;
  879. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  880. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  881. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
  882. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  883. ocp_data &= ~NOW_IS_OOB;
  884. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  885. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  886. ocp_data &= ~MCU_BORW_EN;
  887. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  888. for (i = 0; i < 1000; i++) {
  889. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  890. if (ocp_data & LINK_LIST_READY)
  891. break;
  892. mdelay(1);
  893. }
  894. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  895. ocp_data |= RE_INIT_LL;
  896. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  897. for (i = 0; i < 1000; i++) {
  898. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  899. if (ocp_data & LINK_LIST_READY)
  900. break;
  901. mdelay(1);
  902. }
  903. rtl8152_nic_reset(tp);
  904. /* rx share fifo credit full threshold */
  905. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  906. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
  907. ocp_data &= STAT_SPEED_MASK;
  908. if (ocp_data == STAT_SPEED_FULL) {
  909. /* rx share fifo credit near full threshold */
  910. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  911. RXFIFO_THR2_FULL);
  912. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  913. RXFIFO_THR3_FULL);
  914. } else {
  915. /* rx share fifo credit near full threshold */
  916. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  917. RXFIFO_THR2_HIGH);
  918. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  919. RXFIFO_THR3_HIGH);
  920. }
  921. /* TX share fifo free credit full threshold */
  922. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
  923. ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
  924. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_BUF_THR);
  925. ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
  926. TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
  927. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  928. ocp_data &= ~CPCR_RX_VLAN;
  929. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  930. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  931. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  932. ocp_data |= TCR0_AUTO_FIFO;
  933. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  934. }
  935. static void r8152b_enter_oob(struct r8152 *tp)
  936. {
  937. u32 ocp_data;
  938. int i;
  939. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  940. ocp_data &= ~NOW_IS_OOB;
  941. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  942. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
  943. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
  944. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
  945. rtl8152_disable(tp);
  946. for (i = 0; i < 1000; i++) {
  947. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  948. if (ocp_data & LINK_LIST_READY)
  949. break;
  950. mdelay(1);
  951. }
  952. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  953. ocp_data |= RE_INIT_LL;
  954. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  955. for (i = 0; i < 1000; i++) {
  956. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  957. if (ocp_data & LINK_LIST_READY)
  958. break;
  959. mdelay(1);
  960. }
  961. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  962. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  963. ocp_data |= MAGIC_EN;
  964. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
  965. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  966. ocp_data |= CPCR_RX_VLAN;
  967. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  968. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  969. ocp_data |= ALDPS_PROXY_MODE;
  970. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  971. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  972. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  973. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  974. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
  975. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  976. ocp_data &= ~RXDY_GATED_EN;
  977. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  978. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  979. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  980. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  981. }
  982. static void r8152b_disable_aldps(struct r8152 *tp)
  983. {
  984. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
  985. msleep(20);
  986. }
  987. static inline void r8152b_enable_aldps(struct r8152 *tp)
  988. {
  989. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
  990. LINKENA | DIS_SDSAVE);
  991. }
  992. static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
  993. {
  994. u16 bmcr, anar;
  995. int ret = 0;
  996. cancel_delayed_work_sync(&tp->schedule);
  997. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  998. anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  999. ADVERTISE_100HALF | ADVERTISE_100FULL);
  1000. if (autoneg == AUTONEG_DISABLE) {
  1001. if (speed == SPEED_10) {
  1002. bmcr = 0;
  1003. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1004. } else if (speed == SPEED_100) {
  1005. bmcr = BMCR_SPEED100;
  1006. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  1007. } else {
  1008. ret = -EINVAL;
  1009. goto out;
  1010. }
  1011. if (duplex == DUPLEX_FULL)
  1012. bmcr |= BMCR_FULLDPLX;
  1013. } else {
  1014. if (speed == SPEED_10) {
  1015. if (duplex == DUPLEX_FULL)
  1016. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1017. else
  1018. anar |= ADVERTISE_10HALF;
  1019. } else if (speed == SPEED_100) {
  1020. if (duplex == DUPLEX_FULL) {
  1021. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1022. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  1023. } else {
  1024. anar |= ADVERTISE_10HALF;
  1025. anar |= ADVERTISE_100HALF;
  1026. }
  1027. } else {
  1028. ret = -EINVAL;
  1029. goto out;
  1030. }
  1031. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  1032. }
  1033. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  1034. r8152_mdio_write(tp, MII_BMCR, bmcr);
  1035. out:
  1036. schedule_delayed_work(&tp->schedule, 5 * HZ);
  1037. return ret;
  1038. }
  1039. static void rtl8152_down(struct r8152 *tp)
  1040. {
  1041. u32 ocp_data;
  1042. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1043. ocp_data &= ~POWER_CUT;
  1044. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1045. r8152b_disable_aldps(tp);
  1046. r8152b_enter_oob(tp);
  1047. r8152b_enable_aldps(tp);
  1048. }
  1049. static void set_carrier(struct r8152 *tp)
  1050. {
  1051. struct net_device *netdev = tp->netdev;
  1052. u8 speed;
  1053. speed = rtl8152_get_speed(tp);
  1054. if (speed & LINK_STATUS) {
  1055. if (!(tp->speed & LINK_STATUS)) {
  1056. rtl8152_enable(tp);
  1057. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1058. netif_carrier_on(netdev);
  1059. }
  1060. } else {
  1061. if (tp->speed & LINK_STATUS) {
  1062. netif_carrier_off(netdev);
  1063. rtl8152_disable(tp);
  1064. }
  1065. }
  1066. tp->speed = speed;
  1067. }
  1068. static void rtl_work_func_t(struct work_struct *work)
  1069. {
  1070. struct r8152 *tp = container_of(work, struct r8152, schedule.work);
  1071. if (!test_bit(WORK_ENABLE, &tp->flags))
  1072. goto out1;
  1073. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1074. goto out1;
  1075. set_carrier(tp);
  1076. if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
  1077. _rtl8152_set_rx_mode(tp->netdev);
  1078. schedule_delayed_work(&tp->schedule, HZ);
  1079. out1:
  1080. return;
  1081. }
  1082. static int rtl8152_open(struct net_device *netdev)
  1083. {
  1084. struct r8152 *tp = netdev_priv(netdev);
  1085. int res = 0;
  1086. tp->speed = rtl8152_get_speed(tp);
  1087. if (tp->speed & LINK_STATUS) {
  1088. res = rtl8152_enable(tp);
  1089. if (res) {
  1090. if (res == -ENODEV)
  1091. netif_device_detach(tp->netdev);
  1092. netif_err(tp, ifup, netdev,
  1093. "rtl8152_open failed: %d\n", res);
  1094. return res;
  1095. }
  1096. netif_carrier_on(netdev);
  1097. } else {
  1098. netif_stop_queue(netdev);
  1099. netif_carrier_off(netdev);
  1100. }
  1101. rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL);
  1102. netif_start_queue(netdev);
  1103. set_bit(WORK_ENABLE, &tp->flags);
  1104. schedule_delayed_work(&tp->schedule, 0);
  1105. return res;
  1106. }
  1107. static int rtl8152_close(struct net_device *netdev)
  1108. {
  1109. struct r8152 *tp = netdev_priv(netdev);
  1110. int res = 0;
  1111. clear_bit(WORK_ENABLE, &tp->flags);
  1112. cancel_delayed_work_sync(&tp->schedule);
  1113. netif_stop_queue(netdev);
  1114. rtl8152_disable(tp);
  1115. return res;
  1116. }
  1117. static void rtl_clear_bp(struct r8152 *tp)
  1118. {
  1119. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
  1120. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
  1121. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
  1122. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
  1123. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
  1124. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
  1125. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
  1126. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
  1127. mdelay(3);
  1128. ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
  1129. ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
  1130. }
  1131. static void r8152b_enable_eee(struct r8152 *tp)
  1132. {
  1133. u32 ocp_data;
  1134. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  1135. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  1136. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  1137. ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
  1138. EEE_10_CAP | EEE_NWAY_EN |
  1139. TX_QUIET_EN | RX_QUIET_EN |
  1140. SDRISETIME | RG_RXLPI_MSK_HFDUP |
  1141. SDFALLTIME);
  1142. ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
  1143. RG_LDVQUIET_EN | RG_CKRSEL |
  1144. RG_EEEPRG_EN);
  1145. ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
  1146. ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
  1147. ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
  1148. ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
  1149. ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
  1150. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  1151. }
  1152. static void r8152b_enable_fc(struct r8152 *tp)
  1153. {
  1154. u16 anar;
  1155. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  1156. anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1157. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  1158. }
  1159. static void r8152b_hw_phy_cfg(struct r8152 *tp)
  1160. {
  1161. r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
  1162. r8152b_disable_aldps(tp);
  1163. }
  1164. static void r8152b_init(struct r8152 *tp)
  1165. {
  1166. u32 ocp_data;
  1167. int i;
  1168. rtl_clear_bp(tp);
  1169. if (tp->version == RTL_VER_01) {
  1170. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  1171. ocp_data &= ~LED_MODE_MASK;
  1172. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  1173. }
  1174. r8152b_hw_phy_cfg(tp);
  1175. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1176. ocp_data &= ~POWER_CUT;
  1177. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1178. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  1179. ocp_data &= ~RWSUME_INDICATE;
  1180. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  1181. r8152b_exit_oob(tp);
  1182. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  1183. ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
  1184. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  1185. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
  1186. ocp_data &= ~MCU_CLK_RATIO_MASK;
  1187. ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
  1188. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
  1189. ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
  1190. SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
  1191. ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
  1192. r8152b_enable_eee(tp);
  1193. r8152b_enable_aldps(tp);
  1194. r8152b_enable_fc(tp);
  1195. r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
  1196. BMCR_ANRESTART);
  1197. for (i = 0; i < 100; i++) {
  1198. udelay(100);
  1199. if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
  1200. break;
  1201. }
  1202. /* disable rx aggregation */
  1203. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  1204. ocp_data |= RX_AGG_DISABLE;
  1205. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  1206. }
  1207. static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
  1208. {
  1209. struct r8152 *tp = usb_get_intfdata(intf);
  1210. netif_device_detach(tp->netdev);
  1211. if (netif_running(tp->netdev)) {
  1212. clear_bit(WORK_ENABLE, &tp->flags);
  1213. cancel_delayed_work_sync(&tp->schedule);
  1214. }
  1215. rtl8152_down(tp);
  1216. return 0;
  1217. }
  1218. static int rtl8152_resume(struct usb_interface *intf)
  1219. {
  1220. struct r8152 *tp = usb_get_intfdata(intf);
  1221. r8152b_init(tp);
  1222. netif_device_attach(tp->netdev);
  1223. if (netif_running(tp->netdev)) {
  1224. rtl8152_enable(tp);
  1225. set_bit(WORK_ENABLE, &tp->flags);
  1226. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1227. schedule_delayed_work(&tp->schedule, 0);
  1228. }
  1229. return 0;
  1230. }
  1231. static void rtl8152_get_drvinfo(struct net_device *netdev,
  1232. struct ethtool_drvinfo *info)
  1233. {
  1234. struct r8152 *tp = netdev_priv(netdev);
  1235. strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
  1236. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  1237. usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
  1238. }
  1239. static
  1240. int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  1241. {
  1242. struct r8152 *tp = netdev_priv(netdev);
  1243. if (!tp->mii.mdio_read)
  1244. return -EOPNOTSUPP;
  1245. return mii_ethtool_gset(&tp->mii, cmd);
  1246. }
  1247. static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1248. {
  1249. struct r8152 *tp = netdev_priv(dev);
  1250. return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
  1251. }
  1252. static struct ethtool_ops ops = {
  1253. .get_drvinfo = rtl8152_get_drvinfo,
  1254. .get_settings = rtl8152_get_settings,
  1255. .set_settings = rtl8152_set_settings,
  1256. .get_link = ethtool_op_get_link,
  1257. };
  1258. static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  1259. {
  1260. struct r8152 *tp = netdev_priv(netdev);
  1261. struct mii_ioctl_data *data = if_mii(rq);
  1262. int res = 0;
  1263. switch (cmd) {
  1264. case SIOCGMIIPHY:
  1265. data->phy_id = R8152_PHY_ID; /* Internal PHY */
  1266. break;
  1267. case SIOCGMIIREG:
  1268. data->val_out = r8152_mdio_read(tp, data->reg_num);
  1269. break;
  1270. case SIOCSMIIREG:
  1271. if (!capable(CAP_NET_ADMIN)) {
  1272. res = -EPERM;
  1273. break;
  1274. }
  1275. r8152_mdio_write(tp, data->reg_num, data->val_in);
  1276. break;
  1277. default:
  1278. res = -EOPNOTSUPP;
  1279. }
  1280. return res;
  1281. }
  1282. static const struct net_device_ops rtl8152_netdev_ops = {
  1283. .ndo_open = rtl8152_open,
  1284. .ndo_stop = rtl8152_close,
  1285. .ndo_do_ioctl = rtl8152_ioctl,
  1286. .ndo_start_xmit = rtl8152_start_xmit,
  1287. .ndo_tx_timeout = rtl8152_tx_timeout,
  1288. .ndo_set_rx_mode = rtl8152_set_rx_mode,
  1289. .ndo_set_mac_address = rtl8152_set_mac_address,
  1290. .ndo_change_mtu = eth_change_mtu,
  1291. .ndo_validate_addr = eth_validate_addr,
  1292. };
  1293. static void r8152b_get_version(struct r8152 *tp)
  1294. {
  1295. u32 ocp_data;
  1296. u16 version;
  1297. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
  1298. version = (u16)(ocp_data & VERSION_MASK);
  1299. switch (version) {
  1300. case 0x4c00:
  1301. tp->version = RTL_VER_01;
  1302. break;
  1303. case 0x4c10:
  1304. tp->version = RTL_VER_02;
  1305. break;
  1306. default:
  1307. netif_info(tp, probe, tp->netdev,
  1308. "Unknown version 0x%04x\n", version);
  1309. break;
  1310. }
  1311. }
  1312. static int rtl8152_probe(struct usb_interface *intf,
  1313. const struct usb_device_id *id)
  1314. {
  1315. struct usb_device *udev = interface_to_usbdev(intf);
  1316. struct r8152 *tp;
  1317. struct net_device *netdev;
  1318. if (udev->actconfig->desc.bConfigurationValue != 1) {
  1319. usb_driver_set_configuration(udev, 1);
  1320. return -ENODEV;
  1321. }
  1322. netdev = alloc_etherdev(sizeof(struct r8152));
  1323. if (!netdev) {
  1324. dev_err(&intf->dev, "Out of memory");
  1325. return -ENOMEM;
  1326. }
  1327. tp = netdev_priv(netdev);
  1328. tp->msg_enable = 0x7FFF;
  1329. tasklet_init(&tp->tl, rx_fixup, (unsigned long)tp);
  1330. INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
  1331. tp->udev = udev;
  1332. tp->netdev = netdev;
  1333. netdev->netdev_ops = &rtl8152_netdev_ops;
  1334. netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
  1335. netdev->features &= ~NETIF_F_IP_CSUM;
  1336. SET_ETHTOOL_OPS(netdev, &ops);
  1337. tp->speed = 0;
  1338. tp->mii.dev = netdev;
  1339. tp->mii.mdio_read = read_mii_word;
  1340. tp->mii.mdio_write = write_mii_word;
  1341. tp->mii.phy_id_mask = 0x3f;
  1342. tp->mii.reg_num_mask = 0x1f;
  1343. tp->mii.phy_id = R8152_PHY_ID;
  1344. tp->mii.supports_gmii = 0;
  1345. r8152b_get_version(tp);
  1346. r8152b_init(tp);
  1347. set_ethernet_addr(tp);
  1348. if (!alloc_all_urbs(tp)) {
  1349. netif_err(tp, probe, netdev, "out of memory");
  1350. goto out;
  1351. }
  1352. tp->rx_skb = netdev_alloc_skb(netdev,
  1353. RTL8152_RMS + sizeof(struct rx_desc));
  1354. if (!tp->rx_skb)
  1355. goto out1;
  1356. usb_set_intfdata(intf, tp);
  1357. SET_NETDEV_DEV(netdev, &intf->dev);
  1358. if (register_netdev(netdev) != 0) {
  1359. netif_err(tp, probe, netdev, "couldn't register the device");
  1360. goto out2;
  1361. }
  1362. netif_info(tp, probe, netdev, "%s", DRIVER_VERSION);
  1363. return 0;
  1364. out2:
  1365. usb_set_intfdata(intf, NULL);
  1366. dev_kfree_skb(tp->rx_skb);
  1367. out1:
  1368. free_all_urbs(tp);
  1369. out:
  1370. free_netdev(netdev);
  1371. return -EIO;
  1372. }
  1373. static void rtl8152_unload(struct r8152 *tp)
  1374. {
  1375. u32 ocp_data;
  1376. if (tp->version != RTL_VER_01) {
  1377. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1378. ocp_data |= POWER_CUT;
  1379. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1380. }
  1381. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  1382. ocp_data &= ~RWSUME_INDICATE;
  1383. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  1384. }
  1385. static void rtl8152_disconnect(struct usb_interface *intf)
  1386. {
  1387. struct r8152 *tp = usb_get_intfdata(intf);
  1388. usb_set_intfdata(intf, NULL);
  1389. if (tp) {
  1390. set_bit(RTL8152_UNPLUG, &tp->flags);
  1391. tasklet_kill(&tp->tl);
  1392. unregister_netdev(tp->netdev);
  1393. rtl8152_unload(tp);
  1394. free_all_urbs(tp);
  1395. if (tp->rx_skb)
  1396. dev_kfree_skb(tp->rx_skb);
  1397. free_netdev(tp->netdev);
  1398. }
  1399. }
  1400. /* table of devices that work with this driver */
  1401. static struct usb_device_id rtl8152_table[] = {
  1402. {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
  1403. {}
  1404. };
  1405. MODULE_DEVICE_TABLE(usb, rtl8152_table);
  1406. static struct usb_driver rtl8152_driver = {
  1407. .name = MODULENAME,
  1408. .probe = rtl8152_probe,
  1409. .disconnect = rtl8152_disconnect,
  1410. .id_table = rtl8152_table,
  1411. .suspend = rtl8152_suspend,
  1412. .resume = rtl8152_resume
  1413. };
  1414. module_usb_driver(rtl8152_driver);
  1415. MODULE_AUTHOR(DRIVER_AUTHOR);
  1416. MODULE_DESCRIPTION(DRIVER_DESC);
  1417. MODULE_LICENSE("GPL");