r8152.c 81 KB

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