r8152.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847
  1. /*
  2. * Copyright (c) 2014 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/signal.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/mii.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/usb.h>
  17. #include <linux/crc32.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/list.h>
  21. #include <linux/ip.h>
  22. #include <linux/ipv6.h>
  23. /* Version Information */
  24. #define DRIVER_VERSION "v1.04.0 (2014/01/15)"
  25. #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
  26. #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
  27. #define MODULENAME "r8152"
  28. #define R8152_PHY_ID 32
  29. #define PLA_IDR 0xc000
  30. #define PLA_RCR 0xc010
  31. #define PLA_RMS 0xc016
  32. #define PLA_RXFIFO_CTRL0 0xc0a0
  33. #define PLA_RXFIFO_CTRL1 0xc0a4
  34. #define PLA_RXFIFO_CTRL2 0xc0a8
  35. #define PLA_FMC 0xc0b4
  36. #define PLA_CFG_WOL 0xc0b6
  37. #define PLA_TEREDO_CFG 0xc0bc
  38. #define PLA_MAR 0xcd00
  39. #define PLA_BACKUP 0xd000
  40. #define PAL_BDC_CR 0xd1a0
  41. #define PLA_TEREDO_TIMER 0xd2cc
  42. #define PLA_REALWOW_TIMER 0xd2e8
  43. #define PLA_LEDSEL 0xdd90
  44. #define PLA_LED_FEATURE 0xdd92
  45. #define PLA_PHYAR 0xde00
  46. #define PLA_BOOT_CTRL 0xe004
  47. #define PLA_GPHY_INTR_IMR 0xe022
  48. #define PLA_EEE_CR 0xe040
  49. #define PLA_EEEP_CR 0xe080
  50. #define PLA_MAC_PWR_CTRL 0xe0c0
  51. #define PLA_MAC_PWR_CTRL2 0xe0ca
  52. #define PLA_MAC_PWR_CTRL3 0xe0cc
  53. #define PLA_MAC_PWR_CTRL4 0xe0ce
  54. #define PLA_WDT6_CTRL 0xe428
  55. #define PLA_TCR0 0xe610
  56. #define PLA_TCR1 0xe612
  57. #define PLA_TXFIFO_CTRL 0xe618
  58. #define PLA_RSTTELLY 0xe800
  59. #define PLA_CR 0xe813
  60. #define PLA_CRWECR 0xe81c
  61. #define PLA_CONFIG5 0xe822
  62. #define PLA_PHY_PWR 0xe84c
  63. #define PLA_OOB_CTRL 0xe84f
  64. #define PLA_CPCR 0xe854
  65. #define PLA_MISC_0 0xe858
  66. #define PLA_MISC_1 0xe85a
  67. #define PLA_OCP_GPHY_BASE 0xe86c
  68. #define PLA_TELLYCNT 0xe890
  69. #define PLA_SFF_STS_7 0xe8de
  70. #define PLA_PHYSTATUS 0xe908
  71. #define PLA_BP_BA 0xfc26
  72. #define PLA_BP_0 0xfc28
  73. #define PLA_BP_1 0xfc2a
  74. #define PLA_BP_2 0xfc2c
  75. #define PLA_BP_3 0xfc2e
  76. #define PLA_BP_4 0xfc30
  77. #define PLA_BP_5 0xfc32
  78. #define PLA_BP_6 0xfc34
  79. #define PLA_BP_7 0xfc36
  80. #define PLA_BP_EN 0xfc38
  81. #define USB_U2P3_CTRL 0xb460
  82. #define USB_DEV_STAT 0xb808
  83. #define USB_USB_CTRL 0xd406
  84. #define USB_PHY_CTRL 0xd408
  85. #define USB_TX_AGG 0xd40a
  86. #define USB_RX_BUF_TH 0xd40c
  87. #define USB_USB_TIMER 0xd428
  88. #define USB_RX_EARLY_AGG 0xd42c
  89. #define USB_PM_CTRL_STATUS 0xd432
  90. #define USB_TX_DMA 0xd434
  91. #define USB_TOLERANCE 0xd490
  92. #define USB_LPM_CTRL 0xd41a
  93. #define USB_UPS_CTRL 0xd800
  94. #define USB_MISC_0 0xd81a
  95. #define USB_POWER_CUT 0xd80a
  96. #define USB_AFE_CTRL2 0xd824
  97. #define USB_WDT11_CTRL 0xe43c
  98. #define USB_BP_BA 0xfc26
  99. #define USB_BP_0 0xfc28
  100. #define USB_BP_1 0xfc2a
  101. #define USB_BP_2 0xfc2c
  102. #define USB_BP_3 0xfc2e
  103. #define USB_BP_4 0xfc30
  104. #define USB_BP_5 0xfc32
  105. #define USB_BP_6 0xfc34
  106. #define USB_BP_7 0xfc36
  107. #define USB_BP_EN 0xfc38
  108. /* OCP Registers */
  109. #define OCP_ALDPS_CONFIG 0x2010
  110. #define OCP_EEE_CONFIG1 0x2080
  111. #define OCP_EEE_CONFIG2 0x2092
  112. #define OCP_EEE_CONFIG3 0x2094
  113. #define OCP_BASE_MII 0xa400
  114. #define OCP_EEE_AR 0xa41a
  115. #define OCP_EEE_DATA 0xa41c
  116. #define OCP_PHY_STATUS 0xa420
  117. #define OCP_POWER_CFG 0xa430
  118. #define OCP_EEE_CFG 0xa432
  119. #define OCP_SRAM_ADDR 0xa436
  120. #define OCP_SRAM_DATA 0xa438
  121. #define OCP_DOWN_SPEED 0xa442
  122. #define OCP_EEE_CFG2 0xa5d0
  123. #define OCP_ADC_CFG 0xbc06
  124. /* SRAM Register */
  125. #define SRAM_LPF_CFG 0x8012
  126. #define SRAM_10M_AMP1 0x8080
  127. #define SRAM_10M_AMP2 0x8082
  128. #define SRAM_IMPEDANCE 0x8084
  129. /* PLA_RCR */
  130. #define RCR_AAP 0x00000001
  131. #define RCR_APM 0x00000002
  132. #define RCR_AM 0x00000004
  133. #define RCR_AB 0x00000008
  134. #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
  135. /* PLA_RXFIFO_CTRL0 */
  136. #define RXFIFO_THR1_NORMAL 0x00080002
  137. #define RXFIFO_THR1_OOB 0x01800003
  138. /* PLA_RXFIFO_CTRL1 */
  139. #define RXFIFO_THR2_FULL 0x00000060
  140. #define RXFIFO_THR2_HIGH 0x00000038
  141. #define RXFIFO_THR2_OOB 0x0000004a
  142. #define RXFIFO_THR2_NORMAL 0x00a0
  143. /* PLA_RXFIFO_CTRL2 */
  144. #define RXFIFO_THR3_FULL 0x00000078
  145. #define RXFIFO_THR3_HIGH 0x00000048
  146. #define RXFIFO_THR3_OOB 0x0000005a
  147. #define RXFIFO_THR3_NORMAL 0x0110
  148. /* PLA_TXFIFO_CTRL */
  149. #define TXFIFO_THR_NORMAL 0x00400008
  150. #define TXFIFO_THR_NORMAL2 0x01000008
  151. /* PLA_FMC */
  152. #define FMC_FCR_MCU_EN 0x0001
  153. /* PLA_EEEP_CR */
  154. #define EEEP_CR_EEEP_TX 0x0002
  155. /* PLA_WDT6_CTRL */
  156. #define WDT6_SET_MODE 0x0010
  157. /* PLA_TCR0 */
  158. #define TCR0_TX_EMPTY 0x0800
  159. #define TCR0_AUTO_FIFO 0x0080
  160. /* PLA_TCR1 */
  161. #define VERSION_MASK 0x7cf0
  162. /* PLA_CR */
  163. #define CR_RST 0x10
  164. #define CR_RE 0x08
  165. #define CR_TE 0x04
  166. /* PLA_CRWECR */
  167. #define CRWECR_NORAML 0x00
  168. #define CRWECR_CONFIG 0xc0
  169. /* PLA_OOB_CTRL */
  170. #define NOW_IS_OOB 0x80
  171. #define TXFIFO_EMPTY 0x20
  172. #define RXFIFO_EMPTY 0x10
  173. #define LINK_LIST_READY 0x02
  174. #define DIS_MCU_CLROOB 0x01
  175. #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
  176. /* PLA_MISC_1 */
  177. #define RXDY_GATED_EN 0x0008
  178. /* PLA_SFF_STS_7 */
  179. #define RE_INIT_LL 0x8000
  180. #define MCU_BORW_EN 0x4000
  181. /* PLA_CPCR */
  182. #define CPCR_RX_VLAN 0x0040
  183. /* PLA_CFG_WOL */
  184. #define MAGIC_EN 0x0001
  185. /* PLA_TEREDO_CFG */
  186. #define TEREDO_SEL 0x8000
  187. #define TEREDO_WAKE_MASK 0x7f00
  188. #define TEREDO_RS_EVENT_MASK 0x00fe
  189. #define OOB_TEREDO_EN 0x0001
  190. /* PAL_BDC_CR */
  191. #define ALDPS_PROXY_MODE 0x0001
  192. /* PLA_CONFIG5 */
  193. #define LAN_WAKE_EN 0x0002
  194. /* PLA_LED_FEATURE */
  195. #define LED_MODE_MASK 0x0700
  196. /* PLA_PHY_PWR */
  197. #define TX_10M_IDLE_EN 0x0080
  198. #define PFM_PWM_SWITCH 0x0040
  199. /* PLA_MAC_PWR_CTRL */
  200. #define D3_CLK_GATED_EN 0x00004000
  201. #define MCU_CLK_RATIO 0x07010f07
  202. #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
  203. #define ALDPS_SPDWN_RATIO 0x0f87
  204. /* PLA_MAC_PWR_CTRL2 */
  205. #define EEE_SPDWN_RATIO 0x8007
  206. /* PLA_MAC_PWR_CTRL3 */
  207. #define PKT_AVAIL_SPDWN_EN 0x0100
  208. #define SUSPEND_SPDWN_EN 0x0004
  209. #define U1U2_SPDWN_EN 0x0002
  210. #define L1_SPDWN_EN 0x0001
  211. /* PLA_MAC_PWR_CTRL4 */
  212. #define PWRSAVE_SPDWN_EN 0x1000
  213. #define RXDV_SPDWN_EN 0x0800
  214. #define TX10MIDLE_EN 0x0100
  215. #define TP100_SPDWN_EN 0x0020
  216. #define TP500_SPDWN_EN 0x0010
  217. #define TP1000_SPDWN_EN 0x0008
  218. #define EEE_SPDWN_EN 0x0001
  219. /* PLA_GPHY_INTR_IMR */
  220. #define GPHY_STS_MSK 0x0001
  221. #define SPEED_DOWN_MSK 0x0002
  222. #define SPDWN_RXDV_MSK 0x0004
  223. #define SPDWN_LINKCHG_MSK 0x0008
  224. /* PLA_PHYAR */
  225. #define PHYAR_FLAG 0x80000000
  226. /* PLA_EEE_CR */
  227. #define EEE_RX_EN 0x0001
  228. #define EEE_TX_EN 0x0002
  229. /* PLA_BOOT_CTRL */
  230. #define AUTOLOAD_DONE 0x0002
  231. /* USB_DEV_STAT */
  232. #define STAT_SPEED_MASK 0x0006
  233. #define STAT_SPEED_HIGH 0x0000
  234. #define STAT_SPEED_FULL 0x0001
  235. /* USB_TX_AGG */
  236. #define TX_AGG_MAX_THRESHOLD 0x03
  237. /* USB_RX_BUF_TH */
  238. #define RX_THR_SUPPER 0x0c350180
  239. #define RX_THR_HIGH 0x7a120180
  240. #define RX_THR_SLOW 0xffff0180
  241. /* USB_TX_DMA */
  242. #define TEST_MODE_DISABLE 0x00000001
  243. #define TX_SIZE_ADJUST1 0x00000100
  244. /* USB_UPS_CTRL */
  245. #define POWER_CUT 0x0100
  246. /* USB_PM_CTRL_STATUS */
  247. #define RESUME_INDICATE 0x0001
  248. /* USB_USB_CTRL */
  249. #define RX_AGG_DISABLE 0x0010
  250. /* USB_U2P3_CTRL */
  251. #define U2P3_ENABLE 0x0001
  252. /* USB_POWER_CUT */
  253. #define PWR_EN 0x0001
  254. #define PHASE2_EN 0x0008
  255. /* USB_MISC_0 */
  256. #define PCUT_STATUS 0x0001
  257. /* USB_RX_EARLY_AGG */
  258. #define EARLY_AGG_SUPPER 0x0e832981
  259. #define EARLY_AGG_HIGH 0x0e837a12
  260. #define EARLY_AGG_SLOW 0x0e83ffff
  261. /* USB_WDT11_CTRL */
  262. #define TIMER11_EN 0x0001
  263. /* USB_LPM_CTRL */
  264. #define LPM_TIMER_MASK 0x0c
  265. #define LPM_TIMER_500MS 0x04 /* 500 ms */
  266. #define LPM_TIMER_500US 0x0c /* 500 us */
  267. /* USB_AFE_CTRL2 */
  268. #define SEN_VAL_MASK 0xf800
  269. #define SEN_VAL_NORMAL 0xa000
  270. #define SEL_RXIDLE 0x0100
  271. /* OCP_ALDPS_CONFIG */
  272. #define ENPWRSAVE 0x8000
  273. #define ENPDNPS 0x0200
  274. #define LINKENA 0x0100
  275. #define DIS_SDSAVE 0x0010
  276. /* OCP_PHY_STATUS */
  277. #define PHY_STAT_MASK 0x0007
  278. #define PHY_STAT_LAN_ON 3
  279. #define PHY_STAT_PWRDN 5
  280. /* OCP_POWER_CFG */
  281. #define EEE_CLKDIV_EN 0x8000
  282. #define EN_ALDPS 0x0004
  283. #define EN_10M_PLLOFF 0x0001
  284. /* OCP_EEE_CONFIG1 */
  285. #define RG_TXLPI_MSK_HFDUP 0x8000
  286. #define RG_MATCLR_EN 0x4000
  287. #define EEE_10_CAP 0x2000
  288. #define EEE_NWAY_EN 0x1000
  289. #define TX_QUIET_EN 0x0200
  290. #define RX_QUIET_EN 0x0100
  291. #define SDRISETIME 0x0010 /* bit 4 ~ 6 */
  292. #define RG_RXLPI_MSK_HFDUP 0x0008
  293. #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
  294. /* OCP_EEE_CONFIG2 */
  295. #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
  296. #define RG_DACQUIET_EN 0x0400
  297. #define RG_LDVQUIET_EN 0x0200
  298. #define RG_CKRSEL 0x0020
  299. #define RG_EEEPRG_EN 0x0010
  300. /* OCP_EEE_CONFIG3 */
  301. #define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
  302. #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
  303. #define MSK_PH 0x0006 /* bit 0 ~ 3 */
  304. /* OCP_EEE_AR */
  305. /* bit[15:14] function */
  306. #define FUN_ADDR 0x0000
  307. #define FUN_DATA 0x4000
  308. /* bit[4:0] device addr */
  309. #define DEVICE_ADDR 0x0007
  310. /* OCP_EEE_DATA */
  311. #define EEE_ADDR 0x003C
  312. #define EEE_DATA 0x0002
  313. /* OCP_EEE_CFG */
  314. #define CTAP_SHORT_EN 0x0040
  315. #define EEE10_EN 0x0010
  316. /* OCP_DOWN_SPEED */
  317. #define EN_10M_BGOFF 0x0080
  318. /* OCP_EEE_CFG2 */
  319. #define MY1000_EEE 0x0004
  320. #define MY100_EEE 0x0002
  321. /* OCP_ADC_CFG */
  322. #define CKADSEL_L 0x0100
  323. #define ADC_EN 0x0080
  324. #define EN_EMI_L 0x0040
  325. /* SRAM_LPF_CFG */
  326. #define LPF_AUTO_TUNE 0x8000
  327. /* SRAM_10M_AMP1 */
  328. #define GDAC_IB_UPALL 0x0008
  329. /* SRAM_10M_AMP2 */
  330. #define AMP_DN 0x0200
  331. /* SRAM_IMPEDANCE */
  332. #define RX_DRIVING_MASK 0x6000
  333. enum rtl_register_content {
  334. _1000bps = 0x10,
  335. _100bps = 0x08,
  336. _10bps = 0x04,
  337. LINK_STATUS = 0x02,
  338. FULL_DUP = 0x01,
  339. };
  340. #define RTL8152_MAX_TX 10
  341. #define RTL8152_MAX_RX 10
  342. #define INTBUFSIZE 2
  343. #define CRC_SIZE 4
  344. #define TX_ALIGN 4
  345. #define RX_ALIGN 8
  346. #define INTR_LINK 0x0004
  347. #define RTL8152_REQT_READ 0xc0
  348. #define RTL8152_REQT_WRITE 0x40
  349. #define RTL8152_REQ_GET_REGS 0x05
  350. #define RTL8152_REQ_SET_REGS 0x05
  351. #define BYTE_EN_DWORD 0xff
  352. #define BYTE_EN_WORD 0x33
  353. #define BYTE_EN_BYTE 0x11
  354. #define BYTE_EN_SIX_BYTES 0x3f
  355. #define BYTE_EN_START_MASK 0x0f
  356. #define BYTE_EN_END_MASK 0xf0
  357. #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
  358. #define RTL8152_TX_TIMEOUT (HZ)
  359. /* rtl8152 flags */
  360. enum rtl8152_flags {
  361. RTL8152_UNPLUG = 0,
  362. RTL8152_SET_RX_MODE,
  363. WORK_ENABLE,
  364. RTL8152_LINK_CHG,
  365. };
  366. /* Define these values to match your device */
  367. #define VENDOR_ID_REALTEK 0x0bda
  368. #define PRODUCT_ID_RTL8152 0x8152
  369. #define PRODUCT_ID_RTL8153 0x8153
  370. #define VENDOR_ID_SAMSUNG 0x04e8
  371. #define PRODUCT_ID_SAMSUNG 0xa101
  372. #define MCU_TYPE_PLA 0x0100
  373. #define MCU_TYPE_USB 0x0000
  374. struct rx_desc {
  375. __le32 opts1;
  376. #define RX_LEN_MASK 0x7fff
  377. __le32 opts2;
  378. __le32 opts3;
  379. __le32 opts4;
  380. __le32 opts5;
  381. __le32 opts6;
  382. };
  383. struct tx_desc {
  384. __le32 opts1;
  385. #define TX_FS (1 << 31) /* First segment of a packet */
  386. #define TX_LS (1 << 30) /* Final segment of a packet */
  387. #define TX_LEN_MASK 0x3ffff
  388. __le32 opts2;
  389. #define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
  390. #define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
  391. #define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
  392. #define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
  393. };
  394. struct r8152;
  395. struct rx_agg {
  396. struct list_head list;
  397. struct urb *urb;
  398. struct r8152 *context;
  399. void *buffer;
  400. void *head;
  401. };
  402. struct tx_agg {
  403. struct list_head list;
  404. struct urb *urb;
  405. struct r8152 *context;
  406. void *buffer;
  407. void *head;
  408. u32 skb_num;
  409. u32 skb_len;
  410. };
  411. struct r8152 {
  412. unsigned long flags;
  413. struct usb_device *udev;
  414. struct tasklet_struct tl;
  415. struct usb_interface *intf;
  416. struct net_device *netdev;
  417. struct urb *intr_urb;
  418. struct tx_agg tx_info[RTL8152_MAX_TX];
  419. struct rx_agg rx_info[RTL8152_MAX_RX];
  420. struct list_head rx_done, tx_free;
  421. struct sk_buff_head tx_queue;
  422. spinlock_t rx_lock, tx_lock;
  423. struct delayed_work schedule;
  424. struct mii_if_info mii;
  425. struct rtl_ops {
  426. void (*init)(struct r8152 *);
  427. int (*enable)(struct r8152 *);
  428. void (*disable)(struct r8152 *);
  429. void (*down)(struct r8152 *);
  430. void (*unload)(struct r8152 *);
  431. } rtl_ops;
  432. int intr_interval;
  433. u32 msg_enable;
  434. u32 tx_qlen;
  435. u16 ocp_base;
  436. u8 *intr_buff;
  437. u8 version;
  438. u8 speed;
  439. };
  440. enum rtl_version {
  441. RTL_VER_UNKNOWN = 0,
  442. RTL_VER_01,
  443. RTL_VER_02,
  444. RTL_VER_03,
  445. RTL_VER_04,
  446. RTL_VER_05,
  447. RTL_VER_MAX
  448. };
  449. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  450. * The RTL chips use a 64 element hash table based on the Ethernet CRC.
  451. */
  452. static const int multicast_filter_limit = 32;
  453. static unsigned int rx_buf_sz = 16384;
  454. static
  455. int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  456. {
  457. int ret;
  458. void *tmp;
  459. tmp = kmalloc(size, GFP_KERNEL);
  460. if (!tmp)
  461. return -ENOMEM;
  462. ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
  463. RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
  464. value, index, tmp, size, 500);
  465. memcpy(data, tmp, size);
  466. kfree(tmp);
  467. return ret;
  468. }
  469. static
  470. int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  471. {
  472. int ret;
  473. void *tmp;
  474. tmp = kmalloc(size, GFP_KERNEL);
  475. if (!tmp)
  476. return -ENOMEM;
  477. memcpy(tmp, data, size);
  478. ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
  479. RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
  480. value, index, tmp, size, 500);
  481. kfree(tmp);
  482. return ret;
  483. }
  484. static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
  485. void *data, u16 type)
  486. {
  487. u16 limit = 64;
  488. int ret = 0;
  489. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  490. return -ENODEV;
  491. /* both size and indix must be 4 bytes align */
  492. if ((size & 3) || !size || (index & 3) || !data)
  493. return -EPERM;
  494. if ((u32)index + (u32)size > 0xffff)
  495. return -EPERM;
  496. while (size) {
  497. if (size > limit) {
  498. ret = get_registers(tp, index, type, limit, data);
  499. if (ret < 0)
  500. break;
  501. index += limit;
  502. data += limit;
  503. size -= limit;
  504. } else {
  505. ret = get_registers(tp, index, type, size, data);
  506. if (ret < 0)
  507. break;
  508. index += size;
  509. data += size;
  510. size = 0;
  511. break;
  512. }
  513. }
  514. return ret;
  515. }
  516. static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
  517. u16 size, void *data, u16 type)
  518. {
  519. int ret;
  520. u16 byteen_start, byteen_end, byen;
  521. u16 limit = 512;
  522. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  523. return -ENODEV;
  524. /* both size and indix must be 4 bytes align */
  525. if ((size & 3) || !size || (index & 3) || !data)
  526. return -EPERM;
  527. if ((u32)index + (u32)size > 0xffff)
  528. return -EPERM;
  529. byteen_start = byteen & BYTE_EN_START_MASK;
  530. byteen_end = byteen & BYTE_EN_END_MASK;
  531. byen = byteen_start | (byteen_start << 4);
  532. ret = set_registers(tp, index, type | byen, 4, data);
  533. if (ret < 0)
  534. goto error1;
  535. index += 4;
  536. data += 4;
  537. size -= 4;
  538. if (size) {
  539. size -= 4;
  540. while (size) {
  541. if (size > limit) {
  542. ret = set_registers(tp, index,
  543. type | BYTE_EN_DWORD,
  544. limit, data);
  545. if (ret < 0)
  546. goto error1;
  547. index += limit;
  548. data += limit;
  549. size -= limit;
  550. } else {
  551. ret = set_registers(tp, index,
  552. type | BYTE_EN_DWORD,
  553. size, data);
  554. if (ret < 0)
  555. goto error1;
  556. index += size;
  557. data += size;
  558. size = 0;
  559. break;
  560. }
  561. }
  562. byen = byteen_end | (byteen_end >> 4);
  563. ret = set_registers(tp, index, type | byen, 4, data);
  564. if (ret < 0)
  565. goto error1;
  566. }
  567. error1:
  568. return ret;
  569. }
  570. static inline
  571. int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  572. {
  573. return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
  574. }
  575. static inline
  576. int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  577. {
  578. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
  579. }
  580. static inline
  581. int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  582. {
  583. return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
  584. }
  585. static inline
  586. int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  587. {
  588. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
  589. }
  590. static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
  591. {
  592. __le32 data;
  593. generic_ocp_read(tp, index, sizeof(data), &data, type);
  594. return __le32_to_cpu(data);
  595. }
  596. static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
  597. {
  598. __le32 tmp = __cpu_to_le32(data);
  599. generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
  600. }
  601. static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
  602. {
  603. u32 data;
  604. __le32 tmp;
  605. u8 shift = index & 2;
  606. index &= ~3;
  607. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  608. data = __le32_to_cpu(tmp);
  609. data >>= (shift * 8);
  610. data &= 0xffff;
  611. return (u16)data;
  612. }
  613. static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
  614. {
  615. u32 mask = 0xffff;
  616. __le32 tmp;
  617. u16 byen = BYTE_EN_WORD;
  618. u8 shift = index & 2;
  619. data &= mask;
  620. if (index & 2) {
  621. byen <<= shift;
  622. mask <<= (shift * 8);
  623. data <<= (shift * 8);
  624. index &= ~3;
  625. }
  626. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  627. data |= __le32_to_cpu(tmp) & ~mask;
  628. tmp = __cpu_to_le32(data);
  629. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  630. }
  631. static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
  632. {
  633. u32 data;
  634. __le32 tmp;
  635. u8 shift = index & 3;
  636. index &= ~3;
  637. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  638. data = __le32_to_cpu(tmp);
  639. data >>= (shift * 8);
  640. data &= 0xff;
  641. return (u8)data;
  642. }
  643. static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
  644. {
  645. u32 mask = 0xff;
  646. __le32 tmp;
  647. u16 byen = BYTE_EN_BYTE;
  648. u8 shift = index & 3;
  649. data &= mask;
  650. if (index & 3) {
  651. byen <<= shift;
  652. mask <<= (shift * 8);
  653. data <<= (shift * 8);
  654. index &= ~3;
  655. }
  656. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  657. data |= __le32_to_cpu(tmp) & ~mask;
  658. tmp = __cpu_to_le32(data);
  659. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  660. }
  661. static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
  662. {
  663. u16 ocp_base, ocp_index;
  664. ocp_base = addr & 0xf000;
  665. if (ocp_base != tp->ocp_base) {
  666. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  667. tp->ocp_base = ocp_base;
  668. }
  669. ocp_index = (addr & 0x0fff) | 0xb000;
  670. return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
  671. }
  672. static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
  673. {
  674. u16 ocp_base, ocp_index;
  675. ocp_base = addr & 0xf000;
  676. if (ocp_base != tp->ocp_base) {
  677. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  678. tp->ocp_base = ocp_base;
  679. }
  680. ocp_index = (addr & 0x0fff) | 0xb000;
  681. ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
  682. }
  683. static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
  684. {
  685. ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
  686. }
  687. static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
  688. {
  689. return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
  690. }
  691. static void sram_write(struct r8152 *tp, u16 addr, u16 data)
  692. {
  693. ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
  694. ocp_reg_write(tp, OCP_SRAM_DATA, data);
  695. }
  696. static u16 sram_read(struct r8152 *tp, u16 addr)
  697. {
  698. ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
  699. return ocp_reg_read(tp, OCP_SRAM_DATA);
  700. }
  701. static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
  702. {
  703. struct r8152 *tp = netdev_priv(netdev);
  704. if (phy_id != R8152_PHY_ID)
  705. return -EINVAL;
  706. return r8152_mdio_read(tp, reg);
  707. }
  708. static
  709. void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
  710. {
  711. struct r8152 *tp = netdev_priv(netdev);
  712. if (phy_id != R8152_PHY_ID)
  713. return;
  714. r8152_mdio_write(tp, reg, val);
  715. }
  716. static
  717. int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
  718. static inline void set_ethernet_addr(struct r8152 *tp)
  719. {
  720. struct net_device *dev = tp->netdev;
  721. u8 node_id[8] = {0};
  722. if (pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id) < 0)
  723. netif_notice(tp, probe, dev, "inet addr fail\n");
  724. else {
  725. memcpy(dev->dev_addr, node_id, dev->addr_len);
  726. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  727. }
  728. }
  729. static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
  730. {
  731. struct r8152 *tp = netdev_priv(netdev);
  732. struct sockaddr *addr = p;
  733. if (!is_valid_ether_addr(addr->sa_data))
  734. return -EADDRNOTAVAIL;
  735. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  736. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  737. pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
  738. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  739. return 0;
  740. }
  741. static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
  742. {
  743. return &dev->stats;
  744. }
  745. static void read_bulk_callback(struct urb *urb)
  746. {
  747. struct net_device *netdev;
  748. unsigned long flags;
  749. int status = urb->status;
  750. struct rx_agg *agg;
  751. struct r8152 *tp;
  752. int result;
  753. agg = urb->context;
  754. if (!agg)
  755. return;
  756. tp = agg->context;
  757. if (!tp)
  758. return;
  759. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  760. return;
  761. if (!test_bit(WORK_ENABLE, &tp->flags))
  762. return;
  763. netdev = tp->netdev;
  764. /* When link down, the driver would cancel all bulks. */
  765. /* This avoid the re-submitting bulk */
  766. if (!netif_carrier_ok(netdev))
  767. return;
  768. switch (status) {
  769. case 0:
  770. if (urb->actual_length < ETH_ZLEN)
  771. break;
  772. spin_lock_irqsave(&tp->rx_lock, flags);
  773. list_add_tail(&agg->list, &tp->rx_done);
  774. spin_unlock_irqrestore(&tp->rx_lock, flags);
  775. tasklet_schedule(&tp->tl);
  776. return;
  777. case -ESHUTDOWN:
  778. set_bit(RTL8152_UNPLUG, &tp->flags);
  779. netif_device_detach(tp->netdev);
  780. return;
  781. case -ENOENT:
  782. return; /* the urb is in unlink state */
  783. case -ETIME:
  784. if (net_ratelimit())
  785. netdev_warn(netdev, "maybe reset is needed?\n");
  786. break;
  787. default:
  788. if (net_ratelimit())
  789. netdev_warn(netdev, "Rx status %d\n", status);
  790. break;
  791. }
  792. result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
  793. if (result == -ENODEV) {
  794. netif_device_detach(tp->netdev);
  795. } else if (result) {
  796. spin_lock_irqsave(&tp->rx_lock, flags);
  797. list_add_tail(&agg->list, &tp->rx_done);
  798. spin_unlock_irqrestore(&tp->rx_lock, flags);
  799. tasklet_schedule(&tp->tl);
  800. }
  801. }
  802. static void write_bulk_callback(struct urb *urb)
  803. {
  804. struct net_device_stats *stats;
  805. unsigned long flags;
  806. struct tx_agg *agg;
  807. struct r8152 *tp;
  808. int status = urb->status;
  809. agg = urb->context;
  810. if (!agg)
  811. return;
  812. tp = agg->context;
  813. if (!tp)
  814. return;
  815. stats = rtl8152_get_stats(tp->netdev);
  816. if (status) {
  817. if (net_ratelimit())
  818. netdev_warn(tp->netdev, "Tx status %d\n", status);
  819. stats->tx_errors += agg->skb_num;
  820. } else {
  821. stats->tx_packets += agg->skb_num;
  822. stats->tx_bytes += agg->skb_len;
  823. }
  824. spin_lock_irqsave(&tp->tx_lock, flags);
  825. list_add_tail(&agg->list, &tp->tx_free);
  826. spin_unlock_irqrestore(&tp->tx_lock, flags);
  827. if (!netif_carrier_ok(tp->netdev))
  828. return;
  829. if (!test_bit(WORK_ENABLE, &tp->flags))
  830. return;
  831. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  832. return;
  833. if (!skb_queue_empty(&tp->tx_queue))
  834. tasklet_schedule(&tp->tl);
  835. }
  836. static void intr_callback(struct urb *urb)
  837. {
  838. struct r8152 *tp;
  839. __le16 *d;
  840. int status = urb->status;
  841. int res;
  842. tp = urb->context;
  843. if (!tp)
  844. return;
  845. if (!test_bit(WORK_ENABLE, &tp->flags))
  846. return;
  847. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  848. return;
  849. switch (status) {
  850. case 0: /* success */
  851. break;
  852. case -ECONNRESET: /* unlink */
  853. case -ESHUTDOWN:
  854. netif_device_detach(tp->netdev);
  855. case -ENOENT:
  856. return;
  857. case -EOVERFLOW:
  858. netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
  859. goto resubmit;
  860. /* -EPIPE: should clear the halt */
  861. default:
  862. netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
  863. goto resubmit;
  864. }
  865. d = urb->transfer_buffer;
  866. if (INTR_LINK & __le16_to_cpu(d[0])) {
  867. if (!(tp->speed & LINK_STATUS)) {
  868. set_bit(RTL8152_LINK_CHG, &tp->flags);
  869. schedule_delayed_work(&tp->schedule, 0);
  870. }
  871. } else {
  872. if (tp->speed & LINK_STATUS) {
  873. set_bit(RTL8152_LINK_CHG, &tp->flags);
  874. schedule_delayed_work(&tp->schedule, 0);
  875. }
  876. }
  877. resubmit:
  878. res = usb_submit_urb(urb, GFP_ATOMIC);
  879. if (res == -ENODEV)
  880. netif_device_detach(tp->netdev);
  881. else if (res)
  882. netif_err(tp, intr, tp->netdev,
  883. "can't resubmit intr, status %d\n", res);
  884. }
  885. static inline void *rx_agg_align(void *data)
  886. {
  887. return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
  888. }
  889. static inline void *tx_agg_align(void *data)
  890. {
  891. return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
  892. }
  893. static void free_all_mem(struct r8152 *tp)
  894. {
  895. int i;
  896. for (i = 0; i < RTL8152_MAX_RX; i++) {
  897. usb_free_urb(tp->rx_info[i].urb);
  898. tp->rx_info[i].urb = NULL;
  899. kfree(tp->rx_info[i].buffer);
  900. tp->rx_info[i].buffer = NULL;
  901. tp->rx_info[i].head = NULL;
  902. }
  903. for (i = 0; i < RTL8152_MAX_TX; i++) {
  904. usb_free_urb(tp->tx_info[i].urb);
  905. tp->tx_info[i].urb = NULL;
  906. kfree(tp->tx_info[i].buffer);
  907. tp->tx_info[i].buffer = NULL;
  908. tp->tx_info[i].head = NULL;
  909. }
  910. usb_free_urb(tp->intr_urb);
  911. tp->intr_urb = NULL;
  912. kfree(tp->intr_buff);
  913. tp->intr_buff = NULL;
  914. }
  915. static int alloc_all_mem(struct r8152 *tp)
  916. {
  917. struct net_device *netdev = tp->netdev;
  918. struct usb_interface *intf = tp->intf;
  919. struct usb_host_interface *alt = intf->cur_altsetting;
  920. struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
  921. struct urb *urb;
  922. int node, i;
  923. u8 *buf;
  924. node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
  925. spin_lock_init(&tp->rx_lock);
  926. spin_lock_init(&tp->tx_lock);
  927. INIT_LIST_HEAD(&tp->rx_done);
  928. INIT_LIST_HEAD(&tp->tx_free);
  929. skb_queue_head_init(&tp->tx_queue);
  930. for (i = 0; i < RTL8152_MAX_RX; i++) {
  931. buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  932. if (!buf)
  933. goto err1;
  934. if (buf != rx_agg_align(buf)) {
  935. kfree(buf);
  936. buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
  937. node);
  938. if (!buf)
  939. goto err1;
  940. }
  941. urb = usb_alloc_urb(0, GFP_KERNEL);
  942. if (!urb) {
  943. kfree(buf);
  944. goto err1;
  945. }
  946. INIT_LIST_HEAD(&tp->rx_info[i].list);
  947. tp->rx_info[i].context = tp;
  948. tp->rx_info[i].urb = urb;
  949. tp->rx_info[i].buffer = buf;
  950. tp->rx_info[i].head = rx_agg_align(buf);
  951. }
  952. for (i = 0; i < RTL8152_MAX_TX; i++) {
  953. buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  954. if (!buf)
  955. goto err1;
  956. if (buf != tx_agg_align(buf)) {
  957. kfree(buf);
  958. buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
  959. node);
  960. if (!buf)
  961. goto err1;
  962. }
  963. urb = usb_alloc_urb(0, GFP_KERNEL);
  964. if (!urb) {
  965. kfree(buf);
  966. goto err1;
  967. }
  968. INIT_LIST_HEAD(&tp->tx_info[i].list);
  969. tp->tx_info[i].context = tp;
  970. tp->tx_info[i].urb = urb;
  971. tp->tx_info[i].buffer = buf;
  972. tp->tx_info[i].head = tx_agg_align(buf);
  973. list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
  974. }
  975. tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  976. if (!tp->intr_urb)
  977. goto err1;
  978. tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
  979. if (!tp->intr_buff)
  980. goto err1;
  981. tp->intr_interval = (int)ep_intr->desc.bInterval;
  982. usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
  983. tp->intr_buff, INTBUFSIZE, intr_callback,
  984. tp, tp->intr_interval);
  985. return 0;
  986. err1:
  987. free_all_mem(tp);
  988. return -ENOMEM;
  989. }
  990. static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
  991. {
  992. struct tx_agg *agg = NULL;
  993. unsigned long flags;
  994. spin_lock_irqsave(&tp->tx_lock, flags);
  995. if (!list_empty(&tp->tx_free)) {
  996. struct list_head *cursor;
  997. cursor = tp->tx_free.next;
  998. list_del_init(cursor);
  999. agg = list_entry(cursor, struct tx_agg, list);
  1000. }
  1001. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1002. return agg;
  1003. }
  1004. static void
  1005. r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
  1006. {
  1007. memset(desc, 0, sizeof(*desc));
  1008. desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
  1009. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1010. __be16 protocol;
  1011. u8 ip_protocol;
  1012. u32 opts2 = 0;
  1013. if (skb->protocol == htons(ETH_P_8021Q))
  1014. protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
  1015. else
  1016. protocol = skb->protocol;
  1017. switch (protocol) {
  1018. case htons(ETH_P_IP):
  1019. opts2 |= IPV4_CS;
  1020. ip_protocol = ip_hdr(skb)->protocol;
  1021. break;
  1022. case htons(ETH_P_IPV6):
  1023. opts2 |= IPV6_CS;
  1024. ip_protocol = ipv6_hdr(skb)->nexthdr;
  1025. break;
  1026. default:
  1027. ip_protocol = IPPROTO_RAW;
  1028. break;
  1029. }
  1030. if (ip_protocol == IPPROTO_TCP) {
  1031. opts2 |= TCP_CS;
  1032. opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
  1033. } else if (ip_protocol == IPPROTO_UDP) {
  1034. opts2 |= UDP_CS;
  1035. } else {
  1036. WARN_ON_ONCE(1);
  1037. }
  1038. desc->opts2 = cpu_to_le32(opts2);
  1039. }
  1040. }
  1041. static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
  1042. {
  1043. int remain;
  1044. u8 *tx_data;
  1045. tx_data = agg->head;
  1046. agg->skb_num = agg->skb_len = 0;
  1047. remain = rx_buf_sz;
  1048. while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
  1049. struct tx_desc *tx_desc;
  1050. struct sk_buff *skb;
  1051. unsigned int len;
  1052. skb = skb_dequeue(&tp->tx_queue);
  1053. if (!skb)
  1054. break;
  1055. remain -= sizeof(*tx_desc);
  1056. len = skb->len;
  1057. if (remain < len) {
  1058. skb_queue_head(&tp->tx_queue, skb);
  1059. break;
  1060. }
  1061. tx_data = tx_agg_align(tx_data);
  1062. tx_desc = (struct tx_desc *)tx_data;
  1063. tx_data += sizeof(*tx_desc);
  1064. r8152_tx_csum(tp, tx_desc, skb);
  1065. memcpy(tx_data, skb->data, len);
  1066. agg->skb_num++;
  1067. agg->skb_len += len;
  1068. dev_kfree_skb_any(skb);
  1069. tx_data += len;
  1070. remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
  1071. }
  1072. netif_tx_lock(tp->netdev);
  1073. if (netif_queue_stopped(tp->netdev) &&
  1074. skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
  1075. netif_wake_queue(tp->netdev);
  1076. netif_tx_unlock(tp->netdev);
  1077. usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
  1078. agg->head, (int)(tx_data - (u8 *)agg->head),
  1079. (usb_complete_t)write_bulk_callback, agg);
  1080. return usb_submit_urb(agg->urb, GFP_ATOMIC);
  1081. }
  1082. static void rx_bottom(struct r8152 *tp)
  1083. {
  1084. unsigned long flags;
  1085. struct list_head *cursor, *next;
  1086. spin_lock_irqsave(&tp->rx_lock, flags);
  1087. list_for_each_safe(cursor, next, &tp->rx_done) {
  1088. struct rx_desc *rx_desc;
  1089. struct rx_agg *agg;
  1090. int len_used = 0;
  1091. struct urb *urb;
  1092. u8 *rx_data;
  1093. int ret;
  1094. list_del_init(cursor);
  1095. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1096. agg = list_entry(cursor, struct rx_agg, list);
  1097. urb = agg->urb;
  1098. if (urb->actual_length < ETH_ZLEN)
  1099. goto submit;
  1100. rx_desc = agg->head;
  1101. rx_data = agg->head;
  1102. len_used += sizeof(struct rx_desc);
  1103. while (urb->actual_length > len_used) {
  1104. struct net_device *netdev = tp->netdev;
  1105. struct net_device_stats *stats;
  1106. unsigned int pkt_len;
  1107. struct sk_buff *skb;
  1108. pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
  1109. if (pkt_len < ETH_ZLEN)
  1110. break;
  1111. len_used += pkt_len;
  1112. if (urb->actual_length < len_used)
  1113. break;
  1114. stats = rtl8152_get_stats(netdev);
  1115. pkt_len -= CRC_SIZE;
  1116. rx_data += sizeof(struct rx_desc);
  1117. skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
  1118. if (!skb) {
  1119. stats->rx_dropped++;
  1120. break;
  1121. }
  1122. memcpy(skb->data, rx_data, pkt_len);
  1123. skb_put(skb, pkt_len);
  1124. skb->protocol = eth_type_trans(skb, netdev);
  1125. netif_rx(skb);
  1126. stats->rx_packets++;
  1127. stats->rx_bytes += pkt_len;
  1128. rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
  1129. rx_desc = (struct rx_desc *)rx_data;
  1130. len_used = (int)(rx_data - (u8 *)agg->head);
  1131. len_used += sizeof(struct rx_desc);
  1132. }
  1133. submit:
  1134. ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
  1135. spin_lock_irqsave(&tp->rx_lock, flags);
  1136. if (ret && ret != -ENODEV) {
  1137. list_add_tail(&agg->list, next);
  1138. tasklet_schedule(&tp->tl);
  1139. }
  1140. }
  1141. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1142. }
  1143. static void tx_bottom(struct r8152 *tp)
  1144. {
  1145. int res;
  1146. do {
  1147. struct tx_agg *agg;
  1148. if (skb_queue_empty(&tp->tx_queue))
  1149. break;
  1150. agg = r8152_get_tx_agg(tp);
  1151. if (!agg)
  1152. break;
  1153. res = r8152_tx_agg_fill(tp, agg);
  1154. if (res) {
  1155. struct net_device_stats *stats;
  1156. struct net_device *netdev;
  1157. unsigned long flags;
  1158. netdev = tp->netdev;
  1159. stats = rtl8152_get_stats(netdev);
  1160. if (res == -ENODEV) {
  1161. netif_device_detach(netdev);
  1162. } else {
  1163. netif_warn(tp, tx_err, netdev,
  1164. "failed tx_urb %d\n", res);
  1165. stats->tx_dropped += agg->skb_num;
  1166. spin_lock_irqsave(&tp->tx_lock, flags);
  1167. list_add_tail(&agg->list, &tp->tx_free);
  1168. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1169. }
  1170. }
  1171. } while (res == 0);
  1172. }
  1173. static void bottom_half(unsigned long data)
  1174. {
  1175. struct r8152 *tp;
  1176. tp = (struct r8152 *)data;
  1177. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1178. return;
  1179. if (!test_bit(WORK_ENABLE, &tp->flags))
  1180. return;
  1181. /* When link down, the driver would cancel all bulks. */
  1182. /* This avoid the re-submitting bulk */
  1183. if (!netif_carrier_ok(tp->netdev))
  1184. return;
  1185. rx_bottom(tp);
  1186. tx_bottom(tp);
  1187. }
  1188. static
  1189. int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
  1190. {
  1191. usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
  1192. agg->head, rx_buf_sz,
  1193. (usb_complete_t)read_bulk_callback, agg);
  1194. return usb_submit_urb(agg->urb, mem_flags);
  1195. }
  1196. static void rtl8152_tx_timeout(struct net_device *netdev)
  1197. {
  1198. struct r8152 *tp = netdev_priv(netdev);
  1199. int i;
  1200. netif_warn(tp, tx_err, netdev, "Tx timeout\n");
  1201. for (i = 0; i < RTL8152_MAX_TX; i++)
  1202. usb_unlink_urb(tp->tx_info[i].urb);
  1203. }
  1204. static void rtl8152_set_rx_mode(struct net_device *netdev)
  1205. {
  1206. struct r8152 *tp = netdev_priv(netdev);
  1207. if (tp->speed & LINK_STATUS) {
  1208. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1209. schedule_delayed_work(&tp->schedule, 0);
  1210. }
  1211. }
  1212. static void _rtl8152_set_rx_mode(struct net_device *netdev)
  1213. {
  1214. struct r8152 *tp = netdev_priv(netdev);
  1215. u32 mc_filter[2]; /* Multicast hash filter */
  1216. __le32 tmp[2];
  1217. u32 ocp_data;
  1218. clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1219. netif_stop_queue(netdev);
  1220. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1221. ocp_data &= ~RCR_ACPT_ALL;
  1222. ocp_data |= RCR_AB | RCR_APM;
  1223. if (netdev->flags & IFF_PROMISC) {
  1224. /* Unconditionally log net taps. */
  1225. netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
  1226. ocp_data |= RCR_AM | RCR_AAP;
  1227. mc_filter[1] = mc_filter[0] = 0xffffffff;
  1228. } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
  1229. (netdev->flags & IFF_ALLMULTI)) {
  1230. /* Too many to filter perfectly -- accept all multicasts. */
  1231. ocp_data |= RCR_AM;
  1232. mc_filter[1] = mc_filter[0] = 0xffffffff;
  1233. } else {
  1234. struct netdev_hw_addr *ha;
  1235. mc_filter[1] = mc_filter[0] = 0;
  1236. netdev_for_each_mc_addr(ha, netdev) {
  1237. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  1238. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1239. ocp_data |= RCR_AM;
  1240. }
  1241. }
  1242. tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
  1243. tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
  1244. pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
  1245. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1246. netif_wake_queue(netdev);
  1247. }
  1248. static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
  1249. struct net_device *netdev)
  1250. {
  1251. struct r8152 *tp = netdev_priv(netdev);
  1252. skb_tx_timestamp(skb);
  1253. skb_queue_tail(&tp->tx_queue, skb);
  1254. if (list_empty(&tp->tx_free) &&
  1255. skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
  1256. netif_stop_queue(netdev);
  1257. if (!list_empty(&tp->tx_free))
  1258. tasklet_schedule(&tp->tl);
  1259. return NETDEV_TX_OK;
  1260. }
  1261. static void r8152b_reset_packet_filter(struct r8152 *tp)
  1262. {
  1263. u32 ocp_data;
  1264. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
  1265. ocp_data &= ~FMC_FCR_MCU_EN;
  1266. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  1267. ocp_data |= FMC_FCR_MCU_EN;
  1268. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  1269. }
  1270. static void rtl8152_nic_reset(struct r8152 *tp)
  1271. {
  1272. int i;
  1273. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
  1274. for (i = 0; i < 1000; i++) {
  1275. if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
  1276. break;
  1277. udelay(100);
  1278. }
  1279. }
  1280. static void set_tx_qlen(struct r8152 *tp)
  1281. {
  1282. struct net_device *netdev = tp->netdev;
  1283. tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
  1284. sizeof(struct tx_desc));
  1285. }
  1286. static inline u8 rtl8152_get_speed(struct r8152 *tp)
  1287. {
  1288. return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
  1289. }
  1290. static void rtl_set_eee_plus(struct r8152 *tp)
  1291. {
  1292. u32 ocp_data;
  1293. u8 speed;
  1294. speed = rtl8152_get_speed(tp);
  1295. if (speed & _10bps) {
  1296. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  1297. ocp_data |= EEEP_CR_EEEP_TX;
  1298. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  1299. } else {
  1300. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  1301. ocp_data &= ~EEEP_CR_EEEP_TX;
  1302. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  1303. }
  1304. }
  1305. static int rtl_enable(struct r8152 *tp)
  1306. {
  1307. u32 ocp_data;
  1308. int i, ret;
  1309. r8152b_reset_packet_filter(tp);
  1310. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
  1311. ocp_data |= CR_RE | CR_TE;
  1312. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
  1313. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  1314. ocp_data &= ~RXDY_GATED_EN;
  1315. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  1316. INIT_LIST_HEAD(&tp->rx_done);
  1317. ret = 0;
  1318. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1319. INIT_LIST_HEAD(&tp->rx_info[i].list);
  1320. ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
  1321. }
  1322. return ret;
  1323. }
  1324. static int rtl8152_enable(struct r8152 *tp)
  1325. {
  1326. set_tx_qlen(tp);
  1327. rtl_set_eee_plus(tp);
  1328. return rtl_enable(tp);
  1329. }
  1330. static void r8153_set_rx_agg(struct r8152 *tp)
  1331. {
  1332. u8 speed;
  1333. speed = rtl8152_get_speed(tp);
  1334. if (speed & _1000bps) {
  1335. if (tp->udev->speed == USB_SPEED_SUPER) {
  1336. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
  1337. RX_THR_SUPPER);
  1338. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
  1339. EARLY_AGG_SUPPER);
  1340. } else {
  1341. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
  1342. RX_THR_HIGH);
  1343. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
  1344. EARLY_AGG_HIGH);
  1345. }
  1346. } else {
  1347. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
  1348. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
  1349. EARLY_AGG_SLOW);
  1350. }
  1351. }
  1352. static int rtl8153_enable(struct r8152 *tp)
  1353. {
  1354. set_tx_qlen(tp);
  1355. rtl_set_eee_plus(tp);
  1356. r8153_set_rx_agg(tp);
  1357. return rtl_enable(tp);
  1358. }
  1359. static void rtl8152_disable(struct r8152 *tp)
  1360. {
  1361. struct net_device_stats *stats = rtl8152_get_stats(tp->netdev);
  1362. struct sk_buff *skb;
  1363. u32 ocp_data;
  1364. int i;
  1365. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1366. ocp_data &= ~RCR_ACPT_ALL;
  1367. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1368. while ((skb = skb_dequeue(&tp->tx_queue))) {
  1369. dev_kfree_skb(skb);
  1370. stats->tx_dropped++;
  1371. }
  1372. for (i = 0; i < RTL8152_MAX_TX; i++)
  1373. usb_kill_urb(tp->tx_info[i].urb);
  1374. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  1375. ocp_data |= RXDY_GATED_EN;
  1376. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  1377. for (i = 0; i < 1000; i++) {
  1378. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1379. if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
  1380. break;
  1381. mdelay(1);
  1382. }
  1383. for (i = 0; i < 1000; i++) {
  1384. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
  1385. break;
  1386. mdelay(1);
  1387. }
  1388. for (i = 0; i < RTL8152_MAX_RX; i++)
  1389. usb_kill_urb(tp->rx_info[i].urb);
  1390. rtl8152_nic_reset(tp);
  1391. }
  1392. static void r8152b_exit_oob(struct r8152 *tp)
  1393. {
  1394. u32 ocp_data;
  1395. int i;
  1396. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1397. ocp_data &= ~RCR_ACPT_ALL;
  1398. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1399. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  1400. ocp_data |= RXDY_GATED_EN;
  1401. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  1402. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  1403. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
  1404. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1405. ocp_data &= ~NOW_IS_OOB;
  1406. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  1407. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  1408. ocp_data &= ~MCU_BORW_EN;
  1409. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  1410. for (i = 0; i < 1000; i++) {
  1411. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1412. if (ocp_data & LINK_LIST_READY)
  1413. break;
  1414. mdelay(1);
  1415. }
  1416. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  1417. ocp_data |= RE_INIT_LL;
  1418. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  1419. for (i = 0; i < 1000; i++) {
  1420. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1421. if (ocp_data & LINK_LIST_READY)
  1422. break;
  1423. mdelay(1);
  1424. }
  1425. rtl8152_nic_reset(tp);
  1426. /* rx share fifo credit full threshold */
  1427. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  1428. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
  1429. ocp_data &= STAT_SPEED_MASK;
  1430. if (ocp_data == STAT_SPEED_FULL) {
  1431. /* rx share fifo credit near full threshold */
  1432. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  1433. RXFIFO_THR2_FULL);
  1434. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  1435. RXFIFO_THR3_FULL);
  1436. } else {
  1437. /* rx share fifo credit near full threshold */
  1438. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  1439. RXFIFO_THR2_HIGH);
  1440. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  1441. RXFIFO_THR3_HIGH);
  1442. }
  1443. /* TX share fifo free credit full threshold */
  1444. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
  1445. ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
  1446. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
  1447. ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
  1448. TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
  1449. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  1450. ocp_data &= ~CPCR_RX_VLAN;
  1451. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  1452. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  1453. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  1454. ocp_data |= TCR0_AUTO_FIFO;
  1455. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  1456. }
  1457. static void r8152b_enter_oob(struct r8152 *tp)
  1458. {
  1459. u32 ocp_data;
  1460. int i;
  1461. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1462. ocp_data &= ~NOW_IS_OOB;
  1463. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  1464. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
  1465. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
  1466. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
  1467. rtl8152_disable(tp);
  1468. for (i = 0; i < 1000; i++) {
  1469. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1470. if (ocp_data & LINK_LIST_READY)
  1471. break;
  1472. mdelay(1);
  1473. }
  1474. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  1475. ocp_data |= RE_INIT_LL;
  1476. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  1477. for (i = 0; i < 1000; i++) {
  1478. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1479. if (ocp_data & LINK_LIST_READY)
  1480. break;
  1481. mdelay(1);
  1482. }
  1483. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  1484. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  1485. ocp_data |= MAGIC_EN;
  1486. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
  1487. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  1488. ocp_data |= CPCR_RX_VLAN;
  1489. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  1490. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  1491. ocp_data |= ALDPS_PROXY_MODE;
  1492. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  1493. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1494. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  1495. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  1496. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
  1497. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  1498. ocp_data &= ~RXDY_GATED_EN;
  1499. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  1500. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1501. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  1502. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1503. }
  1504. static void r8152b_disable_aldps(struct r8152 *tp)
  1505. {
  1506. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
  1507. msleep(20);
  1508. }
  1509. static inline void r8152b_enable_aldps(struct r8152 *tp)
  1510. {
  1511. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
  1512. LINKENA | DIS_SDSAVE);
  1513. }
  1514. static void r8153_hw_phy_cfg(struct r8152 *tp)
  1515. {
  1516. u32 ocp_data;
  1517. u16 data;
  1518. ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
  1519. r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
  1520. if (tp->version == RTL_VER_03) {
  1521. data = ocp_reg_read(tp, OCP_EEE_CFG);
  1522. data &= ~CTAP_SHORT_EN;
  1523. ocp_reg_write(tp, OCP_EEE_CFG, data);
  1524. }
  1525. data = ocp_reg_read(tp, OCP_POWER_CFG);
  1526. data |= EEE_CLKDIV_EN;
  1527. ocp_reg_write(tp, OCP_POWER_CFG, data);
  1528. data = ocp_reg_read(tp, OCP_DOWN_SPEED);
  1529. data |= EN_10M_BGOFF;
  1530. ocp_reg_write(tp, OCP_DOWN_SPEED, data);
  1531. data = ocp_reg_read(tp, OCP_POWER_CFG);
  1532. data |= EN_10M_PLLOFF;
  1533. ocp_reg_write(tp, OCP_POWER_CFG, data);
  1534. data = sram_read(tp, SRAM_IMPEDANCE);
  1535. data &= ~RX_DRIVING_MASK;
  1536. sram_write(tp, SRAM_IMPEDANCE, data);
  1537. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  1538. ocp_data |= PFM_PWM_SWITCH;
  1539. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  1540. data = sram_read(tp, SRAM_LPF_CFG);
  1541. data |= LPF_AUTO_TUNE;
  1542. sram_write(tp, SRAM_LPF_CFG, data);
  1543. data = sram_read(tp, SRAM_10M_AMP1);
  1544. data |= GDAC_IB_UPALL;
  1545. sram_write(tp, SRAM_10M_AMP1, data);
  1546. data = sram_read(tp, SRAM_10M_AMP2);
  1547. data |= AMP_DN;
  1548. sram_write(tp, SRAM_10M_AMP2, data);
  1549. }
  1550. static void r8153_u1u2en(struct r8152 *tp, int enable)
  1551. {
  1552. u8 u1u2[8];
  1553. if (enable)
  1554. memset(u1u2, 0xff, sizeof(u1u2));
  1555. else
  1556. memset(u1u2, 0x00, sizeof(u1u2));
  1557. usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
  1558. }
  1559. static void r8153_u2p3en(struct r8152 *tp, int enable)
  1560. {
  1561. u32 ocp_data;
  1562. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
  1563. if (enable)
  1564. ocp_data |= U2P3_ENABLE;
  1565. else
  1566. ocp_data &= ~U2P3_ENABLE;
  1567. ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
  1568. }
  1569. static void r8153_power_cut_en(struct r8152 *tp, int enable)
  1570. {
  1571. u32 ocp_data;
  1572. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
  1573. if (enable)
  1574. ocp_data |= PWR_EN | PHASE2_EN;
  1575. else
  1576. ocp_data &= ~(PWR_EN | PHASE2_EN);
  1577. ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  1578. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  1579. ocp_data &= ~PCUT_STATUS;
  1580. ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
  1581. }
  1582. static void r8153_teredo_off(struct r8152 *tp)
  1583. {
  1584. u32 ocp_data;
  1585. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  1586. ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
  1587. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  1588. ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
  1589. ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
  1590. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
  1591. }
  1592. static void r8153_first_init(struct r8152 *tp)
  1593. {
  1594. u32 ocp_data;
  1595. int i;
  1596. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  1597. ocp_data |= RXDY_GATED_EN;
  1598. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  1599. r8153_teredo_off(tp);
  1600. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1601. ocp_data &= ~RCR_ACPT_ALL;
  1602. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1603. r8153_hw_phy_cfg(tp);
  1604. rtl8152_nic_reset(tp);
  1605. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1606. ocp_data &= ~NOW_IS_OOB;
  1607. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  1608. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  1609. ocp_data &= ~MCU_BORW_EN;
  1610. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  1611. for (i = 0; i < 1000; i++) {
  1612. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1613. if (ocp_data & LINK_LIST_READY)
  1614. break;
  1615. mdelay(1);
  1616. }
  1617. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  1618. ocp_data |= RE_INIT_LL;
  1619. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  1620. for (i = 0; i < 1000; i++) {
  1621. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1622. if (ocp_data & LINK_LIST_READY)
  1623. break;
  1624. mdelay(1);
  1625. }
  1626. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  1627. ocp_data &= ~CPCR_RX_VLAN;
  1628. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  1629. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  1630. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  1631. ocp_data |= TCR0_AUTO_FIFO;
  1632. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  1633. rtl8152_nic_reset(tp);
  1634. /* rx share fifo credit full threshold */
  1635. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  1636. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
  1637. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
  1638. /* TX share fifo free credit full threshold */
  1639. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
  1640. /* rx aggregation */
  1641. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  1642. ocp_data &= ~RX_AGG_DISABLE;
  1643. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  1644. }
  1645. static void r8153_enter_oob(struct r8152 *tp)
  1646. {
  1647. u32 ocp_data;
  1648. int i;
  1649. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1650. ocp_data &= ~NOW_IS_OOB;
  1651. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  1652. rtl8152_disable(tp);
  1653. for (i = 0; i < 1000; i++) {
  1654. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1655. if (ocp_data & LINK_LIST_READY)
  1656. break;
  1657. mdelay(1);
  1658. }
  1659. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  1660. ocp_data |= RE_INIT_LL;
  1661. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  1662. for (i = 0; i < 1000; i++) {
  1663. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1664. if (ocp_data & LINK_LIST_READY)
  1665. break;
  1666. mdelay(1);
  1667. }
  1668. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  1669. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  1670. ocp_data |= MAGIC_EN;
  1671. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
  1672. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  1673. ocp_data &= ~TEREDO_WAKE_MASK;
  1674. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  1675. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  1676. ocp_data |= CPCR_RX_VLAN;
  1677. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  1678. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  1679. ocp_data |= ALDPS_PROXY_MODE;
  1680. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  1681. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1682. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  1683. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  1684. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
  1685. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  1686. ocp_data &= ~RXDY_GATED_EN;
  1687. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  1688. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1689. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  1690. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1691. }
  1692. static void r8153_disable_aldps(struct r8152 *tp)
  1693. {
  1694. u16 data;
  1695. data = ocp_reg_read(tp, OCP_POWER_CFG);
  1696. data &= ~EN_ALDPS;
  1697. ocp_reg_write(tp, OCP_POWER_CFG, data);
  1698. msleep(20);
  1699. }
  1700. static void r8153_enable_aldps(struct r8152 *tp)
  1701. {
  1702. u16 data;
  1703. data = ocp_reg_read(tp, OCP_POWER_CFG);
  1704. data |= EN_ALDPS;
  1705. ocp_reg_write(tp, OCP_POWER_CFG, data);
  1706. }
  1707. static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
  1708. {
  1709. u16 bmcr, anar, gbcr;
  1710. int ret = 0;
  1711. cancel_delayed_work_sync(&tp->schedule);
  1712. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  1713. anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  1714. ADVERTISE_100HALF | ADVERTISE_100FULL);
  1715. if (tp->mii.supports_gmii) {
  1716. gbcr = r8152_mdio_read(tp, MII_CTRL1000);
  1717. gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  1718. } else {
  1719. gbcr = 0;
  1720. }
  1721. if (autoneg == AUTONEG_DISABLE) {
  1722. if (speed == SPEED_10) {
  1723. bmcr = 0;
  1724. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1725. } else if (speed == SPEED_100) {
  1726. bmcr = BMCR_SPEED100;
  1727. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  1728. } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
  1729. bmcr = BMCR_SPEED1000;
  1730. gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  1731. } else {
  1732. ret = -EINVAL;
  1733. goto out;
  1734. }
  1735. if (duplex == DUPLEX_FULL)
  1736. bmcr |= BMCR_FULLDPLX;
  1737. } else {
  1738. if (speed == SPEED_10) {
  1739. if (duplex == DUPLEX_FULL)
  1740. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1741. else
  1742. anar |= ADVERTISE_10HALF;
  1743. } else if (speed == SPEED_100) {
  1744. if (duplex == DUPLEX_FULL) {
  1745. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1746. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  1747. } else {
  1748. anar |= ADVERTISE_10HALF;
  1749. anar |= ADVERTISE_100HALF;
  1750. }
  1751. } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
  1752. if (duplex == DUPLEX_FULL) {
  1753. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  1754. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  1755. gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  1756. } else {
  1757. anar |= ADVERTISE_10HALF;
  1758. anar |= ADVERTISE_100HALF;
  1759. gbcr |= ADVERTISE_1000HALF;
  1760. }
  1761. } else {
  1762. ret = -EINVAL;
  1763. goto out;
  1764. }
  1765. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  1766. }
  1767. if (tp->mii.supports_gmii)
  1768. r8152_mdio_write(tp, MII_CTRL1000, gbcr);
  1769. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  1770. r8152_mdio_write(tp, MII_BMCR, bmcr);
  1771. out:
  1772. return ret;
  1773. }
  1774. static void rtl8152_down(struct r8152 *tp)
  1775. {
  1776. u32 ocp_data;
  1777. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1778. ocp_data &= ~POWER_CUT;
  1779. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1780. r8152b_disable_aldps(tp);
  1781. r8152b_enter_oob(tp);
  1782. r8152b_enable_aldps(tp);
  1783. }
  1784. static void rtl8153_down(struct r8152 *tp)
  1785. {
  1786. r8153_u1u2en(tp, 0);
  1787. r8153_power_cut_en(tp, 0);
  1788. r8153_disable_aldps(tp);
  1789. r8153_enter_oob(tp);
  1790. r8153_enable_aldps(tp);
  1791. }
  1792. static void set_carrier(struct r8152 *tp)
  1793. {
  1794. struct net_device *netdev = tp->netdev;
  1795. u8 speed;
  1796. clear_bit(RTL8152_LINK_CHG, &tp->flags);
  1797. speed = rtl8152_get_speed(tp);
  1798. if (speed & LINK_STATUS) {
  1799. if (!(tp->speed & LINK_STATUS)) {
  1800. tp->rtl_ops.enable(tp);
  1801. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1802. netif_carrier_on(netdev);
  1803. }
  1804. } else {
  1805. if (tp->speed & LINK_STATUS) {
  1806. netif_carrier_off(netdev);
  1807. tasklet_disable(&tp->tl);
  1808. tp->rtl_ops.disable(tp);
  1809. tasklet_enable(&tp->tl);
  1810. }
  1811. }
  1812. tp->speed = speed;
  1813. }
  1814. static void rtl_work_func_t(struct work_struct *work)
  1815. {
  1816. struct r8152 *tp = container_of(work, struct r8152, schedule.work);
  1817. if (!test_bit(WORK_ENABLE, &tp->flags))
  1818. goto out1;
  1819. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1820. goto out1;
  1821. if (test_bit(RTL8152_LINK_CHG, &tp->flags))
  1822. set_carrier(tp);
  1823. if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
  1824. _rtl8152_set_rx_mode(tp->netdev);
  1825. out1:
  1826. return;
  1827. }
  1828. static int rtl8152_open(struct net_device *netdev)
  1829. {
  1830. struct r8152 *tp = netdev_priv(netdev);
  1831. int res = 0;
  1832. rtl8152_set_speed(tp, AUTONEG_ENABLE,
  1833. tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
  1834. DUPLEX_FULL);
  1835. tp->speed = 0;
  1836. netif_carrier_off(netdev);
  1837. netif_start_queue(netdev);
  1838. set_bit(WORK_ENABLE, &tp->flags);
  1839. res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  1840. if (res) {
  1841. if (res == -ENODEV)
  1842. netif_device_detach(tp->netdev);
  1843. netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
  1844. res);
  1845. }
  1846. return res;
  1847. }
  1848. static int rtl8152_close(struct net_device *netdev)
  1849. {
  1850. struct r8152 *tp = netdev_priv(netdev);
  1851. int res = 0;
  1852. clear_bit(WORK_ENABLE, &tp->flags);
  1853. usb_kill_urb(tp->intr_urb);
  1854. cancel_delayed_work_sync(&tp->schedule);
  1855. netif_stop_queue(netdev);
  1856. tasklet_disable(&tp->tl);
  1857. tp->rtl_ops.disable(tp);
  1858. tasklet_enable(&tp->tl);
  1859. return res;
  1860. }
  1861. static void rtl_clear_bp(struct r8152 *tp)
  1862. {
  1863. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
  1864. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
  1865. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
  1866. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
  1867. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
  1868. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
  1869. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
  1870. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
  1871. mdelay(3);
  1872. ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
  1873. ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
  1874. }
  1875. static void r8153_clear_bp(struct r8152 *tp)
  1876. {
  1877. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
  1878. ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
  1879. rtl_clear_bp(tp);
  1880. }
  1881. static void r8152b_enable_eee(struct r8152 *tp)
  1882. {
  1883. u32 ocp_data;
  1884. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  1885. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  1886. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  1887. ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
  1888. EEE_10_CAP | EEE_NWAY_EN |
  1889. TX_QUIET_EN | RX_QUIET_EN |
  1890. SDRISETIME | RG_RXLPI_MSK_HFDUP |
  1891. SDFALLTIME);
  1892. ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
  1893. RG_LDVQUIET_EN | RG_CKRSEL |
  1894. RG_EEEPRG_EN);
  1895. ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
  1896. ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
  1897. ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
  1898. ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
  1899. ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
  1900. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  1901. }
  1902. static void r8153_enable_eee(struct r8152 *tp)
  1903. {
  1904. u32 ocp_data;
  1905. u16 data;
  1906. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  1907. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  1908. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  1909. data = ocp_reg_read(tp, OCP_EEE_CFG);
  1910. data |= EEE10_EN;
  1911. ocp_reg_write(tp, OCP_EEE_CFG, data);
  1912. data = ocp_reg_read(tp, OCP_EEE_CFG2);
  1913. data |= MY1000_EEE | MY100_EEE;
  1914. ocp_reg_write(tp, OCP_EEE_CFG2, data);
  1915. }
  1916. static void r8152b_enable_fc(struct r8152 *tp)
  1917. {
  1918. u16 anar;
  1919. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  1920. anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1921. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  1922. }
  1923. static void r8152b_hw_phy_cfg(struct r8152 *tp)
  1924. {
  1925. r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
  1926. r8152b_disable_aldps(tp);
  1927. }
  1928. static void r8152b_init(struct r8152 *tp)
  1929. {
  1930. u32 ocp_data;
  1931. int i;
  1932. rtl_clear_bp(tp);
  1933. if (tp->version == RTL_VER_01) {
  1934. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  1935. ocp_data &= ~LED_MODE_MASK;
  1936. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  1937. }
  1938. r8152b_hw_phy_cfg(tp);
  1939. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1940. ocp_data &= ~POWER_CUT;
  1941. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1942. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  1943. ocp_data &= ~RESUME_INDICATE;
  1944. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  1945. r8152b_exit_oob(tp);
  1946. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  1947. ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
  1948. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  1949. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
  1950. ocp_data &= ~MCU_CLK_RATIO_MASK;
  1951. ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
  1952. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
  1953. ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
  1954. SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
  1955. ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
  1956. r8152b_enable_eee(tp);
  1957. r8152b_enable_aldps(tp);
  1958. r8152b_enable_fc(tp);
  1959. r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
  1960. BMCR_ANRESTART);
  1961. for (i = 0; i < 100; i++) {
  1962. udelay(100);
  1963. if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
  1964. break;
  1965. }
  1966. /* enable rx aggregation */
  1967. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  1968. ocp_data &= ~RX_AGG_DISABLE;
  1969. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  1970. }
  1971. static void r8153_init(struct r8152 *tp)
  1972. {
  1973. u32 ocp_data;
  1974. int i;
  1975. r8153_u1u2en(tp, 0);
  1976. for (i = 0; i < 500; i++) {
  1977. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  1978. AUTOLOAD_DONE)
  1979. break;
  1980. msleep(20);
  1981. }
  1982. for (i = 0; i < 500; i++) {
  1983. ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
  1984. if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
  1985. break;
  1986. msleep(20);
  1987. }
  1988. r8153_u2p3en(tp, 0);
  1989. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
  1990. ocp_data &= ~TIMER11_EN;
  1991. ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
  1992. r8153_clear_bp(tp);
  1993. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  1994. ocp_data &= ~LED_MODE_MASK;
  1995. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  1996. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
  1997. ocp_data &= ~LPM_TIMER_MASK;
  1998. if (tp->udev->speed == USB_SPEED_SUPER)
  1999. ocp_data |= LPM_TIMER_500US;
  2000. else
  2001. ocp_data |= LPM_TIMER_500MS;
  2002. ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
  2003. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
  2004. ocp_data &= ~SEN_VAL_MASK;
  2005. ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
  2006. ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
  2007. r8153_power_cut_en(tp, 0);
  2008. r8153_u1u2en(tp, 1);
  2009. r8153_first_init(tp);
  2010. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
  2011. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
  2012. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
  2013. PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
  2014. U1U2_SPDWN_EN | L1_SPDWN_EN);
  2015. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
  2016. PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
  2017. TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
  2018. EEE_SPDWN_EN);
  2019. r8153_enable_eee(tp);
  2020. r8153_enable_aldps(tp);
  2021. r8152b_enable_fc(tp);
  2022. r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
  2023. BMCR_ANRESTART);
  2024. }
  2025. static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
  2026. {
  2027. struct r8152 *tp = usb_get_intfdata(intf);
  2028. netif_device_detach(tp->netdev);
  2029. if (netif_running(tp->netdev)) {
  2030. clear_bit(WORK_ENABLE, &tp->flags);
  2031. usb_kill_urb(tp->intr_urb);
  2032. cancel_delayed_work_sync(&tp->schedule);
  2033. tasklet_disable(&tp->tl);
  2034. }
  2035. tp->rtl_ops.down(tp);
  2036. return 0;
  2037. }
  2038. static int rtl8152_resume(struct usb_interface *intf)
  2039. {
  2040. struct r8152 *tp = usb_get_intfdata(intf);
  2041. tp->rtl_ops.init(tp);
  2042. netif_device_attach(tp->netdev);
  2043. if (netif_running(tp->netdev)) {
  2044. rtl8152_set_speed(tp, AUTONEG_ENABLE,
  2045. tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
  2046. DUPLEX_FULL);
  2047. tp->speed = 0;
  2048. netif_carrier_off(tp->netdev);
  2049. set_bit(WORK_ENABLE, &tp->flags);
  2050. usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  2051. tasklet_enable(&tp->tl);
  2052. }
  2053. return 0;
  2054. }
  2055. static void rtl8152_get_drvinfo(struct net_device *netdev,
  2056. struct ethtool_drvinfo *info)
  2057. {
  2058. struct r8152 *tp = netdev_priv(netdev);
  2059. strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
  2060. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  2061. usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
  2062. }
  2063. static
  2064. int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  2065. {
  2066. struct r8152 *tp = netdev_priv(netdev);
  2067. if (!tp->mii.mdio_read)
  2068. return -EOPNOTSUPP;
  2069. return mii_ethtool_gset(&tp->mii, cmd);
  2070. }
  2071. static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2072. {
  2073. struct r8152 *tp = netdev_priv(dev);
  2074. return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
  2075. }
  2076. static struct ethtool_ops ops = {
  2077. .get_drvinfo = rtl8152_get_drvinfo,
  2078. .get_settings = rtl8152_get_settings,
  2079. .set_settings = rtl8152_set_settings,
  2080. .get_link = ethtool_op_get_link,
  2081. };
  2082. static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  2083. {
  2084. struct r8152 *tp = netdev_priv(netdev);
  2085. struct mii_ioctl_data *data = if_mii(rq);
  2086. int res = 0;
  2087. switch (cmd) {
  2088. case SIOCGMIIPHY:
  2089. data->phy_id = R8152_PHY_ID; /* Internal PHY */
  2090. break;
  2091. case SIOCGMIIREG:
  2092. data->val_out = r8152_mdio_read(tp, data->reg_num);
  2093. break;
  2094. case SIOCSMIIREG:
  2095. if (!capable(CAP_NET_ADMIN)) {
  2096. res = -EPERM;
  2097. break;
  2098. }
  2099. r8152_mdio_write(tp, data->reg_num, data->val_in);
  2100. break;
  2101. default:
  2102. res = -EOPNOTSUPP;
  2103. }
  2104. return res;
  2105. }
  2106. static const struct net_device_ops rtl8152_netdev_ops = {
  2107. .ndo_open = rtl8152_open,
  2108. .ndo_stop = rtl8152_close,
  2109. .ndo_do_ioctl = rtl8152_ioctl,
  2110. .ndo_start_xmit = rtl8152_start_xmit,
  2111. .ndo_tx_timeout = rtl8152_tx_timeout,
  2112. .ndo_set_rx_mode = rtl8152_set_rx_mode,
  2113. .ndo_set_mac_address = rtl8152_set_mac_address,
  2114. .ndo_change_mtu = eth_change_mtu,
  2115. .ndo_validate_addr = eth_validate_addr,
  2116. };
  2117. static void r8152b_get_version(struct r8152 *tp)
  2118. {
  2119. u32 ocp_data;
  2120. u16 version;
  2121. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
  2122. version = (u16)(ocp_data & VERSION_MASK);
  2123. switch (version) {
  2124. case 0x4c00:
  2125. tp->version = RTL_VER_01;
  2126. break;
  2127. case 0x4c10:
  2128. tp->version = RTL_VER_02;
  2129. break;
  2130. case 0x5c00:
  2131. tp->version = RTL_VER_03;
  2132. tp->mii.supports_gmii = 1;
  2133. break;
  2134. case 0x5c10:
  2135. tp->version = RTL_VER_04;
  2136. tp->mii.supports_gmii = 1;
  2137. break;
  2138. case 0x5c20:
  2139. tp->version = RTL_VER_05;
  2140. tp->mii.supports_gmii = 1;
  2141. break;
  2142. default:
  2143. netif_info(tp, probe, tp->netdev,
  2144. "Unknown version 0x%04x\n", version);
  2145. break;
  2146. }
  2147. }
  2148. static void rtl8152_unload(struct r8152 *tp)
  2149. {
  2150. u32 ocp_data;
  2151. if (tp->version != RTL_VER_01) {
  2152. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  2153. ocp_data |= POWER_CUT;
  2154. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  2155. }
  2156. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  2157. ocp_data &= ~RESUME_INDICATE;
  2158. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  2159. }
  2160. static void rtl8153_unload(struct r8152 *tp)
  2161. {
  2162. r8153_power_cut_en(tp, 1);
  2163. }
  2164. static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
  2165. {
  2166. struct rtl_ops *ops = &tp->rtl_ops;
  2167. int ret = -ENODEV;
  2168. switch (id->idVendor) {
  2169. case VENDOR_ID_REALTEK:
  2170. switch (id->idProduct) {
  2171. case PRODUCT_ID_RTL8152:
  2172. ops->init = r8152b_init;
  2173. ops->enable = rtl8152_enable;
  2174. ops->disable = rtl8152_disable;
  2175. ops->down = rtl8152_down;
  2176. ops->unload = rtl8152_unload;
  2177. ret = 0;
  2178. break;
  2179. case PRODUCT_ID_RTL8153:
  2180. ops->init = r8153_init;
  2181. ops->enable = rtl8153_enable;
  2182. ops->disable = rtl8152_disable;
  2183. ops->down = rtl8153_down;
  2184. ops->unload = rtl8153_unload;
  2185. ret = 0;
  2186. break;
  2187. default:
  2188. break;
  2189. }
  2190. break;
  2191. case VENDOR_ID_SAMSUNG:
  2192. switch (id->idProduct) {
  2193. case PRODUCT_ID_SAMSUNG:
  2194. ops->init = r8153_init;
  2195. ops->enable = rtl8153_enable;
  2196. ops->disable = rtl8152_disable;
  2197. ops->down = rtl8153_down;
  2198. ops->unload = rtl8153_unload;
  2199. ret = 0;
  2200. break;
  2201. default:
  2202. break;
  2203. }
  2204. break;
  2205. default:
  2206. break;
  2207. }
  2208. if (ret)
  2209. netif_err(tp, probe, tp->netdev, "Unknown Device\n");
  2210. return ret;
  2211. }
  2212. static int rtl8152_probe(struct usb_interface *intf,
  2213. const struct usb_device_id *id)
  2214. {
  2215. struct usb_device *udev = interface_to_usbdev(intf);
  2216. struct r8152 *tp;
  2217. struct net_device *netdev;
  2218. int ret;
  2219. if (udev->actconfig->desc.bConfigurationValue != 1) {
  2220. usb_driver_set_configuration(udev, 1);
  2221. return -ENODEV;
  2222. }
  2223. usb_reset_device(udev);
  2224. netdev = alloc_etherdev(sizeof(struct r8152));
  2225. if (!netdev) {
  2226. dev_err(&intf->dev, "Out of memory\n");
  2227. return -ENOMEM;
  2228. }
  2229. SET_NETDEV_DEV(netdev, &intf->dev);
  2230. tp = netdev_priv(netdev);
  2231. tp->msg_enable = 0x7FFF;
  2232. tp->udev = udev;
  2233. tp->netdev = netdev;
  2234. tp->intf = intf;
  2235. ret = rtl_ops_init(tp, id);
  2236. if (ret)
  2237. goto out;
  2238. tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
  2239. INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
  2240. netdev->netdev_ops = &rtl8152_netdev_ops;
  2241. netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
  2242. netdev->features |= NETIF_F_IP_CSUM;
  2243. netdev->hw_features = NETIF_F_IP_CSUM;
  2244. SET_ETHTOOL_OPS(netdev, &ops);
  2245. tp->mii.dev = netdev;
  2246. tp->mii.mdio_read = read_mii_word;
  2247. tp->mii.mdio_write = write_mii_word;
  2248. tp->mii.phy_id_mask = 0x3f;
  2249. tp->mii.reg_num_mask = 0x1f;
  2250. tp->mii.phy_id = R8152_PHY_ID;
  2251. tp->mii.supports_gmii = 0;
  2252. r8152b_get_version(tp);
  2253. tp->rtl_ops.init(tp);
  2254. set_ethernet_addr(tp);
  2255. ret = alloc_all_mem(tp);
  2256. if (ret)
  2257. goto out;
  2258. usb_set_intfdata(intf, tp);
  2259. ret = register_netdev(netdev);
  2260. if (ret != 0) {
  2261. netif_err(tp, probe, netdev, "couldn't register the device\n");
  2262. goto out1;
  2263. }
  2264. netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
  2265. return 0;
  2266. out1:
  2267. usb_set_intfdata(intf, NULL);
  2268. out:
  2269. free_netdev(netdev);
  2270. return ret;
  2271. }
  2272. static void rtl8152_disconnect(struct usb_interface *intf)
  2273. {
  2274. struct r8152 *tp = usb_get_intfdata(intf);
  2275. usb_set_intfdata(intf, NULL);
  2276. if (tp) {
  2277. set_bit(RTL8152_UNPLUG, &tp->flags);
  2278. tasklet_kill(&tp->tl);
  2279. unregister_netdev(tp->netdev);
  2280. tp->rtl_ops.unload(tp);
  2281. free_all_mem(tp);
  2282. free_netdev(tp->netdev);
  2283. }
  2284. }
  2285. /* table of devices that work with this driver */
  2286. static struct usb_device_id rtl8152_table[] = {
  2287. {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
  2288. {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
  2289. {USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
  2290. {}
  2291. };
  2292. MODULE_DEVICE_TABLE(usb, rtl8152_table);
  2293. static struct usb_driver rtl8152_driver = {
  2294. .name = MODULENAME,
  2295. .id_table = rtl8152_table,
  2296. .probe = rtl8152_probe,
  2297. .disconnect = rtl8152_disconnect,
  2298. .suspend = rtl8152_suspend,
  2299. .resume = rtl8152_resume,
  2300. .reset_resume = rtl8152_resume,
  2301. };
  2302. module_usb_driver(rtl8152_driver);
  2303. MODULE_AUTHOR(DRIVER_AUTHOR);
  2304. MODULE_DESCRIPTION(DRIVER_DESC);
  2305. MODULE_LICENSE("GPL");